]> git.donarmstrong.com Git - infobot.git/blob - src/Modules/News.pl
* Missed DebianBugs.pl in the previous merge
[infobot.git] / src / Modules / News.pl
1 #
2 # News.pl: Advanced news management
3 #   Author: dms
4 #  Version: v0.3 (20010412)
5 #  Created: 20010326
6 #    Notes: Testing done by greycat, kudos!
7 #
8 ### structure:
9 # news{ channel }{ string } { item }
10 # newsuser{ channel }{ user } = time()
11 ### where item is:
12 #       Time    - when it was added (used for sorting)
13 #       Author  - Who by.
14 #       Expire  - Time to expire.
15 #       Text    - Actual text.
16 ###
17
18 package News;
19
20 use strict;
21
22 use vars qw($who $chan);
23
24 sub Parse {
25     my ($what) = @_;
26     $chan = undef;
27     $who  = lc $::who;
28
29     if ( !keys %::news ) {
30         if ( !exists $::cache{newsFirst} ) {
31             &::DEBUG(
32 "news: looks like we enabled news option just then; loading up news file just in case."
33             );
34             $::cache{newsFirst} = 1;
35         }
36
37         &readNews();
38     }
39
40     if ( $::msgType ne 'private' ) {
41         $chan = $::chan;
42     }
43
44     if ( defined $what and $what =~ s/^($::mask{chan})\s*// ) {
45
46         # TODO: check if the channel exists aswell.
47         $chan = lc $1;
48
49         if ( !&::IsNickInChan( $who, $chan ) ) {
50             &::notice( $who, "sorry but you're not on $chan." );
51             return;
52         }
53     }
54
55     if ( !defined $chan ) {
56         my @chans = &::getNickInChans($who);
57
58         if ( scalar @chans > 1 ) {
59             &::notice( $who,
60 "error: I dunno which channel you are referring to since you're on more than one. Try 'news #chan ...' instead"
61             );
62             return;
63         }
64
65         if ( scalar @chans == 0 ) {
66             &::notice( $who,
67                 "error: I couldn't find you on any chan. This must be a bug!" );
68             return;
69         }
70
71         $chan = $chans[0];
72         &::VERB( "Guessed $who being on chan $chan", 2 );
73         $::chan = $chan;    # hack for IsChanConf().
74     }
75
76     if ( !defined $what or $what =~ /^\s*$/ ) {
77         &list();
78         return;
79     }
80
81     if ( $what =~ /^add(\s+(.*))?$/i ) {
82         &add($2);
83
84     }
85     elsif ( $what =~ /^del(\s+(.*))?$/i ) {
86         &del($2);
87
88     }
89     elsif ( $what =~ /^mod(\s+(.*))?$/i ) {
90         &mod($2);
91
92     }
93     elsif ( $what =~ /^set(\s+(.*))?$/i ) {
94         &set($2);
95
96     }
97     elsif ( $what =~ /^(\d+)$/i ) {
98         &::VERB( "News: read shortcut called.", 2 );
99         &read($1);
100
101     }
102     elsif ( $what =~ /^read(\s+(.*))?$/i ) {
103         &read($2);
104
105     }
106     elsif ( $what =~ /^(latest|new)(\s+(.*))?$/i ) {
107         &latest( $3 || $chan, 1 );
108
109         #       $::cmdstats{'News latest'}++;
110
111     }
112     elsif ( $what =~ /^stats?$/i ) {
113         &stats();
114
115     }
116     elsif ( $what =~ /^list$/i ) {
117         &list();
118
119     }
120     elsif ( $what =~ /^(expire|text|desc)(\s+(.*))?$/i ) {
121
122         # shortcut/link.
123         # nice hack.
124         my $cmd = $1;
125         my ( $arg1, $arg2 ) = split( /\s+/, $3, 2 );
126         &set("$arg1 $cmd $arg2");
127
128     }
129     elsif ( $what =~ /^help(\s+(.*))?$/i ) {
130         &::help("news $2");
131
132     }
133     elsif ( $what =~ /^newsflush$/i ) {
134         &::msg( $who, "newsflush called... check out the logs!" );
135         &::newsFlush();
136
137     }
138     elsif ( $what =~ /^(un)?notify$/i ) {
139         my $state = ($1) ? 0 : 1;
140
141         # TODO: don't notify even if 'News' is called.
142         if ( &::IsChanConf('newsNotifyAll') <= 0 ) {
143             &::DEBUG("news: chan => $chan, ::chan => $::chan.");
144             &::notice( $who,
145                 "not available for this channel or disabled altogether." );
146             return;
147         }
148
149         my $t = $::newsuser{$chan}{$who};
150         if ($state) {    # state = 1
151             if ( defined $t and ( $t == 0 or $t == -1 ) ) {
152                 &::notice( $who, "enabled notify." );
153                 delete $::newsuser{$chan}{$who};
154                 return;
155             }
156             &::notice( $who, "already enabled." );
157
158         }
159         else {           # state = 0
160             my $x = $::newsuser{$chan}{$who};
161             if ( defined $x and ( $x == 0 or $x == -1 ) ) {
162                 &::notice( $who, 'notify already disabled' );
163                 return;
164             }
165             $::newsuser{$chan}{$who} = -1;
166             &::notice( $who, "notify is now disabled." );
167         }
168
169     }
170     else {
171         &::notice( $who, "unknown command: $what" );
172     }
173 }
174
175 sub readNews {
176     my $file = "$::bot_base_dir/infobot-news.txt";
177     if ( !-f $file or -z $file ) {
178         return;
179     }
180
181     if ( fileno NEWS ) {
182         &::DEBUG("readNews: fileno exists, should never happen.");
183         return;
184     }
185
186     my ( $item, $chan );
187     my ( $ci, $cu ) = ( 0, 0 );
188
189     open( NEWS, $file );
190     while (<NEWS>) {
191         chop;
192
193         # TODO: allow commands.
194
195         if (/^[\s\t]+(\S+):[\s\t]+(.*)$/) {
196             if ( !defined $item ) {
197                 &::DEBUG("news: !defined item, never happen!");
198                 next;
199             }
200
201             $::news{$chan}{$item}{$1} = $2;
202             next;
203         }
204
205         # U <chan> <nick> <time>
206         if (/^U\s+(\S+)\s+(\S+)\s+(\d+)$/) {
207             $::newsuser{$1}{$2} = $3;
208             $cu++;
209             next;
210         }
211
212         if (/^(\S+)[\s\t]+(.*)$/) {
213             $chan = $1;
214             $item = $2;
215             $ci++;
216         }
217     }
218     close NEWS;
219
220     my $cn = scalar( keys %::news );
221     return unless ( $ci or $cn or $cu );
222
223     &::status(
224         "News: read " . $ci
225           . &::fixPlural( ' item', $ci ) . ' for '
226           . $cn
227           . &::fixPlural( ' chan', $cn ) . ', '
228           . $cu
229           . &::fixPlural( ' user', $cu ),
230         ' cache'
231     );
232 }
233
234 sub writeNews {
235     if ( !scalar keys %::news and !scalar keys %::newsuser ) {
236         &::VERB( "wN: nothing to write.", 2 );
237         return;
238     }
239
240     # should define this at the top of file.
241     my $file = "$::bot_base_dir/infobot-news.txt";
242
243     if ( fileno NEWS ) {
244         &::ERROR("News: write: fileno NEWS exists, should never happen.");
245         return;
246     }
247
248     # TODO: add commands to output file.
249     my $c = 0;
250     my ( $cc, $ci, $cu ) = ( 0, 0, 0 );
251
252     open( NEWS, ">$file" );
253     foreach $chan ( sort keys %::news ) {
254         $c = scalar keys %{ $::news{$chan} };
255         next unless ($c);
256         $cc++;
257         my $item;
258
259         foreach $item ( sort keys %{ $::news{$chan} } ) {
260             $c = scalar keys %{ $::news{$chan}{$item} };
261             next unless ($c);
262             $ci++;
263
264             print NEWS "$chan $item\n";
265             my $what;
266             foreach $what ( sort keys %{ $::news{$chan}{$item} } ) {
267                 print NEWS "    $what: $::news{$chan}{$item}{$what}\n";
268             }
269             print NEWS "\n";
270         }
271     }
272
273     # TODO: show how many users we wrote down.
274     if ( &::getChanConfList('newsKeepRead') ) {
275
276         # old users are removed in newsFlush(), perhaps it should be
277         # done here.
278
279         foreach $chan ( sort keys %::newsuser ) {
280
281             foreach ( sort keys %{ $::newsuser{$chan} } ) {
282                 print NEWS "U $chan $_ $::newsuser{$chan}{$_}\n";
283                 $cu++;
284             }
285         }
286     }
287
288     close NEWS;
289
290     &::status("News: Wrote $ci items for $cc chans, $cu user cache.");
291 }
292
293 sub add {
294     my ($str) = @_;
295
296     if ( !defined $chan or !defined $str or $str =~ /^\s*$/ ) {
297         &::help('news add');
298         return;
299     }
300
301     if ( length $str > 64 ) {
302         &::notice( $who, "That's not really an item (>64chars)" );
303         return;
304     }
305
306     if ( exists $::news{$chan}{$str}{Time} ) {
307         &::notice( $who, "'$str' for $chan already exists!" );
308         return;
309     }
310
311     $::news{$chan}{$str}{Time} = time();
312     my $expire = &::getChanConfDefault( 'newsDefaultExpire', 7, $chan );
313     $::news{$chan}{$str}{Expire} = time() + $expire * 60 * 60 * 24;
314     $::news{$chan}{$str}{Author} = $::who;                            # case!
315
316     my $agestr = &::Time2String( $::news{$chan}{$str}{Expire} - time() );
317     my $item   = &newsS2N($str);
318     &::notice( $who,
319             "Added '\037$str\037' at ["
320           . gmtime(time)
321           . "] by \002$::who\002 for item #\002$item\002." );
322     &::notice( $who, "Now do 'news text $item <your_description>'" );
323     &::notice( $who,
324             "This item will expire at \002"
325           . gmtime( $::news{$chan}{$str}{Expire} )
326           . "\002 [$agestr from now] " );
327
328     &writeNews();
329 }
330
331 sub del {
332     my ($what) = @_;
333     my $item = 0;
334
335     if ( !defined $what ) {
336         &::help('news del');
337         return;
338     }
339
340     if ( $what =~ /^\d+$/ ) {
341         my $count = scalar keys %{ $::news{$chan} };
342         if ( !$count ) {
343             &::notice( $who, "No news for $chan." );
344             return;
345         }
346
347         if ( $what > $count or $what < 0 ) {
348             &::notice( $who, "$what is out of range (max $count)" );
349             return;
350         }
351
352         $item = &getNewsItem($what);
353         $what = $item;                 # hack hack hack.
354
355     }
356     else {
357         $_ = &getNewsItem($what);       # hack hack hack.
358         $what = $_ if ( defined $_ );
359
360         if ( !exists $::news{$chan}{$what} ) {
361             my @found;
362             foreach ( keys %{ $::news{$chan} } ) {
363                 next unless (/\Q$what\E/);
364                 push( @found, $_ );
365             }
366
367             if ( !scalar @found ) {
368                 &::notice( $who, "could not find $what." );
369                 return;
370             }
371
372             if ( scalar @found > 1 ) {
373                 &::notice( $who, "too many matches for $what." );
374                 return;
375             }
376
377             $what = $found[0];
378             &::DEBUG("news: del: str: guessed what => $what");
379         }
380     }
381
382     if ( exists $::news{$chan}{$what} ) {
383         my $auth = 0;
384         $auth++ if ( $::who eq $::news{$chan}{$what}{Author} );
385         $auth++ if ( &::IsFlag('o') );
386
387         if ( !$auth ) {
388
389             # TODO: show when it'll expire.
390             &::notice( $who,
391 "Sorry, you cannot remove items; just let them expire on their own."
392             );
393             return;
394         }
395
396         &::notice( $who, "ok, deleted '$what' from \002$chan\002..." );
397         delete $::news{$chan}{$what};
398     }
399     else {
400         &::notice( $who, "error: not found $what in news for $chan." );
401     }
402 }
403
404 sub list {
405     if ( !scalar keys %{ $::news{$chan} } ) {
406         &::notice( $who, "No news for \002$chan\002." );
407         return;
408     }
409
410     if ( &::IsChanConf('newsKeepRead') > 0 ) {
411         my $x = $::newsuser{$chan}{$who};
412
413         if ( defined $x and ( $x == 0 or $x == -1 ) ) {
414             &::DEBUG("news: not updating time for $who.");
415         }
416         else {
417             if ( !scalar keys %{ $::news{$chan} } ) {
418                 &::DEBUG("news: should not add $chan/$who to cache!");
419             }
420
421             $::newsuser{$chan}{$who} = time();
422         }
423     }
424
425     # &notice() breaks OPN :( - using msg() instead!
426     my $count = scalar keys %{ $::news{$chan} };
427     &::msg( $who, "|==== News for \002$chan\002: ($count items)" );
428     my $newest = 0;
429     my $expire = 0;
430     my $eno    = 0;
431     foreach ( keys %{ $::news{$chan} } ) {
432         my $t = $::news{$chan}{$_}{Time};
433         my $e = $::news{$chan}{$_}{Expire};
434         $newest = $t if ( $t > $newest );
435         if ( $e > 1 and $e < $expire ) {
436             $expire = $e;
437             &::DEBUG("before newsS2N($_)");
438             $eno = &newsS2N($_);
439             &::DEBUG("after newsS2N($_) == $eno");
440         }
441     }
442     my $timestr = &::Time2String( time() - $newest );
443     &::msg( $who, "|= Last updated $timestr ago." );
444     &::msg( $who, " \037Num\037  \037Item " . ( ' ' x 40 ) . " \037" );
445
446     #    &::DEBUG("news: list: expire = $expire");
447     #    &::DEBUG("news: list: eno    = $eno");
448
449     my $i = 1;
450     foreach ( &getNewsAll() ) {
451         my $subtopic = $_;
452         my $setby    = $::news{$chan}{$subtopic}{Author};
453         my $chr      = ( exists $::News{$chan}{$subtopic}{Text} ) ? '' : '*';
454
455         if ( !defined $subtopic ) {
456             &::DEBUG("news: warn: subtopic == undef.");
457             next;
458         }
459
460         # TODO: show request stats aswell.
461         &::msg( $who,
462             sprintf( "\002[\002%2d\002]\002%s %s", $i, $chr, $subtopic ) );
463         $i++;
464     }
465
466     my $z = $::newsuser{$who};
467     if ( defined $z ) {
468         &::DEBUG("cache $who: $z");
469     }
470     else {
471         &::DEBUG("cache: $who doesn't have newscache set.");
472     }
473
474     &::msg( $who, "|= End of News." );
475     &::msg( $who, "use 'news read <#>' or 'news read <keyword>'" );
476 }
477
478 sub read {
479     my ($str) = @_;
480
481     if ( !defined $chan or !defined $str or $str =~ /^\s*$/ ) {
482         &::help('news read');
483         return;
484     }
485
486     if ( !scalar keys %{ $::news{$chan} } ) {
487         &::notice( $who, "No news for \002$chan\002." );
488         return;
489     }
490
491     my $item = &getNewsItem($str);
492     if ( !defined $item or !scalar keys %{ $::news{$chan}{$item} } ) {
493
494         # TODO: numerical check.
495         if (   $str =~ /^(\d+)[-, ](\d+)$/
496             or $str =~ /^-(\d+)$/
497             or $str =~ /^(\d+)-$/
498             or 0 )
499         {
500             &::notice( $who,
501                 "We don't support multiple requests of news items yet.  Sorry."
502             );
503             return;
504         }
505
506         &::notice( $who, "No news item called '$str'" );
507         return;
508     }
509
510     if ( !exists $::news{$chan}{$item}{Text} ) {
511         &::notice( $who, 'Someone forgot to add info to this news item' );
512         return;
513     }
514
515     my $t      = gmtime( $::news{$chan}{$item}{Time} );
516     my $a      = $::news{$chan}{$item}{Author};
517     my $text   = $::news{$chan}{$item}{Text};
518     my $num    = &newsS2N($item);
519     my $rwho   = $::news{$chan}{$item}{Request_By} || $::who;
520     my $rcount = $::news{$chan}{$item}{Request_Count} || 0;
521
522     if ( length $text < $::param{maxKeySize} ) {
523         &::VERB( "NEWS: Possible news->factoid redirection.", 2 );
524         my $f = &::getFactoid($text);
525
526         if ( defined $f ) {
527             &::VERB( "NEWS: ok, $text is factoid redirection.", 2 );
528             $f =~ s/^<REPLY>\s*//i;    # anything else?
529             $text = $f;
530         }
531     }
532
533     $_ = $::news{$chan}{$item}{'Expire'};
534     my $e;
535     if ($_) {
536         $e = sprintf(
537             "\037%s\037  [%s from now]",
538             scalar( gmtime($_) ),
539             &::Time2String( $_ - time() )
540         );
541     }
542
543     &::notice( $who, "+- News \002$chan\002 #$num: $item" );
544     &::notice( $who, "| Added by $a at \037$t\037" );
545     &::notice( $who, "| Expire: $e" ) if ( defined $e );
546     &::notice( $who, $text );
547     &::notice( $who,
548         "| Requested \002$rcount\002 times, last by \002$rwho\002" )
549       if ( $rcount and $rwho );
550
551     $::news{$chan}{$item}{'Request_By'}   = $::who;
552     $::news{$chan}{$item}{'Request_Time'} = time();
553     $::news{$chan}{$item}{'Request_Count'}++;
554 }
555
556 sub mod {
557     my ( $item, $str ) = split /\s+/, $_[0], 2;
558
559     if ( !defined $item or $item eq '' or $str =~ /^\s*$/ ) {
560         &::help('news mod');
561         return;
562     }
563
564     my $news = &getNewsItem($item);
565
566     if ( !defined $news ) {
567         &::DEBUG("news: error: mod: news == undefined.");
568         return;
569     }
570     my $nnews     = $::news{$chan}{$news}{Text};
571     my $mod_news  = $news;
572     my $mod_nnews = $nnews;
573
574     # SAR patch. mu++
575     if ( $str =~ m|^\s*s([/,#\|])(.+?)\1(.*?)\1([a-z]*);?\s*$| ) {
576         my ( $delim, $op, $np, $flags ) = ( $1, $2, $3, $4 );
577
578         if ( $flags !~ /^(g)?$/ ) {
579             &::notice( $who, "error: Invalid flags to regex." );
580             return;
581         }
582
583         ### TODO: use m### to make code safe!
584         # TODO: make code safer.
585         my $done = 0;
586
587         # TODO: use eval to deal with flags easily.
588         if ( $flags eq '' ) {
589             $done++ if ( !$done and $mod_news  =~ s/\Q$op\E/$np/ );
590             $done++ if ( !$done and $mod_nnews =~ s/\Q$op\E/$np/ );
591         }
592         elsif ( $flags eq 'g' ) {
593             $done++ if ( $mod_news  =~ s/\Q$op\E/$np/g );
594             $done++ if ( $mod_nnews =~ s/\Q$op\E/$np/g );
595         }
596
597         if ( !$done ) {
598             &::notice( $who, "warning: regex not found in news." );
599             return;
600         }
601
602         if ( $mod_news ne $news ) {    # news item.
603             if ( exists $::news{$chan}{$mod_news} ) {
604                 &::notice( $who, "item '$mod_news' already exists." );
605                 return;
606             }
607
608             &::notice( $who,
609                 "Moving item '$news' to '$mod_news' with SAR s/$op/$np/." );
610             foreach ( keys %{ $::news{$chan}{$news} } ) {
611                 $::news{$chan}{$mod_news}{$_} = $::news{$chan}{$news}{$_};
612                 delete $::news{$chan}{$news}{$_};
613             }
614
615             # needed?
616             delete $::news{$chan}{$news};
617         }
618
619         if ( $mod_nnews ne $nnews ) {    # news Text/Description.
620             &::notice( $who, "Changing text for '$news' SAR s/$op/$np/." );
621             if ( $mod_news ne $news ) {
622                 $::news{$chan}{$mod_news}{Text} = $mod_nnews;
623             }
624             else {
625                 $::news{$chan}{$news}{Text} = $mod_nnews;
626             }
627         }
628
629         return;
630     }
631     else {
632         &::notice( $who, "error: that regex failed ;(" );
633         return;
634     }
635
636     &::notice( $who, "error: Invalid regex. Try s/1/2/, s#3#4#..." );
637 }
638
639 sub set {
640     my ($args) = @_;
641     my ( $item, $what, $value );
642
643     if ( !defined $args ) {
644         &::DEBUG("news: set: args == NULL.");
645         return;
646     }
647
648     $item = $1 if ( $args =~ s/^(\S+)\s*// );
649     $what = $1 if ( $args =~ s/^(\S+)\s*// );
650     $value = $args;
651
652     if ( $item eq '' ) {
653         &::help('news set');
654         return;
655     }
656
657     my $news = &getNewsItem($item);
658
659     if ( !defined $news ) {
660         &::notice( $who,
661             "Could not find item '$item' substring or # in news list." );
662         return;
663     }
664
665     # list all values for chan.
666     if ( !defined $what or $what =~ /^\s*$/ ) {
667         &::msg( $who,
668             "set: you didn't fill me on the arguments! (what and values)" );
669         return;
670     }
671
672     my $ok = 0;
673     my @elements = ( 'Expire', 'Text' );
674     foreach (@elements) {
675         next unless ( $what =~ /^$_$/i );
676         $what = $_;
677         $ok++;
678         last;
679     }
680
681     if ( !$ok ) {
682         &::notice( $who, "Invalid set.  Try: @elements" );
683         return;
684     }
685
686     # show (read) what.
687     if ( !defined $value or $value =~ /^\s*$/ ) {
688         &::msg( $who, "set: you didn't fill me on the arguments! (value)" );
689         return;
690     }
691
692     if ( !exists $::news{$chan}{$news} ) {
693         &::notice( $who, "news '$news' does not exist" );
694         return;
695     }
696
697     if ( $what eq 'Expire' ) {
698
699         # TODO: use do_set().
700
701         my $time = 0;
702         my $plus = ( $value =~ s/^\+//g );
703         while ( $value =~ s/^(\d+)(\S*)\s*// ) {
704             my ( $int, $unit ) = ( $1, $2 );
705             $time += $int           if ( $unit =~ /^s(ecs?)?$/i );
706             $time += $int * 60      if ( $unit =~ /^m(in(utes?)?)?$/i );
707             $time += $int * 60 * 60 if ( $unit =~ /^h(ours?)?$/i );
708             $time += $int * 60 * 60 * 24
709               if ( !$unit or $unit =~ /^d(ays?)?$/i );
710             $time += $int * 60 * 60 * 24 * 7  if ( $unit =~ /^w(eeks?)?$/i );
711             $time += $int * 60 * 60 * 24 * 30 if ( $unit =~ /^mon(th)?$/i );
712         }
713
714         if ( $value =~ s/^never$//i ) {
715
716             # never.
717             $time = -1;
718         }
719         elsif ($plus) {
720
721             # from now.
722             $time += time();
723         }
724         else {
725
726             # from creation of item.
727             $time += $::news{$chan}{$news}{Time};
728         }
729
730         if ( !$time or ( $value and $value !~ /^never$/i ) ) {
731             &::DEBUG("news: set: Expire... need to parse.");
732             &::msg( $who, "hrm... couldn't parse that." );
733             return;
734         }
735
736         if ( $time == -1 ) {
737             &::notice( $who, "Set never expire for \002$item\002." );
738         }
739         elsif ( $time < -1 ) {
740             &::DEBUG("news: time should never be negative ($time).");
741             return;
742         }
743         else {
744             &::notice( $who,
745                     "Set expire for \002$item\002, to "
746                   . gmtime($time) . " ["
747                   . &::Time2String( $time - time() )
748                   . "]" );
749
750             if ( time() > $time ) {
751                 &::DEBUG("news: hrm... time() > $time, should expire.");
752             }
753         }
754
755         $::news{$chan}{$news}{Expire} = $time;
756
757         return;
758     }
759
760     my $auth = 0;
761
762     #    &::DEBUG("news: who => '$who'");
763     my $author = $::news{$chan}{$news}{Author};
764     $auth++ if ( $::who eq $author );
765     $auth++ if ( &::IsFlag('o') );
766     if ( !defined $author ) {
767         &::DEBUG(
768             "news: news{$chan}{$news}{Author} is not defined! auth'd anyway");
769         $::news{$chan}{$news}{Author} = $::who;
770         $author = $::who;
771         $auth++;
772     }
773
774     if ( !$auth ) {
775
776         # TODO: show when it'll expire.
777         &::notice( $who,
778             "Sorry, you cannot set items. (author $author owns it)" );
779         return;
780     }
781
782     # TODO: clean this up.
783     my $old = $::news{$chan}{$news}{$what};
784     if ( defined $old ) {
785         &::DEBUG("news: old => $old.");
786     }
787     $::news{$chan}{$news}{$what} = $value;
788     &::notice( $who, "Setting [$chan]/{$news}/<$what> to '$value'." );
789 }
790
791 sub latest {
792     my ( $tchan, $flag ) = @_;
793
794     # hack hack hack.  fix later.
795     $chan = $tchan;
796     $who  = $::who;
797
798     # TODO: if chan = undefined, guess.
799     #    if (!exists $::news{$chan}) {
800     if ( !exists $::channels{$chan} ) {
801         &::notice( $who, "invalid chan $chan" ) if ($flag);
802         return;
803     }
804
805     my $t = $::newsuser{$chan}{$who};
806
807     #    if (defined $t) {
808     #   &::DEBUG("newsuser: $chan/$who == $t");
809     #    } else {
810     #   &::DEBUG("newsuser: $chan/$who == undefined");
811     #    }
812
813     if ( defined $t and ( $t == 0 or $t == -1 ) ) {
814         if ($flag) {
815             &::notice( $who,
816 "if you want to read news, try \002/msg $::ident news $chan\002 or \002/msg $::ident news $chan notify\002"
817             );
818         }
819         else {
820             &::DEBUG("news: not displaying any new news for $who");
821             return;
822         }
823     }
824
825     $::chan = $chan;
826     return if ( &::IsChanConf('newsNotifyAll') <= 0 );
827
828     # I don't understand this code ;)
829     $t = 1 if ( !defined $t );
830
831     if ( !defined $t ) {
832
833         #       &::msg($who, "News is disabled for $chan");
834         &::DEBUG("news: $chan: something went really wrong.");
835         return;
836     }
837
838     my @new;
839     foreach ( keys %{ $::news{$chan} } ) {
840         next if ( !defined $t );
841         next if ( $t > $::news{$chan}{$_}{Time} );
842
843         # don't list new items if they don't have Text.
844         if ( !exists $::news{$chan}{$_}{Text} ) {
845             if ( time() - $::news{$chan}{$_}{Time} > 60 * 60 * 24 * 3 ) {
846                 &::DEBUG(
847 "deleting news{$chan}{$_} because it was too old and had no text info."
848                 );
849                 delete $::news{$chan}{$_};
850             }
851
852             next;
853         }
854
855         push( @new, $_ );
856     }
857
858     # !scalar @new, $flag
859     if ( !scalar @new and $flag ) {
860         &::notice( $who, "no new news for $chan for $who." );
861
862         # valid to set this?
863         $::newsuser{$chan}{$who} = time();
864         return;
865     }
866
867     # scalar @new, !$flag
868     my $unread = scalar @new;
869     my $total  = scalar keys %{ $::news{$chan} };
870     if ( !$flag && &::IsChanConf('newsTellUnread') <= 0 ) {
871         return;
872     }
873
874     if ( !$flag ) {
875         return unless ($unread);
876
877         # just a temporary measure not to flood ourself off the
878         # network with news until we get global notice() and msg()
879         # throttling.
880         if ( time() - ( $::cache{newsTime} || 0 ) < 5 ) {
881             &::status("news: not displaying latest notice to $who/$chan.");
882             return;
883         }
884
885         $::cache{newsTime} = time();
886         my $reply =
887 "There are unread news in $chan ($unread unread, $total total). /msg $::ident news $::chan latest";
888         $reply .=
889 "  If you don't want further news notification, /msg $::ident news unnotify"
890           if ( $unread == $total );
891         &::notice( $who, $reply );
892
893         return;
894     }
895
896     # scalar @new, $flag
897     if ( scalar @new ) {
898         &::notice( $who,
899             "+==== New news for \002$chan\002 ($unread new; $total total):" );
900
901         my $t = $::newsuser{$chan}{$who};
902         if ( defined $t and $t > 1 ) {
903             my $timestr = &::Time2String( time() - $t );
904             &::notice( $who, "|= Last time read $timestr ago" );
905         }
906
907         my $i;
908         my @sorted;
909         foreach (@new) {
910             $i = &newsS2N($_);
911             $sorted[$i] = $_;
912         }
913
914         for ( $i = 0 ; $i <= scalar(@sorted) ; $i++ ) {
915             my $news = $sorted[$i];
916             next unless ( defined $news );
917
918             #       my $age = time() - $::news{$chan}{$news}{Time};
919             my $msg = sprintf( "\002[\002%2d\002]\002 %s", $i, $news );
920 ###                     $i, $_, &::Time2String($age)
921             $::conn->schedule(
922                 int( ( 2 + $i ) / 2 ),
923                 sub {
924                     &::notice( $who, $msg );
925                 }
926             );
927         }
928
929         # TODO: implement throttling via schedule into &notice() / &msg().
930         $::conn->schedule(
931             int( ( 2 + $i ) / 2 ),
932             sub {
933                 &::notice( $who,
934 "|= to read, do \002news $chan read <#>\002 or \002news $chan read <keyword>\002"
935                 );
936             }
937         );
938
939         # lame hack to prevent dupes if we just ignore it.
940         my $x = $::newsuser{$chan}{$who};
941         if ( defined $x and ( $x == 0 or $x == -1 ) ) {
942             &::DEBUG("news: not updating time for $who. (2)");
943         }
944         else {
945             $::newsuser{$chan}{$who} = time();
946         }
947     }
948 }
949
950 ###
951 ### helpers...
952 ###
953
954 sub getNewsAll {
955     my %time;
956     foreach ( keys %{ $::news{$chan} } ) {
957         $time{ $::news{$chan}{$_}{Time} } = $_;
958     }
959
960     my @items;
961     foreach ( sort { $a <=> $b } keys %time ) {
962         push( @items, $time{$_} );
963     }
964
965     return @items;
966 }
967
968 sub newsS2N {
969     my ($what) = @_;
970     my $item = 0;
971     my @items;
972     my $no;
973
974     my %time;
975     foreach ( keys %{ $::news{$chan} } ) {
976         my $t = $::news{$chan}{$_}{Time};
977
978         if ( !defined $t or $t !~ /^\d+$/ ) {
979             &::DEBUG(
980 "news: warn: t is undefined for news{$chan}{$_}{Time}; removing item."
981             );
982             delete $::news{$chan}{$_};
983             next;
984         }
985
986         $time{$t} = $_;
987     }
988
989     foreach ( sort { $a <=> $b } keys %time ) {
990         $item++;
991         return $item if ( $time{$_} eq $what );
992     }
993
994     &::DEBUG("newsS2N($what): failed...");
995 }
996
997 sub getNewsItem {
998     my ($what) = @_;
999     my $item = 0;
1000
1001     $what =~ s/^\#//;    # '#1' for example.
1002
1003     my %time;
1004     foreach ( keys %{ $::news{$chan} } ) {
1005         my $t = $::news{$chan}{$_}{Time};
1006
1007         if ( !defined $t or $t !~ /^\d+$/ ) {
1008             &::DEBUG(
1009 "news: warn: t is undefined for news{$chan}{$_}{Time}; removing item."
1010             );
1011             delete $::news{$chan}{$_};
1012             next;
1013         }
1014
1015         $time{$t} = $_;
1016     }
1017
1018     # number to string resolution.
1019     if ( $what =~ /^\d+$/ ) {
1020         foreach ( sort { $a <=> $b } keys %time ) {
1021             $item++;
1022             return $time{$_} if ( $item == $what );
1023         }
1024
1025     }
1026     else {
1027
1028         # partial string to full string resolution
1029         # in some cases, string->number resolution.
1030
1031         my @items;
1032         my $no;
1033         foreach ( sort { $a <=> $b } keys %time ) {
1034             $item++;
1035
1036             #       $no = $item if ($time{$_} eq $what);
1037 ##          if ($time{$_} eq $what) {
1038 ##              $no = $item;
1039 ##              next;
1040 ##          }
1041
1042             push( @items, $time{$_} ) if ( $time{$_} =~ /\Q$what\E/i );
1043         }
1044
1045 ##      if (defined $no and !@items) {
1046 ##          &::DEBUG("news: string->number resolution: $what->$no.");
1047 ##          return $no;
1048 ##      }
1049
1050         if ( scalar @items > 1 ) {
1051             &::DEBUG("news: Multiple matches, not guessing.");
1052             &::notice( $who, "Multiple matches, not guessing." );
1053             return;
1054         }
1055
1056         if (@items) {
1057
1058         #           &::DEBUG("news: gNI: part_string->full_string: $what->$items[0]");
1059             return $items[0];
1060         }
1061         else {
1062             &::DEBUG("news: gNI: No match for '$what'");
1063             return;
1064         }
1065     }
1066
1067     &::ERROR("news: gNI: should not happen (what = $what)");
1068     return;
1069 }
1070
1071 sub do_set {
1072     my ( $what, $value ) = @_;
1073
1074     if ( !defined $chan ) {
1075         &::DEBUG("news: do_set: chan not defined.");
1076         return;
1077     }
1078
1079     if ( !defined $what or $what =~ /^\s*$/ ) {
1080         &::DEBUG("news: what $what is not defined.");
1081         return;
1082     }
1083
1084     if ( !defined $value or $value =~ /^\s*$/ ) {
1085         &::DEBUG("news: value $value is not defined.");
1086         return;
1087     }
1088
1089     &::TODO("news: do_set:");
1090 }
1091
1092 sub stats {
1093     &::DEBUG("News: stats called.");
1094     &::msg( $who, "check my logs/console." );
1095     my ( $i, $j ) = ( 0, 0 );
1096
1097     # total request count.
1098     foreach $chan ( keys %::news ) {
1099         foreach ( keys %{ $::news{$chan} } ) {
1100             $i += $::news{$chan}{$_}{Request_Count};
1101         }
1102     }
1103     &::DEBUG("news: stats: total request count => $i");
1104     $i = 0;
1105
1106     # total user cached.
1107     foreach $chan ( keys %::newsuser ) {
1108         $i += $::newsuser{$chan}{$_};
1109     }
1110     &::DEBUG("news: stats: total user cache => $i");
1111     $i = 0;
1112
1113     # average latest time read.
1114     my $t = time();
1115     foreach $chan ( keys %::newsuser ) {
1116         $i += $t - $::newsuser{$chan}{$_};
1117         &::DEBUG(" i = $i");
1118         $j++;
1119     }
1120     &::DEBUG("news: stats: average latest time read: total time: $i");
1121     &::DEBUG("news: ... count: $j");
1122     &::DEBUG( "news:   average: "
1123           . sprintf( "%.02f", $i / ( $j || 1 ) )
1124           . " sec/user" );
1125     $i = $j = 0;
1126 }
1127
1128 sub AUTOLOAD { &::AUTOLOAD(@_); }
1129
1130 1;
1131
1132 # vim:ts=4:sw=4:expandtab:tw=80