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.
25 if (!exists $::cache{newsFirst}) {
26 &::DEBUG("news: looks like we enabled news option just then; loading up news file just in case.");
27 $::cache{newsFirst} = 1;
33 if ($::msgType ne "private") {
37 if (defined $what and $what =~ s/^($::mask{chan})\s*//) {
38 # todo: check if the channel exists aswell.
41 if (!&::IsNickInChan($::who, $chan)) {
42 &::notice($::who, "sorry but you're not on $chan.");
48 my @chans = &::getNickInChans($::who);
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");
55 if (scalar @chans == 0) {
56 &::notice($::who, "error: I couldn't find you on any chan. This must be a bug!");
61 &::DEBUG("Guessed $::who being on chan $chan",2);
62 $::chan = $chan; # hack for IsChanConf().
65 if (!defined $what or $what =~ /^\s*$/) {
70 if ($what =~ /^add(\s+(.*))?$/i) {
73 } elsif ($what =~ /^del(\s+(.*))?$/i) {
76 } elsif ($what =~ /^mod(\s+(.*))?$/i) {
79 } elsif ($what =~ /^set(\s+(.*))?$/i) {
82 } elsif ($what =~ /^(\d+)$/i) {
83 &::VERB("News: read shortcut called.",2);
86 } elsif ($what =~ /^read(\s+(.*))?$/i) {
89 } elsif ($what =~ /^(latest|new)(\s+(.*))?$/i) {
90 &latest($3 || $chan, 1);
91 # $::cmdstats{'News latest'}++;
93 } elsif ($what =~ /^stats?$/i) {
96 } elsif ($what =~ /^list$/i) {
99 } elsif ($what =~ /^(expire|text|desc)(\s+(.*))?$/i) {
102 my($arg1,$arg2) = split(/\s+/, $3, 2);
103 &set("$arg1 $1 $arg2");
105 } elsif ($what =~ /^help(\s+(.*))?$/i) {
108 } elsif ($what =~ /^newsflush$/i) {
109 &::msg($::who, "newsflush called... check out the logs!");
112 } elsif ($what =~ /^(un)?notify$/i) {
113 my $state = ($1) ? 0 : 1;
115 # todo: don't notify even if "news" is called.
116 if (!&::IsChanConf("newsNotifyAll")) {
117 &::DEBUG("news: chan => $chan, ::chan => $::chan.");
118 &::notice($::who, "not available for this channel or disabled altogether.");
122 my $t = $::newsuser{$chan}{$::who};
123 if ($state) { # state = 1
124 if (defined $t and ($t == 0 or $t == -1)) {
125 &::notice($::who, "enabled notify.");
126 delete $::newsuser{$chan}{$::who};
129 &::notice($::who, "already enabled.");
132 my $x = $::newsuser{$chan}{$::who};
133 if (defined $x and ($x == 0 or $x == -1)) {
134 &::notice($::who, "notify already disabled");
137 $::newsuser{$chan}{$::who} = -1;
138 &::notice($::who, "notify is now disabled.");
142 &::DEBUG("news: could not parse '$what'");
143 &::notice($::who, "unknown command: $what");
148 my $file = "$::bot_base_dir/blootbot-news.txt";
149 if (! -f $file or -z $file) {
154 &::DEBUG("readNews: fileno exists, should never happen.");
165 # todo: allow commands.
167 if (/^[\s\t]+(\S+):[\s\t]+(.*)$/) {
168 if (!defined $item) {
169 &::DEBUG("news: !defined item, never happen!");
173 $::news{$chan}{$item}{$1} = $2;
177 # U <chan> <nick> <time>
178 if (/^U\s+(\S+)\s+(\S+)\s+(\d+)$/) {
179 $::newsuser{$1}{$2} = $3;
184 if (/^(\S+)[\s\t]+(.*)$/) {
192 my $cn = scalar(keys %::news);
193 &::status("News: Read ".
194 $ci. &::fixPlural(" item", $ci). " for ".
195 $cn. &::fixPlural(" chan", $cn). ", ".
196 $cu. &::fixPlural(" user", $cu), " cache"
197 ) if ($ci or $cn or $cu);
201 if (!scalar keys %::news and !scalar keys %::newsuser) {
202 &::VERB("wN: nothing to write.",2);
206 my $file = "$::bot_base_dir/blootbot-news.txt";
209 &::ERROR("fileno NEWS exists, should never happen.");
213 # todo: add commands to output file.
215 my($cc,$ci,$cu) = (0,0,0);
217 open(NEWS, ">$file");
218 foreach $chan (sort keys %::news) {
219 $c = scalar keys %{ $::news{$chan} };
223 foreach $item (sort keys %{ $::news{$chan} }) {
224 $c = scalar keys %{ $::news{$chan}{$item} };
228 print NEWS "$chan $item\n";
229 foreach $what (sort keys %{ $::news{$chan}{$item} }) {
230 print NEWS " $what: $::news{$chan}{$item}{$what}\n";
236 # todo: show how many users we wrote down.
237 if (&::getChanConfList("newsKeepRead")) {
238 # old users are removed in newsFlush(), perhaps it should be
241 foreach $chan (sort keys %::newsuser) {
243 foreach (sort keys %{ $::newsuser{$chan} }) {
244 print NEWS "U $chan $_ $::newsuser{$chan}{$_}\n";
252 &::status("News: Wrote $ci items for $cc chans, $cu user cache.");
258 if (!defined $chan or !defined $str or $str =~ /^\s*$/) {
263 if (length $str > 64) {
264 &::notice($::who, "That's not really an item (>64chars)");
268 if (exists $::news{$chan}{$str}{Time}) {
269 &::notice($::who, "'$str' for $chan already exists!");
273 $::news{$chan}{$str}{Time} = time();
274 my $expire = &::getChanConfDefault("newsDefaultExpire",7);
275 $::news{$chan}{$str}{Expire} = time() + $expire*60*60*24;
276 $::news{$chan}{$str}{Author} = $::who;
278 my $agestr = &::Time2String($::news{$chan}{$str}{Expire} - time() );
279 my $item = &newsS2N($str);
280 &::notice($::who, "Added '\037$str\037' at [".localtime(time).
281 "] by \002$::who\002 for item #\002$item\002.");
282 &::notice($::who, "Now do 'news text $item <your_description>'");
283 &::notice($::who, "This item will expire at \002".
284 localtime($::news{$chan}{$str}{Expire})."\002 [$agestr from now] "
294 if (!defined $what) {
299 if ($what =~ /^\d+$/) {
300 my $count = scalar keys %{ $::news{$chan} };
302 &::notice($::who, "No news for $chan.");
306 if ($what > $count or $what < 0) {
307 &::notice($::who, "$what is out of range (max $count)");
311 $item = &getNewsItem($what);
312 $what = $item; # hack hack hack.
315 $_ = &getNewsItem($what); # hack hack hack.
316 $what = $_ if (defined $_);
318 if (!exists $::news{$chan}{$what}) {
320 foreach (keys %{ $::news{$chan} }) {
321 next unless (/\Q$what\E/);
325 if (!scalar @found) {
326 &::notice($::who, "could not find $what.");
330 if (scalar @found > 1) {
331 &::notice($::who, "too many matches for $what.");
336 &::DEBUG("news: del: str: guessed what => $what");
340 if (exists $::news{$chan}{$what}) {
342 $auth++ if ($::who eq $::news{$chan}{$what}{Author});
343 $auth++ if (&::IsFlag("o"));
346 # todo: show when it'll expire.
347 &::notice($::who, "Sorry, you cannot remove items; just let them expire on their own.");
351 &::notice($::who, "ok, deleted '$what' from \002$chan\002...");
352 delete $::news{$chan}{$what};
354 &::notice($::who, "error: not found $what in news for $chan.");
359 if (!scalar keys %{ $::news{$chan} }) {
360 &::notice($::who, "No News for \002$chan\002.");
364 if (&::IsChanConf("newsKeepRead")) {
365 my $x = $::newsuser{$chan}{$::who};
367 if (defined $x and ($x == 0 or $x == -1)) {
368 &::DEBUG("news: not updating time for $::who.");
370 if (!scalar keys %{ $::news{$chan} }) {
371 &::DEBUG("news: should not add $chan/$::who to cache!");
374 $::newsuser{$chan}{$::who} = time();
378 # ¬ice() breaks OPN :( - using msg() instead!
379 my $count = scalar keys %{ $::news{$chan} };
380 &::msg($::who, "|==== News for \002$chan\002: ($count items)");
382 foreach (keys %{ $::news{$chan} }) {
383 my $t = $::news{$chan}{$_}{Time};
384 $newest = $t if ($t > $newest);
386 my $timestr = &::Time2String(time() - $newest);
387 &::msg($::who, "|= Last updated $timestr ago.");
388 &::msg($::who, " \037Num\037 \037Item ".(" "x40)." \037");
391 foreach ( &getNewsAll() ) {
393 my $setby = $::news{$chan}{$subtopic}{Author};
395 if (!defined $subtopic) {
396 &::DEBUG("news: warn: subtopic == undef.");
400 # todo: show request stats aswell.
401 &::msg($::who, sprintf("\002[\002%2d\002]\002 %s",
406 &::msg($::who, "|= End of News.");
407 &::msg($::who, "use 'news read <#>' or 'news read <keyword>'");
413 if (!defined $chan or !defined $str or $str =~ /^\s*$/) {
414 &::help("news read");
418 if (!scalar keys %{ $::news{$chan} }) {
419 &::notice($::who, "No News for \002$chan\002.");
423 my $item = &getNewsItem($str);
424 if (!defined $item or !scalar keys %{ $::news{$chan}{$item} }) {
425 &::notice($::who, "No news item called '$str'");
429 if (!exists $::news{$chan}{$item}{Text}) {
430 &::notice($::who, "Someone forgot to add info to this news item");
434 my $t = localtime( $::news{$chan}{$item}{Time} );
435 my $a = $::news{$chan}{$item}{Author};
436 my $text = $::news{$chan}{$item}{Text};
437 my $num = &newsS2N($item);
438 my $rwho = $::news{$chan}{$item}{Request_By} || $::who;
439 my $rcount = $::news{$chan}{$item}{Request_Count} || 0;
441 if (length $text < $::param{maxKeySize}) {
442 &::VERB("NEWS: Possible news->factoid redirection.",2);
443 my $f = &::getFactoid($text);
446 &::VERB("NEWS: ok, $text is factoid redirection.",2);
447 $f =~ s/^<REPLY>\s*//i; # anything else?
452 $_ = $::news{$chan}{$item}{'Expire'};
455 $e = sprintf("\037%s\037 [%s from now]",
456 scalar(localtime($_)),
457 &::Time2String($_ - time())
461 &::notice($::who, "+- News \002$chan\002 #$num: $item");
462 &::notice($::who, "| Added by $a at \037$t\037");
463 &::notice($::who, "| Expire: $e") if (defined $e);
464 &::notice($::who, $text);
465 &::notice($::who, "| Requested \002$rcount\002 times, last by \002$rwho\002") if ($rcount and $rwho);
467 $::news{$chan}{$item}{'Request_By'} = $::who;
468 $::news{$chan}{$item}{'Request_Time'} = time();
469 $::news{$chan}{$item}{'Request_Count'}++;
473 my($item, $str) = split /\s+/, $_[0], 2;
475 if (!defined $item or $item eq "" or $str =~ /^\s*$/) {
480 my $news = &getNewsItem($item);
482 if (!defined $news) {
483 &::DEBUG("news: error: mod: news == undefined.");
486 my $nnews = $::news{$chan}{$news}{Text};
487 my $mod_news = $news;
488 my $mod_nnews = $nnews;
491 if ($str =~ m|^\s*s([/,#\|])(.+?)\1(.*?)\1([a-z]*);?\s*$|) {
492 my ($delim, $op, $np, $flags) = ($1,$2,$3,$4);
494 if ($flags !~ /^(g)?$/) {
495 &::notice($::who, "error: Invalid flags to regex.");
499 ### TODO: use m### to make code safe!
500 # todo: make code safer.
502 # todo: use eval to deal with flags easily.
504 $done++ if (!$done and $mod_news =~ s/\Q$op\E/$np/);
505 $done++ if (!$done and $mod_nnews =~ s/\Q$op\E/$np/);
506 } elsif ($flags eq "g") {
507 $done++ if ($mod_news =~ s/\Q$op\E/$np/g);
508 $done++ if ($mod_nnews =~ s/\Q$op\E/$np/g);
512 &::notice($::who, "warning: regex not found in news.");
516 if ($mod_news ne $news) { # news item.
517 if (exists $::news{$chan}{$mod_news}) {
518 &::notice($::who, "item '$mod_news' already exists.");
522 &::notice($::who, "Moving item '$news' to '$mod_news' with SAR s/$op/$np/.");
523 foreach (keys %{ $::news{$chan}{$news} }) {
524 $::news{$chan}{$mod_news}{$_} = $::news{$chan}{$news}{$_};
525 delete $::news{$chan}{$news}{$_};
528 delete $::news{$chan}{$news};
531 if ($mod_nnews ne $nnews) { # news Text/Description.
532 &::notice($::who, "Changing text for '$news' SAR s/$op/$np/.");
533 if ($mod_news ne $news) {
534 $::news{$chan}{$mod_news}{Text} = $mod_nnews;
536 $::news{$chan}{$news}{Text} = $mod_nnews;
542 &::notice($::who, "error: that regex failed ;(");
546 &::notice($::who, "error: Invalid regex. Try s/1/2/, s#3#4#...");
551 $args =~ /^(\S+)\s+(\S+)\s+(.*)$/;
552 my($item, $what, $value) = ($1,$2,$3);
554 &::DEBUG("news: set called.");
561 &::DEBUG("news: set: item => '$item'.");
562 my $news = &getNewsItem($item);
564 if (!defined $news) {
565 &::notice($::who, "Could not find item '$item' substring or # in news list.");
569 # list all values for chan.
570 if (!defined $what) {
571 &::DEBUG("news: set: 1");
576 my @elements = ("Expire","Text");
577 foreach (@elements) {
578 next unless ($what =~ /^$_$/i);
585 &::notice($::who, "Invalid set. Try: @elements");
590 if (!defined $value) {
591 &::DEBUG("news: set: 2");
595 if (!exists $::news{$chan}{$news}) {
596 &::notice($::who, "news '$news' does not exist");
600 if ($what eq "Expire") {
601 # todo: use do_set().
604 my $plus = ($value =~ s/^\+//g);
605 while ($value =~ s/^(\d+)(\S*)\s*//) {
606 my($int,$unit) = ($1,$2);
607 $time += $int if ($unit =~ /^s(ecs?)?$/i);
608 $time += $int*60 if ($unit =~ /^m(in(utes?)?)?$/i);
609 $time += $int*60*60 if ($unit =~ /^h(ours?)?$/i);
610 $time += $int*60*60*24 if (!$unit or $unit =~ /^d(ays?)?$/i);
611 $time += $int*60*60*24*7 if ($unit =~ /^w(eeks?)?$/i);
612 $time += $int*60*60*24*30 if ($unit =~ /^mon(th)?$/i);
615 if ($value =~ s/^never$//i) {
622 # from creation of item.
623 $time += $::news{$chan}{$news}{Time};
626 if (!$time or ($value and $value !~ /^never$/i)) {
627 &::DEBUG("news: set: Expire... need to parse.");
632 &::notice($::who, "Set never expire for \002$item\002." );
633 } elsif ($time < -1) {
634 &::DEBUG("news: time should never be negative ($time).");
637 &::notice($::who, "Set expire for \002$item\002, to ".
638 localtime($time) ." [".&::Time2String($time - time())."]" );
640 if (time() > $time) {
641 &::DEBUG("news: hrm... time() > $time, should expire.");
646 $::news{$chan}{$news}{Expire} = $time;
652 &::DEBUG("news: who => '$::who'");
653 my $author = $::news{$chan}{$news}{Author};
654 $auth++ if ($::who eq $author);
655 $auth++ if (&::IsFlag("o"));
656 if (!defined $author) {
657 &::DEBUG("news: news{$chan}{$news}{Author} is not defined! auth'd anyway");
658 $::news{$chan}{$news}{Author} = $::who;
664 # todo: show when it'll expire.
665 &::notice($::who, "Sorry, you cannot set items. (author $author owns it)");
669 # todo: clean this up.
670 my $old = $::news{$chan}{$news}{$what};
672 &::DEBUG("news: old => $old.");
674 $::news{$chan}{$news}{$what} = $value;
675 &::notice($::who, "Setting [$chan]/{$news}/<$what> to '$value'.");
679 my($tchan, $flag) = @_;
681 $chan ||= $tchan; # hack hack hack.
683 # todo: if chan = undefined, guess.
684 # if (!exists $::news{$chan}) {
685 if (!exists $::channels{$chan}) {
686 &::notice($::who, "invalid chan $chan") if ($flag);
690 my $t = $::newsuser{$chan}{$::who};
691 if (defined $t and ($t == 0 or $t == -1)) {
693 &::notice($::who, "if you want to read news, try /msg $::ident news or /msg $::ident news notify");
695 &::DEBUG("news: not displaying any new news for $::who");
701 my $x = &::IsChanConf("newsNotifyAll");
702 if (&::IsChanConf("newsNotifyAll") and !defined $t) {
707 &::DEBUG("news: something went really wrong.");
708 &::DEBUG("news: chan => $chan, ::chan => $::chan");
709 &::notice($::who, "something went really wrong.");
714 foreach (keys %{ $::news{$chan} }) {
715 next if (!defined $t);
716 next if ($t > $::news{$chan}{$_}{Time});
718 # don't list new items if they don't have Text.
719 if (!exists $::news{$chan}{$_}{Text}) {
720 if (time() - $::news{$chan}{$_}{Time} > 60*60*24*3) {
721 &::DEBUG("deleting news{$chan}{$_} because it was too old and had no text info.");
722 delete $::news{$chan}{$_};
731 # !scalar @new, $flag
732 if (!scalar @new and $flag) {
733 &::notice($::who, "no new news for $chan.");
737 # scalar @new, !$flag
738 my $unread = scalar @new;
739 my $total = scalar keys %{ $::news{$chan} };
741 return unless ($unread);
743 my $reply = "There are unread news in $chan ($unread unread, $total total). /msg $::ident news $::chan latest";
744 $reply .= " If you don't want further news notification, /msg $::ident news unnotify" if ($unread == $total);
745 &::notice($::who, $reply);
752 &::notice($::who, "+==== New news for \002$chan\002 ($unread new; $total total):");
754 my $t = $::newsuser{$chan}{$::who};
755 if (defined $t and $t > 1) {
756 my $timestr = &::Time2String( time() - $t );
757 &::notice($::who, "|= Last time read $timestr ago");
762 my $i = &newsS2N($_);
766 for (my $i=0; $i<=scalar(@sorted); $i++) {
767 my $news = $sorted[$i];
768 next unless (defined $news);
770 my $age = time() - $::news{$chan}{$news}{Time};
771 &::notice($::who, sprintf("\002[\002%2d\002]\002 %s",
773 # $i, $_, &::Time2String($age) ) );
776 &::notice($::who, "|= to read, do 'news read <#>' or 'news read <keyword>'");
778 # lame hack to prevent dupes if we just ignore it.
779 my $x = $::newsuser{$chan}{$::who};
780 if (defined $x and ($x == 0 or $x == -1)) {
781 &::DEBUG("news: not updating time for $::who. (2)");
783 $::newsuser{$chan}{$::who} = time();
794 foreach (keys %{ $::news{$chan} }) {
795 $time{ $::news{$chan}{$_}{Time} } = $_;
799 foreach (sort { $a <=> $b } keys %time) {
800 push(@items, $time{$_});
813 foreach (keys %{ $::news{$chan} }) {
814 my $t = $::news{$chan}{$_}{Time};
816 if (!defined $t or $t !~ /^\d+$/) {
817 &::DEBUG("news: warn: t is undefined for news{$chan}{$_}{Time}; removing item.");
818 delete $::news{$chan}{$_};
825 foreach (sort { $a <=> $b } keys %time) {
827 return $item if ($time{$_} eq $what);
830 &::DEBUG("newsS2N($what): failed...");
838 foreach (keys %{ $::news{$chan} }) {
839 my $t = $::news{$chan}{$_}{Time};
841 if (!defined $t or $t !~ /^\d+$/) {
842 &::DEBUG("news: warn: t is undefined for news{$chan}{$_}{Time}; removing item.");
843 delete $::news{$chan}{$_};
850 # number to string resolution.
851 if ($what =~ /^\d+$/) {
852 foreach (sort { $a <=> $b } keys %time) {
854 return $time{$_} if ($item == $what);
858 # partial string to full string resolution
859 # in some cases, string->number resolution.
863 foreach (sort { $a <=> $b } keys %time) {
865 # $no = $item if ($time{$_} eq $what);
866 if ($time{$_} eq $what) {
871 push(@items, $time{$_}) if ($time{$_} =~ /\Q$what\E/i);
874 if (defined $no and !@items) {
875 &::DEBUG("news: string->number resolution: $what->$no.");
879 if (scalar @items > 1) {
880 &::DEBUG("news: Multiple matches, not guessing.");
881 &::notice($::who, "Multiple matches, not guessing.");
886 &::DEBUG("news: gNI: part_string->full_string: $what->$items[0]");
889 &::DEBUG("news: gNI: No match for '$what'");
894 &::ERROR("getNewsItem: Should not happen (what = $what)");
899 my($what,$value) = @_;
901 if (!defined $chan) {
902 &::DEBUG("news: do_set: chan not defined.");
906 if (!defined $what or $what =~ /^\s*$/) {
907 &::DEBUG("news: what $what is not defined.");
911 if (!defined $value or $value =~ /^\s*$/) {
912 &::DEBUG("news: value $value is not defined.");
916 &::DEBUG("news: do_set: TODO...");
920 &::DEBUG("News: stats called.");
921 &::msg($::who, "check my logs/console.");
924 # total request count.
925 foreach $chan (keys %::news) {
926 foreach (keys %{ $::news{$chan} }) {
927 $i += $::news{$chan}{$_}{Request_Count};
930 &::DEBUG("news: stats: total request count => $i");
934 foreach $chan (keys %::newsuser) {
935 $i += $::newsuser{$chan}{$_};
937 &::DEBUG("news: stats: total user cache => $i");
940 # average latest time read.
942 foreach $chan (keys %::newsuser) {
943 $i += $t - $::newsuser{$chan}{$_};
946 &::DEBUG("news: stats: average latest time read: total time: $i");
947 &::DEBUG("news: ... count: $j");
948 &::DEBUG("news: average: ".sprintf("%.02f", $i/($j||1))." sec/user");