[BACK]Return to botnow.pl CVS log [TXT][DIR] Up to [local] / botnow

Annotation of botnow/botnow.pl, Revision 1.3

1.1       bountyht    1: #!/usr/bin/perl
                      2:
                      3: use strict;
                      4: no strict 'refs';
                      5: use warnings;
                      6: use IO::Socket;
                      7: use IO::Select;
                      8: use OpenBSD::Pledge;
                      9: use OpenBSD::Unveil;
                     10:
                     11: my $confpath = "botnow.conf";
                     12: our %conf;
                     13: foreach my $line (readarray($confpath)) {
                     14:        if ($line =~ /^#/ or $line =~ /^\s*$/) { # skip comments and whitespace
                     15:                next;
                     16:        } elsif ($line =~ /^([-_a-zA-Z0-9]+)\s*=\s*([[:print:]]+)$/) {
                     17:                $conf{$1} = $2;
                     18:        } else {
                     19:                die "ERROR: botnow.conf format invalid: $line";
                     20:        }
                     21: }
                     22:
                     23: # Name of local network
                     24: $conf{localnet} = $conf{localnet} || "ircnow";
                     25:
                     26: # Internal IPv4 address and plaintext port
                     27: $conf{host} = $conf{host} || "127.0.0.1";
                     28: $conf{port} = $conf{port} || 1337;
                     29:
                     30: # Bouncer hostname
                     31: chomp($conf{hostname} = $conf{hostname} || `hostname`);
                     32:
                     33: # External IPv4 address, plaintext and ssl port
                     34: $conf{ip4} = $conf{ip4} or die "ERROR: botnow.conf: ip4";
1.2       bountyht   35: $conf{ip6} = $conf{ip6} or die "ERROR: botnow.conf: ip6";
                     36: $conf{ip6subnet} = $conf{ip6subnet} or die "ERROR: botnow.conf: ip6subnet";
1.1       bountyht   37: $conf{plainport} = $conf{plainport} || 1337;
                     38: $conf{sslport} = $conf{sslport} || 31337;
                     39:
                     40: # Nick and password of bot -- Make sure to add to oper block
                     41: $conf{nick} = $conf{nick} || "botnow";
                     42: $conf{pass} = $conf{pass} or die "ERROR: botnow.conf: pass";
                     43:
                     44: # Comma-separated list of channels for requesting bouncers
                     45: $conf{chans} = $conf{chans} || "#ircnow";
                     46:
                     47: #Join chans on localnet?
                     48: $conf{localchans} = defined($conf{localchans}) && ($conf{localchans} =~ /^true/i);
                     49:
                     50: # Number of words in password
                     51: $conf{passlength} = $conf{passlength} || 3;
                     52:
                     53: # Mail from address
                     54: if (!defined($conf{mailname})) {
                     55:        if ($conf{mailfrom} =~ /^([^@]+)@/) {
                     56:                $conf{mailname} = $1 or die "ERROR: botnow.conf mailname";
                     57:        }
                     58: }
                     59:
                     60: # ZNC install directory
                     61: $conf{zncdir} = $conf{zncdir} || "/home/znc/home/znc";
                     62:
                     63: # NSD zone dir
                     64: $conf{zonedir} = $conf{zonedir} || "/var/nsd/zones/master/";
                     65:
                     66: # Network Interface Config File
                     67: $conf{hostnameif} = $conf{hostnameif} || "/etc/hostname.vio0";
                     68:
                     69: # Verbosity: 0 (no errors), 1 (errors), 2 (warnings), 3 (diagnostic)
                     70: use constant {
                     71:        NONE => 0,
                     72:        ERRORS => 1,
                     73:        WARNINGS => 2,
                     74:        ALL => 3,
                     75: };
                     76: $conf{verbose} = $conf{verbose} || ERRORS;
                     77:
                     78: # Terms of Service; don't edit lines with the word EOF
