welcome.pm 1.3 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344
  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_raw_public => sub {
  19. BotIrc::check_ctx() or return 1;
  20. my $ev = $_[ARG0];
  21. my $msg = $ev->{params}[1];
  22. my $source = BotIrc::ctx_source();
  23. my $chan = BotIrc::ctx_target('wisdom');
  24. return 0 if defined $welcomed_users{lc $chan}{lc $source};
  25. # We'll want to check this first; no need to flag someone if
  26. # the channel doesn't have a welcome message in the first place
  27. my $msg;
  28. return 0 if !defined($msg = $BotIrc::config->{welcome_channels}{lc $chan});
  29. # No hi first thing? Put them on the naughty list
  30. goto flag_as_welcomed if $msg !~ /^(?:hi|hello|hey)\b/i;
  31. # arbitrary threshold; prevent responding to an actual question
  32. goto flag_as_welcomed if length($msg) > 15;
  33. BotIrc::ctx_set_addressee($source);
  34. BotIrc::send_wisdom($msg);
  35. flag_as_welcomed:
  36. $BotDb::db->do("INSERT INTO welcomed (nick, channel) VALUES(?,?)", {}, lc $source, lc $chan);
  37. $welcomed_users{lc $chan} //= {};
  38. $welcomed_users{lc $chan}{lc $source} = 1;
  39. return 0;
  40. },
  41. };