76 |
use URI::Escape; |
use URI::Escape; |
77 |
use Data::Dump qw/dump/; |
use Data::Dump qw/dump/; |
78 |
use DateTime::Format::ISO8601; |
use DateTime::Format::ISO8601; |
79 |
|
use Carp qw/confess/; |
80 |
|
|
81 |
my $use_twitter = 1; |
my $use_twitter = 1; |
82 |
eval { require Net::Twitter; }; |
eval { require Net::Twitter; }; |
89 |
'log:s' => \$log_path, |
'log:s' => \$log_path, |
90 |
); |
); |
91 |
|
|
92 |
|
$SIG{__DIE__} = sub { |
93 |
|
confess "fatal error"; |
94 |
|
}; |
95 |
|
|
96 |
open(STDOUT, '>', $log_path) || warn "can't redirect log to $log_path: $!"; |
open(STDOUT, '>', $log_path) || warn "can't redirect log to $log_path: $!"; |
97 |
|
|
98 |
sub _log { |
sub _log { |
235 |
sub get_from_log { |
sub get_from_log { |
236 |
my $args = {@_}; |
my $args = {@_}; |
237 |
|
|
238 |
$args->{fmt} ||= { |
if ( ! $args->{fmt} ) { |
239 |
date => '[%s] ', |
$args->{fmt} = { |
240 |
time => '{%s} ', |
date => '[%s] ', |
241 |
time_channel => '{%s %s} ', |
time => '{%s} ', |
242 |
nick => '%s: ', |
time_channel => '{%s %s} ', |
243 |
me_nick => '***%s ', |
nick => '%s: ', |
244 |
message => '%s', |
me_nick => '***%s ', |
245 |
}; |
message => '%s', |
246 |
|
}; |
247 |
|
} |
248 |
|
|
249 |
my $sql_message = qq{ |
my $sql_message = qq{ |
250 |
select |
select |
267 |
|
|
268 |
my $sql = $context ? $sql_context : $sql_message; |
my $sql = $context ? $sql_context : $sql_message; |
269 |
|
|
270 |
$sql .= " where message ilike ? or nick ilike ? " if ($args->{search}); |
sub check_date { |
271 |
$sql .= " where id in (" . join(",", @{ $tags->{ $args->{tag} } }) . ") " if ($args->{tag} && $tags->{ $args->{tag} }); |
my $date = shift || return; |
272 |
if ($args->{date}) { |
my $new_date = eval { DateTime::Format::ISO8601->parse_datetime( $date )->ymd; }; |
|
my $date = eval { DateTime::Format::ISO8601->parse_datetime( $args->{date} )->ymd; }; |
|
273 |
if ( $@ ) { |
if ( $@ ) { |
274 |
warn "invalid date ", $args->{date}, $/; |
warn "invalid date $date\n"; |
275 |
$date = DateTime->now->ymd; |
$new_date = DateTime->now->ymd; |
276 |
} |
} |
277 |
$sql .= " where date(time) = ? "; |
return $new_date; |
|
$args->{date} = $date; |
|
278 |
} |
} |
|
$sql .= " order by log.time desc"; |
|
|
$sql .= " limit " . $args->{limit} if ($args->{limit}); |
|
279 |
|
|
280 |
my $sth = $dbh->prepare( $sql ); |
my @where; |
281 |
|
my @args; |
282 |
|
|
283 |
if (my $search = $args->{search}) { |
if (my $search = $args->{search}) { |
284 |
$search =~ s/^\s+//; |
$search =~ s/^\s+//; |
285 |
$search =~ s/\s+$//; |
$search =~ s/\s+$//; |
286 |
$sth->execute( ( '%' . $search . '%' ) x 2 ); |
push @where, 'message ilike ? or nick ilike ?'; |
287 |
_log "search for '$search' returned ", $sth->rows, " results ", $context || ''; |
push @args, ( ( '%' . $search . '%' ) x 2 ); |
288 |
} elsif (my $tag = $args->{tag}) { |
_log "search for '$search'"; |
289 |
$sth->execute(); |
} |
290 |
_log "tag '$tag' returned ", $sth->rows, " results ", $context || ''; |
|
291 |
} elsif (my $date = $args->{date}) { |
if ($args->{tag} && $tags->{ $args->{tag} }) { |
292 |
$sth->execute($date); |
push @where, 'id in (' . join(',', @{ $tags->{ $args->{tag} } }) . ')'; |
293 |
_log "found ", $sth->rows, " messages for date $date ", $context || ''; |
_log "search for tags $args->{tag}"; |
|
} else { |
|
|
$sth->execute(); |
|
294 |
} |
} |
295 |
|
|
296 |
|
if (my $date = $args->{date} ) { |
297 |
|
$date = check_date( $date ); |
298 |
|
push @where, 'date(time) = ?'; |
299 |
|
push @args, $date; |
300 |
|
_log "search for date $date"; |
301 |
|
} |
302 |
|
|
303 |
|
$sql .= " where " . join(" and ", @where) if @where; |
304 |
|
|
305 |
|
$sql .= " order by log.time desc"; |
306 |
|
$sql .= " limit " . $args->{limit} if ($args->{limit}); |
307 |
|
|
308 |
|
#warn "### sql: $sql ", dump( @args ); |
309 |
|
|
310 |
|
my $sth = $dbh->prepare( $sql ); |
311 |
|
eval { $sth->execute( @args ) }; |
312 |
|
return if $@; |
313 |
|
|
314 |
my $last_row = { |
my $last_row = { |
315 |
date => '', |
date => '', |
316 |
time => '', |
time => '', |
510 |
if ($import_dircproxy) { |
if ($import_dircproxy) { |
511 |
open(my $l, $import_dircproxy) || die "can't open $import_dircproxy: $!"; |
open(my $l, $import_dircproxy) || die "can't open $import_dircproxy: $!"; |
512 |
warn "importing $import_dircproxy...\n"; |
warn "importing $import_dircproxy...\n"; |
513 |
my $tz_offset = 2 * 60 * 60; # TZ GMT+2 |
my $tz_offset = 1 * 60 * 60; # TZ GMT+2 |
514 |
while(<$l>) { |
while(<$l>) { |
515 |
chomp; |
chomp; |
516 |
if (/^@(\d+)\s(\S+)\s(.+)$/) { |
if (/^@(\d+)\s(\S+)\s(.+)$/) { |
971 |
} else { |
} else { |
972 |
$html .= join("</p><p>", |
$html .= join("</p><p>", |
973 |
get_from_log( |
get_from_log( |
974 |
limit => $q->param('last') || $q->param('date') ? undef : 100, |
limit => ( $q->param('last') || $q->param('date') ) ? undef : 100, |
975 |
search => $search || undef, |
search => $search || undef, |
976 |
tag => $q->param('tag') || undef, |
tag => $q->param('tag') || undef, |
977 |
date => $q->param('date') || undef, |
date => $q->param('date') || undef, |