summaryrefslogtreecommitdiff
path: root/lib/App/smsg/Command/Talk.pm
blob: c837ccac3c7689e413019b9c35abd91eb6aff9bf (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 File::Spec;
  20. use File::Slurp;
  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. # [ 'verbose|v', "print extra stuff"],
  29. [ 'debug', "print debug stuff"],
  30. );
  31. sub validate_args {
  32. my ($self, $opt, $args) = @_;
  33. $self->usage_error("too few arguments") unless @$args >= 3;
  34. }
  35. sub execute {
  36. my ($self, $opt, $args) = @_;
  37. my ($jid, $pw, $inputfile, $room) = @$args;
  38. my @msgs = read_file( File::Spec->rel2abs( $inputfile ) );
  39. chomp @msgs;
  40. sub answer_to {
  41. my ($msg, @msgs) = @_;
  42. my $talkmsg = $msgs[int (rand (@msgs))];
  43. "You said '$msg' but... " . $talkmsg;
  44. }
  45. binmode STDOUT, ":utf8";
  46. my $j = AnyEvent->condvar;
  47. my $cl = AnyEvent::XMPP::Client->new (debug => $opt->debug);
  48. my $disco = AnyEvent::XMPP::Ext::Disco->new;
  49. my $version = AnyEvent::XMPP::Ext::Version->new;
  50. my $muc = AnyEvent::XMPP::Ext::MUC->new (disco => $disco);
  51. $cl->add_extension ($disco);
  52. $cl->add_extension ($version);
  53. $cl->add_extension ($muc);
  54. $cl->set_presence (undef, 'I\'m a talking bot.', 1);
  55. $cl->add_account ($jid, $pw);
  56. warn "connecting to $jid...\n";
  57. $cl->reg_cb (
  58. session_ready => sub {
  59. my ($cl, $acc) = @_;
  60. $muc->join_room ($acc->connection, $room, node_jid ($acc->jid));
  61. $muc->reg_cb (
  62. message => sub {
  63. my ($cl, $room, $msg, $is_echo) = @_;
  64. return if $is_echo;
  65. return if $msg->is_delayed;
  66. my $mynick = res_jid ($room->nick_jid);
  67. if ($msg->any_body =~ /^\s*\Q$mynick\E:\s+(.*?)\s*$/) {
  68. my $ans = answer_to ($1, @msgs);
  69. my $repl = $msg->make_reply;
  70. $repl->add_body ($ans);
  71. $repl->send;
  72. }
  73. }
  74. );
  75. },
  76. message => sub {
  77. my ($cl, $acc, $msg) = @_;
  78. my $talkmsg = $msgs[int (rand (@msgs))];
  79. my $repl = $msg->make_reply;
  80. $repl->add_body (answer_to ($msg->any_body));
  81. warn "Got message: '".$msg->any_body."' from ".$msg->from."\n";
  82. warn "Answered: $talkmsg\n";
  83. $repl->send;
  84. },
  85. contact_request_subscribe => sub {
  86. my ($cl, $acc, $roster, $contact) = @_;
  87. $contact->send_subscribed;
  88. warn "Subscribed to ".$contact->jid."\n";
  89. },
  90. error => sub {
  91. my ($cl, $acc, $error) = @_;
  92. warn "Error encountered: ".$error->string."\n";
  93. $j->broadcast;
  94. },
  95. disconnect => sub {
  96. warn "Got disconnected: [@_]\n";
  97. $j->broadcast;
  98. },
  99. );
  100. $cl->start;
  101. $j->wait;
  102. }
  103. 1;