File Coverage

blib/lib/Net/XMPP3/Message.pm
Criterion Covered Total %
statement 41 58 70.6
branch 6 14 42.8
condition 2 6 33.3
subroutine 8 14 57.1
pod 0 8 0.0
total 57 100 57.0


line stmt bran cond sub pod time code
1             ##############################################################################
2             #
3             # This library is free software; you can redistribute it and/or
4             # modify it under the terms of the GNU Library General Public
5             # License as published by the Free Software Foundation; either
6             # version 2 of the License, or (at your option) any later version.
7             #
8             # This library is distributed in the hope that it will be useful,
9             # but WITHOUT ANY WARRANTY; without even the implied warranty of
10             # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
11             # Library General Public License for more details.
12             #
13             # You should have received a copy of the GNU Library General Public
14             # License along with this library; if not, write to the
15             # Free Software Foundation, Inc., 59 Temple Place - Suite 330,
16             # Boston, MA 02111-1307, USA.
17             #
18             # Copyright (C) 1998-2004 Jabber Software Foundation http://jabber.org/
19             #
20             ##############################################################################
21              
22             package Net::XMPP3::Message;
23              
24             =head1 NAME
25              
26             Net::XMPP3::Message - XMPP Message Module
27              
28             =head1 SYNOPSIS
29              
30             Net::XMPP3::Message is a companion to the Net::XMPP3 module.
31             It provides the user a simple interface to set and retrieve all
32             parts of an XMPP Message.
33              
34             =head1 DESCRIPTION
35              
36             A Net::XMPP3::Message object is passed to the callback function for
37             the message. Also, the first argument to the callback functions is
38             the session ID from XML::Stream. There are some cases where you
39             might want thisinformation, like if you created a Client that
40             connects to two servers at once, or for writing a mini server.
41              
42             use Net::XMPP3;
43              
44             sub message {
45             my ($sid,$Mess) = @_;
46             .
47             .
48             .
49             }
50              
51             You now have access to all of the retrieval functions available.
52              
53             To create a new message to send to the server:
54              
55             use Net::XMPP3;
56              
57             $Mess = new Net::XMPP3::Message();
58              
59             Now you can call the creation functions below to populate the tag
60             before sending it.
61              
62             =head1 METHODS
63              
64             =head2 Retrieval functions
65              
66             GetTo() - returns the value in the to='' attribute for the
67             GetTo("jid") . If you specify "jid" as an argument
68             then a Net::XMPP3::JID object is returned and
69             you can easily parse the parts of the JID.
70              
71             $to = $Mess->GetTo();
72             $toJID = $Mess->GetTo("jid");
73              
74             GetFrom() - returns the value in the from='' attribute for the
75             GetFrom("jid") . If you specify "jid" as an argument
76             then a Net::XMPP3::JID object is returned and
77             you can easily parse the parts of the JID.
78              
79             $from = $Mess->GetFrom();
80             $fromJID = $Mess->GetFrom("jid");
81              
82             GetType() - returns the type='' attribute of the . Each
83             message is one of four types:
84              
85             normal regular message (default if type is blank)
86             chat one on one chat
87             groupchat multi-person chat
88             headline headline
89             error error message
90              
91             $type = $Mess->GetType();
92              
93             GetSubject() - returns the data in the tag.
94              
95             $subject = $Mess->GetSubject();
96              
97             GetBody() - returns the data in the tag.
98              
99             $body = $Mess->GetBody();
100              
101             GetThread() - returns the data in the tag.
102              
103             $thread = $Mess->GetThread();
104              
105             GetError() - returns a string with the data of the tag.
106              
107             $error = $Mess->GetError();
108              
109             GetErrorCode() - returns a string with the code='' attribute of the
110             tag.
111              
112             $errCode = $Mess->GetErrorCode();
113              
114             GetTimeStamp() - returns a string that represents the time this
115             message object was created (and probably received)
116             for sending to the client. If there is a
117             jabber:x:delay tag then that time is used to show
118             when the message was sent.
119              
120             $date = $Mess->GetTimeStamp();
121              
122              
123             =head2 Creation functions
124              
125             SetMessage(to=>string|JID, - set multiple fields in the
126             from=>string|JID, at one time. This is a cumulative
127             type=>string, and over writing action. If you set
128             subject=>string, the "to" attribute twice, the second
129             body=>string, setting is what is used. If you set
130             thread=>string, the subject, and then set the body
131             errorcode=>string, then both will be in the
132             error=>string) tag. For valid settings read the
133             specific Set functions below.
134              
135             $Mess->SetMessage(TO=>"bob\@jabber.org",
136             Subject=>"Lunch",
137             BoDy=>"Let's do lunch!");
138             $Mess->SetMessage(to=>"bob\@jabber.org",
139             from=>"jabber.org",
140             errorcode=>404,
141             error=>"Not found");
142              
143             SetTo(string) - sets the to='' attribute. You can either pass
144             SetTo(JID) a string or a JID object. They must be valid JIDs
145             or the server will return an error message.
146             (ie. bob@jabber.org/Work)
147              
148             $Mess->SetTo("test\@jabber.org");
149              
150             SetFrom(string) - sets the from='' attribute. You can either pass
151             SetFrom(JID) a string or a JID object. They must be valid JIDs
152             or the server will return an error message. (ie.
153             jabber:bob@jabber.org/Work) This field is not
154             required if you are writing a Client since the
155             server will put the JID of your connection in
156             there to prevent spamming.
157              
158             $Mess->SetFrom("me\@jabber.org");
159              
160             SetType(string) - sets the type attribute. Valid settings are:
161              
162             normal regular message (default if blank)
163             chat one one one chat style message
164             groupchat multi-person chatroom message
165             headline news headline, stock ticker, etc...
166             error error message
167              
168             $Mess->SetType("groupchat");
169              
170             SetSubject(string) - sets the subject of the .
171              
172             $Mess->SetSubject("This is a test");
173              
174             SetBody(string) - sets the body of the .
175              
176             $Mess->SetBody("To be or not to be...");
177              
178             SetThread(string) - sets the thread of the . You should
179             copy this out of the message being replied to so
180             that the thread is maintained.
181              
182             $Mess->SetThread("AE912B3");
183              
184             SetErrorCode(string) - sets the error code of the .
185              
186             $Mess->SetErrorCode(403);
187              
188             SetError(string) - sets the error string of the .
189              
190             $Mess->SetError("Permission Denied");
191              
192             Reply(hash) - creates a new Message object and populates the
193             to/from, and the subject by putting "re: " in
194             front. If you specify a hash the same as with
195             SetMessage then those values will override the
196             Reply values.
197              
198             $Reply = $Mess->Reply();
199             $Reply = $Mess->Reply(type=>"chat");
200              
201             =head2 Removal functions
202              
203             RemoveTo() - removes the to attribute from the .
204              
205             $Mess->RemoveTo();
206              
207             RemoveFrom() - removes the from attribute from the .
208              
209             $Mess->RemoveFrom();
210              
211             RemoveType() - removes the type attribute from the .
212              
213             $Mess->RemoveType();
214              
215             RemoveSubject() - removes the element from the
216             .
217              
218             $Mess->RemoveSubject();
219              
220             RemoveBody() - removes the element from the
221             .
222              
223             $Mess->RemoveBody();
224              
225             RemoveThread() - removes the element from the .
226              
227             $Mess->RemoveThread();
228              
229             RemoveError() - removes the element from the .
230              
231             $Mess->RemoveError();
232              
233             RemoveErrorCode() - removes the code attribute from the
234             element in the .
235              
236             $Mess->RemoveErrorCode();
237              
238             =head2 Test functions
239              
240             DefinedTo() - returns 1 if the to attribute is defined in the
241             , 0 otherwise.
242              
243             $test = $Mess->DefinedTo();
244              
245             DefinedFrom() - returns 1 if the from attribute is defined in the
246             , 0 otherwise.
247              
248             $test = $Mess->DefinedFrom();
249              
250             DefinedType() - returns 1 if the type attribute is defined in the
251             , 0 otherwise.
252              
253             $test = $Mess->DefinedType();
254              
255             DefinedSubject() - returns 1 if is defined in the
256             , 0 otherwise.
257              
258             $test = $Mess->DefinedSubject();
259              
260             DefinedBody() - returns 1 if is defined in the ,
261             0 otherwise.
262              
263             $test = $Mess->DefinedBody();
264              
265             DefinedThread() - returns 1 if is defined in the ,
266             0 otherwise.
267              
268             $test = $Mess->DefinedThread();
269              
270             DefinedErrorCode() - returns 1 if is defined in the
271             , 0 otherwise.
272              
273             $test = $Mess->DefinedErrorCode();
274              
275             DefinedError() - returns 1 if the code attribute is defined in the
276             , 0 otherwise.
277              
278             $test = $Mess->DefinedError();
279              
280             =head1 AUTHOR
281              
282             Ryan Eatmon
283              
284             =head1 COPYRIGHT
285              
286             This module is free software, you can redistribute it and/or modify it
287             under the LGPL.
288              
289             =cut
290              
291             require 5.003;
292 11     11   64 use strict;
  11         18  
  11         404  
