]> git.donarmstrong.com Git - infobot.git/blob - src/Modules/News.pl
- userAdd, if no mask is given, don't add it.
[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;
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     foreach (keys %{ $::news{$chan} }) {
385         my $t   = $::news{$chan}{$_}{Time};
386         $newest = $t if ($t > $newest);
387     }
388     my $timestr = &::Time2String(time() - $newest);
389     &::msg($::who, "|= Last updated $timestr ago.");
390     &::msg($::who, " \037Num\037  \037Item ".(" "x40)." \037");
391
392     my $i = 1;
393     foreach ( &getNewsAll() ) {
394         my $subtopic    = $_;
395         my $setby       = $::news{$chan}{$subtopic}{Author};
396         my $chr         = (exists $::News{$chan}{$subtopic}{Text}) ? "" : "*";
397
398         if (!defined $subtopic) {
399             &::DEBUG("news: warn: subtopic == undef.");
400             next;
401         }
402
403         # todo: show request stats aswell.
404         &::msg($::who, sprintf("\002[\002%2d\002]\002%s %s",
405                                 $i, $chr, $subtopic));
406         $i++;
407     }
408
409     my $z = $::newsuser{$::who};
410     if (defined $z) {
411         &::DEBUG("cache $::who: $z");
412     } else {
413         &::DEBUG("cache: $::who doesn't have newscache set.");
414     }
415
416     &::msg($::who, "|= End of News.");
417     &::msg($::who, "use 'news read <#>' or 'news read <keyword>'");
418 }
419
420 sub read {
421     my($str) = @_;
422
423     if (!defined $chan or !defined $str or $str =~ /^\s*$/) {
424         &::help("news read");
425         return;
426     }
427
428     if (!scalar keys %{ $::news{$chan} }) {
429         &::notice($::who, "No News for \002$chan\002.");
430         return;
431     }
432
433     my $item    = &getNewsItem($str);
434     if (!defined $item or !scalar keys %{ $::news{$chan}{$item} }) {
435         &::notice($::who, "No news item called '$str'");
436         return;
437     }
438
439     if (!exists $::news{$chan}{$item}{Text}) {
440         &::notice($::who, "Someone forgot to add info to this news item");
441         return;
442     }
443
444     my $t       = localtime( $::news{$chan}{$item}{Time} );
445     my $a       = $::news{$chan}{$item}{Author};
446     my $text    = $::news{$chan}{$item}{Text};
447     my $num     = &newsS2N($item);
448     my $rwho    = $::news{$chan}{$item}{Request_By} || $::who;
449     my $rcount  = $::news{$chan}{$item}{Request_Count} || 0;
450
451     if (length $text < $::param{maxKeySize}) {
452         &::VERB("NEWS: Possible news->factoid redirection.",2);
453         my $f   = &::getFactoid($text);
454
455         if (defined $f) {
456             &::VERB("NEWS: ok, $text is factoid redirection.",2);
457             $f =~ s/^<REPLY>\s*//i;     # anything else?
458             $text = $f;
459         }
460     }
461
462     $_ = $::news{$chan}{$item}{'Expire'};
463     my $e;
464     if ($_) {
465         $e = sprintf("\037%s\037  [%s from now]",
466                 scalar(localtime($_)),
467                 &::Time2String($_ - time())
468         );
469     }
470
471     &::notice($::who, "+- News \002$chan\002 #$num: $item");
472     &::notice($::who, "| Added by $a at \037$t\037");
473     &::notice($::who, "| Expire: $e") if (defined $e);
474     &::notice($::who, $text);
475     &::notice($::who, "| Requested \002$rcount\002 times, last by \002$rwho\002") if ($rcount and $rwho);
476
477     $::news{$chan}{$item}{'Request_By'}   = $::who;
478     $::news{$chan}{$item}{'Request_Time'} = time();
479     $::news{$chan}{$item}{'Request_Count'}++;
480 }
481
482 sub mod {
483     my($item, $str) = split /\s+/, $_[0], 2;
484
485     if (!defined $item or $item eq "" or $str =~ /^\s*$/) {
486         &::help("news mod");
487         return;
488     }
489
490     my $news = &getNewsItem($item);
491
492     if (!defined $news) {
493         &::DEBUG("news: error: mod: news == undefined.");
494         return;
495     }
496     my $nnews = $::news{$chan}{$news}{Text};
497     my $mod_news  = $news;
498     my $mod_nnews = $nnews;
499
500     # SAR patch. mu++
501     if ($str =~ m|^\s*s([/,#\|])(.+?)\1(.*?)\1([a-z]*);?\s*$|) {
502         my ($delim, $op, $np, $flags) = ($1,$2,$3,$4);
503
504         if ($flags !~ /^(g)?$/) {
505             &::notice($::who, "error: Invalid flags to regex.");
506             return;
507         }
508
509         ### TODO: use m### to make code safe!
510         # todo: make code safer.
511         my $done = 0;
512         # todo: use eval to deal with flags easily.
513         if ($flags eq "") {
514             $done++ if (!$done and $mod_news  =~ s/\Q$op\E/$np/);
515             $done++ if (!$done and $mod_nnews =~ s/\Q$op\E/$np/);
516         } elsif ($flags eq "g") {
517             $done++ if ($mod_news  =~ s/\Q$op\E/$np/g);
518             $done++ if ($mod_nnews =~ s/\Q$op\E/$np/g);
519         }
520
521         if (!$done) {
522             &::notice($::who, "warning: regex not found in news.");
523             return;
524         }
525
526         if ($mod_news ne $news) { # news item.
527             if (exists $::news{$chan}{$mod_news}) {
528                 &::notice($::who, "item '$mod_news' already exists.");
529                 return;
530             }
531
532             &::notice($::who, "Moving item '$news' to '$mod_news' with SAR s/$op/$np/.");
533             foreach (keys %{ $::news{$chan}{$news} }) {
534                 $::news{$chan}{$mod_news}{$_} = $::news{$chan}{$news}{$_};
535                 delete $::news{$chan}{$news}{$_};
536             }
537             # needed?
538             delete $::news{$chan}{$news};
539         }
540
541         if ($mod_nnews ne $nnews) { # news Text/Description.
542             &::notice($::who, "Changing text for '$news' SAR s/$op/$np/.");
543             if ($mod_news ne $news) {
544                 $::news{$chan}{$mod_news}{Text} = $mod_nnews;
545             } else {
546                 $::news{$chan}{$news}{Text}     = $mod_nnews;
547             }
548         }
549
550         return;
551     } else {
552         &::notice($::who, "error: that regex failed ;(");
553         return;
554     }
555
556     &::notice($::who, "error: Invalid regex. Try s/1/2/, s#3#4#...");
557 }
558
559 sub set {
560     my($args) = @_;
561     my($item, $what, $value);
562
563     $item = $1 if ($args =~ s/^(\S+)\s*//);
564     $what = $1 if ($args =~ s/^(\S+)\s*//);
565     $value = $args;
566
567     &::DEBUG("news: set called.");
568
569     if ($item eq "") {
570         &::help("news set");
571         return;
572     }
573
574     my $news = &getNewsItem($item);
575
576     if (!defined $news) {
577         &::notice($::who, "Could not find item '$item' substring or # in news list.");
578         return;
579     }
580
581     # list all values for chan.
582     if (!defined $what or $what =~ /^\s*$/) {
583         &::msg($::who, "set: you didn't fill me on the arguments! (what and values)");
584         return;
585     }
586
587     my $ok = 0;
588     my @elements = ("Expire","Text");
589     foreach (@elements) {
590         next unless ($what =~ /^$_$/i);
591         $what = $_;
592         $ok++;
593         last;
594     }
595
596     if (!$ok) {
597         &::notice($::who, "Invalid set.  Try: @elements");
598         return;
599     }
600
601     # show (read) what.
602     if (!defined $value or $value =~ /^\s*$/) {
603         &::msg($::who, "set: you didn't fill me on the arguments! (value)");
604         return;
605     }
606
607     if (!exists $::news{$chan}{$news}) {
608         &::notice($::who, "news '$news' does not exist");
609         return;
610     }
611
612     if ($what eq "Expire") {
613         # todo: use do_set().
614
615         my $time = 0;
616         my $plus = ($value =~ s/^\+//g);
617         while ($value =~ s/^(\d+)(\S*)\s*//) {
618             my($int,$unit) = ($1,$2);
619             $time += $int       if ($unit =~ /^s(ecs?)?$/i);
620             $time += $int*60    if ($unit =~ /^m(in(utes?)?)?$/i);
621             $time += $int*60*60 if ($unit =~ /^h(ours?)?$/i);
622             $time += $int*60*60*24 if (!$unit or $unit =~ /^d(ays?)?$/i);
623             $time += $int*60*60*24*7 if ($unit =~ /^w(eeks?)?$/i);
624             $time += $int*60*60*24*30 if ($unit =~ /^mon(th)?$/i);
625         }
626
627         if ($value =~ s/^never$//i) {
628             # never.
629             $time = -1;
630         } elsif ($plus) {
631             # from now.
632             $time += time();
633         } else {
634             # from creation of item.
635             $time += $::news{$chan}{$news}{Time};
636         }
637
638         if (!$time or ($value and $value !~ /^never$/i)) {
639             &::DEBUG("news: set: Expire... need to parse.");
640             &::msg($::who, "hrm... couldn't parse that.");
641             return;
642         }
643
644         if ($time == -1) {
645             &::notice($::who, "Set never expire for \002$item\002." );
646         } elsif ($time < -1) {
647             &::DEBUG("news: time should never be negative ($time).");
648             return;
649         } else {
650             &::notice($::who, "Set expire for \002$item\002, to ".
651                 localtime($time) ." [".&::Time2String($time - time())."]" );
652
653             if (time() > $time) {
654                 &::DEBUG("news: hrm... time() > $time, should expire.");
655             }
656         }
657
658
659         $::news{$chan}{$news}{Expire} = $time;
660
661         return;
662     }
663
664     my $auth = 0;
665     &::DEBUG("news: who => '$::who'");
666     my $author = $::news{$chan}{$news}{Author};
667     $auth++ if ($::who eq $author);
668     $auth++ if (&::IsFlag("o"));
669     if (!defined $author) {
670         &::DEBUG("news: news{$chan}{$news}{Author} is not defined! auth'd anyway");
671         $::news{$chan}{$news}{Author} = $::who;
672         $author = $::who;
673         $auth++;
674     }
675
676     if (!$auth) {
677         # todo: show when it'll expire.
678         &::notice($::who, "Sorry, you cannot set items. (author $author owns it)");
679         return;
680     }
681
682     # todo: clean this up.
683     my $old = $::news{$chan}{$news}{$what};
684     if (defined $old) {
685         &::DEBUG("news: old => $old.");
686     }
687     $::news{$chan}{$news}{$what} = $value;
688     &::notice($::who, "Setting [$chan]/{$news}/<$what> to '$value'.");
689 }
690
691 sub latest {
692     my($tchan, $flag) = @_;
693
694     # hack hack hack.
695     $chan       ||= $tchan;
696     $who        = $::who;
697
698     # todo: if chan = undefined, guess.
699 #    if (!exists $::news{$chan}) {
700     if (!exists $::channels{$chan}) {
701         &::notice($who, "invalid chan $chan") if ($flag);
702         return;
703     }
704
705     my $t = $::newsuser{$chan}{$who};
706     if (defined $t) {
707         &::DEBUG("newsuser: $chan/$who == $t");
708     } else {
709         &::DEBUG("newsuser: $chan/$who == undefined");
710     }
711
712     if (defined $t and ($t == 0 or $t == -1)) {
713         if ($flag) {
714             &::notice($who, "if you want to read news, try /msg $::ident news or /msg $::ident news notify");
715         } else {
716             &::DEBUG("news: not displaying any new news for $who");
717             return;
718         }
719     }
720
721     $::chan     = $chan;
722     my $x = &::IsChanConf("newsNotifyAll");
723     if (&::IsChanConf("newsNotifyAll") and !defined $t) {
724         $t = 1;
725     }
726
727     if (!defined $t) {
728         &::DEBUG("news: something went really wrong.");
729         &::DEBUG("news: chan => $chan, ::chan => $::chan");
730 #       &::notice($who, "something went really wrong.");
731         return;
732     }
733
734     my @new;
735     foreach (keys %{ $::news{$chan} }) {
736         next if (!defined $t);
737         next if ($t > $::news{$chan}{$_}{Time});
738
739         # don't list new items if they don't have Text.
740         if (!exists $::news{$chan}{$_}{Text}) {
741             if (time() - $::news{$chan}{$_}{Time} > 60*60*24*3) {
742                 &::DEBUG("deleting news{$chan}{$_} because it was too old and had no text info.");
743                 delete $::news{$chan}{$_};
744             }
745
746             next;
747         }
748
749         push(@new, $_);
750     }
751
752     # !scalar @new, $flag
753     if (!scalar @new and $flag) {
754         &::notice($who, "no new news for $chan for $who.");
755         # valid to set this?
756         $::newsuser{$chan}{$who} = time();
757         return;
758     }
759
760     # scalar @new, !$flag
761     my $unread  = scalar @new;
762     my $total   = scalar keys %{ $::news{$chan} };
763     if (!$flag) {
764         return unless ($unread);
765
766         my $reply = "There are unread news in $chan ($unread unread, $total total). /msg $::ident news $::chan latest";
767         $reply   .= "  If you don't want further news notification, /msg $::ident news unnotify" if ($unread == $total);
768         &::notice($who, $reply);
769
770         return;
771     }
772
773     # scalar @new, $flag
774     if (scalar @new) {
775         &::notice($who, "+==== New news for \002$chan\002 ($unread new; $total total):");
776
777         my $t = $::newsuser{$chan}{$who};
778         if (defined $t and $t > 1) {
779             my $timestr = &::Time2String( time() - $t );
780             &::notice($who, "|= Last time read $timestr ago");
781         }
782
783         my @sorted;
784         foreach (@new) {
785             my $i   = &newsS2N($_);
786             $sorted[$i] = $_;
787         }
788
789         for (my $i=0; $i<=scalar(@sorted); $i++) {
790             my $news = $sorted[$i];
791             next unless (defined $news);
792
793             my $age = time() - $::news{$chan}{$news}{Time};
794             &::notice($who, sprintf("\002[\002%2d\002]\002 %s",
795                 $i, $news) );
796 #               $i, $_, &::Time2String($age) ) );
797         }
798
799         &::notice($who, "|= to read, do 'news read <#>' or 'news read <keyword>'");
800
801         # lame hack to prevent dupes if we just ignore it.
802         my $x = $::newsuser{$chan}{$who};
803         if (defined $x and ($x == 0 or $x == -1)) {
804             &::DEBUG("news: not updating time for $who. (2)");
805         } else {
806             $::newsuser{$chan}{$who} = time();
807         }
808     }
809 }
810
811 ###
812 ### helpers...
813 ###
814
815 sub getNewsAll {
816     my %time;
817     foreach (keys %{ $::news{$chan} }) {
818         $time{ $::news{$chan}{$_}{Time} } = $_;
819     }
820
821     my @items;
822     foreach (sort { $a <=> $b } keys %time) {
823         push(@items, $time{$_});
824     }
825
826     return @items;
827 }
828
829 sub newsS2N {
830     my($what)   = @_;
831     my $item    = 0;
832     my @items;
833     my $no;
834
835     my %time;
836     foreach (keys %{ $::news{$chan} }) {
837         my $t = $::news{$chan}{$_}{Time};
838
839         if (!defined $t or $t !~ /^\d+$/) {
840             &::DEBUG("news: warn: t is undefined for news{$chan}{$_}{Time}; removing item.");
841             delete $::news{$chan}{$_};
842             next;
843         }
844
845         $time{$t} = $_;
846     }
847
848     foreach (sort { $a <=> $b } keys %time) {
849         $item++;
850         return $item if ($time{$_} eq $what);
851     }
852
853     &::DEBUG("newsS2N($what): failed...");
854 }
855
856 sub getNewsItem {
857     my($what)   = @_;
858     my $item    = 0;
859
860     my %time;
861     foreach (keys %{ $::news{$chan} }) {
862         my $t = $::news{$chan}{$_}{Time};
863
864         if (!defined $t or $t !~ /^\d+$/) {
865             &::DEBUG("news: warn: t is undefined for news{$chan}{$_}{Time}; removing item.");
866             delete $::news{$chan}{$_};
867             next;
868         }
869
870         $time{$t} = $_;
871     }
872
873     # number to string resolution.
874     if ($what =~ /^\d+$/) {
875         foreach (sort { $a <=> $b } keys %time) {
876             $item++;
877             return $time{$_} if ($item == $what);
878         }
879
880     } else {
881         # partial string to full string resolution
882         # in some cases, string->number resolution.
883
884         my @items;
885         my $no;
886         foreach (sort { $a <=> $b } keys %time) {
887             $item++;
888 #           $no = $item if ($time{$_} eq $what);
889 ##          if ($time{$_} eq $what) {
890 ##              $no = $item;
891 ##              next;
892 ##          }
893
894             push(@items, $time{$_}) if ($time{$_} =~ /\Q$what\E/i);
895         }
896
897 ##      if (defined $no and !@items) {
898 ##          &::DEBUG("news: string->number resolution: $what->$no.");
899 ##          return $no;
900 ##      }
901
902         if (scalar @items > 1) {
903             &::DEBUG("news: Multiple matches, not guessing.");
904             &::notice($::who, "Multiple matches, not guessing.");
905             return;
906         }
907
908         if (@items) {
909             &::DEBUG("news: gNI: part_string->full_string: $what->$items[0]");
910             return $items[0];
911         } else {
912             &::DEBUG("news: gNI: No match for '$what'");
913             return;
914         }
915     }
916
917     &::ERROR("getNewsItem: Should not happen (what = $what)");
918     return;
919 }
920
921 sub do_set {
922     my($what,$value) = @_;
923
924     if (!defined $chan) {
925         &::DEBUG("news: do_set: chan not defined.");
926         return;
927     }
928
929     if (!defined $what or $what =~ /^\s*$/) {
930         &::DEBUG("news: what $what is not defined.");
931         return;
932     }
933
934     if (!defined $value or $value =~ /^\s*$/) {
935         &::DEBUG("news: value $value is not defined.");
936         return;
937     }
938
939     &::DEBUG("news: do_set: TODO...");
940 }
941
942 sub stats {
943     &::DEBUG("News: stats called.");
944     &::msg($::who, "check my logs/console.");
945     my($i,$j) = (0,0);
946
947     # total request count.
948     foreach $chan (keys %::news) {
949         foreach (keys %{ $::news{$chan} }) {
950             $i += $::news{$chan}{$_}{Request_Count};
951         }
952     }
953     &::DEBUG("news: stats: total request count => $i");
954     $i = 0;
955
956     # total user cached.
957     foreach $chan (keys %::newsuser) {
958         $i += $::newsuser{$chan}{$_};
959     }
960     &::DEBUG("news: stats: total user cache => $i");
961     $i = 0;
962
963     # average latest time read.
964     my $t = time();
965     foreach $chan (keys %::newsuser) {
966         $i += $t - $::newsuser{$chan}{$_};
967         &::DEBUG(" i = $i");
968         $j++;
969     }
970     &::DEBUG("news: stats: average latest time read: total time: $i");
971     &::DEBUG("news: ... count: $j");
972     &::DEBUG("news:   average: ".sprintf("%.02f", $i/($j||1))." sec/user");
973     $i = $j = 0;
974 }
975
976 1;