AnyEvent-XMPP

 view release on metacpan or  search on metacpan

lib/AnyEvent/XMPP/Writer.pm  view on Meta::CPAN


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>
must be XML escaped!

=cut

sub send_handshake {
   my ($self, $id, $secret) = @_;
   my $out_secret = encode ("UTF-8", $secret);
   my $out = lc sha1_hex ($id . $out_secret);
   simxml ($self->{writer}, defns => 'component', node => {
      ns => 'component', name => 'handshake', childs => [ $out ]
   });
   $self->flush;
}

=item B<send_end_of_stream>

Sends end of the stream.



( run in 0.728 second using v1.01-cache-2.11-cpan-d7f47b0818f )