set the 'from' address in outbound xmpp messages
[OpenSRF.git] / src / perl / lib / OpenSRF / Transport / SlimJabber / Client.pm
1 package OpenSRF::Transport::SlimJabber::Client;
2
3 use strict;
4 use warnings;
5
6 use OpenSRF::EX;
7 use OpenSRF::Utils::Config;
8 use OpenSRF::Utils::Logger qw/$logger/;
9 use OpenSRF::Transport::SlimJabber::XMPPReader;
10 use OpenSRF::Transport::SlimJabber::XMPPMessage;
11 use IO::Socket::UNIX;
12 use FreezeThaw qw/freeze/;
13
14 sub DESTROY{
15     shift()->disconnect;
16 }
17
18 =head1 NAME
19
20 OpenSRF::Transport::SlimJabber::Client
21
22 =head1 SYNOPSIS
23
24
25
26 =head1 DESCRIPTION
27
28
29
30 =cut
31
32 =head1 METHODS
33
34 =head2 new
35
36 =cut
37
38 sub new {
39         my( $class, %params ) = @_;
40     my $self = bless({}, ref($class) || $class);
41     $self->params(\%params);
42         return $self;
43 }
44
45 =head2 reader
46
47 =cut
48
49 sub reader {
50     my($self, $reader) = @_;
51     $self->{reader} = $reader if $reader;
52     return $self->{reader};
53 }
54
55 =head2 params
56
57 =cut
58
59 sub params {
60     my($self, $params) = @_;
61     $self->{params} = $params if $params;
62     return $self->{params};
63 }
64
65 =head2 socket
66
67 =cut
68
69 sub socket {
70     my($self, $socket) = @_;
71     $self->{socket} = $socket if $socket;
72     return $self->{socket};
73 }
74
75 =head2 disconnect
76
77 =cut
78
79 sub disconnect {
80     my $self = shift;
81         $self->reader->disconnect if $self->reader;
82 }
83
84
85 =head2 gather
86
87 =cut
88
89 sub gather { 
90     my $self = shift; 
91     $self->process( 0 ); 
92 }
93
94 # -------------------------------------------------
95
96 =head2 tcp_connected
97
98 =cut
99
100 sub tcp_connected {
101         my $self = shift;
102     return $self->reader->tcp_connected if $self->reader;
103     return 0;
104 }
105
106
107
108 =head2 send
109
110 =cut
111
112 sub send {
113         my $self = shift;
114     my $msg = OpenSRF::Transport::SlimJabber::XMPPMessage->new(@_);
115     $msg->osrf_xid($logger->get_osrf_xid);
116     $msg->from($self->xmpp_id);
117     $self->reader->send($msg->to_xml);
118 }
119
120 =head2 initialize
121
122 =cut
123
124 sub initialize {
125
126         my $self = shift;
127
128         my $host        = $self->params->{host}; 
129         my $port        = $self->params->{port}; 
130         my $username    = $self->params->{username};
131         my $resource    = $self->params->{resource};
132         my $password    = $self->params->{password};
133
134         my $conf = OpenSRF::Utils::Config->current;
135
136         my $tail = "_$$";
137         $tail = "" if !$conf->bootstrap->router_name and $username eq "router";
138     $resource = "$resource$tail";
139
140     my $socket = IO::Socket::INET->new(
141         PeerHost => $host,
142         PeerPort => int($port),
143         Proto  => 'tcp' );
144
145     throw OpenSRF::EX::Jabber("Could not open TCP socket to Jabber server: $@")
146             unless ( $socket and $socket->connected );
147
148     $self->socket($socket);
149     $self->reader(OpenSRF::Transport::SlimJabber::XMPPReader->new($socket));
150     $self->reader->connect($host, $username, $password, $resource);
151
152     throw OpenSRF::EX::Jabber("Could not authenticate with Jabber server: $@")
153             unless ( $self->reader->connected );
154
155     $self->xmpp_id("$username\@$host/$resource");
156         return $self;
157 }
158
159
160 # Our full login:  username@host/resource
161 sub xmpp_id {
162     my($self, $xmpp_id) = @_;
163     $self->{xmpp_id} = $xmpp_id if $xmpp_id;
164     return $self->{xmpp_id};
165 }
166
167
168 =head2 construct
169
170 =cut
171
172 sub construct {
173         my( $class, $app ) = @_;
174         $class->peer_handle($class->new( $app )->initialize());
175 }
176
177
178 =head2 process
179
180 =cut
181
182 sub process {
183         my($self, $timeout) = @_;
184
185         $timeout ||= 0;
186     $timeout = int($timeout);
187
188         unless( $self->reader and $self->reader->connected ) {
189         throw OpenSRF::EX::JabberDisconnected 
190             ("This JabberClient instance is no longer connected to the server ");
191         }
192
193     return $self->reader->wait_msg($timeout);
194 }
195
196
197 =head2 flush_socket
198
199 Sets the socket to O_NONBLOCK, reads all of the data off of the
200 socket, the restores the sockets flags.  Returns 1 on success, 0 if
201 the socket isn't connected.
202
203 =cut
204
205 sub flush_socket {
206         my $self = shift;
207     return $self->reader->flush_socket;
208 }
209
210 1;
211
212