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