XMPP3/Presence.pm

filter: Statementstmt 98.1% 52 / 53 Branchbran 50.0% 4 / 8 Conditioncond 33.3% 1 / 3 MC/DCmcdc 0.0% 0 / 2 Subroutinesub 100% 8 / 8 Podpod 0.0% 0 / 2 totaltotal 85.5% 65 / 76 CC 6 SCAR 10.8
CC
6
Cov
89%
Reply
5.6
new
2.0
BEGIN
1.0
CRAP 2.9
« XMPP3/Namespaces.pm ↑ Summary XMPP3/IQ.pm »
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::Presence;
23  
24 =head1 NAME
25  
26 Net::XMPP3::Presence - XMPP Presence Module
27  
28 =head1 SYNOPSIS
29  
30 Net::XMPP3::Presence 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 Presence.
33  
34 =head1 DESCRIPTION
35  
36 A Net::XMPP3::Presence 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::Streams. There are some cases where you
39 might want this information, 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 presence {
45 my ($sid,$Pres) = @_;
46 .
47 .
48 .
49 }
50  
51 You now have access to all of the retrieval functions available.
52  
53 To create a new presence to send to the server:
54  
55 use Net::XMPP3;
56  
57 $Pres = new Net::XMPP3::Presence();
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") <presence/>. 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 = $Pres->GetTo();
72 $toJID = $Pres->GetTo("jid");
73  
74 GetFrom() - returns the value in the from='' attribute for the
75 GetFrom("jid") <presence/>. 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 = $Pres->GetFrom();
80 $fromJID = $Pres->GetFrom("jid");
81  
82 GetType() - returns the type='' attribute of the <presence/>. Each
83 presence is one of seven types:
84  
85 available available to receive messages; default
86 unavailable unavailable to receive anything
87 subscribe ask the recipient to subscribe you
88 subscribed tell the sender they are subscribed
89 unsubscribe ask the recipient to unsubscribe you
90 unsubscribed tell the sender they are unsubscribed
91 probe probe
92  
93 $type = $Pres->GetType();
94  
95 GetStatus() - returns a string with the current status of the resource.
96  
97 $status = $Pres->GetStatus();
98  
99 GetPriority() - returns an integer with the priority of the resource
100 The default is 0 if there is no priority in this
101 presence.
102  
103 $priority = $Pres->GetPriority();
104  
105 GetShow() - returns a string with the state the client should show.
106  
107 $show = $Pres->GetShow();
108  
109 =head2 Creation functions
110  
111 SetPresence(to=>string|JID - set multiple fields in the <presence/>
112 from=>string|JID, at one time. This is a cumulative
113 type=>string, and over writing action. If you set
114 status=>string, the "to" attribute twice, the second
115 priority=>integer, setting is what is used. If you set
116 meta=>string, the status, and then set the priority
117 icon=>string, then both will be in the <presence/>
118 show=>string, tag. For valid settings read the
119 loc=>string) specific Set functions below.
120  
121 $Pres->SetPresence(TYPE=>"away",
122 StatuS=>"Out for lunch");
123  
124 SetTo(string) - sets the to attribute. You can either pass a string
125 SetTo(JID) or a JID object. They must be valid JIDs or the
126 server will return an error message.
127 (ie. bob@jabber.org/Silent Bob, etc...)
128  
129 $Pres->SetTo("bob\@jabber.org");
130  
131 SetFrom(string) - sets the from='' attribute. You can either pass
132 SetFrom(JID) a string or a JID object. They must be valid JIDs
133 or the server will return an error message. (ie.
134 jabber:bob@jabber.org/Work) This field is not
135 required if you are writing a Client since the
136 server will put the JID of your connection in there
137 to prevent spamming.
138  
139 $Pres->SetFrom("jojo\@jabber.org");
140  
141 SetType(string) - sets the type attribute. Valid settings are:
142  
143 available available to receive messages; default
144 unavailable unavailable to receive anything
145 subscribe ask the recipient to subscribe you
146 subscribed tell the sender they are subscribed
147 unsubscribe ask the recipient to unsubscribe you
148 unsubscribed tell the sender they are unsubscribed
149 probe probe
150  
151 $Pres->SetType("unavailable");
152  
153 SetStatus(string) - sets the status tag to be whatever string the user
154 wants associated with that resource.
155  
156 $Pres->SetStatus("Taking a nap");
157  
158 SetPriority(integer) - sets the priority of this resource. The highest
159 resource attached to the xmpp account is the
160 one that receives the messages.
161  
162 $Pres->SetPriority(10);
163  
164 SetShow(string) - sets the name of the icon or string to display for
165 this resource.
166  
167 $Pres->SetShow("away");
168  
169 Reply(hash) - creates a new Presence object and populates the to/from
170 fields. If you specify a hash the same as with
171 SetPresence then those values will override the Reply
172 values.
173  
174 $Reply = $Pres->Reply();
175 $Reply = $Pres->Reply(type=>"subscribed");
176  
177 =head2 Removal functions
178  
179 RemoveTo() - removes the to attribute from the <presence/>.
180  
181 $Pres->RemoveTo();
182  
183 RemoveFrom() - removes the from attribute from the <presence/>.
184  
185 $Pres->RemoveFrom();
186  
187 RemoveType() - removes the type attribute from the <presence/>.
188  
189 $Pres->RemoveType();
190  
191 RemoveStatus() - removes the <status/> element from the <presence/>.
192  
193 $Pres->RemoveStatus();
194  
195 RemovePriority() - removes the <priority/> element from the
196 <presence/>.
197  
198 $Pres->RemovePriority();
199  
200 RemoveShow() - removes the <show/> element from the <presence/>.
201  
202 $Pres->RemoveShow();
203  
204 =head2 Test functions
205  
206 DefinedTo() - returns 1 if the to attribute is defined in the
207 <presence/>, 0 otherwise.
208  
209 $test = $Pres->DefinedTo();
210  
211 DefinedFrom() - returns 1 if the from attribute is defined in the
212 <presence/>, 0 otherwise.
213  
214 $test = $Pres->DefinedFrom();
215  
216 DefinedType() - returns 1 if the type attribute is defined in the
217 <presence/>, 0 otherwise.
218  
219 $test = $Pres->DefinedType();
220  
221 DefinedStatus() - returns 1 if <status/> is defined in the
222 <presence/>, 0 otherwise.
223  
224 $test = $Pres->DefinedStatus();
225  
226 DefinedPriority() - returns 1 if <priority/> is defined in the
227 <presence/>, 0 otherwise.
228  
229 $test = $Pres->DefinedPriority();
230  
231 DefinedShow() - returns 1 if <show/> is defined in the <presence/>,
232 0 otherwise.
233  
234 $test = $Pres->DefinedShow();
235  
236 =head1 AUTHOR
237  
238 Ryan Eatmon
239  
240 =head1 COPYRIGHT
241  
242 This module is free software, you can redistribute it and/or modify it
243 under the LGPL.
244  
245 =cut
246  
247 11 require 5.003;
248 11 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
249 11 ▶use Carp;
Subroutine BEGIN: called CC=1 SCAR=0.0
250 11 ▶use vars qw( %FUNCTIONS );
Subroutine BEGIN: called CC=1 SCAR=0.0
251 11 ▶use Net::XMPP3::Stanza;
Subroutine BEGIN: called CC=1 SCAR=0.0
252 11 ▶use base qw( Net::XMPP3::Stanza );
Subroutine BEGIN: called CC=1 SCAR=0.0
253  
254 sub new
255 3 ▶{
Subroutine new: called CC=2 SCAR=7.0
Pod undocumented
256 3 my $proto = shift;
257 3 ▶ my $class = ref($proto) || $proto;
Condition33%1/3
ref $proto || $proto
l!l&&r!l&&!r
030
MC/DC0%0/2
ref $proto || $proto
ref $proto $proto
Truth table33%1/3
A || B
A= ref $protoB= $proto
ABresult
1X1
011
000
258 3 my $self = { };
259  
260 3 bless($self, $proto);
261  
262 3 $self->{DEBUGHEADER} = "Presence";
263 3 $self->{TAG} = "presence";
264  
265 3 $self->{FUNCS} = \%FUNCTIONS;
266  
267 3 $self->_init(@_);
268  
269 3 return $self;
270 }
271  
272 1 ▶sub _presence { my $self = shift; return new Net::XMPP3::Presence(); }
Subroutine _presence: called CC=1 SCAR=0.0
273  
274  
275 11 $FUNCTIONS{Error}->{path} = 'error/text()';
276  
277 11 $FUNCTIONS{ErrorCode}->{path} = 'error/@code';
278  
279 11 $FUNCTIONS{From}->{type} = 'jid';
280 11 $FUNCTIONS{From}->{path} = '@from';
281  
282 11 $FUNCTIONS{ID}->{path} = '@id';
283  
284 11 $FUNCTIONS{Priority}->{path} = 'priority/text()';
285  
286 11 $FUNCTIONS{Show}->{path} = 'show/text()';
287  
288 11 $FUNCTIONS{Status}->{path} = 'status/text()';
289  
290 11 $FUNCTIONS{To}->{type} = 'jid';
291 11 $FUNCTIONS{To}->{path} = '@to';
292  
293 11 $FUNCTIONS{Type}->{path} = '@type';
294  
295 11 $FUNCTIONS{XMLNS}->{path} = '@xmlns';
296  
297 11 $FUNCTIONS{Presence}->{type} = 'master';
298  
299 11 $FUNCTIONS{Child}->{type} = 'child';
300 11 $FUNCTIONS{Child}->{path} = '*[@xmlns]';
301 11 $FUNCTIONS{Child}->{child} = {};
302  
303 ##############################################################################
304 #
305 # Reply - returns a Net::XMPP3::Presence object with the proper fields
306 # already populated for you.
307 #
308 ##############################################################################
309 sub Reply
310 1 ▶{
Subroutine Reply: called CC=5 SCAR=17.3
Pod undocumented
311 1 my $self = shift;
312 1 my %args;
313 0 ▶ while($#_ >= 0) { $args{ lc pop(@_) } = pop(@_); }
Branch: 1 branch
BranchTrueFalseTotal
if ($#_ >= 0) 0 1 1
314  
315 1 my $reply = $self->_presence();
316  
317 1 ▶ $reply->SetID($self->GetID()) if ($self->GetID() ne "");
Branch: 1 branch
BranchTrueFalseTotal
if $self->GetID ne "" 1 0 1
318  
319 1 ▶ $reply->SetPresence((($self->GetFrom() ne "") ?
Branch: 2 branches
BranchTrueFalseTotal
$self->GetFrom ne '' ? : 1 0 1
$self->GetTo ne '' ? : 1 0 1
320 (to=>$self->GetFrom()) :
321 ()
322 ),
323 (($self->GetTo() ne "") ?
324 (from=>$self->GetTo()) :
325 ()
326 ),
327 );
328  
329 1 $reply->SetPresence(%args);
330  
331 1 return $reply;
332 }
333  
334  
335 11 1;
« XMPP3/Namespaces.pm ↑ Summary XMPP3/IQ.pm »