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