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);
144 open(NEWS, ">$file");
145 foreach $chan (sort keys %::news) {
146 $c = scalar keys %{ $::news{$chan} };
150 foreach $item (sort keys %{ $::news{$chan} }) {
151 $c = scalar keys %{ $::news{$chan}{$item} };
155 print NEWS "$chan $item\n";
156 foreach $what (sort keys %{ $::news{$chan}{$item} }) {
157 print NEWS " $what: $::news{$chan}{$item}{$what}\n";
163 # todo: show how many users we wrote down.
164 if (&::getChanConfList("newsKeepRead")) {
165 # old users are removed in newsFlush(), perhaps it should be
168 foreach $chan (sort keys %::newsuser) {
170 foreach (sort keys %{ $::newsuser{$chan} }) {
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);
208 &::DEBUG("item eq str ($item): should never happen.");
210 &::msg($::who, "Added '\037$str\037' at [".localtime(time).
211 "] by \002$::who\002 for item #\002$item\002.");
212 &::msg($::who, "Now do 'news text $item <your_description>'");
213 &::msg($::who, "This item will expire at \002".
214 localtime($::news{$chan}{$str}{Expire})."\002 [$agestr from now] "
222 if (!defined $what) {
227 if ($what =~ /^\d+$/) {
228 my $count = scalar keys %{ $::news{$chan} };
230 &::msg($::who, "No news for $chan.");
234 if ($what > $count or $what < 0) {
235 &::msg($::who, "$what is out of range (max $count)");
239 $item = &getNewsItem($what);
240 &::DEBUG("del: num: item => $item ($what)");
241 $what = $item; # hack hack hack.
244 $_ = &getNewsItem($what); # hack hack hack.
245 $what = $_ if (defined $_);
247 if (!exists $::news{$chan}{$what}) {
249 foreach (keys %{ $::news{$chan} }) {
250 next unless (/\Q$what\E/);
254 if (!scalar @found) {
255 &::msg($::who, "could not find $what.");
259 if (scalar @found > 1) {
260 &::msg($::who, "too many matches for $what.");
265 &::DEBUG("del: str: guessed what => $what");
269 if (exists $::news{$chan}{$what}) {
271 $auth++ if ($::who eq $::news{$chan}{$what}{Author});
272 $auth++ if (&::IsFlag("o"));
275 # todo: show when it'll expire.
276 &::msg($::who, "Sorry, you cannot remove items; just let them expire on their own.");
280 &::msg($::who, "ok, deleted '$what' from \002$chan\002...");
281 delete $::news{$chan}{$what};
283 &::msg($::who, "error: not found $what in news for $chan.");
288 if (!scalar keys %{ $::news{$chan} }) {
289 &::msg($::who, "No News for \002$chan\002.");
293 if (&::IsChanConf("newsKeepRead")) {
294 $::newsuser{$chan}{$::who} = time();
297 &::msg($::who, "|==== News for \002$chan\002:");
299 foreach (keys %{ $::news{$chan} }) {
300 my $t = $::news{$chan}{$_}{Time};
301 $newest = $t if ($t > $newest);
303 my $timestr = &::Time2String(time() - $newest);
304 &::msg($::who, "|= Last updated $timestr ago.");
305 &::msg($::who, " \037No\037 \037Item ".(" "x40)." \037");
308 foreach ( &getNewsAll() ) {
310 my $setby = $::news{$chan}{$subtopic}{Author};
312 if (!defined $subtopic) {
313 &::DEBUG("warn: subtopic == undef.");
317 # todo: show request stats aswell.
318 &::msg($::who, sprintf("\002[\002%2d\002]\002 %s",
323 &::msg($::who, "|= End of News.");
324 &::msg($::who, "use 'news read <#>' or 'news read <keyword>'");
330 if (!defined $chan or !defined $str or $str =~ /^\s*$/) {
331 &::help("news read");
335 if (!scalar keys %{ $::news{$chan} }) {
336 &::msg($::who, "No News for \002$chan\002.");
340 # my $item = (exists $::news{$chan}{$str}) ? $str : &getNewsItem($str);
341 my $item = &getNewsItem($str);
342 if (!defined $item or !scalar keys %{ $::news{$chan}{$item} }) {
343 &::msg($::who, "No news item called '$str'");
347 if (!exists $::news{$chan}{$item}{Text}) {
348 &::msg($::who, "Someone forgot to add info to this news item");
352 # todo: show item number.
353 # todo: show ago-time aswell?
354 # todo: show request stats aswell.
355 my $t = localtime($::news{$chan}{$item}{Time});
356 my $a = $::news{$chan}{$item}{Author};
357 &::msg($::who, "+- News \002$chan\002 ##, item '\037$item\037':");
358 &::msg($::who, "| Added by $a at $t");
359 &::msg($::who, $::news{$chan}{$item}{Text});
361 $::news{$chan}{$item}{'Request_By'} = $::who;
362 $::news{$chan}{$item}{'Request_Time'} = time();
363 $::news{$chan}{$item}{'Request_Count'}++;
367 my($item, $str) = split /\s+/, $_[0], 2;
369 if (!defined $item or $item eq "" or $str =~ /^\s*$/) {
374 my $news = &getNewsItem($item);
376 if (!defined $news) {
377 &::DEBUG("error: mod: news == undefined.");
380 my $nnews = $::news{$chan}{$news}{Text};
381 my $mod_news = $news;
382 my $mod_nnews = $nnews;
385 if ($str =~ m|^\s*s([/,#\|])(.+?)\1(.*?)\1([a-z]*);?\s*$|) {
386 my ($delim, $op, $np, $flags) = ($1,$2,$3,$4);
388 if ($flags !~ /^(g)?$/) {
389 &::msg($::who, "error: Invalid flags to regex.");
393 ### TODO: use m### to make code safe!
394 # todo: make code safer.
396 # todo: use eval to deal with flags easily.
398 $done++ if (!$done and $mod_news =~ s/\Q$op\E/$np/);
399 $done++ if (!$done and $mod_nnews =~ s/\Q$op\E/$np/);
400 } elsif ($flags eq "g") {
401 $done++ if ($mod_news =~ s/\Q$op\E/$np/g);
402 $done++ if ($mod_nnews =~ s/\Q$op\E/$np/g);
406 &::msg($::who, "warning: regex not found in news.");
410 if ($mod_news ne $news) { # news item.
411 if (exists $::news{$chan}{$mod_news}) {
412 &::msg($::who, "item '$mod_news' already exists.");
416 &::msg($::who, "Moving item '$news' to '$mod_news' with SAR s/$op/$np/.");
417 foreach (keys %{ $::news{$chan}{$news} }) {
418 $::news{$chan}{$mod_news}{$_} = $::news{$chan}{$news}{$_};
419 delete $::news{$chan}{$news}{$_};
422 delete $::news{$chan}{$news};
425 if ($mod_nnews ne $nnews) { # news Text/Description.
426 &::msg($::who, "Changing text for '$news' SAR s/$op/$np/.");
427 if ($mod_news ne $news) {
428 $::news{$chan}{$mod_news}{Text} = $mod_nnews;
430 $::news{$chan}{$news}{Text} = $mod_nnews;
436 &::msg($::who, "error: that regex failed ;(");
440 &::msg($::who, "error: Invalid regex. Try s/1/2/, s#3#4#...");
445 $args =~ /^(\S+)\s+(\S+)\s+(.*)$/;
446 my($item, $what, $value) = ($1,$2,$3);
448 &::DEBUG("set called.");
455 &::DEBUG("item => '$item'.");
456 my $news = &getNewsItem($item);
457 &::DEBUG("news => '$news'");
459 if (!defined $news) {
460 &::msg($::who, "Could not find item '$item' substring or # in news list.");
464 # list all values for chan.
465 if (!defined $what) {
471 my @elements = ("Expire","Text");
472 foreach (@elements) {
473 next unless ($what =~ /^$_$/i);
480 &::msg($::who, "Invalid set. Try: @elements");
485 if (!defined $value) {
490 if (!exists $::news{$chan}{$news}) {
491 &::msg($::who, "news '$news' does not exist");
495 if ($what eq "Expire") {
496 # todo: use do_set().
499 my $plus = ($value =~ s/^\+//g);
500 while ($value =~ s/^(\d+)(\S*)\s*//) {
501 my($int,$unit) = ($1,$2);
502 $time += $int if ($unit =~ /^s(ecs?)?$/i);
503 $time += $int*60 if ($unit =~ /^m(in(utes?)?)?$/i);
504 $time += $int*60*60 if ($unit =~ /^h(ours?)?$/i);
505 $time += $int*60*60*24 if (!$unit or $unit =~ /^d(ays?)?$/i);
506 $time += $int*60*60*24*7 if ($unit =~ /^w(eeks?)?$/i);
507 $time += $int*60*60*24*30 if ($unit =~ /^mon(th)?$/i);
510 if ($value =~ s/^never$//i) {
517 # from creation of item.
518 $time += $::news{$chan}{$news}{Time};
521 if (!$time or ($value and $value !~ /^never$/i)) {
522 &::DEBUG("set: Expire... need to parse.");
527 &::msg($::who, "Set never expire for \002$item\002." );
528 } elsif ($time < -1) {
529 &::DEBUG("time should never be negative ($time).");
532 &::msg($::who, "Set expire for \002$item\002, to ".
533 localtime($time) ." [".&::Time2String($time - time())."]" );
535 if (time() > $time) {
536 &::DEBUG("hrm... time() > $time, should expire.");
541 $::news{$chan}{$news}{Expire} = $time;
547 &::DEBUG("who => '$::who'");
548 my $author = $::news{$chan}{$news}{Author};
549 $auth++ if ($::who eq $author);
550 $auth++ if (&::IsFlag("o"));
551 if (!defined $author) {
552 &::DEBUG("news{$chan}{$news}{Author} is not defined! auth'd anyway");
553 $::news{$chan}{$news}{Author} = $::who;
559 # todo: show when it'll expire.
560 &::msg($::who, "Sorry, you cannot set items. (author $author owns it)");
564 my $old = $::news{$chan}{$news}{$what};
566 &::DEBUG("old => $old.");
568 $::news{$chan}{$news}{$what} = $value;
569 &::msg($::who, "Setting [$chan]/{$news}/<$what> to '$value'.");
578 foreach (keys %{ $::news{$chan} }) {
579 $time{ $::news{$chan}{$_}{Time} } = $_;
583 foreach (sort { $a <=> $b } keys %time) {
584 push(@items, $time{$_});
595 foreach (keys %{ $::news{$chan} }) {
596 my $t = $::news{$chan}{$_}{Time};
598 if (!defined $t or $t !~ /^\d+$/) {
599 &::DEBUG("warn: t is undefined for news{$chan}{$_}{Time}; removing item.");
600 delete $::news{$chan}{$_};
607 # number to string resolution.
608 if ($what =~ /^\d+$/) {
609 foreach (sort { $a <=> $b } keys %time) {
611 return $time{$_} if ($item == $what);
615 # partial string to full string resolution
619 foreach (sort { $a <=> $b } keys %time) {
621 $no = $item if ($time{$_} eq $what);
622 push(@items, $time{$_}) if ($time{$_} =~ /\Q$what\E/i);
625 # since we have so much built into this function, there is so
626 # many guesses we can make.
627 # todo: split this command in the future into:
628 # full_string->number and number->string
629 # partial_string->full_string
630 if (defined $no and !@items) {
631 &::DEBUG("string->number resolution.");
635 if (scalar @items > 1) {
636 &::DEBUG("Multiple matches, not guessing.");
637 &::msg($::who, "Multiple matches, not guessing.");
642 &::DEBUG("gNI: Guessed '$items[0]'.");
645 &::DEBUG("gNI: No match.");
650 &::ERROR("getNewsItem: Should not happen (what = $what)");
655 my($what,$value) = @_;
657 if (!defined $chan) {
658 &::DEBUG("do_set: chan not defined.");
662 if (!defined $what or $what =~ /^\s*$/) {
663 &::DEBUG("what $what is not defined.");
666 if (!defined $value or $value =~ /^\s*$/) {
667 &::DEBUG("value $value is not defined.");
671 &::DEBUG("do_set: TODO...");