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) {
66 } elsif ($what =~ /^del(\s+(.*))?$/i) {
68 } elsif ($what =~ /^mod(\s+(.*))?$/i) {
70 } elsif ($what =~ /^set(\s+(.*))?$/i) {
72 } elsif ($what =~ /^(\d)$/i) {
73 &::DEBUG("read shortcut called.");
75 } elsif ($what =~ /^read(\s+(.*))?$/i) {
77 } elsif ($what =~ /^(latest|new)(\s+(.*))?$/i) {
78 &::DEBUG("latest called... hrm");
79 &latest($3 || $chan, 1);
80 } elsif ($what =~ /^list$/i) {
81 &::DEBUG("list longcut called.");
83 } elsif ($what =~ /^(expire|text|desc)(\s+(.*))?$/i) {
86 my($arg1,$arg2) = split(/\s+/, $3, 2);
87 &set("$arg1 $1 $arg2");
88 } elsif ($what =~ /^help(\s+(.*))?$/i) {
91 &::DEBUG("could not parse '$what'.");
92 &::msg($::who, "unknown command: $what");
97 my $file = "$::bot_base_dir/blootbot-news.txt";
103 &::DEBUG("readNews: fileno exists, should never happen.");
114 # todo: allow commands.
116 if (/^[\s\t]+(\S+):[\s\t]+(.*)$/) {
117 if (!defined $item) {
118 &::DEBUG("!defined item, never happen!");
122 $::news{$chan}{$item}{$1} = $2;
126 # U <chan> <nick> <time>
127 if (/^U\s+(\S+)\s+(\S+)\s+(\d+)$/) {
128 $::newsuser{$1}{$2} = $3;
133 if (/^(\S+)[\s\t]+(.*)$/) {
141 &::status("News: Read $ci items for ".scalar(keys %::news)
142 ." chans, $cu users cache");
146 if (!scalar keys %::news) {
147 &::DEBUG("wN: nothing to write.");
151 my $file = "$::bot_base_dir/blootbot-news.txt";
154 &::ERROR("fileno NEWS exists, should never happen.");
158 # todo: add commands to output file.
160 my($cc,$ci,$cu) = (0,0,0);
162 open(NEWS, ">$file");
163 foreach $chan (sort keys %::news) {
164 $c = scalar keys %{ $::news{$chan} };
168 foreach $item (sort keys %{ $::news{$chan} }) {
169 $c = scalar keys %{ $::news{$chan}{$item} };
173 print NEWS "$chan $item\n";
174 foreach $what (sort keys %{ $::news{$chan}{$item} }) {
175 print NEWS " $what: $::news{$chan}{$item}{$what}\n";
181 # todo: show how many users we wrote down.
182 if (&::getChanConfList("newsKeepRead")) {
183 # old users are removed in newsFlush(), perhaps it should be
186 foreach $chan (sort keys %::newsuser) {
188 foreach (sort keys %{ $::newsuser{$chan} }) {
189 print NEWS "U $chan $_ $::newsuser{$chan}{$_}\n";
197 &::status("News: Wrote $ci items for $cc chans, $cu user cache.");
203 if (!defined $chan or !defined $str or $str =~ /^\s*$/) {
208 if (length $str > 64) {
209 &::msg($::who, "That's not really an item (>64chars)");
213 if (exists $::news{$chan}{$str}{Time}) {
214 &::msg($::who, "'$str' for $chan already exists!");
218 $::news{$chan}{$str}{Time} = time();
219 my $expire = &::getChanConfDefault("newsDefaultExpire",7);
220 $::news{$chan}{$str}{Expire} = time() + $expire*60*60*24;
221 $::news{$chan}{$str}{Author} = $::who;
223 my $agestr = &::Time2String($::news{$chan}{$str}{Expire} - time() );
224 my $item = &newsS2N($str);
225 &::msg($::who, "Added '\037$str\037' at [".localtime(time).
226 "] by \002$::who\002 for item #\002$item\002.");
227 &::msg($::who, "Now do 'news text $item <your_description>'");
228 &::msg($::who, "This item will expire at \002".
229 localtime($::news{$chan}{$str}{Expire})."\002 [$agestr from now] "
239 if (!defined $what) {
244 if ($what =~ /^\d+$/) {
245 my $count = scalar keys %{ $::news{$chan} };
247 &::msg($::who, "No news for $chan.");
251 if ($what > $count or $what < 0) {
252 &::msg($::who, "$what is out of range (max $count)");
256 $item = &getNewsItem($what);
257 $what = $item; # hack hack hack.
260 $_ = &getNewsItem($what); # hack hack hack.
261 $what = $_ if (defined $_);
263 if (!exists $::news{$chan}{$what}) {
265 foreach (keys %{ $::news{$chan} }) {
266 next unless (/\Q$what\E/);
270 if (!scalar @found) {
271 &::msg($::who, "could not find $what.");
275 if (scalar @found > 1) {
276 &::msg($::who, "too many matches for $what.");
281 &::DEBUG("del: str: guessed what => $what");
285 if (exists $::news{$chan}{$what}) {
287 $auth++ if ($::who eq $::news{$chan}{$what}{Author});
288 $auth++ if (&::IsFlag("o"));
291 # todo: show when it'll expire.
292 &::msg($::who, "Sorry, you cannot remove items; just let them expire on their own.");
296 &::msg($::who, "ok, deleted '$what' from \002$chan\002...");
297 delete $::news{$chan}{$what};
299 &::msg($::who, "error: not found $what in news for $chan.");
304 if (!scalar keys %{ $::news{$chan} }) {
305 &::msg($::who, "No News for \002$chan\002.");
309 if (&::IsChanConf("newsKeepRead")) {
310 $::newsuser{$chan}{$::who} = time();
313 &::msg($::who, "|==== News for \002$chan\002:");
315 foreach (keys %{ $::news{$chan} }) {
316 my $t = $::news{$chan}{$_}{Time};
317 $newest = $t if ($t > $newest);
319 my $timestr = &::Time2String(time() - $newest);
320 &::msg($::who, "|= Last updated $timestr ago.");
321 &::msg($::who, " \037Num\037 \037Item ".(" "x40)." \037");
324 foreach ( &getNewsAll() ) {
326 my $setby = $::news{$chan}{$subtopic}{Author};
328 if (!defined $subtopic) {
329 &::DEBUG("warn: subtopic == undef.");
333 # todo: show request stats aswell.
334 &::msg($::who, sprintf("\002[\002%2d\002]\002 %s",
339 &::msg($::who, "|= End of News.");
340 &::msg($::who, "use 'news read <#>' or 'news read <keyword>'");
346 if (!defined $chan or !defined $str or $str =~ /^\s*$/) {
347 &::help("news read");
351 if (!scalar keys %{ $::news{$chan} }) {
352 &::msg($::who, "No News for \002$chan\002.");
356 my $item = &getNewsItem($str);
357 if (!defined $item or !scalar keys %{ $::news{$chan}{$item} }) {
358 &::msg($::who, "No news item called '$str'");
362 if (!exists $::news{$chan}{$item}{Text}) {
363 &::msg($::who, "Someone forgot to add info to this news item");
367 my $t = localtime( $::news{$chan}{$item}{Time} );
368 my $a = $::news{$chan}{$item}{Author};
369 my $text = $::news{$chan}{$item}{Text};
370 my $num = &newsS2N($item);
371 my $rwho = $::news{$chan}{$item}{Request_By} || $::who;
372 my $rcount = $::news{$chan}{$item}{Request_Count} || 0;
374 if (length $text < $::param{maxKeySize}) {
375 &::DEBUG("NEWS: Possible news->factoid redirection.");
376 my $f = &::getFactoid($text);
379 &::DEBUG("NEWS: ok, $text is factoid redirection.");
380 $f =~ s/^<REPLY>\s*//i; # anything else?
385 &::msg($::who, "+- News \002$chan\002 #$num: \037$item\037");
386 &::msg($::who, "| Added by $a at $t");
387 &::msg($::who, "| Requested $rcount times, last by $rwho");
388 &::msg($::who, $text);
390 $::news{$chan}{$item}{'Request_By'} = $::who;
391 $::news{$chan}{$item}{'Request_Time'} = time();
392 $::news{$chan}{$item}{'Request_Count'}++;
396 my($item, $str) = split /\s+/, $_[0], 2;
398 if (!defined $item or $item eq "" or $str =~ /^\s*$/) {
403 my $news = &getNewsItem($item);
405 if (!defined $news) {
406 &::DEBUG("error: mod: news == undefined.");
409 my $nnews = $::news{$chan}{$news}{Text};
410 my $mod_news = $news;
411 my $mod_nnews = $nnews;
414 if ($str =~ m|^\s*s([/,#\|])(.+?)\1(.*?)\1([a-z]*);?\s*$|) {
415 my ($delim, $op, $np, $flags) = ($1,$2,$3,$4);
417 if ($flags !~ /^(g)?$/) {
418 &::msg($::who, "error: Invalid flags to regex.");
422 ### TODO: use m### to make code safe!
423 # todo: make code safer.
425 # todo: use eval to deal with flags easily.
427 $done++ if (!$done and $mod_news =~ s/\Q$op\E/$np/);
428 $done++ if (!$done and $mod_nnews =~ s/\Q$op\E/$np/);
429 } elsif ($flags eq "g") {
430 $done++ if ($mod_news =~ s/\Q$op\E/$np/g);
431 $done++ if ($mod_nnews =~ s/\Q$op\E/$np/g);
435 &::msg($::who, "warning: regex not found in news.");
439 if ($mod_news ne $news) { # news item.
440 if (exists $::news{$chan}{$mod_news}) {
441 &::msg($::who, "item '$mod_news' already exists.");
445 &::msg($::who, "Moving item '$news' to '$mod_news' with SAR s/$op/$np/.");
446 foreach (keys %{ $::news{$chan}{$news} }) {
447 $::news{$chan}{$mod_news}{$_} = $::news{$chan}{$news}{$_};
448 delete $::news{$chan}{$news}{$_};
451 delete $::news{$chan}{$news};
454 if ($mod_nnews ne $nnews) { # news Text/Description.
455 &::msg($::who, "Changing text for '$news' SAR s/$op/$np/.");
456 if ($mod_news ne $news) {
457 $::news{$chan}{$mod_news}{Text} = $mod_nnews;
459 $::news{$chan}{$news}{Text} = $mod_nnews;
465 &::msg($::who, "error: that regex failed ;(");
469 &::msg($::who, "error: Invalid regex. Try s/1/2/, s#3#4#...");
474 $args =~ /^(\S+)\s+(\S+)\s+(.*)$/;
475 my($item, $what, $value) = ($1,$2,$3);
477 &::DEBUG("set called.");
484 &::DEBUG("item => '$item'.");
485 my $news = &getNewsItem($item);
486 &::DEBUG("news => '$news'");
488 if (!defined $news) {
489 &::msg($::who, "Could not find item '$item' substring or # in news list.");
493 # list all values for chan.
494 if (!defined $what) {
500 my @elements = ("Expire","Text");
501 foreach (@elements) {
502 next unless ($what =~ /^$_$/i);
509 &::msg($::who, "Invalid set. Try: @elements");
514 if (!defined $value) {
519 if (!exists $::news{$chan}{$news}) {
520 &::msg($::who, "news '$news' does not exist");
524 if ($what eq "Expire") {
525 # todo: use do_set().
528 my $plus = ($value =~ s/^\+//g);
529 while ($value =~ s/^(\d+)(\S*)\s*//) {
530 my($int,$unit) = ($1,$2);
531 $time += $int if ($unit =~ /^s(ecs?)?$/i);
532 $time += $int*60 if ($unit =~ /^m(in(utes?)?)?$/i);
533 $time += $int*60*60 if ($unit =~ /^h(ours?)?$/i);
534 $time += $int*60*60*24 if (!$unit or $unit =~ /^d(ays?)?$/i);
535 $time += $int*60*60*24*7 if ($unit =~ /^w(eeks?)?$/i);
536 $time += $int*60*60*24*30 if ($unit =~ /^mon(th)?$/i);
539 if ($value =~ s/^never$//i) {
546 # from creation of item.
547 $time += $::news{$chan}{$news}{Time};
550 if (!$time or ($value and $value !~ /^never$/i)) {
551 &::DEBUG("set: Expire... need to parse.");
556 &::msg($::who, "Set never expire for \002$item\002." );
557 } elsif ($time < -1) {
558 &::DEBUG("time should never be negative ($time).");
561 &::msg($::who, "Set expire for \002$item\002, to ".
562 localtime($time) ." [".&::Time2String($time - time())."]" );
564 if (time() > $time) {
565 &::DEBUG("hrm... time() > $time, should expire.");
570 $::news{$chan}{$news}{Expire} = $time;
576 &::DEBUG("who => '$::who'");
577 my $author = $::news{$chan}{$news}{Author};
578 $auth++ if ($::who eq $author);
579 $auth++ if (&::IsFlag("o"));
580 if (!defined $author) {
581 &::DEBUG("news{$chan}{$news}{Author} is not defined! auth'd anyway");
582 $::news{$chan}{$news}{Author} = $::who;
588 # todo: show when it'll expire.
589 &::msg($::who, "Sorry, you cannot set items. (author $author owns it)");
593 # todo: clean this up.
594 my $old = $::news{$chan}{$news}{$what};
596 &::DEBUG("old => $old.");
598 $::news{$chan}{$news}{$what} = $value;
599 &::msg($::who, "Setting [$chan]/{$news}/<$what> to '$value'.");
603 my($tchan, $flag) = @_;
605 $chan ||= $tchan; # hack hack hack.
607 # todo: if chan = undefined, guess.
608 if (!exists $::news{$chan}) {
609 &::msg($::who, "invalid chan $chan");
614 foreach (keys %{ $::news{$chan} }) {
615 my $t = $::newsuser{$chan}{$::who};
616 next if (!defined $t);
617 next if ($t > $::news{$chan}{$_}{Time});
622 if (!scalar @new and $flag) {
623 &::msg($::who, "no new news for $chan.");
628 my $unread = scalar @new;
629 my $total = scalar keys %{ $::news{$chan} };
630 &::msg($::who, "There are unread news in $chan ($unread unread, $total, total). /msg $::ident news latest");
635 &::msg($::who, "+==== New news for \002$chan\002 (".
636 scalar(@new)." new items):");
638 my $timestr = &::Time2String( time() - $::newsuser{$chan}{$::who} );
639 &::msg($::who, "|= Last time read $timestr ago");
642 my $i = &newsS2N($_);
643 my $age = time() - $::news{$chan}{$_}{Time};
644 &::msg($::who, sprintf("\002[\002%2d\002]\002 %s",
646 # $i, $_, &::Time2String($age) ) );
649 &::msg($::who, "|= to read, do 'news read <#>' or 'news read <keyword>'");
651 # lame hack to prevent dupes if we just ignore it.
652 $::newsuser{$chan}{$::who} = time();
662 foreach (keys %{ $::news{$chan} }) {
663 $time{ $::news{$chan}{$_}{Time} } = $_;
667 foreach (sort { $a <=> $b } keys %time) {
668 push(@items, $time{$_});
680 foreach (keys %{ $::news{$chan} }) {
681 my $t = $::news{$chan}{$_}{Time};
683 if (!defined $t or $t !~ /^\d+$/) {
684 &::DEBUG("warn: t is undefined for news{$chan}{$_}{Time}; removing item.");
685 delete $::news{$chan}{$_};
692 foreach (sort { $a <=> $b } keys %time) {
694 return $item if ($time{$_} eq $what);
697 &::DEBUG("newsS2N($what): failed...");
705 foreach (keys %{ $::news{$chan} }) {
706 my $t = $::news{$chan}{$_}{Time};
708 if (!defined $t or $t !~ /^\d+$/) {
709 &::DEBUG("warn: t is undefined for news{$chan}{$_}{Time}; removing item.");
710 delete $::news{$chan}{$_};
717 # number to string resolution.
718 if ($what =~ /^\d+$/) {
719 foreach (sort { $a <=> $b } keys %time) {
721 return $time{$_} if ($item == $what);
725 # partial string to full string resolution
726 # in some cases, string->number resolution.
730 foreach (sort { $a <=> $b } keys %time) {
732 # $no = $item if ($time{$_} eq $what);
733 if ($time{$_} eq $what) {
738 push(@items, $time{$_}) if ($time{$_} =~ /\Q$what\E/i);
741 if (defined $no and !@items) {
742 &::DEBUG("string->number resolution: $what->$no.");
746 if (scalar @items > 1) {
747 &::DEBUG("Multiple matches, not guessing.");
748 &::msg($::who, "Multiple matches, not guessing.");
752 &::DEBUG("gNI: part_string->full_string: $what->$items[0]");
754 &::DEBUG("gNI: Guessed '$items[0]'.");
757 &::DEBUG("gNI: No match.");
762 &::ERROR("getNewsItem: Should not happen (what = $what)");
767 my($what,$value) = @_;
769 if (!defined $chan) {
770 &::DEBUG("do_set: chan not defined.");
774 if (!defined $what or $what =~ /^\s*$/) {
775 &::DEBUG("what $what is not defined.");
778 if (!defined $value or $value =~ /^\s*$/) {
779 &::DEBUG("value $value is not defined.");
783 &::DEBUG("do_set: TODO...");