| 1 | package AnyEvent::XMPP::Ext::MUC; | ||
| 2 | use strict; | ||
| 3 | no warnings; | ||
| 4 | use AnyEvent::XMPP::Util qw/prep_bare_jid bare_jid stringprep_jid cmp_jid/; | ||
| 5 | use AnyEvent::XMPP::Namespaces qw/xmpp_ns/; | ||
| 6 | use AnyEvent::XMPP::Ext; | ||
| 7 | use AnyEvent::XMPP::Ext::MUC::Room; | ||
| 8 | use AnyEvent::XMPP::Ext::MUC::RoomInfo; | ||
| 9 | |||
| 10 | our @ISA = qw/AnyEvent::XMPP::Ext/; | ||
| 11 | |||
| 12 | =head1 NAME | ||
| 13 | |||
| 14 | AnyEvent::XMPP::Ext::MUC - Implements XEP-0045: Multi-User Chat | ||
| 15 | |||
| 16 | =head1 SYNOPSIS | ||
| 17 | |||
| 18 | my $con = AnyEvent::XMPP::Connection->new (...); | ||
| 19 | $con->add_extension (my $disco = AnyEvent::XMPP::Ext::Disco->new); | ||
| 20 | $con->add_extension (my $muc = AnyEvent::XMPP::Ext::MUC->new (disco => $disco)); | ||
| 21 | ... | ||
| 22 | |||
| 23 | =head1 DESCRIPTION | ||
| 24 | |||
| 25 | This module handles multi user chats and provides new events to catch | ||
| 26 | multi user chat messages. It intercepts messages from the connection | ||
| 27 | so they don't interfere with your other callbacks on the connection. | ||
| 28 | |||
| 29 | This extension requires the L<AnyEvent::XMPP::Ext::Disco> extension for service | ||
| 30 | discovery. | ||
| 31 | |||
| 32 | =cut | ||
| 33 | |||
| 34 | =head1 METHODS | ||
| 35 | |||
| 36 | =over 4 | ||
| 37 | |||
| 38 | =item B<new> | ||
| 39 | |||
| 40 | This is the constructor for a MUC extension object. | ||
| 41 | It takes no further arguments. | ||
| 42 | |||
| 43 | =cut | ||
| 44 | |||
| 45 | sub new { | ||
| 46 | my $this = shift; | ||
| 47 | my $class = ref($this) || $this; | ||
| 48 | my $self = bless { join_timeout => 60, @_ }, $class; | ||
| 49 | $self->{inhibit_forward} = { map { ($_ => 1) } qw/message presence/ }; | ||
| 50 | $self->init; | ||
| 51 | $self | ||
| 52 | } | ||
| 53 | |||
| 54 | sub disco_feature { xmpp_ns ('muc') } | ||
| 55 | |||
| 56 | sub init { | ||
| 57 | my ($self) = @_; | ||
| 58 | |||
| 59 | my $proxy = sub { | ||
| 60 | my ($self, @args) = @_; | ||
| 61 | $self->event (@args); | ||
| 62 | }; | ||
| 63 | |||
| 64 | $self->{disco}->enable_feature ($self->disco_feature); | ||
| 65 | |||
| 66 | $self->reg_cb ( | ||
| 67 | join_error => $proxy, | ||
| 68 | subject_change_error => $proxy, | ||
| 69 | message_error => $proxy, | ||
| 70 | ); | ||
| 71 | |||
| 72 | $self->reg_cb ( | ||
| 73 | ext_before_presence_xml => sub { | ||
| 74 | my ($self, $con, $node) = @_; | ||
| 75 | |||
| 76 | if (my $room = $self->get_room ($con, $node->attr ('from'))) { | ||
| 77 | $self->stop_event; | ||
| 78 | $room->handle_presence ($node); | ||
| 79 | } | ||
| 80 | }, | ||
| 81 | ext_before_message_xml => sub { | ||
| 82 | my ($self, $con, $node) = @_; | ||
| 83 | |||
| 84 | if (my $room = $self->get_room ($con, $node->attr ('from'))) { | ||
| 85 | $self->stop_event; | ||
| 86 | $room->handle_message ($node); | ||
| 87 | } | ||
| 88 | }, | ||
| 89 | ext_before_leave => sub { | ||
| 90 | my ($self, $room) = @_; | ||
| 91 | $self->uninstall_room ($room->connection, $room); | ||
| 92 | }, | ||
| 93 | disconnect => sub { | ||
| 94 | my ($self, $con, $h, $p, $msg) = @_; | ||
| 95 | $self->cleanup_rooms ($con, "$h:$p: $msg"); | ||
| 96 | } | ||
| 97 | ); | ||
| 98 | } | ||
| 99 | |||
| 100 | sub cleanup_rooms { | ||
| 101 | my ($self, $con, $msg) = @_; | ||
| 102 | |||
| 103 | my $conjid = stringprep_jid $con->jid; | ||
| 104 | |||
| 105 | for (keys %{$self->{rooms}->{$conjid}}) { | ||
| 106 | my $room = delete $self->{rooms}->{$conjid}->{$_}; | ||
| 107 | $self->event (leave => $room, $room->get_me, "disconnected from server $msg"); | ||
| 108 | } | ||
| 109 | } | ||
| 110 | |||
| 111 | =item B<is_conference ($con, $jid, $cb)> | ||
| 112 | |||
| 113 | TODO | ||
| 114 | |||
| 115 | =cut | ||
| 116 | |||
| 117 | sub is_conference { | ||
| 118 | my ($self, $con, $jid, $cb) = @_; | ||
| 119 | |||
| 120 | $self->{disco}->request_info ($con, $jid, undef, sub { | ||
| 121 | my ($disco, $info, $error) = @_; | ||
| 122 | |||
| 123 | if ($error || !$info->features ()->{xmpp_ns ('muc')}) { | ||
| 124 | $cb->(undef, $error); | ||
| 125 | } else { | ||
| 126 | $cb->($info, undef); | ||
| 127 | } | ||
| 128 | }); | ||
| 129 | } | ||
| 130 | |||
| 131 | =item B<is_room ($con, $jid, $cb)> | ||
| 132 | |||
| 133 | This method sends a information discovery to the C<$jid>, via the connection | ||
| 134 | C<$con>. C<$cb> is called when the information arrives or with an error after | ||
| 135 | the usual IQ timeout. | ||
| 136 | |||
| 137 | When the C<$jid> was a room C<$cb> is called with the first argument | ||
| 138 | being a L<AnyEvent::XMPP::Ext::MUC::RoomInfo> object. If the destination | ||
| 139 | wasn't reachable, the room doesn't exist or some other error happened | ||
| 140 | the first argument will be undefined and the second a L<AnyEvent::XMPP::Error::IQ> | ||
| 141 | object. | ||
| 142 | |||
| 143 | =cut | ||
| 144 | |||
| 145 | sub is_room { | ||
| 146 | my ($self, $con, $jid, $cb) = @_; | ||
| 147 | |||
| 148 | $self->{disco}->request_info ($con, $jid, undef, sub { | ||
| 149 | my ($disco, $info, $error) = @_; | ||
| 150 | |||
| 151 | if ($error || !$info->features ()->{xmpp_ns ('muc')}) { | ||
| 152 | $cb->(undef, $error); | ||
| 153 | } else { | ||
| 154 | my $rinfo = AnyEvent::XMPP::Ext::MUC::RoomInfo->new (disco_info => $info); | ||
| 155 | $cb->($rinfo, undef); | ||
| 156 | } | ||
| 157 | }); | ||
| 158 | } | ||
| 159 | |||
| 160 | =item B<join_room ($con, $jid, $nick, %args)> | ||
| 161 | |||
| 162 | This method joins a room. | ||
| 163 | |||
| 164 | C<$con> should be the L<AnyEvent::XMPP::IM::Connection> object that | ||
| 165 | is to be used to send the necessary stanzas. | ||
| 166 | C<$jid> should be the bare JID of the room. | ||
| 167 | C<$nick> should be your desired nickname in the room. | ||
| 168 | |||
| 169 | When you successfully entered the room a C<enter> event is emitted. In case | ||
| 170 | you created the room, and it is locked, a C<locked> event is emitted. Please | ||
| 171 | look in the C<EVENTS> section below for more details about how to handle | ||
| 172 | C<locked> rooms. (You won't have to care about locked rooms if you | ||
| 173 | didn't disable the C<create_instant> flag in C<%args>). | ||
| 174 | |||
| 175 | If an error occurred and we couldn't join the room, the first two arguments are | ||
| 176 | undef and the third is a L<AnyEvent::XMPP::Error::MUC> object signalling the error. | ||
| 177 | |||
| 178 | C<%args> hash can contain one of the following keys: | ||
| 179 | |||
| 180 | =over 4 | ||
| 181 | |||
| 182 | =item timeout => $timeout_in_secs | ||
| 183 | |||
| 184 | This is the timeout for joining the room. | ||
| 185 | The default timeout is 60 seconds if the timeout is not specified. | ||
| 186 | |||
| 187 | =item history => {} | ||
| 188 | |||
| 189 | Manage MUC-history from XEP-0045 (7.1.16) | ||
| 190 | Hash can contain of the following keys: C<chars>, C<stanzas>, C<seconds> | ||
| 191 | |||
| 192 | Example: | ||
| 193 | |||
| 194 | history => {chars => 0} # don't load history | ||
| 195 | history => {stanzas => 3} # load last 3 history elements | ||
| 196 | history => {seconds => 300, chars => 500} | ||
| 197 | # load history in last 5 minutes, but max 500 characters | ||
| 198 | |||
| 199 | TODO: add C<since> attributes | ||
| 200 | |||
| 201 | =item create_instant => $bool | ||
| 202 | |||
| 203 | If you set C<$bool> to a true value we try to establish an instant room | ||
| 204 | on joining if it doesn't already exist. | ||
| 205 | |||
| 206 | XXX XXX XXX | ||
| 207 | XXX XXX XXX | ||
| 208 | XXX XXX XXX | ||
| 209 | XXX XXX XXX | ||
| 210 | |||
| 211 | The default for this flag is true! So if you want to create an reserved room | ||
| 212 | with custom creation in the beginning you have to pass a false value as C<$bool>. | ||
| 213 | |||
| 214 | B<PLEASE NOTE:> If you set C<$bool> to a B<false> value you have to check the | ||
| 215 | C<did_create_room> status flag on your own instance of | ||
| 216 | L<AnyEvent::XMPP::Ext::MUC::User> (provided as the second argument to the | ||
| 217 | callback) to see whether you need to finish room creation! If you don't do this | ||
| 218 | the room B<may stay LOCKED for ever>. | ||
| 219 | |||
| 220 | See also the C<make_instant> and C<request_configuration> methods of | ||
| 221 | L<AnyEvent::XMPP::Ext::MUC>. | ||
| 222 | |||
| 223 | =item password => $password | ||
| 224 | |||
| 225 | The password for the room. | ||
| 226 | |||
| 227 | =item nickcollision_cb => $cb | ||
| 228 | |||
| 229 | If the join to the room results in a nickname collision the C<$cb> | ||
| 230 | will be called with the nickname that collided and the return value will | ||
| 231 | be used as alternate nickname and the join is retried. | ||
| 232 | |||
| 233 | This function is called I<everytime> the nickname collides on join, so you | ||
| 234 | should take care of possible endless retries. | ||
| 235 | |||
| 236 | =back | ||
| 237 | |||
| 238 | =cut | ||
| 239 | |||
| 240 | sub join_room { | ||
| 241 | my ($self, $con, $jid, $nick, %args) = @_; | ||
| 242 | |||
| 243 | unless (exists $args{create_instant}) { | ||
| 244 | $args{create_instant} = 1; | ||
| 245 | } | ||
| 246 | |||
| 247 | my $timeout = $args{timeout} || $self->{join_timeout}; | ||
| 248 | |||
| 249 | my $room = $self->install_room ($con, $jid); | ||
| 250 | |||
| 251 | $room->{locked_cb} = | ||
| 252 | $args{create_instant} ? sub { | ||
| 253 | my ($room) = @_; | ||
| 254 | |||
| 255 | $room->make_instant (sub { | ||
| 256 | my ($room, $error) = @_; | ||
| 257 | |||
| 258 | if ($error) { | ||
| 259 | $self->event (join_error => $room, $error); | ||
| 260 | } else { | ||
| 261 | $self->event (enter => $room, $room->get_me); | ||
| 262 | } | ||
| 263 | }); | ||
| 264 | } : undef; | ||
| 265 | |||
| 266 | $room->{room_join_timer} = | ||
| 267 | AnyEvent->timer (after => $timeout, cb => sub { | ||
| 268 | delete $room->{room_join_timer}; | ||
| 269 | $self->uninstall_room ($con, $room); | ||
| 270 | |||
| 271 | my $muce = AnyEvent::XMPP::Error::MUC->new ( | ||
| 272 | type => 'join_timeout', | ||
| 273 | text => "Couldn't join room in time, timeout after $timeout\n" | ||
| 274 | ); | ||
| 275 | |||
| 276 | $self->event (join_error => $room, $muce); | ||
| 277 | }); | ||
| 278 | |||
| 279 | my $rcb_id; | ||
| 280 | $rcb_id = $self->reg_cb ( | ||
| 281 | join_error => sub { | ||
| 282 | my ($muc, $eroom, $error) = @_; | ||
| 283 | return unless cmp_jid ($eroom->nick_jid, $room->nick_jid); | ||
| 284 | |||
| 285 | if ($error->type eq 'nickname_in_use' | ||
| 286 | && exists $args{nickcollision_cb}) { | ||
| 287 | |||
| 288 | $nick = $args{nickcollision_cb}->($nick); | ||
| 289 | $room->send_join ($nick, $args{password}, $args{history}); | ||
| 290 | return; | ||
| 291 | } | ||
| 292 | |||
| 293 | delete $room->{room_join_timer}; | ||
| 294 | $self->uninstall_room ($con, $room); | ||
| 295 | $muc->unreg_cb ($rcb_id); | ||
| 296 | }, | ||
| 297 | enter => sub { | ||
| 298 | my ($muc, $eroom, $user) = @_; | ||
| 299 | return unless cmp_jid ($eroom->nick_jid, $room->nick_jid); | ||
| 300 | |||
| 301 | delete $room->{room_join_timer}; | ||
| 302 | $muc->unreg_cb ($rcb_id); | ||
| 303 | } | ||
| 304 | ); | ||
| 305 | |||
| 306 | $room->send_join ($nick, $args{password}, $args{history}); | ||
| 307 | } | ||
| 308 | |||
| 309 | sub install_room { | ||
| 310 | my ($self, $con, $room_jid) = @_; | ||
| 311 | |||
| 312 | my $room | ||
| 313 | = $self->{rooms}->{stringprep_jid $con->jid}->{prep_bare_jid $room_jid} | ||
| 314 | = AnyEvent::XMPP::Ext::MUC::Room->new ( | ||
| 315 | muc => $self, | ||
| 316 | connection => $con, | ||
| 317 | jid => $room_jid | ||
| 318 | ); | ||
| 319 | |||
| 320 | $room | ||
| 321 | } | ||
| 322 | |||
| 323 | sub uninstall_room { | ||
| 324 | my ($self, $con, $room) = @_; | ||
| 325 | my $r = | ||
| 326 | delete $self->{rooms}->{stringprep_jid $con->jid}->{prep_bare_jid $room->jid}; | ||
| 327 | delete $r->{muc}; | ||
| 328 | } | ||
| 329 | |||
| 330 | =item B<get_room ($con, $jid)> | ||
| 331 | |||
| 332 | This returns the L<AnyEvent::XMPP::Ext::MUC::Room> object | ||
| 333 | for the bare part of the C<$jid> if we are joining or have | ||
| 334 | joined such a room. | ||
| 335 | |||
| 336 | If we are not joined undef is returned. | ||
| 337 | |||
| 338 | =cut | ||
| 339 | |||
| 340 | sub get_room { | ||
| 341 | my ($self, $con, $jid) = @_; | ||
| 342 | $self->{rooms}->{stringprep_jid $con->jid}->{prep_bare_jid $jid} | ||
| 343 | } | ||
| 344 | |||
| 345 | =item B<get_rooms ($con)> | ||
| 346 | |||
| 347 | Returns a list of L<AnyEvent::XMPP::Ext::MUC::Room> objects | ||
| 348 | for the connection C<$con>. | ||
| 349 | |||
| 350 | =cut | ||
| 351 | |||
| 352 | sub get_rooms { | ||
| 353 | my ($self, $con) = @_; | ||
| 354 | values %{$self->{rooms}->{stringprep_jid $con->jid} || {}} | ||
| 355 | } | ||
| 356 | |||
| 357 | =back | ||
| 358 | |||
| 359 | =head1 EVENTS | ||
| 360 | |||
| 361 | These are the events that are issued by this MUC extension: | ||
| 362 | |||
| 363 | C<$room> is the L<AnyEvent::XMPP::Ext::MUC::Room> object which | ||
| 364 | the event belongs to. | ||
| 365 | |||
| 366 | =over 4 | ||
| 367 | |||
| 368 | =item message => $room, $msg, $is_echo | ||
| 369 | |||
| 370 | This event is emitted when a message was received from the room. | ||
| 371 | C<$msg> is a L<AnyEvent::XMPP::Ext::MUC::Message> object and C<$is_echo> | ||
| 372 | is true if the message is an echo. | ||
| 373 | |||
| 374 | B<NOTE:> Please note that some conferences send messages already | ||
| 375 | before you have finished joining a room. That means that you might | ||
| 376 | already get a C<message> event for a room that you haven't got an C<enter> | ||
| 377 | for event yet. That means that methods like C<get_me> might return undef. | ||
| 378 | |||
| 379 | =item subject_change => $room, $msg, $is_echo | ||
| 380 | |||
| 381 | This event is emitted when a user changes the room subject. | ||
| 382 | C<$msg> is a L<AnyEvent::XMPP::Ext::MUC::Message> object and C<$is_echo> | ||
| 383 | is true if the message is an echo. | ||
| 384 | |||
| 385 | The room subject is the subject of that C<$msg>. | ||
| 386 | |||
| 387 | =item subject_change_error => $room, $error | ||
| 388 | |||
| 389 | If you weren't allowed to change the subject or some other error | ||
| 390 | occurred you will receive this event. | ||
| 391 | C<$error> is a L<AnyEvent::XMPP::Error::MUC> object. | ||
| 392 | |||
| 393 | =item error => $room, $error | ||
| 394 | |||
| 395 | This event is emitted when any error occurred. | ||
| 396 | C<$error> is a L<AnyEvent::XMPP::Error::MUC> object. | ||
| 397 | |||
| 398 | =item join_error => $room, $error | ||
| 399 | |||
| 400 | This event is emitted when a error occurred when joining a room. | ||
| 401 | C<$error> is a L<AnyEvent::XMPP::Error::MUC> object. | ||
| 402 | |||
| 403 | =item locked => $room | ||
| 404 | |||
| 405 | This event is emitted when you disabled the 'create_instant' | ||
| 406 | flag when calling C<join_room>. It means that you just created | ||
| 407 | a new room, which is locked. You need to configure it before | ||
| 408 | it is unlocked and others can enter. | ||
| 409 | |||
| 410 | Please consult the methods C<make_instant>, C<request_configuration> and | ||
| 411 | C<send_configuration> of L<AnyEvent::XMPP::Ext::MUC::Room> for more information | ||
| 412 | about how to configure a room. | ||
| 413 | |||
| 414 | B<NOTE:> You won't get another event when you finished configuring the room, so | ||
| 415 | you maybe want to call this on the C<AnyEvent::XMPP::Ext::MUC> object when you | ||
| 416 | finished configuring the room successfully: | ||
| 417 | |||
| 418 | $muc->event (enter => $room, $room->get_me); | ||
| 419 | |||
| 420 | That could be helpful if you want to place some generic | ||
| 421 | stuff in your C<enter> event handlers. | ||
| 422 | |||
| 423 | B<NOTE2:> If you didn't disable the C<create_instant> flag of C<join_room> you | ||
| 424 | won't have to care about a C<locked> event, as everything will be internally | ||
| 425 | handled for you and you will get an C<enter> event if the room is finally | ||
| 426 | setted up. | ||
| 427 | |||
| 428 | =item enter => $room, $user | ||
| 429 | |||
| 430 | This event is emitted when we successfully joined the room. | ||
| 431 | C<$user> is a L<AnyEvent::XMPP::Ext::MUC::User> object which is | ||
| 432 | the user handle for ourself. | ||
| 433 | |||
| 434 | =item join => $room, $user | ||
| 435 | |||
| 436 | This event is emitted when a new user joins the room. | ||
| 437 | C<$user> is the L<AnyEvent::XMPP::Ext::MUC::User> object of that user. | ||
| 438 | |||
| 439 | =item nick_change => $room, $user, $oldnick, $newnick | ||
| 440 | |||
| 441 | This event is emitted when a user changed his nickname. | ||
| 442 | C<$user> is the L<AnyEvent::XMPP::Ext::MUC::User> object of that user. | ||
| 443 | C<$oldnick> is the old nickname and C<$newnick> is the new nickname. | ||
| 444 | |||
| 445 | =item presence => $room, $user | ||
| 446 | |||
| 447 | This event is emitted when a user changes it's presence status | ||
| 448 | (eg. affiliation or role, or away status). | ||
| 449 | C<$user> is the L<AnyEvent::XMPP::Ext::MUC::User> object of that user. | ||
| 450 | |||
| 451 | =item part => $room, $user | ||
| 452 | |||
| 453 | This event is emitted when a user leaves the channel. C<$user> is the | ||
| 454 | L<AnyEvent::XMPP::Ext::MUC::User> of that user, but please note that you shouldn't | ||
| 455 | send any messages to this user anymore. | ||
| 456 | |||
| 457 | =item leave => $room, $user | ||
| 458 | |||
| 459 | This event is emitted when we leave the room. C<$user> is your | ||
| 460 | L<AnyEvent::XMPP::Ext::MUC::User> handle. | ||
| 461 | |||
| 462 | =back | ||
| 463 | |||
| 464 | =head1 AUTHOR | ||
| 465 | |||
| 466 | Robin Redeker, C<< <elmex at ta-sa.org> >>, JID: C<< <elmex at jabber.org> >> | ||
| 467 | |||
| 468 | =head1 COPYRIGHT & LICENSE | ||
| 469 | |||
| 470 | Copyright 2007, 2008 Robin Redeker, all rights reserved. | ||
| 471 | |||
| 472 | This program is free software; you can redistribute it and/or modify it | ||
| 473 | under the same terms as Perl itself. | ||
| 474 | |||
| 475 | =cut | ||
| 476 | |||
| 477 | 1; # End of AnyEvent::XMPP::Ext::MUC |