File: JABBER.pm

package info (click to toggle)
movabletype-opensource 5.1.4%2Bdfsg-4%2Bdeb7u3
  • links: PTS, VCS
  • area: main
  • in suites: wheezy
  • size: 32,996 kB
  • sloc: perl: 197,285; php: 62,405; sh: 166; xml: 117; makefile: 83; sql: 32
file content (245 lines) | stat: -rw-r--r-- 6,683 bytes parent folder | download | duplicates (2)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
# ======================================================================
#
# Copyright (C) 2000-2001 Paul Kulchenko (paulclinger@yahoo.com)
# SOAP::Lite is free software; you can redistribute it
# and/or modify it under the same terms as Perl itself.
#
# $Id: JABBER.pm 51 2004-11-14 19:30:50Z byrnereese $
#
# ======================================================================

package SOAP::Transport::JABBER;

use strict;
use vars qw($VERSION);
#$VERSION = sprintf("%d.%s", map {s/_//g; $_} q$Name$ =~ /-(\d+)_([\d_]+)/);
$VERSION = $SOAP::Lite::VERSION;

use Net::Jabber 1.0021 qw(Client); 
use URI::Escape; 
use URI;
use SOAP::Lite;

my $NAMESPACE = "http://namespaces.soaplite.com/transport/jabber";

{ local $^W; 
  # fix problem with printData in 1.0021
  *Net::Jabber::printData = sub {'nothing'} if Net::Jabber->VERSION == 1.0021;

  # fix problem with Unicode encoding in EscapeXML. Jabber ALWAYS convert latin to utf8
  *Net::Jabber::EscapeXML = *Net::Jabber::EscapeXML = # that's Jabber 1.0021
  *XML::Stream::EscapeXML = *XML::Stream::EscapeXML = # that's Jabber 1.0022
    \&SOAP::Utils::encode_data; 

  # There is also an error in XML::Stream::UnescapeXML 1.12, but
  # we can't do anything there, except hack it also :(
}

# ======================================================================

package URI::jabber; # ok, lets do 'jabber://' scheme
require URI::_server; require URI::_userpass; 
@URI::jabber::ISA=qw(URI::_server URI::_userpass);

  # jabber://soaplite_client:soapliteclient@jabber.org:5222/soaplite_server@jabber.org/Home
  # ^^^^^^   ^^^^^^^^^^^^^^^ ^^^^^^^^^^^^^^ ^^^^^^^^^^ ^^^^ ^^^^^^^^^^^^^^^^^^^^^^^^^^ ^^^^

# ======================================================================

package SOAP::Transport::JABBER::Query;

sub new {
  my $proto = shift;
  bless {} => ref($proto) || $proto;
}

sub SetPayload {
  shift; Net::Jabber::SetXMLData("single",shift->{QUERY},"payload",shift,{});
}

sub GetPayload {
  shift; Net::Jabber::GetXMLData("value",shift->{QUERY},"payload","");
}

# ======================================================================

package SOAP::Transport::JABBER::Client;

use vars qw(@ISA);
@ISA = qw(SOAP::Client Net::Jabber::Client);

sub DESTROY { SOAP::Trace::objects('()') }

sub new { 
  my $self = shift;

  unless (ref $self) {
    my $class = ref($self) || $self;
    my(@params, @methods);
    while (@_) { $class->can($_[0]) ? push(@methods, shift() => shift) : push(@params, shift) }
    $self = $class->SUPER::new(@params);
    while (@methods) { my($method, $params) = splice(@methods,0,2);
      $self->$method(ref $params eq 'ARRAY' ? @$params : $params) 
    }
    SOAP::Trace::objects('()');
  }
  return $self;
}

