2 # News.pl: Advanced news management
4 # Version: v0.2 (20010326)
6 # Notes: Testing done by greycat, kudos!
9 # news{ channel }{ string } { items }
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 eq "private") {
38 if (defined $what and $what =~ s/^($::mask{chan})\s*//) {
39 # todo: check if the channel exists aswell.
44 my @chans = &::GetNickInChans($::who);
46 if (scalar @chans > 1) {
47 &::msg($::who, "error: I dunno which channel you are referring to since you're on more than one.");
51 if (scalar @chans == 0) {
52 &::msg($::who, "error: I couldn't find you on any chan. This must be a bug!");
57 &::DEBUG("Guessed $::who being on chan $chan");
60 if (!defined $what or $what =~ /^\s*$/) {
65 if ($what =~ /^add(\s+(.*))?$/i) {
67 } elsif ($what =~ /^del(\s+(.*))?$/i) {
69 } elsif ($what =~ /^mod(\s+(.*))?$/i) {
71 } elsif ($what =~ /^set(\s+(.*))?$/i) {
73 } elsif ($what =~ /^(\d)$/i) {
74 &::DEBUG("read shortcut called.");
76 } elsif ($what =~ /^read(\s+(.*))?$/i) {
78 } elsif ($what =~ /^(latest|new)(\s+(.*))?$/i) {
79 &::DEBUG("latest called... hrm");
80 &latest($3 || $chan, 1);
81 } elsif ($what =~ /^list$/i) {
82 &::DEBUG("list longcut called.");
84 } elsif ($what =~ /^(expire|text|desc)(\s+(.*))?$/i) {
87 my($arg1,$arg2) = split(/\s+/, $3, 2);
88 &set("$arg1 $1 $arg2");
89 } elsif ($what =~ /^help(\s+(.*))?$/i) {
92 &::DEBUG("could not parse '$what'.");
93 &::msg($::who, "unknown command: $what");
98 my $file = "$::bot_base_dir/blootbot-news.txt";
104 &::DEBUG("readNews: fileno exists, should never happen.");
115 # todo: allow commands.
117 if (/^[\s\t]+(\S+):[\s\t]+(.*)$/) {
118 if (!defined $item) {
119 &::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 = (exists $::news{$chan}{$str}) ? $str : &getNewsItem($str);
357 my $item = &getNewsItem($str);
358 if (!defined $item or !scalar keys %{ $::news{$chan}{$item} }) {
359 &::msg($::who, "No news item called '$str'");
363 if (!exists $::news{$chan}{$item}{Text}) {
364 &::msg($::who, "Someone forgot to add info to this news item");
368 # todo: show item number.
369 # todo: show ago-time aswell?
370 # todo: show request stats aswell.
371 my $t = localtime($::news{$chan}{$item}{Time});
372 my $a = $::news{$chan}{$item}{Author};
373 &::msg($::who, "+- News \002$chan\002 ##, item '\037$item\037':");
374 &::msg($::who, "| Added by $a at $t");
375 &::msg($::who, $::news{$chan}{$item}{Text});
377 $::news{$chan}{$item}{'Request_By'} = $::who;
378 $::news{$chan}{$item}{'Request_Time'} = time();
379 $::news{$chan}{$item}{'Request_Count'}++;
383 my($item, $str) = split /\s+/, $_[0], 2;
385 if (!defined $item or $item eq "" or $str =~ /^\s*$/) {
390 my $news = &getNewsItem($item);
392 if (!defined $news) {
393 &::DEBUG("error: mod: news == undefined.");
396 my $nnews = $::news{$chan}{$news}{Text};
397 my $mod_news = $news;
398 my $mod_nnews = $nnews;
401 if ($str =~ m|^\s*s([/,#\|])(.+?)\1(.*?)\1([a-z]*);?\s*$|) {
402 my ($delim, $op, $np, $flags) = ($1,$2,$3,$4);
404 if ($flags !~ /^(g)?$/) {
405 &::msg($::who, "error: Invalid flags to regex.");
409 ### TODO: use m### to make code safe!
410 # todo: make code safer.
412 # todo: use eval to deal with flags easily.
414 $done++ if (!$done and $mod_news =~ s/\Q$op\E/$np/);
415 $done++ if (!$done and $mod_nnews =~ s/\Q$op\E/$np/);
416 } elsif ($flags eq "g") {
417 $done++ if ($mod_news =~ s/\Q$op\E/$np/g);
418 $done++ if ($mod_nnews =~ s/\Q$op\E/$np/g);
422 &::msg($::who, "warning: regex not found in news.");
426 if ($mod_news ne $news) { # news item.
427 if (exists $::news{$chan}{$mod_news}) {
428 &::msg($::who, "item '$mod_news' already exists.");
432 &::msg($::who, "Moving item '$news' to '$mod_news' with SAR s/$op/$np/.");
433 foreach (keys %{ $::news{$chan}{$news} }) {
434 $::news{$chan}{$mod_news}{$_} = $::news{$chan}{$news}{$_};
435 delete $::news{$chan}{$news}{$_};
438 delete $::news{$chan}{$news};
441 if ($mod_nnews ne $nnews) { # news Text/Description.
442 &::msg($::who, "Changing text for '$news' SAR s/$op/$np/.");
443 if ($mod_news ne $news) {
444 $::news{$chan}{$mod_news}{Text} = $mod_nnews;
446 $::news{$chan}{$news}{Text} = $mod_nnews;
452 &::msg($::who, "error: that regex failed ;(");
456 &::msg($::who, "error: Invalid regex. Try s/1/2/, s#3#4#...");
461 $args =~ /^(\S+)\s+(\S+)\s+(.*)$/;
462 my($item, $what, $value) = ($1,$2,$3);
464 &::DEBUG("set called.");
471 &::DEBUG("item => '$item'.");
472 my $news = &getNewsItem($item);
473 &::DEBUG("news => '$news'");
475 if (!defined $news) {
476 &::msg($::who, "Could not find item '$item' substring or # in news list.");
480 # list all values for chan.
481 if (!defined $what) {
487 my @elements = ("Expire","Text");
488 foreach (@elements) {
489 next unless ($what =~ /^$_$/i);
496 &::msg($::who, "Invalid set. Try: @elements");
501 if (!defined $value) {
506 if (!exists $::news{$chan}{$news}) {
507 &::msg($::who, "news '$news' does not exist");
511 if ($what eq "Expire") {
512 # todo: use do_set().
515 my $plus = ($value =~ s/^\+//g);
516 while ($value =~ s/^(\d+)(\S*)\s*//) {
517 my($int,$unit) = ($1,$2);
518 $time += $int if ($unit =~ /^s(ecs?)?$/i);
519 $time += $int*60 if ($unit =~ /^m(in(utes?)?)?$/i);
520 $time += $int*60*60 if ($unit =~ /^h(ours?)?$/i);
521 $time += $int*60*60*24 if (!$unit or $unit =~ /^d(ays?)?$/i);
522 $time += $int*60*60*24*7 if ($unit =~ /^w(eeks?)?$/i);
523 $time += $int*60*60*24*30 if ($unit =~ /^mon(th)?$/i);
526 if ($value =~ s/^never$//i) {
533 # from creation of item.
534 $time += $::news{$chan}{$news}{Time};
537 if (!$time or ($value and $value !~ /^never$/i)) {
538 &::DEBUG("set: Expire... need to parse.");
543 &::msg($::who, "Set never expire for \002$item\002." );
544 } elsif ($time < -1) {
545 &::DEBUG("time should never be negative ($time).");
548 &::msg($::who, "Set expire for \002$item\002, to ".
549 localtime($time) ." [".&::Time2String($time - time())."]" );
551 if (time() > $time) {
552 &::DEBUG("hrm... time() > $time, should expire.");
557 $::news{$chan}{$news}{Expire} = $time;
563 &::DEBUG("who => '$::who'");
564 my $author = $::news{$chan}{$news}{Author};
565 $auth++ if ($::who eq $author);
566 $auth++ if (&::IsFlag("o"));
567 if (!defined $author) {
568 &::DEBUG("news{$chan}{$news}{Author} is not defined! auth'd anyway");
569 $::news{$chan}{$news}{Author} = $::who;
575 # todo: show when it'll expire.
576 &::msg($::who, "Sorry, you cannot set items. (author $author owns it)");
580 my $old = $::news{$chan}{$news}{$what};
582 &::DEBUG("old => $old.");
584 $::news{$chan}{$news}{$what} = $value;
585 &::msg($::who, "Setting [$chan]/{$news}/<$what> to '$value'.");
589 my($tchan, $flag) = @_;
591 $chan ||= $tchan; # hack hack hack.
593 # todo: if chan = undefined, guess.
594 if (!exists $::news{$chan}) {
595 &::msg($::who, "invalid chan $chan");
600 foreach (keys %{ $::news{$chan} }) {
601 my $t = $::newsuser{$chan}{$::who};
602 next if (!defined $t);
603 next if ($t > $::news{$chan}{$_}{Time});
608 if (!scalar @new and $flag) {
609 &::msg($::who, "no new news for $chan.");
614 &::msg($::who, "+==== New news for \002$chan\002 (".
615 scalar(@new)." new items):");
617 my $timestr = &::Time2String( time() - $::newsuser{$chan}{$::who} );
618 &::msg($::who, "|= Last time read $timestr ago");
621 my $i = &newsS2N($_);
622 &::DEBUG("i = $i, _ => $_");
623 my $age = time() - $::news{$chan}{$_}{Time};
624 &::msg($::who, sprintf("\002[\002%2d\002]\002 %s",
626 # $i, $_, &::Time2String($age) ) );
629 &::msg($::who, "|= to read, do 'news read <#>' or 'news read <keyword>'");
631 # lame hack to prevent dupes if we just ignore it.
632 $::newsuser{$chan}{$::who} = time();
642 foreach (keys %{ $::news{$chan} }) {
643 $time{ $::news{$chan}{$_}{Time} } = $_;
647 foreach (sort { $a <=> $b } keys %time) {
648 push(@items, $time{$_});
660 foreach (keys %{ $::news{$chan} }) {
661 my $t = $::news{$chan}{$_}{Time};
663 if (!defined $t or $t !~ /^\d+$/) {
664 &::DEBUG("warn: t is undefined for news{$chan}{$_}{Time}; removing item.");
665 delete $::news{$chan}{$_};
672 foreach (sort { $a <=> $b } keys %time) {
674 return $item if ($time{$_} eq $what);
677 &::DEBUG("newsS2N($what): failed...");
685 foreach (keys %{ $::news{$chan} }) {
686 my $t = $::news{$chan}{$_}{Time};
688 if (!defined $t or $t !~ /^\d+$/) {
689 &::DEBUG("warn: t is undefined for news{$chan}{$_}{Time}; removing item.");
690 delete $::news{$chan}{$_};
697 # number to string resolution.
698 if ($what =~ /^\d+$/) {
699 foreach (sort { $a <=> $b } keys %time) {
701 return $time{$_} if ($item == $what);
705 # partial string to full string resolution
709 foreach (sort { $a <=> $b } keys %time) {
711 # $no = $item if ($time{$_} eq $what);
712 if ($time{$_} eq $what) {
717 push(@items, $time{$_}) if ($time{$_} =~ /\Q$what\E/i);
720 # since we have so much built into this function, there is so
721 # many guesses we can make.
722 # todo: split this command in the future into:
723 # full_string->number and number->string
724 # partial_string->full_string
725 &::DEBUG("no => $no, items => @items.");
726 if (defined $no and !@items) {
727 &::DEBUG("string->number resolution.");
731 if (scalar @items > 1) {
732 &::DEBUG("Multiple matches, not guessing.");
733 &::msg($::who, "Multiple matches, not guessing.");
737 &::DEBUG("gNI: string->number(??): $what->$items[0]");
739 &::DEBUG("gNI: Guessed '$items[0]'.");
742 &::DEBUG("gNI: No match.");
747 &::ERROR("getNewsItem: Should not happen (what = $what)");
752 my($what,$value) = @_;
754 if (!defined $chan) {
755 &::DEBUG("do_set: chan not defined.");
759 if (!defined $what or $what =~ /^\s*$/) {
760 &::DEBUG("what $what is not defined.");
763 if (!defined $value or $value =~ /^\s*$/) {
764 &::DEBUG("value $value is not defined.");
768 &::DEBUG("do_set: TODO...");