ViewVC Help
View File | Revision Log | Show Annotations | Download File
/cvs/Net-XMPP2/samples/find_servers
Revision: 1.1
Committed: Thu Jul 19 11:36:34 2007 UTC (18 years, 8 months ago) by elmex
Branch: MAIN
CVS Tags: HEAD
Log Message:
added initial_presence argument to the IM::Connection and the
Client. added and upgraded some examples. further work on the
registration forms.

File Contents

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