PageRenderTime 54ms CodeModel.GetById 16ms RepoModel.GetById 0ms app.codeStats 0ms

/branches/don/proposed/src/Modules/News.pl

#
Perl | 1032 lines | 862 code | 104 blank | 66 comment | 105 complexity | c0a7a2266628d505dbf72768769a62e6 MD5 | raw file
Possible License(s): LGPL-2.0
  1. #
  2. # News.pl: Advanced news management
  3. # Author: dms
  4. # Version: v0.3 (20010412)
  5. # Created: 20010326
  6. # Notes: Testing done by greycat, kudos!
  7. #
  8. ### structure:
  9. # news{ channel }{ string } { item }
  10. # newsuser{ channel }{ user } = time()
  11. ### where item is:
  12. # Time - when it was added (used for sorting)
  13. # Author - Who by.
  14. # Expire - Time to expire.
  15. # Text - Actual text.
  16. ###
  17. package News;
  18. use strict;
  19. use vars qw($who $chan);
  20. sub Parse {
  21. my($what) = @_;
  22. $chan = undef;
  23. $who = lc $::who;
  24. if (!keys %::news) {
  25. if (!exists $::cache{newsFirst}) {
  26. &::DEBUG("news: looks like we enabled news option just then; loading up news file just in case.");
  27. $::cache{newsFirst} = 1;
  28. }
  29. &readNews();
  30. }
  31. if ($::msgType ne 'private') {
  32. $chan = $::chan;
  33. }
  34. if (defined $what and $what =~ s/^($::mask{chan})\s*//) {
  35. # TODO: check if the channel exists aswell.
  36. $chan = lc $1;
  37. if (!&::IsNickInChan($who, $chan)) {
  38. &::notice($who, "sorry but you're not on $chan.");
  39. return;
  40. }
  41. }
  42. if (!defined $chan) {
  43. my @chans = &::getNickInChans($who);
  44. if (scalar @chans > 1) {
  45. &::notice($who, "error: I dunno which channel you are referring to since you're on more than one. Try 'news #chan ...' instead");
  46. return;
  47. }
  48. if (scalar @chans == 0) {
  49. &::notice($who, "error: I couldn't find you on any chan. This must be a bug!");
  50. return;
  51. }
  52. $chan = $chans[0];
  53. &::VERB("Guessed $who being on chan $chan",2);
  54. $::chan = $chan; # hack for IsChanConf().
  55. }
  56. if (!defined $what or $what =~ /^\s*$/) {
  57. &list();
  58. return;
  59. }
  60. if ($what =~ /^add(\s+(.*))?$/i) {
  61. &add($2);
  62. } elsif ($what =~ /^del(\s+(.*))?$/i) {
  63. &del($2);
  64. } elsif ($what =~ /^mod(\s+(.*))?$/i) {
  65. &mod($2);
  66. } elsif ($what =~ /^set(\s+(.*))?$/i) {
  67. &set($2);
  68. } elsif ($what =~ /^(\d+)$/i) {
  69. &::VERB("News: read shortcut called.",2);
  70. &read($1);
  71. } elsif ($what =~ /^read(\s+(.*))?$/i) {
  72. &read($2);
  73. } elsif ($what =~ /^(latest|new)(\s+(.*))?$/i) {
  74. &latest($3 || $chan, 1);
  75. # $::cmdstats{'News latest'}++;
  76. } elsif ($what =~ /^stats?$/i) {
  77. &stats();
  78. } elsif ($what =~ /^list$/i) {
  79. &list();
  80. } elsif ($what =~ /^(expire|text|desc)(\s+(.*))?$/i) {
  81. # shortcut/link.
  82. # nice hack.
  83. my $cmd = $1;
  84. my($arg1,$arg2) = split(/\s+/, $3, 2);
  85. &set("$arg1 $cmd $arg2");
  86. } elsif ($what =~ /^help(\s+(.*))?$/i) {
  87. &::help("news $2");
  88. } elsif ($what =~ /^newsflush$/i) {
  89. &::msg($who, "newsflush called... check out the logs!");
  90. &::newsFlush();
  91. } elsif ($what =~ /^(un)?notify$/i) {
  92. my $state = ($1) ? 0 : 1;
  93. # TODO: don't notify even if 'News' is called.
  94. if (&::IsChanConf('newsNotifyAll') <= 0) {
  95. &::DEBUG("news: chan => $chan, ::chan => $::chan.");
  96. &::notice($who, "not available for this channel or disabled altogether.");
  97. return;
  98. }
  99. my $t = $::newsuser{$chan}{$who};
  100. if ($state) { # state = 1
  101. if (defined $t and ($t == 0 or $t == -1)) {
  102. &::notice($who, "enabled notify.");
  103. delete $::newsuser{$chan}{$who};
  104. return;
  105. }
  106. &::notice($who, "already enabled.");
  107. } else { # state = 0
  108. my $x = $::newsuser{$chan}{$who};
  109. if (defined $x and ($x == 0 or $x == -1)) {
  110. &::notice($who, 'notify already disabled');
  111. return;
  112. }
  113. $::newsuser{$chan}{$who} = -1;
  114. &::notice($who, "notify is now disabled.");
  115. }
  116. } else {
  117. &::notice($who, "unknown command: $what");
  118. }
  119. }
  120. sub readNews {
  121. my $file = "$::bot_base_dir/infobot-news.txt";
  122. if (! -f $file or -z $file) {
  123. return;
  124. }
  125. if (fileno NEWS) {
  126. &::DEBUG("readNews: fileno exists, should never happen.");
  127. return;
  128. }
  129. my($item,$chan);
  130. my($ci,$cu) = (0,0);
  131. open(NEWS, $file);
  132. while (<NEWS>) {
  133. chop;
  134. # TODO: allow commands.
  135. if (/^[\s\t]+(\S+):[\s\t]+(.*)$/) {
  136. if (!defined $item) {
  137. &::DEBUG("news: !defined item, never happen!");
  138. next;
  139. }
  140. $::news{$chan}{$item}{$1} = $2;
  141. next;
  142. }
  143. # U <chan> <nick> <time>
  144. if (/^U\s+(\S+)\s+(\S+)\s+(\d+)$/) {
  145. $::newsuser{$1}{$2} = $3;
  146. $cu++;
  147. next;
  148. }
  149. if (/^(\S+)[\s\t]+(.*)$/) {
  150. $chan = $1;
  151. $item = $2;
  152. $ci++;
  153. }
  154. }
  155. close NEWS;
  156. my $cn = scalar(keys %::news);
  157. return unless ($ci or $cn or $cu);
  158. &::status("News: read ".
  159. $ci. &::fixPlural(' item', $ci). ' for '.
  160. $cn. &::fixPlural(' chan', $cn). ', '.
  161. $cu. &::fixPlural(' user', $cu), ' cache'
  162. );
  163. }
  164. sub writeNews {
  165. if (!scalar keys %::news and !scalar keys %::newsuser) {
  166. &::VERB("wN: nothing to write.",2);
  167. return;
  168. }
  169. # should define this at the top of file.
  170. my $file = "$::bot_base_dir/infobot-news.txt";
  171. if (fileno NEWS) {
  172. &::ERROR("News: write: fileno NEWS exists, should never happen.");
  173. return;
  174. }
  175. # TODO: add commands to output file.
  176. my $c = 0;
  177. my($cc,$ci,$cu) = (0,0,0);
  178. open(NEWS, ">$file");
  179. foreach $chan (sort keys %::news) {
  180. $c = scalar keys %{ $::news{$chan} };
  181. next unless ($c);
  182. $cc++;
  183. my $item;
  184. foreach $item (sort keys %{ $::news{$chan} }) {
  185. $c = scalar keys %{ $::news{$chan}{$item} };
  186. next unless ($c);
  187. $ci++;
  188. print NEWS "$chan $item\n";
  189. my $what;
  190. foreach $what (sort keys %{ $::news{$chan}{$item} }) {
  191. print NEWS " $what: $::news{$chan}{$item}{$what}\n";
  192. }
  193. print NEWS "\n";
  194. }
  195. }
  196. # TODO: show how many users we wrote down.
  197. if (&::getChanConfList('newsKeepRead')) {
  198. # old users are removed in newsFlush(), perhaps it should be
  199. # done here.
  200. foreach $chan (sort keys %::newsuser) {
  201. foreach (sort keys %{ $::newsuser{$chan} }) {
  202. print NEWS "U $chan $_ $::newsuser{$chan}{$_}\n";
  203. $cu++;
  204. }
  205. }
  206. }
  207. close NEWS;
  208. &::status("News: Wrote $ci items for $cc chans, $cu user cache.");
  209. }
  210. sub add {
  211. my($str) = @_;
  212. if (!defined $chan or !defined $str or $str =~ /^\s*$/) {
  213. &::help('news add');
  214. return;
  215. }
  216. if (length $str > 64) {
  217. &::notice($who, "That's not really an item (>64chars)");
  218. return;
  219. }
  220. if (exists $::news{$chan}{$str}{Time}) {
  221. &::notice($who, "'$str' for $chan already exists!");
  222. return;
  223. }
  224. $::news{$chan}{$str}{Time} = time();
  225. my $expire = &::getChanConfDefault('newsDefaultExpire',7, $chan);
  226. $::news{$chan}{$str}{Expire} = time() + $expire*60*60*24;
  227. $::news{$chan}{$str}{Author} = $::who; # case!
  228. my $agestr = &::Time2String($::news{$chan}{$str}{Expire} - time() );
  229. my $item = &newsS2N($str);
  230. &::notice($who, "Added '\037$str\037' at [".gmtime(time).
  231. "] by \002$::who\002 for item #\002$item\002.");
  232. &::notice($who, "Now do 'news text $item <your_description>'");
  233. &::notice($who, "This item will expire at \002".
  234. gmtime($::news{$chan}{$str}{Expire})."\002 [$agestr from now] "
  235. );
  236. &writeNews();
  237. }
  238. sub del {
  239. my($what) = @_;
  240. my $item = 0;
  241. if (!defined $what) {
  242. &::help('news del');
  243. return;
  244. }
  245. if ($what =~ /^\d+$/) {
  246. my $count = scalar keys %{ $::news{$chan} };
  247. if (!$count) {
  248. &::notice($who, "No news for $chan.");
  249. return;
  250. }
  251. if ($what > $count or $what < 0) {
  252. &::notice($who, "$what is out of range (max $count)");
  253. return;
  254. }
  255. $item = &getNewsItem($what);
  256. $what = $item; # hack hack hack.
  257. } else {
  258. $_ = &getNewsItem($what); # hack hack hack.
  259. $what = $_ if (defined $_);
  260. if (!exists $::news{$chan}{$what}) {
  261. my @found;
  262. foreach (keys %{ $::news{$chan} }) {
  263. next unless (/\Q$what\E/);
  264. push(@found, $_);
  265. }
  266. if (!scalar @found) {
  267. &::notice($who, "could not find $what.");
  268. return;
  269. }
  270. if (scalar @found > 1) {
  271. &::notice($who, "too many matches for $what.");
  272. return;
  273. }
  274. $what = $found[0];
  275. &::DEBUG("news: del: str: guessed what => $what");
  276. }
  277. }
  278. if (exists $::news{$chan}{$what}) {
  279. my $auth = 0;
  280. $auth++ if ($::who eq $::news{$chan}{$what}{Author});
  281. $auth++ if (&::IsFlag('o'));
  282. if (!$auth) {
  283. # TODO: show when it'll expire.
  284. &::notice($who, "Sorry, you cannot remove items; just let them expire on their own.");
  285. return;
  286. }
  287. &::notice($who, "ok, deleted '$what' from \002$chan\002...");
  288. delete $::news{$chan}{$what};
  289. } else {
  290. &::notice($who, "error: not found $what in news for $chan.");
  291. }
  292. }
  293. sub list {
  294. if (!scalar keys %{ $::news{$chan} }) {
  295. &::notice($who, "No news for \002$chan\002.");
  296. return;
  297. }
  298. if (&::IsChanConf('newsKeepRead') > 0) {
  299. my $x = $::newsuser{$chan}{$who};
  300. if (defined $x and ($x == 0 or $x == -1)) {
  301. &::DEBUG("news: not updating time for $who.");
  302. } else {
  303. if (!scalar keys %{ $::news{$chan} }) {
  304. &::DEBUG("news: should not add $chan/$who to cache!");
  305. }
  306. $::newsuser{$chan}{$who} = time();
  307. }
  308. }
  309. # &notice() breaks OPN :( - using msg() instead!
  310. my $count = scalar keys %{ $::news{$chan} };
  311. &::msg($who, "|==== News for \002$chan\002: ($count items)");
  312. my $newest = 0;
  313. my $expire = 0;
  314. my $eno = 0;
  315. foreach (keys %{ $::news{$chan} }) {
  316. my $t = $::news{$chan}{$_}{Time};
  317. my $e = $::news{$chan}{$_}{Expire};
  318. $newest = $t if ($t > $newest);
  319. if ($e > 1 and $e < $expire) {
  320. $expire = $e;
  321. &::DEBUG("before newsS2N($_)");
  322. $eno = &newsS2N($_);
  323. &::DEBUG("after newsS2N($_) == $eno");
  324. }
  325. }
  326. my $timestr = &::Time2String(time() - $newest);
  327. &::msg($who, "|= Last updated $timestr ago.");
  328. &::msg($who, " \037Num\037 \037Item ".(' 'x40)." \037");
  329. # &::DEBUG("news: list: expire = $expire");
  330. # &::DEBUG("news: list: eno = $eno");
  331. my $i = 1;
  332. foreach ( &getNewsAll() ) {
  333. my $subtopic = $_;
  334. my $setby = $::news{$chan}{$subtopic}{Author};
  335. my $chr = (exists $::News{$chan}{$subtopic}{Text}) ? '' : '*';
  336. if (!defined $subtopic) {
  337. &::DEBUG("news: warn: subtopic == undef.");
  338. next;
  339. }
  340. # TODO: show request stats aswell.
  341. &::msg($who, sprintf("\002[\002%2d\002]\002%s %s",
  342. $i, $chr, $subtopic));
  343. $i++;
  344. }
  345. my $z = $::newsuser{$who};
  346. if (defined $z) {
  347. &::DEBUG("cache $who: $z");
  348. } else {
  349. &::DEBUG("cache: $who doesn't have newscache set.");
  350. }
  351. &::msg($who, "|= End of News.");
  352. &::msg($who, "use 'news read <#>' or 'news read <keyword>'");
  353. }
  354. sub read {
  355. my($str) = @_;
  356. if (!defined $chan or !defined $str or $str =~ /^\s*$/) {
  357. &::help('news read');
  358. return;
  359. }
  360. if (!scalar keys %{ $::news{$chan} }) {
  361. &::notice($who, "No news for \002$chan\002.");
  362. return;
  363. }
  364. my $item = &getNewsItem($str);
  365. if (!defined $item or !scalar keys %{ $::news{$chan}{$item} }) {
  366. # TODO: numerical check.
  367. if ($str =~ /^(\d+)[-, ](\d+)$/ or
  368. $str =~ /^-(\d+)$/ or
  369. $str =~ /^(\d+)-$/ or 0
  370. ) {
  371. &::notice($who, "We don't support multiple requests of news items yet. Sorry.");
  372. return;
  373. }
  374. &::notice($who, "No news item called '$str'");
  375. return;
  376. }
  377. if (!exists $::news{$chan}{$item}{Text}) {
  378. &::notice($who, 'Someone forgot to add info to this news item');
  379. return;
  380. }
  381. my $t = gmtime( $::news{$chan}{$item}{Time} );
  382. my $a = $::news{$chan}{$item}{Author};
  383. my $text = $::news{$chan}{$item}{Text};
  384. my $num = &newsS2N($item);
  385. my $rwho = $::news{$chan}{$item}{Request_By} || $::who;
  386. my $rcount = $::news{$chan}{$item}{Request_Count} || 0;
  387. if (length $text < $::param{maxKeySize}) {
  388. &::VERB("NEWS: Possible news->factoid redirection.",2);
  389. my $f = &::getFactoid($text);
  390. if (defined $f) {
  391. &::VERB("NEWS: ok, $text is factoid redirection.",2);
  392. $f =~ s/^<REPLY>\s*//i; # anything else?
  393. $text = $f;
  394. }
  395. }
  396. $_ = $::news{$chan}{$item}{'Expire'};
  397. my $e;
  398. if ($_) {
  399. $e = sprintf("\037%s\037 [%s from now]",
  400. scalar(gmtime($_)),
  401. &::Time2String($_ - time())
  402. );
  403. }
  404. &::notice($who, "+- News \002$chan\002 #$num: $item");
  405. &::notice($who, "| Added by $a at \037$t\037");
  406. &::notice($who, "| Expire: $e") if (defined $e);
  407. &::notice($who, $text);
  408. &::notice($who, "| Requested \002$rcount\002 times, last by \002$rwho\002") if ($rcount and $rwho);
  409. $::news{$chan}{$item}{'Request_By'} = $::who;
  410. $::news{$chan}{$item}{'Request_Time'} = time();
  411. $::news{$chan}{$item}{'Request_Count'}++;
  412. }
  413. sub mod {
  414. my($item, $str) = split /\s+/, $_[0], 2;
  415. if (!defined $item or $item eq '' or $str =~ /^\s*$/) {
  416. &::help('news mod');
  417. return;
  418. }
  419. my $news = &getNewsItem($item);
  420. if (!defined $news) {
  421. &::DEBUG("news: error: mod: news == undefined.");
  422. return;
  423. }
  424. my $nnews = $::news{$chan}{$news}{Text};
  425. my $mod_news = $news;
  426. my $mod_nnews = $nnews;
  427. # SAR patch. mu++
  428. if ($str =~ m|^\s*s([/,#\|])(.+?)\1(.*?)\1([a-z]*);?\s*$|) {
  429. my ($delim, $op, $np, $flags) = ($1,$2,$3,$4);
  430. if ($flags !~ /^(g)?$/) {
  431. &::notice($who, "error: Invalid flags to regex.");
  432. return;
  433. }
  434. ### TODO: use m### to make code safe!
  435. # TODO: make code safer.
  436. my $done = 0;
  437. # TODO: use eval to deal with flags easily.
  438. if ($flags eq '') {
  439. $done++ if (!$done and $mod_news =~ s/\Q$op\E/$np/);
  440. $done++ if (!$done and $mod_nnews =~ s/\Q$op\E/$np/);
  441. } elsif ($flags eq 'g') {
  442. $done++ if ($mod_news =~ s/\Q$op\E/$np/g);
  443. $done++ if ($mod_nnews =~ s/\Q$op\E/$np/g);
  444. }
  445. if (!$done) {
  446. &::notice($who, "warning: regex not found in news.");
  447. return;
  448. }
  449. if ($mod_news ne $news) { # news item.
  450. if (exists $::news{$chan}{$mod_news}) {
  451. &::notice($who, "item '$mod_news' already exists.");
  452. return;
  453. }
  454. &::notice($who, "Moving item '$news' to '$mod_news' with SAR s/$op/$np/.");
  455. foreach (keys %{ $::news{$chan}{$news} }) {
  456. $::news{$chan}{$mod_news}{$_} = $::news{$chan}{$news}{$_};
  457. delete $::news{$chan}{$news}{$_};
  458. }
  459. # needed?
  460. delete $::news{$chan}{$news};
  461. }
  462. if ($mod_nnews ne $nnews) { # news Text/Description.
  463. &::notice($who, "Changing text for '$news' SAR s/$op/$np/.");
  464. if ($mod_news ne $news) {
  465. $::news{$chan}{$mod_news}{Text} = $mod_nnews;
  466. } else {
  467. $::news{$chan}{$news}{Text} = $mod_nnews;
  468. }
  469. }
  470. return;
  471. } else {
  472. &::notice($who, "error: that regex failed ;(");
  473. return;
  474. }
  475. &::notice($who, "error: Invalid regex. Try s/1/2/, s#3#4#...");
  476. }
  477. sub set {
  478. my($args) = @_;
  479. my($item, $what, $value);
  480. if (!defined $args) {
  481. &::DEBUG("news: set: args == NULL.");
  482. return;
  483. }
  484. $item = $1 if ($args =~ s/^(\S+)\s*//);
  485. $what = $1 if ($args =~ s/^(\S+)\s*//);
  486. $value = $args;
  487. if ($item eq '') {
  488. &::help('news set');
  489. return;
  490. }
  491. my $news = &getNewsItem($item);
  492. if (!defined $news) {
  493. &::notice($who, "Could not find item '$item' substring or # in news list.");
  494. return;
  495. }
  496. # list all values for chan.
  497. if (!defined $what or $what =~ /^\s*$/) {
  498. &::msg($who, "set: you didn't fill me on the arguments! (what and values)");
  499. return;
  500. }
  501. my $ok = 0;
  502. my @elements = ('Expire','Text');
  503. foreach (@elements) {
  504. next unless ($what =~ /^$_$/i);
  505. $what = $_;
  506. $ok++;
  507. last;
  508. }
  509. if (!$ok) {
  510. &::notice($who, "Invalid set. Try: @elements");
  511. return;
  512. }
  513. # show (read) what.
  514. if (!defined $value or $value =~ /^\s*$/) {
  515. &::msg($who, "set: you didn't fill me on the arguments! (value)");
  516. return;
  517. }
  518. if (!exists $::news{$chan}{$news}) {
  519. &::notice($who, "news '$news' does not exist");
  520. return;
  521. }
  522. if ($what eq 'Expire') {
  523. # TODO: use do_set().
  524. my $time = 0;
  525. my $plus = ($value =~ s/^\+//g);
  526. while ($value =~ s/^(\d+)(\S*)\s*//) {
  527. my($int,$unit) = ($1,$2);
  528. $time += $int if ($unit =~ /^s(ecs?)?$/i);
  529. $time += $int*60 if ($unit =~ /^m(in(utes?)?)?$/i);
  530. $time += $int*60*60 if ($unit =~ /^h(ours?)?$/i);
  531. $time += $int*60*60*24 if (!$unit or $unit =~ /^d(ays?)?$/i);
  532. $time += $int*60*60*24*7 if ($unit =~ /^w(eeks?)?$/i);
  533. $time += $int*60*60*24*30 if ($unit =~ /^mon(th)?$/i);
  534. }
  535. if ($value =~ s/^never$//i) {
  536. # never.
  537. $time = -1;
  538. } elsif ($plus) {
  539. # from now.
  540. $time += time();
  541. } else {
  542. # from creation of item.
  543. $time += $::news{$chan}{$news}{Time};
  544. }
  545. if (!$time or ($value and $value !~ /^never$/i)) {
  546. &::DEBUG("news: set: Expire... need to parse.");
  547. &::msg($who, "hrm... couldn't parse that.");
  548. return;
  549. }
  550. if ($time == -1) {
  551. &::notice($who, "Set never expire for \002$item\002." );
  552. } elsif ($time < -1) {
  553. &::DEBUG("news: time should never be negative ($time).");
  554. return;
  555. } else {
  556. &::notice($who, "Set expire for \002$item\002, to ".
  557. gmtime($time) ." [".&::Time2String($time - time())."]" );
  558. if (time() > $time) {
  559. &::DEBUG("news: hrm... time() > $time, should expire.");
  560. }
  561. }
  562. $::news{$chan}{$news}{Expire} = $time;
  563. return;
  564. }
  565. my $auth = 0;
  566. # &::DEBUG("news: who => '$who'");
  567. my $author = $::news{$chan}{$news}{Author};
  568. $auth++ if ($::who eq $author);
  569. $auth++ if (&::IsFlag('o'));
  570. if (!defined $author) {
  571. &::DEBUG("news: news{$chan}{$news}{Author} is not defined! auth'd anyway");
  572. $::news{$chan}{$news}{Author} = $::who;
  573. $author = $::who;
  574. $auth++;
  575. }
  576. if (!$auth) {
  577. # TODO: show when it'll expire.
  578. &::notice($who, "Sorry, you cannot set items. (author $author owns it)");
  579. return;
  580. }
  581. # TODO: clean this up.
  582. my $old = $::news{$chan}{$news}{$what};
  583. if (defined $old) {
  584. &::DEBUG("news: old => $old.");
  585. }
  586. $::news{$chan}{$news}{$what} = $value;
  587. &::notice($who, "Setting [$chan]/{$news}/<$what> to '$value'.");
  588. }
  589. sub latest {
  590. my ($tchan, $flag) = @_;
  591. # hack hack hack. fix later.
  592. $chan = $tchan;
  593. $who = $::who;
  594. # TODO: if chan = undefined, guess.
  595. # if (!exists $::news{$chan}) {
  596. if (!exists $::channels{$chan}) {
  597. &::notice($who, "invalid chan $chan") if ($flag);
  598. return;
  599. }
  600. my $t = $::newsuser{$chan}{$who};
  601. # if (defined $t) {
  602. # &::DEBUG("newsuser: $chan/$who == $t");
  603. # } else {
  604. # &::DEBUG("newsuser: $chan/$who == undefined");
  605. # }
  606. if (defined $t and ($t == 0 or $t == -1)) {
  607. if ($flag) {
  608. &::notice($who, "if you want to read news, try \002/msg $::ident news $chan\002 or \002/msg $::ident news $chan notify\002");
  609. } else {
  610. &::DEBUG("news: not displaying any new news for $who");
  611. return;
  612. }
  613. }
  614. $::chan = $chan;
  615. return if (&::IsChanConf('newsNotifyAll') <= 0);
  616. # I don't understand this code ;)
  617. $t = 1 if (!defined $t);
  618. if (!defined $t) {
  619. # &::msg($who, "News is disabled for $chan");
  620. &::DEBUG("news: $chan: something went really wrong.");
  621. return;
  622. }
  623. my @new;
  624. foreach (keys %{ $::news{$chan} }) {
  625. next if (!defined $t);
  626. next if ($t > $::news{$chan}{$_}{Time});
  627. # don't list new items if they don't have Text.
  628. if (!exists $::news{$chan}{$_}{Text}) {
  629. if (time() - $::news{$chan}{$_}{Time} > 60*60*24*3) {
  630. &::DEBUG("deleting news{$chan}{$_} because it was too old and had no text info.");
  631. delete $::news{$chan}{$_};
  632. }
  633. next;
  634. }
  635. push(@new, $_);
  636. }
  637. # !scalar @new, $flag
  638. if (!scalar @new and $flag) {
  639. &::notice($who, "no new news for $chan for $who.");
  640. # valid to set this?
  641. $::newsuser{$chan}{$who} = time();
  642. return;
  643. }
  644. # scalar @new, !$flag
  645. my $unread = scalar @new;
  646. my $total = scalar keys %{ $::news{$chan} };
  647. if (!$flag && &::IsChanConf('newsTellUnread') <= 0) {
  648. return;
  649. }
  650. if (!$flag) {
  651. return unless ($unread);
  652. # just a temporary measure not to flood ourself off the
  653. # network with news until we get global notice() and msg()
  654. # throttling.
  655. if (time() - ($::cache{newsTime} || 0) < 5) {
  656. &::status("news: not displaying latest notice to $who/$chan.");
  657. return;
  658. }
  659. $::cache{newsTime} = time();
  660. my $reply = "There are unread news in $chan ($unread unread, $total total). /msg $::ident news $::chan latest";
  661. $reply .= " If you don't want further news notification, /msg $::ident news unnotify" if ($unread == $total);
  662. &::notice($who, $reply);
  663. return;
  664. }
  665. # scalar @new, $flag
  666. if (scalar @new) {
  667. &::notice($who, "+==== New news for \002$chan\002 ($unread new; $total total):");
  668. my $t = $::newsuser{$chan}{$who};
  669. if (defined $t and $t > 1) {
  670. my $timestr = &::Time2String( time() - $t );
  671. &::notice($who, "|= Last time read $timestr ago");
  672. }
  673. my $i;
  674. my @sorted;
  675. foreach (@new) {
  676. $i = &newsS2N($_);
  677. $sorted[$i] = $_;
  678. }
  679. for ($i=0; $i<=scalar(@sorted); $i++) {
  680. my $news = $sorted[$i];
  681. next unless (defined $news);
  682. # my $age = time() - $::news{$chan}{$news}{Time};
  683. my $msg = sprintf("\002[\002%2d\002]\002 %s", $i, $news);
  684. ### $i, $_, &::Time2String($age)
  685. $::conn->schedule(int((2+$i)/2), sub {
  686. &::notice($who, $msg);
  687. } );
  688. }
  689. # TODO: implement throttling via schedule into &notice() / &msg().
  690. $::conn->schedule(int((2+$i)/2), sub {
  691. &::notice($who, "|= to read, do \002news $chan read <#>\002 or \002news $chan read <keyword>\002");
  692. } );
  693. # lame hack to prevent dupes if we just ignore it.
  694. my $x = $::newsuser{$chan}{$who};
  695. if (defined $x and ($x == 0 or $x == -1)) {
  696. &::DEBUG("news: not updating time for $who. (2)");
  697. } else {
  698. $::newsuser{$chan}{$who} = time();
  699. }
  700. }
  701. }
  702. ###
  703. ### helpers...
  704. ###
  705. sub getNewsAll {
  706. my %time;
  707. foreach (keys %{ $::news{$chan} }) {
  708. $time{ $::news{$chan}{$_}{Time} } = $_;
  709. }
  710. my @items;
  711. foreach (sort { $a <=> $b } keys %time) {
  712. push(@items, $time{$_});
  713. }
  714. return @items;
  715. }
  716. sub newsS2N {
  717. my($what) = @_;
  718. my $item = 0;
  719. my @items;
  720. my $no;
  721. my %time;
  722. foreach (keys %{ $::news{$chan} }) {
  723. my $t = $::news{$chan}{$_}{Time};
  724. if (!defined $t or $t !~ /^\d+$/) {
  725. &::DEBUG("news: warn: t is undefined for news{$chan}{$_}{Time}; removing item.");
  726. delete $::news{$chan}{$_};
  727. next;
  728. }
  729. $time{$t} = $_;
  730. }
  731. foreach (sort { $a <=> $b } keys %time) {
  732. $item++;
  733. return $item if ($time{$_} eq $what);
  734. }
  735. &::DEBUG("newsS2N($what): failed...");
  736. }
  737. sub getNewsItem {
  738. my($what) = @_;
  739. my $item = 0;
  740. $what =~ s/^\#//; # '#1' for example.
  741. my %time;
  742. foreach (keys %{ $::news{$chan} }) {
  743. my $t = $::news{$chan}{$_}{Time};
  744. if (!defined $t or $t !~ /^\d+$/) {
  745. &::DEBUG("news: warn: t is undefined for news{$chan}{$_}{Time}; removing item.");
  746. delete $::news{$chan}{$_};
  747. next;
  748. }
  749. $time{$t} = $_;
  750. }
  751. # number to string resolution.
  752. if ($what =~ /^\d+$/) {
  753. foreach (sort { $a <=> $b } keys %time) {
  754. $item++;
  755. return $time{$_} if ($item == $what);
  756. }
  757. } else {
  758. # partial string to full string resolution
  759. # in some cases, string->number resolution.
  760. my @items;
  761. my $no;
  762. foreach (sort { $a <=> $b } keys %time) {
  763. $item++;
  764. # $no = $item if ($time{$_} eq $what);
  765. ## if ($time{$_} eq $what) {
  766. ## $no = $item;
  767. ## next;
  768. ## }
  769. push(@items, $time{$_}) if ($time{$_} =~ /\Q$what\E/i);
  770. }
  771. ## if (defined $no and !@items) {
  772. ## &::DEBUG("news: string->number resolution: $what->$no.");
  773. ## return $no;
  774. ## }
  775. if (scalar @items > 1) {
  776. &::DEBUG("news: Multiple matches, not guessing.");
  777. &::notice($who, "Multiple matches, not guessing.");
  778. return;
  779. }
  780. if (@items) {
  781. # &::DEBUG("news: gNI: part_string->full_string: $what->$items[0]");
  782. return $items[0];
  783. } else {
  784. &::DEBUG("news: gNI: No match for '$what'");
  785. return;
  786. }
  787. }
  788. &::ERROR("news: gNI: should not happen (what = $what)");
  789. return;
  790. }
  791. sub do_set {
  792. my($what,$value) = @_;
  793. if (!defined $chan) {
  794. &::DEBUG("news: do_set: chan not defined.");
  795. return;
  796. }
  797. if (!defined $what or $what =~ /^\s*$/) {
  798. &::DEBUG("news: what $what is not defined.");
  799. return;
  800. }
  801. if (!defined $value or $value =~ /^\s*$/) {
  802. &::DEBUG("news: value $value is not defined.");
  803. return;
  804. }
  805. &::TODO("news: do_set:");
  806. }
  807. sub stats {
  808. &::DEBUG("News: stats called.");
  809. &::msg($who, "check my logs/console.");
  810. my($i,$j) = (0,0);
  811. # total request count.
  812. foreach $chan (keys %::news) {
  813. foreach (keys %{ $::news{$chan} }) {
  814. $i += $::news{$chan}{$_}{Request_Count};
  815. }
  816. }
  817. &::DEBUG("news: stats: total request count => $i");
  818. $i = 0;
  819. # total user cached.
  820. foreach $chan (keys %::newsuser) {
  821. $i += $::newsuser{$chan}{$_};
  822. }
  823. &::DEBUG("news: stats: total user cache => $i");
  824. $i = 0;
  825. # average latest time read.
  826. my $t = time();
  827. foreach $chan (keys %::newsuser) {
  828. $i += $t - $::newsuser{$chan}{$_};
  829. &::DEBUG(" i = $i");
  830. $j++;
  831. }
  832. &::DEBUG("news: stats: average latest time read: total time: $i");
  833. &::DEBUG("news: ... count: $j");
  834. &::DEBUG("news: average: ".sprintf("%.02f", $i/($j||1))." sec/user");
  835. $i = $j = 0;
  836. }
  837. sub AUTOLOAD { &::AUTOLOAD(@_); }
  838. 1;
  839. # vim:ts=4:sw=4:expandtab:tw=80