2 |
use strict; |
use strict; |
3 |
$|++; |
$|++; |
4 |
|
|
5 |
|
use POE qw(Component::IRC Component::Server::HTTP); |
6 |
|
use HTTP::Status; |
7 |
|
use DBI; |
8 |
|
use Regexp::Common qw /URI/; |
9 |
|
use CGI::Simple; |
10 |
|
use HTML::TagCloud; |
11 |
|
use POSIX qw/strftime/; |
12 |
|
use HTML::CalendarMonthSimple; |
13 |
|
use Getopt::Long; |
14 |
|
use DateTime; |
15 |
|
use URI::Escape; |
16 |
|
use Data::Dump qw/dump/; |
17 |
|
use DateTime::Format::ISO8601; |
18 |
|
use Carp qw/confess/; |
19 |
|
use XML::Feed; |
20 |
|
use DateTime::Format::Flexible; |
21 |
|
|
22 |
=head1 NAME |
=head1 NAME |
23 |
|
|
24 |
irc-logger.pl |
irc-logger.pl |
52 |
|
|
53 |
my $NICK = 'irc-logger'; |
my $NICK = 'irc-logger'; |
54 |
$NICK .= '-dev' if ($HOSTNAME =~ m/llin/); |
$NICK .= '-dev' if ($HOSTNAME =~ m/llin/); |
55 |
my $CONNECT = |
my $CONNECT = { |
56 |
{Server => 'irc.freenode.net', |
Server => 'irc.freenode.net', |
57 |
Nick => $NICK, |
Nick => $NICK, |
58 |
Ircname => "try /msg $NICK help", |
Ircname => "try /msg $NICK help", |
59 |
}; |
}; |
60 |
my $CHANNEL = '#razmjenavjestina'; |
my $CHANNEL = '#razmjenavjestina'; |
61 |
$CHANNEL = '#irc-logger' if ($HOSTNAME =~ m/llin/); |
$CHANNEL = '#irc-logger' if ($HOSTNAME =~ m/llin/); |
62 |
my $IRC_ALIAS = "log"; |
my $IRC_ALIAS = "log"; |
63 |
|
|
64 |
|
if ( $HOSTNAME =~ m/lugarin/ ) { |
65 |
|
$CONNECT->{Server} = 'irc.carnet.hr'; |
66 |
|
$CHANNEL = '#riss'; |
67 |
|
} |
68 |
|
|
69 |
|
warn dump( $HOSTNAME, $CONNECT ); |
70 |
|
|
71 |
my $DSN = 'DBI:Pg:dbname=' . $NICK; |
my $DSN = 'DBI:Pg:dbname=' . $NICK; |
72 |
|
|
73 |
my $TIMESTAMP = '%Y-%m-%d %H:%M:%S'; |
my $TIMESTAMP = '%Y-%m-%d %H:%M:%S'; |
79 |
|
|
80 |
# don't pull rss feeds more often than this |
# don't pull rss feeds more often than this |
81 |
my $rss_min_delay = 60; |
my $rss_min_delay = 60; |
|
$rss_min_delay = 15; |
|
82 |
|
|
83 |
my $http_port = $NICK =~ m/-dev/ ? 8001 : 8000; |
my $http_port = $NICK =~ m/-dev/ ? 8001 : 8000; |
84 |
|
|
86 |
|
|
87 |
## END CONFIG |
## END CONFIG |
88 |
|
|
|
use POE qw(Component::IRC Component::Server::HTTP); |
|
|
use HTTP::Status; |
|
|
use DBI; |
|
|
use Regexp::Common qw /URI/; |
|
|
use CGI::Simple; |
|
|
use HTML::TagCloud; |
|
|
use POSIX qw/strftime/; |
|
|
use HTML::CalendarMonthSimple; |
|
|
use Getopt::Long; |
|
|
use DateTime; |
|
|
use URI::Escape; |
|
|
use Data::Dump qw/dump/; |
|
|
use DateTime::Format::ISO8601; |
|
|
use Carp qw/confess/; |
|
|
use XML::Feed; |
|
|
use DateTime::Format::Flexible; |
|
|
|
|
89 |
my $use_twitter = 1; |
my $use_twitter = 1; |
90 |
eval { require Net::Twitter; }; |
eval { require Net::Twitter; }; |
91 |
$use_twitter = 0 if ($@); |
$use_twitter = 0 if ($@); |
124 |
# protect HTML from wiki modifications |
# protect HTML from wiki modifications |
125 |
sub e { |
sub e { |
126 |
my $t = shift; |
my $t = shift; |
127 |
return 'uri_unescape{' . uri_escape($t) . '}'; |
return 'uri_unescape{' . uri_escape($t, '^a-zA-Z0-9') . '}'; |
128 |
} |
} |
129 |
|
|
130 |
$m =~ s/($escape_re)/$escape{$1}/gs; |
$m =~ s/($escape_re)/$escape{$1}/gs; |
131 |
$m =~ s#($RE{URI}{HTTP})#e(qq{<a href="$1">$1</a>})#egs || |
$m =~ s#($RE{URI}{HTTP})#e(qq{<a href="$1">$1</a>})#egs; |
132 |
$m =~ s#\/(\w+)\/#<i>$1</i>#gs; |
$m =~ s#\/(\w+)\/#<i>$1</i>#gs; |
133 |
$m =~ s#$tag_regex#e(qq{<a href="$url?tag=$1" class="tag">$1</a>})#egs; |
$m =~ s#$tag_regex#e(qq{<a href="$url?tag=$1" class="tag">$1</a>})#egs; |
134 |
$m =~ s#\*(\w+)\*#<b>$1</b>#gs; |
$m =~ s#\*(\w+)\*#<b>$1</b>#gs; |
185 |
name text, |
name text, |
186 |
delay interval not null default '5 min', |
delay interval not null default '5 min', |
187 |
active boolean default true, |
active boolean default true, |
188 |
|
channel text not null, |
189 |
|
nick text not null, |
190 |
|
private boolean default false, |
191 |
last_update timestamp default 'now()', |
last_update timestamp default 'now()', |
192 |
polls int default 0, |
polls int default 0, |
193 |
updates int default 0 |
updates int default 0 |
194 |
); |
); |
195 |
create unique index feeds_url on feeds(url); |
create unique index feeds_url on feeds(url); |
196 |
insert into feeds (url,name) values ('http://wiki.razmjenavjestina.org/feed/workspace/razmjenavjestina?category=Recent%20Changes','wiki'); |
insert into feeds (url,name,channel,nick) values ('http://wiki.razmjenavjestina.org/feed/workspace/razmjenavjestina?category=Recent%20Changes','wiki','$CHANNEL','dpavlin'); |
197 |
}, |
}, |
198 |
}; |
}; |
199 |
|
|
237 |
if ( $@ || ! $sth->rows ) { |
if ( $@ || ! $sth->rows ) { |
238 |
$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()) }); |
239 |
$sth->execute( $value, $nick, $channel, $name ); |
$sth->execute( $value, $nick, $channel, $name ); |
240 |
_log "created $nick/$channel/$name = $value"; |
warn "## created $nick/$channel/$name = $value\n"; |
241 |
} else { |
} else { |
242 |
_log "updated $nick/$channel/$name = $value "; |
warn "## updated $nick/$channel/$name = $value\n"; |
243 |
} |
} |
244 |
|
|
245 |
return $value; |
return $value; |
249 |
my $sth = $dbh->prepare(qq{ select value,changed from meta where nick = ? and channel = ? and name = ? }); |
my $sth = $dbh->prepare(qq{ select value,changed from meta where nick = ? and channel = ? and name = ? }); |
250 |
$sth->execute( $nick, $channel, $name ); |
$sth->execute( $nick, $channel, $name ); |
251 |
my ($v,$c) = $sth->fetchrow_array; |
my ($v,$c) = $sth->fetchrow_array; |
252 |
_log "fetched $nick/$channel/$name = $v [$c]"; |
warn "## fetched $nick/$channel/$name = $v [$c]\n"; |
253 |
return ($v,$c) if wantarray; |
return ($v,$c) if wantarray; |
254 |
return $v; |
return $v; |
255 |
|
|
346 |
|
|
347 |
my @where; |
my @where; |
348 |
my @args; |
my @args; |
349 |
|
my $msg; |
350 |
|
|
351 |
if (my $search = $args->{search}) { |
if (my $search = $args->{search}) { |
352 |
$search =~ s/^\s+//; |
$search =~ s/^\s+//; |
353 |
$search =~ s/\s+$//; |
$search =~ s/\s+$//; |
354 |
push @where, 'message ilike ? or nick ilike ?'; |
push @where, 'message ilike ? or nick ilike ?'; |
355 |
push @args, ( ( '%' . $search . '%' ) x 2 ); |
push @args, ( ( '%' . $search . '%' ) x 2 ); |
356 |
_log "search for '$search'"; |
$msg = "Search for '$search'"; |
357 |
} |
} |
358 |
|
|
359 |
if ($args->{tag} && $tags->{ $args->{tag} }) { |
if ($args->{tag} && $tags->{ $args->{tag} }) { |
360 |
push @where, 'id in (' . join(',', @{ $tags->{ $args->{tag} } }) . ')'; |
push @where, 'id in (' . join(',', @{ $tags->{ $args->{tag} } }) . ')'; |
361 |
_log "search for tags $args->{tag}"; |
$msg = "Search for tags $args->{tag}"; |
362 |
} |
} |
363 |
|
|
364 |
if (my $date = $args->{date} ) { |
if (my $date = $args->{date} ) { |
365 |
$date = check_date( $date ); |
$date = check_date( $date ); |
366 |
push @where, 'date(time) = ?'; |
push @where, 'date(time) = ?'; |
367 |
push @args, $date; |
push @args, $date; |
368 |
_log "search for date $date"; |
$msg = "search for date $date"; |
369 |
} |
} |
370 |
|
|
371 |
$sql .= " where " . join(" and ", @where) if @where; |
$sql .= " where " . join(" and ", @where) if @where; |
379 |
eval { $sth->execute( @args ) }; |
eval { $sth->execute( @args ) }; |
380 |
return if $@; |
return if $@; |
381 |
|
|
382 |
|
my $nr_results = $sth->rows; |
383 |
|
|
384 |
my $last_row = { |
my $last_row = { |
385 |
date => '', |
date => '', |
386 |
time => '', |
time => '', |
401 |
|
|
402 |
return @rows if ($args->{full_rows}); |
return @rows if ($args->{full_rows}); |
403 |
|
|
404 |
my @msgs = ( |
$msg .= ' produced ' . ( |
405 |
"Showing " . ($#rows + 1) . " messages..." |
$nr_results == 0 ? 'no results' : |
406 |
|
$nr_results == 0 ? 'one result' : |
407 |
|
$nr_results . ' results' |
408 |
); |
); |
409 |
|
|
410 |
|
my @msgs = ( $msg ); |
411 |
|
|
412 |
if ($context) { |
if ($context) { |
413 |
my @ids = @rows; |
my @ids = @rows; |
414 |
@rows = (); |
@rows = (); |
650 |
$total++; |
$total++; |
651 |
|
|
652 |
# seen allready? |
# seen allready? |
653 |
next if $_rss->{$feed->link}->{seen}->{$entry->id}++ > 0; |
next if $_rss->{$args->{channel}}->{$feed->link}->{$entry->id}++ > 0; |
654 |
|
|
655 |
sub prefix { |
sub prefix { |
656 |
my ($txt,$var) = @_; |
my ($txt,$var) = @_; |
660 |
return $txt . $var if $var; |
return $txt . $var if $var; |
661 |
} |
} |
662 |
|
|
663 |
|
# fix absolute and relative links to feed entries |
664 |
|
my $link = $entry->link; |
665 |
|
if ( $link =~ m!^/! ) { |
666 |
|
my $host = $args->{url}; |
667 |
|
$host =~ s!^(http://[^/]+).*$!$1!; #!vim |
668 |
|
$link = "$host/$link"; |
669 |
|
} elsif ( $link !~ m!^http! ) { |
670 |
|
$link = $args->{url} . $link; |
671 |
|
} |
672 |
|
|
673 |
my $msg; |
my $msg; |
674 |
$msg .= prefix( 'From: ' , $args->{name} || $feed->title ); |
$msg .= prefix( 'From: ' , $args->{name} || $feed->title ); |
675 |
$msg .= prefix( ' by ' , $entry->author ); |
$msg .= prefix( ' by ' , $entry->author ); |
676 |
$msg .= prefix( ' | ' , $entry->title ); |
$msg .= prefix( ' | ' , $entry->title ); |
677 |
$msg .= prefix( ' | ' , $entry->link ); |
$msg .= prefix( ' | ' , $link ); |
678 |
# $msg .= prefix( ' id ' , $entry->id ); |
# $msg .= prefix( ' id ' , $entry->id ); |
679 |
|
|
680 |
if ( $args->{kernel} && $send_rss_msgs ) { |
if ( $args->{kernel} && $send_rss_msgs ) { |
681 |
$send_rss_msgs--; |
$send_rss_msgs--; |
682 |
_log('>>', $msg); |
$sth_insert_log->execute( $args->{channel}, 1, $NICK, $msg, 'now()' ); |
683 |
$sth_insert_log->execute( $CHANNEL, 1, $NICK, $msg, 'now()' ); |
my ( $type, $to ) = ( 'notice', $args->{channel} ); |
684 |
$args->{kernel}->post( $IRC_ALIAS => notice => $CHANNEL, $msg ); |
( $type, $to ) = ( 'privmsg', $args->{nick} ) if $args->{private}; |
685 |
|
_log(">> $type $to |", $msg); |
686 |
|
$args->{kernel}->post( $IRC_ALIAS => $type => $to, $msg ); |
687 |
$updates++; |
$updates++; |
688 |
} |
} |
689 |
} |
} |
701 |
sub rss_fetch_all { |
sub rss_fetch_all { |
702 |
my $kernel = shift; |
my $kernel = shift; |
703 |
my $sql = qq{ |
my $sql = qq{ |
704 |
select id, url, name |
select id, url, name, channel, nick, private |
705 |
from feeds |
from feeds |
706 |
where active is true |
where active is true |
707 |
}; |
}; |
721 |
|
|
722 |
sub rss_check_updates { |
sub rss_check_updates { |
723 |
my $kernel = shift; |
my $kernel = shift; |
724 |
my $last_t = $_rss->{last_poll} || time(); |
$_rss->{last_poll} ||= time(); |
725 |
my $t = time(); |
my $dt = time() - $_rss->{last_poll}; |
726 |
if ( $t - $last_t > $rss_min_delay ) { |
warn "## rss_check_updates $dt > $rss_min_delay\n"; |
727 |
$_rss->{last_poll} = $t; |
if ( $dt > $rss_min_delay ) { |
728 |
|
$_rss->{last_poll} = time(); |
729 |
_log rss_fetch_all( $kernel ); |
_log rss_fetch_all( $kernel ); |
730 |
} |
} |
731 |
} |
} |
758 |
|
|
759 |
save_message( channel => $channel, me => 0, nick => $nick, message => $msg); |
save_message( channel => $channel, me => 0, nick => $nick, message => $msg); |
760 |
meta( $nick, $channel, 'last-msg', $msg ); |
meta( $nick, $channel, 'last-msg', $msg ); |
761 |
|
rss_check_updates( $kernel ); |
762 |
}, |
}, |
763 |
irc_ctcp_action => sub { |
irc_ctcp_action => sub { |
764 |
my $kernel = $_[KERNEL]; |
my $kernel = $_[KERNEL]; |
932 |
$dbh->do( qq{ update feeds set last_update = now() - delay } ); |
$dbh->do( qq{ update feeds set last_update = now() - delay } ); |
933 |
$res = "OK, cleaned RSS cache"; |
$res = "OK, cleaned RSS cache"; |
934 |
} elsif ($msg =~ m/^rss-list/) { |
} elsif ($msg =~ m/^rss-list/) { |
935 |
my $sth = $dbh->prepare(qq{ select url,name,last_update,active from feeds }); |
my $sth = $dbh->prepare(qq{ select url,name,last_update,active,channel,nick,private from feeds }); |
936 |
$sth->execute; |
$sth->execute; |
937 |
while (my @row = $sth->fetchrow_array) { |
while (my @row = $sth->fetchrow_array) { |
938 |
$_[KERNEL]->post( $IRC_ALIAS => privmsg => $nick, join(' | ',@row) ); |
$_[KERNEL]->post( $IRC_ALIAS => privmsg => $nick, join(' | ',@row) ); |
939 |
} |
} |
940 |
$res = ''; |
$res = ''; |
941 |
} elsif ($msg =~ m!^rss-(add|remove|stop|start)\s+(http://\S+)\s*(.*)!) { |
} elsif ($msg =~ m!^rss-(add|remove|stop|start)(?:-(private))?\s+(http://\S+)\s*(.*)!) { |
942 |
|
my ( $command, $sub, $url, $arg ) = ( $1,$2,$3,$4 ); |
943 |
|
|
944 |
|
my $channel = $1 if ( $arg =~ s/\s*(#\S+)\s*// ); |
945 |
|
$channel = $nick if $sub eq 'private'; |
946 |
|
|
947 |
my $sql = { |
my $sql = { |
948 |
add => qq{ insert into feeds (url,name) values (?,?) }, |
add => qq{ insert into feeds (url,name,channel,nick,private) values (?,?,?,?,?) }, |
949 |
# remove => qq{ delete from feeds where url = ? and name = ? }, |
# remove => qq{ delete from feeds where url = ? and name = ? }, |
950 |
start => qq{ update feeds set active = true where url = ? }, |
start => qq{ update feeds set active = true where url = ? }, |
951 |
stop => qq{ update feeds set active = false where url = ? }, |
stop => qq{ update feeds set active = false where url = ? }, |
|
|
|
952 |
}; |
}; |
953 |
if (my $q = $sql->{$1} ) { |
|
954 |
|
if ( $command eq 'add' && ! $channel ) { |
955 |
|
$res = "ERROR: got '$msg' which doesn't have #channel in it, ignoring!"; |
956 |
|
} elsif (my $q = $sql->{$command} ) { |
957 |
my $sth = $dbh->prepare( $q ); |
my $sth = $dbh->prepare( $q ); |
958 |
my @data = ( $2 ); |
my @data = ( $url ); |
959 |
push @data, $3 if ( $q =~ s/\?//g == 2 ); |
if ( $command eq 'add' ) { |
960 |
warn "## $1 SQL $q with ",dump( @data ),"\n"; |
push @data, ( $arg, $channel, $nick, $sub eq 'private' ? 1 : 0 ); |
961 |
|
} |
962 |
|
warn "## $command SQL $q with ",dump( @data ),"\n"; |
963 |
eval { $sth->execute( @data ) }; |
eval { $sth->execute( @data ) }; |
964 |
|
if ($@) { |
965 |
|
$res = "ERROR: $@"; |
966 |
|
} else { |
967 |
|
$res = "OK, RSS [$command|$sub|$url|$arg]"; |
968 |
|
} |
969 |
|
} else { |
970 |
|
$res = "ERROR: don't know what to do with: $msg"; |
971 |
} |
} |
|
|
|
|
$res = "OK, RSS $1 : $2 - $3"; |
|
972 |
} |
} |
973 |
|
|
974 |
if ($res) { |
if ($res) { |
1172 |
$feed->add_entry( $feed_entry ); |
$feed->add_entry( $feed_entry ); |
1173 |
} |
} |
1174 |
|
|
1175 |
|
my $feed_entry = XML::Feed::Entry->new($type); |
1176 |
|
$feed_entry->title( "Internal stats" ); |
1177 |
|
$feed_entry->content( |
1178 |
|
'<![CDATA[<pre>' . dump( $_rss ) . '</pre>]]>' |
1179 |
|
); |
1180 |
|
$feed->add_entry( $feed_entry ); |
1181 |
|
|
1182 |
} else { |
} else { |
1183 |
_log "unknown rss request ",$request->url; |
_log "unknown rss request ",$request->url; |
1184 |
return RC_DENY; |
return RC_DENY; |