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