summaryrefslogtreecommitdiff
path: root/lib/App/smsg/Command/Talk.pm
blob: 9f853f7eb84957de33e19dc7af955ba9ad400196 (plain)
  1. package App::smsg::Command::Talk;
  2. use 5.010;
  3. use strict;
  4. use warnings;
  5. use utf8;
  6. BEGIN {
  7. $App::smsg::Command::Talk::AUTHORITY = 'https://dr.jones.dk/me#me';
  8. $App::smsg::Command::Talk::VERSION = '0.001';
  9. }
  10. use App::smsg -command;
  11. use namespace::clean;
  12. use AnyEvent;
  13. use AnyEvent::XMPP::Client;
  14. use AnyEvent::XMPP::Ext::Disco;
  15. use AnyEvent::XMPP::Ext::Version;
  16. use AnyEvent::XMPP::Ext::MUC;
  17. use AnyEvent::XMPP::Namespaces qw/xmpp_ns/;
  18. use AnyEvent::XMPP::Util qw/node_jid res_jid/;
  19. use constant abstract => q (Respond (stupidly simple) when addressed in a Jabber MUC.);
  20. use constant usage_desc => '%c talk %o <jid> <password> <talkfile> [<room>]';
  21. use constant description => <<'DESCRIPTION';
  22. Connect to a Jabber MUC (a.k.a. chatroom) and emit a (silly) reply to
  23. each message posted which starts with bot name and a colon.
  24. DESCRIPTION
  25. use constant opt_spec => (
  26. # [ 'verbose|v', "print extra stuff"],
  27. [ 'debug', "print debug stuff"],
  28. );
  29. sub validate_args {
  30. my ($self, $opt, $args) = @_;
  31. $self->usage_error("too few arguments") unless @$args >= 3;
  32. }
  33. sub execute {
  34. my ($self, $opt, $args) = @_;
  35. my ($jid, $pw, $inputfile, $room) = @$args;
  36. my @msgs;
  37. sub read_messages {
  38. my ($msgs_file) = @_;
  39. open my $f, $msgs_file
  40. or die "Couldn't open messages file: '$msgs_file'\n";
  41. (@msgs) = map { chomp; $_ } <$f>;
  42. close $f;
  43. }
  44. sub answer_to {
  45. my ($msg) = @_;
  46. my $talkmsg = $msgs[int (rand (@msgs))];
  47. "You said '$msg' but... " . $talkmsg;
  48. }
  49. binmode STDOUT, ":utf8";
  50. read_messages ($inputfile);
  51. my $j = AnyEvent->condvar;
  52. my $cl = AnyEvent::XMPP::Client->new (debug => $opt->debug);
  53. my $disco = AnyEvent::XMPP::Ext::Disco->new;
  54. my $version = AnyEvent::XMPP::Ext::Version->new;
  55. my $muc = AnyEvent::XMPP::Ext::MUC->new (disco => $disco);
  56. $cl->add_extension ($disco);
  57. $cl->add_extension ($version);
  58. $cl->add_extension ($muc);
  59. $cl->set_presence (undef, 'I\'m a talking bot.', 1);
  60. $cl->add_account ($jid, $pw);
  61. warn "connecting to $jid...\n";
  62. $cl->reg_cb (
  63. session_ready => sub {
  64. my ($cl, $acc) = @_;
  65. $muc->join_room ($acc->connection, $room, node_jid ($acc->jid));
  66. $muc->reg_cb (
  67. message => sub {
  68. my ($cl, $room, $msg, $is_echo) = @_;
  69. return if $is_echo;
  70. return if $msg->is_delayed;
  71. my $mynick = res_jid ($room->nick_jid);
  72. if ($msg->any_body =~ /^\s*\Q$mynick\E:\s+(.*?)\s*$/) {
  73. my $ans = answer_to ($1);
  74. my $repl = $msg->make_reply;
  75. $repl->add_body ($ans);
  76. $repl->send;
  77. }
  78. }
  79. );
  80. },
  81. message => sub {
  82. my ($cl, $acc, $msg) = @_;
  83. my $talkmsg = $msgs[int (rand (@msgs))];
  84. my $repl = $msg->make_reply;
  85. $repl->add_body (answer_to ($msg->any_body));
  86. warn "Got message: '".$msg->any_body."' from ".$msg->from."\n";
  87. warn "Answered: $talkmsg\n";
  88. $repl->send;
  89. },
  90. contact_request_subscribe => sub {
  91. my ($cl, $acc, $roster, $contact) = @_;
  92. $contact->send_subscribed;
  93. warn "Subscribed to ".$contact->jid."\n";
  94. },
  95. error => sub {
  96. my ($cl, $acc, $error) = @_;
  97. warn "Error encountered: ".$error->string."\n";
  98. $j->broadcast;
  99. },
  100. disconnect => sub {
  101. warn "Got disconnected: [@_]\n";
  102. $j->broadcast;
  103. },
  104. );
  105. $cl->start;
  106. $j->wait;
  107. }
  108. 1;