/[irc-logger]/trunk/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/irc-logger.pl

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

revision 20 by dpavlin, Tue Mar 14 17:17:53 2006 UTC revision 21 by dpavlin, Sat Mar 18 16:02:32 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 99  values (?,?,?,?) Line 99  values (?,?,?,?)
99                          # modify message content                          # modify message content
100                          return shift;                          return shift;
101                  }                  }
102          }          },
103            context => 5,
104   );   );
105    
106  Order is important. Fields are first passed through C<filter> (if available) and  Order is important. Fields are first passed through C<filter> (if available) and
107  then throgh C<< sprintf($fmt->{message}, $message >> if available.  then throgh C<< sprintf($fmt->{message}, $message >> if available.
108    
109    C<context> defines number of messages around each search hit for display.
110    
111  =cut  =cut
112    
113  sub get_from_log {  sub get_from_log {
# Line 120  sub get_from_log { Line 123  sub get_from_log {
123                  message => '%s',                  message => '%s',
124          };          };
125    
126          my $sql = qq{          my $sql_message = qq{
127                  select                  select
128                          time::date as date,                          time::date as date,
129                          time::time as time,                          time::time as time,
# Line 130  sub get_from_log { Line 133  sub get_from_log {
133                          message                          message
134                  from log                  from log
135          };          };
136    
137            my $sql_context = qq{
138                    select
139                            id
140                    from log
141            };
142    
143            my $context = $1 if ($args->{search} && $args->{search} =~ s/\s*\+(\d+)\s*/ /);
144    
145            my $sql = $context ? $sql_context : $sql_message;
146    
147          $sql .= " where message ilike ? " if ($args->{search});          $sql .= " where message ilike ? " if ($args->{search});
148          $sql .= " order by log.time desc";          $sql .= " order by log.time desc";
149          $sql .= " limit " . $args->{limit};          $sql .= " limit " . $args->{limit};
150    
151          my $sth = $dbh->prepare( $sql );          my $sth = $dbh->prepare( $sql );
152          if ($args->{search}) {          if (my $search = $args->{search}) {
153                  $sth->execute( '%' . $args->{search} . '%' );                  $search =~ s/^\s+//;
154                  warn "search for '$args->{search}' returned ", $sth->rows, " results\n";                  $search =~ s/\s+$//;
155                    $sth->execute( '%' . $search . '%' );
156                    warn "search for '$search' returned ", $sth->rows, " results ", $context || '', "\n";
157          } else {          } else {
158                  $sth->execute();                  $sth->execute();
159          }          }
# Line 158  sub get_from_log { Line 174  sub get_from_log {
174                  "Showing " . ($#rows + 1) . " messages..."                  "Showing " . ($#rows + 1) . " messages..."
175          );          );
176    
177            if ($context) {
178                    my @ids = @rows;
179                    @rows = ();
180    
181                    my $last_to = 0;
182    
183                    my $sth = $dbh->prepare( $sql_message . qq{ where id >= ? and id < ? } );
184                    foreach my $row_id (sort { $a->{id} <=> $b->{id} } @ids) {
185                            my $id = $row_id->{id} || die "can't find id in row";
186            
187                            my ($from, $to) = ($id - $context, $id + $context);
188                            $from = $last_to if ($from < $last_to);
189                            $last_to = $to;
190                            $sth->execute( $from, $to );
191    
192                            #warn "## id: $id from: $from to: $to returned: ", $sth->rows, "\n";
193    
194                            while (my $row = $sth->fetchrow_hashref) {
195                                    push @rows, $row;
196                            }
197    
198                    }
199            }
200    
201          foreach my $row (@rows) {          foreach my $row (@rows) {
202    
203                  $row->{time} =~ s#\.\d+##;                  $row->{time} =~ s#\.\d+##;
# Line 297  POE::Session->create Line 337  POE::Session->create
337    
338                          $res = '';                          $res = '';
339    
340                  } elsif ($msg =~ m/^(search|grep)\s+(.*)$/i) {                  } elsif ($msg =~ m/^(search|grep)\s+(.*)\s*$/i) {
341    
342                          my $what = $2;                          my $what = $2;
343    
344                          foreach my $res (get_from_log( limit => 20, search => $what )) {                          foreach my $res (get_from_log(
345                                            limit => 20,
346                                            search => $what,
347                                    )) {
348                                  print "search [$what]: $res\n";                                  print "search [$what]: $res\n";
349                                  from_to($res, $ENCODING, 'UTF-8');                                  from_to($res, $ENCODING, 'UTF-8');
350                                  $_[KERNEL]->post( $IRC_ALIAS => privmsg => $nick, $res );                                  $_[KERNEL]->post( $IRC_ALIAS => privmsg => $nick, $res );

Legend:
Removed from v.20  
changed lines
  Added in v.21

  ViewVC Help
Powered by ViewVC 1.1.26