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 &::DEBUG("read shortcut called.");
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 =~ /^(un)?notify$/i) {
106 my $state = ($1) ? 0 : 1;
108 # todo: don't notify even if "news" is called.
109 if (!&::IsChanConf("newsNotifyAll")) {
110 &::DEBUG("chan => $chan, ::chan => $::chan.");
111 &::notice($::who, "not available for this channel or disabled altogether.");
115 my $t = $::newsuser{$chan}{$::who};
116 if ($state) { # state = 1
117 if (defined $t and ($t == 0 or $t == -1)) {
118 &::notice($::who, "enabled notify.");
119 delete $::newsuser{$chan}{$::who};
122 &::notice($::who, "already enabled.");
125 my $x = $::newsuser{$chan}{$::who};
126 if (defined $x and ($x == 0 or $x == -1)) {
127 &::notice($::who, "notify already disabled");
130 $::newsuser{$chan}{$::who} = -1;
131 &::notice($::who, "notify is now disabled.");
135 &::DEBUG("could not parse '$what'.");
136 &::notice($::who, "unknown command: $what");
141 my $file = "$::bot_base_dir/blootbot-news.txt";
147 &::DEBUG("readNews: fileno exists, should never happen.");
158 # todo: allow commands.
160 if (/^[\s\t]+(\S+):[\s\t]+(.*)$/) {
161 if (!defined $item) {
162 &::DEBUG("!defined item, never happen!");
166 $::news{$chan}{$item}{$1} = $2;
170 # U <chan> <nick> <time>
171 if (/^U\s+(\S+)\s+(\S+)\s+(\d+)$/) {
172 $::newsuser{$1}{$2} = $3;
177 if (/^(\S+)[\s\t]+(.*)$/) {
185 &::status("News: Read $ci items for ".scalar(keys %::news)
186 ." chans, $cu users cache");
190 if (!scalar keys %::news) {
191 &::DEBUG("wN: nothing to write.");
195 my $file = "$::bot_base_dir/blootbot-news.txt";
198 &::ERROR("fileno NEWS exists, should never happen.");
202 # todo: add commands to output file.
204 my($cc,$ci,$cu) = (0,0,0);
206 open(NEWS, ">$file");
207 foreach $chan (sort keys %::news) {
208 $c = scalar keys %{ $::news{$chan} };
212 foreach $item (sort keys %{ $::news{$chan} }) {
213 $c = scalar keys %{ $::news{$chan}{$item} };
217 print NEWS "$chan $item\n";
218 foreach $what (sort keys %{ $::news{$chan}{$item} }) {
219 print NEWS " $what: $::news{$chan}{$item}{$what}\n";
225 # todo: show how many users we wrote down.
226 if (&::getChanConfList("newsKeepRead")) {
227 # old users are removed in newsFlush(), perhaps it should be
230 foreach $chan (sort keys %::newsuser) {
232 foreach (sort keys %{ $::newsuser{$chan} }) {
233 print NEWS "U $chan $_ $::newsuser{$chan}{$_}\n";
241 &::status("News: Wrote $ci items for $cc chans, $cu user cache.");
247 if (!defined $chan or !defined $str or $str =~ /^\s*$/) {
252 if (length $str > 64) {
253 &::notice($::who, "That's not really an item (>64chars)");
257 if (exists $::news{$chan}{$str}{Time}) {
258 &::notice($::who, "'$str' for $chan already exists!");
262 $::news{$chan}{$str}{Time} = time();
263 my $expire = &::getChanConfDefault("newsDefaultExpire",7);
264 $::news{$chan}{$str}{Expire} = time() + $expire*60*60*24;
265 $::news{$chan}{$str}{Author} = $::who;
267 my $agestr = &::Time2String($::news{$chan}{$str}{Expire} - time() );
268 my $item = &newsS2N($str);
269 &::notice($::who, "Added '\037$str\037' at [".localtime(time).
270 "] by \002$::who\002 for item #\002$item\002.");
271 &::notice($::who, "Now do 'news text $item <your_description>'");
272 &::notice($::who, "This item will expire at \002".
273 localtime($::news{$chan}{$str}{Expire})."\002 [$agestr from now] "
283 if (!defined $what) {
288 if ($what =~ /^\d+$/) {
289 my $count = scalar keys %{ $::news{$chan} };
291 &::notice($::who, "No news for $chan.");
295 if ($what > $count or $what < 0) {
296 &::notice($::who, "$what is out of range (max $count)");
300 $item = &getNewsItem($what);
301 $what = $item; # hack hack hack.
304 $_ = &getNewsItem($what); # hack hack hack.
305 $what = $_ if (defined $_);
307 if (!exists $::news{$chan}{$what}) {
309 foreach (keys %{ $::news{$chan} }) {
310 next unless (/\Q$what\E/);
314 if (!scalar @found) {
315 &::notice($::who, "could not find $what.");
319 if (scalar @found > 1) {
320 &::notice($::who, "too many matches for $what.");
325 &::DEBUG("del: str: guessed what => $what");
329 if (exists $::news{$chan}{$what}) {
331 $auth++ if ($::who eq $::news{$chan}{$what}{Author});
332 $auth++ if (&::IsFlag("o"));
335 # todo: show when it'll expire.
336 &::notice($::who, "Sorry, you cannot remove items; just let them expire on their own.");
340 &::notice($::who, "ok, deleted '$what' from \002$chan\002...");
341 delete $::news{$chan}{$what};
343 &::notice($::who, "error: not found $what in news for $chan.");
348 if (!scalar keys %{ $::news{$chan} }) {
349 &::notice($::who, "No News for \002$chan\002.");
353 if (&::IsChanConf("newsKeepRead")) {
354 my $x = $::newsuser{$chan}{$::who};
356 if (defined $x and ($x == 0 or $x == -1)) {
357 &::DEBUG("not updating time for $::who.");
359 $::newsuser{$chan}{$::who} = time();
363 my $count = scalar keys %{ $::news{$chan} };
364 &::notice($::who, "|==== News for \002$chan\002: ($count items)");
366 foreach (keys %{ $::news{$chan} }) {
367 my $t = $::news{$chan}{$_}{Time};
368 $newest = $t if ($t > $newest);
370 my $timestr = &::Time2String(time() - $newest);
371 &::notice($::who, "|= Last updated $timestr ago.");
372 &::notice($::who, " \037Num\037 \037Item ".(" "x40)." \037");
375 foreach ( &getNewsAll() ) {
377 my $setby = $::news{$chan}{$subtopic}{Author};
379 if (!defined $subtopic) {
380 &::DEBUG("warn: subtopic == undef.");
384 # todo: show request stats aswell.
385 &::notice($::who, sprintf("\002[\002%2d\002]\002 %s",
390 &::notice($::who, "|= End of News.");
391 &::notice($::who, "use 'news read <#>' or 'news read <keyword>'");
397 if (!defined $chan or !defined $str or $str =~ /^\s*$/) {
398 &::help("news read");
402 if (!scalar keys %{ $::news{$chan} }) {
403 &::notice($::who, "No News for \002$chan\002.");
407 my $item = &getNewsItem($str);
408 if (!defined $item or !scalar keys %{ $::news{$chan}{$item} }) {
409 &::notice($::who, "No news item called '$str'");
413 if (!exists $::news{$chan}{$item}{Text}) {
414 &::notice($::who, "Someone forgot to add info to this news item");
418 my $t = localtime( $::news{$chan}{$item}{Time} );
419 my $a = $::news{$chan}{$item}{Author};
420 my $text = $::news{$chan}{$item}{Text};
421 my $num = &newsS2N($item);
422 my $rwho = $::news{$chan}{$item}{Request_By} || $::who;
423 my $rcount = $::news{$chan}{$item}{Request_Count} || 0;
425 if (length $text < $::param{maxKeySize}) {
426 &::DEBUG("NEWS: Possible news->factoid redirection.");
427 my $f = &::getFactoid($text);
430 &::DEBUG("NEWS: ok, $text is factoid redirection.");
431 $f =~ s/^<REPLY>\s*//i; # anything else?
436 &::notice($::who, "+- News \002$chan\002 #$num: \037$item\037");
437 &::notice($::who, "| Added by $a at $t");
438 &::notice($::who, "| Requested $rcount times, last by $rwho") if ($rcount);
439 &::notice($::who, $text);
441 $::news{$chan}{$item}{'Request_By'} = $::who;
442 $::news{$chan}{$item}{'Request_Time'} = time();
443 $::news{$chan}{$item}{'Request_Count'}++;
447 my($item, $str) = split /\s+/, $_[0], 2;
449 if (!defined $item or $item eq "" or $str =~ /^\s*$/) {
454 my $news = &getNewsItem($item);
456 if (!defined $news) {
457 &::DEBUG("error: mod: news == undefined.");
460 my $nnews = $::news{$chan}{$news}{Text};
461 my $mod_news = $news;
462 my $mod_nnews = $nnews;
465 if ($str =~ m|^\s*s([/,#\|])(.+?)\1(.*?)\1([a-z]*);?\s*$|) {
466 my ($delim, $op, $np, $flags) = ($1,$2,$3,$4);
468 if ($flags !~ /^(g)?$/) {
469 &::notice($::who, "error: Invalid flags to regex.");
473 ### TODO: use m### to make code safe!
474 # todo: make code safer.
476 # todo: use eval to deal with flags easily.
478 $done++ if (!$done and $mod_news =~ s/\Q$op\E/$np/);
479 $done++ if (!$done and $mod_nnews =~ s/\Q$op\E/$np/);
480 } elsif ($flags eq "g") {
481 $done++ if ($mod_news =~ s/\Q$op\E/$np/g);
482 $done++ if ($mod_nnews =~ s/\Q$op\E/$np/g);
486 &::notice($::who, "warning: regex not found in news.");
490 if ($mod_news ne $news) { # news item.
491 if (exists $::news{$chan}{$mod_news}) {
492 &::notice($::who, "item '$mod_news' already exists.");
496 &::notice($::who, "Moving item '$news' to '$mod_news' with SAR s/$op/$np/.");
497 foreach (keys %{ $::news{$chan}{$news} }) {
498 $::news{$chan}{$mod_news}{$_} = $::news{$chan}{$news}{$_};
499 delete $::news{$chan}{$news}{$_};
502 delete $::news{$chan}{$news};
505 if ($mod_nnews ne $nnews) { # news Text/Description.
506 &::notice($::who, "Changing text for '$news' SAR s/$op/$np/.");
507 if ($mod_news ne $news) {
508 $::news{$chan}{$mod_news}{Text} = $mod_nnews;
510 $::news{$chan}{$news}{Text} = $mod_nnews;
516 &::notice($::who, "error: that regex failed ;(");
520 &::notice($::who, "error: Invalid regex. Try s/1/2/, s#3#4#...");
525 $args =~ /^(\S+)\s+(\S+)\s+(.*)$/;
526 my($item, $what, $value) = ($1,$2,$3);
528 &::DEBUG("set called.");
535 &::DEBUG("item => '$item'.");
536 my $news = &getNewsItem($item);
537 &::DEBUG("news => '$news'");
539 if (!defined $news) {
540 &::notice($::who, "Could not find item '$item' substring or # in news list.");
544 # list all values for chan.
545 if (!defined $what) {
551 my @elements = ("Expire","Text");
552 foreach (@elements) {
553 next unless ($what =~ /^$_$/i);
560 &::notice($::who, "Invalid set. Try: @elements");
565 if (!defined $value) {
570 if (!exists $::news{$chan}{$news}) {
571 &::notice($::who, "news '$news' does not exist");
575 if ($what eq "Expire") {
576 # todo: use do_set().
579 my $plus = ($value =~ s/^\+//g);
580 while ($value =~ s/^(\d+)(\S*)\s*//) {
581 my($int,$unit) = ($1,$2);
582 $time += $int if ($unit =~ /^s(ecs?)?$/i);
583 $time += $int*60 if ($unit =~ /^m(in(utes?)?)?$/i);
584 $time += $int*60*60 if ($unit =~ /^h(ours?)?$/i);
585 $time += $int*60*60*24 if (!$unit or $unit =~ /^d(ays?)?$/i);
586 $time += $int*60*60*24*7 if ($unit =~ /^w(eeks?)?$/i);
587 $time += $int*60*60*24*30 if ($unit =~ /^mon(th)?$/i);
590 if ($value =~ s/^never$//i) {
597 # from creation of item.
598 $time += $::news{$chan}{$news}{Time};
601 if (!$time or ($value and $value !~ /^never$/i)) {
602 &::DEBUG("set: Expire... need to parse.");
607 &::notice($::who, "Set never expire for \002$item\002." );
608 } elsif ($time < -1) {
609 &::DEBUG("time should never be negative ($time).");
612 &::notice($::who, "Set expire for \002$item\002, to ".
613 localtime($time) ." [".&::Time2String($time - time())."]" );
615 if (time() > $time) {
616 &::DEBUG("hrm... time() > $time, should expire.");
621 $::news{$chan}{$news}{Expire} = $time;
627 &::DEBUG("who => '$::who'");
628 my $author = $::news{$chan}{$news}{Author};
629 $auth++ if ($::who eq $author);
630 $auth++ if (&::IsFlag("o"));
631 if (!defined $author) {
632 &::DEBUG("news{$chan}{$news}{Author} is not defined! auth'd anyway");
633 $::news{$chan}{$news}{Author} = $::who;
639 # todo: show when it'll expire.
640 &::notice($::who, "Sorry, you cannot set items. (author $author owns it)");
644 # todo: clean this up.
645 my $old = $::news{$chan}{$news}{$what};
647 &::DEBUG("old => $old.");
649 $::news{$chan}{$news}{$what} = $value;
650 &::notice($::who, "Setting [$chan]/{$news}/<$what> to '$value'.");
654 my($tchan, $flag) = @_;
656 $chan ||= $tchan; # hack hack hack.
658 # todo: if chan = undefined, guess.
659 if (!exists $::news{$chan}) {
660 &::notice($::who, "invalid chan $chan");
664 my $t = $::newsuser{$chan}{$::who};
665 if (defined $t and ($t == 0 or $t == -1)) {
667 &::notice($::who, "if you want to read news, try /msg $::ident news or /msg $::ident news notify");
669 &::DEBUG("not displaying any new news for $::who");
674 my $x = &::IsChanConf("newsNotifyAll");
675 if (&::IsChanConf("newsNotifyAll") and !defined $t) {
680 &::DEBUG("something went really wrong.");
681 &::DEBUG("chan => $chan, ::chan => $::chan");
682 &::notice($::who, "something went really wrong.");
687 foreach (keys %{ $::news{$chan} }) {
688 next if (!defined $t);
689 next if ($t > $::news{$chan}{$_}{Time});
691 # don't list new items if they don't have Text.
692 if (!exists $::news{$chan}{$_}{Text}) {
693 &::WARN("news: news{$chan}{$_}{Text} undef.");
700 # !scalar @new, $flag
701 if (!scalar @new and $flag) {
702 &::notice($::who, "no new news for $chan.");
706 # scalar @new, !$flag
707 my $unread = scalar @new;
708 my $total = scalar keys %{ $::news{$chan} };
710 return unless ($unread);
712 my $reply = "There are unread news in $chan ($unread unread, $total total). /msg $::ident news latest.";
713 $reply .= " If you don't want further news notification, /msg $::ident news unnotify" if ($unread == $total);
714 &::notice($::who, $reply);
721 &::notice($::who, "+==== New news for \002$chan\002 ($unread new; $total total):");
723 my $t = $::newsuser{$chan}{$::who};
724 if (defined $t and $t > 1) {
725 my $timestr = &::Time2String( time() - $t );
726 &::notice($::who, "|= Last time read $timestr ago");
731 my $i = &newsS2N($_);
735 for (my $i=0; $i<=scalar(@sorted); $i++) {
736 my $news = $sorted[$i];
737 next unless (defined $news);
739 my $age = time() - $::news{$chan}{$news}{Time};
740 &::notice($::who, sprintf("\002[\002%2d\002]\002 %s",
742 # $i, $_, &::Time2String($age) ) );
745 &::notice($::who, "|= to read, do 'news read <#>' or 'news read <keyword>'");
747 # lame hack to prevent dupes if we just ignore it.
748 my $x = $::newsuser{$chan}{$::who};
749 if (defined $x and ($x == 0 or $x == -1)) {
750 &::DEBUG("not updating time for $::who. (2)");
752 $::newsuser{$chan}{$::who} = time();
763 foreach (keys %{ $::news{$chan} }) {
764 $time{ $::news{$chan}{$_}{Time} } = $_;
768 foreach (sort { $a <=> $b } keys %time) {
769 push(@items, $time{$_});
782 foreach (keys %{ $::news{$chan} }) {
783 my $t = $::news{$chan}{$_}{Time};
785 if (!defined $t or $t !~ /^\d+$/) {
786 &::DEBUG("warn: t is undefined for news{$chan}{$_}{Time}; removing item.");
787 delete $::news{$chan}{$_};
794 foreach (sort { $a <=> $b } keys %time) {
796 return $item if ($time{$_} eq $what);
799 &::DEBUG("newsS2N($what): failed...");
807 foreach (keys %{ $::news{$chan} }) {
808 my $t = $::news{$chan}{$_}{Time};
810 if (!defined $t or $t !~ /^\d+$/) {
811 &::DEBUG("warn: t is undefined for news{$chan}{$_}{Time}; removing item.");
812 delete $::news{$chan}{$_};
819 # number to string resolution.
820 if ($what =~ /^\d+$/) {
821 foreach (sort { $a <=> $b } keys %time) {
823 return $time{$_} if ($item == $what);
827 # partial string to full string resolution
828 # in some cases, string->number resolution.
832 foreach (sort { $a <=> $b } keys %time) {
834 # $no = $item if ($time{$_} eq $what);
835 if ($time{$_} eq $what) {
840 push(@items, $time{$_}) if ($time{$_} =~ /\Q$what\E/i);
843 if (defined $no and !@items) {
844 &::DEBUG("string->number resolution: $what->$no.");
848 if (scalar @items > 1) {
849 &::DEBUG("Multiple matches, not guessing.");
850 &::notice($::who, "Multiple matches, not guessing.");
854 &::DEBUG("gNI: part_string->full_string: $what->$items[0]");
856 &::DEBUG("gNI: Guessed '$items[0]'.");
859 &::DEBUG("gNI: No match.");
864 &::ERROR("getNewsItem: Should not happen (what = $what)");
869 my($what,$value) = @_;
871 if (!defined $chan) {
872 &::DEBUG("do_set: chan not defined.");
876 if (!defined $what or $what =~ /^\s*$/) {
877 &::DEBUG("what $what is not defined.");
880 if (!defined $value or $value =~ /^\s*$/) {
881 &::DEBUG("value $value is not defined.");
885 &::DEBUG("do_set: TODO...");