/[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 35 by dpavlin, Sun Jun 25 00:10:13 2006 UTC revision 39 by dpavlin, Sun Jun 25 19:16:12 2006 UTC
# Line 10  irc-logger.pl Line 10  irc-logger.pl
10    
11  ./irc-logger.pl  ./irc-logger.pl
12    
13    =head2 Options
14    
15    =over 4
16    
17    =item --import-dircproxy=filename
18    
19    Import log from C<dircproxy> to C<irc-logger> database
20    
21  =head1 DESCRIPTION  =head1 DESCRIPTION
22    
23  log all conversation on irc channel  log all conversation on irc channel
# Line 55  use CGI::Simple; Line 63  use CGI::Simple;
63  use HTML::TagCloud;  use HTML::TagCloud;
64  use POSIX qw/strftime/;  use POSIX qw/strftime/;
65  use HTML::CalendarMonthSimple;  use HTML::CalendarMonthSimple;
66    use Getopt::Long;
67    use DateTime;
68    
69    my $import_dircproxy;
70    GetOptions(
71            'import-dircproxy:s' => \$import_dircproxy,
72    );
73    
74  my $dbh = DBI->connect($DSN,"","", { RaiseError => 1, AutoCommit => 1 }) || die $DBI::errstr;  my $dbh = DBI->connect($DSN,"","", { RaiseError => 1, AutoCommit => 1 }) || die $DBI::errstr;
75    
# Line 85  _SQL_SCHEMA_ Line 100  _SQL_SCHEMA_
100    
101  my $sth = $dbh->prepare(qq{  my $sth = $dbh->prepare(qq{
102  insert into log  insert into log
103          (channel, me, nick, message)          (channel, me, nick, message, time)
104  values (?,?,?,?)  values (?,?,?,?,?)
105  });  });
106    
107  my $tags;  my $tags;
# Line 281  sub get_from_log { Line 296  sub get_from_log {
296          return @msgs;          return @msgs;
297  }  }
298    
299    # tags support
300    
301    my $cloud = HTML::TagCloud->new;
302    
303    =head2 add_tag
304    
305     add_tag( id => 42, message => 'irc message' );
306    
307    =cut
308    
309    sub add_tag {
310            my $arg = {@_};
311    
312            return unless ($arg->{id} && $arg->{message});
313    
314            my $m = $arg->{message};
315            from_to('UTF-8', 'iso-8859-2', $m) if (is_utf8($m));
316    
317            while ($m =~ s#$tag_regex##s) {
318                    my $tag = $1;
319                    next if (! $tag || $tag =~ m/https?:/i);
320                    push @{ $tags->{$tag} }, $arg->{id};
321                    #warn "+tag $tag: $arg->{id}\n";
322                    $cloud->add($tag, "?tag=$tag", scalar @{$tags->{$tag}} + 1);
323            }
324    }
325    
326    =head2 seed_tags
327    
328    Read all tags from database and create in-memory cache for tags
329    
330    =cut
331    
332    sub seed_tags {
333            my $sth = $dbh->prepare(qq{ select id,message from log where message like '%//%' });
334            $sth->execute;
335            while (my $row = $sth->fetchrow_hashref) {
336                    add_tag( %$row );
337            }
338    
339            foreach my $tag (keys %$tags) {
340                    $cloud->add($tag, "?tag=$tag", scalar @{$tags->{$tag}} + 1);
341            }
342    }
343    
344    seed_tags;
345    
346    
347    =head2 save_message
348    
349      save_message(
350            channel => '#foobar',
351            me => 0,
352            nick => 'dpavlin',
353            msg => 'test message',
354            time => '2006-06-25 18:57:18',
355      );
356    
357    C<time> is optional, it will use C<< now() >> if it's not available.
358    
359    C<me> if not specified will be C<0> (not C</me> message)
360    
361    =cut
362    
363    sub save_message {
364            my $a = {@_};
365            $a->{me} ||= 0;
366            $a->{time} ||= strftime($TIMESTAMP,localtime());
367    
368            print
369                    $a->{time}, " ",
370                    $a->{channel}, " ",
371                    $a->{me} ? "***" . $a->{nick} : "<" . $a->{nick} . ">",
372                    " " . $a->{msg} . "\n";
373    
374            from_to($a->{msg}, 'UTF-8', $ENCODING);
375    
376            $sth->execute($a->{channel}, $a->{me}, $a->{nick}, $a->{msg}, $a->{time});
377            add_tag( id => $dbh->last_insert_id(undef,undef,"log",undef),
378                    message => $a->{msg});
379    }
380    
381    if ($import_dircproxy) {
382            open(my $l, $import_dircproxy) || die "can't open $import_dircproxy: $!";
383            warn "importing $import_dircproxy...\n";
384            my $tz_offset = 2 * 60 * 60;    # TZ GMT+2
385            while(<$l>) {
386                    chomp;
387                    if (/^@(\d+)\s(\S+)\s(.+)$/) {
388                            my ($time, $nick, $msg) = ($1,$2,$3);
389    
390                            my $dt = DateTime->from_epoch( epoch => $time + $tz_offset );
391    
392                            my $me = 0;
393                            $me = 1 if ($nick =~ m/^\[\S+]/);
394                            $nick =~ s/^[\[<]([^!]+).*$/$1/;
395    
396                            $msg =~ s/^ACTION\s+// if ($me);
397    
398                            save_message(
399                                    channel => $CHANNEL,
400                                    me => $me,
401                                    nick => $nick,
402                                    msg => $msg,
403                                    time => $dt->ymd . " " . $dt->hms,
404                            ) if ($nick !~ m/^-/);
405    
406                    } else {
407                            warn "can't parse: $_\n";
408                    }
409            }
410            close($l);
411            warn "import over\n";
412            exit;
413    }
414    
415    
416    #
417    # POE handing part
418    #
419    
420  my $SKIPPING = 0;               # if skipping, how many we've done  my $SKIPPING = 0;               # if skipping, how many we've done
421  my $SEND_QUEUE;                 # cache  my $SEND_QUEUE;                 # cache
# Line 305  POE::Session->create( inline_states => Line 440  POE::Session->create( inline_states =>
440                  my $channel = $_[ARG1]->[0];                  my $channel = $_[ARG1]->[0];
441                  my $msg = $_[ARG2];                  my $msg = $_[ARG2];
442    
443                  from_to($msg, 'UTF-8', $ENCODING);                  save_message( channel => $channel, me => 0, nick => $nick, msg => $msg);
   
                 print "$channel: <$nick> $msg\n";  
                 $sth->execute($channel, 0, $nick, $msg);  
                 add_tag( id => $dbh->last_insert_id(undef,undef,"log",undef),  
                         message => $msg);  
444      },      },
445      irc_ctcp_action => sub {      irc_ctcp_action => sub {
446                  my $kernel = $_[KERNEL];                  my $kernel = $_[KERNEL];
# Line 318  POE::Session->create( inline_states => Line 448  POE::Session->create( inline_states =>
448                  my $channel = $_[ARG1]->[0];                  my $channel = $_[ARG1]->[0];
449                  my $msg = $_[ARG2];                  my $msg = $_[ARG2];
450    
451                  from_to($msg, 'UTF-8', $ENCODING);                  save_message( channel => $channel, me => 1, nick => $nick, msg => $msg);
   
                 print "$channel ***$nick $msg\n";  
                 $sth->execute($channel, 1, $nick, $msg);  
                 add_tag( id => $dbh->last_insert_id(undef,undef,"log",undef),  
                         message => $msg);  
452      },      },
453          irc_msg => sub {          irc_msg => sub {
454                  my $kernel = $_[KERNEL];                  my $kernel = $_[KERNEL];
# Line 362  POE::Session->create( inline_states => Line 487  POE::Session->create( inline_states =>
487                          $res .= join(" | ", @users);                          $res .= join(" | ", @users);
488                  } elsif ($msg =~ m/^last.*?\s*(\d*)/i) {                  } elsif ($msg =~ m/^last.*?\s*(\d*)/i) {
489    
490                          foreach my $res (get_from_log( limit => $1 )) {                          foreach my $res (get_from_log( limit => ($1 || 100) )) {
491                                  print "last: $res\n";                                  print "last: $res\n";
492                                  from_to($res, $ENCODING, 'UTF-8');                                  from_to($res, $ENCODING, 'UTF-8');
493                                  $_[KERNEL]->post( $IRC_ALIAS => privmsg => $nick, $res );                                  $_[KERNEL]->post( $IRC_ALIAS => privmsg => $nick, $res );
# Line 485  POE::Session->create( inline_states => Line 610  POE::Session->create( inline_states =>
610     },     },
611    );    );
612    
 # tags support  
   
 my $cloud = HTML::TagCloud->new;  
   
 =head2 add_tag  
   
  add_tag( id => 42, message => 'irc message' );  
   
 =cut  
   
 sub add_tag {  
         my $arg = {@_};  
   
         return unless ($arg->{id} && $arg->{message});  
   
         my $m = $arg->{message};  
         from_to('UTF-8', 'iso-8859-2', $m) if (is_utf8($m));  
   
         while ($m =~ s#$tag_regex##s) {  
                 my $tag = $1;  
                 next if (! $tag || $tag =~ m/https?:/i);  
                 push @{ $tags->{$tag} }, $arg->{id};  
                 #warn "+tag $tag: $arg->{id}\n";  
                 $cloud->add($tag, "?tag=$tag", scalar @{$tags->{$tag}} + 1);  
         }  
 }  
   
 =head2 seed_tags  
   
 Read all tags from database and create in-memory cache for tags  
   
 =cut  
   
 sub seed_tags {  
         my $sth = $dbh->prepare(qq{ select id,message from log where message like '%//%' });  
         $sth->execute;  
         while (my $row = $sth->fetchrow_hashref) {  
                 add_tag( %$row );  
         }  
   
         foreach my $tag (keys %$tags) {  
                 $cloud->add($tag, "?tag=$tag", scalar @{$tags->{$tag}} + 1);  
         }  
 }  
   
 seed_tags;  
   
613  # http server  # http server
614    
615  my $httpd = POE::Component::Server::HTTP->new(  my $httpd = POE::Component::Server::HTTP->new(
# Line 628  sub root_handler { Line 706  sub root_handler {
706                                  fmt => {                                  fmt => {
707                                          date => sub {                                          date => sub {
708                                                  my $date = shift || return;                                                  my $date = shift || return;
709                                                  qq{<hr/><div class="date"><a href="/?date=$date">$date</a></div> '};                                                  qq{<hr/><div class="date"><a href="/?date=$date">$date</a></div>};
710                                          },                                          },
711                                          time => '<span class="time">%s</span> ',                                          time => '<span class="time">%s</span> ',
712                                          time_channel => '<span class="channel">%s %s</span> ',                                          time_channel => '<span class="channel">%s %s</span> ',

Legend:
Removed from v.35  
changed lines
  Added in v.39

  ViewVC Help
Powered by ViewVC 1.1.26