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");
59 if (!defined $what or $what =~ /^\s*$/) {
64 if ($what =~ /^add(\s+(.*))?$/i) {
67 } elsif ($what =~ /^del(\s+(.*))?$/i) {
70 } elsif ($what =~ /^mod(\s+(.*))?$/i) {
73 } elsif ($what =~ /^set(\s+(.*))?$/i) {
76 } elsif ($what =~ /^(\d)$/i) {
77 &::DEBUG("read shortcut called.");
80 } elsif ($what =~ /^read(\s+(.*))?$/i) {
83 } elsif ($what =~ /^(latest|new)(\s+(.*))?$/i) {
84 &::DEBUG("latest called... hrm");
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;
103 # todo: don't notify even if "news" is called.
104 if (!&::IsChanConf("newsNotifyAll")) {
105 &::msg($::who, "not available for this channel or disabled altogether.");
109 my $t = $::newsuser{$chan}{$::who};
110 if ($state) { # state = 1
111 if (defined $t and ($t == 0 or $t == -1)) {
112 &::msg($::who, "enabled notify.");
113 delete $::newsuser{$chan}{$::who};
116 &::msg($::who, "already enabled.");
119 my $x = $::newsuser{$chan}{$::who};
120 if (defined $x and ($x == 0 or $x == -1)) {
121 &::msg($::who, "notify already disabled");
124 $::newsuser{$chan}{$::who} = -1;
125 &::msg($::who, "notify is now disabled.");
129 &::DEBUG("could not parse '$what'.");
130 &::msg($::who, "unknown command: $what");
135 my $file = "$::bot_base_dir/blootbot-news.txt";
141 &::DEBUG("readNews: fileno exists, should never happen.");
152 # todo: allow commands.
154 if (/^[\s\t]+(\S+):[\s\t]+(.*)$/) {
155 if (!defined $item) {
156 &::DEBUG("!defined item, never happen!");
160 $::news{$chan}{$item}{$1} = $2;
164 # U <chan> <nick> <time>
165 if (/^U\s+(\S+)\s+(\S+)\s+(\d+)$/) {
166 $::newsuser{$1}{$2} = $3;
171 if (/^(\S+)[\s\t]+(.*)$/) {
179 &::status("News: Read $ci items for ".scalar(keys %::news)
180 ." chans, $cu users cache");
184 if (!scalar keys %::news) {
185 &::DEBUG("wN: nothing to write.");
189 my $file = "$::bot_base_dir/blootbot-news.txt";
192 &::ERROR("fileno NEWS exists, should never happen.");
196 # todo: add commands to output file.
198 my($cc,$ci,$cu) = (0,0,0);
200 open(NEWS, ">$file");
201 foreach $chan (sort keys %::news) {
202 $c = scalar keys %{ $::news{$chan} };
206 foreach $item (sort keys %{ $::news{$chan} }) {
207 $c = scalar keys %{ $::news{$chan}{$item} };
211 print NEWS "$chan $item\n";
212 foreach $what (sort keys %{ $::news{$chan}{$item} }) {
213 print NEWS " $what: $::news{$chan}{$item}{$what}\n";
219 # todo: show how many users we wrote down.
220 if (&::getChanConfList("newsKeepRead")) {
221 # old users are removed in newsFlush(), perhaps it should be
224 foreach $chan (sort keys %::newsuser) {
226 foreach (sort keys %{ $::newsuser{$chan} }) {
227 print NEWS "U $chan $_ $::newsuser{$chan}{$_}\n";
235 &::status("News: Wrote $ci items for $cc chans, $cu user cache.");
241 if (!defined $chan or !defined $str or $str =~ /^\s*$/) {
246 if (length $str > 64) {
247 &::msg($::who, "That's not really an item (>64chars)");
251 if (exists $::news{$chan}{$str}{Time}) {
252 &::msg($::who, "'$str' for $chan already exists!");
256 $::news{$chan}{$str}{Time} = time();
257 my $expire = &::getChanConfDefault("newsDefaultExpire",7);
258 $::news{$chan}{$str}{Expire} = time() + $expire*60*60*24;
259 $::news{$chan}{$str}{Author} = $::who;
261 my $agestr = &::Time2String($::news{$chan}{$str}{Expire} - time() );
262 my $item = &newsS2N($str);
263 &::msg($::who, "Added '\037$str\037' at [".localtime(time).
264 "] by \002$::who\002 for item #\002$item\002.");
265 &::msg($::who, "Now do 'news text $item <your_description>'");
266 &::msg($::who, "This item will expire at \002".
267 localtime($::news{$chan}{$str}{Expire})."\002 [$agestr from now] "
277 if (!defined $what) {
282 if ($what =~ /^\d+$/) {
283 my $count = scalar keys %{ $::news{$chan} };
285 &::msg($::who, "No news for $chan.");
289 if ($what > $count or $what < 0) {
290 &::msg($::who, "$what is out of range (max $count)");
294 $item = &getNewsItem($what);
295 $what = $item; # hack hack hack.
298 $_ = &getNewsItem($what); # hack hack hack.
299 $what = $_ if (defined $_);
301 if (!exists $::news{$chan}{$what}) {
303 foreach (keys %{ $::news{$chan} }) {
304 next unless (/\Q$what\E/);
308 if (!scalar @found) {
309 &::msg($::who, "could not find $what.");
313 if (scalar @found > 1) {
314 &::msg($::who, "too many matches for $what.");
319 &::DEBUG("del: str: guessed what => $what");
323 if (exists $::news{$chan}{$what}) {
325 $auth++ if ($::who eq $::news{$chan}{$what}{Author});
326 $auth++ if (&::IsFlag("o"));
329 # todo: show when it'll expire.
330 &::msg($::who, "Sorry, you cannot remove items; just let them expire on their own.");
334 &::msg($::who, "ok, deleted '$what' from \002$chan\002...");
335 delete $::news{$chan}{$what};
337 &::msg($::who, "error: not found $what in news for $chan.");
342 if (!scalar keys %{ $::news{$chan} }) {
343 &::msg($::who, "No News for \002$chan\002.");
347 if (&::IsChanConf("newsKeepRead")) {
348 my $x = $::newsuser{$chan}{$::who};
350 if (defined $x and ($x == 0 or $x == -1)) {
351 &::DEBUG("not updating time for $::who.");
353 $::newsuser{$chan}{$::who} = time();
357 &::msg($::who, "|==== News for \002$chan\002:");
359 foreach (keys %{ $::news{$chan} }) {
360 my $t = $::news{$chan}{$_}{Time};
361 $newest = $t if ($t > $newest);
363 my $timestr = &::Time2String(time() - $newest);
364 &::msg($::who, "|= Last updated $timestr ago.");
365 &::msg($::who, " \037Num\037 \037Item ".(" "x40)." \037");
368 foreach ( &getNewsAll() ) {
370 my $setby = $::news{$chan}{$subtopic}{Author};
372 if (!defined $subtopic) {
373 &::DEBUG("warn: subtopic == undef.");
377 # todo: show request stats aswell.
378 &::msg($::who, sprintf("\002[\002%2d\002]\002 %s",
383 &::msg($::who, "|= End of News.");
384 &::msg($::who, "use 'news read <#>' or 'news read <keyword>'");
390 if (!defined $chan or !defined $str or $str =~ /^\s*$/) {
391 &::help("news read");
395 if (!scalar keys %{ $::news{$chan} }) {
396 &::msg($::who, "No News for \002$chan\002.");
400 my $item = &getNewsItem($str);
401 if (!defined $item or !scalar keys %{ $::news{$chan}{$item} }) {
402 &::msg($::who, "No news item called '$str'");
406 if (!exists $::news{$chan}{$item}{Text}) {
407 &::msg($::who, "Someone forgot to add info to this news item");
411 my $t = localtime( $::news{$chan}{$item}{Time} );
412 my $a = $::news{$chan}{$item}{Author};
413 my $text = $::news{$chan}{$item}{Text};
414 my $num = &newsS2N($item);
415 my $rwho = $::news{$chan}{$item}{Request_By} || $::who;
416 my $rcount = $::news{$chan}{$item}{Request_Count} || 0;
418 if (length $text < $::param{maxKeySize}) {
419 &::DEBUG("NEWS: Possible news->factoid redirection.");
420 my $f = &::getFactoid($text);
423 &::DEBUG("NEWS: ok, $text is factoid redirection.");
424 $f =~ s/^<REPLY>\s*//i; # anything else?
429 &::msg($::who, "+- News \002$chan\002 #$num: \037$item\037");
430 &::msg($::who, "| Added by $a at $t");
431 &::msg($::who, "| Requested $rcount times, last by $rwho");
432 &::msg($::who, $text);
434 $::news{$chan}{$item}{'Request_By'} = $::who;
435 $::news{$chan}{$item}{'Request_Time'} = time();
436 $::news{$chan}{$item}{'Request_Count'}++;
440 my($item, $str) = split /\s+/, $_[0], 2;
442 if (!defined $item or $item eq "" or $str =~ /^\s*$/) {
447 my $news = &getNewsItem($item);
449 if (!defined $news) {
450 &::DEBUG("error: mod: news == undefined.");
453 my $nnews = $::news{$chan}{$news}{Text};
454 my $mod_news = $news;
455 my $mod_nnews = $nnews;
458 if ($str =~ m|^\s*s([/,#\|])(.+?)\1(.*?)\1([a-z]*);?\s*$|) {
459 my ($delim, $op, $np, $flags) = ($1,$2,$3,$4);
461 if ($flags !~ /^(g)?$/) {
462 &::msg($::who, "error: Invalid flags to regex.");
466 ### TODO: use m### to make code safe!
467 # todo: make code safer.
469 # todo: use eval to deal with flags easily.
471 $done++ if (!$done and $mod_news =~ s/\Q$op\E/$np/);
472 $done++ if (!$done and $mod_nnews =~ s/\Q$op\E/$np/);
473 } elsif ($flags eq "g") {
474 $done++ if ($mod_news =~ s/\Q$op\E/$np/g);
475 $done++ if ($mod_nnews =~ s/\Q$op\E/$np/g);
479 &::msg($::who, "warning: regex not found in news.");
483 if ($mod_news ne $news) { # news item.
484 if (exists $::news{$chan}{$mod_news}) {
485 &::msg($::who, "item '$mod_news' already exists.");
489 &::msg($::who, "Moving item '$news' to '$mod_news' with SAR s/$op/$np/.");
490 foreach (keys %{ $::news{$chan}{$news} }) {
491 $::news{$chan}{$mod_news}{$_} = $::news{$chan}{$news}{$_};
492 delete $::news{$chan}{$news}{$_};
495 delete $::news{$chan}{$news};
498 if ($mod_nnews ne $nnews) { # news Text/Description.
499 &::msg($::who, "Changing text for '$news' SAR s/$op/$np/.");
500 if ($mod_news ne $news) {
501 $::news{$chan}{$mod_news}{Text} = $mod_nnews;
503 $::news{$chan}{$news}{Text} = $mod_nnews;
509 &::msg($::who, "error: that regex failed ;(");
513 &::msg($::who, "error: Invalid regex. Try s/1/2/, s#3#4#...");
518 $args =~ /^(\S+)\s+(\S+)\s+(.*)$/;
519 my($item, $what, $value) = ($1,$2,$3);
521 &::DEBUG("set called.");
528 &::DEBUG("item => '$item'.");
529 my $news = &getNewsItem($item);
530 &::DEBUG("news => '$news'");
532 if (!defined $news) {
533 &::msg($::who, "Could not find item '$item' substring or # in news list.");
537 # list all values for chan.
538 if (!defined $what) {
544 my @elements = ("Expire","Text");
545 foreach (@elements) {
546 next unless ($what =~ /^$_$/i);
553 &::msg($::who, "Invalid set. Try: @elements");
558 if (!defined $value) {
563 if (!exists $::news{$chan}{$news}) {
564 &::msg($::who, "news '$news' does not exist");
568 if ($what eq "Expire") {
569 # todo: use do_set().
572 my $plus = ($value =~ s/^\+//g);
573 while ($value =~ s/^(\d+)(\S*)\s*//) {
574 my($int,$unit) = ($1,$2);
575 $time += $int if ($unit =~ /^s(ecs?)?$/i);
576 $time += $int*60 if ($unit =~ /^m(in(utes?)?)?$/i);
577 $time += $int*60*60 if ($unit =~ /^h(ours?)?$/i);
578 $time += $int*60*60*24 if (!$unit or $unit =~ /^d(ays?)?$/i);
579 $time += $int*60*60*24*7 if ($unit =~ /^w(eeks?)?$/i);
580 $time += $int*60*60*24*30 if ($unit =~ /^mon(th)?$/i);
583 if ($value =~ s/^never$//i) {
590 # from creation of item.
591 $time += $::news{$chan}{$news}{Time};
594 if (!$time or ($value and $value !~ /^never$/i)) {
595 &::DEBUG("set: Expire... need to parse.");
600 &::msg($::who, "Set never expire for \002$item\002." );
601 } elsif ($time < -1) {
602 &::DEBUG("time should never be negative ($time).");
605 &::msg($::who, "Set expire for \002$item\002, to ".
606 localtime($time) ." [".&::Time2String($time - time())."]" );
608 if (time() > $time) {
609 &::DEBUG("hrm... time() > $time, should expire.");
614 $::news{$chan}{$news}{Expire} = $time;
620 &::DEBUG("who => '$::who'");
621 my $author = $::news{$chan}{$news}{Author};
622 $auth++ if ($::who eq $author);
623 $auth++ if (&::IsFlag("o"));
624 if (!defined $author) {
625 &::DEBUG("news{$chan}{$news}{Author} is not defined! auth'd anyway");
626 $::news{$chan}{$news}{Author} = $::who;
632 # todo: show when it'll expire.
633 &::msg($::who, "Sorry, you cannot set items. (author $author owns it)");
637 # todo: clean this up.
638 my $old = $::news{$chan}{$news}{$what};
640 &::DEBUG("old => $old.");
642 $::news{$chan}{$news}{$what} = $value;
643 &::msg($::who, "Setting [$chan]/{$news}/<$what> to '$value'.");
647 my($tchan, $flag) = @_;
649 $chan ||= $tchan; # hack hack hack.
651 # todo: if chan = undefined, guess.
652 if (!exists $::news{$chan}) {
653 &::msg($::who, "invalid chan $chan");
657 my $t = $::newsuser{$chan}{$::who};
658 if (defined $t and ($t == 0 or $t == -1)) {
659 &::DEBUG("not displaying any new news for $::who");
664 foreach (keys %{ $::news{$chan} }) {
665 if (&::IsChanConf("newsNotifyAll") and !defined $t) {
666 &::DEBUG("setting time for $::who to 1...");
667 $::newsuser{$chan}{$::who} = 1;
670 next if (!defined $t);
671 next if ($t > $::news{$chan}{$_}{Time});
676 if (!scalar @new and $flag) {
677 &::msg($::who, "no new news for $chan.");
682 my $unread = scalar @new;
683 my $total = scalar keys %{ $::news{$chan} };
684 return unless ($unread);
686 &::msg($::who, "There are unread news in $chan ($unread unread, $total total). /msg $::ident news latest. If you don't want further news notification, /msg $::ident news unnotify");
692 &::msg($::who, "+==== New news for \002$chan\002 (".
693 scalar(@new)." new items):");
695 my $timestr = &::Time2String( time() - $::newsuser{$chan}{$::who} );
696 &::msg($::who, "|= Last time read $timestr ago");
699 my $i = &newsS2N($_);
700 my $age = time() - $::news{$chan}{$_}{Time};
701 &::msg($::who, sprintf("\002[\002%2d\002]\002 %s",
703 # $i, $_, &::Time2String($age) ) );
706 &::msg($::who, "|= to read, do 'news read <#>' or 'news read <keyword>'");
708 # lame hack to prevent dupes if we just ignore it.
709 my $x = $::newsuser{$chan}{$::who};
710 if (defined $x and ($x == 0 or $x == -1)) {
711 &::DEBUG("not updating time for $::who. (2)");
713 $::newsuser{$chan}{$::who} = time();
724 foreach (keys %{ $::news{$chan} }) {
725 $time{ $::news{$chan}{$_}{Time} } = $_;
729 foreach (sort { $a <=> $b } keys %time) {
730 push(@items, $time{$_});
742 foreach (keys %{ $::news{$chan} }) {
743 my $t = $::news{$chan}{$_}{Time};
745 if (!defined $t or $t !~ /^\d+$/) {
746 &::DEBUG("warn: t is undefined for news{$chan}{$_}{Time}; removing item.");
747 delete $::news{$chan}{$_};
754 foreach (sort { $a <=> $b } keys %time) {
756 return $item if ($time{$_} eq $what);
759 &::DEBUG("newsS2N($what): failed...");
767 foreach (keys %{ $::news{$chan} }) {
768 my $t = $::news{$chan}{$_}{Time};
770 if (!defined $t or $t !~ /^\d+$/) {
771 &::DEBUG("warn: t is undefined for news{$chan}{$_}{Time}; removing item.");
772 delete $::news{$chan}{$_};
779 # number to string resolution.
780 if ($what =~ /^\d+$/) {
781 foreach (sort { $a <=> $b } keys %time) {
783 return $time{$_} if ($item == $what);
787 # partial string to full string resolution
788 # in some cases, string->number resolution.
792 foreach (sort { $a <=> $b } keys %time) {
794 # $no = $item if ($time{$_} eq $what);
795 if ($time{$_} eq $what) {
800 push(@items, $time{$_}) if ($time{$_} =~ /\Q$what\E/i);
803 if (defined $no and !@items) {
804 &::DEBUG("string->number resolution: $what->$no.");
808 if (scalar @items > 1) {
809 &::DEBUG("Multiple matches, not guessing.");
810 &::msg($::who, "Multiple matches, not guessing.");
814 &::DEBUG("gNI: part_string->full_string: $what->$items[0]");
816 &::DEBUG("gNI: Guessed '$items[0]'.");
819 &::DEBUG("gNI: No match.");
824 &::ERROR("getNewsItem: Should not happen (what = $what)");
829 my($what,$value) = @_;
831 if (!defined $chan) {
832 &::DEBUG("do_set: chan not defined.");
836 if (!defined $what or $what =~ /^\s*$/) {
837 &::DEBUG("what $what is not defined.");
840 if (!defined $value or $value =~ /^\s*$/) {
841 &::DEBUG("value $value is not defined.");
845 &::DEBUG("do_set: TODO...");