293 11     11   59 use Carp;
  11         23  
  11         830  
294 11     11   61 use vars qw( %FUNCTIONS );
  11         20  
  11         504  
295 11     11   58 use Net::XMPP3::Stanza;
  11         19  
  11         265  
296 11     11   63 use base qw( Net::XMPP3::Stanza );
  11         19  
  11         9883  
297              
298             sub new
299             {
300 4     4 0 2415 my $proto = shift;
301 4   33     32 my $class = ref($proto) || $proto;
302 4         15 my $self = {};
303              
304 4         14 bless($self, $proto);
305              
306 4         31 $self->{DEBUGHEADER} = "Message";
307 4         13 $self->{TAG} = "message";
308 4         26 $self->{TIMESTAMP} = &Net::XMPP3::GetTimeStamp("local");
309              
310 4         13 $self->{FUNCS} = \%FUNCTIONS;
311              
312 4         34 $self->_init(@_);
313              
314 4         13 return $self;
315             }
316              
317 1     1   3 sub _message { my $self = shift; return new Net::XMPP3::Message(); }
  1         6  
318              
319             # Copied from Net::Jabber::Message because GetTimeStamp doesn't work without DefinedX
320 0     0 0 0 sub GetX { my $self = shift; $self->GetChild(@_); }
  0         0  
