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.
43 my @chans = &::GetNickInChans($::who);
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");
50 if (scalar @chans == 0) {
51 &::msg($::who, "error: I couldn't find you on any chan. This must be a bug!");
56 &::DEBUG("Guessed $::who being on chan $chan");
57 $::chan = $chan; # hack for IsChanConf().
60 if (!defined $what or $what =~ /^\s*$/) {
65 if ($what =~ /^add(\s+(.*))?$/i) {
68 } elsif ($what =~ /^del(\s+(.*))?$/i) {
71 } elsif ($what =~ /^mod(\s+(.*))?$/i) {
74 } elsif ($what =~ /^set(\s+(.*))?$/i) {
77 } elsif ($what =~ /^(\d)$/i) {
78 &::DEBUG("read shortcut called.");
81 } elsif ($what =~ /^read(\s+(.*))?$/i) {
84 } elsif ($what =~ /^(latest|new)(\s+(.*))?$/i) {
85 &latest($3 || $chan, 1);
87 } elsif ($what =~ /^list$/i) {
88 &::DEBUG("list longcut called.");
91 } elsif ($what =~ /^(expire|text|desc)(\s+(.*))?$/i) {
94 my($arg1,$arg2) = split(/\s+/, $3, 2);
95 &set("$arg1 $1 $arg2");
97 } elsif ($what =~ /^help(\s+(.*))?$/i) {
100 } elsif ($what =~ /^(un)?notify$/i) {
101 my $state = ($1) ? 0 : 1;
102 &::DEBUG("chan => $chan");
103 &::DEBUG("::chan => $::chan");
105 # todo: don't notify even if "news" is called.
106 if (!&::IsChanConf("newsNotifyAll")) {
107 &::msg($::who, "not available for this channel or disabled altogether.");
111 my $t = $::newsuser{$chan}{$::who};
112 if ($state) { # state = 1
113 if (defined $t and ($t == 0 or $t == -1)) {
114 &::msg($::who, "enabled notify.");
115 delete $::newsuser{$chan}{$::who};
118 &::msg($::who, "already enabled.");
121 my $x = $::newsuser{$chan}{$::who};
122 if (defined $x and ($x == 0 or $x == -1)) {
123 &::msg($::who, "notify already disabled");
126 $::newsuser{$chan}{$::who} = -1;
127 &::msg($::who, "notify is now disabled.");
131 &::DEBUG("could not parse '$what'.");
132 &::msg($::who, "unknown command: $what");
137 my $file = "$::bot_base_dir/blootbot-news.txt";
143 &::DEBUG("readNews: fileno exists, should never happen.");
154 # todo: allow commands.
156 if (/^[\s\t]+(\S+):[\s\t]+(.*)$/) {
157 if (!defined $item) {
158 &::DEBUG("!defined item, never happen!");
162 $::news{$chan}{$item}{$1} = $2;
166 # U <chan> <nick> <time>
167 if (/^U\s+(\S+)\s+(\S+)\s+(\d+)$/) {
168 $::newsuser{$1}{$2} = $3;
173 if (/^(\S+)[\s\t]+(.*)$/) {
181 &::status("News: Read $ci items for ".scalar(keys %::news)
182 ." chans, $cu users cache");
186 if (!scalar keys %::news) {
187 &::DEBUG("wN: nothing to write.");
191 my $file = "$::bot_base_dir/blootbot-news.txt";
194 &::ERROR("fileno NEWS exists, should never happen.");
198 # todo: add commands to output file.
200 my($cc,$ci,$cu) = (0,0,0);
202 open(NEWS, ">$file");
203 foreach $chan (sort keys %::news) {
204 $c = scalar keys %{ $::news{$chan} };
208 foreach $item (sort keys %{ $::news{$chan} }) {
209 $c = scalar keys %{ $::news{$chan}{$item} };
213 print NEWS "$chan $item\n";
214 foreach $what (sort keys %{ $::news{$chan}{$item} }) {
215 print NEWS " $what: $::news{$chan}{$item}{$what}\n";
221 # todo: show how many users we wrote down.
222 if (&::getChanConfList("newsKeepRead")) {
223 # old users are removed in newsFlush(), perhaps it should be
226 foreach $chan (sort keys %::newsuser) {
228 foreach (sort keys %{ $::newsuser{$chan} }) {
229 print NEWS "U $chan $_ $::newsuser{$chan}{$_}\n";
237 &::status("News: Wrote $ci items for $cc chans, $cu user cache.");
243 if (!defined $chan or !defined $str or $str =~ /^\s*$/) {
248 if (length $str > 64) {
249 &::msg($::who, "That's not really an item (>64chars)");
253 if (exists $::news{$chan}{$str}{Time}) {
254 &::msg($::who, "'$str' for $chan already exists!");
258 $::news{$chan}{$str}{Time} = time();
259 my $expire = &::getChanConfDefault("newsDefaultExpire",7);
260 $::news{$chan}{$str}{Expire} = time() + $expire*60*60*24;
261 $::news{$chan}{$str}{Author} = $::who;
263 my $agestr = &::Time2String($::news{$chan}{$str}{Expire} - time() );
264 my $item = &newsS2N($str);
265 &::msg($::who, "Added '\037$str\037' at [".localtime(time).
266 "] by \002$::who\002 for item #\002$item\002.");
267 &::msg($::who, "Now do 'news text $item <your_description>'");
268 &::msg($::who, "This item will expire at \002".
269 localtime($::news{$chan}{$str}{Expire})."\002 [$agestr from now] "
279 if (!defined $what) {
284 if ($what =~ /^\d+$/) {
285 my $count = scalar keys %{ $::news{$chan} };
287 &::msg($::who, "No news for $chan.");
291 if ($what > $count or $what < 0) {
292 &::msg($::who, "$what is out of range (max $count)");
296 $item = &getNewsItem($what);
297 $what = $item; # hack hack hack.
300 $_ = &getNewsItem($what); # hack hack hack.
301 $what = $_ if (defined $_);
303 if (!exists $::news{$chan}{$what}) {
305 foreach (keys %{ $::news{$chan} }) {
306 next unless (/\Q$what\E/);
310 if (!scalar @found) {
311 &::msg($::who, "could not find $what.");
315 if (scalar @found > 1) {
316 &::msg($::who, "too many matches for $what.");
321 &::DEBUG("del: str: guessed what => $what");
325 if (exists $::news{$chan}{$what}) {
327 $auth++ if ($::who eq $::news{$chan}{$what}{Author});
328 $auth++ if (&::IsFlag("o"));
331 # todo: show when it'll expire.
332 &::msg($::who, "Sorry, you cannot remove items; just let them expire on their own.");
336 &::msg($::who, "ok, deleted '$what' from \002$chan\002...");
337 delete $::news{$chan}{$what};
339 &::msg($::who, "error: not found $what in news for $chan.");
344 if (!scalar keys %{ $::news{$chan} }) {
345 &::msg($::who, "No News for \002$chan\002.");
349 if (&::IsChanConf("newsKeepRead")) {
350 my $x = $::newsuser{$chan}{$::who};
352 if (defined $x and ($x == 0 or $x == -1)) {
353 &::DEBUG("not updating time for $::who.");
355 $::newsuser{$chan}{$::who} = time();
359 my $count = scalar keys %{ $::news{$chan} };
360 &::msg($::who, "|==== News for \002$chan\002: ($count items)");
362 foreach (keys %{ $::news{$chan} }) {
363 my $t = $::news{$chan}{$_}{Time};
364 $newest = $t if ($t > $newest);
366 my $timestr = &::Time2String(time() - $newest);
367 &::msg($::who, "|= Last updated $timestr ago.");
368 &::msg($::who, " \037Num\037 \037Item ".(" "x40)." \037");
371 foreach ( &getNewsAll() ) {
373 my $setby = $::news{$chan}{$subtopic}{Author};
375 if (!defined $subtopic) {
376 &::DEBUG("warn: subtopic == undef.");
380 # todo: show request stats aswell.
381 &::msg($::who, sprintf("\002[\002%2d\002]\002 %s",
386 &::msg($::who, "|= End of News.");
387 &::msg($::who, "use 'news read <#>' or 'news read <keyword>'");
393 if (!defined $chan or !defined $str or $str =~ /^\s*$/) {
394 &::help("news read");
398 if (!scalar keys %{ $::news{$chan} }) {
399 &::msg($::who, "No News for \002$chan\002.");
403 my $item = &getNewsItem($str);
404 if (!defined $item or !scalar keys %{ $::news{$chan}{$item} }) {
405 &::msg($::who, "No news item called '$str'");
409 if (!exists $::news{$chan}{$item}{Text}) {
410 &::msg($::who, "Someone forgot to add info to this news item");
414 my $t = localtime( $::news{$chan}{$item}{Time} );
415 my $a = $::news{$chan}{$item}{Author};
416 my $text = $::news{$chan}{$item}{Text};
417 my $num = &newsS2N($item);
418 my $rwho = $::news{$chan}{$item}{Request_By} || $::who;
419 my $rcount = $::news{$chan}{$item}{Request_Count} || 0;
421 if (length $text < $::param{maxKeySize}) {
422 &::DEBUG("NEWS: Possible news->factoid redirection.");
423 my $f = &::getFactoid($text);
426 &::DEBUG("NEWS: ok, $text is factoid redirection.");
427 $f =~ s/^<REPLY>\s*//i; # anything else?
432 &::msg($::who, "+- News \002$chan\002 #$num: \037$item\037");
433 &::msg($::who, "| Added by $a at $t");
434 &::msg($::who, "| Requested $rcount times, last by $rwho");
435 &::msg($::who, $text);
437 $::news{$chan}{$item}{'Request_By'} = $::who;
438 $::news{$chan}{$item}{'Request_Time'} = time();
439 $::news{$chan}{$item}{'Request_Count'}++;
443 my($item, $str) = split /\s+/, $_[0], 2;
445 if (!defined $item or $item eq "" or $str =~ /^\s*$/) {
450 my $news = &getNewsItem($item);
452 if (!defined $news) {
453 &::DEBUG("error: mod: news == undefined.");
456 my $nnews = $::news{$chan}{$news}{Text};
457 my $mod_news = $news;
458 my $mod_nnews = $nnews;
461 if ($str =~ m|^\s*s([/,#\|])(.+?)\1(.*?)\1([a-z]*);?\s*$|) {
462 my ($delim, $op, $np, $flags) = ($1,$2,$3,$4);
464 if ($flags !~ /^(g)?$/) {
465 &::msg($::who, "error: Invalid flags to regex.");
469 ### TODO: use m### to make code safe!
470 # todo: make code safer.
472 # todo: use eval to deal with flags easily.
474 $done++ if (!$done and $mod_news =~ s/\Q$op\E/$np/);
475 $done++ if (!$done and $mod_nnews =~ s/\Q$op\E/$np/);
476 } elsif ($flags eq "g") {
477 $done++ if ($mod_news =~ s/\Q$op\E/$np/g);
478 $done++ if ($mod_nnews =~ s/\Q$op\E/$np/g);
482 &::msg($::who, "warning: regex not found in news.");
486 if ($mod_news ne $news) { # news item.
487 if (exists $::news{$chan}{$mod_news}) {
488 &::msg($::who, "item '$mod_news' already exists.");
492 &::msg($::who, "Moving item '$news' to '$mod_news' with SAR s/$op/$np/.");
493 foreach (keys %{ $::news{$chan}{$news} }) {
494 $::news{$chan}{$mod_news}{$_} = $::news{$chan}{$news}{$_};
495 delete $::news{$chan}{$news}{$_};
498 delete $::news{$chan}{$news};
501 if ($mod_nnews ne $nnews) { # news Text/Description.
502 &::msg($::who, "Changing text for '$news' SAR s/$op/$np/.");
503 if ($mod_news ne $news) {
504 $::news{$chan}{$mod_news}{Text} = $mod_nnews;
506 $::news{$chan}{$news}{Text} = $mod_nnews;
512 &::msg($::who, "error: that regex failed ;(");
516 &::msg($::who, "error: Invalid regex. Try s/1/2/, s#3#4#...");
521 $args =~ /^(\S+)\s+(\S+)\s+(.*)$/;
522 my($item, $what, $value) = ($1,$2,$3);
524 &::DEBUG("set called.");
531 &::DEBUG("item => '$item'.");
532 my $news = &getNewsItem($item);
533 &::DEBUG("news => '$news'");
535 if (!defined $news) {
536 &::msg($::who, "Could not find item '$item' substring or # in news list.");
540 # list all values for chan.
541 if (!defined $what) {
547 my @elements = ("Expire","Text");
548 foreach (@elements) {
549 next unless ($what =~ /^$_$/i);
556 &::msg($::who, "Invalid set. Try: @elements");
561 if (!defined $value) {
566 if (!exists $::news{$chan}{$news}) {
567 &::msg($::who, "news '$news' does not exist");
571 if ($what eq "Expire") {
572 # todo: use do_set().
575 my $plus = ($value =~ s/^\+//g);
576 while ($value =~ s/^(\d+)(\S*)\s*//) {
577 my($int,$unit) = ($1,$2);
578 $time += $int if ($unit =~ /^s(ecs?)?$/i);
579 $time += $int*60 if ($unit =~ /^m(in(utes?)?)?$/i);
580 $time += $int*60*60 if ($unit =~ /^h(ours?)?$/i);
581 $time += $int*60*60*24 if (!$unit or $unit =~ /^d(ays?)?$/i);
582 $time += $int*60*60*24*7 if ($unit =~ /^w(eeks?)?$/i);
583 $time += $int*60*60*24*30 if ($unit =~ /^mon(th)?$/i);
586 if ($value =~ s/^never$//i) {
593 # from creation of item.
594 $time += $::news{$chan}{$news}{Time};
597 if (!$time or ($value and $value !~ /^never$/i)) {
598 &::DEBUG("set: Expire... need to parse.");
603 &::msg($::who, "Set never expire for \002$item\002." );
604 } elsif ($time < -1) {
605 &::DEBUG("time should never be negative ($time).");
608 &::msg($::who, "Set expire for \002$item\002, to ".
609 localtime($time) ." [".&::Time2String($time - time())."]" );
611 if (time() > $time) {
612 &::DEBUG("hrm... time() > $time, should expire.");
617 $::news{$chan}{$news}{Expire} = $time;
623 &::DEBUG("who => '$::who'");
624 my $author = $::news{$chan}{$news}{Author};
625 $auth++ if ($::who eq $author);
626 $auth++ if (&::IsFlag("o"));
627 if (!defined $author) {
628 &::DEBUG("news{$chan}{$news}{Author} is not defined! auth'd anyway");
629 $::news{$chan}{$news}{Author} = $::who;
635 # todo: show when it'll expire.
636 &::msg($::who, "Sorry, you cannot set items. (author $author owns it)");
640 # todo: clean this up.
641 my $old = $::news{$chan}{$news}{$what};
643 &::DEBUG("old => $old.");
645 $::news{$chan}{$news}{$what} = $value;
646 &::msg($::who, "Setting [$chan]/{$news}/<$what> to '$value'.");
650 my($tchan, $flag) = @_;
652 $chan ||= $tchan; # hack hack hack.
654 # todo: if chan = undefined, guess.
655 if (!exists $::news{$chan}) {
656 &::msg($::who, "invalid chan $chan");
660 my $t = $::newsuser{$chan}{$::who};
661 if (defined $t and ($t == 0 or $t == -1)) {
663 &::msg($::who, "if you want to read news, try /msg $::ident news or /msg $::ident news notify");
665 &::DEBUG("not displaying any new news for $::who");
671 my $x = &::IsChanConf("newsNotifyAll");
672 if (&::IsChanConf("newsNotifyAll") and !defined $t) {
677 &::DEBUG("something went really wrong.");
678 &::msg($::who, "something went really wrong.");
683 foreach (keys %{ $::news{$chan} }) {
684 next if (!defined $t);
685 next if ($t > $::news{$chan}{$_}{Time});
690 # !scalar @new, $flag
691 if (!scalar @new and $flag) {
692 &::msg($::who, "no new news for $chan.");
696 # scalar @new, !$flag
697 my $unread = scalar @new;
698 my $total = scalar keys %{ $::news{$chan} };
700 return unless ($unread);
702 my $reply = "There are unread news in $chan ($unread unread, $total total). /msg $::ident news latest.";
703 $reply .= " If you don't want further news notification, /msg $::ident news unnotify" if ($unread == $total);
704 &::msg($::who, $reply);
711 &::msg($::who, "+==== New news for \002$chan\002 ($unread new; $total total):");
713 if ($::newsuser{$chan}{$::who}) {
714 my $timestr = &::Time2String( time() - $::newsuser{$chan}{$::who} );
715 &::msg($::who, "|= Last time read $timestr ago");
720 my $i = &newsS2N($_);
724 for (my $i=0; $i<=scalar(@sorted); $i++) {
725 my $news = $sorted[$i];
726 next unless (defined $news);
728 my $age = time() - $::news{$chan}{$news}{Time};
729 &::msg($::who, sprintf("\002[\002%2d\002]\002 %s",
731 # $i, $_, &::Time2String($age) ) );
734 &::msg($::who, "|= to read, do 'news read <#>' or 'news read <keyword>'");
736 # lame hack to prevent dupes if we just ignore it.
737 my $x = $::newsuser{$chan}{$::who};
738 if (defined $x and ($x == 0 or $x == -1)) {
739 &::DEBUG("not updating time for $::who. (2)");
741 $::newsuser{$chan}{$::who} = time();
752 foreach (keys %{ $::news{$chan} }) {
753 $time{ $::news{$chan}{$_}{Time} } = $_;
757 foreach (sort { $a <=> $b } keys %time) {
758 push(@items, $time{$_});
771 foreach (keys %{ $::news{$chan} }) {
772 my $t = $::news{$chan}{$_}{Time};
774 if (!defined $t or $t !~ /^\d+$/) {
775 &::DEBUG("warn: t is undefined for news{$chan}{$_}{Time}; removing item.");
776 delete $::news{$chan}{$_};
783 foreach (sort { $a <=> $b } keys %time) {
785 return $item if ($time{$_} eq $what);
788 &::DEBUG("newsS2N($what): failed...");
796 foreach (keys %{ $::news{$chan} }) {
797 my $t = $::news{$chan}{$_}{Time};
799 if (!defined $t or $t !~ /^\d+$/) {
800 &::DEBUG("warn: t is undefined for news{$chan}{$_}{Time}; removing item.");
801 delete $::news{$chan}{$_};
808 # number to string resolution.
809 if ($what =~ /^\d+$/) {
810 foreach (sort { $a <=> $b } keys %time) {
812 return $time{$_} if ($item == $what);
816 # partial string to full string resolution
817 # in some cases, string->number resolution.
821 foreach (sort { $a <=> $b } keys %time) {
823 # $no = $item if ($time{$_} eq $what);
824 if ($time{$_} eq $what) {
829 push(@items, $time{$_}) if ($time{$_} =~ /\Q$what\E/i);
832 if (defined $no and !@items) {
833 &::DEBUG("string->number resolution: $what->$no.");
837 if (scalar @items > 1) {
838 &::DEBUG("Multiple matches, not guessing.");
839 &::msg($::who, "Multiple matches, not guessing.");
843 &::DEBUG("gNI: part_string->full_string: $what->$items[0]");
845 &::DEBUG("gNI: Guessed '$items[0]'.");
848 &::DEBUG("gNI: No match.");
853 &::ERROR("getNewsItem: Should not happen (what = $what)");
858 my($what,$value) = @_;
860 if (!defined $chan) {
861 &::DEBUG("do_set: chan not defined.");
865 if (!defined $what or $what =~ /^\s*$/) {
866 &::DEBUG("what $what is not defined.");
869 if (!defined $value or $value =~ /^\s*$/) {
870 &::DEBUG("value $value is not defined.");
874 &::DEBUG("do_set: TODO...");