/[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 48 by dpavlin, Sat Feb 3 12:50:45 2007 UTC revision 50 by dpavlin, Sun Mar 18 15:37:05 2007 UTC
# Line 90  sub _log { Line 90  sub _log {
90    
91  my $dbh = DBI->connect($DSN,"","", { RaiseError => 1, AutoCommit => 1 }) || die $DBI::errstr;  my $dbh = DBI->connect($DSN,"","", { RaiseError => 1, AutoCommit => 1 }) || die $DBI::errstr;
92    
93  eval {  my $sql_schema = {
94          $dbh->do(qq{ select count(*) from log });          log => '
 };  
   
 if ($@) {  
         warn "creating database table in $DSN\n";  
         $dbh->do(<<'_SQL_SCHEMA_');  
   
95  create table log (  create table log (
96          id serial,          id serial,
97          time timestamp default now(),          time timestamp default now(),
# Line 111  create table log ( Line 105  create table log (
105  create index log_time on log(time);  create index log_time on log(time);
106  create index log_channel on log(channel);  create index log_channel on log(channel);
107  create index log_nick on log(nick);  create index log_nick on log(nick);
108            ',
109            meta => '
110    create table meta (
111            nick text not null,
112            channel text not null,
113            name text not null,
114            value text,
115            changed timestamp default now(),
116            primary key(nick,channel,name)
117    );
118            ',
119    };
120    
121    foreach my $table ( keys %$sql_schema ) {
122    
123            eval {
124                    $dbh->do(qq{ select count(*) from $table });
125            };
126    
127  _SQL_SCHEMA_          if ($@) {
128                    warn "creating database table $table in $DSN\n";
129                    $dbh->do( $sql_schema->{ $table } );
130            }
131  }  }
132    
133    
134    =head2 meta
135    
136    Set or get some meta data into database
137    
138            meta('nick','channel','var_name', $var_value );
139    
140            $var_value = meta('nick','channel','var_name');
141            ( $var_value, $changed ) = meta('nick','channel','var_name');
142    
143    =cut
144    
145    sub meta {
146            my ($nick,$channel,$name,$value) = @_;
147    
148            # normalize channel name
149            $channel =~ s/^#//;
150    
151            if (defined($value)) {
152    
153                    my $sth = $dbh->prepare(qq{ update meta set value = ?, changed = now() where nick = ? and channel = ? and name = ? });
154    
155                    eval { $sth->execute( $value, $nick, $channel, $name ) };
156    
157                    # error or no result
158                    if ( $@ || ! $sth->rows ) {
159                            $sth = $dbh->prepare(qq{ insert into meta (value,nick,channel,name,changed) values (?,?,?,?,now()) });
160                            $sth->execute( $value, $nick, $channel, $name );
161                            _log "created $nick/$channel/$name = $value";
162                    } else {
163                            _log "updated $nick/$channel/$name = $value ";
164                    }
165    
166                    return $value;
167    
168            } else {
169    
170                    my $sth = $dbh->prepare(qq{ select value,changed from meta where nick = ? and channel = ? and name = ? });
171                    $sth->execute( $nick, $channel, $name );
172                    my ($v,$c) = $sth->fetchrow_array;
173                    _log "fetched $nick/$channel/$name = $v [$c]";
174                    return ($v,$c) if wantarray;
175                    return $v;
176    
177            }
178    }
179    
180    
181    
182  my $sth = $dbh->prepare(qq{  my $sth = $dbh->prepare(qq{
183  insert into log  insert into log
184          (channel, me, nick, message, time)          (channel, me, nick, message, time)
185  values (?,?,?,?,?)  values (?,?,?,?,?)
186  });  });
187    
188    
189  my $tags;  my $tags;
190  my $tag_regex = '\b([\w-_]+)//';  my $tag_regex = '\b([\w-_]+)//';
191    
# Line 405  sub save_message { Line 470  sub save_message {
470                  message => $a->{msg});                  message => $a->{msg});
471  }  }
472    
473    
474  if ($import_dircproxy) {  if ($import_dircproxy) {
475          open(my $l, $import_dircproxy) || die "can't open $import_dircproxy: $!";          open(my $l, $import_dircproxy) || die "can't open $import_dircproxy: $!";
476          warn "importing $import_dircproxy...\n";          warn "importing $import_dircproxy...\n";
# Line 469  POE::Session->create( inline_states => Line 535  POE::Session->create( inline_states =>
535                  my $msg = $_[ARG2];                  my $msg = $_[ARG2];
536    
537                  save_message( channel => $channel, me => 0, nick => $nick, msg => $msg);                  save_message( channel => $channel, me => 0, nick => $nick, msg => $msg);
538                    meta( $nick, $channel, 'last-msg', $msg );
539      },      },
540      irc_ctcp_action => sub {      irc_ctcp_action => sub {
541                  my $kernel = $_[KERNEL];                  my $kernel = $_[KERNEL];
# Line 477  POE::Session->create( inline_states => Line 544  POE::Session->create( inline_states =>
544                  my $msg = $_[ARG2];                  my $msg = $_[ARG2];
545    
546                  save_message( channel => $channel, me => 1, nick => $nick, msg => $msg);                  save_message( channel => $channel, me => 1, nick => $nick, msg => $msg);
547    
548                    if ( my $twitter = ( $nick, $channel, 'twitter' ) ) {
549                            _log("FIXME: send twitter for $nick on $channel [$twitter]");
550                    }
551    
552      },      },
553          irc_ping => sub {          irc_ping => sub {
554                  warn "pong ", $_[ARG0], $/;                  warn "pong ", $_[ARG0], $/;
# Line 486  POE::Session->create( inline_states => Line 558  POE::Session->create( inline_states =>
558                  my $kernel = $_[KERNEL];                  my $kernel = $_[KERNEL];
559                  my $nick = (split /!/, $_[ARG0])[0];                  my $nick = (split /!/, $_[ARG0])[0];
560                  my $channel = $_[ARG1];                  my $channel = $_[ARG1];
                   
561    
562                  warn "invited to $channel by $nick";                  warn "invited to $channel by $nick";
563    
# Line 498  POE::Session->create( inline_states => Line 569  POE::Session->create( inline_states =>
569                  my $kernel = $_[KERNEL];                  my $kernel = $_[KERNEL];
570                  my $nick = (split /!/, $_[ARG0])[0];                  my $nick = (split /!/, $_[ARG0])[0];
571                  my $msg = $_[ARG2];                  my $msg = $_[ARG2];
572                    my $channel = $_[ARG1]->[0];
573                  from_to($msg, 'UTF-8', $ENCODING);                  from_to($msg, 'UTF-8', $ENCODING);
574    
575                  my $res = "unknown command '$msg', try /msg $NICK help!";                  my $res = "unknown command '$msg', try /msg $NICK help!";
# Line 538  POE::Session->create( inline_states => Line 610  POE::Session->create( inline_states =>
610                          $res .= join(" | ", @users);                          $res .= join(" | ", @users);
611                  } elsif ($msg =~ m/^last.*?\s*(\d*)/i) {                  } elsif ($msg =~ m/^last.*?\s*(\d*)/i) {
612    
613                          foreach my $res (get_from_log( limit => ($1 || 100) )) {                          my $limit = $1 || meta( $nick, $channel, 'last-size' ) || 10;
614    
615                            foreach my $res (get_from_log( limit => $limit )) {
616                                  _log "last: $res";                                  _log "last: $res";
617                                  from_to($res, $ENCODING, 'UTF-8');                                  from_to($res, $ENCODING, 'UTF-8');
618                                  $_[KERNEL]->post( $IRC_ALIAS => privmsg => $nick, $res );                                  $_[KERNEL]->post( $IRC_ALIAS => privmsg => $nick, $res );
# Line 595  POE::Session->create( inline_states => Line 669  POE::Session->create( inline_states =>
669    
670                  } elsif ($msg =~ m/^ping/) {                  } elsif ($msg =~ m/^ping/) {
671                          $res = "ping = " . dump( $ping );                          $res = "ping = " . dump( $ping );
672                    } elsif ($msg =~ m/^(?:twitter)\s+(\S+)\s+(.*?)/) {
673                            if ( defined( $2 ) ) {
674                                    meta($nick, $channel, 'twitter', "$1\t$2");
675                                    $res = "saved twitter auth for $1 -- /me on $channel will auto-update twitter status";
676                            } else {
677                                    meta($nick, $channel, 'twitter', '' );
678                                    $res = "removed twitter status update for /me on $channel";
679                            }
680                    } elsif ($msg =~ m/^conf(?:ig)*\s*(last-size)*\s*(\d*)/) {
681                            if ( ! defined( $1 ) ) {
682                                    my $sth = $dbh->prepare(qq{ select name,value,changed from meta where nick = ? and channel = ? });
683                                    $sth->execute( $nick, $channel );
684                                    $res = "config for $nick ";
685                                    while ( my ($n,$v) = $sth->fetchrow_array ) {
686                                            $res .= "| $n = $v";
687                                    }
688                            } elsif ( defined( $2 ) ) {
689                                    meta( $nick, $channel, $1, $2 );
690                                    $res = "saved $1 = $2";
691                            } else {
692                                    my $val = meta( $nick, $channel, $1 );
693                                    $res = "current $1 = " . ( $val ? $val : 'undefined' );
694                            }
695                  }                  }
696    
697                  if ($res) {                  if ($res) {

Legend:
Removed from v.48  
changed lines
  Added in v.50

  ViewVC Help
Powered by ViewVC 1.1.26