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