-
Notifications
You must be signed in to change notification settings - Fork 0
/
send-xmpp
executable file
·184 lines (136 loc) · 4.19 KB
/
send-xmpp
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
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
#!/usr/bin/perl
use 5.010;
use strict;
use warnings;
use autodie;
use open ':locale';
use Getopt::Long;
use Pod::Usage;
use AnyEvent::XMPP; # net-im/p5-AnyEvent-XMPP
use AnyEvent::XMPP::IM::Connection;
###############################################################################
my ($cfg_file, $username, $password, $resource, $connect_timeout, $wait);
GetOptions(
'file|f=s' => \$cfg_file,
'username|u=s' => \$username,
'password|p=s' => \$password,
'resource|r=s' => \$resource,
'timeout|t=i' => \$connect_timeout,
'wait|w=f' => \$wait,
) or pod2usage(2);
# default values for options
$resource //= 'AnyEvent::XMPP ' . AnyEvent::XMPP->VERSION();
$connect_timeout //= 5;
$cfg_file //= $ENV{'HOME'}.'/.sendxmpprc';
$wait //= 3;
# parse config file if exists
if (-r $cfg_file) {
my $mode = (stat($cfg_file))[2];
die "$cfg_file must not be readable by everyone\n" if ($mode & 07) != 0;
open(my $cfg_fh, '<', $cfg_file);
while (<$cfg_fh>) {
chomp;
s/#.*$//; # strip comments
next if /^\s*$/;
if (/([-\.\w]+@[-\.\w]+)\s+(\S+)\s*$/) {
$username //= $1;
$password //= $2;
} else {
die "bad config line: '$_'\n";
}
}
close $cfg_fh;
}
die "supply username/passowrd in command line or in $cfg_file\n" unless $username && $password;
###############################################################################
pod2usage(2) unless $ARGV[0];
my @to = split(/,/, $ARGV[0]);
my $message_text;
{
local $/ = undef;
$message_text = <STDIN>;
}
die "non empty message is required\n" unless $message_text;
my $con = AnyEvent::XMPP::IM::Connection->new(
jid => $username,
password => $password,
resource => $resource,
dont_retrieve_roster => 1,
connect_timeout => $connect_timeout,
);
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";
},
message => sub {
my (undef, $msg) = @_;
warn $msg->type . ' message from: ' . $msg->from . ":\n" . $msg->any_body . "\n"
if defined $msg->any_body;
},
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 for incoming messages (errors, subscribe requests)
my $timer = AnyEvent->timer(
after => $wait,
cb => sub { $con->disconnect() }
);
$disconnected_cv->wait;
__END__
=head1 NAME
send-xmpp - send xmpp messages from the commandline.
=head1 SYNOPSIS
send-xmpp [options] <recipient_jid1>,..,<recipient_jidN>
=head1 DESCRIPTION
send-xmpp is a program to send XMPP (Jabber) messages from the
commandline. Message body readed from <STDIN>
=head1 OPTIONS
B<-f>,B<--file> <file>
use <file> configuration file instead of ~/.sendxmpprc
B<-u>,B<--username> <jid>
connect to server using <jid> instead of the one in the configuration file
B<-p>,B<--password> <password>
use <password> instead of the one in the configuration file
B<-r>,B<--resource> <res>
use resource <res> for the sender
B<-t>,B<--timeout> <seconds>
this sets the connection timeout
B<-w>,B<--wait> <seconds>
wait for incoming events, e. g. subscribe requests.
=head1 CONFIG FILE
You may define a '~/.sendxmpprc' file with the necessary data for
xmpp-account, with a line of the format:
<user>@<host> <password>
e.g.:
[email protected] 1df266098fc41fa4b8ce23e24e5f8230
NOTE: for your security, send-xmpp demands that the configuration file
is not readable by everyone