File: User.pm

package info (click to toggle)
libanyevent-xmpp-perl 0.55-1
  • links: PTS, VCS
  • area: main
  • in suites: jessie, jessie-kfreebsd
  • size: 784 kB
  • ctags: 553
  • sloc: perl: 8,004; makefile: 13
file content (182 lines) | stat: -rw-r--r-- 3,411 bytes parent folder | download | duplicates (6)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
package AnyEvent::XMPP::Ext::MUC::User;
use strict;
use AnyEvent::XMPP::Namespaces qw/xmpp_ns/;
use AnyEvent::XMPP::IM::Presence;
use AnyEvent::XMPP::Ext::MUC::Message;
use AnyEvent::XMPP::Util qw/split_jid/;

our @ISA = qw/AnyEvent::XMPP::IM::Presence/;

=head1 NAME

AnyEvent::XMPP::Ext::MUC::User - User class

=head1 SYNOPSIS

=head1 DESCRIPTION

This module represents a user (occupant) handle for a MUC.
This class is derived from L<AnyEvent::XMPP::Presence> as a user has
also a presence within a room.

=head1 METHODS

=over 4

=item B<new (%args)>

=cut

sub new {
   my $this = shift;
   my $class = ref($this) || $this;
   my $self = $class->SUPER::new (@_);
   $self->init;
   $self
}

sub update {
   my ($self, $node) = @_;
   $self->SUPER::update ($node);
   my ($xuser) = $node->find_all ([qw/muc_user x/]);
   my $from = $node->attr ('from');
   my ($room, $srv, $nick) = split_jid ($from);

   my ($aff, $role, $stati, $jid, $new_nick);
   $self->{stati} ||= {};
   $stati = $self->{stati};

   delete $self->{stati}->{'303'}; # nick change

   if ($xuser) {
      if (my ($item) = $xuser->find_all ([qw/muc_user item/])) {
         $aff      = $item->attr ('affiliation');
         $role     = $item->attr ('role');
         $jid      = $item->attr ('jid');
         $new_nick = $item->attr ('nick');
      }

      for ($xuser->find_all ([qw/muc_user status/])) {
         $stati->{$_->attr ('code')}++;
      }
   }

   $self->{jid}         = $from;
   $self->{nick}        = $nick;
   $self->{affiliation} = $aff;
   $self->{real_jid}    = $jid if defined $jid && $jid ne '';
   $self->{role}        = $role;

   if ($self->is_in_nick_change) {
      $self->{old_nick} = $self->{nick};
      $self->{nick} = $new_nick;
   }
}

sub init {
   my ($self) = @_;
   $self->{connection} = $self->{room}->{muc}->{connection}
}

=item B<nick>

The nickname of the MUC user.

=cut

sub nick { $_[0]->{nick} }

=item B<affiliation>

The affiliation of the user.

=cut

sub affiliation { $_[0]->{affiliation} }

=item B<role>

The role of the user.

=cut

sub role { $_[0]->{role} }

=item B<room>

The L<AnyEvent::XMPP::Ext::MUD::Room> this user is in.

=cut

sub room { $_[0]->{room} }

=item B<in_room_jid>

The room local JID of the user.

=cut

sub in_room_jid { $_[0]->{jid} }

=item B<real_jid>

The real JID of the user, this might be undef if it is an
anonymous room.

=cut

sub real_jid { $_[0]->{real_jid} }

=item B<make_message (%args)>

Returns a L<AnyEvent::XMPP::Ext::MUC::Message> object with the to field set to
this presence full JID.

C<%args> are further arguments to the constructor of the message.

=cut

sub message_class { 'AnyEvent::XMPP::Ext::MUC::Message' }


=item B<did_create_room>

This method returns true if the user created a room.

=cut

sub did_create_room { $_[0]->{stati}->{'201'} }

sub make_message {
   my ($self, %args) = @_;
   $self->message_class ()->new (
      room       => $self->room,
      to         => $self->jid,
      %args
   );
}

sub is_in_nick_change {
   $_[0]->{stati}->{'303'}
}

sub nick_change_old_nick {
   $_[0]->{old_nick}
}

=back

=head1 AUTHOR

Robin Redeker, C<< <elmex at ta-sa.org> >>, JID: C<< <elmex at jabber.org> >>

=head1 COPYRIGHT & LICENSE

Copyright 2007, 2008 Robin Redeker, all rights reserved.

This program is free software; you can redistribute it and/or modify it
under the same terms as Perl itself.

=cut

1;