ViewVC Help
View File | Revision Log | Show Annotations | Download File
/cvs/Net-XMPP2/samples/room_lister
Revision: 1.3
Committed: Thu Jul 19 11:36:34 2007 UTC (19 years, 4 months ago) by elmex
Branch: MAIN
Changes since 1.2: +37 -19 lines
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 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     $cl->add_account ('net_xmpp2@jabber.org/test', 'test');
35    
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 next if $data->{$jid}->{$i->{jid}};
106    
107     fetch_room_occupants ($con, $i->{jid}, sub {
108     my ($room_jid, $cnt) = @_;
109     unless (defined $cnt) {
110     if ($i->{name} =~ /\((\d+)\)\s*$/) {
111     $cnt = $1;
112     }
113 elmex 1.1 }
114 elmex 1.2 $cb->($jid, $i->{jid}, $i->{name}, $cnt);
115     });
116 elmex 1.1 }
117 elmex 1.2 EVQ::finreq ($ID);
118     });
119 elmex 1.1 });
120     }
121    
122 elmex 1.2 my $con;
123     my $A = AnyEvent->condvar;
124 elmex 1.1
125     $cl->reg_cb (
126     error => sub {
127     my ($cl, $acc, $err) = @_;
128     print "ERROR: " . $err->string . "\n";
129     1
130     },
131     iq_result_cb_exception => sub {
132     my ($cl, $acc, $ex) = @_;
133     print "EXCEPTION: $ex\n";
134     1
135     },
136     session_ready => sub {
137     my ($cl, $acc) = @_;
138     print "session ready, requesting items for $ARGV[0]\n";
139     my $c = $acc->connection ();
140     $c->set_default_iq_timeout (30);
141 elmex 1.2 $con = $c;
142     $A->broadcast;
143 elmex 1.1 0
144     },
145     message => sub {
146     my ($cl, $acc, $msg) = @_;
147     print "message from: " . $msg->from . ": " . $msg->any_body . "\n";
148     1
149     }
150     );
151    
152     $cl->start;
153    
154 elmex 1.2 $A->wait;
155    
156     print "EVQ start\n";
157     EVQ::start ();
158    
159 elmex 1.3 my $t;
160     sub mkti { $t = AnyEvent->timer (after => 10, cb => sub { sync_data (); mkti (); }) }
161     mkti;
162    
163 elmex 1.2 for my $SERVER (keys %{$conferences}) {
164     my $conf = $conferences->{$SERVER};
165 elmex 1.3 for my $cj (keys %$conf) {
166     disco_conference ($con, $cj, sub {
167     my ($cjid, $rjid, $rname, $rocc) = @_;
168     $data->{$cjid}->{$rjid} = [$cjid, $rjid, $rname, $rocc];
169     printf "\t*** %-30s: %-50s: %3d\n",
170     $cjid, $rjid, $rocc;
171     });
172    
173     # disco_items ($con, $cj, sub {
174     # my ($i) = @_;
175     # for ($i->items) {
176     # disco_info ($con, $_->{jid}, sub {
177     # my ($i) = @_;
178     # print "\t\tdisco info from room ".$i->jid.":".(join ", ", map { "$_->{category}/$_->{type}" } $i->identities)."\n";
179     # if (grep { $_->{category} eq 'conference'
180     # && $_->{type} eq 'text' } $i->identities ())
181     # {
182     # # XXX
183     # }
184     # });
185     # }
186     # });
187     }
188 elmex 1.2 }
189    
190    
191     EVQ::wait ();