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