File Coverage

blib/lib/AnyEvent/XMPP/IM/Account.pm
Criterion Covered Total %
statement 4 6 66.6
branch n/a
condition n/a
subroutine 2 2 100.0
pod n/a
total 6 8 75.0


line stmt bran cond sub pod time code
1             package AnyEvent::XMPP::IM::Account;
2 1     1   2322 use strict;
  1         27  
  1         57  
3 1     1   64 use AnyEvent::XMPP::Util qw/stringprep_jid prep_bare_jid split_jid cmp_jid node_jid/;
  0            
  0            
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.
22              
23             You can get an instance of this class only by calling the C
24             method on a L 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
77              
78             Returns the L object if this account already
79             has one (undef otherwise).
80              
81             =cut
82              
83             sub connection { $_[0]->{con} }
84              
85             =item B
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
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
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
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
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
157              
158             This method sends the L object in C<$msg>.
159             The C 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 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
188             (eg. when disconnected) this event is emitted before it is destroyed.
189              
190             =back
191              
192             =head1 AUTHOR
193              
194             Robin Redeker, C<< >>, JID: C<< >>
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;