Debian Codegraph @

samples/disco_info

raw
1 #!/opt/perl/bin/perl
2 use strict;
3 use utf8;
4 use AnyEvent;
5 use AnyEvent::XMPP::Client;
6 use AnyEvent::XMPP::Ext::Disco;
7 use AnyEvent::XMPP::Ext::DataForm;
8 use AnyEvent::XMPP::Ext::Version;
9 use AnyEvent::XMPP::Namespaces qw/xmpp_ns/;
10
11 binmode STDOUT, ":utf8";
12
13 my ($jid, $pw, $discodest, $disconode) = @ARGV;
14
15 unless (@ARGV >= 3) {
16 warn "usage: disco_info <jid> <password> <disco-request-destination-jid> [<disco-node>]\n";
17 exit;
18 }
19
20 my $j = AnyEvent->condvar;
21 my $cl = AnyEvent::XMPP::Client->new (debug => 1);
22 my $disco = AnyEvent::XMPP::Ext::Disco->new;
23 my $version = AnyEvent::XMPP::Ext::Version->new;
24 $cl->add_extension ($disco);
25 $cl->add_extension ($version);
26
27 $cl->set_presence ('away', 'I\'m a bot now.', -1);
28
29 $cl->add_account ($jid, $pw);
30 warn "connecting to $jid...\n";
31
32 my ($gitems, $ginfo, $gvers);
33
34 $cl->reg_cb (
35 session_ready => sub {
36 my ($cl, $acc) = @_;
37 my $con = $acc->connection;
38 my $cnt = 0;
39 warn "session ready for $jid!\n";
40
41 $disco->request_items ($con, $discodest, $disconode, sub {
42 my ($disco, $items, $error) = @_;
43 if ($error) {
44 warn "DISCO ITEM ERROR: " . $error->string . "\n";
45 } else {
46 $gitems = $items;
47 }
48 $cnt++; $j->broadcast if $cnt > 1;
49 });
50
51 $disco->request_info ($con, $discodest, $disconode, sub {
52 my ($disco, $info, $error) = @_;
53 if ($error) {
54 warn "DISCO INFO ERROR: " . $error->string . "\n";
55 $cnt++; $j->broadcast if $cnt > 1;
56 } else {
57 $ginfo = $info;
58
59 if ($info->features->{xmpp_ns ('version')}) {
60 $version->request_version ($con, $discodest, sub {
61 my ($vers, $err) = @_;
62 $gvers = $vers;
63 if ($err) {
64 warn "VERSION ERROR: " . $err->string . "\n";
65 }
66 $cnt++; $j->broadcast if $cnt > 1;
67 });
68 } else {
69 $cnt++; $j->broadcast if $cnt > 1;
70 }
71 }
72 });
73 },
74 error => sub {
75 my ($cl, $acc, $error) = @_;
76 warn "ERROR: ".$error->string."\n";
77 },
78 disconnect => sub {
79 warn "DISCON[@_]\n";
80 $j->broadcast;
81 },
82 );
83
84 $cl->start;
85
86 $j->wait;
87
88 if ($gvers) {
89 printf "version: %s\t%s\t%s\n", $gvers->{name}, $gvers->{version}, $gvers->{os}
90 }
91 if ($ginfo) {
92 print "info: " . $ginfo->jid . "\n";
93
94 for (sort { $a->{category}.$a->{type} cmp $b->{category}.$b->{type} }
95 $ginfo->identities)
96 {
97 print "id: $_->{category}/$_->{type}: $_->{name}\n";
98 }
99
100 for (sort keys %{$ginfo->features}) {
101 print "feature: $_\n";
102 }
103
104 if (my ($f) = $ginfo->xml_node->find_all ([qw/data_form x/])) {
105 my $form = AnyEvent::XMPP::Ext::DataForm->new;
106 $form->from_node ($f);
107 print "form: " . $form->as_debug_string . "\n";
108 }
109
110 }
111 if ($gitems) {
112 print "items: " . $gitems->jid . "\n";
113
114 for ($gitems->items) {
115 print "item: $_->{jid}\n";
116 if (defined $_->{node}) { print "\tnode: $_->{node}\n" }
117 if (defined $_->{name}) { print "\tname: $_->{name}\n" }
118 }
119 }