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