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