Revision 4

Date:
2006/02/27 11:54:38
Author:
dpavlin
Revision Log:
IRC bot which replace human memory
Files:

Legend:

 
Added
 
Removed
 
Modified
  • trunk/irc-logger.pl

     
    1 #!/usr/bin/perl -w
    2 use strict;
    3 $|++;
    4
    5 ## CONFIG
    6
    7 my $NICK = 'irc-logger';
    8 my $CONNECT =
    9 {Server => 'irc.freenode.net',
    10 Nick => $NICK,
    11 Ircname => 'logger: ask dpavlin@rot13.org'
    12 };
    13 my $CHANNEL = '#razmjenavjestina';
    14 my $IRC_ALIAS = "log";
    15
    16 my %FOLLOWS =
    17 (
    18 ACCESS => "/var/log/apache/access.log",
    19 ERROR => "/var/log/apache/error.log",
    20 );
    21
    22 ## END CONFIG
    23
    24 my $SKIPPING = 0; # if skipping, how many we've done
    25 my $SEND_QUEUE; # cache
    26
    27 use POE qw(Component::IRC Wheel::FollowTail);
    28
    29 POE::Component::IRC->new($IRC_ALIAS);
    30
    31 POE::Session->create
    32 (inline_states =>
    33 {_start => sub {
    34 $_[KERNEL]->post($IRC_ALIAS => register => 'all');
    35 $_[KERNEL]->post($IRC_ALIAS => connect => $CONNECT);
    36 },
    37 irc_255 => sub { # server is done blabbing
    38 $_[KERNEL]->post($IRC_ALIAS => join => $CHANNEL);
    39 $_[KERNEL]->post($IRC_ALIAS => join => '#logger');
    40 $_[KERNEL]->yield("heartbeat"); # start heartbeat
    41 # $_[KERNEL]->yield("my_add", $_) for keys %FOLLOWS;
    42 },
    43 irc_public => sub {
    44 my $kernel = $_[KERNEL];
    45 my $nick = (split /!/, $_[ARG0])[0];
    46 my $channel = $_[ARG1]->[0];
    47 my $msg = $_[ARG2];
    48
    49 print "$channel: <$nick> $msg\n";
    50 },
    51 (map
    52 {
    53 ;"irc_$_" => sub { }}
    54 qw(join
    55 ctcp_version
    56 connected snotice ctcp_action ping notice mode part quit
    57 001 002 003 004 005
    58 250 251 252 253 254 265 266
    59 332 333 353 366 372 375 376)),
    60 _child => sub {},
    61 _default => sub {
    62 printf "%s: session %s caught an unhandled %s event.\n",
    63 scalar localtime(), $_[SESSION]->ID, $_[ARG0];
    64 print "The $_[ARG0] event was given these parameters: ",
    65 join(" ", map({"ARRAY" eq ref $_ ? "[@$_]" : "$_"} @{$_[ARG1]})), "\n";
    66 0; # false for signals
    67 },
    68 my_add => sub {
    69 my $trailing = $_[ARG0];
    70 my $session = $_[SESSION];
    71 POE::Session->create
    72 (inline_states =>
    73 {_start => sub {
    74 $_[HEAP]->{wheel} =
    75 POE::Wheel::FollowTail->new
    76 (
    77 Filename => $FOLLOWS{$trailing},
    78 InputEvent => 'got_line',
    79 );
    80 },
    81 got_line => sub {
    82 $_[KERNEL]->post($session => my_tailed =>
    83 time, $trailing, $_[ARG0]);
    84 },
    85 },
    86 );
    87
    88 },
    89 my_tailed => sub {
    90 my ($time, $file, $line) = @_[ARG0..ARG2];
    91 ## $time will be undef on a probe, or a time value if a real line
    92
    93 ## PoCo::IRC has throttling built in, but no external visibility
    94 ## so this is reaching "under the hood"
    95 $SEND_QUEUE ||=
    96 $_[KERNEL]->alias_resolve($IRC_ALIAS)->get_heap->{send_queue};
    97
    98 ## handle "no need to keep skipping" transition
    99 if ($SKIPPING and @$SEND_QUEUE < 1) {
    100 $_[KERNEL]->post($IRC_ALIAS => privmsg => $CHANNEL =>
    101 "[discarded $SKIPPING messages]");
    102 $SKIPPING = 0;
    103 }
    104
    105 ## handle potential message display
    106 if ($time) {
    107 if ($SKIPPING or @$SEND_QUEUE > 3) { # 3 msgs per 10 seconds
    108 $SKIPPING++;
    109 } else {
    110 my @time = localtime $time;
    111 $_[KERNEL]->post($IRC_ALIAS => privmsg => $CHANNEL =>
    112 sprintf "%02d:%02d:%02d: %s: %s",
    113 ($time[2] + 11) % 12 + 1, $time[1], $time[0],
    114 $file, $line);
    115 }
    116 }
    117
    118 ## handle re-probe/flush if skipping
    119 if ($SKIPPING) {
    120 $_[KERNEL]->delay($_[STATE] => 0.5); # $time will be undef
    121 }
    122
    123 },
    124 my_heartbeat => sub {
    125 $_[KERNEL]->yield(my_tailed => time, "heartbeat", "beep");
    126 $_[KERNEL]->delay($_[STATE] => 10);
    127 }
    128 },
    129 );
    130
    131 POE::Kernel->run;