faq.pm 1.9 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667
  1. use Encode;
  2. use JSON;
  3. use POE;
  4. my $faq_cacheupdate = sub {
  5. return 1 if defined $BotIrc::heap{faq_cache};
  6. my $error = shift || sub {};
  7. my $faq = BotIrc::read_file($BotIrc::config->{faq_cachefile}) or do {
  8. BotIrc::error("FAQ cache broken: $!");
  9. $error->("FAQ cache is broken. The bot owner has been notified.");
  10. return 0;
  11. };
  12. while ($faq =~ /<span id="([a-z-]+)" title="(.*?)">/g) {
  13. $BotIrc::heap{faq_cache}{$1} = $2;
  14. }
  15. return 1;
  16. };
  17. {
  18. on_load => sub {
  19. $BotIrc::heap{faq_cache} = undef;
  20. },
  21. before_unload => sub {
  22. delete $BotIrc::heap{faq_cache};
  23. },
  24. control_commands => {
  25. faq_list => sub {
  26. my ($client, $data, @args) = @_;
  27. $faq_cacheupdate->(sub { send($client, "error", "faqcache_broken", $_); }) or return;
  28. # Hack to get rid of spurious double encoding
  29. BotCtl::send($client, "ok", encode('iso-8859-1', to_json($BotIrc::heap{faq_cache}, {canonical => 1})));
  30. },
  31. },
  32. irc_commands => {
  33. faq_update => sub {
  34. my ($source, $targets, $args, $auth) = @_;
  35. BotIrc::check_ctx(authed => 1) or return;
  36. system("wget --no-check-certificate -q -O '$BotIrc::config->{faq_cachefile}' '$BotIrc::config->{faq_geturl}' &");
  37. BotIrc::send_noise("FAQ is updating. Please allow a few seconds before using again.");
  38. $BotIrc::heap{faq_cache} = undef;
  39. return 1;
  40. }
  41. },
  42. irc_on_raw_anymsg => sub {
  43. my $ev = $_[ARG0];
  44. my $msg = $ev->{params}[1];
  45. return 0 if (msg !~ /\bfaq\s+([a-z-]+)/);
  46. BotIrc::check_ctx(wisdom_auto_redirect => 1) or return 0;
  47. $faq_cacheupdate->(\&BotIrc::send_noise) or return 1;
  48. while ($msg =~ /\bfaq\s+([a-z-]+)/g) {
  49. my $page = $1;
  50. next if (!exists $BotIrc::heap{faq_cache}{$page});
  51. my $info = $BotIrc::heap{faq_cache}{$page};
  52. if ($info) {
  53. $info .= "; more details available at";
  54. } else {
  55. $info = "please see the FAQ page at";
  56. }
  57. BotIrc::send_wisdom("$info $BotIrc::config->{faq_baseurl}#$page");
  58. }
  59. return 0;
  60. },
  61. };