File: disco_info

package info (click to toggle)
libanyevent-xmpp-perl 0.55-1
  • links: PTS, VCS
  • area: main
  • in suites: jessie, jessie-kfreebsd
  • size: 784 kB
  • ctags: 553
  • sloc: perl: 8,004; makefile: 13
file content (119 lines) | stat: -rwxr-xr-x 3,147 bytes parent folder | download | duplicates (6)
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
#!/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 AnyEvent::XMPP::Ext::Version;
use AnyEvent::XMPP::Namespaces qw/xmpp_ns/;

binmode STDOUT, ":utf8";

my ($jid, $pw, $discodest, $disconode) = @ARGV;

unless (@ARGV >= 3) {
   warn "usage: disco_info <jid> <password> <disco-request-destination-jid> [<disco-node>]\n";
   exit;
}

my $j     = AnyEvent->condvar;
my $cl    = AnyEvent::XMPP::Client->new (debug => 1);
my $disco = AnyEvent::XMPP::Ext::Disco->new;
my $version = AnyEvent::XMPP::Ext::Version->new;
$cl->add_extension ($disco);
$cl->add_extension ($version);

$cl->set_presence ('away', 'I\'m a bot now.', -1);

$cl->add_account ($jid, $pw);
warn "connecting to $jid...\n";

my ($gitems, $ginfo, $gvers);

$cl->reg_cb (
   session_ready => sub {
      my ($cl, $acc) = @_;
      my $con = $acc->connection;
      my $cnt = 0;
      warn "session ready for $jid!\n";

      $disco->request_items ($con, $discodest, $disconode, sub {
         my ($disco, $items, $error) = @_;
         if ($error) {
            warn "DISCO ITEM ERROR: " . $error->string . "\n";
         } else {
            $gitems = $items;
         }
         $cnt++; $j->broadcast if $cnt > 1;
      });

      $disco->request_info ($con, $discodest, $disconode, sub {
         my ($disco, $info, $error) = @_;
         if ($error) {
            warn "DISCO INFO ERROR: " . $error->string . "\n";
            $cnt++; $j->broadcast if $cnt > 1;
         } else {
            $ginfo = $info;

            if ($info->features->{xmpp_ns ('version')}) {
               $version->request_version ($con, $discodest, sub {
                  my ($vers, $err) = @_;
                  $gvers = $vers;
                  if ($err) {
                     warn "VERSION ERROR: " . $err->string . "\n";
                  }
                  $cnt++; $j->broadcast if $cnt > 1;
               });
            } else {
               $cnt++; $j->broadcast if $cnt > 1;
            }
         }
      });
   },
   error => sub {
      my ($cl, $acc, $error) = @_;
      warn "ERROR: ".$error->string."\n";
   },
   disconnect => sub {
      warn "DISCON[@_]\n";
      $j->broadcast;
   },
);

$cl->start;

$j->wait;

if ($gvers) {
   printf "version: %s\t%s\t%s\n", $gvers->{name}, $gvers->{version}, $gvers->{os}
}
if ($ginfo) {
   print "info: " . $ginfo->jid . "\n";

   for (sort { $a->{category}.$a->{type} cmp $b->{category}.$b->{type} }
           $ginfo->identities)
   {
      print "id: $_->{category}/$_->{type}: $_->{name}\n";
   }

   for (sort keys %{$ginfo->features}) {
      print "feature: $_\n";
   }

   if (my ($f) = $ginfo->xml_node->find_all ([qw/data_form x/])) {
      my $form = AnyEvent::XMPP::Ext::DataForm->new;
      $form->from_node ($f);
      print "form: " . $form->as_debug_string . "\n";
   }

}
if ($gitems) {
   print "items: " . $gitems->jid . "\n";

   for ($gitems->items) {
      print "item: $_->{jid}\n";
      if (defined $_->{node}) { print "\tnode: $_->{node}\n" }
      if (defined $_->{name}) { print "\tname: $_->{name}\n" }
   }
}