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