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("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 &::VERB("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);
92 } elsif ($what =~ /^list$/i) {
93 &::DEBUG("list longcut called.");
96 } elsif ($what =~ /^(expire|text|desc)(\s+(.*))?$/i) {
99 my($arg1,$arg2) = split(/\s+/, $3, 2);
100 &set("$arg1 $1 $arg2");
102 } elsif ($what =~ /^help(\s+(.*))?$/i) {
105 } elsif ($what =~ /^newsflush$/i) {
106 &::msg($::who, "newsflush called... check out the logs!");
109 } elsif ($what =~ /^(un)?notify$/i) {
110 my $state = ($1) ? 0 : 1;
112 # todo: don't notify even if "news" is called.
113 if (!&::IsChanConf("newsNotifyAll")) {
114 &::DEBUG("chan => $chan, ::chan => $::chan.");
115 &::notice($::who, "not available for this channel or disabled altogether.");
119 my $t = $::newsuser{$chan}{$::who};
120 if ($state) { # state = 1
121 if (defined $t and ($t == 0 or $t == -1)) {
122 &::notice($::who, "enabled notify.");
123 delete $::newsuser{$chan}{$::who};
126 &::notice($::who, "already enabled.");
129 my $x = $::newsuser{$chan}{$::who};
130 if (defined $x and ($x == 0 or $x == -1)) {
131 &::notice($::who, "notify already disabled");
134 $::newsuser{$chan}{$::who} = -1;
135 &::notice($::who, "notify is now disabled.");
139 &::DEBUG("could not parse '$what'.");
140 &::notice($::who, "unknown command: $what");
145 my $file = "$::bot_base_dir/blootbot-news.txt";
146 if (! -f $file or -z $file) {
151 &::DEBUG("readNews: fileno exists, should never happen.");
162 # todo: allow commands.
164 if (/^[\s\t]+(\S+):[\s\t]+(.*)$/) {
165 if (!defined $item) {
166 &::DEBUG("!defined item, never happen!");
170 $::news{$chan}{$item}{$1} = $2;
174 # U <chan> <nick> <time>
175 if (/^U\s+(\S+)\s+(\S+)\s+(\d+)$/) {
176 $::newsuser{$1}{$2} = $3;
181 if (/^(\S+)[\s\t]+(.*)$/) {
189 my $cn = scalar(keys %::news);
190 &::status("News: Read $ci items for $cn chans, $cu users cache")
191 if ($ci or $cn or $cu);
195 if (!scalar keys %::news and !scalar keys %::newsuser) {
196 &::VERB("wN: nothing to write.",2);
200 my $file = "$::bot_base_dir/blootbot-news.txt";
203 &::ERROR("fileno NEWS exists, should never happen.");
207 # todo: add commands to output file.
209 my($cc,$ci,$cu) = (0,0,0);
211 open(NEWS, ">$file");
212 foreach $chan (sort keys %::news) {
213 $c = scalar keys %{ $::news{$chan} };
217 foreach $item (sort keys %{ $::news{$chan} }) {
218 $c = scalar keys %{ $::news{$chan}{$item} };
222 print NEWS "$chan $item\n";
223 foreach $what (sort keys %{ $::news{$chan}{$item} }) {
224 print NEWS " $what: $::news{$chan}{$item}{$what}\n";
230 # todo: show how many users we wrote down.
231 if (&::getChanConfList("newsKeepRead")) {
232 # old users are removed in newsFlush(), perhaps it should be
235 foreach $chan (sort keys %::newsuser) {
237 foreach (sort keys %{ $::newsuser{$chan} }) {
238 print NEWS "U $chan $_ $::newsuser{$chan}{$_}\n";
246 &::status("News: Wrote $ci items for $cc chans, $cu user cache.");
252 if (!defined $chan or !defined $str or $str =~ /^\s*$/) {
257 if (length $str > 64) {
258 &::notice($::who, "That's not really an item (>64chars)");
262 if (exists $::news{$chan}{$str}{Time}) {
263 &::notice($::who, "'$str' for $chan already exists!");
267 $::news{$chan}{$str}{Time} = time();
268 my $expire = &::getChanConfDefault("newsDefaultExpire",7);
269 $::news{$chan}{$str}{Expire} = time() + $expire*60*60*24;
270 $::news{$chan}{$str}{Author} = $::who;
272 my $agestr = &::Time2String($::news{$chan}{$str}{Expire} - time() );
273 my $item = &newsS2N($str);
274 &::notice($::who, "Added '\037$str\037' at [".localtime(time).
275 "] by \002$::who\002 for item #\002$item\002.");
276 &::notice($::who, "Now do 'news text $item <your_description>'");
277 &::notice($::who, "This item will expire at \002".
278 localtime($::news{$chan}{$str}{Expire})."\002 [$agestr from now] "
288 if (!defined $what) {
293 if ($what =~ /^\d+$/) {
294 my $count = scalar keys %{ $::news{$chan} };
296 &::notice($::who, "No news for $chan.");
300 if ($what > $count or $what < 0) {
301 &::notice($::who, "$what is out of range (max $count)");
305 $item = &getNewsItem($what);
306 $what = $item; # hack hack hack.
309 $_ = &getNewsItem($what); # hack hack hack.
310 $what = $_ if (defined $_);
312 if (!exists $::news{$chan}{$what}) {
314 foreach (keys %{ $::news{$chan} }) {
315 next unless (/\Q$what\E/);
319 if (!scalar @found) {
320 &::notice($::who, "could not find $what.");
324 if (scalar @found > 1) {
325 &::notice($::who, "too many matches for $what.");
330 &::DEBUG("del: str: guessed what => $what");
334 if (exists $::news{$chan}{$what}) {
336 $auth++ if ($::who eq $::news{$chan}{$what}{Author});
337 $auth++ if (&::IsFlag("o"));
340 # todo: show when it'll expire.
341 &::notice($::who, "Sorry, you cannot remove items; just let them expire on their own.");
345 &::notice($::who, "ok, deleted '$what' from \002$chan\002...");
346 delete $::news{$chan}{$what};
348 &::notice($::who, "error: not found $what in news for $chan.");
353 if (!scalar keys %{ $::news{$chan} }) {
354 &::notice($::who, "No News for \002$chan\002.");
358 if (&::IsChanConf("newsKeepRead")) {
359 my $x = $::newsuser{$chan}{$::who};
361 if (defined $x and ($x == 0 or $x == -1)) {
362 &::DEBUG("not updating time for $::who.");
364 $::newsuser{$chan}{$::who} = time();
368 my $count = scalar keys %{ $::news{$chan} };
369 &::notice($::who, "|==== News for \002$chan\002: ($count items)");
371 foreach (keys %{ $::news{$chan} }) {
372 my $t = $::news{$chan}{$_}{Time};
373 $newest = $t if ($t > $newest);
375 my $timestr = &::Time2String(time() - $newest);
376 &::notice($::who, "|= Last updated $timestr ago.");
377 &::notice($::who, " \037Num\037 \037Item ".(" "x40)." \037");
380 foreach ( &getNewsAll() ) {
382 my $setby = $::news{$chan}{$subtopic}{Author};
384 if (!defined $subtopic) {
385 &::DEBUG("warn: subtopic == undef.");
389 # todo: show request stats aswell.
390 &::notice($::who, sprintf("\002[\002%2d\002]\002 %s",
395 &::notice($::who, "|= End of News.");
396 &::notice($::who, "use 'news read <#>' or 'news read <keyword>'");
402 if (!defined $chan or !defined $str or $str =~ /^\s*$/) {
403 &::help("news read");
407 if (!scalar keys %{ $::news{$chan} }) {
408 &::notice($::who, "No News for \002$chan\002.");
412 my $item = &getNewsItem($str);
413 if (!defined $item or !scalar keys %{ $::news{$chan}{$item} }) {
414 &::notice($::who, "No news item called '$str'");
418 if (!exists $::news{$chan}{$item}{Text}) {
419 &::notice($::who, "Someone forgot to add info to this news item");
423 my $t = localtime( $::news{$chan}{$item}{Time} );
424 my $a = $::news{$chan}{$item}{Author};
425 my $text = $::news{$chan}{$item}{Text};
426 my $num = &newsS2N($item);
427 my $rwho = $::news{$chan}{$item}{Request_By} || $::who;
428 my $rcount = $::news{$chan}{$item}{Request_Count} || 0;
430 if (length $text < $::param{maxKeySize}) {
431 &::VERB("NEWS: Possible news->factoid redirection.",2);
432 my $f = &::getFactoid($text);
435 &::VERB("NEWS: ok, $text is factoid redirection.",2);
436 $f =~ s/^<REPLY>\s*//i; # anything else?
441 $_ = $::news{$chan}{$item}{'Expire'};
444 $e = sprintf("\037%s\037 [%s from now]",
445 scalar(localtime($_)),
446 &::Time2String($_ - time())
450 &::notice($::who, "+- News \002$chan\002 #$num: $item");
451 &::notice($::who, "| Added by $a at \037$t\037");
452 &::notice($::who, "| Expire: $e") if (defined $e);
453 &::notice($::who, $text);
454 &::notice($::who, "| Requested \002$rcount\002 times, last by \002$rwho\002") if ($rcount and $rwho);
456 $::news{$chan}{$item}{'Request_By'} = $::who;
457 $::news{$chan}{$item}{'Request_Time'} = time();
458 $::news{$chan}{$item}{'Request_Count'}++;
462 my($item, $str) = split /\s+/, $_[0], 2;
464 if (!defined $item or $item eq "" or $str =~ /^\s*$/) {
469 my $news = &getNewsItem($item);
471 if (!defined $news) {
472 &::DEBUG("error: mod: news == undefined.");
475 my $nnews = $::news{$chan}{$news}{Text};
476 my $mod_news = $news;
477 my $mod_nnews = $nnews;
480 if ($str =~ m|^\s*s([/,#\|])(.+?)\1(.*?)\1([a-z]*);?\s*$|) {
481 my ($delim, $op, $np, $flags) = ($1,$2,$3,$4);
483 if ($flags !~ /^(g)?$/) {
484 &::notice($::who, "error: Invalid flags to regex.");
488 ### TODO: use m### to make code safe!
489 # todo: make code safer.
491 # todo: use eval to deal with flags easily.
493 $done++ if (!$done and $mod_news =~ s/\Q$op\E/$np/);
494 $done++ if (!$done and $mod_nnews =~ s/\Q$op\E/$np/);
495 } elsif ($flags eq "g") {
496 $done++ if ($mod_news =~ s/\Q$op\E/$np/g);
497 $done++ if ($mod_nnews =~ s/\Q$op\E/$np/g);
501 &::notice($::who, "warning: regex not found in news.");
505 if ($mod_news ne $news) { # news item.
506 if (exists $::news{$chan}{$mod_news}) {
507 &::notice($::who, "item '$mod_news' already exists.");
511 &::notice($::who, "Moving item '$news' to '$mod_news' with SAR s/$op/$np/.");
512 foreach (keys %{ $::news{$chan}{$news} }) {
513 $::news{$chan}{$mod_news}{$_} = $::news{$chan}{$news}{$_};
514 delete $::news{$chan}{$news}{$_};
517 delete $::news{$chan}{$news};
520 if ($mod_nnews ne $nnews) { # news Text/Description.
521 &::notice($::who, "Changing text for '$news' SAR s/$op/$np/.");
522 if ($mod_news ne $news) {
523 $::news{$chan}{$mod_news}{Text} = $mod_nnews;
525 $::news{$chan}{$news}{Text} = $mod_nnews;
531 &::notice($::who, "error: that regex failed ;(");
535 &::notice($::who, "error: Invalid regex. Try s/1/2/, s#3#4#...");
540 $args =~ /^(\S+)\s+(\S+)\s+(.*)$/;
541 my($item, $what, $value) = ($1,$2,$3);
543 &::DEBUG("set called.");
550 &::DEBUG("item => '$item'.");
551 my $news = &getNewsItem($item);
553 if (!defined $news) {
554 &::notice($::who, "Could not find item '$item' substring or # in news list.");
558 # list all values for chan.
559 if (!defined $what) {
565 my @elements = ("Expire","Text");
566 foreach (@elements) {
567 next unless ($what =~ /^$_$/i);
574 &::notice($::who, "Invalid set. Try: @elements");
579 if (!defined $value) {
584 if (!exists $::news{$chan}{$news}) {
585 &::notice($::who, "news '$news' does not exist");
589 if ($what eq "Expire") {
590 # todo: use do_set().
593 my $plus = ($value =~ s/^\+//g);
594 while ($value =~ s/^(\d+)(\S*)\s*//) {
595 my($int,$unit) = ($1,$2);
596 $time += $int if ($unit =~ /^s(ecs?)?$/i);
597 $time += $int*60 if ($unit =~ /^m(in(utes?)?)?$/i);
598 $time += $int*60*60 if ($unit =~ /^h(ours?)?$/i);
599 $time += $int*60*60*24 if (!$unit or $unit =~ /^d(ays?)?$/i);
600 $time += $int*60*60*24*7 if ($unit =~ /^w(eeks?)?$/i);
601 $time += $int*60*60*24*30 if ($unit =~ /^mon(th)?$/i);
604 if ($value =~ s/^never$//i) {
611 # from creation of item.
612 $time += $::news{$chan}{$news}{Time};
615 if (!$time or ($value and $value !~ /^never$/i)) {
616 &::DEBUG("set: Expire... need to parse.");
621 &::notice($::who, "Set never expire for \002$item\002." );
622 } elsif ($time < -1) {
623 &::DEBUG("time should never be negative ($time).");
626 &::notice($::who, "Set expire for \002$item\002, to ".
627 localtime($time) ." [".&::Time2String($time - time())."]" );
629 if (time() > $time) {
630 &::DEBUG("hrm... time() > $time, should expire.");
635 $::news{$chan}{$news}{Expire} = $time;
641 &::DEBUG("who => '$::who'");
642 my $author = $::news{$chan}{$news}{Author};
643 $auth++ if ($::who eq $author);
644 $auth++ if (&::IsFlag("o"));
645 if (!defined $author) {
646 &::DEBUG("news{$chan}{$news}{Author} is not defined! auth'd anyway");
647 $::news{$chan}{$news}{Author} = $::who;
653 # todo: show when it'll expire.
654 &::notice($::who, "Sorry, you cannot set items. (author $author owns it)");
658 # todo: clean this up.
659 my $old = $::news{$chan}{$news}{$what};
661 &::DEBUG("old => $old.");
663 $::news{$chan}{$news}{$what} = $value;
664 &::notice($::who, "Setting [$chan]/{$news}/<$what> to '$value'.");
668 my($tchan, $flag) = @_;
670 $chan ||= $tchan; # hack hack hack.
672 # todo: if chan = undefined, guess.
673 if (!exists $::news{$chan}) {
674 &::notice($::who, "invalid chan $chan") if ($flag);
678 my $t = $::newsuser{$chan}{$::who};
679 if (defined $t and ($t == 0 or $t == -1)) {
681 &::notice($::who, "if you want to read news, try /msg $::ident news or /msg $::ident news notify");
683 &::DEBUG("not displaying any new news for $::who");
688 my $x = &::IsChanConf("newsNotifyAll");
689 if (&::IsChanConf("newsNotifyAll") and !defined $t) {
694 &::DEBUG("something went really wrong.");
695 &::DEBUG("chan => $chan, ::chan => $::chan");
696 &::notice($::who, "something went really wrong.");
701 foreach (keys %{ $::news{$chan} }) {
702 next if (!defined $t);
703 next if ($t > $::news{$chan}{$_}{Time});
705 # don't list new items if they don't have Text.
706 if (!exists $::news{$chan}{$_}{Text}) {
707 if (time() - $::news{$chan}{$_}{Time} > 60*60*24*3) {
708 &::DEBUG("deleting news{$chan}{$_} because it was too old and had no text info.");
709 delete $::news{$chan}{$_};
718 # !scalar @new, $flag
719 if (!scalar @new and $flag) {
720 &::notice($::who, "no new news for $chan.");
724 # scalar @new, !$flag
725 my $unread = scalar @new;
726 my $total = scalar keys %{ $::news{$chan} };
728 return unless ($unread);
730 my $reply = "There are unread news in $chan ($unread unread, $total total). /msg $::ident news latest";
731 $reply .= " If you don't want further news notification, /msg $::ident news unnotify" if ($unread == $total);
732 &::notice($::who, $reply);
739 &::notice($::who, "+==== New news for \002$chan\002 ($unread new; $total total):");
741 my $t = $::newsuser{$chan}{$::who};
742 if (defined $t and $t > 1) {
743 my $timestr = &::Time2String( time() - $t );
744 &::notice($::who, "|= Last time read $timestr ago");
749 my $i = &newsS2N($_);
753 for (my $i=0; $i<=scalar(@sorted); $i++) {
754 my $news = $sorted[$i];
755 next unless (defined $news);
757 my $age = time() - $::news{$chan}{$news}{Time};
758 &::notice($::who, sprintf("\002[\002%2d\002]\002 %s",
760 # $i, $_, &::Time2String($age) ) );
763 &::notice($::who, "|= to read, do 'news read <#>' or 'news read <keyword>'");
765 # lame hack to prevent dupes if we just ignore it.
766 my $x = $::newsuser{$chan}{$::who};
767 if (defined $x and ($x == 0 or $x == -1)) {
768 &::DEBUG("not updating time for $::who. (2)");
770 $::newsuser{$chan}{$::who} = time();
781 foreach (keys %{ $::news{$chan} }) {
782 $time{ $::news{$chan}{$_}{Time} } = $_;
786 foreach (sort { $a <=> $b } keys %time) {
787 push(@items, $time{$_});
800 foreach (keys %{ $::news{$chan} }) {
801 my $t = $::news{$chan}{$_}{Time};
803 if (!defined $t or $t !~ /^\d+$/) {
804 &::DEBUG("warn: t is undefined for news{$chan}{$_}{Time}; removing item.");
805 delete $::news{$chan}{$_};
812 foreach (sort { $a <=> $b } keys %time) {
814 return $item if ($time{$_} eq $what);
817 &::DEBUG("newsS2N($what): failed...");
825 foreach (keys %{ $::news{$chan} }) {
826 my $t = $::news{$chan}{$_}{Time};
828 if (!defined $t or $t !~ /^\d+$/) {
829 &::DEBUG("warn: t is undefined for news{$chan}{$_}{Time}; removing item.");
830 delete $::news{$chan}{$_};
837 # number to string resolution.
838 if ($what =~ /^\d+$/) {
839 foreach (sort { $a <=> $b } keys %time) {
841 return $time{$_} if ($item == $what);
845 # partial string to full string resolution
846 # in some cases, string->number resolution.
850 foreach (sort { $a <=> $b } keys %time) {
852 # $no = $item if ($time{$_} eq $what);
853 if ($time{$_} eq $what) {
858 push(@items, $time{$_}) if ($time{$_} =~ /\Q$what\E/i);
861 if (defined $no and !@items) {
862 &::DEBUG("string->number resolution: $what->$no.");
866 if (scalar @items > 1) {
867 &::DEBUG("Multiple matches, not guessing.");
868 &::notice($::who, "Multiple matches, not guessing.");
873 &::DEBUG("gNI: part_string->full_string: $what->$items[0]");
876 &::DEBUG("gNI: No match.");
881 &::ERROR("getNewsItem: Should not happen (what = $what)");
886 my($what,$value) = @_;
888 if (!defined $chan) {
889 &::DEBUG("do_set: chan not defined.");
893 if (!defined $what or $what =~ /^\s*$/) {
894 &::DEBUG("what $what is not defined.");
897 if (!defined $value or $value =~ /^\s*$/) {
898 &::DEBUG("value $value is not defined.");
902 &::DEBUG("do_set: TODO...");