XMPP/IM/Account.pm

filter: Statementstmt 66.6% 4 / 6 Branchbran n/a Conditioncond n/a MC/DCmcdc n/a Subroutinesub 100% 2 / 2 Podpod n/a totaltotal 75.0% 6 / 8 CC 1 SCAR 1.4
CC
1
Cov
67%
BEGIN
1.3
BEGIN
1.0
CRAP 1.1
« XMPP/Ext/Registration.pm ↑ Summary XMPP/IM/Delayed.pm »
1 package AnyEvent::XMPP::IM::Account;
2 1 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
3 0 ▶use AnyEvent::XMPP::Util qw/stringprep_jid prep_bare_jid split_jid cmp_jid node_jid/;
Subroutine BEGIN: called CC=1 SCAR=2.6
4 use AnyEvent::XMPP::IM::Connection;
5  
6 use base Object::Event::;
7  
8 =head1 NAME
9  
10 AnyEvent::XMPP::IM::Account - Instant messaging account
11  
12 =head1 SYNOPSIS
13  
14 my $cl = AnyEvent::XMPP::IM::Client->new;
15 ...
16 my $acc = $cl->get_account ($jid);
17  
18 =head1 DESCRIPTION
19  
20 This module represents a class for IM accounts. It is used
21 by L<AnyEvent::XMPP::Client>.
22  
23 You can get an instance of this class only by calling the C<get_account>
24 method on a L<AnyEvent::XMPP::Client> object.
25  
26 =cut
27  
28 sub new {
29 my $this = shift;
30 my $class = ref($this) || $this;
31 my $self = bless { @_ }, $class;
32 $self
33 }
34  
35 sub remove_connection {
36 my ($self) = @_;
37 delete $self->{con}
38 }
39  
40 sub spawn_connection {
41 my ($self, %args) = @_;
42  
43 $self->{con} = AnyEvent::XMPP::IM::Connection->new (
44 jid => $self->jid,
45 password => $self->{password},
46 (defined $self->{host} ? (host => $self->{host}) : ()),
47 (defined $self->{port} ? (port => $self->{port}) : ()),
48 %args,
49 %{$self->{args} || {}},
50 );
51  
52 $self->{con}->reg_cb (
53 ext_before_session_ready => sub {
54 my ($con) = @_;
55 $self->{track} = {};
56 },
57 ext_before_message => sub {
58 my ($con, $msg) = @_;
59 my $t = $self->{track};
60 my $pfrom = prep_bare_jid $msg->from;
61  
62 if (not (exists $t->{$pfrom}) || !cmp_jid ($t->{$pfrom}, $msg->from)) {
63 $t->{$pfrom} = $msg->from;
64 $self->event (tracked_message_destination => $pfrom, $msg->from);
65 }
66 }
67 );
68  
69 $self->{con}
70 }
71  
72 =head1 METHODS
73  
74 =over 4
75  
76 =item B<connection ()>
77  
78 Returns the L<AnyEvent::XMPP::IM::Connection> object if this account already
79 has one (undef otherwise).
80  
81 =cut
82  
83 sub connection { $_[0]->{con} }
84  
85 =item B<is_connected ()>
86  
87 Returns true if this accunt is connected.
88  
89 =cut
90  
91 sub is_connected {
92 my ($self) = @_;
93 $self->{con} && $self->{con}->is_connected
94 }
95  
96 =item B<jid ()>
97  
98 Returns either the full JID if the account is
99 connected or returns the bare jid if not.
100  
101 =cut
102  
103 sub jid {
104 my ($self) = @_;
105 if ($self->is_connected) {
106 return $self->{con}->jid;
107 }
108 $_[0]->{jid}
109 }
110  
111 =item B<bare_jid ()>
112  
113 Returns always the bare JID of this account after stringprep has been applied,
114 so you can compare the JIDs returned from this function.
115  
116 =cut
117  
118 sub bare_jid {
119 my ($self) = @_;
120 prep_bare_jid $self->jid
121 }
122  
123 =item B<nickname ()>
124  
125 Your nickname for this account.
126  
127 =cut
128  
129 sub nickname {
130 my ($self) = @_;
131 # FIXME: fetch real nickname from server somehow? Does that exist?
132 # eg. from the roster?
133 my ($user, $host, $res) = split_jid ($self->bare_jid);
134 $user
135 }
136  
137 =item B<nickname_for_jid ($jid)>
138  
139 This method transforms the C<$jid> to a nickname. It looks the C<$jid>
140 up in the roster and looks for a nickname. If no nickname could be found
141 in the roster it returns the node part for the C<$jid>.
142  
143 =cut
144  
145 sub nickname_for_jid {
146 my ($self, $jid) = @_;
147  
148 if ($self->is_connected) {
149 my $c = $self->connection->get_roster->get_contact ($jid);
150 return $c ? $c->nickname : node_jid ($jid);
151 } else {
152 return node_jid ($jid);
153 }
154 }
155  
156 =item B<send_tracked_message ($msg)>
157  
158 This method sends the L<AnyEvent::XMPP::IM::Message> object in C<$msg>.
159 The C<to> attribute of the message is adjusted by the conversation tracking
160 mechanism.
161  
162 =cut
163  
164 sub send_tracked_message {
165 my ($self, $msg) = @_;
166  
167 my $bjid = prep_bare_jid $msg->to;
168 $msg->to ($self->{track}->{$bjid} || $bjid);
169 $msg->send ($self->connection)
170 }
171  
172 =back
173  
174 =head1 EVENTS
175  
176 For these events callbacks can be registered (with the L<Object::Event> interface):
177  
178 =over 4
179  
180 =item tracked_message_destination => $bare_jid, $full_jid
181  
182 This event is emitted whenever the message tracking mechanism changes (or sets)
183 it's destination resource for the C<$bare_jid> to C<$full_jid>.
184  
185 =item removed
186  
187 Whenever the account is removed from the L<AnyEvent::XMPP::Client>
188 (eg. when disconnected) this event is emitted before it is destroyed.
189  
190 =back
191  
192 =head1 AUTHOR
193  
194 Robin Redeker, C<< <elmex at ta-sa.org> >>, JID: C<< <elmex at jabber.org> >>
195  
196 =head1 COPYRIGHT & LICENSE
197  
198 Copyright 2007, 2008 Robin Redeker, all rights reserved.
199  
200 This program is free software; you can redistribute it and/or modify it
201 under the same terms as Perl itself.
202  
203 =cut
204  
205  
206 1;
« XMPP/Ext/Registration.pm ↑ Summary XMPP/IM/Delayed.pm »