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