XMPP/Ext/MUC/Room.pm untested

filter: Statementstmt 0.0% 0 / 235 Branchbran 0.0% 0 / 76 Conditioncond 0.0% 0 / 36 MC/DCmcdc 0.0% 0 / 21 Subroutinesub 0.0% 0 / 36 Podpod 0.0% 0 / 28 totaltotal 0.0% 0 / 432 CC 39 SCAR 46.8
CC
39
Cov
0%
handle_presence
380.0
send_join
56.0
send_part
30.0
CRAP 108.0
1 package AnyEvent::XMPP::Ext::MUC::Room;
2 use strict;
3 no warnings;
4 use AnyEvent::XMPP::Namespaces qw/xmpp_ns/;
5 use AnyEvent::XMPP::Util qw/
6 bare_jid prep_bare_jid cmp_jid split_jid join_jid is_bare_jid
7 prep_res_jid prep_join_jid resourceprep
8 /;
9 use AnyEvent::XMPP::Ext::MUC::User;
10 use AnyEvent::XMPP::Ext::DataForm;
11 use AnyEvent::XMPP::Error::MUC;
12  
13 use constant {
14 JOIN_SENT => 1,
15 JOINED => 2,
16 LEFT => 3,
17 };
18  
19 =head1 NAME
20  
21 AnyEvent::XMPP::Ext::MUC::Room - Room class
22  
23 =head1 SYNOPSIS
24  
25 =head1 DESCRIPTION
26  
27 This module represents a room handle for a MUC.
28  
29 =head1 METHODS
30  
31 =over 4
32  
33 =item B<new (%args)>
34  
35 =cut
36  
37 sub new {
38 my $this = shift;
39 my $class = ref($this) || $this;
40 my $self = bless { status => LEFT, @_ }, $class;
41 $self->init;
42 $self
43 }
44  
45 sub init {
46 my ($self) = @_;
47 $self->{jid} = bare_jid ($self->{jid});
48 }
49  
50 sub event {
51 my ($self, $ev, @args) = @_;
52 $self->{muc}->event ($ev, $self, @args);
53 }
54  
55 sub handle_message {
56 my ($self, $node) = @_;
57  
58 my $msg = AnyEvent::XMPP::Ext::MUC::Message->new (room => $self);
59  
60 if ($node->attr ('type') eq 'error') {
61 my $error =
62 AnyEvent::XMPP::Error::MUC->new (
63 message_node => $node,
64 message => $msg
65 );
66  
67 if ($error->type eq 'subject_change_forbidden') {
68 $self->event (subject_change_error => $error);
69 } else {
70 $self->event (message_error => $error);
71 }
72  
73 } else {
74 $msg->from_node ($node);
75 my $is_echo = cmp_jid ($msg->from, $self->nick_jid)
76 && $msg->type eq 'groupchat';
77  
78 if (not (defined $msg->any_body)
79 && defined $msg->any_subject) { # subject change
80 $self->event (subject_change => $msg, $is_echo);
81 return;
82 }
83  
84 $self->event (message => $msg, $is_echo);
85 }
86 }
87  
88 sub handle_presence {
89 my ($self, $node) = @_;
90  
91 my $s = $self->{status};
92  
93 my $from = $node->attr ('from');
94 my $type = $node->attr ('type');
95  
96 my $error;
97 if ($type eq 'error') {
98 $error = AnyEvent::XMPP::Error::Presence->new (node => $node);
99 }
100  
101 my $stati = {};
102 my $new_nick;
103  
104 if (my ($x) = $node->find_all ([qw/muc_user x/])) {
105 for ($x->find_all ([qw/muc_user status/])) {
106 $stati->{$_->attr ('code')}++;
107 }
108  
109 if (my ($i) = $x->find_all ([qw/muc_user item/])) {
110 $new_nick = $i->attr ('nick');
111 }
112 }
113  
114 my $nick_change = $stati->{'303'};
115  
116 if ($s == JOIN_SENT) {
117 if ($error) {
118 my $muce = AnyEvent::XMPP::Error::MUC->new (
119 presence_error => $error,
120 type => 'presence_error'
121 );
122 $self->event (join_error => $muce);
123  
124 } else {
125 if (cmp_jid ($from, $self->nick_jid)) {
126 my $user = $self->add_user_xml ($node);
127 $self->{status} = JOINED;
128 $self->{me} = $user;
129  
130 if ($user->did_create_room) {
131 if ($self->{locked_cb}) {
132 (delete $self->{locked_cb})->($self);
133  
134 } else {
135 $self->event ('locked');
136 }
137 } else {
138 $self->event (enter => $user);
139 }
140  
141 } else {
142 $self->add_user_xml ($node);
143 }
144 }
145  
146 } elsif ($s == JOINED) { # nick changes?
147 if ($error) {
148 my $muce = AnyEvent::XMPP::Error::MUC->new (
149 presence_error => $error,
150 type => 'presence_error'
151 );
152 $self->event (error => $muce);
153  
154 } elsif (!$nick_change && $type eq 'unavailable') {
155 if (cmp_jid ($from, $self->nick_jid)) {
156 $self->event ('leave', $self->get_me);
157 $self->we_left_room ();
158  
159 } else {
160 my $nick = prep_res_jid ($from);
161  
162 my $user = delete $self->{users}->{$nick};
163 if ($user) {
164 $user->update ($node);
165 $self->event (part => $user);
166 } else {
167 warn "User with '$nick' not found in room $self->{jid}!\n";
168 }
169 }
170  
171 } elsif ($nick_change && $type eq 'unavailable') {
172 my $nick = prep_res_jid ($from);
173 my $nnick = resourceprep ($new_nick);
174  
175 my $user = $self->{users}->{$nnick} = delete $self->{users}->{$nick};
176 if ($user) {
177 $user->update ($node);
178 $self->event (nick_change_leave => $user, $nick, $new_nick);
179 } else {
180 warn "User with '$nick' not found in room $self->{jid} for nickchange!\n";
181 }
182  
183 } else {
184 my $nick = prep_res_jid $from;
185 my $pre = $self->{users}->{$nick};
186 my $in_nick_change = $pre ? $pre->is_in_nick_change : undef;
187 my $user = $self->add_user_xml ($node);
188  
189 if ($pre) {
190 if ($in_nick_change) {
191 $self->event (nick_change => $user, $user->{old_nick}, $user->nick);
192 } else {
193 $self->event (presence => $user);
194 }
195 } else {
196 $self->event (join => $user);
197 }
198 }
199 }
200 }
201  
202 sub we_left_room {
203 my ($self) = @_;
204 $self->{users} = {};
205 $self->{status} = LEFT;
206 delete $self->{me};
207 }
208  
209 =item B<get_user ($nick)>
210  
211 This method returns the user with the C<$nick> in the room.
212  
213 =cut
214  
215 sub get_user {
216 my ($self, $nick) = @_;
217 $self->{users}->{$nick}
218 }
219  
220 =item B<get_me>
221  
222 This method returns the L<AnyEvent::XMPP::Ext::MUC::User> object of yourself
223 in the room. If will return undef if we are not in the room anymore.
224  
225 =cut
226  
227 sub get_me {
228 my ($self) = @_;
229 $self->{me}
230 }
231  
232 =item B<get_user_jid ($jid)>
233  
234 This method looks whether a user with the JID C<$jid> exists
235 in the room. That means whether the node and domain part of the
236 JID match the rooms node and domain part, and the resource part of the
237 JID matches a joined nick.
238  
239 =cut
240  
241 sub get_user_jid {
242 my ($self, $jid) = @_;
243 my ($room, $srv, $nick) = split_jid ($jid);
244 return unless prep_join_jid ($room, $srv) eq prep_bare_jid $self->jid;
245 $self->{users}->{$nick}
246 }
247  
248 =item B<get_users>
249  
250 This method returns the list of occupants as L<AnyEvent::XMPP::Ext::MUC::User> objects.
251  
252 =cut
253  
254 sub get_users {
255 my ($self) = @_;
256 values %{$self->{users}};
257 }
258  
259 sub add_user_xml {
260 my ($self, $node) = @_;
261 my $from = $node->attr ('from');
262 my $nick = prep_res_jid ($from);
263  
264 my $user = $self->{users}->{$nick};
265 unless ($user) {
266 $user = $self->{users}->{$nick} =
267 AnyEvent::XMPP::Ext::MUC::User->new (room => $self);
268 }
269  
270 $user->update ($node);
271  
272 $user
273 }
274  
275 sub _join_jid_nick {
276 my ($jid, $nick) = @_;
277 my ($node, $host) = split_jid $jid;
278 join_jid ($node, $host, $nick);
279 }
280  
281 sub check_online {
282 my ($self) = @_;
283 unless ($self->is_connected) {
284 warn "room $self not connected anymore!";
285 return 0;
286 }
287 1
288 }
289  
290 sub send_join {
291 my ($self, $nick, $password, $history) = @_;
292 $self->check_online or return;
293  
294 $self->{nick_jid} = _join_jid_nick ($self->{jid}, $nick);
295 $self->{status} = JOIN_SENT;
296  
297 my @chlds;
298 if (defined $password) {
299 push @chlds, { name => 'password', childs => [ $password ] };
300 }
301  
302 if (defined $history) {
303 my $h;
304 push @{$h->{attrs}}, ('maxchars', $history->{chars})
305 if defined $history->{chars};
306 push @{$h->{attrs}}, ('maxstanzas', $history->{stanzas})
307 if defined $history->{stanzas};
308 push @{$h->{attrs}}, ('seconds', $history->{seconds})
309 if defined $history->{seconds};
310  
311 if (defined $h->{attrs}) {
312 $h->{name} = 'history';
313 push @chlds, $h;
314 }
315 }
316  
317 my $con = $self->{connection};
318 $con->send_presence (undef, {
319 defns => 'muc', node => { ns => 'muc', name => 'x', childs => [ @chlds ] }
320 }, to => $self->{nick_jid});
321 }
322  
323 =item B<make_instant ($cb)>
324  
325 If you just created a room you can create an instant room with this
326 method instead of going through room configuration for a reserved room.
327  
328 If you want to create a reserved room instead don't forget to unset the
329 C<create_instant> argument of the C<join_room> method of L<AnyEvent::XMPP::Ext::MUC>!
330  
331 See also the C<request_configuration> method below for the reserved room config.
332  
333 C<$cb> is the callback that will be called when the instant room creation is
334 finished. If successful the first argument will be this room object
335 (C<$self>), if unsuccessful the first argument will be undef and the second
336 will be a L<AnyEvent::XMPP::Error::IQ> object.
337  
338 =cut
339  
340 sub make_instant {
341 my ($self, $cb) = @_;
342 $self->check_online or return;
343  
344 my $df = AnyEvent::XMPP::Ext::DataForm->new;
345 $df->set_form_type ('submit');
346 my $sxl = $df->to_simxml;
347  
348 $self->{connection}->send_iq (
349 set => {
350 defns => 'muc_owner', node => {
351 name => 'query', childs => [ $sxl ]
352 }
353 }, sub {
354 my ($n, $e) = @_;
355  
356 if ($e) {
357 $cb->(undef, $e);
358 } else {
359 $cb->($self, undef);
360 }
361 },
362 to => $self->jid
363 );
364 }
365  
366 =item B<request_configuration ($cb)>
367  
368 This method requests the room configuration. When the configuration
369 form or an error arrives C<$cb> will be called.
370 The first argument to the callback will be a L<AnyEvent::XMPP::Ext::DataForm>
371 with the room configuration form or undef in case of an error.
372 The second argument will be a L<AnyEvent::XMPP::Error::MUC> error object if an
373 error occurred or undef if no error occurred.
374  
375 If you made an answer form you can send it via the C<send_configuration>
376 method below.
377  
378 Here is an example:
379  
380 $room->request_configuration (sub {
381 my ($form, $err) = @_;
382 $form or return;
383  
384 my $af = AnyEvent::XMPP::Ext::DataForm->new;
385 $af->make_answer_form ($form);
386 $af->set_field_value ('muc#roomconfig_maxusers', 20);
387 $af->clear_empty_fields;
388  
389 $roomhdl->send_configuration ($af, sub {
390 # ...
391 });
392 });
393  
394 =cut
395  
396 sub request_configuration {
397 my ($self, $cb) = @_;
398 $self->check_online or return;
399  
400 $self->{connection}->send_iq (
401 get => {
402 defns => 'muc_owner', node => { name => 'query' }
403 }, sub {
404 my ($n, $e) = @_;
405 if ($n) {
406 if (my ($x) = $n->find_all ([qw/muc_owner query/], [qw/data_form x/])) {
407 my $form = AnyEvent::XMPP::Ext::DataForm->new;
408 $form->from_node ($x);
409 $cb->($form, undef);
410 } else {
411 $e = AnyEvent::XMPP::Error::MUC->new (
412 type => 'no_config_form',
413 text => "The room didn't provide a configuration form"
414 );
415 $cb->(undef, $e);
416 }
417 } else {
418 $cb->(undef, $e);
419 }
420 },
421 to => $self->jid
422 );
423 }
424  
425 =item C<send_configuration ($answer_form, $cb)>
426  
427 This method sends the answer form to a configuration request to the room.
428 C<$answer_form> should be a L<AnyEvent::XMPP::Ext::DataForm> object containig the
429 answer form with the changed configuration.
430  
431 The first argument of C<$cb> will be a true value if the configuration change
432 was successful. The second argument of C<$cb> will be a C<AnyEvent::XMPP::Error::IQ>
433 object if the configuration change was not successful.
434  
435 =cut
436  
437 sub send_configuration {
438 my ($self, $form, $cb) = @_;
439 $self->check_online or return;
440  
441 $self->{connection}->send_iq (
442 set => {
443 defns => 'muc_owner', node => { name => 'query', childs => [
444 $form->to_simxml
445 ]}
446 }, sub {
447 my ($n, $e) = @_;
448 if ($e) {
449 $cb->(undef, $e);
450 } else {
451 $cb->(1, undef);
452 }
453 },
454 to => $self->jid
455 );
456 }
457  
458 sub message_class { 'AnyEvent::XMPP::Ext::MUC::Message' }
459  
460 =item B<make_message (%args)>
461  
462 This method constructs a L<AnyEvent::XMPP::Ext::MUC::Message> with
463 a connection to this room.
464  
465 C<%args> are further arguments for the constructor of L<AnyEvent::XMPP::Ext::MUC::Message>.
466 The default C<to> argument for the message is the room and the
467 C<type> will be 'groupchat'.
468  
469 =cut
470  
471 sub make_message {
472 my ($self, %args) = @_;
473  
474 $self->message_class ()->new (
475 room => $self,
476 to => $self->jid,
477 type => 'groupchat',
478 %args
479 )
480 }
481  
482 =item B<send_part ($msg, $cb, $timeout)>
483  
484 This lets you part the room, C<$msg> is an optional part message
485 and can be undef if no custom message should be generated.
486  
487 C<$cb> is called when we successfully left the room or after
488 C<$timeout> seconds. The default for C<$timeout> is 60.
489  
490 The first argument to the call of C<$cb> will be undef if
491 we successfully parted, or a true value when the timeout hit.
492 Even if we timeout we consider ourself parted (and a 'leave' event
493 is generated).
494  
495 =cut
496  
497 sub send_part {
498 my ($self, $msg, $cb, $timeout) = @_;
499 $self->check_online or return;
500 $timeout ||= 60;
501  
502 my $con = $self->{connection};
503 my $timeouted = 0;
504  
505 if ($cb) {
506 $self->{_part_timeout} =
507 AnyEvent->timer (after => $timeout, cb => sub {
508 delete $self->{_part_timeout};
509 $timeouted = 1;
510 $self->event ('leave', $self->get_me);
511 });
512  
513 $self->{muc}->reg_cb (ext_after_leave => sub {
514 my ($muc, $room) = @_;
515 return unless cmp_jid ($room->nick_jid, $self->nick_jid);
516  
517 delete $self->{_part_timeout};
518 $cb->($timeouted) if $cb;
519 $muc->unreg_me;
520 });
521 }
522  
523 $con->send_presence (
524 'unavailable', undef,
525 (defined $msg ? (status => $msg) : ()),
526 to => $self->{nick_jid}
527 );
528 }
529  
530 =item B<users>
531  
532 Returns a list of L<AnyEvent::XMPP::Ext::MUC::User> objects
533 which are in this room.
534  
535 =cut
536  
537 sub users {
538 my ($self) = @_;
539 values %{$self->{users}}
540 }
541  
542 =item B<jid>
543  
544 Returns the bare JID of this room.
545  
546 =cut
547  
548 sub jid { $_[0]->{jid} }
549  
550 =item B<nick_jid>
551  
552 Returns the full JID of yourself in the room.
553  
554 =cut
555  
556 sub nick_jid { $_[0]->{nick_jid} }
557  
558 =item B<is_connected>
559  
560 Returns true if this room is still connected (but maybe not joined (yet)).
561  
562 =cut
563  
564 sub is_connected {
565 my ($self) = @_;
566 $self->{muc} && $self->{connection} && $self->{connection}->is_connected
567 }
568  
569 =item B<connection>
570  
571 If the room is still joined this method will return the connection
572 on which the room is connected.
573  
574 =cut
575  
576 sub connection {
577 my ($self) = @_;
578 $self->{connection}
579 }
580  
581 =item B<is_joined>
582  
583 Returns true if this room is still joined (and connected).
584  
585 =cut
586  
587 sub is_joined {
588 my ($self) = @_;
589 $self->is_connected
590 && $self->{status} == JOINED
591 }
592  
593 =item B<change_nick ($newnick)>
594  
595 This method lets you change your nickname in this room.
596  
597 =cut
598  
599 sub change_nick {
600 my ($self, $newnick) = @_;
601 my ($room, $srv) = split_jid $self->jid;
602 $self->{connection}->send_presence (
603 undef, undef, to => join_jid ($room, $srv, $newnick)
604 );
605 }
606  
607 =item B<change_subject ($newsubject)>
608  
609 This methods changes the subject of the room.
610  
611 =cut
612  
613 sub change_subject {
614 my ($self, $newsubject) = @_;
615 my $msg = $self->make_message (subject => $newsubject);
616 $msg->send;
617 }
618  
619 =back
620  
621 =head1 AUTHOR
622  
623 Robin Redeker, C<< <elmex at ta-sa.org> >>, JID: C<< <elmex at jabber.org> >>
624  
625 =head1 COPYRIGHT & LICENSE
626  
627 Copyright 2007, 2008 Robin Redeker, all rights reserved.
628  
629 This program is free software; you can redistribute it and/or modify it
630 under the same terms as Perl itself.
631  
632 =cut
633  
634 1;