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