ViewVC Help
View File | Revision Log | Show Annotations | Download File
/cvs/cvsroot/Net-XMPP2/lib/Net/XMPP2/Ext/RegisterForm.pm
Revision: 1.7
Committed: Wed Jul 25 21:29:12 2007 UTC (19 years, 2 months ago) by elmex
Branch: MAIN
Changes since 1.6: +2 -1 lines
Log Message:
fixed small thing in register form

File Contents

# Content
1 package Net::XMPP2::Ext::RegisterForm;
2 use strict;
3 use Net::XMPP2::Util;
4 use Net::XMPP2::Namespaces qw/xmpp_ns/;
5 use Net::XMPP2::Ext::DataForm;
6 use Net::XMPP2::Ext::OOB;
7
8 =head1 NAME
9
10 Net::XMPP2::Ext::RegisterForm - Handle for in band registration
11
12 =head1 SYNOPSIS
13
14 my $con = Net::XMPP2::Connection->new (...);
15 ...
16 $con->do_in_band_register (sub {
17 my ($form, $error) = @_;
18 if ($error) { print "ERROR: ".$error->string."\n" }
19 else {
20 if ($form->type eq 'simple') {
21 if ($form->has_field ('username') && $form->has_field ('password')) {
22 $form->set_field (
23 username => 'test',
24 password => 'qwerty',
25 );
26 $form->submit (sub {
27 my ($form, $error) = @_;
28 if ($error) { print "SUBMIT ERROR: ".$error->string."\n" }
29 else {
30 print "Successfully registered as ".$form->field ('username')."\n"
31 }
32 });
33 } else {
34 print "Couldn't fill out the form: " . $form->field ('instructions') ."\n";
35 }
36 } elsif ($form->type eq 'data_form' {
37 my $dform = $form->data_form;
38 ... fill out the form $dform (of type Net::XMPP2::DataForm) ...
39 $form->submit_data_form ($dform, sub {
40 my ($form, $error) = @_;
41 if ($error) { print "DATA FORM SUBMIT ERROR: ".$error->string."\n" }
42 else {
43 print "Successfully registered as ".$form->field ('username')."\n"
44 }
45 })
46 }
47 }
48 });
49
50 =head1 DESCRIPTION
51
52 This module represents an in band registration form
53 which can be filled out and submitted.
54
55 You can get an instance of this class only by requesting it
56 from a L<Net::XMPP2::Connection> by calling the C<request_inband_register_form>
57 method.
58
59 =cut
60
61 sub new {
62 my $this = shift;
63 my $class = ref($this) || $this;
64 my $self = bless { @_ }, $class;
65 $self
66 }
67
68 sub _get_legacy_form {
69 my ($self, $node) = @_;
70
71 my $form = {};
72
73 for ($node->nodes) {
74 if ($_->eq_ns ('register')) {
75 $form->{$_->name} = $_->text;
76 }
77 }
78
79 $form
80 }
81
82 sub try_fillout_registration {
83 my ($self, $username, $password) = @_;
84
85 my $form;
86 my $nform;
87
88 if (my $df = $self->get_data_form) {
89 my $af = Net::XMPP2::Ext::DataForm->new;
90 $af->make_answer_form ($df);
91 $af->set_field_value (username => $username);
92 $af->set_field_value (password => $password);
93 $nform = $af;
94
95 } else {
96 my $frm = $self->get_standard_form_fields;
97 $form = {
98 username => $username,
99 password => $password
100 };
101 }
102
103 return
104 Net::XMPP2::Ext::RegisterForm->new (
105 data_form => $nform,
106 legacy_form => $form,
107 answered => 1
108 );
109 }
110
111 sub is_answer_form {
112 my ($self) = @_;
113 $self->{answered}
114 }
115
116 sub is_already_registered {
117 my ($self) = @_;
118 exists $self->{legacy_form}
119 && exists $self->{legacy_form}->{registered}
120 }
121
122 sub init_new_form {
123 my ($self, $node) = @_;
124
125 my (@x) = $node->find_all ([qw/register query/], [qw/data_form x/]);
126
127 if (@x) {
128 my $df = Net::XMPP2::Ext::DataForm->new;
129 $df->from_node (@x);
130 $self->{data_form} = $df;
131
132 } else {
133 die "TODO!";
134 }
135 }
136
137
138 =item B<get_legacy_form_fields>
139
140 This method returns a hash with....
141 as specified in the in band registration XEP.
142
143 =cut
144
145 sub get_legacy_form_fields {
146 my ($self) = @_;
147 $self->{legacy_form}
148 }
149
150 =item B<get_data_form>
151
152 This method returns the L<Net::XMPP2::Ext::DataForm> that came
153 with the registration response. If no data form was provided by the
154 server this method returns undef.
155
156 =cut
157
158 sub get_data_form {
159 my ($self) = @_;
160 $self->{data_form}
161 }
162
163
164 =item B<get_oob>
165
166 This method returns a hash like the one returned from
167 the function C<url_from_node> in L<Net::XMPP2::Ext::OOB>.
168 It contains the out of band data for this registration form.
169
170 =cut
171
172 sub get_oob {
173 my ($self) = @_;
174 $self->{oob}
175 }
176
177 sub init_from_node {
178 my ($self, $node) = @_;
179
180 if ($node->find_all ([qw/register query/], [qw/data_form x/])) {
181 $self->init_new_form ($node);
182 }
183 if (my ($xoob) = $node->find_all ([qw/register query/], [qw/oob x/])) {
184 $self->{oob} = Net::XMPP2::Ext::OOB->url_from_node ($xoob);
185 }
186
187 my $form = $self->_get_legacy_form ($node);
188 $self->{legacy_form} = $form;
189 }
190
191 =head1 AUTHOR
192
193 Robin Redeker, C<< <elmex at ta-sa.org> >>, JID: C<< <elmex at jabber.org> >>
194
195 =head1 COPYRIGHT & LICENSE
196
197 Copyright 2007 Robin Redeker, all rights reserved.
198
199 This program is free software; you can redistribute it and/or modify it
200 under the same terms as Perl itself.
201
202 =cut
203
204 1; # End of Net::XMPP2::RegisterForm