ViewVC Help
View File | Revision Log | Show Annotations | Download File
/cvs/Net-XMPP2/samples/room_lister
Revision: 1.4
Committed: Thu Jul 26 19:45:20 2007 UTC (19 years, 3 months ago) by elmex
Branch: MAIN
Changes since 1.3: +9 -19 lines
Log Message:
fixed some small bugs in the room lister

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