| 1 |
#!/opt/perl/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::Namespaces qw/xmpp_ns/; |
| 9 |
|
| 10 |
my @msgs; |
| 11 |
|
| 12 |
sub read_messages { |
| 13 |
my ($msgs_file) = @_; |
| 14 |
open my $f, $msgs_file |
| 15 |
or die "Couldn't open messages file: '$msgs_file'\n"; |
| 16 |
(@msgs) = map { chomp; $_ } <$f>; |
| 17 |
close $f; |
| 18 |
} |
| 19 |
|
| 20 |
binmode STDOUT, ":utf8"; |
| 21 |
|
| 22 |
my ($jid, $pw, $inputfile) = @ARGV; |
| 23 |
|
| 24 |
unless (@ARGV >= 3) { |
| 25 |
warn "usage: talkbot <jid> <password> <talkfile>\n"; |
| 26 |
exit; |
| 27 |
} |
| 28 |
|
| 29 |
read_messages ($inputfile); |
| 30 |
|
| 31 |
my $j = AnyEvent->condvar; |
| 32 |
my $cl = AnyEvent::XMPP::Client->new (debug => 1); |
| 33 |
my $disco = AnyEvent::XMPP::Ext::Disco->new; |
| 34 |
my $version = AnyEvent::XMPP::Ext::Version->new; |
| 35 |
|
| 36 |
$cl->add_extension ($disco); |
| 37 |
$cl->add_extension ($version); |
| 38 |
|
| 39 |
$cl->set_presence (undef, 'I\'m a talking bot.', 1); |
| 40 |
|
| 41 |
$cl->add_account ($jid, $pw); |
| 42 |
warn "connecting to $jid...\n"; |
| 43 |
|
| 44 |
$cl->reg_cb ( |
| 45 |
session_ready => sub { |
| 46 |
my ($cl, $acc) = @_; |
| 47 |
warn "connected!\n"; |
| 48 |
}, |
| 49 |
message => sub { |
| 50 |
my ($cl, $acc, $msg) = @_; |
| 51 |
my $talkmsg = $msgs[int (rand (@msgs))]; |
| 52 |
my $repl = $msg->make_reply; |
| 53 |
$repl->add_body ("You said '".$msg->any_body."' but... " . $talkmsg); |
| 54 |
warn "Got message: '".$msg->any_body."' from ".$msg->from."\n"; |
| 55 |
warn "Answered: $talkmsg\n"; |
| 56 |
$repl->send; |
| 57 |
}, |
| 58 |
contact_request_subscribe => sub { |
| 59 |
my ($cl, $acc, $roster, $contact) = @_; |
| 60 |
$contact->send_subscribed; |
| 61 |
warn "Subscribed to ".$contact->jid."\n"; |
| 62 |
}, |
| 63 |
error => sub { |
| 64 |
my ($cl, $acc, $error) = @_; |
| 65 |
warn "Error encountered: ".$error->string."\n"; |
| 66 |
$j->broadcast; |
| 67 |
}, |
| 68 |
disconnect => sub { |
| 69 |
warn "Got disconnected: [@_]\n"; |
| 70 |
$j->broadcast; |
| 71 |
}, |
| 72 |
); |
| 73 |
|
| 74 |
$cl->start; |
| 75 |
|
| 76 |
$j->wait; |