Debian Codegraph @

samples/simple_register_example

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::Registration;
7 use AnyEvent::XMPP::Util qw/split_jid/;
8
9 sub result {
10 my ($reg, $ok, $error, $form, $comment) = @_;
11 $comment ||= 'REGISTERED!';
12 if ($ok) {
13 print "$comment\n";
14 } else {
15 print "ERROR: " . $error->string . " [form: $form]\n";
16 }
17 }
18
19 my ($user, $pw, $act) = @ARGV;
20 my ($username, $server) = split_jid ($user);
21
22 unless ($user && $pw) {
23 print <<USAGE;
24 usage:
25 register account:
26 ./simple_register_example <jid> <password>
27
28 unregister account:
29 ./simple_register_example <jid> <password> unregister
30
31 change password:
32 ./simple_register_example <jid> <password> chpw:<newpassword>
33
34 USAGE
35 die "\n";
36 }
37
38 my $j = AnyEvent->condvar;
39 my $cl = AnyEvent::XMPP::Client->new (debug => 1);
40 $cl->add_account ($user, $pw);
41 $cl->reg_cb (
42 stream_pre_authentication => sub {
43 my ($cl, $acc) = @_;
44
45 my $reg = AnyEvent::XMPP::Ext::Registration->new (connection => $acc->connection);
46
47 if ($act eq '') {
48 $reg->send_registration_request (sub {
49 my ($reg, $form, $error) = @_;
50
51 if ($error) {
52 print "ERROR: " . $error->string . "\n";
53
54 } else {
55 my $df = $form->get_data_form;
56 my $lf = $form->get_legacy_form_fields;
57 my $oo = $form->get_oob;
58
59 if ($df) {
60 print "GOT FORM! ".($df ? $df->as_debug_string : "")."\n";
61 }
62 if ($lf) {
63 require Data::Dumper;
64 print "LEGACY: " . Data::Dumper->Dump ([$lf]) . "\n";
65 }
66 if ($oo) {
67 print "OOB: $oo->{url} / $oo->{desc}\n";
68 }
69
70 my $af = $form->try_fillout_registration ($username, $pw);
71 $reg->submit_form ($af, \&result);
72 }
73 });
74 return 0
75 }
76 1
77 },
78 session_ready => sub {
79 my ($cl, $acc) = @_;
80
81 my $reg = AnyEvent::XMPP::Ext::Registration->new (connection => $acc->connection);
82
83 if ($act eq 'unregister') {
84 $reg->send_unregistration_request (
85 sub { result ($_[0], $_[1], $_[2], $_[3], 'UNREGISTERED!') }
86 );
87
88 } elsif ($act =~ /chpw:(\S+)/) {
89 $reg->send_password_change_request ($username, $1,
90 sub { result ($_[0], $_[1], $_[2], $_[3], 'CHANGED PW!') }
91 );
92 }
93 },
94 disconnect => sub {
95 my ($cl, $acc, $h, $p, $reas) = @_;
96 print "disconnect ($h:$p): $reas\n";
97 },
98 error => sub {
99 my ($cl, $acc, $err) = @_;
100 print "ERROR: " . $err->string . "\n";
101 },
102 message => sub {
103 my ($cl, $acc, $msg) = @_;
104 print "message from: " . $msg->from . ": " . $msg->any_body . "\n";
105 }
106 );
107
108 $cl->start;
109 $j->wait;