73 |
use HTML::CalendarMonthSimple; |
use HTML::CalendarMonthSimple; |
74 |
use Getopt::Long; |
use Getopt::Long; |
75 |
use DateTime; |
use DateTime; |
76 |
|
use URI::Escape; |
77 |
use Data::Dump qw/dump/; |
use Data::Dump qw/dump/; |
78 |
|
use DateTime::Format::ISO8601; |
79 |
|
|
80 |
|
my $use_twitter = 1; |
81 |
|
eval { require Net::Twitter; }; |
82 |
|
$use_twitter = 0 if ($@); |
83 |
|
|
84 |
my $import_dircproxy; |
my $import_dircproxy; |
85 |
my $log_path; |
my $log_path; |
96 |
|
|
97 |
my $dbh = DBI->connect($DSN,"","", { RaiseError => 1, AutoCommit => 1 }) || die $DBI::errstr; |
my $dbh = DBI->connect($DSN,"","", { RaiseError => 1, AutoCommit => 1 }) || die $DBI::errstr; |
98 |
|
|
99 |
eval { |
my $sql_schema = { |
100 |
$dbh->do(qq{ select count(*) from log }); |
log => ' |
|
}; |
|
|
|
|
|
if ($@) { |
|
|
warn "creating database table in $DSN\n"; |
|
|
$dbh->do(<<'_SQL_SCHEMA_'); |
|
|
|
|
101 |
create table log ( |
create table log ( |
102 |
id serial, |
id serial, |
103 |
time timestamp default now(), |
time timestamp default now(), |
111 |
create index log_time on log(time); |
create index log_time on log(time); |
112 |
create index log_channel on log(channel); |
create index log_channel on log(channel); |
113 |
create index log_nick on log(nick); |
create index log_nick on log(nick); |
114 |
|
', |
115 |
|
meta => ' |
116 |
|
create table meta ( |
117 |
|
nick text not null, |
118 |
|
channel text not null, |
119 |
|
name text not null, |
120 |
|
value text, |
121 |
|
changed timestamp default now(), |
122 |
|
primary key(nick,channel,name) |
123 |
|
); |
124 |
|
', |
125 |
|
}; |
126 |
|
|
127 |
|
foreach my $table ( keys %$sql_schema ) { |
128 |
|
|
129 |
_SQL_SCHEMA_ |
eval { |
130 |
|
$dbh->do(qq{ select count(*) from $table }); |
131 |
|
}; |
132 |
|
|
133 |
|
if ($@) { |
134 |
|
warn "creating database table $table in $DSN\n"; |
135 |
|
$dbh->do( $sql_schema->{ $table } ); |
136 |
|
} |
137 |
} |
} |
138 |
|
|
139 |
|
|
140 |
|
=head2 meta |
141 |
|
|
142 |
|
Set or get some meta data into database |
143 |
|
|
144 |
|
meta('nick','channel','var_name', $var_value ); |
145 |
|
|
146 |
|
$var_value = meta('nick','channel','var_name'); |
147 |
|
( $var_value, $changed ) = meta('nick','channel','var_name'); |
148 |
|
|
149 |
|
=cut |
150 |
|
|
151 |
|
sub meta { |
152 |
|
my ($nick,$channel,$name,$value) = @_; |
153 |
|
|
154 |
|
# normalize channel name |
155 |
|
$channel =~ s/^#//; |
156 |
|
|
157 |
|
if (defined($value)) { |
158 |
|
|
159 |
|
my $sth = $dbh->prepare(qq{ update meta set value = ?, changed = now() where nick = ? and channel = ? and name = ? }); |
160 |
|
|
161 |
|
eval { $sth->execute( $value, $nick, $channel, $name ) }; |
162 |
|
|
163 |
|
# error or no result |
164 |
|
if ( $@ || ! $sth->rows ) { |
165 |
|
$sth = $dbh->prepare(qq{ insert into meta (value,nick,channel,name,changed) values (?,?,?,?,now()) }); |
166 |
|
$sth->execute( $value, $nick, $channel, $name ); |
167 |
|
_log "created $nick/$channel/$name = $value"; |
168 |
|
} else { |
169 |
|
_log "updated $nick/$channel/$name = $value "; |
170 |
|
} |
171 |
|
|
172 |
|
return $value; |
173 |
|
|
174 |
|
} else { |
175 |
|
|
176 |
|
my $sth = $dbh->prepare(qq{ select value,changed from meta where nick = ? and channel = ? and name = ? }); |
177 |
|
$sth->execute( $nick, $channel, $name ); |
178 |
|
my ($v,$c) = $sth->fetchrow_array; |
179 |
|
_log "fetched $nick/$channel/$name = $v [$c]"; |
180 |
|
return ($v,$c) if wantarray; |
181 |
|
return $v; |
182 |
|
|
183 |
|
} |
184 |
|
} |
185 |
|
|
186 |
|
|
187 |
|
|
188 |
my $sth = $dbh->prepare(qq{ |
my $sth = $dbh->prepare(qq{ |
189 |
insert into log |
insert into log |
190 |
(channel, me, nick, message, time) |
(channel, me, nick, message, time) |
191 |
values (?,?,?,?,?) |
values (?,?,?,?,?) |
192 |
}); |
}); |
193 |
|
|
194 |
|
|
195 |
my $tags; |
my $tags; |
196 |
my $tag_regex = '\b([\w-_]+)//'; |
my $tag_regex = '\b([\w-_]+)//'; |
197 |
|
|
260 |
|
|
261 |
my $sql = $context ? $sql_context : $sql_message; |
my $sql = $context ? $sql_context : $sql_message; |
262 |
|
|
263 |
|
sub check_date { |
264 |
|
my $date = shift; |
265 |
|
$date = eval { DateTime::Format::ISO8601->parse_datetime( $args->{date} )->ymd; }; |
266 |
|
if ( $@ ) { |
267 |
|
warn "invalid date ", $args->{date}, $/; |
268 |
|
$date = DateTime->now->ymd; |
269 |
|
} |
270 |
|
return $date; |
271 |
|
} |
272 |
|
|
273 |
$sql .= " where message ilike ? or nick ilike ? " if ($args->{search}); |
$sql .= " where message ilike ? or nick ilike ? " if ($args->{search}); |
274 |
$sql .= " where id in (" . join(",", @{ $tags->{ $args->{tag} } }) . ") " if ($args->{tag} && $tags->{ $args->{tag} }); |
$sql .= " where id in (" . join(",", @{ $tags->{ $args->{tag} } }) . ") " if ($args->{tag} && $tags->{ $args->{tag} }); |
275 |
$sql .= " where date(time) = ? " if ($args->{date}); |
if ($args->{date}) { |
276 |
|
$sql .= " where date(time) = ? "; |
277 |
|
$args->{date} = check_date( $args->{date} ); |
278 |
|
} |
279 |
$sql .= " order by log.time desc"; |
$sql .= " order by log.time desc"; |
280 |
$sql .= " limit " . $args->{limit} if ($args->{limit}); |
$sql .= " limit " . $args->{limit} if ($args->{limit}); |
281 |
|
|
289 |
$sth->execute(); |
$sth->execute(); |
290 |
_log "tag '$tag' returned ", $sth->rows, " results ", $context || ''; |
_log "tag '$tag' returned ", $sth->rows, " results ", $context || ''; |
291 |
} elsif (my $date = $args->{date}) { |
} elsif (my $date = $args->{date}) { |
292 |
$sth->execute($date); |
$sth->execute( check_date($date) ); |
293 |
_log "found ", $sth->rows, " messages for date $date ", $context || ''; |
_log "found ", $sth->rows, " messages for date $date ", $context || ''; |
294 |
} else { |
} else { |
295 |
$sth->execute(); |
$sth->execute(); |
489 |
message => $a->{msg}); |
message => $a->{msg}); |
490 |
} |
} |
491 |
|
|
492 |
|
|
493 |
if ($import_dircproxy) { |
if ($import_dircproxy) { |
494 |
open(my $l, $import_dircproxy) || die "can't open $import_dircproxy: $!"; |
open(my $l, $import_dircproxy) || die "can't open $import_dircproxy: $!"; |
495 |
warn "importing $import_dircproxy...\n"; |
warn "importing $import_dircproxy...\n"; |
554 |
my $msg = $_[ARG2]; |
my $msg = $_[ARG2]; |
555 |
|
|
556 |
save_message( channel => $channel, me => 0, nick => $nick, msg => $msg); |
save_message( channel => $channel, me => 0, nick => $nick, msg => $msg); |
557 |
|
meta( $nick, $channel, 'last-msg', $msg ); |
558 |
}, |
}, |
559 |
irc_ctcp_action => sub { |
irc_ctcp_action => sub { |
560 |
my $kernel = $_[KERNEL]; |
my $kernel = $_[KERNEL]; |
563 |
my $msg = $_[ARG2]; |
my $msg = $_[ARG2]; |
564 |
|
|
565 |
save_message( channel => $channel, me => 1, nick => $nick, msg => $msg); |
save_message( channel => $channel, me => 1, nick => $nick, msg => $msg); |
566 |
|
|
567 |
|
if ( $use_twitter ) { |
568 |
|
if ( my $twitter = meta( $nick, $NICK, 'twitter' ) ) { |
569 |
|
my ($login,$passwd) = split(/\s+/,$twitter,2); |
570 |
|
_log("sending twitter for $nick/$login on $channel "); |
571 |
|
my $bot = Net::Twitter->new( username=>$login, password=>$passwd ); |
572 |
|
$bot->update("<${channel}> $msg"); |
573 |
|
} |
574 |
|
} |
575 |
|
|
576 |
}, |
}, |
577 |
irc_ping => sub { |
irc_ping => sub { |
578 |
warn "pong ", $_[ARG0], $/; |
warn "pong ", $_[ARG0], $/; |
582 |
my $kernel = $_[KERNEL]; |
my $kernel = $_[KERNEL]; |
583 |
my $nick = (split /!/, $_[ARG0])[0]; |
my $nick = (split /!/, $_[ARG0])[0]; |
584 |
my $channel = $_[ARG1]; |
my $channel = $_[ARG1]; |
|
|
|
585 |
|
|
586 |
warn "invited to $channel by $nick"; |
warn "invited to $channel by $nick"; |
587 |
|
|
593 |
my $kernel = $_[KERNEL]; |
my $kernel = $_[KERNEL]; |
594 |
my $nick = (split /!/, $_[ARG0])[0]; |
my $nick = (split /!/, $_[ARG0])[0]; |
595 |
my $msg = $_[ARG2]; |
my $msg = $_[ARG2]; |
596 |
|
my $channel = $_[ARG1]->[0]; |
597 |
from_to($msg, 'UTF-8', $ENCODING); |
from_to($msg, 'UTF-8', $ENCODING); |
598 |
|
|
599 |
my $res = "unknown command '$msg', try /msg $NICK help!"; |
my $res = "unknown command '$msg', try /msg $NICK help!"; |
617 |
|
|
618 |
my $sth = $dbh->prepare(qq{ |
my $sth = $dbh->prepare(qq{ |
619 |
select |
select |
620 |
nick, |
trim(both '_' from nick) as nick, |
621 |
count(*) as count, |
count(*) as count, |
622 |
sum(length(message)) as len |
sum(length(message)) as len |
623 |
from log |
from log |
624 |
group by nick |
group by trim(both '_' from nick) |
625 |
order by len desc,count desc |
order by len desc,count desc |
626 |
limit $nr |
limit $nr |
627 |
}); |
}); |
634 |
$res .= join(" | ", @users); |
$res .= join(" | ", @users); |
635 |
} elsif ($msg =~ m/^last.*?\s*(\d*)/i) { |
} elsif ($msg =~ m/^last.*?\s*(\d*)/i) { |
636 |
|
|
637 |
foreach my $res (get_from_log( limit => ($1 || 100) )) { |
my $limit = $1 || meta( $nick, $channel, 'last-size' ) || 10; |
638 |
|
|
639 |
|
foreach my $res (get_from_log( limit => $limit )) { |
640 |
_log "last: $res"; |
_log "last: $res"; |
641 |
from_to($res, $ENCODING, 'UTF-8'); |
from_to($res, $ENCODING, 'UTF-8'); |
642 |
$_[KERNEL]->post( $IRC_ALIAS => privmsg => $nick, $res ); |
$_[KERNEL]->post( $IRC_ALIAS => privmsg => $nick, $res ); |
693 |
|
|
694 |
} elsif ($msg =~ m/^ping/) { |
} elsif ($msg =~ m/^ping/) { |
695 |
$res = "ping = " . dump( $ping ); |
$res = "ping = " . dump( $ping ); |
696 |
|
} elsif ($msg =~ m/^conf(?:ig)*\s*(last-size|twitter)*\s*(.*)/) { |
697 |
|
if ( ! defined( $1 ) ) { |
698 |
|
my $sth = $dbh->prepare(qq{ select name,value,changed from meta where nick = ? and channel = ? }); |
699 |
|
$sth->execute( $nick, $channel ); |
700 |
|
$res = "config for $nick on $channel"; |
701 |
|
while ( my ($n,$v) = $sth->fetchrow_array ) { |
702 |
|
$res .= " | $n = $v"; |
703 |
|
} |
704 |
|
} elsif ( ! $2 ) { |
705 |
|
my $val = meta( $nick, $channel, $1 ); |
706 |
|
$res = "current $1 = " . ( $val ? $val : 'undefined' ); |
707 |
|
} else { |
708 |
|
my $validate = { |
709 |
|
'last-size' => qr/^\d+/, |
710 |
|
'twitter' => qr/^\w+\s+\w+/, |
711 |
|
}; |
712 |
|
|
713 |
|
my ( $op, $val ) = ( $1, $2 ); |
714 |
|
|
715 |
|
if ( my $regex = $validate->{$op} ) { |
716 |
|
if ( $val =~ $regex ) { |
717 |
|
meta( $nick, $channel, $op, $val ); |
718 |
|
$res = "saved $op = $val"; |
719 |
|
} else { |
720 |
|
$res = "config option $op = $val doesn't validate against $regex"; |
721 |
|
} |
722 |
|
} else { |
723 |
|
$res = "config option $op doesn't exist"; |
724 |
|
} |
725 |
|
} |
726 |
} |
} |
727 |
|
|
728 |
if ($res) { |
if ($res) { |
850 |
.nick { color: #000000; font-size: 80%; padding: 2px; font-family: courier, courier new, monospace ; } |
.nick { color: #000000; font-size: 80%; padding: 2px; font-family: courier, courier new, monospace ; } |
851 |
.message { color: #000000; font-size: 100%; } |
.message { color: #000000; font-size: 100%; } |
852 |
.search { float: right; } |
.search { float: right; } |
853 |
|
a:link.tag, a:visited.tag { border: 1px dashed #ccc; backgound: #ccc; text-decoration: none } |
854 |
|
a:hover.tag { border: 1px solid #eee } |
855 |
|
hr { border: 1px dashed #ccc; height: 1px; clear: both; } |
856 |
|
/* |
857 |
.col-0 { background: #ffff66 } |
.col-0 { background: #ffff66 } |
858 |
.col-1 { background: #a0ffff } |
.col-1 { background: #a0ffff } |
859 |
.col-2 { background: #99ff99 } |
.col-2 { background: #99ff99 } |
860 |
.col-3 { background: #ff9999 } |
.col-3 { background: #ff9999 } |
861 |
.col-4 { background: #ff66ff } |
.col-4 { background: #ff66ff } |
862 |
a:link.tag, a:visited.tag { border: 1px dashed #ccc; backgound: #ccc; text-decoration: none } |
*/ |
863 |
a:hover.tag { border: 1px solid #eee } |
.calendar { border: 1px solid red; width: 100%; } |
864 |
hr { border: 1px dashed #ccc; height: 1px; clear: both; } |
.month { border: 0px; width: 100%; } |
865 |
_END_OF_STYLE_ |
_END_OF_STYLE_ |
866 |
|
|
867 |
my $max_color = 4; |
my $max_color = 4; |
868 |
|
|
869 |
|
my @cols = qw( |
870 |
|
#ffcccc #ccffe6 #ccccff #e6ccff #ffccff #ffcce6 #ff9999 #ffcc99 #ffff99 |
871 |
|
#ccff99 #99ff99 #99ffcc #99ccff #9999ff #cc99ff #ff6666 #ffb366 #ffff66 |
872 |
|
#66ff66 #66ffb3 #66b3ff #6666ff #ff3333 #33ff33 #3399ff #3333ff #ff3399 |
873 |
|
#a0a0a0 #ff0000 #ffff00 #80ff00 #0000ff #8000ff #ff00ff #ff0080 #994d00 |
874 |
|
#999900 #009900 #cc0066 #c0c0c0 #ccff99 #99ff33 #808080 #660033 #ffffff |
875 |
|
); |
876 |
|
|
877 |
|
$max_color = 0; |
878 |
|
foreach my $c (@cols) { |
879 |
|
$style .= ".col-${max_color} { background: $c }\n"; |
880 |
|
$max_color++; |
881 |
|
} |
882 |
|
warn "defined $max_color colors for users...\n"; |
883 |
|
|
884 |
my %nick_enumerator; |
my %nick_enumerator; |
885 |
|
|
886 |
sub root_handler { |
sub root_handler { |
914 |
qq{<p>}; |
qq{<p>}; |
915 |
if ($request->url =~ m#/history#) { |
if ($request->url =~ m#/history#) { |
916 |
my $sth = $dbh->prepare(qq{ |
my $sth = $dbh->prepare(qq{ |
917 |
select date(time) as date,count(*) as nr |
select date(time) as date,count(*) as nr,sum(length(message)) as len |
918 |
from log |
from log |
919 |
group by date(time) |
group by date(time) |
920 |
order by date(time) desc |
order by date(time) desc |
921 |
}); |
}); |
922 |
$sth->execute(); |
$sth->execute(); |
923 |
my ($l_yyyy,$l_mm) = (0,0); |
my ($l_yyyy,$l_mm) = (0,0); |
924 |
|
$html .= qq{<table class="calendar"><tr>}; |
925 |
my $cal; |
my $cal; |
926 |
|
my $ord = 0; |
927 |
while (my $row = $sth->fetchrow_hashref) { |
while (my $row = $sth->fetchrow_hashref) { |
928 |
# this is probably PostgreSQL specific, expects ISO date |
# this is probably PostgreSQL specific, expects ISO date |
929 |
my ($yyyy,$mm,$dd) = split(/-/, $row->{date}); |
my ($yyyy,$mm,$dd) = split(/-/, $row->{date}); |
930 |
if ($yyyy != $l_yyyy || $mm != $l_mm) { |
if ($yyyy != $l_yyyy || $mm != $l_mm) { |
931 |
$html .= $cal->as_HTML() if ($cal); |
if ( $cal ) { |
932 |
|
$html .= qq{<td valign="top">} . $cal->as_HTML() . qq{</td>}; |
933 |
|
$ord++; |
934 |
|
$html .= qq{</tr><tr>} if ( $ord % 3 == 0 ); |
935 |
|
} |
936 |
$cal = new HTML::CalendarMonthSimple('month'=>$mm,'year'=>$yyyy); |
$cal = new HTML::CalendarMonthSimple('month'=>$mm,'year'=>$yyyy); |
937 |
$cal->border(2); |
$cal->border(1); |
938 |
|
$cal->width('30%'); |
939 |
|
$cal->cellheight('5em'); |
940 |
|
$cal->tableclass('month'); |
941 |
|
#$cal->cellclass('day'); |
942 |
|
$cal->sunday('SUN'); |
943 |
|
$cal->saturday('SAT'); |
944 |
|
$cal->weekdays('MON','TUE','WED','THU','FRI'); |
945 |
($l_yyyy,$l_mm) = ($yyyy,$mm); |
($l_yyyy,$l_mm) = ($yyyy,$mm); |
946 |
} |
} |
947 |
$cal->setcontent($dd, qq{ |
$cal->setcontent($dd, qq{ |
948 |
<a href="/?date=$row->{date}">$row->{nr}</a> |
<a href="/?date=$row->{date}">$row->{nr}</a><br/>$row->{len} |
949 |
}); |
}); |
950 |
|
|
951 |
} |
} |
952 |
$html .= $cal->as_HTML() if ($cal); |
$html .= qq{<td valign="top">} . $cal->as_HTML() . qq{</td></tr></table>}; |
953 |
|
|
954 |
} else { |
} else { |
955 |
$html .= join("</p><p>", |
$html .= join("</p><p>", |
957 |
limit => $q->param('last') || $q->param('date') ? undef : 100, |
limit => $q->param('last') || $q->param('date') ? undef : 100, |
958 |
search => $search || undef, |
search => $search || undef, |
959 |
tag => $q->param('tag') || undef, |
tag => $q->param('tag') || undef, |
960 |
date => $q->param('date') || undef, |
date => check_date( $q->param('date') ), |
961 |
fmt => { |
fmt => { |
962 |
date => sub { |
date => sub { |
963 |
my $date = shift || return; |
my $date = shift || return; |
972 |
filter => { |
filter => { |
973 |
message => sub { |
message => sub { |
974 |
my $m = shift || return; |
my $m = shift || return; |
975 |
|
|
976 |
|
# protect HTML from wiki modifications |
977 |
|
sub e { |
978 |
|
my $t = shift; |
979 |
|
return 'uri_unescape{' . uri_escape($t) . '}'; |
980 |
|
} |
981 |
|
|
982 |
$m =~ s/($escape_re)/$escape{$1}/gs; |
$m =~ s/($escape_re)/$escape{$1}/gs; |
983 |
$m =~ s#($RE{URI}{HTTP})#<a href="$1">$1</a>#gs; |
$m =~ s#($RE{URI}{HTTP})#e(qq{<a href="$1">$1</a>})#egs; |
984 |
$m =~ s#$tag_regex#<a href="?tag=$1" class="tag">$1</a>#g; |
$m =~ s#$tag_regex#e(qq{<a href="?tag=$1" class="tag">$1</a>})#egs; |
985 |
|
$m =~ s#\*(\w+)\*#<b>$1</b>#gs; |
986 |
|
$m =~ s#_(\w+)_#<u>$1</u>#gs; |
987 |
|
$m =~ s#\/(\w+)\/#<i>$1</i>#gs; |
988 |
|
|
989 |
|
$m =~ s#uri_unescape{([^}]+)}#uri_unescape($1)#egs; |
990 |
return $m; |
return $m; |
991 |
}, |
}, |
992 |
nick => sub { |
nick => sub { |