AnyEvent-XMPP
view release on metacpan or search on metacpan
lib/AnyEvent/XMPP/Writer.pm view on Meta::CPAN
character data or attribute values containing unescaped characters
that map to the predefined entities (Section 4.6 therein);
such characters MUST be escaped
This means:
You have to escape '>' in the character data. I don't know whether XML::Writer
does that. And I honestly don't care much about this. XMPP is broken by design and
I have barely time to writer my own XML parsers and writers to suit their sick taste
of "XML". (Do I repeat myself?)
I would be happy if they finally say (in RFC3920): "XMPP is NOT XML. It's just
XML-like, and some XML utilities allow you to process this kind of XML.".
=head1 METHODS
=over 4
=item B<new (%args)>
This methods takes following arguments:
=over 4
=item write_cb
The callback that is called when a XML stanza was completely written and is
ready for transfer. The first argument of the callback will be the character
data to send to the socket.
=back
And calls C<init>.
=cut
sub new {
my $this = shift;
my $class = ref($this) || $this;
my $self = {
write_cb => sub {},
send_iq_cb => sub {},
send_msg_cb => sub {},
send_pres_cb => sub {},
@_
};
bless $self, $class;
$self->init;
return $self;
}
=item B<init>
(Re)initializes the writer.
=cut
sub init {
my ($self) = @_;
$self->{write_buf} = "";
$self->{writer} =
XML::Writer->new (OUTPUT => \$self->{write_buf}, NAMESPACES => 1, UNSAFE => 1);
}
=item B<flush ()>
This method flushes the internal write buffer and will invoke the C<write_cb>
callback. (see also C<new ()> above)
=cut
sub flush {
my ($self) = @_;
$self->{write_cb}->(substr $self->{write_buf}, 0, (length $self->{write_buf}), '');
}
=item B<send_init_stream ($language, $domain, $namespace)>
This method will generate a XMPP stream header. C<$domain> has to be the
domain of the server (or endpoint) we want to connect to.
C<$namespace> is the namespace URI or the tag (from L<AnyEvent::XMPP::Namespaces>)
for the stream namespace. (This is used by L<AnyEvent::XMPP::Component> to connect
as component to a server). C<$namespace> can also be undefined, in this case
the C<client> namespace will be used.
=cut
sub send_init_stream {
my ($self, $language, $domain, $ns, $vers_override) = @_;
$ns ||= 'client';
my $w = $self->{writer};
$w->xmlDecl ();
$w->addPrefix (xmpp_ns ('stream'), 'stream');
$w->addPrefix (xmpp_ns ($ns), '');
$w->forceNSDecl (xmpp_ns ($ns));
$w->startTag (
[xmpp_ns ('stream'), 'stream'],
to => $domain,
version => (defined $vers_override ? $vers_override : '1.0'),
[xmpp_ns ('xml'), 'lang'] => $language
);
$self->flush;
}
=item B<send_whitespace_ping>
This method sends a single space to the server.
=cut
sub send_whitespace_ping {
my ($self) = @_;
$self->{writer}->raw (' ');
$self->flush;
}
=item B<send_handshake ($streamid, $secret)>
This method sends a component handshake. Please note that C<$secret>
( run in 0.994 second using v1.01-cache-2.11-cpan-5b529ec07f3 )