--- trunk/irc-logger.pl 2006/03/01 21:29:14 7 +++ trunk/irc-logger.pl 2006/03/01 22:42:21 8 @@ -22,7 +22,7 @@ my $CONNECT = {Server => 'irc.freenode.net', Nick => $NICK, - Ircname => 'logger: ask dpavlin@rot13.org' + Ircname => "try /msg $NICK help", }; my $CHANNEL = '#razmjenavjestina'; my $IRC_ALIAS = "log"; @@ -107,11 +107,15 @@ my $msg = $_[ARG2]; from_to($msg, 'UTF-8', 'ISO-8859-2'); - my $res = 'unknown command ' . $msg; + my $res = "unknown command '$msg', try /msg $NICK help!"; print "<< $msg\n"; - if ($msg =~ m/^stat.*\s*(\d*)/) { + if ($msg =~ m/^help/i) { + + $res = "usage: /msg $NICK stat - shows user statistics | /msg $NICK last - show backtrace of conversations"; + + } elsif ($msg =~ m/^stat.*?\s*(\d*)/i) { my $nr = $1 || 10; @@ -120,22 +124,86 @@ }); $sth->execute(); $res = "Top $nr users: "; + my @users; while (my $row = $sth->fetchrow_hashref) { - $res .= $row->{nick} . ': ' . $row->{count} . ", "; + push @users,$row->{nick} . ': ' . $row->{count}; } - } + $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 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; + } + + my @msgs; + + foreach my $row (@rows) { - $res =~ s/,\s*$//; - print ">> [$nick] $res\n"; + $row->{time} =~ s#\.\d+##; - from_to($res, 'ISO-8859-2', 'UTF-8'); - $_[KERNEL]->post( $IRC_ALIAS => privmsg => $nick, $res ); + 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; + } + + foreach my $res (@msgs) { + print "last: $res\n"; + from_to($res, 'ISO-8859-2', 'UTF-8'); + $_[KERNEL]->post( $IRC_ALIAS => privmsg => $nick, $res ); + } + + $res = ''; + } + + if ($res) { + print ">> [$nick] $res\n"; + from_to($res, 'ISO-8859-2', 'UTF-8'); + $_[KERNEL]->post( $IRC_ALIAS => privmsg => $nick, $res ); + } }, irc_505 => sub { print "# irc_505: ",$_[ARG1], "\n"; $_[KERNEL]->post( $IRC_ALIAS => privmsg => 'nickserv', "register $NICK" ); warn "## register $NICK\n"; + }, + irc_registered => sub { $_[KERNEL]->post( $IRC_ALIAS => privmsg => 'nickserv', "IDENTIFY $NICK" ); warn "## indetify $NICK\n"; },