diff options
-rw-r--r-- | util/waterball.pl | 199 |
1 files changed, 66 insertions, 133 deletions
diff --git a/util/waterball.pl b/util/waterball.pl index f76d32f9..2258356a 100644 --- a/util/waterball.pl +++ b/util/waterball.pl @@ -1,164 +1,97 @@ #!/usr/bin/perl # $Id$ use lib '/home/bbs/bin/'; -use LocalVars; use Time::Local; -use POSIX; -use FileHandle; -use strict; +use LocalVars; use Mail::Sender; +use IO::All; -my($fndes, $fnsrc, $userid, $mailto, $outmode); -foreach $fndes ( <$JOBSPOOL/water.des.*> ){ #des: userid, mailto, outmode - (open FH, "< $fndes") or next; - chomp($userid = <FH>); - chomp($mailto = <FH>); - chomp($outmode= <FH>); - close FH; - next if( !$userid ); - print "$userid, $mailto, $outmode\n"; - `rm -Rf $TMP/water`; - `mkdir -p $TMP/water`; +sub main +{ + foreach $fndes ( <$JOBSPOOL/water.des.*> ){ + ($userid, $mailto, $outmode, $fnsrc) = parsedes($fndes); + next if( !userid || $mailto !~ /\@/ || !-e $fnsrc ); - $fnsrc = $fndes; - $fnsrc =~ s/\.des\./\.src\./; - eval{ + print "($userid, $mailto, $outmode, $fnsrc)\n"; + undef %water; process($fnsrc, "$TMP/water/", $outmode, $userid); - }; - if( $@ ){ - print "$@\n"; - } - else{ - chdir "$TMP/water"; - if( $mailto eq '.' || $mailto =~ /\.bbs/ ){ - $mailto = "$userid.bbs\@$hostname" if( $mailto eq '.' ); - foreach my $fn ( <$TMP/water/*> ){ - my $who = substr($fn, rindex($fn, '/') + 1); - my $content = ''; - open FH, "< $fn";while( <FH> ){chomp;$content .= "$_\n";} - if( !MakeMail({mailto => $mailto, - subject => "和 $who 的水球記錄", - body => $content, - }) ){ print "fault\n"; } - sleep(2) if( $mailto =~ /\.bbs/ ); - } - unlink $fnsrc; - unlink $fndes; - } - else{ - my $body = - "親愛的使用者您好:\n\n". - "歡迎您使用 Ptt系列的水球整理功能 ^_^\n". - "水球整理的結果被壓縮好附加在本信中\n". - "您須要先將其解壓縮 (如用 tar+gunzip, winzip 等程式)\n". - "解出來的檔案為純文字格式, \n". - "您可以透過任何純文字編輯程式 (如 emacs, notepad, word)\n". - "打開它進行編輯整理\n\n". - "再次感謝您使用本系統以及對 $hostname 的支持 ^^\n". - "\n $hostname 站長群 ". POSIX::ctime(time()); - if( MakeMail({tartarget => "$TMP/$userid.waterball.tgz", - tarsource => "*", - mailto => "$userid <$mailto>", - subject => "水球紀錄", - body => $body}) ){ - unlink $fnsrc; - unlink $fndes; - } - } + output($mailto eq '.' ? "$userid.bbs\@$MYHOSTNAME" : $mailto, + $mailto eq '.' || $mailto =~ /\.bbs/); + unlink($fndes, $fnsrc); } } -sub process +sub parsedes($) +{ + my $t < io($_[0]); + my $fnsrc = $_[0]; + $fnsrc =~ s/\.des\./\.src\./; + return (split("\n", $t), $fnsrc); +} + +sub process($$$$) { my($fn, $outdir, $outmode, $me) = @_; - my($cmode, $who, $time, $say, $orig, %FH, %LAST, $len); - open DIN, "< $fn"; + open DIN, "<$fn"; while( <DIN> ){ - chomp; - next if( !(($cmode, $who, $time, $say, $orig) = parse($_)) ); - next if( !$who ); + next if( !(($cmode, $who, $time, $say, $orig) = parse($_)) || !who ); - if( ! $FH{$who} ){ - $FH{$who} = new FileHandle "> $outdir/$who"; - } - if( $outmode == 0 ){ - next if( $say =~ /<<下站通知>> -- 我走囉!/ || - $say =~ /<<上站通知>> -- 我來啦!/ ); + if( $outmode ){ + $water{$who} .= $orig; + } else { + next if( $say =~ /<<(上|下)站通知>> -- 我(走|來)囉!/ ); if( $time - $LAST{$who} > 1800 ){ - if( $LAST{$who} != 0 ){ - ($FH{$who})->print( POSIX::ctime($LAST{$who}) , "\n"); - } - ($FH{$who})->print( POSIX::ctime($time) ); - $LAST{$who} = $time; + $water{$who} .= (scalar localtime($LAST{$who}))."\n\n" + if( $LAST{$who} ); + $water{$who} .= scalar localtime($time) . "\n"; } - $len = (length($who) > length($me) ? length($who) : length($me))+1; - ($FH{$who})->printf("%-${len}s %s\n", ($cmode?$who:$me).':', $say); - } - elsif( $outmode == 1 ){ - ($FH{$who})->print("$orig\n"); + + $len = max(length($who), length($me)) + 1; + $water{$who} .= sprintf("%-${len}s %s\n", + ($cmode ? $who : $me).':' , + $say); + $LAST{$who} = $time; } } if( $outmode == 0 ){ - foreach( keys %FH ){ - ($FH{$_})->print( POSIX::ctime($LAST{$_}) ); - } + $water{$_} .= scalar localtime($LAST{$_}) + foreach( keys %LAST ); } - foreach( keys %FH ){ - ($FH{$_})->close(); - } - close DIN; } -sub parse +sub parse($) { - my $dat = $_[0]; + my($str) = @_; my($cmode, $who, $year, $month, $day, $hour, $min, $sec, $say); - if( $dat =~ /^To/ ){ - $cmode = 0; - ($who, $say, $month, $day, $year, $hour, $min, $sec) = - $dat =~ m|^To (\w+):\s*(.*)\[(\d+)/(\d+)/(\d+) (\d+):(\d+):(\d+)\]|; - } - else{ - $cmode = 1; - ($who, $say, $month, $day, $year, $hour, $min, $sec) = - $dat =~ m|★(\w+?)\[37;45m\s*(.*).*?\[(\w+)/(\w+)/(\w+) (\w+):(\w+):(\w+)\]|; + $cmode = ($str =~ /^To/) ? 0 : 1; + ($who, $say, $month, $day, $year, $hour, $min, $sec) = + $cmode ? + $str =~ m|★(\w+?)\[37;45m\s*(.*).*?\[(\w+)/(\w+)/(\w+) (\w+):(\w+):(\w+)\]| : + $str =~ m|^To (\w+):\s*(.*)\[(\d+)/(\d+)/(\d+) (\d+):(\d+):(\d+)\]|; + return (!$month ? () : + ($cmode, $who, + timelocal($sec, $min, $hour, $day, $month - 1, $year), + $say, $_[0])); +} - } -# $time = timelocal($sec,$min,$hours,$mday,$mon,$year); +sub output +{ + my($tomail, $bbsmail) = @_; + my $ms = new Mail::Sender{smtp => $SMTPSERVER, + from => "$userid.bbs\@$MYHOSTNAME", + charset => 'big5'}; - return undef if( $month == 0 ); - return ($cmode, $who, timelocal($sec, $min, $hour, $day, $month - 1, $year), $say, $_[0]); + foreach( keys %water ){ + $ms->MailMsg({to => $tomail, + subject => "和 $_ 的水球記錄", + msg => $water{$_}}); + } } -sub MakeMail +sub max { - my($arg) = @_; - my $sender; - `$TAR zcf $arg->{tartarget} $arg->{tarsource}` - if( $arg->{tarsource} ); - $sender = new Mail::Sender{smtp => $SMTPSERVER, - from => "$hostname水球整理程式 <$userid.bbs\@$MYHOSTNAME>"}; - foreach( 0..3 ){ - if( (!$arg->{tartarget} && - $sender->MailMsg({to => $arg->{mailto}, - subject => $arg->{subject}, - msg => $arg->{body} - }) ) || - ($arg->{tartarget} && - $sender->MailFile({to => $arg->{mailto}, - subject => $arg->{subject}, - msg => $arg->{body}, - file => $arg->{tartarget}})) ){ - unlink $arg->{tartarget} if( $arg->{tartarget} ); - return 1; - } - } - $sender->MailMsg({to => "$userid.bbs\@$MYHOSTNAME", - subject => "無法寄出水球整理", - msg => - "親愛的使用者您好\n\n". - "你的水球整理記錄無法寄達指定位置 $mailto \n\n". - "$hostname站長群 敬上 ".POSIX::ctime(time())}); - unlink $arg->{tartarget} if( $arg->{tartarget} ); - return 1; + return $_[0] > $_[1] ? $_[0] : $_[1]; } + +main(); +1; |