welcome.pm 1.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142
  1. use POE;
  2. my %welcomed_users = ();
  3. {
  4. schemata => {
  5. 0 => [
  6. "CREATE TABLE welcomed (
  7. nick TEXT PRIMARY KEY NOT NULL,
  8. channel TEXT NOT NULL)",
  9. ],
  10. },
  11. on_load => sub {
  12. my $res = $BotDb::db->selectall_arrayref("SELECT nick, channel FROM welcomed", {Slice => {}});
  13. for (@$res) {
  14. $welcomed_users{$_->{channel}} //= {};
  15. $welcomed_users{$_->{channel}}{$_->{nick}} = 1;
  16. }
  17. },
  18. irc_on_public => sub {
  19. BotIrc::check_ctx() or return 1;
  20. my $source = BotIrc::ctx_source();
  21. my $chan = BotIrc::ctx_target('wisdom');
  22. return 0 if defined $welcomed_users{lc $chan}{lc $source};
  23. # We'll want to check this first; no need to flag someone if
  24. # the channel doesn't have a welcome message in the first place
  25. my $msg;
  26. return 0 if !defined($msg = $BotIrc::config->{welcome_channels}{lc $chan});
  27. # No hi first thing? Put them on the naughty list
  28. goto flag_as_welcomed if $_[ARG2] !~ /^(?:hi|hello|hey)\b/i;
  29. # arbitrary threshold; prevent responding to an actual question
  30. goto flag_as_welcomed if length($_[ARG2]) > 15;
  31. BotIrc::ctx_set_addressee($source);
  32. BotIrc::send_wisdom($msg);
  33. flag_as_welcomed:
  34. $BotDb::db->do("INSERT INTO welcomed (nick, channel) VALUES(?,?)", {}, lc $source, lc $chan);
  35. $welcomed_users{lc $chan} //= {};
  36. $welcomed_users{lc $chan}{lc $source} = 1;
  37. return 0;
  38. },
  39. };