1.3     ! bountyht   79: $conf{terms} = $conf{terms} || "IRCNow: Of the User, By the User, For the User. Rules: no porn, no illegal drugs, no gambling, no slander, no warez, no promoting violence, no spam, illegal cracking, or DDoS. Only one account per person. Don't share passwords. Full terms: https://ircnow.org/terms.php";
1.1       bountyht   80:
                     81: $conf{ipv6path} = "ipv6s"; # ipv6 file path
                     82: $conf{netpath} = "networks"; # networks file path
                     83: $conf{expires} = $conf{expires} || 1800; # time before captcha expires
                     84:
                     85: if(defined($conf{die})) { die $conf{die}; }
                     86:
                     87: my @modules;
                     88: if (defined($conf{modules})) {
                     89:        @modules = split(/\s+/, $conf{modules});
                     90: }
                     91: use lib './';
                     92: foreach my $mod (@modules) {
                     93:        require "$mod.pm";
                     94: }
                     95: foreach my $mod (@modules) {
                     96:        my $init = "${mod}::init";
                     97:        $init->();
                     98: }
                     99:
                    100: our @networks;
                    101: my @bots;
                    102: my @months = qw( Jan Feb Mar Apr May Jun Jul Aug Sep Oct Nov Dec );
                    103: my @days = qw(Sun Mon Tue Wed Thu Fri Sat Sun);
                    104: my @chans = split /[,\s]+/m, $conf{chans};
                    105: my @teamchans;
                    106: if (defined($conf{teamchans})) { @teamchans = split /[,\s]+/m, $conf{teamchans}; }
                    107: my $call;
                    108: my $botnick = $conf{nick};
                    109: my $host = $conf{host};
                    110: my $port = $conf{port};
                    111: my $pass = $conf{pass};
                    112: my $localnet = $conf{localnet};
                    113: my $staff = $conf{staff};
                    114: my @stafflist = split(/ /,$staff);
                    115: my $verbose = $conf{verbose};
                    116: my $ipv6path = $conf{ipv6path};
                    117: my $netpath = $conf{netpath};
                    118: my $expires = $conf{expires};
                    119: my $localchans = $conf{localchans};
                    120:
                    121: unveil("./", "r") or die "Unable to unveil $!";
                    122: unveil("$confpath", "r") or die "Unable to unveil $!";
                    123: unveil("$netpath", "r") or die "Unable to unveil $!";
                    124: unveil("$ipv6path", "rwc") or die "Unable to unveil $!";
                    125: unveil() or die "Unable to lock unveil $!";
                    126:
                    127: #dns and inet for sockets, proc and exec for figlet
                    128: #rpath for reading file, wpath for writing file, cpath for creating path
                    129: #flock, fattr for sqlite
                    130: pledge( qw(stdio rpath wpath cpath inet dns proc exec flock fattr) ) or die "Unable to pledge: $!";
                    131:
                    132: # Read from filename and return array of lines without trailing newlines
                    133: sub readarray {
                    134:        my ($filename) = @_;
                    135:        open(my $fh, '<', $filename) or die "Could not read file '$filename' $!";
                    136:        chomp(my @lines = <$fh>);
                    137:        close $fh;
                    138:        return @lines;
                    139: }
                    140:
                    141: # Read from filename and return as string
                    142: sub readstr {
                    143:        my ($filename) = @_;
                    144:        open my $fh, '<', $filename or die "Could not read file '$filename' $!";
                    145:        my $str = do { local $/; <$fh> };
                    146:        close $fh;
                    147:        return $str;
                    148: }
                    149:
                    150: # Write str to filename
                    151: sub writefile {
                    152:        my ($filename, $str) = @_;
                    153:        open(my $fh, '>', "$filename") or die "Could not write to $filename";
                    154:        print $fh $str;
                    155:        close $fh;
                    156: }
                    157:
                    158: # Append str to filename
                    159: sub appendfile {
                    160:        my ($filename, $str) = @_;
                    161:        open(my $fh, '>>', "$filename") or die "Could not append to $filename";
                    162:        print $fh $str;
                    163:        close $fh;
                    164: }
                    165:
                    166: # Return list of networks from filename
                    167: # To add multiple servers for a single network, simply create a new entry with
                    168: # the same net name; znc ignores addnetwork commands when a network already exists
                    169: sub readnetworks {
                    170:        my ($filename) = @_;
                    171:        my @lines = readarray($filename);
                    172:        my @networks;
                    173:        foreach my $line (@lines) {
                    174:                if ($line =~ /^#/ or $line =~ /^\s*$/) { # skip comments and whitespace
                    175:                        next;
                    176:                } elsif ($line =~ /^\s*([-a-zA-Z0-9]+)\s*([-_.:a-zA-Z0-9]+)\s*(~|\+)?([0-9]+)\s*$/) {
                    177:                        my ($name, $server, $port) = ($1, $2, $4);
                    178:                        my $trustcerts;
                    179:                        if (!defined($3)) {
                    180:                                $trustcerts = 0;
                    181:                        } elsif ($3 eq "~") { # Use SSL but trust all certs
                    182:                                $port = "+".$port;
                    183:                                $trustcerts = 1;
                    184:                        } else { # Use SSL and verify certs
                    185:                                $port = "+".$port;
                    186:                                $trustcerts = 0;
                    187:                        }
                    188:                        push(@networks, {"name" => $name, "server" => $server, "port" => $port, "trustcerts" => $trustcerts });
                    189:                } else {
                    190:                        die "network format invalid: $line\n";
                    191:                }
                    192:        }
                    193:        return @networks;
                    194: }
                    195:
                    196: @networks = readnetworks($netpath);
                    197:
                    198: # networks must be sorted to avoid multiple connections
                    199: @networks = sort @networks;
                    200:
                    201: # create sockets
                    202: my $sel = IO::Select->new( );
                    203: my $lastnet = "";
                    204: foreach my $network (@networks) {
                    205:        # avoid duplicate connections
                    206:        if ($lastnet eq $network->{name}) { next; }
                    207:        $lastnet = $network->{name};
                    208:        my $socket = IO::Socket::INET->new(PeerAddr=>$host, PeerPort=>$port, Proto=>'tcp', Timeout=>'300') || print "Failed to establish connection\n";
                    209:        $sel->add($socket);
                    210:        my $bot = {("sock" => $socket), %$network};
                    211:        push(@bots, $bot);
                    212:        putserv($bot, "NICK $botnick");
                    213:        putserv($bot, "USER $botnick * * :$botnick");
                    214: }
                    215:
                    216: while(my @ready = $sel->can_read) {
                    217:        my ($bot, $response);
                    218:        my ($sender, $val);
                    219:        foreach my $socket (@ready) {
                    220:                foreach my $b (@bots) {
                    221:                        if($socket == $b->{sock}) {
                    222:                                $bot = $b;
                    223:                                last;
                    224:                        }
                    225:                }
                    226:                if (!defined($response = <$socket>)) {
                    227:                        debug(ERRORS, "ERROR ".$bot->{name}." has no response:");
                    228:                        next;
                    229:                }
                    230:                if ($response =~ /^PING :(.*)\r\n$/i) {
                    231:                        putserv($bot, "PONG :$1");
                    232:                } elsif ($response =~ /^:irc.znc.in (.*) (.*) :(.*)\r\n$/) {
                    233:                        my ($type, $target, $text) = ($1, $2, $3);
                    234:                        if ($type eq "001" && $target =~ /^$botnick.?$/ && $text eq "Welcome to ZNC") {
                    235:                        } elsif ($type eq "NOTICE" && $target =~ /^$botnick.?$/ && $text eq "*** To connect now, you can use /quote PASS <username>:<password>, or /quote PASS <username>/<network>:<password> to connect to a specific network.") {
                    236:                        } elsif ($type eq "NOTICE" && $target =~ /^$botnick.?$/ && $text eq "*** You need to send your password. Configure your client to send a server password.") {
                    237:                        } elsif ($type eq "464" && $target =~ /^$botnick.?$/ && $text eq "Password required") {
                    238:                                putserv($bot, "PASS $botnick/$bot->{name}:$pass");
                    239:                                if ($bot->{name} =~ /^$localnet$/i) {
                    240:                                        putserv($bot, "OPER $botnick $pass");
                    241:                                        putserv($bot, "PRIVMSG *status :LoadMod --type=user controlpanel");
                    242:                                        putserv($bot, "PRIVMSG *controlpanel :get Admin $botnick");
                    243:                                        putserv($bot, "PRIVMSG *controlpanel :get Nick cloneuser");
                    244:                                        foreach my $chan (@teamchans) {
                    245:                                                putserv($bot, "JOIN $chan");
                    246:                                        }
                    247:                                }
                    248:                                if ($bot->{name} !~ /^$localnet$/i or $localchans) {
                    249:                                        foreach my $chan (@chans) {
                    250:                                                putserv($bot, "JOIN $chan");
                    251:                                        }
                    252:                                }
                    253:                        } elsif ($type eq "464" && $target =~ /^$botnick.?$/ && $text eq "Invalid Password") {
                    254:                                die "ERROR: Wrong Username/Password: $bot->{name}";
                    255:                        } else {
                    256:                                debug(ERRORS, "Unexpected bncnow.pl 257: type: $type, target: $target, text: $text");
                    257:                        }
                    258:                } elsif($response =~ /^:(([^!]+)!([^@]+@[^@ ]+)) PRIVMSG ([^ ]+) :(.*)\r\n$/i) {
                    259:                        my ($hostmask, $nick, $host, $target, $text) = ($1, $2, $3, $4, $5);
                    260:                        if ($hostmask eq '*status!znc@znc.in' && $target =~ /^$botnick.?$/) {
                    261:                                if ($text =~ /Network ([[:ascii:]]+) doesn't exist./) {
                    262:                                        debug(ERRORS, "nonexistent: $1");
                    263:                                } elsif ($text eq "You are currently disconnected from IRC. Use 'connect' to reconnect.") {
                    264:                                        debug(ERRORS, "disconnected: $bot->{name}");
                    265:                                } elsif ($text =~ /Unable to load module (.*): Module (.*) already loaded./) {
                    266:                                        debug(ALL, "Module $1 already loaded\n");
                    267:                                } elsif ($text =~ /^Disconnected from IRC.*$/) {
                    268:                                        debug(ERRORS, "$bot->{name}: $text");
                    269:                                } elsif ($text =~ /^|/) {
                    270:                                        debug(ERRORS, "$bot->{name}: $text");
                    271:                                } else {
                    272:                                        debug(ERRORS, "Unexpected bncnow.pl 273: $response");
                    273:                                }
                    274:                        } elsif ($text =~ /^!([[:graph:]]+)\s*(.*)/) {
                    275:                                my ($cmd, $text) = ($1, $2);
                    276:                                my $hand = $staff; # TODO fix later
                    277:                                if ($target =~ /^#/) {
                    278:                                        foreach my $c (@{$call->{pub}}) {
                    279:                                                if ($cmd eq $c->{cmd}) {
                    280:                                                        my $proc = $c->{proc};
                    281:                                                        $proc->($bot, $nick, $host, $hand, $target, $text);
                    282:                                                }
                    283:                                        }
                    284:                                } else {
                    285:                                        foreach my $c (@{$call->{msg}}) {
                    286:                                                if ($cmd eq $c->{cmd}) {
                    287:                                                        my $proc = $c->{proc};
                    288:                                                        $proc->($bot, $nick, $host, $hand, $text);
                    289:                                                }
                    290:                                        }
                    291:                                }
                    292:                        } else {
                    293:                                my $hand = $staff; # TODO fix later
                    294:                                if ($target =~ /^#/) {
                    295:                                        foreach my $c (@{$call->{pubm}}) {
                    296:                                                my $proc = $c->{proc};
                    297:                                                $proc->($bot, $nick, $host, $hand, $target, $text);
                    298:                                        }
                    299:                                } else {
                    300:                                        foreach my $c (@{$call->{msgm}}) {
                    301:                                                my $proc = $c->{proc};
                    302:                                                $proc->($bot, $nick, $host, $hand, $text);
                    303:                                        }
                    304:                                }
                    305:                        }
                    306:                        debug(ALL, "$hostmask $target $text");
                    307:                } elsif($response =~ /^:([^ ]+) NOTICE ([^ ]+) :(.*)\r\n$/i) {
                    308:                        my ($hostmask, $target, $text) = ($1, $2, $3);
                    309:                        if ($hostmask =~ /([^!]+)!([^@]+@[^@ ]+)/) {
                    310:                                my ($nick, $host) = ($1, $2);
                    311:                                my $hand = $staff; # TODO fix later
                    312:                                foreach my $c (@{$call->{notc}}) {
                    313:                                        #       if ($text eq $c->{mask}) { # TODO fix later
                    314:                                        my $proc = $c->{proc};
                    315:                                        $proc->($bot, $nick, $host, $hand, $text, $target);
                    316:                                        #       }
                    317:                                }
                    318:                                # TODO use CTCR
                    319:                                # CTCP replies
                    320:                                if ($hostmask ne '*status!znc@znc.in') {
                    321:                                        if ($text =~ /^&(PING|VERSION|TIME|USERINFO) (.*)&$/i) {
                    322:                                                my ($key, $val) = ($1, $2);
                    323:                                                my $id = SQLite::id("irc", "nick", $nick, $expires);
                    324:                                                SQLite::set("irc", "id", $id, "ctcp".lc($key), $val);
                    325:                                                SQLite::set("irc", "id", $id, "localtime", time());
                    326:                                        }
                    327:                                }
                    328:                        }
                    329:                        debug(ALL, "$hostmask NOTICE $target $text");
                    330: #:portlane.se.quakenet.org NOTICE guava :Highest connection count: 1541 (1540 clients)
                    331: #:portlane.se.quakenet.org NOTICE guava :on 2 ca 2(4) ft 20(20) tr
                    332:                } elsif($response =~ /^:([^ ]+) MODE ([^ ]+) ([^ ]+)\s*(.*)\r\n$/i) {
                    333:                        my ($hostmask, $chan, $change, $targets) = ($1, $2, $3, $4);
                    334:                        if ($hostmask =~ /([^!]+)!([^@]+@[^@ ]+)/) {
                    335:                                my ($nick, $host) = ($1, $2);
                    336:                                my $hand = $staff; # TODO fix later
                    337:                                foreach my $c (@{$call->{mode}}) {
                    338:                                        # TODO filter by mask
                    339:                                        my $proc = $c->{proc};
                    340:                                        $proc->($bot, $nick, $host, $hand, $chan, $change, $targets);
                    341:                                }
                    342:                        }
                    343:                        debug(ALL, "$hostmask MODE $chan $change $targets");
                    344: #:guava!guava@guava.guava.ircnow.org MODE guava :+Ci
                    345: #:ChanServ!services@services.irc.ircnow.org MODE #testing +q jrmu
                    346: #:jrmu!jrmu@jrmu.staff.ircnow.org MODE #testing +o jrmu
                    347: #Unexpected bncnow.pl 460: :irc.guava.ircnow.org MODE guava :+o
                    348:                } elsif($response =~ /^:(([^!]+)!([^@]+@[^@ ]+)) JOIN :?(.*)\r\n$/i) {
                    349:                        my ($hostmask, $nick, $host, $chan) = ($1, $2, $3, $4);
                    350:                        my $hand = $staff; # TODO fix later
                    351:                        foreach my $c (@{$call->{join}}) {
                    352:                                my $proc = $c->{proc};
                    353:                                $proc->($bot, $nick, $host, $hand, $chan);
                    354:                        }
                    355:                        debug(ALL, "$hostmask JOIN $chan");
                    356: #:jrmu!jrmu@jrmu.staff.ircnow.org JOIN :#testing
                    357:                } elsif($response =~ /^:(([^!]+)!([^@]+@[^@ ]+)) PART ([^ ]+) :(.*)\r\n$/i) {
                    358:                        my ($hostmask, $nick, $host, $chan, $text) = ($1, $2, $3, $4, $5);
                    359:                        my $hand = $staff; # TODO fix later
                    360:                        foreach my $c (@{$call->{part}}) {
                    361:                                #       if ($text eq $c->{mask}) { # TODO fix later
                    362:                                my $proc = $c->{proc};
                    363:                                $proc->($bot, $nick, $host, $hand, $chan, $text);
                    364:                                #       }
                    365:                        }
                    366:                        debug(ALL, "$hostmask PART $chan :$text");
                    367: #:jrmu!jrmu@jrmu.staff.ircnow.org PART #testing :
                    368:                } elsif($response =~ /^:(([^!]+)!([^@]+@[^@ ]+)) KICK (#[^ ]+) ([^ ]+) :(.*)\r\n$/i) {
                    369:                        my ($hostmask, $nick, $host, $chan, $kicked, $text) = ($1, $2, $3, $4, $5, $6);
                    370:                        my $hand = $staff; # TODO fix later
                    371:                        foreach my $c (@{$call->{kick}}) {
                    372:                                #       if ($text eq $c->{mask}) { # TODO fix later
                    373:                                my $proc = $c->{proc};
                    374:                                $proc->($bot, $nick, $host, $hand, $chan, $text);
                    375:                                #       }
                    376:                        }
                    377:                        debug(ALL, "$hostmask KICK $chan $kicked :$text");
                    378: #jrmu!jrmu@jrmu.users.undernet.org KICK #ircnow guava :this is a test
                    379:                } elsif($response =~ /^:(([^!]+)!([^@]+@[^@ ]+)) NICK :?(.*)\r\n$/i) {
                    380:                        my ($hostmask, $nick, $host, $text) = ($1, $2, $3, $4);
                    381:                        debug(ALL, "$hostmask NICK $text");
                    382: #:Fly0nDaWaLL|dal!psybnc@do.not.h4ck.me NICK :nec|dal
                    383:                } elsif($response =~ /^:(([^!]+)!([^@]+@[^@ ]+)) QUIT :(.*)\r\n$/i) {
                    384:                        my ($hostmask, $nick, $host, $text) = ($1, $2, $3, $4);
                    385:                        debug(ALL, "$hostmask QUIT :$text");
                    386: #:Testah!~sid268081@aa38a510 QUIT :Client closed connection
                    387:                } elsif($response =~ /^NOTICE AUTH :(.*)\r\n$/i) {
                    388:                        my ($text) = ($1);
                    389:                        debug(ALL, "NOTICE AUTH: $text");
                    390: #NOTICE AUTH :*** Looking up your hostname
                    391: #NOTICE AUTH: *** Looking up your hostname
                    392: #NOTICE AUTH: *** Checking Ident
                    393: #NOTICE AUTH: *** Got ident response
                    394: #NOTICE AUTH: *** Found your hostname
                    395:                } elsif ($response =~ /^:([[:graph:]]+) (\d\d\d) $botnick.? :?(.*)\r?\n?\r$/i) {
                    396:                        my ($server, $code, $text) = ($1, $2, $3);
                    397:                        if ($code =~ /^001$/) { # Server Info
                    398:                                debug(ERRORS, "connected: $bot->{name}");
                    399:                        } elsif ($code =~ /^0\d\d$/) { # Server Info
                    400:                                debug(ALL, "$server $code $text");
                    401:                        } elsif ($code =~ /^2\d\d$/) { # Server Stats
                    402:                                debug(ALL, "$server $code $text");
                    403:                        } elsif ($code == 301 && $text =~ /^([-_\|`a-zA-Z0-9]+) :([[:graph:]]+)/) {
                    404:                                debug(ALL, "$text");
                    405:                        } elsif ($code == 307 && $text =~ /^([-_\|`a-zA-Z0-9]+) (.*)/) {
                    406:                                my ($sender, $key) = ($1, "registered");
                    407:                                $val = $2 eq ":is a registered nick" ? "True" : "$2";
                    408:                                my $id = SQLite::id("irc", "nick", $sender, $expires);
                    409:                                SQLite::set("irc", "id", $id, "identified", $val);
                    410:                                debug(ALL, "$key: $val");
                    411:                        } elsif ($code == 311 && $text =~ /^([-_\|`a-zA-Z0-9]+) ([^:]+)\s+([^:]+) \* :([^:]*)/) {
                    412:                                my ($sender, $key, $val) = ($1, "hostmask", "$1\!$2\@$3");
                    413:                                my $id = SQLite::id("irc", "nick", $sender, $expires);
                    414:                                SQLite::set("irc", "id", $id, $key, $val);
                    415:                                debug(ALL, "$key: $val");
                    416:                        } elsif ($code == 312 && $text =~ /^([-_\|`a-zA-Z0-9]+) ([^:]+) :([^:]+)/) {
                    417:                                my ($sender, $key, $val) = ($1, "server", $2);
                    418:                                my $id = SQLite::id("irc", "nick", $sender, $expires);
                    419:                                SQLite::set("irc", "id", $id, $key, $val);
                    420:                                debug(ALL, "$key: $val");
                    421:                        } elsif ($code == 313 && $text =~ /^([-_\|`a-zA-Z0-9]+) :?(.*)/) {
                    422:                                my ($sender, $key, $val) = ($1, "oper", ($2 eq "is an IRC operator" ? "True" : "$2"));
                    423:                                my $id = SQLite::id("irc", "nick", $sender, $expires);
                    424:                                SQLite::set("irc", "id", $id, $key, $val);
                    425:                                debug(ALL, "$key: $val");
                    426:                        } elsif ($code == 315 && $text =~ /^([-_\|`a-zA-Z0-9]+) :End of \/?WHO(IS)? list/) {
                    427:                                debug(ALL, "End of WHOIS");
                    428:                        } elsif ($code == 317 && $text =~ /^([-_\|`a-zA-Z0-9]+) (\d+) (\d+) :?(.*)/) {
                    429:                                ($sender, my $idle, my $epochtime) = ($1, $2, $3);
                    430:                                my $id = SQLite::id("irc", "nick", $sender, $expires);
                    431:                                SQLite::set("irc", "id", $id, "idle", $idle);
                    432: #                              SQLite::set("irc", "id", $id, "epochtime", time());
                    433:                                debug(ALL, "idle: $idle, epochtime: $epochtime");
                    434:                        } elsif ($code == 318 && $text =~ /^([-_\|`a-zA-Z0-9]+) :End of \/?WHOIS list/) {
                    435:                                debug(ALL, "End of WHOIS");
                    436:                        } elsif ($code == 319 && $text =~ /^([-_\|`a-zA-Z0-9]+) :(.*)/) {
                    437:                                my ($sender, $key, $val) = ($1, "chans", $2);
                    438:                                my $id = SQLite::id("irc", "nick", $sender, $expires);
                    439:                                SQLite::set("irc", "id", $id, $key, $val);
                    440:                                debug(ALL, "$key: $val");
                    441:                        } elsif ($code == 330 && $text =~ /^([-_\|`a-zA-Z0-9]+) ([-_\|`a-zA-Z0-9]+) :?(.*)/) {
                    442:                                my ($sender, $key, $val) = ($1, "identified", ($3 eq "is logged in as" ? "True" : $2));
                    443:                                my $id = SQLite::id("irc", "nick", $sender, $expires);
                    444:                                SQLite::set("irc", "id", $id, $key, $val);
                    445:                                debug(ALL, "$key: $val");
                    446:                        } elsif ($code == 338 && $text =~ /^([-_\|`a-zA-Z0-9]+) ([0-9a-fA-F:.]+) :actually using host/) {
                    447:                                my ($sender, $key, $val) = ($1, "ip", $2);
                    448:                                my $id = SQLite::id("irc", "nick", $sender, $expires);
                    449:                                SQLite::set("irc", "id", $id, $key, $val);
                    450:                                debug(ALL, "$key: $val");
                    451:        #Unexpected: efnet.port80.se 338 jrmu 206.253.167.44 :actually using host
                    452:                        } elsif ($code == 378 && $text =~ /^([-_\|`a-zA-Z0-9]+) :is connecting from ([^ ]+)\s*([0-9a-fA-F:.]+)?/) {
                    453:                                my ($sender, $key, $val) = ($1, "ip", $3);
                    454:                                my $id = SQLite::id("irc", "nick", $sender, $expires);
                    455:                                SQLite::set("irc", "id", $id, $key, $val);
                    456:                                debug(ALL, "$key: $val");
                    457:                        } elsif ($code == 671 && $text =~ /^([-_\|`a-zA-Z0-9]+) :is using a secure connection/) {
                    458:                                my ($sender, $key, $val) = ($1, "ssl", "True");
                    459:                                my $id = SQLite::id("irc", "nick", $sender, $expires);
                    460:                                SQLite::set("irc", "id", $id, $key, $val);
                    461:                                debug(ALL, "$key: $val");
                    462:                        } elsif ($code =~ /^332$/) { # Topic
                    463:                #               print "$text\r\n";
                    464:                        } elsif ($code =~ /^333$/) { #
                    465:                #               print "$server $text\r\n";
                    466:                #karatkievich.freenode.net 333 #ircnow jrmu!znc@206.253.167.44 1579277253
                    467:                        } elsif ($code =~ /^352$/) { # Hostmask
                    468: #:datapacket.hk.quakenet.org 352 * znc guava.guava.ircnow.org *.quakenet.org guava H :0 guava
                    469:                #               print "$server $code $text\r\n";
                    470:                        } elsif ($code =~ /^353$/) { # Names
                    471:                #               print "$server $code $text\r\n";
                    472:                        } elsif ($code =~ /^366$/) { # End of names
                    473:                #               print "$server $code $text\r\n";
                    474:                        } elsif ($code =~ /^37\d$/) { # MOTD
                    475:                #               print "$server $code $text\r\n";
                    476:                        } elsif ($code =~ /^381$/) { # IRC Operator Verified
                    477:                #               print "IRC Oper Verified\r\n";
                    478:                        } elsif ($code =~ /^401$/) { # IRC Operator Verified
                    479:                #               print "IRC Oper Verified\r\n";
                    480:                        } elsif ($code =~ /^403$/) { # No such channel
                    481:                #               debug(ERRORS, "$text");
                    482:                        } elsif ($code =~ /^422$/) { # MOTD missing
                    483:                #               print "$server $code $text\r\n";
                    484:                        } elsif ($code =~ /^396$/) { # Display hostname
                    485:                #               print "$server $code $text\r\n";
                    486: #Unexpected bncnow.pl 454: irc.guava.ircnow.org 396 guava.guava.ircnow.org :is your displayed hostname now
                    487:                        } elsif ($code =~ /^464$/) { # Invalid password for oper
                    488:                                foreach my $chan (@teamchans) {
                    489:                                        putserv($bot, "PRIVMSG $chan :$botnick oper password failed; the bot will be unable to view uncloaked IP addresses");
                    490:                                }
                    491:                        } elsif ($code =~ /^477$/) { # Can't join channel
                    492:                                foreach my $chan (@teamchans) {
                    493:                                        putserv($bot, "PRIVMSG $chan :ERROR: $botnick on $server: $text");
                    494:                                }
                    495:                        } elsif ($code == 716 && $text =~ /^([-_\|`a-zA-Z0-9]+) :is in \+g mode \(server-side ignore.\)/) {
                    496:                                debug(ALL, "$text");
                    497:                        } else {
                    498:                                debug(ERRORS, "Unexpected bncnow.pl 454: $server $code $text");
                    499:                        }
                    500:                } else {
                    501:                        debug(ERRORS, "Unexpected bncnow.pl 460: $response");
                    502:                }
                    503:        }
                    504: }
                    505:
                    506: sub putserv {
                    507:        my( $bot, $text )=@_;
                    508:        my $socket = $bot->{sock};
                    509:        if ($text =~ /^([^:]+):([[:ascii:]]*)$/m) {
                    510:                my ($cmd, $line) = ($1, $2);
                    511:                my @lines = split /\r?\n/m, $line;
                    512:                foreach my $l (@lines) {
                    513:                        print $socket "$cmd:$l\r\n";
                    514:                }
                    515:        } else {
                    516:                print $socket "$text\r\n";
                    517:        }
                    518: }
                    519:
                    520: sub putserv {
                    521:        my( $bot, $text )=@_;
                    522:        my $socket = $bot->{sock};
                    523:        if ($text =~ /^([^:]+):([[:ascii:]]*)$/m) {
                    524:                my ($cmd, $line) = ($1, $2);
                    525:                my @lines = split /\r?\n/m, $line;
                    526:                foreach my $l (@lines) {
                    527:                        print $socket "$cmd:$l\r\n";
                    528:                }
                    529:        } else {
                    530:                print $socket "$text\r\n";
                    531:        }
                    532: }
                    533:
                    534: sub putservlocalnet {
                    535:        my( $bot, $text )=@_;
                    536:        my $botlocalnet;
                    537:        foreach my $b (@bots) {
                    538:                if($b->{name} =~ /^$localnet$/i) {
                    539:                        $botlocalnet = $b;
                    540:                        last;
                    541:                }
                    542:        }
                    543:        putserv($botlocalnet, $text);
                    544: }
                    545:
                    546:
                    547: sub date {
                    548:        my ($sec,$min,$hour,$mday,$mon,$year,$wday,$yday,$isdst) = localtime();
                    549:        my $localtime = sprintf("%04d%02d%02d", $year+1900, $mon+1, $mday);
                    550:        return $localtime;
                    551: }
                    552: sub gettime {
                    553:        my ($sec,$min,$hour,$mday,$mon,$year,$wday,$yday,$isdst) = localtime();
                    554:        my $localtime = sprintf("%s %s %d %02d:%02d:%02d", $days[$wday], $months[$mon], $mday, $hour, $min, $sec);
                    555:        return $localtime;
                    556: }
                    557:
                    558: sub whois {
                    559:        my( $socket, $target )=@_;
                    560:        print $socket "WHOIS $target $target\r\n";
                    561: }
                    562:
                    563: sub ctcp {
                    564:        my( $socket, $target )=@_;
                    565: #      print $socket "PRIVMSG $target :".chr(01)."CLIENTINFO".chr(01)."\r\n";
                    566: #      print $socket "PRIVMSG $target :".chr(01)."FINGER".chr(01)."\r\n";
                    567: #      print $socket "PRIVMSG $target :".chr(01)."SOURCE".chr(01)."\r\n";
                    568:        print $socket "PRIVMSG $target :".chr(01)."TIME".chr(01)."\r\n";
                    569: #      print $socket "PRIVMSG $target :".chr(01)."USERINFO".chr(01)."\r\n";
                    570:        print $socket "PRIVMSG $target :".chr(01)."VERSION".chr(01)."\r\n";
                    571: #      print $socket "PRIVMSG $target :".chr(01)."PING".chr(01)."\r\n";
                    572: }
                    573:
                    574: sub cbind {
                    575:        my ($type, $flags, $cmd, $proc) = @_;
                    576:        if ($type eq "pub") {
                    577:                push(@{$call->{pub}}, {cmd => $cmd, proc => $proc});
                    578:        } elsif ($type eq "msg") {
                    579:                push(@{$call->{msg}}, {cmd => $cmd, proc => $proc});
                    580:        } elsif ($type eq "notc") {
                    581:                push(@{$call->{notc}}, {mask => $cmd, proc => $proc});
                    582:        } elsif ($type eq "mode") {
                    583:                push(@{$call->{mode}}, {mask => $cmd, proc => $proc});
                    584:        } elsif ($type eq "join") {
                    585:                push(@{$call->{join}}, {mask => $cmd, proc => $proc});
                    586:        } elsif ($type eq "partcall") {
                    587:                push(@{$call->{part}}, {mask => $cmd, proc => $proc});
                    588:        } elsif ($type eq "pubm") {
                    589:                push(@{$call->{pubm}}, {mask => $cmd, proc => $proc});
                    590:        } elsif ($type eq "msgm") {
                    591:                push(@{$call->{msgm}}, {mask => $cmd, proc => $proc});
                    592:        }
                    593: }
                    594:
                    595: sub debug {
                    596:        my ($level, $msg) = @_;
                    597:        if ($verbose >= $level) { print "$msg\n"; }
                    598: }
                    599:
                    600: sub isstaff {
                    601:        my( $bot, $nick ) = @_;
                    602:        if( !( $bot->{name} =~ /^$localnet$/i ) )
                    603:        {
                    604:                return 0;
                    605:        }
                    606:        my $lnick = lc $nick;
                    607:        foreach( @stafflist ) {
                    608:                if( $lnick eq $_ ) {
                    609:                        return 1;
                    610:                }
                    611:        }
                    612:        return 0;
                    613: }

CVSweb