2 # News.pl: Advanced news management
4 # Version: v0.3 (20014012)
6 # Notes: Testing done by greycat, kudos!
9 # news{ channel }{ string } { item }
10 # newsuser{ channel }{ user } = time()
12 # Time - when it was added (used for sorting)
14 # Expire - Time to expire.
28 if (!exists $::cache{newsFirst}) {
29 &::DEBUG("news: looks like we enabled news option just then; loading up news file just in case.");
30 $::cache{newsFirst} = 1;
36 if ($::msgType ne "private") {
40 if (defined $what and $what =~ s/^($::mask{chan})\s*//) {
41 # todo: check if the channel exists aswell.
44 if (!&::IsNickInChan($who, $chan)) {
45 &::notice($::who, "sorry but you're not on $chan.");
51 my @chans = &::getNickInChans($::who);
53 if (scalar @chans > 1) {
54 &::notice($::who, "error: I dunno which channel you are referring to since you're on more than one. Try 'news #chan ...' instead");
58 if (scalar @chans == 0) {
59 &::notice($::who, "error: I couldn't find you on any chan. This must be a bug!");
64 &::VERB("Guessed $::who being on chan $chan",2);
65 $::chan = $chan; # hack for IsChanConf().
68 if (!defined $what or $what =~ /^\s*$/) {
73 if ($what =~ /^add(\s+(.*))?$/i) {
76 } elsif ($what =~ /^del(\s+(.*))?$/i) {
79 } elsif ($what =~ /^mod(\s+(.*))?$/i) {
82 } elsif ($what =~ /^set(\s+(.*))?$/i) {
85 } elsif ($what =~ /^(\d+)$/i) {
86 &::VERB("News: read shortcut called.",2);
89 } elsif ($what =~ /^read(\s+(.*))?$/i) {
92 } elsif ($what =~ /^(latest|new)(\s+(.*))?$/i) {
93 &latest($3 || $chan, 1);
94 # $::cmdstats{'News latest'}++;
96 } elsif ($what =~ /^stats?$/i) {
99 } elsif ($what =~ /^list$/i) {
102 } elsif ($what =~ /^(expire|text|desc)(\s+(.*))?$/i) {
105 my($arg1,$arg2) = split(/\s+/, $3, 2);
106 &set("$arg1 $1 $arg2");
108 } elsif ($what =~ /^help(\s+(.*))?$/i) {
111 } elsif ($what =~ /^newsflush$/i) {
112 &::msg($::who, "newsflush called... check out the logs!");
115 } elsif ($what =~ /^(un)?notify$/i) {
116 my $state = ($1) ? 0 : 1;
118 # todo: don't notify even if "news" is called.
119 if (!&::IsChanConf("newsNotifyAll")) {
120 &::DEBUG("news: chan => $chan, ::chan => $::chan.");
121 &::notice($::who, "not available for this channel or disabled altogether.");
125 my $t = $::newsuser{$chan}{$who};
126 if ($state) { # state = 1
127 if (defined $t and ($t == 0 or $t == -1)) {
128 &::notice($::who, "enabled notify.");
129 delete $::newsuser{$chan}{$who};
132 &::notice($::who, "already enabled.");
135 my $x = $::newsuser{$chan}{$who};
136 if (defined $x and ($x == 0 or $x == -1)) {
137 &::notice($::who, "notify already disabled");
140 $::newsuser{$chan}{$who} = -1;
141 &::notice($::who, "notify is now disabled.");
145 &::notice($::who, "unknown command: $what");
150 my $file = "$::bot_base_dir/blootbot-news.txt";
151 if (! -f $file or -z $file) {
156 &::DEBUG("readNews: fileno exists, should never happen.");
167 # todo: allow commands.
169 if (/^[\s\t]+(\S+):[\s\t]+(.*)$/) {
170 if (!defined $item) {
171 &::DEBUG("news: !defined item, never happen!");
175 $::news{$chan}{$item}{$1} = $2;
179 # U <chan> <nick> <time>
180 if (/^U\s+(\S+)\s+(\S+)\s+(\d+)$/) {
181 $::newsuser{$1}{$2} = $3;
186 if (/^(\S+)[\s\t]+(.*)$/) {
194 my $cn = scalar(keys %::news);
195 &::status("News: Read ".
196 $ci. &::fixPlural(" item", $ci). " for ".
197 $cn. &::fixPlural(" chan", $cn). ", ".
198 $cu. &::fixPlural(" user", $cu), " cache"
199 ) if ($ci or $cn or $cu);
203 if (!scalar keys %::news and !scalar keys %::newsuser) {
204 &::VERB("wN: nothing to write.",2);
208 my $file = "$::bot_base_dir/blootbot-news.txt";
211 &::ERROR("fileno NEWS exists, should never happen.");
215 # todo: add commands to output file.
217 my($cc,$ci,$cu) = (0,0,0);
219 open(NEWS, ">$file");
220 foreach $chan (sort keys %::news) {
221 $c = scalar keys %{ $::news{$chan} };
225 foreach $item (sort keys %{ $::news{$chan} }) {
226 $c = scalar keys %{ $::news{$chan}{$item} };
230 print NEWS "$chan $item\n";
231 foreach $what (sort keys %{ $::news{$chan}{$item} }) {
232 print NEWS " $what: $::news{$chan}{$item}{$what}\n";
238 # todo: show how many users we wrote down.
239 if (&::getChanConfList("newsKeepRead")) {
240 # old users are removed in newsFlush(), perhaps it should be
243 foreach $chan (sort keys %::newsuser) {
245 foreach (sort keys %{ $::newsuser{$chan} }) {
246 print NEWS "U $chan $_ $::newsuser{$chan}{$_}\n";
254 &::status("News: Wrote $ci items for $cc chans, $cu user cache.");
260 if (!defined $chan or !defined $str or $str =~ /^\s*$/) {
265 if (length $str > 64) {
266 &::notice($::who, "That's not really an item (>64chars)");
270 if (exists $::news{$chan}{$str}{Time}) {
271 &::notice($::who, "'$str' for $chan already exists!");
275 $::news{$chan}{$str}{Time} = time();
276 my $expire = &::getChanConfDefault("newsDefaultExpire",7);
277 $::news{$chan}{$str}{Expire} = time() + $expire*60*60*24;
278 $::news{$chan}{$str}{Author} = $::who;
280 my $agestr = &::Time2String($::news{$chan}{$str}{Expire} - time() );
281 my $item = &newsS2N($str);
282 &::notice($::who, "Added '\037$str\037' at [".localtime(time).
283 "] by \002$::who\002 for item #\002$item\002.");
284 &::notice($::who, "Now do 'news text $item <your_description>'");
285 &::notice($::who, "This item will expire at \002".
286 localtime($::news{$chan}{$str}{Expire})."\002 [$agestr from now] "
296 if (!defined $what) {
301 if ($what =~ /^\d+$/) {
302 my $count = scalar keys %{ $::news{$chan} };
304 &::notice($::who, "No news for $chan.");
308 if ($what > $count or $what < 0) {
309 &::notice($::who, "$what is out of range (max $count)");
313 $item = &getNewsItem($what);
314 $what = $item; # hack hack hack.
317 $_ = &getNewsItem($what); # hack hack hack.
318 $what = $_ if (defined $_);
320 if (!exists $::news{$chan}{$what}) {
322 foreach (keys %{ $::news{$chan} }) {
323 next unless (/\Q$what\E/);
327 if (!scalar @found) {
328 &::notice($::who, "could not find $what.");
332 if (scalar @found > 1) {
333 &::notice($::who, "too many matches for $what.");
338 &::DEBUG("news: del: str: guessed what => $what");
342 if (exists $::news{$chan}{$what}) {
344 $auth++ if ($::who eq $::news{$chan}{$what}{Author});
345 $auth++ if (&::IsFlag("o"));
348 # todo: show when it'll expire.
349 &::notice($::who, "Sorry, you cannot remove items; just let them expire on their own.");
353 &::notice($::who, "ok, deleted '$what' from \002$chan\002...");
354 delete $::news{$chan}{$what};
356 &::notice($::who, "error: not found $what in news for $chan.");
361 if (!scalar keys %{ $::news{$chan} }) {
362 &::notice($::who, "No News for \002$chan\002.");
366 if (&::IsChanConf("newsKeepRead")) {
367 my $x = $::newsuser{$chan}{$who};
369 if (defined $x and ($x == 0 or $x == -1)) {
370 &::DEBUG("news: not updating time for $::who.");
372 if (!scalar keys %{ $::news{$chan} }) {
373 &::DEBUG("news: should not add $chan/$::who to cache!");
376 $::newsuser{$chan}{$who} = time();
380 # ¬ice() breaks OPN :( - using msg() instead!
381 my $count = scalar keys %{ $::news{$chan} };
382 &::msg($::who, "|==== News for \002$chan\002: ($count items)");
384 foreach (keys %{ $::news{$chan} }) {
385 my $t = $::news{$chan}{$_}{Time};
386 $newest = $t if ($t > $newest);
388 my $timestr = &::Time2String(time() - $newest);
389 &::msg($::who, "|= Last updated $timestr ago.");
390 &::msg($::who, " \037Num\037 \037Item ".(" "x40)." \037");
393 foreach ( &getNewsAll() ) {
395 my $setby = $::news{$chan}{$subtopic}{Author};
397 if (!defined $subtopic) {
398 &::DEBUG("news: warn: subtopic == undef.");
402 # todo: show request stats aswell.
403 &::msg($::who, sprintf("\002[\002%2d\002]\002 %s",
408 &::msg($::who, "|= End of News.");
409 &::msg($::who, "use 'news read <#>' or 'news read <keyword>'");
415 if (!defined $chan or !defined $str or $str =~ /^\s*$/) {
416 &::help("news read");
420 if (!scalar keys %{ $::news{$chan} }) {
421 &::notice($::who, "No News for \002$chan\002.");
425 my $item = &getNewsItem($str);
426 if (!defined $item or !scalar keys %{ $::news{$chan}{$item} }) {
427 &::notice($::who, "No news item called '$str'");
431 if (!exists $::news{$chan}{$item}{Text}) {
432 &::notice($::who, "Someone forgot to add info to this news item");
436 my $t = localtime( $::news{$chan}{$item}{Time} );
437 my $a = $::news{$chan}{$item}{Author};
438 my $text = $::news{$chan}{$item}{Text};
439 my $num = &newsS2N($item);
440 my $rwho = $::news{$chan}{$item}{Request_By} || $::who;
441 my $rcount = $::news{$chan}{$item}{Request_Count} || 0;
443 if (length $text < $::param{maxKeySize}) {
444 &::VERB("NEWS: Possible news->factoid redirection.",2);
445 my $f = &::getFactoid($text);
448 &::VERB("NEWS: ok, $text is factoid redirection.",2);
449 $f =~ s/^<REPLY>\s*//i; # anything else?
454 $_ = $::news{$chan}{$item}{'Expire'};
457 $e = sprintf("\037%s\037 [%s from now]",
458 scalar(localtime($_)),
459 &::Time2String($_ - time())
463 &::notice($::who, "+- News \002$chan\002 #$num: $item");
464 &::notice($::who, "| Added by $a at \037$t\037");
465 &::notice($::who, "| Expire: $e") if (defined $e);
466 &::notice($::who, $text);
467 &::notice($::who, "| Requested \002$rcount\002 times, last by \002$rwho\002") if ($rcount and $rwho);
469 $::news{$chan}{$item}{'Request_By'} = $::who;
470 $::news{$chan}{$item}{'Request_Time'} = time();
471 $::news{$chan}{$item}{'Request_Count'}++;
475 my($item, $str) = split /\s+/, $_[0], 2;
477 if (!defined $item or $item eq "" or $str =~ /^\s*$/) {
482 my $news = &getNewsItem($item);
484 if (!defined $news) {
485 &::DEBUG("news: error: mod: news == undefined.");
488 my $nnews = $::news{$chan}{$news}{Text};
489 my $mod_news = $news;
490 my $mod_nnews = $nnews;
493 if ($str =~ m|^\s*s([/,#\|])(.+?)\1(.*?)\1([a-z]*);?\s*$|) {
494 my ($delim, $op, $np, $flags) = ($1,$2,$3,$4);
496 if ($flags !~ /^(g)?$/) {
497 &::notice($::who, "error: Invalid flags to regex.");
501 ### TODO: use m### to make code safe!
502 # todo: make code safer.
504 # todo: use eval to deal with flags easily.
506 $done++ if (!$done and $mod_news =~ s/\Q$op\E/$np/);
507 $done++ if (!$done and $mod_nnews =~ s/\Q$op\E/$np/);
508 } elsif ($flags eq "g") {
509 $done++ if ($mod_news =~ s/\Q$op\E/$np/g);
510 $done++ if ($mod_nnews =~ s/\Q$op\E/$np/g);
514 &::notice($::who, "warning: regex not found in news.");
518 if ($mod_news ne $news) { # news item.
519 if (exists $::news{$chan}{$mod_news}) {
520 &::notice($::who, "item '$mod_news' already exists.");
524 &::notice($::who, "Moving item '$news' to '$mod_news' with SAR s/$op/$np/.");
525 foreach (keys %{ $::news{$chan}{$news} }) {
526 $::news{$chan}{$mod_news}{$_} = $::news{$chan}{$news}{$_};
527 delete $::news{$chan}{$news}{$_};
530 delete $::news{$chan}{$news};
533 if ($mod_nnews ne $nnews) { # news Text/Description.
534 &::notice($::who, "Changing text for '$news' SAR s/$op/$np/.");
535 if ($mod_news ne $news) {
536 $::news{$chan}{$mod_news}{Text} = $mod_nnews;
538 $::news{$chan}{$news}{Text} = $mod_nnews;
544 &::notice($::who, "error: that regex failed ;(");
548 &::notice($::who, "error: Invalid regex. Try s/1/2/, s#3#4#...");
553 $args =~ /^(\S+)\s+(\S+)\s+(.*)$/;
554 my($item, $what, $value) = ($1,$2,$3);
556 &::DEBUG("news: set called.");
563 &::DEBUG("news: set: item => '$item'.");
564 my $news = &getNewsItem($item);
566 if (!defined $news) {
567 &::notice($::who, "Could not find item '$item' substring or # in news list.");
571 # list all values for chan.
572 if (!defined $what) {
573 &::DEBUG("news: set: 1");
578 my @elements = ("Expire","Text");
579 foreach (@elements) {
580 next unless ($what =~ /^$_$/i);
587 &::notice($::who, "Invalid set. Try: @elements");
592 if (!defined $value) {
593 &::DEBUG("news: set: 2");
597 if (!exists $::news{$chan}{$news}) {
598 &::notice($::who, "news '$news' does not exist");
602 if ($what eq "Expire") {
603 # todo: use do_set().
606 my $plus = ($value =~ s/^\+//g);
607 while ($value =~ s/^(\d+)(\S*)\s*//) {
608 my($int,$unit) = ($1,$2);
609 $time += $int if ($unit =~ /^s(ecs?)?$/i);
610 $time += $int*60 if ($unit =~ /^m(in(utes?)?)?$/i);
611 $time += $int*60*60 if ($unit =~ /^h(ours?)?$/i);
612 $time += $int*60*60*24 if (!$unit or $unit =~ /^d(ays?)?$/i);
613 $time += $int*60*60*24*7 if ($unit =~ /^w(eeks?)?$/i);
614 $time += $int*60*60*24*30 if ($unit =~ /^mon(th)?$/i);
617 if ($value =~ s/^never$//i) {
624 # from creation of item.
625 $time += $::news{$chan}{$news}{Time};
628 if (!$time or ($value and $value !~ /^never$/i)) {
629 &::DEBUG("news: set: Expire... need to parse.");
634 &::notice($::who, "Set never expire for \002$item\002." );
635 } elsif ($time < -1) {
636 &::DEBUG("news: time should never be negative ($time).");
639 &::notice($::who, "Set expire for \002$item\002, to ".
640 localtime($time) ." [".&::Time2String($time - time())."]" );
642 if (time() > $time) {
643 &::DEBUG("news: hrm... time() > $time, should expire.");
648 $::news{$chan}{$news}{Expire} = $time;
654 &::DEBUG("news: who => '$::who'");
655 my $author = $::news{$chan}{$news}{Author};
656 $auth++ if ($::who eq $author);
657 $auth++ if (&::IsFlag("o"));
658 if (!defined $author) {
659 &::DEBUG("news: news{$chan}{$news}{Author} is not defined! auth'd anyway");
660 $::news{$chan}{$news}{Author} = $::who;
666 # todo: show when it'll expire.
667 &::notice($::who, "Sorry, you cannot set items. (author $author owns it)");
671 # todo: clean this up.
672 my $old = $::news{$chan}{$news}{$what};
674 &::DEBUG("news: old => $old.");
676 $::news{$chan}{$news}{$what} = $value;
677 &::notice($::who, "Setting [$chan]/{$news}/<$what> to '$value'.");
681 my($tchan, $flag) = @_;
687 # todo: if chan = undefined, guess.
688 # if (!exists $::news{$chan}) {
689 if (!exists $::channels{$chan}) {
690 &::notice($::who, "invalid chan $chan") if ($flag);
694 my $t = $::newsuser{$chan}{$who};
695 if (defined $t and ($t == 0 or $t == -1)) {
697 &::notice($::who, "if you want to read news, try /msg $::ident news or /msg $::ident news notify");
699 &::DEBUG("news: not displaying any new news for $::who");
705 my $x = &::IsChanConf("newsNotifyAll");
706 if (&::IsChanConf("newsNotifyAll") and !defined $t) {
711 &::DEBUG("news: something went really wrong.");
712 &::DEBUG("news: chan => $chan, ::chan => $::chan");
713 # &::notice($::who, "something went really wrong.");
718 foreach (keys %{ $::news{$chan} }) {
719 next if (!defined $t);
720 next if ($t > $::news{$chan}{$_}{Time});
722 # don't list new items if they don't have Text.
723 if (!exists $::news{$chan}{$_}{Text}) {
724 if (time() - $::news{$chan}{$_}{Time} > 60*60*24*3) {
725 &::DEBUG("deleting news{$chan}{$_} because it was too old and had no text info.");
726 delete $::news{$chan}{$_};
735 # !scalar @new, $flag
736 if (!scalar @new and $flag) {
737 &::notice($::who, "no new news for $chan.");
741 # scalar @new, !$flag
742 my $unread = scalar @new;
743 my $total = scalar keys %{ $::news{$chan} };
745 return unless ($unread);
747 my $reply = "There are unread news in $chan ($unread unread, $total total). /msg $::ident news $::chan latest";
748 $reply .= " If you don't want further news notification, /msg $::ident news unnotify" if ($unread == $total);
749 &::notice($::who, $reply);
756 &::notice($::who, "+==== New news for \002$chan\002 ($unread new; $total total):");
758 my $t = $::newsuser{$chan}{$who};
759 if (defined $t and $t > 1) {
760 my $timestr = &::Time2String( time() - $t );
761 &::notice($::who, "|= Last time read $timestr ago");
766 my $i = &newsS2N($_);
770 for (my $i=0; $i<=scalar(@sorted); $i++) {
771 my $news = $sorted[$i];
772 next unless (defined $news);
774 my $age = time() - $::news{$chan}{$news}{Time};
775 &::notice($::who, sprintf("\002[\002%2d\002]\002 %s",
777 # $i, $_, &::Time2String($age) ) );
780 &::notice($::who, "|= to read, do 'news read <#>' or 'news read <keyword>'");
782 # lame hack to prevent dupes if we just ignore it.
783 my $x = $::newsuser{$chan}{$who};
784 if (defined $x and ($x == 0 or $x == -1)) {
785 &::DEBUG("news: not updating time for $::who. (2)");
787 $::newsuser{$chan}{$who} = time();
798 foreach (keys %{ $::news{$chan} }) {
799 $time{ $::news{$chan}{$_}{Time} } = $_;
803 foreach (sort { $a <=> $b } keys %time) {
804 push(@items, $time{$_});
817 foreach (keys %{ $::news{$chan} }) {
818 my $t = $::news{$chan}{$_}{Time};
820 if (!defined $t or $t !~ /^\d+$/) {
821 &::DEBUG("news: warn: t is undefined for news{$chan}{$_}{Time}; removing item.");
822 delete $::news{$chan}{$_};
829 foreach (sort { $a <=> $b } keys %time) {
831 return $item if ($time{$_} eq $what);
834 &::DEBUG("newsS2N($what): failed...");
842 foreach (keys %{ $::news{$chan} }) {
843 my $t = $::news{$chan}{$_}{Time};
845 if (!defined $t or $t !~ /^\d+$/) {
846 &::DEBUG("news: warn: t is undefined for news{$chan}{$_}{Time}; removing item.");
847 delete $::news{$chan}{$_};
854 # number to string resolution.
855 if ($what =~ /^\d+$/) {
856 foreach (sort { $a <=> $b } keys %time) {
858 return $time{$_} if ($item == $what);
862 # partial string to full string resolution
863 # in some cases, string->number resolution.
867 foreach (sort { $a <=> $b } keys %time) {
869 # $no = $item if ($time{$_} eq $what);
870 if ($time{$_} eq $what) {
875 push(@items, $time{$_}) if ($time{$_} =~ /\Q$what\E/i);
878 if (defined $no and !@items) {
879 &::DEBUG("news: string->number resolution: $what->$no.");
883 if (scalar @items > 1) {
884 &::DEBUG("news: Multiple matches, not guessing.");
885 &::notice($::who, "Multiple matches, not guessing.");
890 &::DEBUG("news: gNI: part_string->full_string: $what->$items[0]");
893 &::DEBUG("news: gNI: No match for '$what'");
898 &::ERROR("getNewsItem: Should not happen (what = $what)");
903 my($what,$value) = @_;
905 if (!defined $chan) {
906 &::DEBUG("news: do_set: chan not defined.");
910 if (!defined $what or $what =~ /^\s*$/) {
911 &::DEBUG("news: what $what is not defined.");
915 if (!defined $value or $value =~ /^\s*$/) {
916 &::DEBUG("news: value $value is not defined.");
920 &::DEBUG("news: do_set: TODO...");
924 &::DEBUG("News: stats called.");
925 &::msg($::who, "check my logs/console.");
928 # total request count.
929 foreach $chan (keys %::news) {
930 foreach (keys %{ $::news{$chan} }) {
931 $i += $::news{$chan}{$_}{Request_Count};
934 &::DEBUG("news: stats: total request count => $i");
938 foreach $chan (keys %::newsuser) {
939 $i += $::newsuser{$chan}{$_};
941 &::DEBUG("news: stats: total user cache => $i");
944 # average latest time read.
946 foreach $chan (keys %::newsuser) {
947 $i += $t - $::newsuser{$chan}{$_};
951 &::DEBUG("news: stats: average latest time read: total time: $i");
952 &::DEBUG("news: ... count: $j");
953 &::DEBUG("news: average: ".sprintf("%.02f", $i/($j||1))." sec/user");