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