ViewVC Help
View File | Revision Log | Show Annotations | Download File
/cvs/cvsroot/Net-XMPP2/lib/Net/XMPP2/Writer.pm
Revision: 1.21
Committed: Fri Jul 20 20:41:48 2007 UTC (19 years, 2 months ago) by elmex
Branch: MAIN
Changes since 1.20: +31 -4 lines
Log Message:
lots of changes. added as_string to Net::XMPP2::Node to restore
the original xml document part of a stanza or subtree.
also implemented the jabber component protocol.

File Contents

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