XMPP/Ext/MUC/User.pm untested

filter: Statementstmt 0.0% 0 / 67 Branchbran 0.0% 0 / 8 Conditioncond 0.0% 0 / 8 MC/DCmcdc 0.0% 0 / 5 Subroutinesub 0.0% 0 / 19 Podpod 0.0% 0 / 14 totaltotal 0.0% 0 / 121 CC 5 SCAR 20.9
CC
5
Cov
0%
update
30.0
new
2.0
init
2.0
CRAP 8.1
1 package AnyEvent::XMPP::Ext::MUC::User;
2 use strict;
3 use AnyEvent::XMPP::Namespaces qw/xmpp_ns/;
4 use AnyEvent::XMPP::IM::Presence;
5 use AnyEvent::XMPP::Ext::MUC::Message;
6 use AnyEvent::XMPP::Util qw/split_jid/;
7  
8 our @ISA = qw/AnyEvent::XMPP::IM::Presence/;
9  
10 =head1 NAME
11  
12 AnyEvent::XMPP::Ext::MUC::User - User class
13  
14 =head1 SYNOPSIS
15  
16 =head1 DESCRIPTION
17  
18 This module represents a user (occupant) handle for a MUC.
19 This class is derived from L<AnyEvent::XMPP::Presence> as a user has
20 also a presence within a room.
21  
22 =head1 METHODS
23  
24 =over 4
25  
26 =item B<new (%args)>
27  
28 =cut
29  
30 sub new {
31 my $this = shift;
32 my $class = ref($this) || $this;
33 my $self = $class->SUPER::new (@_);
34 $self->init;
35 $self
36 }
37  
38 sub update {
39 my ($self, $node) = @_;
40 $self->SUPER::update ($node);
41 my ($xuser) = $node->find_all ([qw/muc_user x/]);
42 my $from = $node->attr ('from');
43 my ($room, $srv, $nick) = split_jid ($from);
44  
45 my ($aff, $role, $stati, $jid, $new_nick);
46 $self->{stati} ||= {};
47 $stati = $self->{stati};
48  
49 delete $self->{stati}->{'303'}; # nick change
50  
51 if ($xuser) {
52 if (my ($item) = $xuser->find_all ([qw/muc_user item/])) {
53 $aff = $item->attr ('affiliation');
54 $role = $item->attr ('role');
55 $jid = $item->attr ('jid');
56 $new_nick = $item->attr ('nick');
57 }
58  
59 for ($xuser->find_all ([qw/muc_user status/])) {
60 $stati->{$_->attr ('code')}++;
61 }
62 }
63  
64 $self->{jid} = $from;
65 $self->{nick} = $nick;
66 $self->{affiliation} = $aff;
67 $self->{real_jid} = $jid if defined $jid && $jid ne '';
68 $self->{role} = $role;
69  
70 if ($self->is_in_nick_change) {
71 $self->{old_nick} = $self->{nick};
72 $self->{nick} = $new_nick;
73 }
74 }
75  
76 sub init {
77 my ($self) = @_;
78 $self->{connection} = $self->{room}->{muc}->{connection}
79 }
80  
81 =item B<nick>
82  
83 The nickname of the MUC user.
84  
85 =cut
86  
87 sub nick { $_[0]->{nick} }
88  
89 =item B<affiliation>
90  
91 The affiliation of the user.
92  
93 =cut
94  
95 sub affiliation { $_[0]->{affiliation} }
96  
97 =item B<role>
98  
99 The role of the user.
100  
101 =cut
102  
103 sub role { $_[0]->{role} }
104  
105 =item B<room>
106  
107 The L<AnyEvent::XMPP::Ext::MUD::Room> this user is in.
108  
109 =cut
110  
111 sub room { $_[0]->{room} }
112  
113 =item B<in_room_jid>
114  
115 The room local JID of the user.
116  
117 =cut
118  
119 sub in_room_jid { $_[0]->{jid} }
120  
121 =item B<real_jid>
122  
123 The real JID of the user, this might be undef if it is an
124 anonymous room.
125  
126 =cut
127  
128 sub real_jid { $_[0]->{real_jid} }
129  
130 =item B<make_message (%args)>
131  
132 Returns a L<AnyEvent::XMPP::Ext::MUC::Message> object with the to field set to
133 this presence full JID.
134  
135 C<%args> are further arguments to the constructor of the message.
136  
137 =cut
138  
139 sub message_class { 'AnyEvent::XMPP::Ext::MUC::Message' }
140  
141  
142 =item B<did_create_room>
143  
144 This method returns true if the user created a room.
145  
146 =cut
147  
148 sub did_create_room { $_[0]->{stati}->{'201'} }
149  
150 sub make_message {
151 my ($self, %args) = @_;
152 $self->message_class ()->new (
153 room => $self->room,
154 to => $self->jid,
155 %args
156 );
157 }
158  
159 sub is_in_nick_change {
160 $_[0]->{stati}->{'303'}
161 }
162  
163 sub nick_change_old_nick {
164 $_[0]->{old_nick}
165 }
166  
167 =back
168  
169 =head1 AUTHOR
170  
171 Robin Redeker, C<< <elmex at ta-sa.org> >>, JID: C<< <elmex at jabber.org> >>
172  
173 =head1 COPYRIGHT & LICENSE
174  
175 Copyright 2007, 2008 Robin Redeker, all rights reserved.
176  
177 This program is free software; you can redistribute it and/or modify it
178 under the same terms as Perl itself.
179  
180 =cut
181  
182 1;