321 0     0 0 0 sub DefinedX { my $self = shift; $self->DefinedChild(@_); }
  0         0  
322 0     0 0 0 sub NewX { my $self = shift; $self->NewChild(@_); }
  0         0  
323 0     0 0 0 sub AddX { my $self = shift; $self->AddChild(@_); }
  0         0  
324 0     0 0 0 sub RemoveX { my $self = shift; $self->RemoveChild(@_); }
  0         0  
325              
326             $FUNCTIONS{Body}->{path} = 'body/text()';
327              
328             $FUNCTIONS{Error}->{path} = 'error/text()';
329              
330             $FUNCTIONS{ErrorCode}->{path} = 'error/@code';
331              
332             $FUNCTIONS{From}->{type} = 'jid';
333             $FUNCTIONS{From}->{path} = '@from';
334              
335             $FUNCTIONS{ID}->{path} = '@id';
336              
337             $FUNCTIONS{Subject}->{path} = 'subject/text()';
338              
339             $FUNCTIONS{Thread}->{path} = 'thread/text()';
340              
341             $FUNCTIONS{To}->{type} = 'jid';
342             $FUNCTIONS{To}->{path} = '@to';
343              
344             $FUNCTIONS{Type}->{path} = '@type';
345              
346             $FUNCTIONS{XMLNS}->{path} = '@xmlns';
347              
348             $FUNCTIONS{Message}->{type} = 'master';
349              
350             $FUNCTIONS{Child}->{type} = 'child';
351             $FUNCTIONS{Child}->{path} = '*[@xmlns]';
352             $FUNCTIONS{Child}->{child} = {};
353              
354             ##############################################################################
355             #
356             # GetTimeStamp - returns a string with the time stamp of when this object
357             # was created.
358             #
359             ##############################################################################
360             sub GetTimeStamp
361             {
362 0     0 0 0 my $self = shift;
363              
364 0 0       0 if ($self->DefinedX("jabber:x:delay"))
365             {
366 0         0 my @xTags = $self->GetX("jabber:x:delay");
367 0         0 my $xTag = $xTags[0];
368 0         0 $self->{TIMESTAMP} = &Net::XMPP3::GetTimeStamp("utcdelaylocal",$xTag->GetStamp());
369             }
370              
371 0         0 return $self->{TIMESTAMP};
372             }
373              
374              
375             ##############################################################################
376             #
377             # Reply - returns a Net::XMPP3::Message object with the proper fields
378             # already populated for you.
379             #
380             ##############################################################################
381             sub Reply
382             {
383 1     1 0 364 my $self = shift;
384 1         2 my %args;
385 1         7 while($#_ >= 0) { $args{ lc pop(@_) } = pop(@_); }
  0         0  
386              
387 1         6 my $reply = $self->_message();
388              
389 1 50 33     9 if (($self->GetType() eq "") || ($self->GetType() eq "normal"))
390             {
391 1         7 my $subject = $self->GetSubject();
392 1         3 $subject =~ s/re\:\s+//i;
393 1         8 $reply->SetSubject("re: $subject");
394             }
395 1 50       29 $reply->SetThread($self->GetThread()) if ($self->GetThread() ne "");
396 1 50       29 $reply->SetID($self->GetID()) if ($self->GetID() ne "");
397 1 50       24 $reply->SetType($self->GetType()) if ($self->GetType() ne "");
398 1 50       8 $reply->SetMessage((($self->GetFrom() ne "") ?
    50          
399             (to=>$self->GetFrom()) :
400             ()
401             ),
402             (($self->GetTo() ne "") ?
403             (from=>$self->GetTo()) :
404             ()
405             ),
406             );
407 1         10 $reply->SetMessage(%args);
408              
409 1         7 return $reply;
410             }
411              
412              
413             1;