| 1 |
elmex |
1.1 |
package Net::XMPP2::Writer; |
| 2 |
|
|
use strict; |
| 3 |
|
|
use XML::Writer; |
| 4 |
|
|
use Authen::SASL; |
| 5 |
|
|
use MIME::Base64; |
| 6 |
|
|
use Net::XMPP2::Namespaces qw/xmpp_ns/; |
| 7 |
|
|
|
| 8 |
|
|
=head1 NAME |
| 9 |
|
|
|
| 10 |
elmex |
1.5 |
Net::XMPP2::Writer - A "XML" writer for XMPP |
| 11 |
elmex |
1.1 |
|
| 12 |
|
|
=head1 SYNOPSIS |
| 13 |
|
|
|
| 14 |
|
|
use Net::XMPP2::Writer; |
| 15 |
|
|
... |
| 16 |
|
|
|
| 17 |
elmex |
1.3 |
=head1 DESCRIPTION |
| 18 |
|
|
|
| 19 |
elmex |
1.5 |
This module contains some helper functions for writing XMPP "XML", |
| 20 |
elmex |
1.3 |
which is not real XML at all ;-( I use L<XML::Writer> and tune it |
| 21 |
elmex |
1.5 |
until it creates "XML" that is accepted by most servers propably |
| 22 |
elmex |
1.3 |
(all of the XMPP servers i tested yet work (jabberd14, jabberd2, ejabberd). |
| 23 |
|
|
|
| 24 |
|
|
I hope the semantics of L<XML::Writer> don't change much over the future, |
| 25 |
|
|
but if they do and you run into problems, please report them! |
| 26 |
|
|
|
| 27 |
elmex |
1.5 |
The whole "XML" concept of XMPP is fundamentally broken anyway. It's supposed |
| 28 |
elmex |
1.3 |
to be an subset of XML. But a subset of XML productions is not XML. Strictly |
| 29 |
elmex |
1.5 |
speaking you need a special XMPP "XML" parser and writer to be 100% conformant. |
| 30 |
elmex |
1.3 |
|
| 31 |
elmex |
1.9 |
But I try to be as "XML" XMPP conformant as possible (it should be around 99-100%). |
| 32 |
elmex |
1.5 |
But it's hard to say what XML is conformant, as the specifications of XMPP "XML" and XML |
| 33 |
elmex |
1.3 |
are contradicting. For example XMPP also says you only have to generated and accept |
| 34 |
|
|
utf-8 encodings of XML, but the XML recommendation says that each parser has |
| 35 |
|
|
to accept utf-8 E<and> utf-16. So, what do you do? Do you use a XML conformant parser |
| 36 |
|
|
or do you write your own? |
| 37 |
|
|
|
| 38 |
elmex |
1.5 |
I'm using XML::Parser::Expat because expat knows how to parse broken (aka 'partial') |
| 39 |
|
|
"XML" documents, as XMPP requires. Another argument is that if you capture a XMPP |
| 40 |
elmex |
1.3 |
conversation to the end, and even if a '</stream:stream>' tag was captured, you |
| 41 |
|
|
wont have a valid XML document. The problem is that you have to resent a <stream> tag |
| 42 |
|
|
after TLS and SASL authentication each! |
| 43 |
|
|
|
| 44 |
|
|
But well... Net::XMPP2 does it's best with expat to cope with the fundamental brokeness |
| 45 |
elmex |
1.5 |
of "XML" in XMPP. |
| 46 |
elmex |
1.3 |
|
| 47 |
elmex |
1.5 |
Back to the issue with "XML" generation: I've discoverd that many XMPP servers (eg. |
| 48 |
elmex |
1.3 |
jabberd14 and ejabberd) have problems with XML namespaces. Thats the reason why |
| 49 |
elmex |
1.9 |
I'm assigning the namespace prefixes manually: The servers just don't accept validly |
| 50 |
elmex |
1.3 |
namespaced XML. The draft 3921bis does even state that a client SHOULD generate a 'stream' |
| 51 |
|
|
prefix for the <stream> tag. |
| 52 |
|
|
|
| 53 |
elmex |
1.5 |
I advice you to explictly set the namespaces too if you generate "XML" for XMPP yourself, |
| 54 |
elmex |
1.3 |
at least until all or most of the XMPP servers have been fixed. Which might take some |
| 55 |
|
|
years :-) And maybe will happen never. |
| 56 |
|
|
|
| 57 |
elmex |
1.5 |
And another note: As XMPP requires all predefined entity characters to be escaped |
| 58 |
|
|
in character data you need a "XML" writer that will escape everything: |
| 59 |
|
|
|
| 60 |
|
|
RFC 3920 - 11.1. Restrictions: |
| 61 |
|
|
|
| 62 |
|
|
character data or attribute values containing unescaped characters |
| 63 |
|
|
that map to the predefined entities (Section 4.6 therein); |
| 64 |
|
|
such characters MUST be escaped |
| 65 |
|
|
|
| 66 |
|
|
This means: |
| 67 |
|
|
You have to escape '>' in the character data. I don't know whether XML::Writer |
| 68 |
elmex |
1.9 |
does that. And I honestly don't care much about this. XMPP is broken by design and |
| 69 |
|
|
I have barely time to writer my own XML parsers and writers to suit their sick taste |
| 70 |
|
|
of "XML". (Do I repeat myself?) |
| 71 |
elmex |
1.5 |
|
| 72 |
elmex |
1.1 |
=head1 METHODS |
| 73 |
|
|
|
| 74 |
|
|
=head2 new (%args) |
| 75 |
|
|
|
| 76 |
|
|
This methods takes following arguments: |
| 77 |
|
|
|
| 78 |
|
|
=over 4 |
| 79 |
|
|
|
| 80 |
|
|
=item write_cb |
| 81 |
|
|
|
| 82 |
|
|
The callback that is called when a XML stanza was completly written |
| 83 |
|
|
and is ready for transfer. The first argument of the callback |
| 84 |
|
|
will be the character data to send to the socket. |
| 85 |
|
|
|
| 86 |
elmex |
1.9 |
=back |
| 87 |
|
|
|
| 88 |
elmex |
1.1 |
And calls C<init>. |
| 89 |
|
|
|
| 90 |
|
|
=cut |
| 91 |
|
|
|
| 92 |
|
|
sub new { |
| 93 |
|
|
my $this = shift; |
| 94 |
|
|
my $class = ref($this) || $this; |
| 95 |
|
|
my $self = { write_cb => sub {}, @_ }; |
| 96 |
|
|
bless $self, $class; |
| 97 |
|
|
$self->init; |
| 98 |
|
|
return $self; |
| 99 |
|
|
} |
| 100 |
|
|
|
| 101 |
|
|
=head2 init |
| 102 |
|
|
|
| 103 |
|
|
(Re)initializes the writer. |
| 104 |
|
|
|
| 105 |
|
|
=cut |
| 106 |
|
|
|
| 107 |
|
|
sub init { |
| 108 |
|
|
my ($self) = @_; |
| 109 |
|
|
$self->{write_buf} = ""; |
| 110 |
|
|
$self->{writer} = XML::Writer->new (OUTPUT => \$self->{write_buf}, NAMESPACES => 1); |
| 111 |
|
|
} |
| 112 |
|
|
|
| 113 |
|
|
=head2 flush () |
| 114 |
|
|
|
| 115 |
|
|
This method flushes the internal write buffer and will invoke the C<write_cb> |
| 116 |
|
|
callback. (see also C<new ()> above) |
| 117 |
|
|
|
| 118 |
|
|
=cut |
| 119 |
|
|
|
| 120 |
|
|
sub flush { |
| 121 |
|
|
my ($self) = @_; |
| 122 |
|
|
$self->{write_cb}->(substr $self->{write_buf}, 0, (length $self->{write_buf}), ''); |
| 123 |
|
|
} |
| 124 |
|
|
|
| 125 |
|
|
=head2 send_init_stream ($domain) |
| 126 |
|
|
|
| 127 |
|
|
This method will generate a XMPP stream header. C<$domain> has to be the |
| 128 |
|
|
domain of the server (or endpoint) we want to connect to. |
| 129 |
|
|
|
| 130 |
|
|
=cut |
| 131 |
|
|
|
| 132 |
|
|
sub send_init_stream { |
| 133 |
|
|
my ($self, $language, $domain) = @_; |
| 134 |
|
|
|
| 135 |
|
|
my $w = $self->{writer}; |
| 136 |
|
|
$w->xmlDecl ('UTF-8'); |
| 137 |
|
|
$w->addPrefix (xmpp_ns ('stream'), 'stream'); |
| 138 |
|
|
$w->addPrefix (xmpp_ns ('client'), ''); |
| 139 |
elmex |
1.9 |
$w->forceNSDecl (xmpp_ns ('client')); |
| 140 |
elmex |
1.1 |
$w->startTag ( |
| 141 |
|
|
[xmpp_ns ('stream'), 'stream'], |
| 142 |
|
|
to => $domain, |
| 143 |
|
|
version => '1.0', |
| 144 |
|
|
[xmpp_ns ('xml'), 'lang'] => $language |
| 145 |
|
|
); |
| 146 |
|
|
$self->flush; |
| 147 |
|
|
} |
| 148 |
|
|
|
| 149 |
|
|
=head2 send_end_of_stream |
| 150 |
|
|
|
| 151 |
|
|
Sends end of the stream. |
| 152 |
|
|
|
| 153 |
|
|
=cut |
| 154 |
|
|
|
| 155 |
|
|
sub send_end_of_stream { |
| 156 |
|
|
my ($self) = @_; |
| 157 |
|
|
my $w = $self->{writer}; |
| 158 |
|
|
$w->endTag ([xmpp_ns ('stream'), 'stream']); |
| 159 |
|
|
$self->flush; |
| 160 |
|
|
} |
| 161 |
|
|
|
| 162 |
|
|
=head2 send_sasl_auth ($mechanisms) |
| 163 |
|
|
|
| 164 |
|
|
This methods sends the start of a SASL authentication. C<$mechanisms> is |
| 165 |
|
|
a string with space seperated mechanisms that are supported by the other |
| 166 |
|
|
end. |
| 167 |
|
|
|
| 168 |
|
|
=cut |
| 169 |
|
|
|
| 170 |
|
|
sub send_sasl_auth { |
| 171 |
|
|
my ($self, $mechanisms, $user, $domain, $pass) = @_; |
| 172 |
|
|
|
| 173 |
|
|
my $sasl = Authen::SASL->new ( |
| 174 |
|
|
mechanism => $mechanisms, |
| 175 |
|
|
callback => { |
| 176 |
elmex |
1.9 |
authname => $user,# . '@' . $domain, |
| 177 |
elmex |
1.1 |
user => $user, |
| 178 |
|
|
pass => $pass, |
| 179 |
|
|
} |
| 180 |
|
|
); |
| 181 |
|
|
|
| 182 |
|
|
$self->{sasl} = $sasl->client_new ('xmpp', $domain); |
| 183 |
|
|
|
| 184 |
|
|
my $w = $self->{writer}; |
| 185 |
|
|
$w->addPrefix (xmpp_ns ('sasl'), ''); |
| 186 |
|
|
$w->startTag ([xmpp_ns ('sasl'), 'auth'], mechanism => $self->{sasl}->mechanism); |
| 187 |
|
|
$w->characters (MIME::Base64::encode_base64 ($self->{sasl}->client_start, '')); |
| 188 |
|
|
$w->endTag; |
| 189 |
|
|
$self->flush; |
| 190 |
|
|
} |
| 191 |
|
|
|
| 192 |
|
|
=head2 send_sasl_response ($challenge) |
| 193 |
|
|
|
| 194 |
|
|
This method generated the SASL authentication response to a C<$challenge>. |
| 195 |
|
|
You must not call this method without calling C<send_sasl_auth ()> before. |
| 196 |
|
|
|
| 197 |
|
|
=cut |
| 198 |
|
|
|
| 199 |
|
|
sub send_sasl_response { |
| 200 |
|
|
my ($self, $challenge) = @_; |
| 201 |
|
|
$challenge = MIME::Base64::decode_base64 ($challenge); |
| 202 |
|
|
my $ret = ''; |
| 203 |
|
|
unless ($challenge =~ /rspauth=/) { # rspauth basically means: we are done |
| 204 |
|
|
$ret = $self->{sasl}->client_step ($challenge); |
| 205 |
|
|
unless ($ret) { |
| 206 |
|
|
die "Error in SASL authentication in client step with challenge: '$challenge'\n"; |
| 207 |
|
|
} |
| 208 |
|
|
} |
| 209 |
|
|
my $w = $self->{writer}; |
| 210 |
|
|
$w->addPrefix (xmpp_ns ('sasl'), ''); |
| 211 |
|
|
$w->startTag ([xmpp_ns ('sasl'), 'response']); |
| 212 |
|
|
$w->characters (MIME::Base64::encode_base64 ($ret, '')); |
| 213 |
|
|
$w->endTag; |
| 214 |
|
|
$self->flush; |
| 215 |
|
|
} |
| 216 |
|
|
|
| 217 |
elmex |
1.2 |
=head2 send_starttls |
| 218 |
|
|
|
| 219 |
|
|
Sends the starttls command to the server. |
| 220 |
|
|
|
| 221 |
|
|
=cut |
| 222 |
|
|
|
| 223 |
|
|
sub send_starttls { |
| 224 |
|
|
my ($self) = @_; |
| 225 |
|
|
my $w = $self->{writer}; |
| 226 |
|
|
$w->addPrefix (xmpp_ns ('tls'), ''); |
| 227 |
|
|
$w->emptyTag ([xmpp_ns ('tls'), 'starttls']); |
| 228 |
|
|
$self->flush; |
| 229 |
|
|
} |
| 230 |
|
|
|
| 231 |
elmex |
1.1 |
=head2 send_iq ($id, $type, $create_cb, %attrs) |
| 232 |
|
|
|
| 233 |
elmex |
1.3 |
This method sends an IQ stanza of type C<$type> (to be compliant |
| 234 |
|
|
only use: 'get', 'set', 'result' and 'error'). C<$create_cb> |
| 235 |
elmex |
1.1 |
will be called with an XML::Writer instance as first argument. |
| 236 |
|
|
C<$create_cb> should be used to fill the IQ xml stanza. |
| 237 |
elmex |
1.3 |
If C<$create_cb> is undefined an empty tag will be generated. |
| 238 |
elmex |
1.1 |
|
| 239 |
|
|
C<%attrs> should have further attributes for the IQ stanza tag. |
| 240 |
|
|
For example 'to' or 'from'. If the C<%attrs> contain a 'lang' attribute |
| 241 |
|
|
it will be put into the 'xml' namespace. |
| 242 |
|
|
|
| 243 |
elmex |
1.3 |
C<$id> is the id to give this IQ stanza and is mandatory in this API. |
| 244 |
elmex |
1.1 |
|
| 245 |
|
|
=cut |
| 246 |
|
|
|
| 247 |
|
|
sub send_iq { |
| 248 |
|
|
my ($self, $id, $type, $create_cb, %attrs) = @_; |
| 249 |
|
|
my $w = $self->{writer}; |
| 250 |
|
|
$w->addPrefix (xmpp_ns ('bind'), ''); |
| 251 |
|
|
my (@from) = ($self->{jid} ? (from => $self->{jid}) : ()); |
| 252 |
|
|
if ($attrs{lang}) { |
| 253 |
|
|
push @from, ([ xmpp_ns ('xml'), 'lang' ] => delete $attrs{leng}) |
| 254 |
|
|
} |
| 255 |
elmex |
1.6 |
push @from, (id => $id) if defined $id; |
| 256 |
elmex |
1.3 |
if (defined $create_cb) { |
| 257 |
elmex |
1.6 |
$w->startTag ('iq', type => $type, @from, %attrs); |
| 258 |
elmex |
1.3 |
$create_cb->($w); |
| 259 |
|
|
$w->endTag; |
| 260 |
|
|
} else { |
| 261 |
elmex |
1.6 |
$w->emptyTag ('iq', type => $type, @from, %attrs); |
| 262 |
elmex |
1.3 |
} |
| 263 |
|
|
$self->flush; |
| 264 |
|
|
} |
| 265 |
|
|
|
| 266 |
|
|
=head2 send_presence ($id, $type, $create_cb, %attrs) |
| 267 |
|
|
|
| 268 |
|
|
Sends a presence stanza. |
| 269 |
|
|
|
| 270 |
|
|
C<$create_cb> has the same meaning as for C<send_iq>. |
| 271 |
|
|
C<%attrs> will let you pass further optional arguments like 'to'. |
| 272 |
|
|
|
| 273 |
|
|
C<$type> is the type of the presence, which may be one of: |
| 274 |
|
|
|
| 275 |
|
|
unavailable, subscribe, subscribed, unsubscribe, unsubscribed, probe, error |
| 276 |
|
|
|
| 277 |
|
|
Or something completly different if you don't like the RFC 3921 :-) |
| 278 |
|
|
|
| 279 |
|
|
C<%attrs> contains further attributes for the presence tag or may contain one of the |
| 280 |
|
|
following exceptional keys: |
| 281 |
|
|
|
| 282 |
|
|
If C<%attrs> contains a 'show' key: a child xml tag with that name will be geenerated |
| 283 |
|
|
with the value as the content, which should be one of 'away', 'chat', 'dnd' and 'xa'. |
| 284 |
|
|
|
| 285 |
|
|
If C<%attrs> contains a 'status' key: a child xml tag with that name will be generated |
| 286 |
|
|
with the value as content. If the value of the 'status' key is an hash reference |
| 287 |
|
|
the keys will be interpreted as language identifiers for the xml:lang attribute |
| 288 |
|
|
of each status element. If one of these keys is the empty string '' no xml:lang attribute |
| 289 |
|
|
will be generated for it. The values will be the character content of the status tags. |
| 290 |
|
|
|
| 291 |
|
|
If C<%attrs> contains a 'priority' key: a child xml tag with that name will be generated |
| 292 |
|
|
with the value as content, which must be a number between -128 and +127. |
| 293 |
|
|
|
| 294 |
|
|
Note: If C<$create_cb> is undefined and one of the above attributes (show, |
| 295 |
|
|
status or priority) were given, the generates presence tag won't be empty. |
| 296 |
|
|
|
| 297 |
|
|
=cut |
| 298 |
|
|
|
| 299 |
|
|
sub _generate_key_xml { |
| 300 |
|
|
my ($w, $key, $value) = @_; |
| 301 |
|
|
$w->startTag ($key); |
| 302 |
|
|
$w->characters ($value); |
| 303 |
elmex |
1.1 |
$w->endTag; |
| 304 |
|
|
} |
| 305 |
|
|
|
| 306 |
elmex |
1.3 |
sub _generate_key_xmls { |
| 307 |
|
|
my ($w, $key, $value) = @_; |
| 308 |
|
|
if (ref ($value) eq 'HASH') { |
| 309 |
|
|
for (keys %$value) { |
| 310 |
|
|
$w->startTag ($key, [xmpp_ns ('xml'), 'lang'] => $_); |
| 311 |
|
|
$w->characters ($value->{$_}); |
| 312 |
|
|
$w->endTag; |
| 313 |
|
|
} |
| 314 |
|
|
} else { |
| 315 |
|
|
$w->startTag ($key); |
| 316 |
|
|
$w->characters ($value); |
| 317 |
|
|
$w->endTag; |
| 318 |
|
|
} |
| 319 |
elmex |
1.1 |
} |
| 320 |
|
|
|
| 321 |
|
|
sub send_presence { |
| 322 |
elmex |
1.4 |
my ($self, $id, $type, $create_cb, %attrs) = @_; |
| 323 |
elmex |
1.3 |
|
| 324 |
elmex |
1.1 |
my $w = $self->{writer}; |
| 325 |
|
|
$w->addPrefix (xmpp_ns ('client'), ''); |
| 326 |
elmex |
1.3 |
|
| 327 |
|
|
my @add; |
| 328 |
elmex |
1.4 |
push @add, (type => $type) if defined $type; |
| 329 |
|
|
push @add, (id => $id) if defined $id; |
| 330 |
elmex |
1.3 |
|
| 331 |
elmex |
1.7 |
my %fattrs = |
| 332 |
|
|
map { $_ => $attrs{$_} } |
| 333 |
|
|
grep { my $k = $_; not grep { $k ne $_ } qw/show priority status/ } |
| 334 |
|
|
keys %attrs; |
| 335 |
|
|
|
| 336 |
elmex |
1.3 |
if (defined $create_cb) { |
| 337 |
elmex |
1.7 |
$w->startTag ('presence', @add, %fattrs); |
| 338 |
|
|
_generate_key_xml ($w, show => $attrs{show}) if defined $attrs{show}; |
| 339 |
|
|
_generate_key_xml ($w, priority => $attrs{priority}) if defined $attrs{priority}; |
| 340 |
|
|
_generate_key_xmls ($w, status => $attrs{status}) if defined $attrs{status}; |
| 341 |
elmex |
1.3 |
$create_cb->($w); |
| 342 |
|
|
$w->endTag; |
| 343 |
|
|
} else { |
| 344 |
|
|
if (exists $attrs{show} or $attrs{priority} or $attrs{status}) { |
| 345 |
elmex |
1.7 |
$w->startTag ('presence', @add, %fattrs); |
| 346 |
|
|
_generate_key_xml ($w, show => $attrs{show}) if defined $attrs{show}; |
| 347 |
|
|
_generate_key_xml ($w, priority => $attrs{priority}) if defined $attrs{priority}; |
| 348 |
|
|
_generate_key_xmls ($w, status => $attrs{status}) if defined $attrs{status}; |
| 349 |
elmex |
1.3 |
$w->endTag; |
| 350 |
|
|
} else { |
| 351 |
elmex |
1.7 |
$w->emptyTag ('presence', @add, %fattrs); |
| 352 |
elmex |
1.3 |
} |
| 353 |
|
|
} |
| 354 |
|
|
|
| 355 |
elmex |
1.1 |
$self->flush; |
| 356 |
|
|
} |
| 357 |
|
|
|
| 358 |
elmex |
1.3 |
=head2 send_message ($id, $to, $type, $create_cb, %attrs) |
| 359 |
|
|
|
| 360 |
|
|
Sends a message stanza. |
| 361 |
|
|
|
| 362 |
|
|
C<$to> is the destination JID of the message. C<$type> is |
| 363 |
elmex |
1.6 |
the type of the message, and if C<$type> is undefined it will default to 'chat'. |
| 364 |
elmex |
1.3 |
C<$type> must be one of the following: 'chat', 'error', 'groupchat', 'headline' |
| 365 |
|
|
or 'normal'. |
| 366 |
|
|
|
| 367 |
|
|
C<$create_cb> has the same meaning as in C<send_iq>. |
| 368 |
|
|
|
| 369 |
|
|
C<%attrs> contains further attributes for the message tag or may contain one of the |
| 370 |
|
|
following exceptional keys: |
| 371 |
|
|
|
| 372 |
|
|
If C<%attrs> contains a 'body' key: a child xml tag with that name will be generated |
| 373 |
|
|
with the value as content. If the value of the 'body' key is an hash reference |
| 374 |
|
|
the keys will be interpreted as language identifiers for the xml:lang attribute |
| 375 |
|
|
of each body element. If one of these keys is the empty string '' no xml:lang attribute |
| 376 |
|
|
will be generated for it. The values will be the character content of the body tags. |
| 377 |
|
|
|
| 378 |
|
|
If C<%attrs> contains a 'subject' key: a child xml tag with that name will be generated |
| 379 |
|
|
with the value as content. If the value of the 'subject' key is an hash reference |
| 380 |
|
|
the keys will be interpreted as language identifiers for the xml:lang attribute |
| 381 |
|
|
of each subject element. If one of these keys is the empty string '' no xml:lang attribute |
| 382 |
|
|
will be generated for it. The values will be the character content of the subject tags. |
| 383 |
|
|
|
| 384 |
|
|
If C<%attrs> contains a 'thread' key: a child xml tag with that name will be generated |
| 385 |
|
|
and the value will be the character content. |
| 386 |
|
|
|
| 387 |
|
|
=cut |
| 388 |
|
|
|
| 389 |
elmex |
1.1 |
sub send_message { |
| 390 |
elmex |
1.4 |
my ($self, $id, $to, $type, $create_cb, %attrs) = @_; |
| 391 |
elmex |
1.3 |
|
| 392 |
elmex |
1.1 |
my $w = $self->{writer}; |
| 393 |
|
|
$w->addPrefix (xmpp_ns ('client'), ''); |
| 394 |
elmex |
1.3 |
|
| 395 |
elmex |
1.4 |
my @add; |
| 396 |
|
|
push @add, (id => $id) if defined $id; |
| 397 |
|
|
|
| 398 |
elmex |
1.3 |
$type ||= 'chat'; |
| 399 |
|
|
|
| 400 |
elmex |
1.7 |
my %fattrs = |
| 401 |
|
|
map { $_ => $attrs{$_} } |
| 402 |
|
|
grep { my $k = $_; not grep { $k ne $_ } qw/subject body thread/ } |
| 403 |
|
|
keys %attrs; |
| 404 |
|
|
|
| 405 |
elmex |
1.3 |
if (defined $create_cb) { |
| 406 |
elmex |
1.7 |
$w->startTag ('message', @add, to => $to, type => $type, %fattrs); |
| 407 |
|
|
_generate_key_xmls ($w, subject => $attrs{subject}) if defined $attrs{subject}; |
| 408 |
|
|
_generate_key_xmls ($w, body => $attrs{body}) if defined $attrs{body}; |
| 409 |
|
|
_generate_key_xml ($w, thread => $attrs{thread}) if defined $attrs{thread}; |
| 410 |
elmex |
1.3 |
$create_cb->($w); |
| 411 |
|
|
$w->endTag; |
| 412 |
|
|
} else { |
| 413 |
|
|
if (exists $attrs{subject} or $attrs{body} or $attrs{thread}) { |
| 414 |
elmex |
1.7 |
$w->startTag ('message', @add, to => $to, type => $type, %fattrs); |
| 415 |
|
|
_generate_key_xmls ($w, subject => $attrs{subject}) if defined $attrs{subject}; |
| 416 |
|
|
_generate_key_xmls ($w, body => $attrs{body}) if defined $attrs{body}; |
| 417 |
|
|
_generate_key_xml ($w, thread => $attrs{thread}) if defined $attrs{thread}; |
| 418 |
elmex |
1.3 |
$w->endTag; |
| 419 |
|
|
} else { |
| 420 |
elmex |
1.7 |
$w->emptyTag ('message', @add, to => $to, type => $type, %fattrs); |
| 421 |
elmex |
1.3 |
} |
| 422 |
|
|
} |
| 423 |
|
|
|
| 424 |
elmex |
1.1 |
$self->flush; |
| 425 |
|
|
} |
| 426 |
|
|
|
| 427 |
elmex |
1.4 |
|
| 428 |
|
|
=head2 write_error_tag ($error_stanza_node, $error_type, $error) |
| 429 |
|
|
|
| 430 |
|
|
C<$error_type> is one of 'cancel', 'continue', 'modify', 'auth' and 'wait'. |
| 431 |
|
|
C<$error> is the name of the error tag child element. If C<$error> is one of |
| 432 |
|
|
the following: |
| 433 |
|
|
|
| 434 |
|
|
'bad-request', 'conflict', 'feature-not-implemented', 'forbidden', 'gone', |
| 435 |
|
|
'internal-server-error', 'item-not-found', 'jid-malformed', 'not-acceptable', |
| 436 |
|
|
'not-allowed', 'not-authorized', 'payment-required', 'recipient-unavailable', |
| 437 |
|
|
'redirect', 'registration-required', 'remote-server-not-found', |
| 438 |
|
|
'remote-server-timeout', 'resource-constraint', 'service-unavailable', |
| 439 |
|
|
'subscription-required', 'undefined-condition', 'unexpected-request' |
| 440 |
|
|
|
| 441 |
|
|
then a default can be select for C<$error_type>, and the argument can be undefined. |
| 442 |
|
|
|
| 443 |
|
|
Note: This method is currently a bit limited in the generation of the xml |
| 444 |
|
|
for the errors, if you need more please contact me. |
| 445 |
|
|
|
| 446 |
|
|
=cut |
| 447 |
|
|
|
| 448 |
|
|
our %STANZA_ERRORS = ( |
| 449 |
|
|
'bad-request' => ['modify', 400], |
| 450 |
|
|
'conflict' => ['cancel', 409], |
| 451 |
|
|
'feature-not-implemented' => ['cancel', 501], |
| 452 |
|
|
'forbidden' => ['auth', 403], |
| 453 |
|
|
'gone' => ['modify', 302], |
| 454 |
|
|
'internal-server-error' => ['wait', 500], |
| 455 |
|
|
'item-not-found' => ['cancel', 404], |
| 456 |
|
|
'jid-malformed' => ['modify', 400], |
| 457 |
|
|
'not-acceptable' => ['modify', 406], |
| 458 |
|
|
'not-allowed' => ['cancel', 405], |
| 459 |
|
|
'not-authorized' => ['auth', 401], |
| 460 |
|
|
'payment-required' => ['auth', 402], |
| 461 |
|
|
'recipient-unavailable' => ['wait', 404], |
| 462 |
|
|
'redirect' => ['modify', 302], |
| 463 |
|
|
'registration-required' => ['auth', 407], |
| 464 |
|
|
'remote-server-not-found' => ['cancel', 404], |
| 465 |
|
|
'remote-server-timeout' => ['wait', 504], |
| 466 |
|
|
'resource-constraint' => ['wait', 500], |
| 467 |
|
|
'service-unavailable' => ['cancel', 503], |
| 468 |
|
|
'subscription-required' => ['auth', 407], |
| 469 |
|
|
'undefined-condition' => ['cancel', 500], |
| 470 |
|
|
'unexpected-request' => ['wait', 400], |
| 471 |
|
|
); |
| 472 |
|
|
|
| 473 |
|
|
sub write_error_tag { |
| 474 |
|
|
my ($self, $errstanza, $type, $error) = @_; |
| 475 |
|
|
|
| 476 |
|
|
my $w = $self->{writer}; |
| 477 |
|
|
|
| 478 |
|
|
$_->write_on ($w) for $errstanza->nodes; |
| 479 |
|
|
|
| 480 |
|
|
my @add; |
| 481 |
|
|
|
| 482 |
|
|
unless (defined $type and defined $STANZA_ERRORS{$error}) { |
| 483 |
|
|
$type = $STANZA_ERRORS{$error}->[0]; |
| 484 |
|
|
} |
| 485 |
|
|
|
| 486 |
|
|
if ($Net::XMPP2::EXTENSION_ENABLED{'86'} and defined $STANZA_ERRORS{$error}) { |
| 487 |
|
|
push @add, (code => $STANZA_ERRORS{$error}->[1]); |
| 488 |
|
|
} |
| 489 |
|
|
|
| 490 |
|
|
$w->addPrefix (xmpp_ns ('client'), ''); |
| 491 |
|
|
$w->startTag ([xmpp_ns ('client') => 'error'], type => $type, @add); |
| 492 |
|
|
$w->addPrefix (xmpp_ns ('stanzas'), ''); |
| 493 |
|
|
$w->emptyTag ([xmpp_ns ('stanzas') => $error]); |
| 494 |
|
|
$w->endTag; |
| 495 |
|
|
} |
| 496 |
|
|
|
| 497 |
elmex |
1.1 |
=head1 AUTHOR |
| 498 |
|
|
|
| 499 |
|
|
Robin Redeker, C<< <elmex at ta-sa.org> >> |
| 500 |
|
|
|
| 501 |
|
|
=head1 COPYRIGHT & LICENSE |
| 502 |
|
|
|
| 503 |
|
|
Copyright 2007 Robin Redeker, all rights reserved. |
| 504 |
|
|
|
| 505 |
|
|
This program is free software; you can redistribute it and/or modify it |
| 506 |
|
|
under the same terms as Perl itself. |
| 507 |
|
|
|
| 508 |
|
|
=cut |
| 509 |
|
|
|
| 510 |
|
|
1; # End of Net::XMPP2 |