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