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; |
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; |
255 |
|
|
256 |
eval { $sth->execute( $value, $nick, $channel, $name ) }; |
eval { $sth->execute( $value, $nick, $channel, $name ) }; |
257 |
|
|
258 |
# error or no result |
if ( $@ ) { |
259 |
if ( $@ || ! $sth->rows ) { |
# error |
260 |
|
_log("META ERROR: $@"); |
261 |
|
} elsif ( ! $sth->rows ) { |
262 |
|
# no result -> add new |
263 |
$sth = $dbh->prepare(qq{ insert into meta (value,nick,channel,name,changed) values (?,?,?,?,now()) }); |
$sth = $dbh->prepare(qq{ insert into meta (value,nick,channel,name,changed) values (?,?,?,?,now()) }); |
264 |
$sth->execute( $value, $nick, $channel, $name ); |
eval { $sth->execute( $value, $nick, $channel, $name ); }; |
265 |
warn "## created $nick/$channel/$name = $value\n"; |
if ( $@ ) { |
266 |
|
_log "META ERROR: $@"; |
267 |
|
} else { |
268 |
|
_log "META: created $nick/$channel/$name = $value\n"; |
269 |
|
} |
270 |
} else { |
} else { |
271 |
warn "## updated $nick/$channel/$name = $value\n"; |
_log "META: updated $nick/$channel/$name = $value\n"; |
272 |
} |
} |
273 |
|
|
274 |
return $value; |
return $value; |
532 |
|
|
533 |
# tags support |
# tags support |
534 |
|
|
535 |
my $cloud = HTML::TagCloud->new; |
my $cloud = TagCloud->new; |
536 |
|
$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; |
|
|
|
|
537 |
|
|
538 |
=head2 save_message |
=head2 save_message |
539 |
|
|
562 |
$a->{me} ? "***" . $a->{nick} : "<" . $a->{nick} . ">", |
$a->{me} ? "***" . $a->{nick} : "<" . $a->{nick} . ">", |
563 |
" " . $a->{message}; |
" " . $a->{message}; |
564 |
|
|
565 |
$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}); }; |
566 |
add_tag( id => $dbh->last_insert_id(undef,undef,"log",undef), %$a ); |
if ( $@ ) { |
567 |
|
_log "ERROR: can't archive ", $a->{message}; |
568 |
|
} else { |
569 |
|
$cloud->add_tag( id => $dbh->last_insert_id(undef,undef,"log",undef), %$a ); |
570 |
|
} |
571 |
} |
} |
572 |
|
|
573 |
|
|
628 |
sub rss_parse_xml { |
sub rss_parse_xml { |
629 |
my ($kernel,$args) = @_; |
my ($kernel,$args) = @_; |
630 |
|
|
631 |
warn "## rss_parse_xml ",dump( @_ ) if $debug; |
warn "## rss_parse_xml ",dump( $args ) if $debug; |
632 |
|
|
633 |
# 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? |
634 |
my $send_rss_msgs = $args->{send_rss_msgs}; |
my $send_rss_msgs = $args->{send_rss_msgs}; |
636 |
|
|
637 |
warn "## RSS fetch first $send_rss_msgs items from", $args->{url} if $debug; |
warn "## RSS fetch first $send_rss_msgs items from", $args->{url} if $debug; |
638 |
|
|
639 |
my $feed = XML::Feed->parse( \$args->{xml} ); |
my $feed; |
640 |
|
eval { $feed = XML::Feed->parse( \$args->{xml} ) }; |
641 |
if ( ! $feed ) { |
if ( ! $feed ) { |
642 |
_log "can't fetch RSS ", $args->{url}, XML::Feed->errstr; |
_log "can't fetch RSS ", $args->{url}, XML::Feed->errstr; |
643 |
return; |
return; |
678 |
$msg .= prefix( ' | ' , $entry->title ); |
$msg .= prefix( ' | ' , $entry->title ); |
679 |
$msg .= prefix( ' | ' , $link ); |
$msg .= prefix( ' | ' , $link ); |
680 |
# $msg .= prefix( ' id ' , $entry->id ); |
# $msg .= prefix( ' id ' , $entry->id ); |
681 |
|
my @categories = $entry->category; |
682 |
|
warn "## category = ", dump( @categories ) if $debug; |
683 |
if ( my $tags = $entry->category ) { |
if ( my $tags = $entry->category ) { |
684 |
|
$tags = join(' ', @$tags) if ref($tags) eq 'ARRAY'; |
685 |
$tags =~ s!^\s+!!; |
$tags =~ s!^\s+!!; |
686 |
$tags =~ s!\s*$! !; |
$tags =~ s!\s*$! !; |
687 |
$tags =~ s!,?\s+!// !g; |
$tags =~ s!,?\s+!// !g; |
755 |
} |
} |
756 |
} |
} |
757 |
|
|
758 |
|
sub process_command { |
759 |
|
my ( $kernel, $nick, $channel, $msg ) = @_; |
760 |
|
|
761 |
|
my $res = "unknown command '$msg', try /msg $NICK help!"; |
762 |
|
|
763 |
|
if ($msg =~ m/^help/i) { |
764 |
|
|
765 |
|
$res = "usage: /msg $NICK comand | commands: stat - user/message stat | last - show backtrace | grep foobar - find foobar"; |
766 |
|
|
767 |
|
} elsif ($msg =~ m/^(privmsg|notice)\s+(\S+)\s+(.*)$/i) { |
768 |
|
|
769 |
|
_log ">> /$1 $2 $3"; |
770 |
|
$kernel->post( $irc => $1 => $2, $3 ); |
771 |
|
$res = ''; |
772 |
|
|
773 |
|
} elsif ($msg =~ m/^stat.*?\s*(\d*)/i) { |
774 |
|
|
775 |
|
my $nr = $1 || 10; |
776 |
|
|
777 |
|
my $sth = $dbh->prepare(qq{ |
778 |
|
select |
779 |
|
trim(both '_' from nick) as nick, |
780 |
|
count(*) as count, |
781 |
|
sum(length(message)) as len |
782 |
|
from log |
783 |
|
group by trim(both '_' from nick) |
784 |
|
order by len desc,count desc |
785 |
|
limit $nr |
786 |
|
}); |
787 |
|
$sth->execute(); |
788 |
|
$res = "Top $nr users: "; |
789 |
|
my @users; |
790 |
|
while (my $row = $sth->fetchrow_hashref) { |
791 |
|
push @users,$row->{nick} . ': ' . $row->{count} . '/' . $row->{len} . '=' . sprintf("%.2f", $row->{len}/$row->{count}); |
792 |
|
} |
793 |
|
$res .= join(" | ", @users); |
794 |
|
} elsif ($msg =~ m/^last.*?\s*(\d*)/i) { |
795 |
|
|
796 |
|
my $limit = $1 || meta( $nick, $channel, 'last-size' ) || 10; |
797 |
|
|
798 |
|
foreach my $res (get_from_log( limit => $limit )) { |
799 |
|
_log "last: $res"; |
800 |
|
$kernel->post( $irc => privmsg => $nick, $res ); |
801 |
|
} |
802 |
|
|
803 |
|
$res = ''; |
804 |
|
|
805 |
|
} elsif ($msg =~ m/^(search|grep)\s+(.*)\s*$/i) { |
806 |
|
|
807 |
|
my $what = $2; |
808 |
|
|
809 |
|
foreach my $res (get_from_log( |
810 |
|
limit => 20, |
811 |
|
search => $what, |
812 |
|
)) { |
813 |
|
_log "search [$what]: $res"; |
814 |
|
$kernel->post( $irc => privmsg => $nick, $res ); |
815 |
|
} |
816 |
|
|
817 |
|
$res = ''; |
818 |
|
|
819 |
|
} elsif ($msg =~ m/^(?:count|poll)\s+(.*)(?:\s+(\d+))?\s*$/i) { |
820 |
|
|
821 |
|
my ($what,$limit) = ($1,$2); |
822 |
|
$limit ||= 100; |
823 |
|
|
824 |
|
my $stat; |
825 |
|
|
826 |
|
foreach my $res (get_from_log( |
827 |
|
limit => $limit, |
828 |
|
search => $what, |
829 |
|
full_rows => 1, |
830 |
|
)) { |
831 |
|
while ($res->{message} =~ s/\Q$what\E(\+|\-)//) { |
832 |
|
$stat->{vote}->{$1}++; |
833 |
|
$stat->{from}->{ $res->{nick} }++; |
834 |
|
} |
835 |
|
} |
836 |
|
|
837 |
|
my @nicks; |
838 |
|
foreach my $nick (sort { $stat->{from}->{$a} <=> $stat->{from}->{$b} } keys %{ $stat->{from} }) { |
839 |
|
push @nicks, $nick . ( $stat->{from}->{$nick} == 1 ? '' : |
840 |
|
"(" . $stat->{from}->{$nick} . ")" |
841 |
|
); |
842 |
|
} |
843 |
|
|
844 |
|
$res = |
845 |
|
"$what ++ " . ( $stat->{vote}->{'+'} || 0 ) . |
846 |
|
" : " . ( $stat->{vote}->{'-'} || 0 ) . " --" . |
847 |
|
" from " . ( join(", ", @nicks) || 'nobody' ); |
848 |
|
|
849 |
|
$kernel->post( $irc => notice => $nick, $res ); |
850 |
|
|
851 |
|
} elsif ($msg =~ m/^ping/) { |
852 |
|
$res = "ping = " . dump( $_stat->{ping} ); |
853 |
|
} elsif ($msg =~ m/^conf(?:ig)*\s*(last-size|twitter)*\s*(.*)/) { |
854 |
|
if ( ! defined( $1 ) ) { |
855 |
|
my $sth = $dbh->prepare(qq{ select name,value,changed from meta where nick = ? and channel = ? }); |
856 |
|
$sth->execute( $nick, $channel ); |
857 |
|
$res = "config for $nick on $channel"; |
858 |
|
while ( my ($n,$v) = $sth->fetchrow_array ) { |
859 |
|
$res .= " | $n = $v"; |
860 |
|
} |
861 |
|
} elsif ( ! $2 ) { |
862 |
|
my $val = meta( $nick, $channel, $1 ); |
863 |
|
$res = "current $1 = " . ( $val ? $val : 'undefined' ); |
864 |
|
} else { |
865 |
|
my $validate = { |
866 |
|
'last-size' => qr/^\d+/, |
867 |
|
'twitter' => qr/^\w+\s+\w+/, |
868 |
|
}; |
869 |
|
|
870 |
|
my ( $op, $val ) = ( $1, $2 ); |
871 |
|
|
872 |
|
if ( my $regex = $validate->{$op} ) { |
873 |
|
if ( $val =~ $regex ) { |
874 |
|
meta( $nick, $channel, $op, $val ); |
875 |
|
$res = "saved $op = $val"; |
876 |
|
} else { |
877 |
|
$res = "config option $op = $val doesn't validate against $regex"; |
878 |
|
} |
879 |
|
} else { |
880 |
|
$res = "config option $op doesn't exist"; |
881 |
|
} |
882 |
|
} |
883 |
|
} elsif ($msg =~ m/^rss-update/) { |
884 |
|
$res = rss_fetch_all( $kernel ); |
885 |
|
} elsif ($msg =~ m/^rss-list/) { |
886 |
|
my $sth = $dbh->prepare(qq{ select url,name,last_update,active,channel,nick,private from feeds }); |
887 |
|
$sth->execute; |
888 |
|
while (my @row = $sth->fetchrow_array) { |
889 |
|
$kernel->post( $irc => privmsg => $nick, join(' | ',@row) ); |
890 |
|
} |
891 |
|
$res = ''; |
892 |
|
} elsif ($msg =~ m!^rss-(add|remove|stop|start|clean)(?:-(private))?\s+(http://\S+)\s*(.*)!) { |
893 |
|
my ( $command, $sub, $url, $arg ) = ( $1,$2,$3,$4 ); |
894 |
|
|
895 |
|
my $channel = $1 if ( $arg =~ s/\s*(#\S+)\s*// ); |
896 |
|
$channel = $nick if $sub eq 'private'; |
897 |
|
|
898 |
|
my $sql = { |
899 |
|
add => qq{ insert into feeds (url,name,channel,nick,private) values (?,?,?,?,?) }, |
900 |
|
remove => qq{ delete from feeds where url = ? and nick = ? }, |
901 |
|
start => qq{ update feeds set active = true where url = ? }, |
902 |
|
stop => qq{ update feeds set active = false where url = ? }, |
903 |
|
clean => qq{ update feeds set last_update = now() - delay where url = ? }, |
904 |
|
}; |
905 |
|
|
906 |
|
if ( $command eq 'add' && ! $channel ) { |
907 |
|
$res = "ERROR: got '$msg' which doesn't have #channel in it, ignoring!"; |
908 |
|
} elsif (my $q = $sql->{$command} ) { |
909 |
|
my $sth = $dbh->prepare( $q ); |
910 |
|
my @data = ( $url ); |
911 |
|
if ( $command eq 'add' ) { |
912 |
|
push @data, ( $arg, $channel, $nick, $sub eq 'private' ? 1 : 0 ); |
913 |
|
} elsif ( $command eq 'remove' ) { |
914 |
|
push @data, $nick; |
915 |
|
} |
916 |
|
warn "## $command SQL $q with ",dump( @data ),"\n"; |
917 |
|
eval { $sth->execute( @data ) }; |
918 |
|
if ($@) { |
919 |
|
$res = "ERROR: $@"; |
920 |
|
} else { |
921 |
|
$res = "OK, RSS executed $command" . |
922 |
|
( $sub ? "-$sub " : ' ' ) . |
923 |
|
( $channel ? "on $channel " : '' ) . |
924 |
|
"url $url"; |
925 |
|
if ( $command eq 'clean' ) { |
926 |
|
my $seen = $_stat->{rss}->{seen} || die "no seen?"; |
927 |
|
my $want_link = $_stat->{rss}->{url2link}->{$url} || warn "no url2link($url)"; |
928 |
|
foreach my $c ( keys %$seen ) { |
929 |
|
my $c_hash = $seen->{$c} || die "no seen->{$c}"; |
930 |
|
die "not HASH with rss links but ", dump($c_hash) unless ref($c_hash) eq 'HASH'; |
931 |
|
foreach my $link ( keys %$c_hash ) { |
932 |
|
next unless $link eq $want_link; |
933 |
|
_log "RSS removed seen $c $url $link"; |
934 |
|
} |
935 |
|
} |
936 |
|
} elsif ( $command eq 'add' ) { |
937 |
|
rss_fetch_all( $kernel ); |
938 |
|
} |
939 |
|
} |
940 |
|
} else { |
941 |
|
$res = "ERROR: don't know what to do with: $msg"; |
942 |
|
} |
943 |
|
} elsif ($msg =~ m/^rss-clean/) { |
944 |
|
# this makes sense because we didn't catch rss-clean http://... before! |
945 |
|
$_stat->{rss} = undef; |
946 |
|
$dbh->do( qq{ update feeds set last_update = now() - delay } ); |
947 |
|
$res = rss_fetch_all( $kernel ); |
948 |
|
} |
949 |
|
|
950 |
|
return $res; |
951 |
|
} |
952 |
|
|
953 |
POE::Session->create( inline_states => { |
POE::Session->create( inline_states => { |
954 |
_start => sub { |
_start => sub { |
955 |
$_[KERNEL]->post( $irc => register => 'all' ); |
$_[KERNEL]->post( $irc => register => 'all' ); |
1014 |
irc_msg => sub { |
irc_msg => sub { |
1015 |
my $kernel = $_[KERNEL]; |
my $kernel = $_[KERNEL]; |
1016 |
my $nick = (split /!/, $_[ARG0])[0]; |
my $nick = (split /!/, $_[ARG0])[0]; |
|
my $msg = $_[ARG2]; |
|
1017 |
my $channel = $_[ARG1]->[0]; |
my $channel = $_[ARG1]->[0]; |
1018 |
|
my $msg = $_[ARG2]; |
1019 |
warn "# ARG = ",dump( @_[ARG0,ARG1,ARG2] ) if $debug; |
warn "# ARG = ",dump( @_[ARG0,ARG1,ARG2] ) if $debug; |
1020 |
|
|
|
my $res = "unknown command '$msg', try /msg $NICK help!"; |
|
|
my @out; |
|
|
|
|
1021 |
_log "<< $msg"; |
_log "<< $msg"; |
1022 |
|
|
1023 |
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 nick = ? }, |
|
|
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 ); |
|
|
} elsif ( $command eq 'remove' ) { |
|
|
push @data, $nick; |
|
|
} |
|
|
warn "## $command SQL $q with ",dump( @data ),"\n"; |
|
|
eval { $sth->execute( @data ) }; |
|
|
if ($@) { |
|
|
$res = "ERROR: $@"; |
|
|
} else { |
|
|
$res = "OK, RSS executed $command" . |
|
|
( $sub ? "-$sub " : ' ' ) . |
|
|
( $channel ? "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"; |
|
|
} |
|
|
} |
|
|
} elsif ( $command eq 'add' ) { |
|
|
rss_fetch_all( $_[KERNEL] ); |
|
|
} |
|
|
} |
|
|
} 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 = rss_fetch_all( $_[KERNEL] ); |
|
|
} |
|
1024 |
|
|
1025 |
if ($res) { |
if ($res) { |
1026 |
_log ">> [$nick] $res"; |
_log ">> [$nick] $res"; |
1092 |
}, |
}, |
1093 |
_child => sub {}, |
_child => sub {}, |
1094 |
_default => sub { |
_default => sub { |
1095 |
_log sprintf "sID:%s %s %s", |
_log '_default SID:', $_[SESSION]->ID, $_[ARG0], dump( $_[ARG1] ); |
1096 |
$_[SESSION]->ID, $_[ARG0], |
0; # false for signals |
|
ref($_[ARG1]) eq "ARRAY" ? join(",", map { ref($_) eq "ARRAY" ? join(";", @{$_}) : $_ } @{ $_[ARG1] }) : |
|
|
$_[ARG1] ? $_[ARG1] : |
|
|
""; |
|
|
0; # false for signals |
|
1097 |
}, |
}, |
1098 |
rss_response => sub { |
rss_response => sub { |
1099 |
my ($request_packet, $response_packet) = @_[ARG0, ARG1]; |
my ($request_packet, $response_packet) = @_[ARG0, ARG1]; |
1197 |
$response->content_type( 'application/' . lc($type) . '+xml' ); |
$response->content_type( 'application/' . lc($type) . '+xml' ); |
1198 |
|
|
1199 |
my $html = '<!-- error -->'; |
my $html = '<!-- error -->'; |
1200 |
#warn "create $type feed from ",dump( @last_tags ); |
#warn "create $type feed from ",dump( $cloud->last_tags ); |
1201 |
|
|
1202 |
my $feed = XML::Feed->new( $type ); |
my $feed = XML::Feed->new( $type ); |
1203 |
$feed->link( $url ); |
$feed->link( $url ); |
1231 |
$feed->title( "last $nr tagged messages from $CHANNEL" ); |
$feed->title( "last $nr tagged messages from $CHANNEL" ); |
1232 |
$feed->description( "collects messages which have tags// in them" ); |
$feed->description( "collects messages which have tags// in them" ); |
1233 |
|
|
1234 |
foreach my $m ( @last_tags ) { |
foreach my $m ( $cloud->last_tags ) { |
1235 |
# warn dump( $m ); |
# warn dump( $m ); |
1236 |
#my $tags = join(' ', @{$m->{tags}} ); |
#my $tags = join(' ', @{$m->{tags}} ); |
1237 |
my $feed_entry = XML::Feed::Entry->new($type); |
my $feed_entry = XML::Feed::Entry->new($type); |
1294 |
$rc = RC_DENY; |
$rc = RC_DENY; |
1295 |
} |
} |
1296 |
|
|
1297 |
$response->content( $feed->as_xml ); |
eval { $response->content( $feed->as_xml ); }; |
1298 |
|
$rc = RC_INTERNAL_SERVER_ERROR if $@; |
1299 |
return $rc; |
return $rc; |
1300 |
} |
} |
1301 |
|
|
1362 |
} else { |
} else { |
1363 |
$html .= join("</p><p>", |
$html .= join("</p><p>", |
1364 |
get_from_log( |
get_from_log( |
1365 |
limit => ( $q->param('last') || $q->param('date') ) ? undef : 100, |
limit => ( $q->param('date') ? undef : $q->param('last') || 100 ), |
1366 |
search => $search || undef, |
search => $search || undef, |
1367 |
tag => $q->param('tag') || undef, |
tag => $q->param('tag') || undef, |
1368 |
date => $q->param('date') || undef, |
date => $q->param('date') || undef, |
1387 |
<p>See <a href="/history">history</a> of all messages.</p> |
<p>See <a href="/history">history</a> of all messages.</p> |
1388 |
</body></html>}; |
</body></html>}; |
1389 |
|
|
1390 |
$response->content( decode('utf-8',$html) ); |
$response->content( $html ); |
1391 |
warn "<< ", $request->method, " ", $request->uri, " created ", length($html), " bytes\n"; |
warn "<< ", $request->method, " ", $request->uri, " created ", length($html), " bytes\n"; |
1392 |
return RC_OK; |
return RC_OK; |
1393 |
} |
} |
1394 |
|
|
1395 |
POE::Kernel->run; |
POE::Kernel->run; |
1396 |
|
|
1397 |
|
=head1 TagCloud |
1398 |
|
|
1399 |
|
Extended L<HTML::TagCloud> |
1400 |
|
|
1401 |
|
=cut |
1402 |
|
|
1403 |
|
package TagCloud; |
1404 |
|
use warnings; |
1405 |
|
use strict; |
1406 |
|
use HTML::TagCloud; |
1407 |
|
use base 'HTML::TagCloud'; |
1408 |
|
use Data::Dump qw/dump/; |
1409 |
|
|
1410 |
|
=head2 html |
1411 |
|
|
1412 |
|
Generate html with number of tags in title of link |
1413 |
|
|
1414 |
|
=cut |
1415 |
|
|
1416 |
|
sub html { |
1417 |
|
my($self, $limit) = @_; |
1418 |
|
my @tags=$self->tags($limit); |
1419 |
|
|
1420 |
|
my $ntags = scalar(@tags); |
1421 |
|
if ($ntags == 0) { |
1422 |
|
return ""; |
1423 |
|
# } elsif ($ntags == 1) { |
1424 |
|
# my $tag = $tags[0]; |
1425 |
|
# return qq{<div id="htmltagcloud"><span class="tagcloud1"><a href="}. |
1426 |
|
# $tag->{url}.qq{">}.$tag->{name}.qq{</a></span></div>\n}; |
1427 |
|
} |
1428 |
|
|
1429 |
|
my $html = qq{<div id="htmltagcloud">}; |
1430 |
|
foreach my $tag ( sort { lc($a->{name}) cmp lc($b->{name}) } @tags) { |
1431 |
|
$html .= sprintf(qq{<span class="tag tagcloud%d"><a href="%s" title="%s">%s</a></span>\n}, |
1432 |
|
$tag->{level}, $tag->{url}, $tag->{count}, $tag->{name} |
1433 |
|
); |
1434 |
|
} |
1435 |
|
$html .= qq{</div>}; |
1436 |
|
return $html; |
1437 |
|
} |
1438 |
|
|
1439 |
|
=head2 last_tags |
1440 |
|
|
1441 |
|
my @tags = $cloud->last_tags; |
1442 |
|
|
1443 |
|
=cut |
1444 |
|
|
1445 |
|
my @last_tags; |
1446 |
|
sub last_tags { |
1447 |
|
return @last_tags; |
1448 |
|
} |
1449 |
|
|
1450 |
|
=head2 add_tag |
1451 |
|
|
1452 |
|
$cloud->add_tag( id => 42, message => 'irc message', nick => 'foobar' [, me => 1 ] ); |
1453 |
|
|
1454 |
|
=cut |
1455 |
|
|
1456 |
|
|
1457 |
|
sub add_tag { |
1458 |
|
my $self = shift; |
1459 |
|
my $arg = {@_}; |
1460 |
|
|
1461 |
|
return unless ($arg->{id} && $arg->{message}); |
1462 |
|
|
1463 |
|
my $m = $arg->{message}; |
1464 |
|
|
1465 |
|
my @tags; |
1466 |
|
|
1467 |
|
while ($m =~ s#$tag_regex##s) { |
1468 |
|
my $tag = $1; |
1469 |
|
next if (! $tag || $tag =~ m/https?:/i); |
1470 |
|
push @{ $tags->{$tag} }, $arg->{id}; |
1471 |
|
#warn "+tag $tag: $arg->{id}\n"; |
1472 |
|
$self->add($tag, "$url?tag=$tag", scalar @{$tags->{$tag}}); |
1473 |
|
push @tags, $tag; |
1474 |
|
|
1475 |
|
} |
1476 |
|
|
1477 |
|
if ( @tags ) { |
1478 |
|
pop @last_tags if $#last_tags == $last_x_tags; |
1479 |
|
unshift @last_tags, { tags => [ @tags ], %$arg }; |
1480 |
|
} |
1481 |
|
|
1482 |
|
} |
1483 |
|
|
1484 |
|
=head2 seed_tags |
1485 |
|
|
1486 |
|
Read all tags from database and create in-memory cache for tags |
1487 |
|
|
1488 |
|
=cut |
1489 |
|
|
1490 |
|
sub seed_tags { |
1491 |
|
my $self = shift; |
1492 |
|
my $sth = $dbh->prepare(qq{ select id,message,nick,me,time from log where message like '%//%' order by time asc }); |
1493 |
|
$sth->execute; |
1494 |
|
while (my $row = $sth->fetchrow_hashref) { |
1495 |
|
$self->add_tag( %$row ); |
1496 |
|
} |
1497 |
|
|
1498 |
|
foreach my $tag (keys %$tags) { |
1499 |
|
$self->add($tag, "$url?tag=$tag", scalar @{$tags->{$tag}}); |
1500 |
|
} |
1501 |
|
} |
1502 |
|
|