ViewVC Help
View File | Revision Log | Show Annotations | Download File
/cvs/cvsroot/Net-XMPP2/samples/conference_lister
Revision: 1.3
Committed: Thu Jul 19 11:36:34 2007 UTC (17 years ago) by elmex
Branch: MAIN
Changes since 1.2: +9 -18 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     use Net::XMPP2::Ext::DataForm;
9     use Storable;
10     use XML::DOM::XPath;
11    
12     our $J = AnyEvent->condvar;
13     our $datafile = "conferences.stor";
14     our $data = {};
15    
16 elmex 1.2 # locking mechanism for requests
17     our %req;
18     our $id = 0;
19     sub addreq { my $k = $id . "_" . $_[0]; $req{$k} = 1; $id++; $k }
20     sub finreq { delete $req{$_[0]}; my @k = keys %req; $J->broadcast if @k == 0 }
21    
22     # timer for status output
23     our $t;
24     sub mktimer {
25     $t = AnyEvent->timer (after => 1, cb => sub {
26     my @keys = keys %req;
27     my @ok = grep { $_ !~ /_timer_/ } @keys;
28     my $timers = scalar (grep { $_ =~ /_timer_/ } @keys);
29     print "\t*** pending requests $timers timers, and : " . join (',', @ok) . "\n";
30     mktimer ();
31     });
32     }
33     mktimer;
34    
35     # server data cache
36     eval { $data = retrieve $datafile };
37     print "finished data: " . join (',', keys %$data) . "\n";
38     sub sync_data { store $data, $datafile }
39 elmex 1.1
40     # MAIN START
41 elmex 1.3 my @servers = map { s/^\s*(\S+)\s*$/\1/; $_ } <STDIN>;
42 elmex 1.1 my $cl = Net::XMPP2::Client->new ();
43     my $d = Net::XMPP2::Ext::Disco->new;
44     $cl->add_extension ($d);
45     $cl->add_account ('net_xmpp2@jabber.org/test', 'test');
46    
47     sub disco_info {
48     my ($con, $jid, $cb) = @_;
49    
50     my $ID = addreq ("di_$jid");
51     $d->request_info ($con, $jid, undef, sub {
52     my ($d, $i, $e) = @_;
53     if ($e) {
54     print "error on disco info on $jid: " . $e->string . "\n";
55     } else {
56     $cb->($i);
57     }
58     finreq ($ID)
59     });
60     }
61    
62     sub disco_items {
63     my ($con, $jid, $cb) = @_;
64    
65     my $ID = addreq ("dit_$jid");
66     $d->request_items ($con, $jid, undef, sub {
67     my ($d, $i, $e) = @_;
68     if ($e) {
69     print "error on disco items on $jid: " . $e->string . "\n";
70     } else {
71     $cb->($i);
72     }
73     finreq ($ID)
74     });
75     }
76    
77     my %req_timers;
78    
79     $cl->reg_cb (
80     error => sub {
81     my ($cl, $acc, $err) = @_;
82     print "ERROR: " . $err->string . "\n";
83     1
84     },
85     iq_result_cb_exception => sub {
86     my ($cl, $acc, $ex) = @_;
87     print "EXCEPTION: $ex\n";
88     1
89     },
90     session_ready => sub {
91     my ($cl, $acc) = @_;
92     print "session ready, requesting items for $ARGV[0]\n";
93     my $c = $acc->connection ();
94     $c->set_default_iq_timeout (30);
95    
96 elmex 1.3 my $timer_step = 0.1;
97 elmex 1.1 my $timer_cnt = 0;
98    
99     for my $SERVER (@servers) {
100     next if $data->{$SERVER};
101     my $t = $timer_cnt;
102    
103     my $ID = addreq ("timer_$t");
104     $req_timers{$t} = AnyEvent->timer (after => $t,
105     cb => sub {
106     disco_items ($c, $SERVER, sub {
107     my ($i) = @_;
108 elmex 1.3 print "got items for $SERVER\n";
109     for my $it ($i->items) {
110     disco_info ($c, $it->{jid}, sub {
111 elmex 1.1 my ($i) = @_;
112 elmex 1.3 my @f = grep { $_ =~ /^muc/ } keys %{$i->features || {}};
113     my @c = grep { $_->{category} eq 'conference' && $_->{type} eq 'text' } $i->identities ();
114     if (@c && !@f) {
115     $data->{$SERVER}->{$i->jid} = 1;
116 elmex 1.1 print "\t*** found conference " . $i->jid . "\n";
117     sync_data ();
118     }
119     });
120     }
121     });
122     delete $req_timers{$t};
123     finreq ($ID);
124     }
125     );
126    
127     $timer_cnt += $timer_step;
128     }
129     0
130     },
131     message => sub {
132     my ($cl, $acc, $msg) = @_;
133     print "message from: " . $msg->from . ": " . $msg->any_body . "\n";
134     1
135     }
136     );
137    
138     $cl->start;
139    
140     $J->wait