summaryrefslogtreecommitdiff
path: root/lib/App/smsg/Command/Talk.pm
blob: cffdaa511db8194f9b6c9154fc1b19aa85b985b2 (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 NetSDS::Kannel;
  20. #use Coro;
  21. use constant abstract => q (Respond (stupidly simple) when addressed in a Jabber MUC.);
  22. use constant usage_desc => '%c talk %o <jid> <password> <talkfile> [<room>]';
  23. use constant description => <<'DESCRIPTION';
  24. Connect to a Jabber MUC (a.k.a. chatroom) and emit a (silly) reply to
  25. each message posted which starts with bot name and a colon.
  26. DESCRIPTION
  27. use constant opt_spec => (
  28. ['kannelserver=s', "the Kannel server to connect to",
  29. {default => 'localhost'}],
  30. ['kannelport=i', "the Kannel port to connect to",
  31. {default => 13013}],
  32. ['kanneluser=s', "the Kannel users to authenticate as",
  33. {default => 'tester'}],
  34. ['kannelpasswd=s', "the Kannel password to authenticate with",
  35. {default => 'foobar'}],
  36. ['kannelsmsc=s', "the Kannel SMSC to correspond with"],
  37. [],
  38. # [ 'verbose|v', "print extra stuff"],
  39. [ 'debug', "print debug stuff"],
  40. );
  41. sub validate_args {
  42. my ($self, $opt, $args) = @_;
  43. $self->usage_error("too few arguments") unless @$args >= 3;
  44. }
  45. sub execute {
  46. my ($self, $opt, $args) = @_;
  47. my ($jid, $pw, $inputfile, $room) = @$args;
  48. # TODO: support overriding full URL
  49. # TODO: use URI module to construct URL
  50. my $kannel = NetSDS::Kannel->new(
  51. sendsms_url => 'http://' . $opt->kannelserver . ':' . $opt->kannelport . '/cgi-bin/sendsms',
  52. sendsms_user => $opt->kanneluser,
  53. sendsms_passwd => $opt->kannelpasswd,
  54. default_smsc => $opt->kannelsmsc,
  55. );
  56. my @msgs;
  57. sub read_messages {
  58. my ($msgs_file) = @_;
  59. open my $f, $msgs_file
  60. or die "Couldn't open messages file: '$msgs_file'\n";
  61. (@msgs) = map { chomp; $_ } <$f>;
  62. close $f;
  63. }
  64. sub answer_to {
  65. my ($msg) = @_;
  66. my $talkmsg = $msgs[int (rand (@msgs))];
  67. "You said '$msg' but... " . $talkmsg;
  68. }
  69. binmode STDOUT, ":utf8";
  70. read_messages ($inputfile);
  71. my $j = AnyEvent->condvar;
  72. my $cl = AnyEvent::XMPP::Client->new (debug => $opt->debug);
  73. my $disco = AnyEvent::XMPP::Ext::Disco->new;
  74. my $version = AnyEvent::XMPP::Ext::Version->new;
  75. my $muc = AnyEvent::XMPP::Ext::MUC->new (disco => $disco);
  76. $cl->add_extension ($disco);
  77. $cl->add_extension ($version);
  78. $cl->add_extension ($muc);
  79. $cl->set_presence (undef, 'I\'m a talking bot.', 1);
  80. $cl->add_account ($jid, $pw);
  81. warn "connecting to $jid...\n";
  82. $cl->reg_cb (
  83. session_ready => sub {
  84. my ($cl, $acc) = @_;
  85. $muc->join_room ($acc->connection, $room, node_jid ($acc->jid));
  86. $muc->reg_cb (
  87. message => sub {
  88. my ($cl, $room, $msg, $is_echo) = @_;
  89. return if $is_echo;
  90. return if $msg->is_delayed;
  91. my $mynick = res_jid ($room->nick_jid);
  92. if ($msg->any_body =~ /^\s*\Q$mynick\E:\s+(.*?)\s*$/) {
  93. my $ans = answer_to ($1);
  94. my $repl = $msg->make_reply;
  95. $repl->add_body ($ans);
  96. $repl->send;
  97. }
  98. }
  99. );
  100. },
  101. message => sub {
  102. my ($cl, $acc, $msg) = @_;
  103. my $talkmsg = $msgs[int (rand (@msgs))];
  104. my $repl = $msg->make_reply;
  105. $repl->add_body (answer_to ($msg->any_body));
  106. warn "Got message: '".$msg->any_body."' from ".$msg->from."\n";
  107. warn "Answered: $talkmsg\n";
  108. $repl->send;
  109. },
  110. contact_request_subscribe => sub {
  111. my ($cl, $acc, $roster, $contact) = @_;
  112. $contact->send_subscribed;
  113. warn "Subscribed to ".$contact->jid."\n";
  114. },
  115. error => sub {
  116. my ($cl, $acc, $error) = @_;
  117. warn "Error encountered: ".$error->string."\n";
  118. $j->broadcast;
  119. },
  120. disconnect => sub {
  121. warn "Got disconnected: [@_]\n";
  122. $j->broadcast;
  123. },
  124. );
  125. $cl->start;
  126. $j->wait;
  127. }
  128. 1;