| 1 |
#!/opt/perl/bin/perl |
| 2 |
use strict; |
| 3 |
use utf8; |
| 4 |
use AnyEvent; |
| 5 |
use AnyEvent::XMPP::IM::Connection; |
| 6 |
|
| 7 |
unless (@ARGV >= 3) { die "sendmsg <account jid> <password> <destination jid>\n" } |
| 8 |
|
| 9 |
my $msg = do { local $/; <STDIN> }; |
| 10 |
my $dest = $ARGV[2]; |
| 11 |
|
| 12 |
my $j = AnyEvent->condvar; |
| 13 |
|
| 14 |
my $con = |
| 15 |
AnyEvent::XMPP::IM::Connection->new ( |
| 16 |
jid => $ARGV[0], |
| 17 |
password => $ARGV[1], |
| 18 |
initial_presence => -10, |
| 19 |
debug => 1 |
| 20 |
); |
| 21 |
|
| 22 |
$con->reg_cb ( |
| 23 |
session_ready => sub { |
| 24 |
my ($con) = @_; |
| 25 |
print "Connected as " . $con->jid . "\n"; |
| 26 |
print "Sending message to $dest:\n$msg\n"; |
| 27 |
my $immsg = AnyEvent::XMPP::IM::Message->new (to => $dest, body => $msg); |
| 28 |
$immsg->send ($con); |
| 29 |
}, |
| 30 |
message => sub { |
| 31 |
my ($con, $msg) = @_; |
| 32 |
print "Message from " . $msg->from . ":\n" . $msg->any_body . "\n---\n"; |
| 33 |
}, |
| 34 |
error => sub { |
| 35 |
my ($con, $error) = @_; |
| 36 |
warn "Error: " . $error->string . "\n"; |
| 37 |
}, |
| 38 |
disconnect => sub { |
| 39 |
my ($con, $h, $p, $reason) = @_; |
| 40 |
warn "Disconnected from $h:$p: $reason\n"; |
| 41 |
$j->broadcast; |
| 42 |
} |
| 43 |
); |
| 44 |
|
| 45 |
print "Trying to connect...\n"; |
| 46 |
$con->connect (); |
| 47 |
|
| 48 |
$j->wait; |