/[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 12 by dpavlin, Sun Mar 12 13:33:20 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 72  insert into log Line 72  insert into log
72  values (?,?,?)  values (?,?,?)
73  });  });
74    
75    =head2 get_from_log
76    
77     my @messages = get_from_log(
78            limit => 42,
79            search => '%what to stuff in ilike%',
80     );
81    
82    =cut
83    
84    sub get_from_log {
85            my $args = {@_};
86    
87            $args->{limit} ||= 10;
88    
89            my $sql = qq{
90                    select
91                            time::date as date,
92                            time::time as time,
93                            channel,
94                            nick,
95                            message
96                    from log
97            };
98            $sql .= " where message ilike ? " if ($args->{search});
99            $sql .= " order by log.time desc";
100            $sql .= " limit " . $args->{limit};
101    
102            my $sth = $dbh->prepare( $sql );
103            if ($args->{search}) {
104                    $sth->execute( $args->{search} );
105            } else {
106                    $sth->execute();
107            }
108            my $last_row = {
109                    date => '',
110                    time => '',
111                    channel => '',
112                    nick => '',
113            };
114    
115            my @rows;
116    
117            while (my $row = $sth->fetchrow_hashref) {
118                    unshift @rows, $row;
119            }
120    
121            my @msgs;
122    
123            foreach my $row (@rows) {
124    
125                    $row->{time} =~ s#\.\d+##;
126    
127                    my $t;
128                    $t = $row->{date} . ' ' if ($last_row->{date} ne $row->{date});
129                    $t .= $row->{time};
130    
131                    my $msg = '';
132    
133                    $msg .= "{$t";
134                    $msg .= ' ' . $row->{channel} if ($last_row->{channel} ne $row->{channel});
135                    $msg .= "} ";
136    
137                    my $append = 1;
138    
139                    if ($last_row->{nick} ne $row->{nick}) {
140                            $msg .= $row->{nick} . ': ';
141                            $append = 0;
142                    }
143    
144                    $msg .= $row->{message};
145    
146                    if ($append && @msgs) {
147                            $msgs[$#msgs] .= " " . $msg;
148                    } else {
149                            push @msgs, $msg;
150                    }
151    
152                    $last_row = $row;
153            }
154    
155            return @msgs;
156    }
157    
158    
159  my $SKIPPING = 0;               # if skipping, how many we've done  my $SKIPPING = 0;               # if skipping, how many we've done
160  my $SEND_QUEUE;                 # cache  my $SEND_QUEUE;                 # cache
# Line 89  POE::Session->create Line 172  POE::Session->create
172                  $_[KERNEL]->post($IRC_ALIAS => join => '#logger');                  $_[KERNEL]->post($IRC_ALIAS => join => '#logger');
173                  $_[KERNEL]->yield("heartbeat"); # start heartbeat                  $_[KERNEL]->yield("heartbeat"); # start heartbeat
174  #               $_[KERNEL]->yield("my_add", $_) for keys %FOLLOWS;  #               $_[KERNEL]->yield("my_add", $_) for keys %FOLLOWS;
175                    $_[KERNEL]->post( $IRC_ALIAS => privmsg => 'nickserv', "IDENTIFY $NICK" );
176      },      },
177      irc_public => sub {      irc_public => sub {
178                  my $kernel = $_[KERNEL];                  my $kernel = $_[KERNEL];
# Line 108  POE::Session->create Line 192  POE::Session->create
192                  from_to($msg, 'UTF-8', 'ISO-8859-2');                  from_to($msg, 'UTF-8', 'ISO-8859-2');
193    
194                  my $res = "unknown command '$msg', try /msg $NICK help!";                  my $res = "unknown command '$msg', try /msg $NICK help!";
195                    my @out;
196    
197                  print "<< $msg\n";                  print "<< $msg\n";
198    
199                  if ($msg =~ m/^help/i) {                  if ($msg =~ m/^help/i) {
200    
201                          $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";
202    
203                  } elsif ($msg =~ m/^msg\s+(\S+)\s+(.*)$/i) {                  } elsif ($msg =~ m/^msg\s+(\S+)\s+(.*)$/i) {
204    
# Line 137  POE::Session->create Line 222  POE::Session->create
222                          $res .= join(" | ", @users);                          $res .= join(" | ", @users);
223                  } elsif ($msg =~ m/^last.*?\s*(\d*)/i) {                  } elsif ($msg =~ m/^last.*?\s*(\d*)/i) {
224    
225                          my $nr = $1 || 10;                          foreach my $res (get_from_log( limit => $1 )) {
226                                    print "last: $res\n";
227                          my $sth = $dbh->prepare(qq{                                  from_to($res, 'ISO-8859-2', 'UTF-8');
228                                  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;  
229                          }                          }
230    
231                          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});  
232    
233                                  $msg .= $row->{message};                  } elsif ($msg =~ m/^(search|grep)\s+(.*)$/i) {
234    
235                                  push @msgs, $msg;                          my $what = $2;
236    
237                                  $last_row = $row;                          foreach my $res (get_from_log( limit => 20, search => "%${what}%" )) {
238                          }                                  print "search [$what]: $res\n";
   
                         foreach my $res (@msgs) {  
                                 print "last: $res\n";  
239                                  from_to($res, 'ISO-8859-2', 'UTF-8');                                  from_to($res, 'ISO-8859-2', 'UTF-8');
240                                  $_[KERNEL]->post( $IRC_ALIAS => privmsg => $nick, $res );                                  $_[KERNEL]->post( $IRC_ALIAS => privmsg => $nick, $res );
241                          }                          }
242    
243                          $res = '';                          $res = '';
244    
245                  }                  }
246    
247                  if ($res) {                  if ($res) {
# Line 218  POE::Session->create Line 265  POE::Session->create
265                  warn "## indetify $NICK\n";                  warn "## indetify $NICK\n";
266                  $_[KERNEL]->post( $IRC_ALIAS => privmsg => 'nickserv', "IDENTIFY $NICK" );                  $_[KERNEL]->post( $IRC_ALIAS => privmsg => 'nickserv', "IDENTIFY $NICK" );
267          },          },
268          irc_433 => sub {  #       irc_433 => sub {
269                  print "# irc_433: ",$_[ARG1], "\n";  #               print "# irc_433: ",$_[ARG1], "\n";
270                  warn "## indetify $NICK\n";  #               warn "## indetify $NICK\n";
271                  $_[KERNEL]->post( $IRC_ALIAS => privmsg => 'nickserv', "IDENTIFY $NICK" );  #               $_[KERNEL]->post( $IRC_ALIAS => privmsg => 'nickserv', "IDENTIFY $NICK" );
272          },  #       },
273          irc_372 => sub {          irc_372 => sub {
274                  print "MOTD: ", $_[ARG1], "\n";                  print "MOTD: ", $_[ARG1], "\n";
275          },          },

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

  ViewVC Help
Powered by ViewVC 1.1.26