sub endpoint {
  my $self = shift;

  return $self->SUPER::endpoint unless @_;

  my $endpoint = shift;

  # nothing to do if new endpoint is the same as current one
  return $self if $self->SUPER::endpoint && $self->SUPER::endpoint eq $endpoint;

  my $uri = URI->new($endpoint);
  my($undef, $to, $resource) = split m!/!, $uri->path, 3;
  $self->Connect(
    hostname => $uri->host, 
    port => $uri->port,
  ) or Carp::croak "Can't connect to @{[$uri->host_port]}: $!";

  my @result = $self->AuthSend(
    username => $uri->user, 
    password => $uri->password,
    resource => 'soapliteClient',
  );
  $result[0] eq "ok" or Carp::croak "Can't authenticate to @{[$uri->host_port]}: @result";

  $self->AddDelegate(
    namespace  => $NAMESPACE,
    parent     => 'Net::Jabber::Query',
    parenttype => 'query',
    delegate   => 'SOAP::Transport::JABBER::Query',
  );

  # Get roster and announce presence
  $self->RosterGet();
  $self->PresenceSend();

  $self->SUPER::endpoint($endpoint);
}

sub send_receive {
  my($self, %parameters) = @_;
  my($envelope, $endpoint, $encoding) = 
    @parameters{qw(envelope endpoint encoding)};

  $self->endpoint($endpoint ||= $self->endpoint);

  my($undef, $to, $resource) = split m!/!, URI->new($endpoint)->path, 3;

  # Create a Jabber info/query message
  my $iq = new Net::Jabber::IQ();
  $iq->SetIQ(
    type => 'set',
    to   => join '/', $to => $resource || 'soapliteServer',
  );
  my $query = $iq->NewQuery($NAMESPACE);
  $query->SetPayload($envelope);

  SOAP::Trace::debug($envelope);

  my $iq_rcvd = $self->SendAndReceiveWithID($iq);
  my($query_rcvd) = $iq_rcvd->GetQuery($NAMESPACE) if $iq_rcvd; # expect only one
  my $msg = $query_rcvd->GetPayload() if $query_rcvd;

  SOAP::Trace::debug($msg);

  my $code = $self->GetErrorCode();

  $self->code($code);
  $self->message($code);
  $self->is_success(!defined $code || $code eq '');
  $self->status($code);

  return $msg;
}

# ======================================================================

package SOAP::Transport::JABBER::Server;

use Carp ();
use vars qw(@ISA $AUTOLOAD);
@ISA = qw(SOAP::Server);

sub new {
  my $self = shift;
    
  unless (ref $self) {
    my $class = ref($self) || $self;
    my $uri = URI->new(shift);
    $self = $class->SUPER::new(@_);

    $self->{_jabberserver} = Net::Jabber::Client->new;
    $self->{_jabberserver}->Connect(
      hostname      => $uri->host,
      port          => $uri->port,
    ) or Carp::croak "Can't connect to @{[$uri->host_port]}: $!";

    my($undef, $resource) = split m!/!, $uri->path, 2;
    my @result = $self->AuthSend(
      username => $uri->user, 
      password => $uri->password,
      resource => $resource || 'soapliteServer',
    );
    $result[0] eq "ok" or Carp::croak "Can't authenticate to @{[$uri->host_port]}: @result";

    $self->{_jabberserver}->SetCallBacks(
      iq => sub {
        shift;
        my $iq = new Net::Jabber::IQ(@_);

        my($query) = $iq->GetQuery($NAMESPACE); # expect only one
        my $request = $query->GetPayload();

        SOAP::Trace::debug($request);

        # Set up response
        my $reply = $iq->Reply;
        my $x = $reply->NewQuery($NAMESPACE);

        my $response = $self->SUPER::handle($request);
        $x->SetPayload($response);

        # Send response
        $self->{_jabberserver}->Send($reply);
      }
    );

    $self->AddDelegate(
      namespace  => $NAMESPACE,
      parent     => 'Net::Jabber::Query',
      parenttype => 'query',
      delegate   => 'SOAP::Transport::JABBER::Query',
    );
  
    $self->RosterGet();
    $self->PresenceSend();
  }
  return $self;
}

sub AUTOLOAD {
  my $method = substr($AUTOLOAD, rindex($AUTOLOAD, '::') + 2);
  return if $method eq 'DESTROY';

  no strict 'refs';
  *$AUTOLOAD = sub { shift->{_jabberserver}->$method(@_) };
  goto &$AUTOLOAD;
}

sub handle {
  shift->Process();
}

# ======================================================================

1;

__END__