]> git.donarmstrong.com Git - infobot.git/blob - src/Modules/News.pl
A round of fixes:
[infobot.git] / src / Modules / News.pl
1 #
2 # News.pl: Advanced news management
3 #   Author: dms
4 #  Version: v0.2 (20010326)
5 #  Created: 20010326
6 #    Notes: Testing done by greycat, kudos!
7 #
8 ### structure:
9 # news{ channel }{ string } { items }
10 # newsuser{ channel }{ user } = time()
11 ### where items 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 eq "private") {
34     } else {
35         $chan = $::chan;
36     }
37
38     if (defined $what and $what =~ s/^($::mask{chan})\s*//) {
39         # todo: check if the channel exists aswell.
40         $chan = $1;
41     }
42
43     if (!defined $chan) {
44         my @chans = &::GetNickInChans($::who);
45
46         if (scalar @chans > 1) {
47             &::msg($::who, "error: I dunno which channel you are referring to since you're on more than one.");
48             return;
49         }
50
51         if (scalar @chans == 0) {
52             &::msg($::who, "error: I couldn't find you on any chan. This must be a bug!");
53             return;
54         }
55
56         $chan = $chans[0];
57         &::DEBUG("Guessed $::who being on chan $chan");
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     } elsif ($what =~ /^del(\s+(.*))?$/i) {
68         &del($2);
69     } elsif ($what =~ /^mod(\s+(.*))?$/i) {
70         &mod($2);
71     } elsif ($what =~ /^set(\s+(.*))?$/i) {
72         &set($2);
73     } elsif ($what =~ /^(\d)$/i) {
74         &::DEBUG("read shortcut called.");
75         &read($1);
76     } elsif ($what =~ /^read(\s+(.*))?$/i) {
77         &read($2);
78     } elsif ($what =~ /^(latest|new)(\s+(.*))?$/i) {
79         &::DEBUG("latest called... hrm");
80         &latest($3 || $chan, 1);
81     } elsif ($what =~ /^list$/i) {
82         &::DEBUG("list longcut called.");
83         &list();
84     } elsif ($what =~ /^(expire|text|desc)(\s+(.*))?$/i) {
85         # shortcut/link.
86         # nice hack.
87         my($arg1,$arg2) = split(/\s+/, $3, 2);
88         &set("$arg1 $1 $arg2");
89     } elsif ($what =~ /^help(\s+(.*))?$/i) {
90         &::help("news$1");
91     } else {
92         &::DEBUG("could not parse '$what'.");
93         &::msg($::who, "unknown command: $what");
94     }
95 }
96
97 sub readNews {
98     my $file = "$::bot_base_dir/blootbot-news.txt";
99     if (! -f $file) {
100         return;
101     }
102
103     if (fileno NEWS) {
104         &::DEBUG("readNews: fileno exists, should never happen.");
105         return;
106     }
107
108     my($item,$chan);
109     my($ci,$cu) = (0,0);
110
111     open(NEWS, $file);
112     while (<NEWS>) {
113         chop;
114
115         # todo: allow commands.
116
117         if (/^[\s\t]+(\S+):[\s\t]+(.*)$/) {
118             if (!defined $item) {
119                 &::DEBUG("!defined item, never happen!");
120                 next;
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   = (exists $::news{$chan}{$str}) ? $str : &getNewsItem($str);
357     my $item    = &getNewsItem($str);
358     if (!defined $item or !scalar keys %{ $::news{$chan}{$item} }) {
359         &::msg($::who, "No news item called '$str'");
360         return;
361     }
362
363     if (!exists $::news{$chan}{$item}{Text}) {
364         &::msg($::who, "Someone forgot to add info to this news item");
365         return;
366     }
367
368     # todo: show item number.
369     # todo: show ago-time aswell?
370     # todo: show request stats aswell.
371     my $t = localtime($::news{$chan}{$item}{Time});
372     my $a = $::news{$chan}{$item}{Author};
373     &::msg($::who, "+- News \002$chan\002 ##, item '\037$item\037':");
374     &::msg($::who, "| Added by $a at $t");
375     &::msg($::who, $::news{$chan}{$item}{Text});
376
377     $::news{$chan}{$item}{'Request_By'}   = $::who;
378     $::news{$chan}{$item}{'Request_Time'} = time();
379     $::news{$chan}{$item}{'Request_Count'}++;
380 }
381
382 sub mod {
383     my($item, $str) = split /\s+/, $_[0], 2;
384
385     if (!defined $item or $item eq "" or $str =~ /^\s*$/) {
386         &::help("news mod");
387         return;
388     }
389
390     my $news = &getNewsItem($item);
391
392     if (!defined $news) {
393         &::DEBUG("error: mod: news == undefined.");
394         return;
395     }
396     my $nnews = $::news{$chan}{$news}{Text};
397     my $mod_news  = $news;
398     my $mod_nnews = $nnews;
399
400     # SAR patch. mu++
401     if ($str =~ m|^\s*s([/,#\|])(.+?)\1(.*?)\1([a-z]*);?\s*$|) {
402         my ($delim, $op, $np, $flags) = ($1,$2,$3,$4);
403
404         if ($flags !~ /^(g)?$/) {
405             &::msg($::who, "error: Invalid flags to regex.");
406             return;
407         }
408
409         ### TODO: use m### to make code safe!
410         # todo: make code safer.
411         my $done = 0;
412         # todo: use eval to deal with flags easily.
413         if ($flags eq "") {
414             $done++ if (!$done and $mod_news  =~ s/\Q$op\E/$np/);
415             $done++ if (!$done and $mod_nnews =~ s/\Q$op\E/$np/);
416         } elsif ($flags eq "g") {
417             $done++ if ($mod_news  =~ s/\Q$op\E/$np/g);
418             $done++ if ($mod_nnews =~ s/\Q$op\E/$np/g);
419         }
420
421         if (!$done) {
422             &::msg($::who, "warning: regex not found in news.");
423             return;
424         }
425
426         if ($mod_news ne $news) { # news item.
427             if (exists $::news{$chan}{$mod_news}) {
428                 &::msg($::who, "item '$mod_news' already exists.");
429                 return;
430             }
431
432             &::msg($::who, "Moving item '$news' to '$mod_news' with SAR s/$op/$np/.");
433             foreach (keys %{ $::news{$chan}{$news} }) {
434                 $::news{$chan}{$mod_news}{$_} = $::news{$chan}{$news}{$_};
435                 delete $::news{$chan}{$news}{$_};
436             }
437             # needed?
438             delete $::news{$chan}{$news};
439         }
440
441         if ($mod_nnews ne $nnews) { # news Text/Description.
442             &::msg($::who, "Changing text for '$news' SAR s/$op/$np/.");
443             if ($mod_news ne $news) {
444                 $::news{$chan}{$mod_news}{Text} = $mod_nnews;
445             } else {
446                 $::news{$chan}{$news}{Text}     = $mod_nnews;
447             }
448         }
449
450         return;
451     } else {
452         &::msg($::who, "error: that regex failed ;(");
453         return;
454     }
455
456     &::msg($::who, "error: Invalid regex. Try s/1/2/, s#3#4#...");
457 }
458
459 sub set {
460     my($args) = @_;
461     $args =~ /^(\S+)\s+(\S+)\s+(.*)$/;
462     my($item, $what, $value) = ($1,$2,$3);
463
464     &::DEBUG("set called.");
465
466     if ($item eq "") {
467         &::help("news set");
468         return;
469     }
470
471     &::DEBUG("item => '$item'.");
472     my $news = &getNewsItem($item);
473     &::DEBUG("news => '$news'");
474
475     if (!defined $news) {
476         &::msg($::who, "Could not find item '$item' substring or # in news list.");
477         return;
478     }
479
480     # list all values for chan.
481     if (!defined $what) {
482         &::DEBUG("set: 1");
483         return;
484     }
485
486     my $ok = 0;
487     my @elements = ("Expire","Text");
488     foreach (@elements) {
489         next unless ($what =~ /^$_$/i);
490         $what = $_;
491         $ok++;
492         last;
493     }
494
495     if (!$ok) {
496         &::msg($::who, "Invalid set.  Try: @elements");
497         return;
498     }
499
500     # show (read) what.
501     if (!defined $value) {
502         &::DEBUG("set: 2");
503         return;
504     }
505
506     if (!exists $::news{$chan}{$news}) {
507         &::msg($::who, "news '$news' does not exist");
508         return;
509     }
510
511     if ($what eq "Expire") {
512         # todo: use do_set().
513
514         my $time = 0;
515         my $plus = ($value =~ s/^\+//g);
516         while ($value =~ s/^(\d+)(\S*)\s*//) {
517             my($int,$unit) = ($1,$2);
518             $time += $int       if ($unit =~ /^s(ecs?)?$/i);
519             $time += $int*60    if ($unit =~ /^m(in(utes?)?)?$/i);
520             $time += $int*60*60 if ($unit =~ /^h(ours?)?$/i);
521             $time += $int*60*60*24 if (!$unit or $unit =~ /^d(ays?)?$/i);
522             $time += $int*60*60*24*7 if ($unit =~ /^w(eeks?)?$/i);
523             $time += $int*60*60*24*30 if ($unit =~ /^mon(th)?$/i);
524         }
525
526         if ($value =~ s/^never$//i) {
527             # never.
528             $time = -1;
529         } elsif ($plus) {
530             # from now.
531             $time += time();
532         } else {
533             # from creation of item.
534             $time += $::news{$chan}{$news}{Time};
535         }
536
537         if (!$time or ($value and $value !~ /^never$/i)) {
538             &::DEBUG("set: Expire... need to parse.");
539             return;
540         }
541
542         if ($time == -1) {
543             &::msg($::who, "Set never expire for \002$item\002." );
544         } elsif ($time < -1) {
545             &::DEBUG("time should never be negative ($time).");
546             return;
547         } else {
548             &::msg($::who, "Set expire for \002$item\002, to ".
549                 localtime($time) ." [".&::Time2String($time - time())."]" );
550
551             if (time() > $time) {
552                 &::DEBUG("hrm... time() > $time, should expire.");
553             }
554         }
555
556
557         $::news{$chan}{$news}{Expire} = $time;
558
559         return;
560     }
561
562     my $auth = 0;
563     &::DEBUG("who => '$::who'");
564     my $author = $::news{$chan}{$news}{Author};
565     $auth++ if ($::who eq $author);
566     $auth++ if (&::IsFlag("o"));
567     if (!defined $author) {
568         &::DEBUG("news{$chan}{$news}{Author} is not defined! auth'd anyway");
569         $::news{$chan}{$news}{Author} = $::who;
570         $author = $::who;
571         $auth++;
572     }
573
574     if (!$auth) {
575         # todo: show when it'll expire.
576         &::msg($::who, "Sorry, you cannot set items. (author $author owns it)");
577         return;
578     }
579
580     my $old = $::news{$chan}{$news}{$what};
581     if (defined $old) {
582         &::DEBUG("old => $old.");
583     }
584     $::news{$chan}{$news}{$what} = $value;
585     &::msg($::who, "Setting [$chan]/{$news}/<$what> to '$value'.");
586 }
587
588 sub latest {
589     my($tchan, $flag) = @_;
590
591     $chan ||= $tchan;   # hack hack hack.
592
593     # todo: if chan = undefined, guess.
594     if (!exists $::news{$chan}) {
595         &::msg($::who, "invalid chan $chan");
596         return;
597     }
598
599     my @new;
600     foreach (keys %{ $::news{$chan} }) {
601         my $t = $::newsuser{$chan}{$::who};
602         next if (!defined $t);
603         next if ($t > $::news{$chan}{$_}{Time});
604
605         push(@new, $_);
606     }
607
608     if (!scalar @new and $flag) {
609         &::msg($::who, "no new news for $chan.");
610         return;
611     }
612
613     if (scalar @new) {
614         &::msg($::who, "+==== New news for \002$chan\002 (".
615                 scalar(@new)." new items):");
616
617         my $timestr = &::Time2String( time() - $::newsuser{$chan}{$::who} );
618         &::msg($::who, "|= Last time read $timestr ago");
619
620         foreach (@new) {
621             my $i   = &newsS2N($_);
622             &::DEBUG("i = $i, _ => $_");
623             my $age = time() - $::news{$chan}{$_}{Time};
624             &::msg($::who, sprintf("\002[\002%2d\002]\002 %s",
625                 $i, $_) );
626 #               $i, $_, &::Time2String($age) ) );
627         }
628
629         &::msg($::who, "|= to read, do 'news read <#>' or 'news read <keyword>'");
630
631         # lame hack to prevent dupes if we just ignore it.
632         $::newsuser{$chan}{$::who} = time();
633     }
634 }
635
636 ###
637 ### helpers...
638 ###
639
640 sub getNewsAll {
641     my %time;
642     foreach (keys %{ $::news{$chan} }) {
643         $time{ $::news{$chan}{$_}{Time} } = $_;
644     }
645
646     my @items;
647     foreach (sort { $a <=> $b } keys %time) {
648         push(@items, $time{$_});
649     }
650
651     return @items;
652 }
653
654 sub newsS2N {
655     my($what)   = @_;
656     my @items;
657     my $no;
658
659     my %time;
660     foreach (keys %{ $::news{$chan} }) {
661         my $t = $::news{$chan}{$_}{Time};
662
663         if (!defined $t or $t !~ /^\d+$/) {
664             &::DEBUG("warn: t is undefined for news{$chan}{$_}{Time}; removing item.");
665             delete $::news{$chan}{$_};
666             next;
667         }
668
669         $time{$t} = $_;
670     }
671
672     foreach (sort { $a <=> $b } keys %time) {
673         $item++;
674         return $item if ($time{$_} eq $what);
675     }
676
677     &::DEBUG("newsS2N($what): failed...");
678 }
679
680 sub getNewsItem {
681     my($what)   = @_;
682     my $item    = 0;
683
684     my %time;
685     foreach (keys %{ $::news{$chan} }) {
686         my $t = $::news{$chan}{$_}{Time};
687
688         if (!defined $t or $t !~ /^\d+$/) {
689             &::DEBUG("warn: t is undefined for news{$chan}{$_}{Time}; removing item.");
690             delete $::news{$chan}{$_};
691             next;
692         }
693
694         $time{$t} = $_;
695     }
696
697     # number to string resolution.
698     if ($what =~ /^\d+$/) {
699         foreach (sort { $a <=> $b } keys %time) {
700             $item++;
701             return $time{$_} if ($item == $what);
702         }
703
704     } else {
705         # partial string to full string resolution
706
707         my @items;
708         my $no;
709         foreach (sort { $a <=> $b } keys %time) {
710             $item++;
711 #           $no = $item if ($time{$_} eq $what);
712             if ($time{$_} eq $what) {
713                 $no = $item;
714                 next;
715             }
716
717             push(@items, $time{$_}) if ($time{$_} =~ /\Q$what\E/i);
718         }
719
720         # since we have so much built into this function, there is so
721         # many guesses we can make.
722         # todo: split this command in the future into:
723         #       full_string->number and number->string
724         #       partial_string->full_string
725         &::DEBUG("no => $no, items => @items.");
726         if (defined $no and !@items) {
727             &::DEBUG("string->number resolution.");
728             return $no;
729         }
730
731         if (scalar @items > 1) {
732             &::DEBUG("Multiple matches, not guessing.");
733             &::msg($::who, "Multiple matches, not guessing.");
734             return;
735         }
736
737         &::DEBUG("gNI: string->number(??): $what->$items[0]");
738         if (@items) {
739             &::DEBUG("gNI: Guessed '$items[0]'.");
740             return $items[0];
741         } else {
742             &::DEBUG("gNI: No match.");
743             return;
744         }
745     }
746
747     &::ERROR("getNewsItem: Should not happen (what = $what)");
748     return;
749 }
750
751 sub do_set {
752     my($what,$value) = @_;
753
754     if (!defined $chan) {
755         &::DEBUG("do_set: chan not defined.");
756         return;
757     }
758
759     if (!defined $what or $what =~ /^\s*$/) {
760         &::DEBUG("what $what is not defined.");
761         return;
762     }
763     if (!defined $value or $value =~ /^\s*$/) {
764         &::DEBUG("value $value is not defined.");
765         return;
766     }
767
768     &::DEBUG("do_set: TODO...");
769 }
770
771 1;