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.
24 if ($::msgType eq "private") {
29 if (defined $what and $what =~ s/^($::mask{chan})\s*//) {
30 # todo: check if the channel exists aswell.
35 my @chans = &::GetNickInChans($::who);
37 if (scalar @chans > 1) {
38 &::msg($::who, "error: I dunno which channel you are referring to since you're on more than one.");
42 if (scalar @chans == 0) {
43 &::msg($::who, "error: I couldn't find you on any chan. This must be a bug!");
48 &::DEBUG("Guessed $::who being on chan $chan");
51 if (!defined $what or $what =~ /^\s*$/) {
56 if ($what =~ /^add(\s+(.*))?$/i) {
58 } elsif ($what =~ /^del(\s+(.*))?$/i) {
60 } elsif ($what =~ /^mod(\s+(.*))?$/i) {
62 } elsif ($what =~ /^set(\s+(.*))?$/i) {
64 } elsif ($what =~ /^(\d)$/i) {
65 &::DEBUG("read shortcut called.");
67 } elsif ($what =~ /^read(\s+(.*))?$/i) {
69 } elsif ($what =~ /^list$/i) {
70 &::DEBUG("list longcut called.");
72 } elsif ($what =~ /^(expire|text|desc)(\s+(.*))?$/i) {
75 my($arg1,$arg2) = split(/\s+/, $3, 2);
76 &set("$arg1 Text $arg2");
77 } elsif ($what =~ /^help(\s+(.*))?$/i) {
80 &::DEBUG("could not parse '$what'.");
81 &::msg($::who, "unknown command: $what");
86 my $file = "$::bot_base_dir/blootbot-news.txt";
92 &::DEBUG("readNews: fileno exists, should never happen.");
103 # todo: allow commands.
105 if (/^[\s\t]+(\S+):[\s\t]+(.*)$/) {
106 if (!defined $item) {
107 &::DEBUG("!defined item, never happen!");
110 $::news{$chan}{$item}{$1} = $2;
114 # U <chan> <nick> <time>
115 if (/^U\s+(\S+)\s+(\S+)\s+(\d+)$/) {
116 $::newsuser{$1}{$2} = $3;
121 if (/^(\S+)[\s\t]+(.*)$/) {
129 &::status("News: Read $ci items for ".scalar(keys %::news)
130 ." chans, $cu users cache");
134 if (!scalar keys %::news) {
135 &::DEBUG("wN: nothing to write.");
139 my $file = "$::bot_base_dir/blootbot-news.txt";
142 &::ERROR("fileno NEWS exists, should never happen.");
146 # todo: add commands to output file.
148 my($cc,$ci,$cu) = (0,0,0);
150 open(NEWS, ">$file");
151 foreach $chan (sort keys %::news) {
152 $c = scalar keys %{ $::news{$chan} };
156 foreach $item (sort keys %{ $::news{$chan} }) {
157 $c = scalar keys %{ $::news{$chan}{$item} };
161 print NEWS "$chan $item\n";
162 foreach $what (sort keys %{ $::news{$chan}{$item} }) {
163 print NEWS " $what: $::news{$chan}{$item}{$what}\n";
169 # todo: show how many users we wrote down.
170 if (&::getChanConfList("newsKeepRead")) {
171 # old users are removed in newsFlush(), perhaps it should be
174 foreach $chan (sort keys %::newsuser) {
176 foreach (sort keys %{ $::newsuser{$chan} }) {
177 print NEWS "U $chan $_ $::newsuser{$chan}{$_}\n";
185 &::status("News: Wrote $ci items for $cc chans, $cu user cache.");
191 if (!defined $chan or !defined $str or $str =~ /^\s*$/) {
196 if (length $str > 64) {
197 &::msg($::who, "That's not really an item (>64chars)");
201 if (exists $::news{$chan}{$str}{Time}) {
202 &::msg($::who, "'$str' for $chan already exists!");
206 $::news{$chan}{$str}{Time} = time();
207 my $expire = &::getChanConfDefault("newsDefaultExpire",7);
208 $::news{$chan}{$str}{Expire} = time() + $expire*60*60*24;
209 $::news{$chan}{$str}{Author} = $::who;
211 my $agestr = &::Time2String($::news{$chan}{$str}{Expire} - time() );
212 my $item = &getNewsItem($str);
214 &::DEBUG("item eq str ($item): should never happen.");
216 &::msg($::who, "Added '\037$str\037' at [".localtime(time).
217 "] by \002$::who\002 for item #\002$item\002.");
218 &::msg($::who, "Now do 'news text $item <your_description>'");
219 &::msg($::who, "This item will expire at \002".
220 localtime($::news{$chan}{$str}{Expire})."\002 [$agestr from now] "
228 if (!defined $what) {
233 if ($what =~ /^\d+$/) {
234 my $count = scalar keys %{ $::news{$chan} };
236 &::msg($::who, "No news for $chan.");
240 if ($what > $count or $what < 0) {
241 &::msg($::who, "$what is out of range (max $count)");
245 $item = &getNewsItem($what);
246 &::DEBUG("del: num: item => $item ($what)");
247 $what = $item; # hack hack hack.
250 $_ = &getNewsItem($what); # hack hack hack.
251 $what = $_ if (defined $_);
253 if (!exists $::news{$chan}{$what}) {
255 foreach (keys %{ $::news{$chan} }) {
256 next unless (/\Q$what\E/);
260 if (!scalar @found) {
261 &::msg($::who, "could not find $what.");
265 if (scalar @found > 1) {
266 &::msg($::who, "too many matches for $what.");
271 &::DEBUG("del: str: guessed what => $what");
275 if (exists $::news{$chan}{$what}) {
277 $auth++ if ($::who eq $::news{$chan}{$what}{Author});
278 $auth++ if (&::IsFlag("o"));
281 # todo: show when it'll expire.
282 &::msg($::who, "Sorry, you cannot remove items; just let them expire on their own.");
286 &::msg($::who, "ok, deleted '$what' from \002$chan\002...");
287 delete $::news{$chan}{$what};
289 &::msg($::who, "error: not found $what in news for $chan.");
294 if (!scalar keys %{ $::news{$chan} }) {
295 &::msg($::who, "No News for \002$chan\002.");
299 if (&::IsChanConf("newsKeepRead")) {
300 $::newsuser{$chan}{$::who} = time();
303 &::msg($::who, "|==== News for \002$chan\002:");
305 foreach (keys %{ $::news{$chan} }) {
306 my $t = $::news{$chan}{$_}{Time};
307 $newest = $t if ($t > $newest);
309 my $timestr = &::Time2String(time() - $newest);
310 &::msg($::who, "|= Last updated $timestr ago.");
311 &::msg($::who, " \037Num\037 \037Item ".(" "x40)." \037");
314 foreach ( &getNewsAll() ) {
316 my $setby = $::news{$chan}{$subtopic}{Author};
318 if (!defined $subtopic) {
319 &::DEBUG("warn: subtopic == undef.");
323 # todo: show request stats aswell.
324 &::msg($::who, sprintf("\002[\002%2d\002]\002 %s",
329 &::msg($::who, "|= End of News.");
330 &::msg($::who, "use 'news read <#>' or 'news read <keyword>'");
336 if (!defined $chan or !defined $str or $str =~ /^\s*$/) {
337 &::help("news read");
341 if (!scalar keys %{ $::news{$chan} }) {
342 &::msg($::who, "No News for \002$chan\002.");
346 # my $item = (exists $::news{$chan}{$str}) ? $str : &getNewsItem($str);
347 my $item = &getNewsItem($str);
348 if (!defined $item or !scalar keys %{ $::news{$chan}{$item} }) {
349 &::msg($::who, "No news item called '$str'");
353 if (!exists $::news{$chan}{$item}{Text}) {
354 &::msg($::who, "Someone forgot to add info to this news item");
358 # todo: show item number.
359 # todo: show ago-time aswell?
360 # todo: show request stats aswell.
361 my $t = localtime($::news{$chan}{$item}{Time});
362 my $a = $::news{$chan}{$item}{Author};
363 &::msg($::who, "+- News \002$chan\002 ##, item '\037$item\037':");
364 &::msg($::who, "| Added by $a at $t");
365 &::msg($::who, $::news{$chan}{$item}{Text});
367 $::news{$chan}{$item}{'Request_By'} = $::who;
368 $::news{$chan}{$item}{'Request_Time'} = time();
369 $::news{$chan}{$item}{'Request_Count'}++;
373 my($item, $str) = split /\s+/, $_[0], 2;
375 if (!defined $item or $item eq "" or $str =~ /^\s*$/) {
380 my $news = &getNewsItem($item);
382 if (!defined $news) {
383 &::DEBUG("error: mod: news == undefined.");
386 my $nnews = $::news{$chan}{$news}{Text};
387 my $mod_news = $news;
388 my $mod_nnews = $nnews;
391 if ($str =~ m|^\s*s([/,#\|])(.+?)\1(.*?)\1([a-z]*);?\s*$|) {
392 my ($delim, $op, $np, $flags) = ($1,$2,$3,$4);
394 if ($flags !~ /^(g)?$/) {
395 &::msg($::who, "error: Invalid flags to regex.");
399 ### TODO: use m### to make code safe!
400 # todo: make code safer.
402 # todo: use eval to deal with flags easily.
404 $done++ if (!$done and $mod_news =~ s/\Q$op\E/$np/);
405 $done++ if (!$done and $mod_nnews =~ s/\Q$op\E/$np/);
406 } elsif ($flags eq "g") {
407 $done++ if ($mod_news =~ s/\Q$op\E/$np/g);
408 $done++ if ($mod_nnews =~ s/\Q$op\E/$np/g);
412 &::msg($::who, "warning: regex not found in news.");
416 if ($mod_news ne $news) { # news item.
417 if (exists $::news{$chan}{$mod_news}) {
418 &::msg($::who, "item '$mod_news' already exists.");
422 &::msg($::who, "Moving item '$news' to '$mod_news' with SAR s/$op/$np/.");
423 foreach (keys %{ $::news{$chan}{$news} }) {
424 $::news{$chan}{$mod_news}{$_} = $::news{$chan}{$news}{$_};
425 delete $::news{$chan}{$news}{$_};
428 delete $::news{$chan}{$news};
431 if ($mod_nnews ne $nnews) { # news Text/Description.
432 &::msg($::who, "Changing text for '$news' SAR s/$op/$np/.");
433 if ($mod_news ne $news) {
434 $::news{$chan}{$mod_news}{Text} = $mod_nnews;
436 $::news{$chan}{$news}{Text} = $mod_nnews;
442 &::msg($::who, "error: that regex failed ;(");
446 &::msg($::who, "error: Invalid regex. Try s/1/2/, s#3#4#...");
451 $args =~ /^(\S+)\s+(\S+)\s+(.*)$/;
452 my($item, $what, $value) = ($1,$2,$3);
454 &::DEBUG("set called.");
461 &::DEBUG("item => '$item'.");
462 my $news = &getNewsItem($item);
463 &::DEBUG("news => '$news'");
465 if (!defined $news) {
466 &::msg($::who, "Could not find item '$item' substring or # in news list.");
470 # list all values for chan.
471 if (!defined $what) {
477 my @elements = ("Expire","Text");
478 foreach (@elements) {
479 next unless ($what =~ /^$_$/i);
486 &::msg($::who, "Invalid set. Try: @elements");
491 if (!defined $value) {
496 if (!exists $::news{$chan}{$news}) {
497 &::msg($::who, "news '$news' does not exist");
501 if ($what eq "Expire") {
502 # todo: use do_set().
505 my $plus = ($value =~ s/^\+//g);
506 while ($value =~ s/^(\d+)(\S*)\s*//) {
507 my($int,$unit) = ($1,$2);
508 $time += $int if ($unit =~ /^s(ecs?)?$/i);
509 $time += $int*60 if ($unit =~ /^m(in(utes?)?)?$/i);
510 $time += $int*60*60 if ($unit =~ /^h(ours?)?$/i);
511 $time += $int*60*60*24 if (!$unit or $unit =~ /^d(ays?)?$/i);
512 $time += $int*60*60*24*7 if ($unit =~ /^w(eeks?)?$/i);
513 $time += $int*60*60*24*30 if ($unit =~ /^mon(th)?$/i);
516 if ($value =~ s/^never$//i) {
523 # from creation of item.
524 $time += $::news{$chan}{$news}{Time};
527 if (!$time or ($value and $value !~ /^never$/i)) {
528 &::DEBUG("set: Expire... need to parse.");
533 &::msg($::who, "Set never expire for \002$item\002." );
534 } elsif ($time < -1) {
535 &::DEBUG("time should never be negative ($time).");
538 &::msg($::who, "Set expire for \002$item\002, to ".
539 localtime($time) ." [".&::Time2String($time - time())."]" );
541 if (time() > $time) {
542 &::DEBUG("hrm... time() > $time, should expire.");
547 $::news{$chan}{$news}{Expire} = $time;
553 &::DEBUG("who => '$::who'");
554 my $author = $::news{$chan}{$news}{Author};
555 $auth++ if ($::who eq $author);
556 $auth++ if (&::IsFlag("o"));
557 if (!defined $author) {
558 &::DEBUG("news{$chan}{$news}{Author} is not defined! auth'd anyway");
559 $::news{$chan}{$news}{Author} = $::who;
565 # todo: show when it'll expire.
566 &::msg($::who, "Sorry, you cannot set items. (author $author owns it)");
570 my $old = $::news{$chan}{$news}{$what};
572 &::DEBUG("old => $old.");
574 $::news{$chan}{$news}{$what} = $value;
575 &::msg($::who, "Setting [$chan]/{$news}/<$what> to '$value'.");
584 foreach (keys %{ $::news{$chan} }) {
585 $time{ $::news{$chan}{$_}{Time} } = $_;
589 foreach (sort { $a <=> $b } keys %time) {
590 push(@items, $time{$_});
601 foreach (keys %{ $::news{$chan} }) {
602 my $t = $::news{$chan}{$_}{Time};
604 if (!defined $t or $t !~ /^\d+$/) {
605 &::DEBUG("warn: t is undefined for news{$chan}{$_}{Time}; removing item.");
606 delete $::news{$chan}{$_};
613 # number to string resolution.
614 if ($what =~ /^\d+$/) {
615 foreach (sort { $a <=> $b } keys %time) {
617 return $time{$_} if ($item == $what);
621 # partial string to full string resolution
625 foreach (sort { $a <=> $b } keys %time) {
627 $no = $item if ($time{$_} eq $what);
628 push(@items, $time{$_}) if ($time{$_} =~ /\Q$what\E/i);
631 # since we have so much built into this function, there is so
632 # many guesses we can make.
633 # todo: split this command in the future into:
634 # full_string->number and number->string
635 # partial_string->full_string
636 if (defined $no and !@items) {
637 &::DEBUG("string->number resolution.");
641 if (scalar @items > 1) {
642 &::DEBUG("Multiple matches, not guessing.");
643 &::msg($::who, "Multiple matches, not guessing.");
648 &::DEBUG("gNI: Guessed '$items[0]'.");
651 &::DEBUG("gNI: No match.");
656 &::ERROR("getNewsItem: Should not happen (what = $what)");
661 my($what,$value) = @_;
663 if (!defined $chan) {
664 &::DEBUG("do_set: chan not defined.");
668 if (!defined $what or $what =~ /^\s*$/) {
669 &::DEBUG("what $what is not defined.");
672 if (!defined $value or $value =~ /^\s*$/) {
673 &::DEBUG("value $value is not defined.");
677 &::DEBUG("do_set: TODO...");