]> git.somenet.org - irc/bugbot.git/blob - BotModules/Infobot.pl
some old base
[irc/bugbot.git] / BotModules / Infobot.pl
1 #!/usr/bin/perl -w
2 ######################################
3 # Infobot Factoid Import/Export Tool #
4 ######################################
5
6 use strict;
7 use AnyDBM_File;
8 use Fcntl;
9
10 if (not @ARGV == 2) {
11     &use();
12 } else {
13     my $command = shift @ARGV;
14     my $filename = shift @ARGV;
15     if ($command eq '-d') {
16         &dump($filename);
17     } elsif ($command eq '-i') {
18         &import($filename);
19     } else {
20         &use();
21     }
22 }
23
24 sub use {
25     print "\n";
26     print "  usage:  $0 -d dbname\n";
27     print "          prints out an ascii flat file of the database listed.\n";
28     print "          dbname should be the basename of the db, e.g.\n";
29     print "            $0 -d ../factoids-is > is.fact\n";
30     print "            $0 -d ../factoids-are > are.fact\n";
31     print "\n";
32     print "          $0 -i dbname\n";
33     print "          imports an ascii flat file into the database listed.\n";
34     print "          dbname should be the basename of the db, e.g.\n";
35     print "            $0 -i ../factoids-is < chemicals.fact\n";
36     print "            $0 -i ../factoids-is < is.fact\n";
37     print "            $0 -i ../factoids-are < are.fact\n";
38     print "\n";
39     exit(1);
40 }
41
42 sub dump {
43     my %db;
44     tie(%db, 'AnyDBM_File', shift, O_RDONLY, 0666);
45     while (my ($key, $val) = each %db) {
46         chomp $val;
47         print "$key => $val\n";
48     }
49 }
50
51 sub import {
52     my %db;
53     tie(%db, 'AnyDBM_File', shift, O_WRONLY|O_CREAT, 0666);
54     while (<STDIN>) {
55         chomp;
56         unless (m/\s*(.+?)\s+=(?:is=|are=)?>\s+(.+?)\s*$/o) {
57             m/\s*(.+?)\s+(?:is|are)?\s+(.+?)\s*$/o;
58         }
59         if (length($1) and length($2)) {
60             if (defined($db{$1})) {
61                 if (not $db{$1} =~ m/^(|.*\|)\Q$2\E(|.*\|)$/s) {
62                     $db{$1} .= "|$2";
63                 }
64             } else {
65                 $db{$1} = $2;
66             }
67         }
68     }
69 }