7 |
use DBI; |
use DBI; |
8 |
use Regexp::Common qw /URI/; |
use Regexp::Common qw /URI/; |
9 |
use CGI::Simple; |
use CGI::Simple; |
|
use HTML::TagCloud; |
|
10 |
use POSIX qw/strftime/; |
use POSIX qw/strftime/; |
11 |
use HTML::CalendarMonthSimple; |
use HTML::CalendarMonthSimple; |
12 |
use Getopt::Long; |
use Getopt::Long; |
17 |
use Carp qw/confess/; |
use Carp qw/confess/; |
18 |
use XML::Feed; |
use XML::Feed; |
19 |
use DateTime::Format::Flexible; |
use DateTime::Format::Flexible; |
|
use IPC::DirQueue; |
|
|
use File::Slurp; |
|
20 |
use Encode; |
use Encode; |
21 |
|
|
22 |
=head1 NAME |
=head1 NAME |
56 |
ircname => 'Anna the bot: try /msg irc-logger help', |
ircname => 'Anna the bot: try /msg irc-logger help', |
57 |
}; |
}; |
58 |
|
|
|
my $queue_dir = './queue'; |
|
|
|
|
59 |
my $HOSTNAME = `hostname -f`; |
my $HOSTNAME = `hostname -f`; |
60 |
chomp($HOSTNAME); |
chomp($HOSTNAME); |
61 |
|
|
108 |
GetOptions( |
GetOptions( |
109 |
'import-dircproxy:s' => \$import_dircproxy, |
'import-dircproxy:s' => \$import_dircproxy, |
110 |
'log:s' => \$log_path, |
'log:s' => \$log_path, |
|
'queue:s' => \$queue_dir, |
|
111 |
'debug!' => \$debug, |
'debug!' => \$debug, |
112 |
); |
); |
113 |
|
|
121 |
|
|
122 |
open(STDOUT, '>', $log_path) && warn "log to $log_path: $!\n"; |
open(STDOUT, '>', $log_path) && warn "log to $log_path: $!\n"; |
123 |
|
|
|
# queue |
|
|
|
|
|
if ( ! -d $queue_dir ) { |
|
|
warn "## creating queue directory $queue_dir"; |
|
|
mkdir $queue_dir or die "can't create queue directory $queue_dir: $!"; |
|
|
} |
|
|
|
|
|
my $dq = IPC::DirQueue->new({ dir => $queue_dir }); |
|
124 |
|
|
125 |
# HTML formatters |
# HTML formatters |
126 |
|
|
127 |
my %escape = ('<'=>'<', '>'=>'>', '&'=>'&', '"'=>'"'); |
my %escape = ('<'=>'<', '>'=>'>', '&'=>'&', '"'=>'"'); |
128 |
my $escape_re = join '|' => keys %escape; |
my $escape_re = join '|' => keys %escape; |
129 |
|
|
130 |
my $tag_regex = '\b([\w-_]+)//'; |
my $tag_regex = '\b([\w\-_]+)//'; |
131 |
|
|
132 |
my %nick_enumerator; |
my %nick_enumerator; |
133 |
my $max_color = 0; |
my $max_color = 0; |
525 |
|
|
526 |
# tags support |
# tags support |
527 |
|
|
528 |
my $cloud = HTML::TagCloud->new; |
my $cloud = TagCloud->new; |
529 |
|
$cloud->seed_tags; |
|
=head2 add_tag |
|
|
|
|
|
add_tag( id => 42, message => 'irc message', nick => 'foobar' [, me => 1 ] ); |
|
|
|
|
|
=cut |
|
|
|
|
|
my @last_tags; |
|
|
|
|
|
sub add_tag { |
|
|
my $arg = {@_}; |
|
|
|
|
|
return unless ($arg->{id} && $arg->{message}); |
|
|
|
|
|
my $m = $arg->{message}; |
|
|
|
|
|
my @tags; |
|
|
|
|
|
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, "$url?tag=$tag", scalar @{$tags->{$tag}} + 1); |
|
|
push @tags, $tag; |
|
|
|
|
|
} |
|
|
|
|
|
if ( @tags ) { |
|
|
pop @last_tags if $#last_tags == $last_x_tags; |
|
|
unshift @last_tags, { tags => [ @tags ], %$arg }; |
|
|
} |
|
|
|
|
|
} |
|
|
|
|
|
=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,nick,me,time from log where message like '%//%' order by time asc }); |
|
|
$sth->execute; |
|
|
while (my $row = $sth->fetchrow_hashref) { |
|
|
add_tag( %$row ); |
|
|
} |
|
|
|
|
|
foreach my $tag (keys %$tags) { |
|
|
$cloud->add($tag, "$url?tag=$tag", scalar @{$tags->{$tag}} + 1); |
|
|
} |
|
|
} |
|
|
|
|
|
seed_tags; |
|
|
|
|
530 |
|
|
531 |
=head2 save_message |
=head2 save_message |
532 |
|
|
555 |
$a->{me} ? "***" . $a->{nick} : "<" . $a->{nick} . ">", |
$a->{me} ? "***" . $a->{nick} : "<" . $a->{nick} . ">", |
556 |
" " . $a->{message}; |
" " . $a->{message}; |
557 |
|
|
558 |
$sth_insert_log->execute($a->{channel}, $a->{me}, $a->{nick}, $a->{message}, $a->{time}); |
eval { $sth_insert_log->execute($a->{channel}, $a->{me}, $a->{nick}, $a->{message}, $a->{time}); }; |
559 |
add_tag( id => $dbh->last_insert_id(undef,undef,"log",undef), %$a ); |
_log "ERROR: can't archive ", $a->{message} if $@; |
560 |
|
$cloud->add_tag( id => $dbh->last_insert_id(undef,undef,"log",undef), %$a ); |
561 |
} |
} |
562 |
|
|
563 |
|
|
616 |
=cut |
=cut |
617 |
|
|
618 |
sub rss_parse_xml { |
sub rss_parse_xml { |
619 |
my ($args) = @_; |
my ($kernel,$args) = @_; |
620 |
|
|
621 |
warn "## rss_parse_xml ",dump( @_ ) if $debug; |
warn "## rss_parse_xml ",dump( $args ) if $debug; |
622 |
|
|
623 |
# how many messages to send out when feed is seen for the first time? |
# how many messages to send out when feed is seen for the first time? |
624 |
my $send_rss_msgs = $args->{send_rss_msgs}; |
my $send_rss_msgs = $args->{send_rss_msgs}; |
625 |
$send_rss_msgs = 1 if ! defined $send_rss_msgs; |
$send_rss_msgs = 1 if ! defined $send_rss_msgs; |
626 |
|
|
627 |
_log "RSS fetch first $send_rss_msgs items from", $args->{url}; |
warn "## RSS fetch first $send_rss_msgs items from", $args->{url} if $debug; |
628 |
|
|
629 |
my $feed = XML::Feed->parse( \$args->{xml} ); |
my $feed = XML::Feed->parse( \$args->{xml} ); |
630 |
if ( ! $feed ) { |
if ( ! $feed ) { |
667 |
$msg .= prefix( ' | ' , $entry->title ); |
$msg .= prefix( ' | ' , $entry->title ); |
668 |
$msg .= prefix( ' | ' , $link ); |
$msg .= prefix( ' | ' , $link ); |
669 |
# $msg .= prefix( ' id ' , $entry->id ); |
# $msg .= prefix( ' id ' , $entry->id ); |
670 |
|
my @categories = $entry->category; |
671 |
|
warn "## category = ", dump( @categories ) if $debug; |
672 |
if ( my $tags = $entry->category ) { |
if ( my $tags = $entry->category ) { |
673 |
|
$tags = join(' ', @$tags) if ref($tags) eq 'ARRAY'; |
674 |
$tags =~ s!^\s+!!; |
$tags =~ s!^\s+!!; |
675 |
$tags =~ s!\s*$! !; |
$tags =~ s!\s*$! !; |
676 |
$tags =~ s!,?\s+!// !g; |
$tags =~ s!,?\s+!// !g; |
687 |
my ( $type, $to ) = ( 'notice', $args->{channel} ); |
my ( $type, $to ) = ( 'notice', $args->{channel} ); |
688 |
( $type, $to ) = ( 'privmsg', $args->{nick} ) if $args->{private}; |
( $type, $to ) = ( 'privmsg', $args->{nick} ) if $args->{private}; |
689 |
|
|
690 |
_log("RSS generated $type to $to:", $msg); |
_log(">> RSS $type to $to:", $msg); |
691 |
# XXX enqueue message to send later |
$kernel->post( $irc => $type => $to => $msg ); |
|
sub enqueue_post { |
|
|
my $post = dump( @_ ); |
|
|
warn "## queue_post $post\n" if $debug; |
|
|
$dq->enqueue_string( $post ); |
|
|
} |
|
|
enqueue_post( $type => $to => $msg ); |
|
692 |
|
|
693 |
$updates++; |
$updates++; |
694 |
} |
} |
699 |
$sql .= qq{where id = } . $args->{id}; |
$sql .= qq{where id = } . $args->{id}; |
700 |
eval { $dbh->do( $sql ) }; |
eval { $dbh->do( $sql ) }; |
701 |
|
|
702 |
_log "RSS got $total items of which $updates new from", $args->{url}; |
_log "RSS $updates/$total new items from", $args->{url}; |
703 |
|
|
704 |
return $updates; |
return $updates; |
705 |
} |
} |
742 |
$_stat->{rss}->{last_poll} = time(); |
$_stat->{rss}->{last_poll} = time(); |
743 |
_log rss_fetch_all( $kernel ); |
_log rss_fetch_all( $kernel ); |
744 |
} |
} |
745 |
# XXX send queue messages |
} |
746 |
while ( my $job = $dq->pickup_queued_job() ) { |
|
747 |
my $data = read_file( $job->get_data_path ) || die "can't load ", $job->get_data_path, ": $!"; |
sub process_command { |
748 |
my @data = eval $data; |
my ( $kernel, $nick, $channel, $msg ) = @_; |
749 |
_log "IRC post from queue:", @data; |
|
750 |
$kernel->post( $irc => @data ); |
my $res = "unknown command '$msg', try /msg $NICK help!"; |
751 |
$job->finish; |
|
752 |
warn "## done queued job: ",dump( @data ) if $debug; |
if ($msg =~ m/^help/i) { |
753 |
|
|
754 |
|
$res = "usage: /msg $NICK comand | commands: stat - user/message stat | last - show backtrace | grep foobar - find foobar"; |
755 |
|
|
756 |
|
} elsif ($msg =~ m/^(privmsg|notice)\s+(\S+)\s+(.*)$/i) { |
757 |
|
|
758 |
|
_log ">> /$1 $2 $3"; |
759 |
|
$kernel->post( $irc => $1 => $2, $3 ); |
760 |
|
$res = ''; |
761 |
|
|
762 |
|
} elsif ($msg =~ m/^stat.*?\s*(\d*)/i) { |
763 |
|
|
764 |
|
my $nr = $1 || 10; |
765 |
|
|
766 |
|
my $sth = $dbh->prepare(qq{ |
767 |
|
select |
768 |
|
trim(both '_' from nick) as nick, |
769 |
|
count(*) as count, |
770 |
|
sum(length(message)) as len |
771 |
|
from log |
772 |
|
group by trim(both '_' from nick) |
773 |
|
order by len desc,count desc |
774 |
|
limit $nr |
775 |
|
}); |
776 |
|
$sth->execute(); |
777 |
|
$res = "Top $nr users: "; |
778 |
|
my @users; |
779 |
|
while (my $row = $sth->fetchrow_hashref) { |
780 |
|
push @users,$row->{nick} . ': ' . $row->{count} . '/' . $row->{len} . '=' . sprintf("%.2f", $row->{len}/$row->{count}); |
781 |
|
} |
782 |
|
$res .= join(" | ", @users); |
783 |
|
} elsif ($msg =~ m/^last.*?\s*(\d*)/i) { |
784 |
|
|
785 |
|
my $limit = $1 || meta( $nick, $channel, 'last-size' ) || 10; |
786 |
|
|
787 |
|
foreach my $res (get_from_log( limit => $limit )) { |
788 |
|
_log "last: $res"; |
789 |
|
$kernel->post( $irc => privmsg => $nick, $res ); |
790 |
|
} |
791 |
|
|
792 |
|
$res = ''; |
793 |
|
|
794 |
|
} elsif ($msg =~ m/^(search|grep)\s+(.*)\s*$/i) { |
795 |
|
|
796 |
|
my $what = $2; |
797 |
|
|
798 |
|
foreach my $res (get_from_log( |
799 |
|
limit => 20, |
800 |
|
search => $what, |
801 |
|
)) { |
802 |
|
_log "search [$what]: $res"; |
803 |
|
$kernel->post( $irc => privmsg => $nick, $res ); |
804 |
|
} |
805 |
|
|
806 |
|
$res = ''; |
807 |
|
|
808 |
|
} elsif ($msg =~ m/^(?:count|poll)\s+(.*)(?:\s+(\d+))?\s*$/i) { |
809 |
|
|
810 |
|
my ($what,$limit) = ($1,$2); |
811 |
|
$limit ||= 100; |
812 |
|
|
813 |
|
my $stat; |
814 |
|
|
815 |
|
foreach my $res (get_from_log( |
816 |
|
limit => $limit, |
817 |
|
search => $what, |
818 |
|
full_rows => 1, |
819 |
|
)) { |
820 |
|
while ($res->{message} =~ s/\Q$what\E(\+|\-)//) { |
821 |
|
$stat->{vote}->{$1}++; |
822 |
|
$stat->{from}->{ $res->{nick} }++; |
823 |
|
} |
824 |
|
} |
825 |
|
|
826 |
|
my @nicks; |
827 |
|
foreach my $nick (sort { $stat->{from}->{$a} <=> $stat->{from}->{$b} } keys %{ $stat->{from} }) { |
828 |
|
push @nicks, $nick . ( $stat->{from}->{$nick} == 1 ? '' : |
829 |
|
"(" . $stat->{from}->{$nick} . ")" |
830 |
|
); |
831 |
|
} |
832 |
|
|
833 |
|
$res = |
834 |
|
"$what ++ " . ( $stat->{vote}->{'+'} || 0 ) . |
835 |
|
" : " . ( $stat->{vote}->{'-'} || 0 ) . " --" . |
836 |
|
" from " . ( join(", ", @nicks) || 'nobody' ); |
837 |
|
|
838 |
|
$kernel->post( $irc => notice => $nick, $res ); |
839 |
|
|
840 |
|
} elsif ($msg =~ m/^ping/) { |
841 |
|
$res = "ping = " . dump( $_stat->{ping} ); |
842 |
|
} elsif ($msg =~ m/^conf(?:ig)*\s*(last-size|twitter)*\s*(.*)/) { |
843 |
|
if ( ! defined( $1 ) ) { |
844 |
|
my $sth = $dbh->prepare(qq{ select name,value,changed from meta where nick = ? and channel = ? }); |
845 |
|
$sth->execute( $nick, $channel ); |
846 |
|
$res = "config for $nick on $channel"; |
847 |
|
while ( my ($n,$v) = $sth->fetchrow_array ) { |
848 |
|
$res .= " | $n = $v"; |
849 |
|
} |
850 |
|
} elsif ( ! $2 ) { |
851 |
|
my $val = meta( $nick, $channel, $1 ); |
852 |
|
$res = "current $1 = " . ( $val ? $val : 'undefined' ); |
853 |
|
} else { |
854 |
|
my $validate = { |
855 |
|
'last-size' => qr/^\d+/, |
856 |
|
'twitter' => qr/^\w+\s+\w+/, |
857 |
|
}; |
858 |
|
|
859 |
|
my ( $op, $val ) = ( $1, $2 ); |
860 |
|
|
861 |
|
if ( my $regex = $validate->{$op} ) { |
862 |
|
if ( $val =~ $regex ) { |
863 |
|
meta( $nick, $channel, $op, $val ); |
864 |
|
$res = "saved $op = $val"; |
865 |
|
} else { |
866 |
|
$res = "config option $op = $val doesn't validate against $regex"; |
867 |
|
} |
868 |
|
} else { |
869 |
|
$res = "config option $op doesn't exist"; |
870 |
|
} |
871 |
|
} |
872 |
|
} elsif ($msg =~ m/^rss-update/) { |
873 |
|
$res = rss_fetch_all( $kernel ); |
874 |
|
} elsif ($msg =~ m/^rss-list/) { |
875 |
|
my $sth = $dbh->prepare(qq{ select url,name,last_update,active,channel,nick,private from feeds }); |
876 |
|
$sth->execute; |
877 |
|
while (my @row = $sth->fetchrow_array) { |
878 |
|
$kernel->post( $irc => privmsg => $nick, join(' | ',@row) ); |
879 |
|
} |
880 |
|
$res = ''; |
881 |
|
} elsif ($msg =~ m!^rss-(add|remove|stop|start|clean)(?:-(private))?\s+(http://\S+)\s*(.*)!) { |
882 |
|
my ( $command, $sub, $url, $arg ) = ( $1,$2,$3,$4 ); |
883 |
|
|
884 |
|
my $channel = $1 if ( $arg =~ s/\s*(#\S+)\s*// ); |
885 |
|
$channel = $nick if $sub eq 'private'; |
886 |
|
|
887 |
|
my $sql = { |
888 |
|
add => qq{ insert into feeds (url,name,channel,nick,private) values (?,?,?,?,?) }, |
889 |
|
remove => qq{ delete from feeds where url = ? and nick = ? }, |
890 |
|
start => qq{ update feeds set active = true where url = ? }, |
891 |
|
stop => qq{ update feeds set active = false where url = ? }, |
892 |
|
clean => qq{ update feeds set last_update = now() - delay where url = ? }, |
893 |
|
}; |
894 |
|
|
895 |
|
if ( $command eq 'add' && ! $channel ) { |
896 |
|
$res = "ERROR: got '$msg' which doesn't have #channel in it, ignoring!"; |
897 |
|
} elsif (my $q = $sql->{$command} ) { |
898 |
|
my $sth = $dbh->prepare( $q ); |
899 |
|
my @data = ( $url ); |
900 |
|
if ( $command eq 'add' ) { |
901 |
|
push @data, ( $arg, $channel, $nick, $sub eq 'private' ? 1 : 0 ); |
902 |
|
} elsif ( $command eq 'remove' ) { |
903 |
|
push @data, $nick; |
904 |
|
} |
905 |
|
warn "## $command SQL $q with ",dump( @data ),"\n"; |
906 |
|
eval { $sth->execute( @data ) }; |
907 |
|
if ($@) { |
908 |
|
$res = "ERROR: $@"; |
909 |
|
} else { |
910 |
|
$res = "OK, RSS executed $command" . |
911 |
|
( $sub ? "-$sub " : ' ' ) . |
912 |
|
( $channel ? "on $channel " : '' ) . |
913 |
|
"url $url"; |
914 |
|
if ( $command eq 'clean' ) { |
915 |
|
my $seen = $_stat->{rss}->{seen} || die "no seen?"; |
916 |
|
my $want_link = $_stat->{rss}->{url2link}->{$url} || warn "no url2link($url)"; |
917 |
|
foreach my $c ( keys %$seen ) { |
918 |
|
my $c_hash = $seen->{$c} || die "no seen->{$c}"; |
919 |
|
die "not HASH with rss links but ", dump($c_hash) unless ref($c_hash) eq 'HASH'; |
920 |
|
foreach my $link ( keys %$c_hash ) { |
921 |
|
next unless $link eq $want_link; |
922 |
|
_log "RSS removed seen $c $url $link"; |
923 |
|
} |
924 |
|
} |
925 |
|
} elsif ( $command eq 'add' ) { |
926 |
|
rss_fetch_all( $kernel ); |
927 |
|
} |
928 |
|
} |
929 |
|
} else { |
930 |
|
$res = "ERROR: don't know what to do with: $msg"; |
931 |
|
} |
932 |
|
} elsif ($msg =~ m/^rss-clean/) { |
933 |
|
# this makes sense because we didn't catch rss-clean http://... before! |
934 |
|
$_stat->{rss} = undef; |
935 |
|
$dbh->do( qq{ update feeds set last_update = now() - delay } ); |
936 |
|
$res = rss_fetch_all( $kernel ); |
937 |
} |
} |
938 |
|
|
939 |
|
return $res; |
940 |
} |
} |
941 |
|
|
942 |
POE::Session->create( inline_states => { |
POE::Session->create( inline_states => { |
1003 |
irc_msg => sub { |
irc_msg => sub { |
1004 |
my $kernel = $_[KERNEL]; |
my $kernel = $_[KERNEL]; |
1005 |
my $nick = (split /!/, $_[ARG0])[0]; |
my $nick = (split /!/, $_[ARG0])[0]; |
|
my $msg = $_[ARG2]; |
|
1006 |
my $channel = $_[ARG1]->[0]; |
my $channel = $_[ARG1]->[0]; |
1007 |
|
my $msg = $_[ARG2]; |
1008 |
warn "# ARG = ",dump( @_[ARG0,ARG1,ARG2] ) if $debug; |
warn "# ARG = ",dump( @_[ARG0,ARG1,ARG2] ) if $debug; |
1009 |
|
|
|
my $res = "unknown command '$msg', try /msg $NICK help!"; |
|
|
my @out; |
|
|
|
|
1010 |
_log "<< $msg"; |
_log "<< $msg"; |
1011 |
|
|
1012 |
if ($msg =~ m/^help/i) { |
my $res = process_command( $_[KERNEL], $nick, $channel, $msg ); |
|
|
|
|
$res = "usage: /msg $NICK comand | commands: stat - user/message stat | last - show backtrace | grep foobar - find foobar"; |
|
|
|
|
|
} elsif ($msg =~ m/^(privmsg|notice)\s+(\S+)\s+(.*)$/i) { |
|
|
|
|
|
_log ">> /$1 $2 $3"; |
|
|
$_[KERNEL]->post( $irc => $1 => $2, $3 ); |
|
|
$res = ''; |
|
|
|
|
|
} elsif ($msg =~ m/^stat.*?\s*(\d*)/i) { |
|
|
|
|
|
my $nr = $1 || 10; |
|
|
|
|
|
my $sth = $dbh->prepare(qq{ |
|
|
select |
|
|
trim(both '_' from nick) as nick, |
|
|
count(*) as count, |
|
|
sum(length(message)) as len |
|
|
from log |
|
|
group by trim(both '_' from nick) |
|
|
order by len desc,count desc |
|
|
limit $nr |
|
|
}); |
|
|
$sth->execute(); |
|
|
$res = "Top $nr users: "; |
|
|
my @users; |
|
|
while (my $row = $sth->fetchrow_hashref) { |
|
|
push @users,$row->{nick} . ': ' . $row->{count} . '/' . $row->{len} . '=' . sprintf("%.2f", $row->{len}/$row->{count}); |
|
|
} |
|
|
$res .= join(" | ", @users); |
|
|
} elsif ($msg =~ m/^last.*?\s*(\d*)/i) { |
|
|
|
|
|
my $limit = $1 || meta( $nick, $channel, 'last-size' ) || 10; |
|
|
|
|
|
foreach my $res (get_from_log( limit => $limit )) { |
|
|
_log "last: $res"; |
|
|
$_[KERNEL]->post( $irc => privmsg => $nick, $res ); |
|
|
} |
|
|
|
|
|
$res = ''; |
|
|
|
|
|
} elsif ($msg =~ m/^(search|grep)\s+(.*)\s*$/i) { |
|
|
|
|
|
my $what = $2; |
|
|
|
|
|
foreach my $res (get_from_log( |
|
|
limit => 20, |
|
|
search => $what, |
|
|
)) { |
|
|
_log "search [$what]: $res"; |
|
|
$_[KERNEL]->post( $irc => privmsg => $nick, $res ); |
|
|
} |
|
|
|
|
|
$res = ''; |
|
|
|
|
|
} elsif ($msg =~ m/^(?:count|poll)\s+(.*)(?:\s+(\d+))?\s*$/i) { |
|
|
|
|
|
my ($what,$limit) = ($1,$2); |
|
|
$limit ||= 100; |
|
|
|
|
|
my $stat; |
|
|
|
|
|
foreach my $res (get_from_log( |
|
|
limit => $limit, |
|
|
search => $what, |
|
|
full_rows => 1, |
|
|
)) { |
|
|
while ($res->{message} =~ s/\Q$what\E(\+|\-)//) { |
|
|
$stat->{vote}->{$1}++; |
|
|
$stat->{from}->{ $res->{nick} }++; |
|
|
} |
|
|
} |
|
|
|
|
|
my @nicks; |
|
|
foreach my $nick (sort { $stat->{from}->{$a} <=> $stat->{from}->{$b} } keys %{ $stat->{from} }) { |
|
|
push @nicks, $nick . ( $stat->{from}->{$nick} == 1 ? '' : |
|
|
"(" . $stat->{from}->{$nick} . ")" |
|
|
); |
|
|
} |
|
|
|
|
|
$res = |
|
|
"$what ++ " . ( $stat->{vote}->{'+'} || 0 ) . |
|
|
" : " . ( $stat->{vote}->{'-'} || 0 ) . " --" . |
|
|
" from " . ( join(", ", @nicks) || 'nobody' ); |
|
|
|
|
|
$_[KERNEL]->post( $irc => notice => $nick, $res ); |
|
|
|
|
|
} elsif ($msg =~ m/^ping/) { |
|
|
$res = "ping = " . dump( $_stat->{ping} ); |
|
|
} elsif ($msg =~ m/^conf(?:ig)*\s*(last-size|twitter)*\s*(.*)/) { |
|
|
if ( ! defined( $1 ) ) { |
|
|
my $sth = $dbh->prepare(qq{ select name,value,changed from meta where nick = ? and channel = ? }); |
|
|
$sth->execute( $nick, $channel ); |
|
|
$res = "config for $nick on $channel"; |
|
|
while ( my ($n,$v) = $sth->fetchrow_array ) { |
|
|
$res .= " | $n = $v"; |
|
|
} |
|
|
} elsif ( ! $2 ) { |
|
|
my $val = meta( $nick, $channel, $1 ); |
|
|
$res = "current $1 = " . ( $val ? $val : 'undefined' ); |
|
|
} else { |
|
|
my $validate = { |
|
|
'last-size' => qr/^\d+/, |
|
|
'twitter' => qr/^\w+\s+\w+/, |
|
|
}; |
|
|
|
|
|
my ( $op, $val ) = ( $1, $2 ); |
|
|
|
|
|
if ( my $regex = $validate->{$op} ) { |
|
|
if ( $val =~ $regex ) { |
|
|
meta( $nick, $channel, $op, $val ); |
|
|
$res = "saved $op = $val"; |
|
|
} else { |
|
|
$res = "config option $op = $val doesn't validate against $regex"; |
|
|
} |
|
|
} else { |
|
|
$res = "config option $op doesn't exist"; |
|
|
} |
|
|
} |
|
|
} elsif ($msg =~ m/^rss-update/) { |
|
|
$res = rss_fetch_all( $_[KERNEL] ); |
|
|
} elsif ($msg =~ m/^rss-list/) { |
|
|
my $sth = $dbh->prepare(qq{ select url,name,last_update,active,channel,nick,private from feeds }); |
|
|
$sth->execute; |
|
|
while (my @row = $sth->fetchrow_array) { |
|
|
$_[KERNEL]->post( $irc => privmsg => $nick, join(' | ',@row) ); |
|
|
} |
|
|
$res = ''; |
|
|
} elsif ($msg =~ m!^rss-(add|remove|stop|start|clean)(?:-(private))?\s+(http://\S+)\s*(.*)!) { |
|
|
my ( $command, $sub, $url, $arg ) = ( $1,$2,$3,$4 ); |
|
|
|
|
|
my $channel = $1 if ( $arg =~ s/\s*(#\S+)\s*// ); |
|
|
$channel = $nick if $sub eq 'private'; |
|
|
|
|
|
my $sql = { |
|
|
add => qq{ insert into feeds (url,name,channel,nick,private) values (?,?,?,?,?) }, |
|
|
# remove => qq{ delete from feeds where url = ? and name = ? }, |
|
|
start => qq{ update feeds set active = true where url = ? }, |
|
|
stop => qq{ update feeds set active = false where url = ? }, |
|
|
clean => qq{ update feeds set last_update = now() - delay where url = ? }, |
|
|
}; |
|
|
|
|
|
if ( $command eq 'add' && ! $channel ) { |
|
|
$res = "ERROR: got '$msg' which doesn't have #channel in it, ignoring!"; |
|
|
} elsif (my $q = $sql->{$command} ) { |
|
|
my $sth = $dbh->prepare( $q ); |
|
|
my @data = ( $url ); |
|
|
if ( $command eq 'add' ) { |
|
|
push @data, ( $arg, $channel, $nick, $sub eq 'private' ? 1 : 0 ); |
|
|
} |
|
|
warn "## $command SQL $q with ",dump( @data ),"\n"; |
|
|
eval { $sth->execute( @data ) }; |
|
|
if ($@) { |
|
|
$res = "ERROR: $@"; |
|
|
} else { |
|
|
$res = "OK, RSS executed $command " . ( $sub ? "-$sub" : '' ) ."on $channel url $url"; |
|
|
if ( $command eq 'clean' ) { |
|
|
my $seen = $_stat->{rss}->{seen} || die "no seen?"; |
|
|
my $want_link = $_stat->{rss}->{url2link}->{$url} || warn "no url2link($url)"; |
|
|
foreach my $c ( keys %$seen ) { |
|
|
my $c_hash = $seen->{$c} || die "no seen->{$c}"; |
|
|
die "not HASH with rss links but ", dump($c_hash) unless ref($c_hash) eq 'HASH'; |
|
|
foreach my $link ( keys %$c_hash ) { |
|
|
next unless $link eq $want_link; |
|
|
_log "RSS removed seen $c $url $link"; |
|
|
} |
|
|
} |
|
|
} |
|
|
} |
|
|
} else { |
|
|
$res = "ERROR: don't know what to do with: $msg"; |
|
|
} |
|
|
} elsif ($msg =~ m/^rss-clean/) { |
|
|
# this makes sense because we didn't catch rss-clean http://... before! |
|
|
$_stat->{rss} = undef; |
|
|
$dbh->do( qq{ update feeds set last_update = now() - delay } ); |
|
|
$res = "OK, cleaned RSS cache"; |
|
|
} |
|
1013 |
|
|
1014 |
if ($res) { |
if ($res) { |
1015 |
_log ">> [$nick] $res"; |
_log ">> [$nick] $res"; |
1081 |
}, |
}, |
1082 |
_child => sub {}, |
_child => sub {}, |
1083 |
_default => sub { |
_default => sub { |
1084 |
_log sprintf "sID:%s %s %s", |
_log '_default SID:', $_[SESSION]->ID, $_[ARG0], dump( $_[ARG1] ); |
1085 |
$_[SESSION]->ID, $_[ARG0], |
0; # false for signals |
|
ref($_[ARG1]) eq "ARRAY" ? join(",", map { ref($_) eq "ARRAY" ? join(";", @{$_}) : $_ } @{ $_[ARG1] }) : |
|
|
$_[ARG1] ? $_[ARG1] : |
|
|
""; |
|
|
0; # false for signals |
|
1086 |
}, |
}, |
1087 |
rss_response => sub { |
rss_response => sub { |
1088 |
my ($request_packet, $response_packet) = @_[ARG0, ARG1]; |
my ($request_packet, $response_packet) = @_[ARG0, ARG1]; |
1092 |
my $row = delete( $_stat->{rss}->{fetch}->{ $request_object->uri } ); |
my $row = delete( $_stat->{rss}->{fetch}->{ $request_object->uri } ); |
1093 |
if ( $row ) { |
if ( $row ) { |
1094 |
$row->{xml} = $response_object->content; |
$row->{xml} = $response_object->content; |
1095 |
rss_parse_xml( $row ); |
rss_parse_xml( $_[KERNEL], $row ); |
1096 |
} else { |
} else { |
1097 |
warn "## can't find rss->fetch for ", $request_object->uri; |
warn "## can't find rss->fetch for ", $request_object->uri; |
1098 |
} |
} |
1186 |
$response->content_type( 'application/' . lc($type) . '+xml' ); |
$response->content_type( 'application/' . lc($type) . '+xml' ); |
1187 |
|
|
1188 |
my $html = '<!-- error -->'; |
my $html = '<!-- error -->'; |
1189 |
#warn "create $type feed from ",dump( @last_tags ); |
#warn "create $type feed from ",dump( $cloud->last_tags ); |
1190 |
|
|
1191 |
my $feed = XML::Feed->new( $type ); |
my $feed = XML::Feed->new( $type ); |
1192 |
$feed->link( $url ); |
$feed->link( $url ); |
1220 |
$feed->title( "last $nr tagged messages from $CHANNEL" ); |
$feed->title( "last $nr tagged messages from $CHANNEL" ); |
1221 |
$feed->description( "collects messages which have tags// in them" ); |
$feed->description( "collects messages which have tags// in them" ); |
1222 |
|
|
1223 |
foreach my $m ( @last_tags ) { |
foreach my $m ( $cloud->last_tags ) { |
1224 |
# warn dump( $m ); |
# warn dump( $m ); |
1225 |
#my $tags = join(' ', @{$m->{tags}} ); |
#my $tags = join(' ', @{$m->{tags}} ); |
1226 |
my $feed_entry = XML::Feed::Entry->new($type); |
my $feed_entry = XML::Feed::Entry->new($type); |
1350 |
} else { |
} else { |
1351 |
$html .= join("</p><p>", |
$html .= join("</p><p>", |
1352 |
get_from_log( |
get_from_log( |
1353 |
limit => ( $q->param('last') || $q->param('date') ) ? undef : 100, |
limit => ( $q->param('date') ? undef : $q->param('last') || 100 ), |
1354 |
search => $search || undef, |
search => $search || undef, |
1355 |
tag => $q->param('tag') || undef, |
tag => $q->param('tag') || undef, |
1356 |
date => $q->param('date') || undef, |
date => $q->param('date') || undef, |
1375 |
<p>See <a href="/history">history</a> of all messages.</p> |
<p>See <a href="/history">history</a> of all messages.</p> |
1376 |
</body></html>}; |
</body></html>}; |
1377 |
|
|
1378 |
$response->content( decode('utf-8',$html) ); |
$response->content( $html ); |
1379 |
warn "<< ", $request->method, " ", $request->uri, " created ", length($html), " bytes\n"; |
warn "<< ", $request->method, " ", $request->uri, " created ", length($html), " bytes\n"; |
1380 |
return RC_OK; |
return RC_OK; |
1381 |
} |
} |
1382 |
|
|
1383 |
POE::Kernel->run; |
POE::Kernel->run; |
1384 |
|
|
1385 |
|
=head1 TagCloud |
1386 |
|
|
1387 |
|
Extended L<HTML::TagCloud> |
1388 |
|
|
1389 |
|
=cut |
1390 |
|
|
1391 |
|
package TagCloud; |
1392 |
|
use warnings; |
1393 |
|
use strict; |
1394 |
|
use HTML::TagCloud; |
1395 |
|
use base 'HTML::TagCloud'; |
1396 |
|
use Data::Dump qw/dump/; |
1397 |
|
|
1398 |
|
=head2 html |
1399 |
|
|
1400 |
|
Generate html with number of tags in title of link |
1401 |
|
|
1402 |
|
=cut |
1403 |
|
|
1404 |
|
sub html { |
1405 |
|
my($self, $limit) = @_; |
1406 |
|
my @tags=$self->tags($limit); |
1407 |
|
|
1408 |
|
my $ntags = scalar(@tags); |
1409 |
|
if ($ntags == 0) { |
1410 |
|
return ""; |
1411 |
|
# } elsif ($ntags == 1) { |
1412 |
|
# my $tag = $tags[0]; |
1413 |
|
# return qq{<div id="htmltagcloud"><span class="tagcloud1"><a href="}. |
1414 |
|
# $tag->{url}.qq{">}.$tag->{name}.qq{</a></span></div>\n}; |
1415 |
|
} |
1416 |
|
|
1417 |
|
my $html = qq{<div id="htmltagcloud">}; |
1418 |
|
foreach my $tag (@tags) { |
1419 |
|
$html .= sprintf(qq{<span class="tag tagcloud%d"><a href="%s" title="%s">%s</a></span>\n}, |
1420 |
|
$tag->{level}, $tag->{url}, $tag->{count}, $tag->{name} |
1421 |
|
); |
1422 |
|
} |
1423 |
|
$html .= qq{</div>}; |
1424 |
|
return $html; |
1425 |
|
} |
1426 |
|
|
1427 |
|
=head2 last_tags |
1428 |
|
|
1429 |
|
my @tags = $cloud->last_tags; |
1430 |
|
|
1431 |
|
=cut |
1432 |
|
|
1433 |
|
my @last_tags; |
1434 |
|
sub last_tags { |
1435 |
|
return @last_tags; |
1436 |
|
} |
1437 |
|
|
1438 |
|
=head2 add_tag |
1439 |
|
|
1440 |
|
$cloud->add_tag( id => 42, message => 'irc message', nick => 'foobar' [, me => 1 ] ); |
1441 |
|
|
1442 |
|
=cut |
1443 |
|
|
1444 |
|
|
1445 |
|
sub add_tag { |
1446 |
|
my $self = shift; |
1447 |
|
my $arg = {@_}; |
1448 |
|
|
1449 |
|
return unless ($arg->{id} && $arg->{message}); |
1450 |
|
|
1451 |
|
my $m = $arg->{message}; |
1452 |
|
|
1453 |
|
my @tags; |
1454 |
|
|
1455 |
|
while ($m =~ s#$tag_regex##s) { |
1456 |
|
my $tag = $1; |
1457 |
|
next if (! $tag || $tag =~ m/https?:/i); |
1458 |
|
push @{ $tags->{$tag} }, $arg->{id}; |
1459 |
|
#warn "+tag $tag: $arg->{id}\n"; |
1460 |
|
$self->add($tag, "$url?tag=$tag", scalar @{$tags->{$tag}}); |
1461 |
|
push @tags, $tag; |
1462 |
|
|
1463 |
|
} |
1464 |
|
|
1465 |
|
if ( @tags ) { |
1466 |
|
pop @last_tags if $#last_tags == $last_x_tags; |
1467 |
|
unshift @last_tags, { tags => [ @tags ], %$arg }; |
1468 |
|
} |
1469 |
|
|
1470 |
|
} |
1471 |
|
|
1472 |
|
=head2 seed_tags |
1473 |
|
|
1474 |
|
Read all tags from database and create in-memory cache for tags |
1475 |
|
|
1476 |
|
=cut |
1477 |
|
|
1478 |
|
sub seed_tags { |
1479 |
|
my $self = shift; |
1480 |
|
my $sth = $dbh->prepare(qq{ select id,message,nick,me,time from log where message like '%//%' order by time asc }); |
1481 |
|
$sth->execute; |
1482 |
|
while (my $row = $sth->fetchrow_hashref) { |
1483 |
|
$self->add_tag( %$row ); |
1484 |
|
} |
1485 |
|
|
1486 |
|
foreach my $tag (keys %$tags) { |
1487 |
|
$self->add($tag, "$url?tag=$tag", scalar @{$tags->{$tag}}); |
1488 |
|
} |
1489 |
|
} |
1490 |
|
|