ViewVC Help
View File | Revision Log | Show Annotations | Download File
/cvs/Net-XMPP2/samples/room_lister
Revision: 1.2
Committed: Sun Jul 8 20:56:01 2007 UTC (19 years, 4 months ago) by elmex
Branch: MAIN
Changes since 1.1: +106 -121 lines
Log Message:
added some more examples  ... err.. utilities

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 elmex 1.2 use Net::XMPP2::Ext::DataForm;
9 elmex 1.1 use Storable;
10     use XML::DOM::XPath;
11 elmex 1.2 use EVQ;
12 elmex 1.1
13 elmex 1.2 our $datafile = "room_data.stor";
14     our $data = {};
15 elmex 1.1
16 elmex 1.2 eval { $data = retrieve $datafile };
17     sub sync_data { store $data, $datafile }
18 elmex 1.1
19     # MAIN START
20 elmex 1.2 my $conferences = retrieve 'conferences.stor';
21 elmex 1.1 my $cl = Net::XMPP2::Client->new ();
22     my $d = Net::XMPP2::Ext::Disco->new;
23     $cl->add_extension ($d);
24     $cl->add_account ('net_xmpp2@jabber.org/test', 'test');
25    
26     sub disco_info {
27     my ($con, $jid, $cb) = @_;
28    
29 elmex 1.2 EVQ::push_request ("di_$jid", sub {
30     my $ID = shift;
31     $d->request_info ($con, $jid, undef, sub {
32     my ($d, $i, $e) = @_;
33     if ($e) {
34     print "error on disco info on $jid: " . $e->string . "\n";
35     } else {
36     $cb->($i);
37     }
38     EVQ::finreq ($ID)
39     });
40 elmex 1.1 });
41     }
42    
43     sub disco_items {
44     my ($con, $jid, $cb) = @_;
45    
46 elmex 1.2 EVQ::push_request ("dit_$jid", sub {
47     my $ID = shift;
48     $d->request_items ($con, $jid, undef, sub {
49     my ($d, $i, $e) = @_;
50     if ($e) {
51     print "error on disco items on $jid: " . $e->string . "\n";
52     } else {
53     $cb->($i);
54     }
55     EVQ::finreq ($ID)
56     });
57     });
58     }
59    
60     sub fetch_room_occupants {
61     my ($con, $jid, $cb) = @_;
62    
63     EVQ::push_request ("fro_$jid", sub {
64     my $ID = shift;
65     $d->request_info ($con, $jid, undef, sub {
66     my ($d, $i, $e) = @_;
67     if ($e) {
68     print "error on disco info to $jid for room occupants: " . $e->string . "\n";
69     } else {
70     my (@q) = $i->xml_node ()->find_all ([qw/data_form x/]);
71     if (@q) {
72     my $df = Net::XMPP2::Ext::DataForm->new;
73     $df->from_node (@q);
74     if (my $f = $df->get_field ('muc#roominfo_occupants')) {
75     $cb->($jid, $f->{values}->[0]);
76     EVQ::finreq ($ID);
77     return;
78     }
79     }
80     $cb->($jid);
81     }
82     EVQ::finreq ($ID);
83     });
84 elmex 1.1 });
85     }
86    
87     sub disco_conference {
88     my ($con, $jid, $cb) = @_;
89    
90 elmex 1.2 EVQ::push_request ("dc_$jid", sub {
91     my $ID = shift;
92     disco_items ($con, $jid, sub {
93     my ($items) = @_;
94 elmex 1.1 for my $i ($items->items) {
95 elmex 1.2 next if $data->{$jid}->{$i->{jid}};
96    
97     fetch_room_occupants ($con, $i->{jid}, sub {
98     my ($room_jid, $cnt) = @_;
99     unless (defined $cnt) {
100     if ($i->{name} =~ /\((\d+)\)\s*$/) {
101     $cnt = $1;
102     }
103 elmex 1.1 }
104 elmex 1.2 $cb->($jid, $i->{jid}, $i->{name}, $cnt);
105     });
106 elmex 1.1 }
107 elmex 1.2 EVQ::finreq ($ID);
108     });
109 elmex 1.1 });
110     }
111    
112 elmex 1.2 my $con;
113     my $A = AnyEvent->condvar;
114 elmex 1.1
115     $cl->reg_cb (
116     error => sub {
117     my ($cl, $acc, $err) = @_;
118     print "ERROR: " . $err->string . "\n";
119     1
120     },
121     iq_result_cb_exception => sub {
122     my ($cl, $acc, $ex) = @_;
123     print "EXCEPTION: $ex\n";
124     1
125     },
126     session_ready => sub {
127     my ($cl, $acc) = @_;
128     print "session ready, requesting items for $ARGV[0]\n";
129     my $c = $acc->connection ();
130     $c->set_default_iq_timeout (30);
131 elmex 1.2 $con = $c;
132     $A->broadcast;
133 elmex 1.1 0
134     },
135     message => sub {
136     my ($cl, $acc, $msg) = @_;
137     print "message from: " . $msg->from . ": " . $msg->any_body . "\n";
138     1
139     }
140     );
141    
142     $cl->start;
143    
144 elmex 1.2 $A->wait;
145    
146     print "EVQ start\n";
147     EVQ::start ();
148    
149     for my $SERVER (keys %{$conferences}) {
150     my $conf = $conferences->{$SERVER};
151     disco_items ($con, $SERVER, sub {
152     my ($i) = @_;
153     for ($i->items) {
154     disco_info ($con, $_->{jid}, sub {
155     my ($i) = @_;
156     if (grep { $_->{category} eq 'conference'
157     && $_->{type} eq 'text' } $i->identities ())
158     {
159     disco_conference ($con, $i->jid, sub {
160     my ($cjid, $rjid, $rname, $rocc) = @_;
161     $data->{$cjid}->{$rjid} = [$cjid, $rjid, $rname, $rocc];
162     sync_data ();
163     printf "\t*** %-30s: %-50s: %3d\n",
164     $cjid, $rjid, $rocc;
165     });
166     }
167     });
168     }
169     });
170     }
171    
172    
173     EVQ::wait ();