1 |
#!/opt/perl/bin/perl |
2 |
use strict; |
3 |
use utf8; |
4 |
use Event; |
5 |
use AnyEvent; |
6 |
use Net::XMPP2::Client; |
7 |
use Net::XMPP2::Ext::Disco; |
8 |
use Net::XMPP2::Ext::DataForm; |
9 |
use Storable; |
10 |
use XML::DOM::XPath; |
11 |
|
12 |
our $J = AnyEvent->condvar; |
13 |
our $datafile = "conferences.stor"; |
14 |
our $data = {}; |
15 |
|
16 |
require "rl.pl"; |
17 |
|
18 |
# MAIN START |
19 |
my @servers = load_servers (); |
20 |
my $cl = Net::XMPP2::Client->new (); |
21 |
my $d = Net::XMPP2::Ext::Disco->new; |
22 |
$cl->add_extension ($d); |
23 |
$cl->add_account ('net_xmpp2@jabber.org/test', 'test'); |
24 |
|
25 |
sub disco_info { |
26 |
my ($con, $jid, $cb) = @_; |
27 |
|
28 |
my $ID = addreq ("di_$jid"); |
29 |
$d->request_info ($con, $jid, undef, sub { |
30 |
my ($d, $i, $e) = @_; |
31 |
if ($e) { |
32 |
print "error on disco info on $jid: " . $e->string . "\n"; |
33 |
} else { |
34 |
$cb->($i); |
35 |
} |
36 |
finreq ($ID) |
37 |
}); |
38 |
} |
39 |
|
40 |
sub disco_items { |
41 |
my ($con, $jid, $cb) = @_; |
42 |
|
43 |
my $ID = addreq ("dit_$jid"); |
44 |
$d->request_items ($con, $jid, undef, sub { |
45 |
my ($d, $i, $e) = @_; |
46 |
if ($e) { |
47 |
print "error on disco items on $jid: " . $e->string . "\n"; |
48 |
} else { |
49 |
$cb->($i); |
50 |
} |
51 |
finreq ($ID) |
52 |
}); |
53 |
} |
54 |
|
55 |
my %req_timers; |
56 |
|
57 |
$cl->reg_cb ( |
58 |
error => sub { |
59 |
my ($cl, $acc, $err) = @_; |
60 |
print "ERROR: " . $err->string . "\n"; |
61 |
1 |
62 |
}, |
63 |
iq_result_cb_exception => sub { |
64 |
my ($cl, $acc, $ex) = @_; |
65 |
print "EXCEPTION: $ex\n"; |
66 |
1 |
67 |
}, |
68 |
session_ready => sub { |
69 |
my ($cl, $acc) = @_; |
70 |
print "session ready, requesting items for $ARGV[0]\n"; |
71 |
my $c = $acc->connection (); |
72 |
$c->set_default_iq_timeout (30); |
73 |
|
74 |
my $timer_step = 3; |
75 |
my $timer_cnt = 0; |
76 |
|
77 |
for my $SERVER (@servers) { |
78 |
next if $data->{$SERVER}; |
79 |
my $t = $timer_cnt; |
80 |
|
81 |
my $ID = addreq ("timer_$t"); |
82 |
$req_timers{$t} = AnyEvent->timer (after => $t, |
83 |
cb => sub { |
84 |
disco_items ($c, $SERVER, sub { |
85 |
my ($i) = @_; |
86 |
for ($i->items) { |
87 |
disco_info ($c, $_->{jid}, sub { |
88 |
my ($i) = @_; |
89 |
if (grep { $_->{category} eq 'conference' && $_->{type} eq 'text' } $i->identities ()) { |
90 |
$data->{$SERVER} = $i->jid; |
91 |
print "\t*** found conference " . $i->jid . "\n"; |
92 |
sync_data (); |
93 |
} |
94 |
}); |
95 |
} |
96 |
}); |
97 |
delete $req_timers{$t}; |
98 |
finreq ($ID); |
99 |
} |
100 |
); |
101 |
|
102 |
$timer_cnt += $timer_step; |
103 |
} |
104 |
0 |
105 |
}, |
106 |
message => sub { |
107 |
my ($cl, $acc, $msg) = @_; |
108 |
print "message from: " . $msg->from . ": " . $msg->any_body . "\n"; |
109 |
1 |
110 |
} |
111 |
); |
112 |
|
113 |
$cl->start; |
114 |
|
115 |
$J->wait |