Debian Codegraph @

samples/find_servers

raw
1 #!/opt/perl/bin/perl
2 use strict;
3 use utf8;
4 use AnyEvent;
5 use AnyEvent::XMPP::Client;
6 use AnyEvent::XMPP::Ext::Disco;
7 use AnyEvent::XMPP::Ext::DataForm;
8 use Storable;
9 use XML::DOM::XPath;
10 use IO::Handle;
11 use EVQ;
12
13 my @servers = map { s/^\s*(\S+)\s*$/\1/; $_ } <STDIN>;
14
15 my $cl = AnyEvent::XMPP::Client->new ();
16 my $d = AnyEvent::XMPP::Ext::Disco->new;
17 $cl->add_extension ($d);
18 $cl->add_account ('[email protected]/test', 'test');
19
20 sub disco_info {
21 my ($con, $jid, $cb) = @_;
22
23 EVQ::push_request ("di_$jid", sub {
24 my $ID = shift;
25 warn ">>$jid?\n";
26 $d->request_info ($con, $jid, undef, sub {
27 my ($d, $i, $e) = @_;
28 if ($e) {
29 warn "error on disco info on $jid: " . $e->string . "\n";
30 } else {
31 $cb->($i);
32 }
33 EVQ::finreq ($ID)
34 });
35 });
36 }
37
38 sub disco_items {
39 my ($con, $jid, $cb) = @_;
40
41 EVQ::push_request ("dit_$jid", sub {
42 my $ID = shift;
43 $d->request_items ($con, $jid, undef, sub {
44 my ($d, $i, $e) = @_;
45 if ($e) {
46 warn "error on disco items on $jid: " . $e->string . "\n";
47 } else {
48 $cb->($i);
49 }
50 EVQ::finreq ($ID)
51 });
52 });
53 }
54
55 sub fetch_room_occupants {
56 my ($con, $jid, $cb) = @_;
57
58 EVQ::push_request ("fro_$jid", sub {
59 my $ID = shift;
60 $d->request_info ($con, $jid, undef, sub {
61 my ($d, $i, $e) = @_;
62 if ($e) {
63 warn "error on disco info to $jid for room occupants: " . $e->string . "\n";
64 } else {
65 my (@q) = $i->xml_node ()->find_all ([qw/data_form x/]);
66 if (@q) {
67 my $df = AnyEvent::XMPP::Ext::DataForm->new;
68 $df->from_node (@q);
69 if (my $f = $df->get_field ('muc#roominfo_occupants')) {
70 $cb->($jid, $f->{values}->[0]);
71 EVQ::finreq ($ID);
72 return;
73 }
74 }
75 $cb->($jid);
76 }
77 EVQ::finreq ($ID);
78 });
79 });
80 }
81
82 my $con;
83 my $A = AnyEvent->condvar;
84
85 $cl->reg_cb (
86 error => sub {
87 my ($cl, $acc, $err) = @_;
88 warn "ERROR: " . $err->string . "\n";
89 1
90 },
91 iq_result_cb_exception => sub {
92 my ($cl, $acc, $ex) = @_;
93 warn "EXCEPTION: $ex\n";
94 1
95 },
96 session_ready => sub {
97 my ($cl, $acc) = @_;
98 warn "session ready, requesting items for $ARGV[0]\n";
99 my $c = $acc->connection ();
100 $c->set_default_iq_timeout (30);
101 $con = $c;
102 $A->broadcast;
103 0
104 },
105 message => sub {
106 my ($cl, $acc, $msg) = @_;
107 warn "message from: " . $msg->from . ": " . $msg->any_body . "\n";
108 1
109 }
110 );
111
112 $cl->start;
113
114 $A->wait;
115
116 EVQ::start ();
117
118 open SERVEROUT, ">servers.list.txt"
119 or die "servers.list.txt: $!";
120
121 for my $SERVER (@servers) {
122 warn "$SERVER?\n";
123 disco_info ($con, $SERVER, sub {
124 my ($i) = @_;
125 my @c = grep { $_->{category} eq 'server' } $i->identities ();
126 if (@c) {
127 print SERVEROUT $i->jid . "\n";
128 SERVEROUT->flush;
129 }
130 });
131 }
132
133
134 EVQ::wait ();