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