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