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") {
25 # todo: check if the channel exists aswell.
26 if (defined $what and $what =~ s/^($::mask{chan})\s*//) {
34 my @chans = &::GetNickInChans($::who);
36 if (scalar @chans > 1) {
37 &::msg($::who, "error: I dunno which channel you are referring to since you're on more than one.");
41 if (scalar @chans == 0) {
42 &::msg($::who, "error: I couldn't find you on any chan. This must be a bug!");
47 &::DEBUG("Guessed $::who being on chan $chan");
50 if (!defined $what or $what =~ /^\s*$/) {
55 if ($what =~ /^add(\s+(.*))?$/i) {
57 } elsif ($what =~ /^del(\s+(.*))?$/i) {
59 } elsif ($what =~ /^mod(\s+(.*))?$/i) {
61 } elsif ($what =~ /^set(\s+(.*))?$/i) {
63 } elsif ($what =~ /^(\d)$/i) {
64 &::DEBUG("read shortcut called.");
66 } elsif ($what =~ /^read(\s+(.*))?$/i) {
68 } elsif ($what =~ /^list$/i) {
69 &::DEBUG("list longcut called.");
71 } elsif ($what =~ /^(expire|text|desc)(\s+(.*))?$/i) {
74 my($arg1,$arg2) = split(/\s+/, $3, 2);
75 &set("$arg1 Text $arg2");
76 } elsif ($what =~ /^help(\s+(.*))?$/i) {
79 &::DEBUG("could not parse '$what'.");
80 &::msg($::who, "unknown command: $what");
85 my $file = "$::bot_base_dir/blootbot-news.txt";
91 &::DEBUG("readNews: fileno exists, should never happen.");
102 # todo: allow commands.
104 if (/^[\s\t]+(\S+):[\s\t]+(.*)$/) {
105 if (!defined $item) {
106 &::DEBUG("!defined item, never happen!");
109 $::news{$chan}{$item}{$1} = $2;
113 # U <chan> <nick> <time>
114 if (/^U\s+(\S+)\s+(\S+)\s+(\d+)$/) {
115 $::newsuser{$1}{$2} = $3;
120 if (/^(\S+)[\s\t]+(.*)$/) {
128 &::status("News: Read $ci items for ".scalar(keys %::news)
129 ." chans, $cu users cache");
133 my $file = "$::bot_base_dir/blootbot-news.txt";
136 &::ERROR("fileno NEWS exists, should never happen.");
140 # todo: add commands to output file.
142 my($cc,$ci,$cu) = (0,0,0);
143 open(NEWS, ">$file");
144 foreach $chan (sort keys %::news) {
145 $c = scalar keys %{ $::news{$chan} };
149 foreach $item (sort keys %{ $::news{$chan} }) {
150 $c = scalar keys %{ $::news{$chan}{$item} };
154 print NEWS "$chan $item\n";
155 foreach $what (sort keys %{ $::news{$chan}{$item} }) {
156 print NEWS " $what: $::news{$chan}{$item}{$what}\n";
162 # todo: show how many users we wrote down.
163 if (&::getChanConfList("newsKeepRead")) {
164 # old users are removed in newsFlush(), perhaps it should be
166 &::DEBUG("newsuser cache...");
167 foreach $chan (sort keys %::newsuser) {
168 &::DEBUG("newsuser cache: chan => $chan");
169 foreach (sort keys %{ $::newsuser{$chan} }) {
170 &::DEBUG("newsuser cache: user => $_");
171 print NEWS "U $chan $_ $::newsuser{$chan}{$_}\n";
179 &::status("News: Wrote $ci items for $cc chans, $cu user cache.");
185 if (!defined $chan or !defined $str or $str =~ /^\s*$/) {
190 if (length $str > 64) {
191 &::msg($::who, "That's not really an item (>64chars)");
195 if (exists $::news{$chan}{$str}{Time}) {
196 &::msg($::who, "'$str' for $chan already exists!");
200 $::news{$chan}{$str}{Time} = time();
201 my $expire = &::getChanConfDefault("newsDefaultExpire",7);
202 $::news{$chan}{$str}{Expire} = time() + $expire*60*60*24;
203 $::news{$chan}{$str}{Author} = $::who;
205 my $agestr = &::Time2String($::news{$chan}{$str}{Expire} - time() );
206 my $item = &getNewsItem($str);
207 &::msg($::who, "Added '\037$str\037' at [".localtime(time).
208 "] by \002$::who\002 for item #\002$item\002.");
209 &::msg($::who, "Now do 'news text $item <your_description>'");
210 &::msg($::who, "This item will expire at \002".
211 localtime($::news{$chan}{$str}{Expire})."\002 [$agestr] "
219 if (!defined $what) {
224 if ($what =~ /^\d+$/) {
225 my $count = scalar keys %{ $::news{$chan} };
227 &::msg($::who, "No news for $chan.");
231 if ($what > $count or $what < 0) {
232 &::msg($::who, "$what is out of range (max $count)");
236 $item = &getNewsItem($what);
237 &::DEBUG("del: num: item => $item ($what)");
238 $what = $item; # hack hack hack.
241 $_ = &getNewsItem($what); # hack hack hack.
242 $what = $_ if (defined $_);
244 if (!exists $::news{$chan}{$what}) {
246 foreach (keys %{ $::news{$chan} }) {
247 next unless (/\Q$what\E/);
251 if (!scalar @found) {
252 &::msg($::who, "could not find $what.");
256 if (scalar @found > 1) {
257 &::msg($::who, "too many matches for $what.");
262 &::DEBUG("del: str: guessed what => $what");
266 if (exists $::news{$chan}{$what}) {
268 $auth++ if ($::who eq $::news{$chan}{$what}{Author});
269 $auth++ if (&::IsFlag("o"));
272 # todo: show when it'll expire.
273 &::msg($::who, "Sorry, you cannot remove items; just let them expire on their own.");
277 &::msg($::who, "ok, deleted '$what' from \002$chan\002...");
278 delete $::news{$chan}{$what};
280 &::msg($::who, "error: not found $what in news for $chan.");
285 if (!scalar keys %{ $::news{$chan} }) {
286 &::msg($::who, "No News for \002$chan\002.");
290 if (&::IsChanConf("newsKeepRead")) {
291 $::newsuser{$chan}{$::who} = time();
294 &::msg($::who, "|==== News for \002$chan\002:");
296 foreach (keys %{ $::news{$chan} }) {
297 my $t = $::news{$chan}{$_}{Time};
298 $newest = $t if ($t > $newest);
300 &::msg($::who, "|= Last updated ".
301 &::Time2String(time() - $newest). " ago.");
302 &::msg($::who, " \037No\037 \037Item ".(" "x40)." \037");
305 foreach ( &getNewsAll() ) {
307 my $setby = $::news{$chan}{$subtopic}{Author};
309 if (!defined $subtopic) {
310 &::DEBUG("warn: subtopic == undef.");
314 # todo: show request stats aswell.
315 &::msg($::who, sprintf("\002[\002%2d\002]\002 %s",
319 &::msg($::who, "|= End of News.");
320 &::msg($::who, "use 'news read <#>' or 'news read <keyword>'");
322 &::DEBUG("news cache => ".scalar(keys %::newsuser) );
328 if (!defined $chan or !defined $str or $str =~ /^\s*$/) {
329 &::help("news read");
333 if (!scalar keys %{ $::news{$chan} }) {
334 &::msg($::who, "No News for \002$chan\002.");
338 # my $item = (exists $::news{$chan}{$str}) ? $str : &getNewsItem($str);
339 my $item = &getNewsItem($str);
340 if (!defined $item or !scalar keys %{ $::news{$chan}{$item} }) {
341 &::msg($::who, "No news item called '$str'");
345 if (!exists $::news{$chan}{$item}{Text}) {
346 &::msg($::who, "Someone forgot to add info to this news item");
350 # todo: show item number.
351 # todo: show ago-time aswell?
352 # todo: show request stats aswell.
353 my $t = localtime($::news{$chan}{$item}{Time});
354 my $a = $::news{$chan}{$item}{Author};
355 &::msg($::who, "+- News \002$chan\002 ##, item '\037$item\037':");
356 &::msg($::who, "| Added by $a at $t");
357 &::msg($::who, $::news{$chan}{$item}{Text});
359 $::news{$chan}{$item}{'Request_By'} = $::who;
360 $::news{$chan}{$item}{'Request_Time'} = time();
361 $::news{$chan}{$item}{'Request_Count'}++;
365 my($item, $str) = split /\s+/, $_[0], 2;
367 if (!defined $item or $item eq "" or $str =~ /^\s*$/) {
372 my $news = &getNewsItem($item);
374 if (!defined $news) {
375 &::DEBUG("error: mod: news == undefined.");
378 my $nnews = $::news{$chan}{$news}{Text};
379 my $mod_news = $news;
380 my $mod_nnews = $nnews;
383 if ($str =~ m|^\s*s([/,#\|])(.+?)\1(.*?)\1([a-z]*);?\s*$|) {
384 my ($delim, $op, $np, $flags) = ($1,$2,$3,$4);
386 if ($flags !~ /^(g)?$/) {
387 &::msg($::who, "error: Invalid flags to regex.");
391 ### TODO: use m### to make code safe!
392 # todo: make code safer.
394 # todo: use eval to deal with flags easily.
396 $done++ if (!$done and $mod_news =~ s/\Q$op\E/$np/);
397 $done++ if (!$done and $mod_nnews =~ s/\Q$op\E/$np/);
398 } elsif ($flags eq "g") {
399 $done++ if ($mod_news =~ s/\Q$op\E/$np/g);
400 $done++ if ($mod_nnews =~ s/\Q$op\E/$np/g);
404 &::msg($::who, "warning: regex not found in news.");
408 if ($mod_news ne $news) { # news item.
409 if (exists $::news{$chan}{$mod_news}) {
410 &::msg($::who, "item '$mod_news' already exists.");
414 &::msg($::who, "Moving item '$news' to '$mod_news' with SAR s/$op/$np/.");
415 foreach (keys %{ $::news{$chan}{$news} }) {
416 $::news{$chan}{$mod_news}{$_} = $::news{$chan}{$news}{$_};
417 delete $::news{$chan}{$news}{$_};
420 delete $::news{$chan}{$news};
423 if ($mod_nnews ne $nnews) { # news Text/Description.
424 &::msg($::who, "Changing text for '$news' SAR s/$op/$np/.");
425 if ($mod_news ne $news) {
426 $::news{$chan}{$mod_news}{Text} = $mod_nnews;
428 $::news{$chan}{$news}{Text} = $mod_nnews;
434 &::msg($::who, "error: that regex failed ;(");
438 &::msg($::who, "error: Invalid regex. Try s/1/2/, s#3#4#...");
443 $args =~ /^(\S+)\s+(\S+)\s+(.*)$/;
444 my($item, $what, $value) = ($1,$2,$3);
446 &::DEBUG("set called.");
453 &::DEBUG("item => '$item'.");
454 my $news = &getNewsItem($item);
455 &::DEBUG("news => '$news'");
457 if (!defined $news) {
458 &::msg($::who, "Could not find item '$item' substring or # in news list.");
462 # list all values for chan.
463 if (!defined $what) {
469 my @elements = ("Expire","Text");
470 foreach (@elements) {
471 next unless ($what =~ /^$_$/i);
478 &::msg($::who, "Invalid set. Try: @elements");
483 if (!defined $value) {
488 if (!exists $::news{$chan}{$news}) {
489 &::msg($::who, "news '$news' does not exist");
493 if ($what eq "Expire") {
494 # todo: use do_set().
497 my $plus = ($value =~ s/^\+//g);
498 while ($value =~ s/^(\d+)(\S*)\s*//) {
499 my($int,$unit) = ($1,$2);
500 $time += $int if ($unit =~ /^s(ecs?)?$/i);
501 $time += $int*60 if ($unit =~ /^m(in(utes?)?)?$/i);
502 $time += $int*60*60 if ($unit =~ /^h(ours?)?$/i);
503 $time += $int*60*60*24 if (!$unit or $unit =~ /^d(ays?)?$/i);
504 $time += $int*60*60*24*7 if ($unit =~ /^w(eeks?)?$/i);
505 $time += $int*60*60*24*30 if ($unit =~ /^mon(th)?$/i);
508 if ($value =~ s/^never$//i) {
515 # from creation of item.
516 $time += $::news{$chan}{$news}{Time};
519 if (!$time or ($value and $value !~ /^never$/i)) {
520 &::DEBUG("set: Expire... need to parse.");
525 &::msg($::who, "Set never expire for \002$item\002." );
526 } elsif ($time < -1) {
527 &::DEBUG("time should never be negative ($time).");
530 &::msg($::who, "Set expire for \002$item\002, to ".
531 localtime($time) ." [".&::Time2String($time - time())."]" );
533 if (time() > $time) {
534 &::DEBUG("hrm... time() > $time, should expire.");
539 $::news{$chan}{$news}{Expire} = $time;
545 &::DEBUG("who => '$::who'");
546 my $author = $::news{$chan}{$news}{Author};
547 $auth++ if ($::who eq $author);
548 $auth++ if (&::IsFlag("o"));
549 if (!defined $author) {
550 &::DEBUG("news{$chan}{$news}{Author} is not defined! auth'd anyway");
551 $::news{$chan}{$news}{Author} = $::who;
557 # todo: show when it'll expire.
558 &::msg($::who, "Sorry, you cannot set items. (author $author owns it)");
562 my $old = $::news{$chan}{$news}{$what};
564 &::DEBUG("old => $old.");
566 $::news{$chan}{$news}{$what} = $value;
567 &::msg($::who, "Setting [$chan]/{$news}/<$what> to '$value'.");
576 foreach (keys %{ $::news{$chan} }) {
577 $time{ $::news{$chan}{$_}{Time} } = $_;
581 foreach (sort { $a <=> $b } keys %time) {
582 push(@items, $time{$_});
593 foreach (keys %{ $::news{$chan} }) {
594 my $t = $::news{$chan}{$_}{Time};
596 if (!defined $t or $t !~ /^\d+$/) {
597 &::DEBUG("warn: t is undefined for news{$chan}{$_}{Time}; removing item.");
598 delete $::news{$chan}{$_};
605 # number to string resolution.
606 if ($what =~ /^\d+$/) {
607 foreach (sort { $a <=> $b } keys %time) {
609 return $time{$_} if ($item == $what);
613 # partial string to full string resolution
617 foreach (sort { $a <=> $b } keys %time) {
619 $no = $item if ($time{$_} eq $what);
620 push(@items, $time{$_}) if ($time{$_} =~ /\Q$what\E/i);
623 # since we have so much built into this function, there is so
624 # many guesses we can make.
625 # todo: split this command in the future into:
626 # full_string->number and number->string
627 # partial_string->full_string
628 if (defined $no and !@items) {
629 &::DEBUG("string->number resolution.");
633 if (scalar @items > 1) {
634 &::DEBUG("Multiple matches, not guessing.");
635 &::msg($::who, "Multiple matches, not guessing.");
640 &::DEBUG("gNI: Guessed '$items[0]'.");
643 &::DEBUG("gNI: No match.");
648 &::ERROR("getNewsItem: Should not happen (what = $what)");
653 my($what,$value) = @_;
655 if (!defined $chan) {
656 &::DEBUG("do_set: chan not defined.");
660 if (!defined $what or $what =~ /^\s*$/) {
661 &::DEBUG("what $what is not defined.");
664 if (!defined $value or $value =~ /^\s*$/) {
665 &::DEBUG("value $value is not defined.");
669 &::DEBUG("do_set: TODO...");