--- trunk/irc-logger.pl 2006/03/02 00:19:12 10 +++ trunk/irc-logger.pl 2006/03/02 00:52:22 11 @@ -72,6 +72,80 @@ values (?,?,?) }); +=head2 get_from_log + + my @messages = get_from_log( + limit => 42, + search => '%what to stuff in ilike%', + ); + +=cut + +sub get_from_log { + my $args = {@_}; + + $args->{limit} ||= 10; + + my $sql = qq{ + select + time::date as date, + time::time as time, + channel, + nick, + message + from log + }; + $sql .= " where message ilike ? " if ($args->{search}); + $sql .= " order by log.time desc"; + $sql .= " limit " . $args->{limit}; + + my $sth = $dbh->prepare( $sql ); + if ($args->{search}) { + $sth->execute( $args->{search} ); + } else { + $sth->execute(); + } + my $last_row = { + date => '', + time => '', + channel => '', + nick => '', + }; + + my @rows; + + while (my $row = $sth->fetchrow_hashref) { + unshift @rows, $row; + } + + my @msgs; + + 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}; + + push @msgs, $msg; + + $last_row = $row; + } + + return @msgs; +} + my $SKIPPING = 0; # if skipping, how many we've done my $SEND_QUEUE; # cache @@ -89,6 +163,7 @@ $_[KERNEL]->post($IRC_ALIAS => join => '#logger'); $_[KERNEL]->yield("heartbeat"); # start heartbeat # $_[KERNEL]->yield("my_add", $_) for keys %FOLLOWS; + $_[KERNEL]->post( $IRC_ALIAS => privmsg => 'nickserv', "IDENTIFY $NICK" ); }, irc_public => sub { my $kernel = $_[KERNEL]; @@ -108,12 +183,13 @@ from_to($msg, 'UTF-8', 'ISO-8859-2'); my $res = "unknown command '$msg', try /msg $NICK help!"; + my @out; print "<< $msg\n"; if ($msg =~ m/^help/i) { - $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"; } elsif ($msg =~ m/^msg\s+(\S+)\s+(.*)$/i) { @@ -137,64 +213,26 @@ $res .= join(" | ", @users); } elsif ($msg =~ m/^last.*?\s*(\d*)/i) { - my $nr = $1 || 10; - - my $sth = $dbh->prepare(qq{ - select - 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; + foreach my $res (get_from_log( limit => $1 )) { + print "last: $res\n"; + from_to($res, 'ISO-8859-2', 'UTF-8'); + $_[KERNEL]->post( $IRC_ALIAS => privmsg => $nick, $res ); } - my @msgs; - - 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}; + $res = ''; - push @msgs, $msg; + } elsif ($msg =~ m/^(search|grep)\s+(.*)$/i) { - $last_row = $row; - } + my $what = $2; - foreach my $res (@msgs) { - print "last: $res\n"; + foreach my $res (get_from_log( limit => 20, search => "%${what}%" )) { + print "search [$what]: $res\n"; from_to($res, 'ISO-8859-2', 'UTF-8'); $_[KERNEL]->post( $IRC_ALIAS => privmsg => $nick, $res ); } $res = ''; + } if ($res) { @@ -218,11 +256,11 @@ warn "## indetify $NICK\n"; $_[KERNEL]->post( $IRC_ALIAS => privmsg => 'nickserv', "IDENTIFY $NICK" ); }, - irc_433 => sub { - print "# irc_433: ",$_[ARG1], "\n"; - warn "## indetify $NICK\n"; - $_[KERNEL]->post( $IRC_ALIAS => privmsg => 'nickserv', "IDENTIFY $NICK" ); - }, +# irc_433 => sub { +# print "# irc_433: ",$_[ARG1], "\n"; +# warn "## indetify $NICK\n"; +# $_[KERNEL]->post( $IRC_ALIAS => privmsg => 'nickserv', "IDENTIFY $NICK" ); +# }, irc_372 => sub { print "MOTD: ", $_[ARG1], "\n"; },