Permalink
Browse files

add send-xmpp

  • Loading branch information...
0 parents commit 5d2416249b9e5355aa76db3cddf59ee78102d3ba @citrin committed Nov 2, 2011
Showing with 77 additions and 0 deletions.
  1. +77 −0 send-xmpp
77 send-xmpp
@@ -0,0 +1,77 @@
+#!/usr/bin/perl
+
+use strict;
+use warnings;
+
+use AnyEvent::XMPP::IM::Connection; # ports/net-im/p5-AnyEvent-XMPP
+
+die "usage: $0 destination_jid_1,destination_jid_2,..,destination_jid_N\n" unless $ARGV[0];
+my @to = split(/,/, $ARGV[0]);
+
+my $message_text;
+
+{
+ local $/;
+ $message_text = <STDIN>;
+}
+
+my $con = AnyEvent::XMPP::IM::Connection->new(
+ jid => 'xmpp-bot@example.ru',
+ password => 'some_secret',
+ resource => $0,
+ dont_retrieve_roster => 1,
+);
+
+my $message_sent_cv = AnyEvent->condvar;
+my $disconnected_cv = AnyEvent->condvar;
+
+$con->reg_cb(
+
+ session_ready => sub {
+ my ($con) = @_;
+ foreach (@to) {
+ $con->send_message($_, 'chat', undef, body => $message_text);
+ }
+ # wait for empty send buffer
+ $con->reg_cb(send_buffer_empty => sub { $message_sent_cv->send });
+ },
+
+ contact_request_subscribe => sub {
+ my ($con, $roster, $contact, $msg) = @_;
+ $contact->send_subscribed;
+ warn "subscription request from ".$contact->jid." accepted\n";
+ },
+
+ error => sub { warn "Error: " . $_[1]->string . "\n" },
+
+ message_error => sub {
+ my @err_info;
+ foreach (qw/text code condition/) {
+ push @err_info, "$_=" . $_[1]->$_ if $_[1]->$_;
+ }
+ my $node = $_[1]->xml_node;
+ push @err_info, "jid=" . $node->attr('from') if $node;
+
+ warn "Message error: " . join(', ', @err_info) . "\n";
+ },
+
+ disconnect => sub {
+ my ($con, $h, $p, $reason) = @_;
+ $h //= '?'; $p //= '?';
+ warn "Disconnected from $h:$p: $reason\n" if $reason;
+ $message_sent_cv->send; # if message don't send yet, it will not be sent
+ $disconnected_cv->send;
+ }
+);
+
+$con->connect;
+
+$message_sent_cv->wait;
+
+# wait 3 seconds for incoming messages (errors, subscribe requests)
+my $timer = AnyEvent->timer(
+ after => 3,
+ cb => sub { $con->disconnect() }
+ );
+
+$disconnected_cv->wait;

0 comments on commit 5d24162

Please sign in to comment.