/[irc-logger]/trunk/bin/irc-logger.pl
This is repository of my old source code which isn't updated any more. Go to git.rot13.org for current projects!
ViewVC logotype

Diff of /trunk/bin/irc-logger.pl

Parent Directory Parent Directory | Revision Log Revision Log | View Patch Patch

revision 8 by dpavlin, Wed Mar 1 22:42:21 2006 UTC revision 14 by dpavlin, Sun Mar 12 14:36:12 2006 UTC
# Line 18  log all conversation on irc channel Line 18  log all conversation on irc channel
18    
19  ## CONFIG  ## CONFIG
20    
21  my $NICK = 'irc-logger-dev';  my $NICK = 'irc-logger';
22  my $CONNECT =  my $CONNECT =
23    {Server => 'irc.freenode.net',    {Server => 'irc.freenode.net',
24     Nick => $NICK,     Nick => $NICK,
# Line 35  my %FOLLOWS = Line 35  my %FOLLOWS =
35    
36  my $DSN = 'DBI:Pg:dbname=irc-logger';  my $DSN = 'DBI:Pg:dbname=irc-logger';
37    
38    my $ENCODING = 'ISO-8859-2';
39    
40  ## END CONFIG  ## END CONFIG
41    
42    
43    
44  use POE qw(Component::IRC Wheel::FollowTail);  use POE qw(Component::IRC Wheel::FollowTail Component::Server::HTTP);
45    use HTTP::Status;
46  use DBI;  use DBI;
47  use Encode qw/from_to/;  use Encode qw/from_to/;
48    
# Line 72  insert into log Line 75  insert into log
75  values (?,?,?)  values (?,?,?)
76  });  });
77    
78    =head2 get_from_log
79    
80     my @messages = get_from_log(
81            limit => 42,
82            search => '%what to stuff in ilike%',
83            fmt => {
84                    time => '{%s} ',
85                    time_channel => '{%s %s} ',
86                    nick => '%s: ',
87                    message => '%s',
88            },
89     );
90    
91    =cut
92    
93    sub get_from_log {
94            my $args = {@_};
95    
96            $args->{limit} ||= 10;
97    
98            $args->{fmt} ||= {
99                    time => '{%s} ',
100                    time_channel => '{%s %s} ',
101                    nick => '%s: ',
102                    message => '%s',
103            };
104    
105            my $sql = qq{
106                    select
107                            time::date as date,
108                            time::time as time,
109                            channel,
110                            nick,
111                            message
112                    from log
113            };
114            $sql .= " where message ilike ? " if ($args->{search});
115            $sql .= " order by log.time desc";
116            $sql .= " limit " . $args->{limit};
117    
118            my $sth = $dbh->prepare( $sql );
119            if ($args->{search}) {
120                    $sth->execute( $args->{search} );
121            } else {
122                    $sth->execute();
123            }
124            my $last_row = {
125                    date => '',
126                    time => '',
127                    channel => '',
128                    nick => '',
129            };
130    
131            my @rows;
132    
133            while (my $row = $sth->fetchrow_hashref) {
134                    unshift @rows, $row;
135            }
136    
137            my @msgs;
138    
139            foreach my $row (@rows) {
140    
141                    $row->{time} =~ s#\.\d+##;
142    
143                    my $t;
144                    $t = $row->{date} . ' ' if ($last_row->{date} ne $row->{date});
145                    $t .= $row->{time};
146    
147                    my $msg = '';
148    
149                    if ($last_row->{channel} ne $row->{channel}) {
150                            $msg .= sprintf($args->{fmt}->{time_channel}, $t, $row->{channel});
151                    } else {
152                            $msg .= sprintf($args->{fmt}->{time}, $t);
153                    }
154    
155                    my $append = 1;
156    
157                    if ($last_row->{nick} ne $row->{nick}) {
158                            $msg .= sprintf($args->{fmt}->{nick}, $row->{nick});
159                            $append = 0;
160                    }
161    
162                    $msg .= sprintf($args->{fmt}->{message}, $row->{message});
163    
164                    if ($append && @msgs) {
165                            $msgs[$#msgs] .= " " . $msg;
166                    } else {
167                            push @msgs, $msg;
168                    }
169    
170                    $last_row = $row;
171            }
172    
173            return @msgs;
174    }
175    
176    
177  my $SKIPPING = 0;               # if skipping, how many we've done  my $SKIPPING = 0;               # if skipping, how many we've done
178  my $SEND_QUEUE;                 # cache  my $SEND_QUEUE;                 # cache
# Line 84  POE::Session->create Line 185  POE::Session->create
185                  $_[KERNEL]->post($IRC_ALIAS => register => 'all');                  $_[KERNEL]->post($IRC_ALIAS => register => 'all');
186                  $_[KERNEL]->post($IRC_ALIAS => connect => $CONNECT);                  $_[KERNEL]->post($IRC_ALIAS => connect => $CONNECT);
187      },      },
188      irc_255 => sub {            # server is done blabbing      irc_255 => sub {    # server is done blabbing
189                  $_[KERNEL]->post($IRC_ALIAS => join => $CHANNEL);                  $_[KERNEL]->post($IRC_ALIAS => join => $CHANNEL);
190                  $_[KERNEL]->post($IRC_ALIAS => join => '#logger');                  $_[KERNEL]->post($IRC_ALIAS => join => '#logger');
191                  $_[KERNEL]->yield("heartbeat"); # start heartbeat                  $_[KERNEL]->yield("heartbeat"); # start heartbeat
192  #               $_[KERNEL]->yield("my_add", $_) for keys %FOLLOWS;  #               $_[KERNEL]->yield("my_add", $_) for keys %FOLLOWS;
193                    $_[KERNEL]->post( $IRC_ALIAS => privmsg => 'nickserv', "IDENTIFY $NICK" );
194      },      },
195      irc_public => sub {      irc_public => sub {
196                  my $kernel = $_[KERNEL];                  my $kernel = $_[KERNEL];
# Line 96  POE::Session->create Line 198  POE::Session->create
198                  my $channel = $_[ARG1]->[0];                  my $channel = $_[ARG1]->[0];
199                  my $msg = $_[ARG2];                  my $msg = $_[ARG2];
200    
201                  from_to($msg, 'UTF-8', 'ISO-8859-2');                  from_to($msg, 'UTF-8', $ENCODING);
202    
203                  print "$channel: <$nick> $msg\n";                  print "$channel: <$nick> $msg\n";
204                  $sth->execute($channel, $nick, $msg);                  $sth->execute($channel, $nick, $msg);
# Line 105  POE::Session->create Line 207  POE::Session->create
207                  my $kernel = $_[KERNEL];                  my $kernel = $_[KERNEL];
208                  my $nick = (split /!/, $_[ARG0])[0];                  my $nick = (split /!/, $_[ARG0])[0];
209                  my $msg = $_[ARG2];                  my $msg = $_[ARG2];
210                  from_to($msg, 'UTF-8', 'ISO-8859-2');                  from_to($msg, 'UTF-8', $ENCODING);
211    
212                  my $res = "unknown command '$msg', try /msg $NICK help!";                  my $res = "unknown command '$msg', try /msg $NICK help!";
213                    my @out;
214    
215                  print "<< $msg\n";                  print "<< $msg\n";
216    
217                  if ($msg =~ m/^help/i) {                  if ($msg =~ m/^help/i) {
218    
219                          $res = "usage: /msg $NICK stat - shows user statistics | /msg $NICK last - show backtrace of conversations";                          $res = "usage: /msg $NICK comand | commands: stat - user/message stat | last - show backtrace | grep foobar - find foobar";
220    
221                    } elsif ($msg =~ m/^msg\s+(\S+)\s+(.*)$/i) {
222    
223                            print ">> /msg $1 $2\n";
224                            $_[KERNEL]->post( $IRC_ALIAS => privmsg => $1, $2 );
225                            $res = '';
226    
227                  } elsif ($msg =~ m/^stat.*?\s*(\d*)/i) {                  } elsif ($msg =~ m/^stat.*?\s*(\d*)/i) {
228    
# Line 131  POE::Session->create Line 240  POE::Session->create
240                          $res .= join(" | ", @users);                          $res .= join(" | ", @users);
241                  } elsif ($msg =~ m/^last.*?\s*(\d*)/i) {                  } elsif ($msg =~ m/^last.*?\s*(\d*)/i) {
242    
243                          my $nr = $1 || 10;                          foreach my $res (get_from_log( limit => $1 )) {
244                                    print "last: $res\n";
245                          my $sth = $dbh->prepare(qq{                                  from_to($res, $ENCODING, 'UTF-8');
246                                  select                                  $_[KERNEL]->post( $IRC_ALIAS => privmsg => $nick, $res );
                                         time::date as date,  
                                         time::time as time,  
                                         channel,  
                                         nick,  
                                         message  
                                 from log order by time desc limit $nr  
                         });  
                         $sth->execute();  
                         $res = "Last $nr messages: ";  
                         my $last_row = {  
                                 date => '',  
                                 time => '',  
                                 channel => '',  
                                 nick => '',  
                         };  
   
                         my @rows;  
   
                         while (my $row = $sth->fetchrow_hashref) {  
                                 unshift @rows, $row;  
247                          }                          }
248    
249                          my @msgs;                          $res = '';
   
                         foreach my $row (@rows) {  
   
                                 $row->{time} =~ s#\.\d+##;  
   
                                 my $t;  
                                 $t = $row->{date} . ' ' if ($last_row->{date} ne $row->{date});  
                                 $t .= $row->{time};  
   
                                 my $msg = '';  
   
                                 $msg .= "($t";  
                                 $msg .= ' ' . $row->{channel} if ($last_row->{channel} ne $row->{channel});  
                                 $msg .= ") ";  
   
                                 $msg .= $row->{nick} . ': '  if ($last_row->{nick} ne $row->{nick});  
   
                                 $msg .= $row->{message};  
250    
251                                  push @msgs, $msg;                  } elsif ($msg =~ m/^(search|grep)\s+(.*)$/i) {
252    
253                                  $last_row = $row;                          my $what = $2;
                         }  
254    
255                          foreach my $res (@msgs) {                          foreach my $res (get_from_log( limit => 20, search => "%${what}%" )) {
256                                  print "last: $res\n";                                  print "search [$what]: $res\n";
257                                  from_to($res, 'ISO-8859-2', 'UTF-8');                                  from_to($res, $ENCODING, 'UTF-8');
258                                  $_[KERNEL]->post( $IRC_ALIAS => privmsg => $nick, $res );                                  $_[KERNEL]->post( $IRC_ALIAS => privmsg => $nick, $res );
259                          }                          }
260    
261                          $res = '';                          $res = '';
262    
263                  }                  }
264    
265                  if ($res) {                  if ($res) {
266                          print ">> [$nick] $res\n";                          print ">> [$nick] $res\n";
267                          from_to($res, 'ISO-8859-2', 'UTF-8');                          from_to($res, $ENCODING, 'UTF-8');
268                          $_[KERNEL]->post( $IRC_ALIAS => privmsg => $nick, $res );                          $_[KERNEL]->post( $IRC_ALIAS => privmsg => $nick, $res );
269                  }                  }
270    
271          },          },
272            irc_477 => sub {
273                    print "# irc_477: ",$_[ARG1], "\n";
274                    $_[KERNEL]->post( $IRC_ALIAS => privmsg => 'nickserv', "register $NICK" );
275            },
276          irc_505 => sub {          irc_505 => sub {
277          print "# irc_505: ",$_[ARG1], "\n";                  print "# irc_505: ",$_[ARG1], "\n";
278                  $_[KERNEL]->post( $IRC_ALIAS => privmsg => 'nickserv', "register $NICK" );                  $_[KERNEL]->post( $IRC_ALIAS => privmsg => 'nickserv', "register $NICK" );
279                  warn "## register $NICK\n";  #               $_[KERNEL]->post( $IRC_ALIAS => privmsg => 'nickserv', "set hide email on" );
280    #               $_[KERNEL]->post( $IRC_ALIAS => privmsg => 'nickserv', "set email dpavlin\@rot13.org" );
281          },          },
282          irc_registered => sub {          irc_registered => sub {
                 $_[KERNEL]->post( $IRC_ALIAS => privmsg => 'nickserv', "IDENTIFY $NICK" );  
283                  warn "## indetify $NICK\n";                  warn "## indetify $NICK\n";
284                    $_[KERNEL]->post( $IRC_ALIAS => privmsg => 'nickserv', "IDENTIFY $NICK" );
285            },
286    #       irc_433 => sub {
287    #               print "# irc_433: ",$_[ARG1], "\n";
288    #               warn "## indetify $NICK\n";
289    #               $_[KERNEL]->post( $IRC_ALIAS => privmsg => 'nickserv', "IDENTIFY $NICK" );
290    #       },
291            irc_372 => sub {
292                    print "MOTD: ", $_[ARG1], "\n";
293            },
294            irc_snotice => sub {
295                    print "(server notice): ", $_[ARG0], "\n";
296          },          },
297      (map      (map
298       {       {
299         ;"irc_$_" => sub { }}         ;"irc_$_" => sub { }}
300       qw(join       qw(
         ctcp_version  
         connected snotice ctcp_action ping notice mode part quit  
         001 002 003 004 005  
         250 251 252 253 254 265 266  
         332 333 353 366 372 375 376  
                 477  
301                  )),                  )),
302    #       join
303    #       ctcp_version
304    #       connected snotice ctcp_action ping notice mode part quit
305    #       001 002 003 004 005
306    #       250 251 252 253 254 265 266
307    #       332 333 353 366 372 375 376
308    #       477
309      _child => sub {},      _child => sub {},
310      _default => sub {      _default => sub {
311        printf "%s: session %s caught an unhandled %s event.\n",        printf "%s: session %s caught an unhandled %s event.\n",
# Line 289  POE::Session->create Line 377  POE::Session->create
377     },     },
378    );    );
379    
380    # http server
381    
382    my $httpd = POE::Component::Server::HTTP->new(
383            Port => $NICK =~ m/-dev/ ? 8001 : 8000,
384            ContentHandler => { '/' => \&root_handler },
385            Headers        => { Server => 'irc-logger' },
386    );
387    
388    my $style = <<'_END_OF_STYLE_';
389    .time, .channel { color: #808080; font-size: 60%; }
390    .nick { color: #0000ff; font-size: 80%; }
391    .message { color: #000000; font-size: 100%; }
392    _END_OF_STYLE_
393    
394    sub root_handler {
395            my ($request, $response) = @_;
396            $response->code(RC_OK);
397            $response->content_type("text/html; charset=$ENCODING");
398            $response->content(
399                    qq{<html><head><title>$NICK</title><style type="text/css">$style</style></head><body>} .
400                    "irc-logger url: " . $request->uri . '<br/>' .
401                    join("<br/>",
402                            get_from_log(
403                                    limit => 100,
404                                    fmt => {
405                                            time => '<span class="time">%s</span> ',
406                                            time_channel => '<span class="channel">%s %s</span> ',
407                                            nick => '<span class="nick">%s:</span> ',
408                                            message => '<span class="message">%s</span>',
409                                    },
410                            )
411                    ) .
412                    qq{</body></html>}
413            );
414            return RC_OK;
415    }
416    
417  POE::Kernel->run;  POE::Kernel->run;

Legend:
Removed from v.8  
changed lines
  Added in v.14

  ViewVC Help
Powered by ViewVC 1.1.26