/[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 10 by dpavlin, Thu Mar 2 00:19:12 2006 UTC revision 13 by dpavlin, Sun Mar 12 14:19:00 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';  my $NICK = 'irc-logger-dev';
22  my $CONNECT =  my $CONNECT =
23    {Server => 'irc.freenode.net',    {Server => 'irc.freenode.net',
24     Nick => $NICK,     Nick => $NICK,
# Line 39  my $DSN = 'DBI:Pg:dbname=irc-logger'; Line 39  my $DSN = 'DBI:Pg:dbname=irc-logger';
39    
40    
41    
42  use POE qw(Component::IRC Wheel::FollowTail);  use POE qw(Component::IRC Wheel::FollowTail Component::Server::HTTP);
43    use HTTP::Status;
44  use DBI;  use DBI;
45  use Encode qw/from_to/;  use Encode qw/from_to/;
46    
# Line 72  insert into log Line 73  insert into log
73  values (?,?,?)  values (?,?,?)
74  });  });
75    
76    =head2 get_from_log
77    
78     my @messages = get_from_log(
79            limit => 42,
80            search => '%what to stuff in ilike%',
81            fmt => {
82                    time => '{%s} ',
83                    time_channel => '{%s %s} ',
84                    nick => '%s: ',
85                    message => '%s',
86            },
87     );
88    
89    =cut
90    
91    sub get_from_log {
92            my $args = {@_};
93    
94            $args->{limit} ||= 10;
95    
96            $args->{fmt} ||= {
97                    time => '{%s} ',
98                    time_channel => '{%s %s} ',
99                    nick => '%s: ',
100                    message => '%s',
101            };
102    
103            my $sql = qq{
104                    select
105                            time::date as date,
106                            time::time as time,
107                            channel,
108                            nick,
109                            message
110                    from log
111            };
112            $sql .= " where message ilike ? " if ($args->{search});
113            $sql .= " order by log.time desc";
114            $sql .= " limit " . $args->{limit};
115    
116            my $sth = $dbh->prepare( $sql );
117            if ($args->{search}) {
118                    $sth->execute( $args->{search} );
119            } else {
120                    $sth->execute();
121            }
122            my $last_row = {
123                    date => '',
124                    time => '',
125                    channel => '',
126                    nick => '',
127            };
128    
129            my @rows;
130    
131            while (my $row = $sth->fetchrow_hashref) {
132                    unshift @rows, $row;
133            }
134    
135            my @msgs;
136    
137            foreach my $row (@rows) {
138    
139                    $row->{time} =~ s#\.\d+##;
140    
141                    my $t;
142                    $t = $row->{date} . ' ' if ($last_row->{date} ne $row->{date});
143                    $t .= $row->{time};
144    
145                    my $msg = '';
146    
147                    if ($last_row->{channel} ne $row->{channel}) {
148                            $msg .= sprintf($args->{fmt}->{time_channel}, $t, $row->{channel});
149                    } else {
150                            $msg .= sprintf($args->{fmt}->{time}, $t);
151                    }
152    
153                    my $append = 1;
154    
155                    if ($last_row->{nick} ne $row->{nick}) {
156                            $msg .= sprintf($args->{fmt}->{nick}, $row->{nick});
157                            $append = 0;
158                    }
159    
160                    $msg .= sprintf($args->{fmt}->{message}, $row->{message});
161    
162                    if ($append && @msgs) {
163                            $msgs[$#msgs] .= " " . $msg;
164                    } else {
165                            push @msgs, $msg;
166                    }
167    
168                    $last_row = $row;
169            }
170    
171            return @msgs;
172    }
173    
174    
175  my $SKIPPING = 0;               # if skipping, how many we've done  my $SKIPPING = 0;               # if skipping, how many we've done
176  my $SEND_QUEUE;                 # cache  my $SEND_QUEUE;                 # cache
# Line 89  POE::Session->create Line 188  POE::Session->create
188                  $_[KERNEL]->post($IRC_ALIAS => join => '#logger');                  $_[KERNEL]->post($IRC_ALIAS => join => '#logger');
189                  $_[KERNEL]->yield("heartbeat"); # start heartbeat                  $_[KERNEL]->yield("heartbeat"); # start heartbeat
190  #               $_[KERNEL]->yield("my_add", $_) for keys %FOLLOWS;  #               $_[KERNEL]->yield("my_add", $_) for keys %FOLLOWS;
191                    $_[KERNEL]->post( $IRC_ALIAS => privmsg => 'nickserv', "IDENTIFY $NICK" );
192      },      },
193      irc_public => sub {      irc_public => sub {
194                  my $kernel = $_[KERNEL];                  my $kernel = $_[KERNEL];
# Line 108  POE::Session->create Line 208  POE::Session->create
208                  from_to($msg, 'UTF-8', 'ISO-8859-2');                  from_to($msg, 'UTF-8', 'ISO-8859-2');
209    
210                  my $res = "unknown command '$msg', try /msg $NICK help!";                  my $res = "unknown command '$msg', try /msg $NICK help!";
211                    my @out;
212    
213                  print "<< $msg\n";                  print "<< $msg\n";
214    
215                  if ($msg =~ m/^help/i) {                  if ($msg =~ m/^help/i) {
216    
217                          $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";
218    
219                  } elsif ($msg =~ m/^msg\s+(\S+)\s+(.*)$/i) {                  } elsif ($msg =~ m/^msg\s+(\S+)\s+(.*)$/i) {
220    
# Line 137  POE::Session->create Line 238  POE::Session->create
238                          $res .= join(" | ", @users);                          $res .= join(" | ", @users);
239                  } elsif ($msg =~ m/^last.*?\s*(\d*)/i) {                  } elsif ($msg =~ m/^last.*?\s*(\d*)/i) {
240    
241                          my $nr = $1 || 10;                          foreach my $res (get_from_log( limit => $1 )) {
242                                    print "last: $res\n";
243                          my $sth = $dbh->prepare(qq{                                  from_to($res, 'ISO-8859-2', 'UTF-8');
244                                  select                                  $_[KERNEL]->post( $IRC_ALIAS => privmsg => $nick, $res );
                                         time::date as date,  
                                         time::time as time,  
                                         channel,  
                                         nick,  
                                         message  
                                 from log order by log.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;  
245                          }                          }
246    
247                          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};  
248    
249                                  push @msgs, $msg;                  } elsif ($msg =~ m/^(search|grep)\s+(.*)$/i) {
250    
251                                  $last_row = $row;                          my $what = $2;
                         }  
252    
253                          foreach my $res (@msgs) {                          foreach my $res (get_from_log( limit => 20, search => "%${what}%" )) {
254                                  print "last: $res\n";                                  print "search [$what]: $res\n";
255                                  from_to($res, 'ISO-8859-2', 'UTF-8');                                  from_to($res, 'ISO-8859-2', 'UTF-8');
256                                  $_[KERNEL]->post( $IRC_ALIAS => privmsg => $nick, $res );                                  $_[KERNEL]->post( $IRC_ALIAS => privmsg => $nick, $res );
257                          }                          }
258    
259                          $res = '';                          $res = '';
260    
261                  }                  }
262    
263                  if ($res) {                  if ($res) {
# Line 218  POE::Session->create Line 281  POE::Session->create
281                  warn "## indetify $NICK\n";                  warn "## indetify $NICK\n";
282                  $_[KERNEL]->post( $IRC_ALIAS => privmsg => 'nickserv', "IDENTIFY $NICK" );                  $_[KERNEL]->post( $IRC_ALIAS => privmsg => 'nickserv', "IDENTIFY $NICK" );
283          },          },
284          irc_433 => sub {  #       irc_433 => sub {
285                  print "# irc_433: ",$_[ARG1], "\n";  #               print "# irc_433: ",$_[ARG1], "\n";
286                  warn "## indetify $NICK\n";  #               warn "## indetify $NICK\n";
287                  $_[KERNEL]->post( $IRC_ALIAS => privmsg => 'nickserv', "IDENTIFY $NICK" );  #               $_[KERNEL]->post( $IRC_ALIAS => privmsg => 'nickserv', "IDENTIFY $NICK" );
288          },  #       },
289          irc_372 => sub {          irc_372 => sub {
290                  print "MOTD: ", $_[ARG1], "\n";                  print "MOTD: ", $_[ARG1], "\n";
291          },          },
# Line 312  POE::Session->create Line 375  POE::Session->create
375     },     },
376    );    );
377    
378    # http server
379    
380    my $httpd = POE::Component::Server::HTTP->new(
381            Port => 8000,
382            ContentHandler => { '/' => \&root_handler },
383            Headers        => { Server => 'irc-logger' },
384    );
385    
386    my $style = <<'_END_OF_STYLE_';
387    .time, .channel { color: #808080; font-size: 60%; }
388    .nick { color: #0000ff; font-size: 80%; }
389    .message { color: #000000; font-size: 100%; }
390    _END_OF_STYLE_
391    
392    sub root_handler {
393            my ($request, $response) = @_;
394            $response->code(RC_OK);
395            $response->content_type('text/html');
396            $response->content(
397                    qq{<html><head><title>$NICK</title><style type="text/css">$style</style></head><body>} .
398                    "irc-logger url: " . $request->uri . '<br/>' .
399                    join("<br/>",
400                            get_from_log(
401                                    limit => 100,
402                                    fmt => {
403                                            time => '<span class="time">%s</span> ',
404                                            time_channel => '<span class="channel">%s %s</span> ',
405                                            nick => '<span class="nick">%s:</span> ',
406                                            message => '<span class="message">%s</span>',
407                                    },
408                            )
409                    ) .
410                    qq{</body></html>}
411            );
412            return RC_OK;
413    }
414    
415  POE::Kernel->run;  POE::Kernel->run;

Legend:
Removed from v.10  
changed lines
  Added in v.13

  ViewVC Help
Powered by ViewVC 1.1.26