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 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542
|
package AnyEvent::XMPP::IM::Message;
use strict;
use overload
'""' => "to_string";
use AnyEvent::XMPP::IM::Delayed;
our @ISA = qw/AnyEvent::XMPP::IM::Delayed/;
=head1 NAME
AnyEvent::XMPP::IM::Message - Instant message
=head1 SYNOPSIS
use AnyEvent::XMPP::IM::Message;
my $con = AnyEvent::XMPP::IM::Connection->new (...);
AnyEvent::XMPP::IM::Message->new (
body => "Hello there!",
to => "elmex@jabber.org"
)->send ($con);
=head1 DESCRIPTION
This module represents an instant message. It's mostly
a shortlived object and acts as wrapper object around the
XML stuff that is happening under the hood.
A L<AnyEvent::XMPP::IM::Message> object overloads the stringification
operation. The string represenation of this object is the return
value of the C<any_body> method.
L<AnyEvent::XMPP::IM::Message> is derived from L<AnyEvent::XMPP::IM::Delayed>,
use the interface described there to find out whether this message was delayed.
=head1 METHODS
=over 4
=item B<new (%args)>
This method creates a new instance of a L<AnyEvent::XMPP::IM::Message>.
C<%args> is the argument hash. All arguments to C<new> are optional.
These are the possible keys:
=over 4
=item connection => $connection
This is the L<AnyEvent::XMPP::IM::Connection> object that will
be used to send this message when the C<send> method is called.
=item to => $jid
This is the destination JID of this message.
C<$jid> should be full if this message is send within a conversation
'context', for example when replying to a previous message.
Replies can also be generated by the C<make_reply> method, see also
the C<from> argument below.
=item from => $jid
This is the source JID of this message, it's mainly
used by the C<make_reply> method.
=item lang => $lang
This is the default language that will be used to tag the values
passed in the C<body> and C<subject> argument to C<new>.
=item body => $body
This is the text C<$body> of the message either with the language
tag from the C<lang> attached or without any language tag.
If you want to attach multiple bodies with different languages use the C<add_body>
method.
=item subject => $subject
This is the C<$subject> of the message either with the language
tag from the C<lang> attached or without any language tag.
If you want to attach the subject with a different language use the C<add_subject>
method.
=item type => $type
This field sets the type of the message. See also the L<type> method below.
The default value for C<$type> is 'normal'.
=back
=cut
sub new {
my $this = shift;
my $class = ref($this) || $this;
my $self = bless { @_ }, $class;
if (my $sub = delete $self->{subject}) {
$self->add_subject ($sub);
}
if (my $body = delete $self->{body}) {
$self->add_body ($body);
}
$self->{type} ||= 'normal'; # default it to 'normal'
$self->{lang} ||= '';
$self
}
sub from_node {
my ($self, $node) = @_;
$self->{node} = $node;
$self->fetch_delay_from_node ($node);
my $id = $node->attr ('id');
my $from = $node->attr ('from');
my $to = $node->attr ('to');
my $type = $node->attr ('type');
my ($thread) = $node->find_all ([qw/client thread/]);
my %bodies;
my %subjects;
$bodies{$_->attr ('lang') || ''} = $_->text
for $node->find_all ([qw/client body/]);
$subjects{$_->attr ('lang') || ''} = $_->text
for $node->find_all ([qw/client subject/]);
$self->{id} = $id;
$self->{from} = $from;
$self->{to} = $to;
$self->{type} = $type;
$self->{thread} = $thread;
$self->{bodies} = \%bodies;
$self->{subjects} = \%subjects;
}
sub to_string {
my ($self) = @_;
$self->any_body
}
=item B<id ([$msg_id])>
This method returns the ID of this message.
If C<$msg_id> is not undef it will replace the current
message id.
=cut
sub id {
my ($self, $id) = @_;
$self->{id} = $id if defined $id;
$self->{id}
}
=item B<from ([$jid])>
This method returns the source JID of this message.
If C<$jid> is not undef it will replace the current
source address.
=cut
sub from {
my ($self, $from) = @_;
$self->{from} = $from if defined $from;
$self->{from}
}
=item B<to ([$jid])>
This method returns the destination JID of this message.
If C<$jid> is not undef it will replace the current
destination address.
=cut
sub to {
my ($self, $to) = @_;
$self->{to} = $to if defined $to;
$self->{to}
}
=item B<make_reply ([$msg])>
This method returns a new instance of L<AnyEvent::XMPP::IM::Message>.
The destination address, connection and type of the returned message
object will be set.
If C<$msg> is defined and an instance of L<AnyEvent::XMPP::IM::Message>
the destination address, connection and type of C<$msg> will be changed
and this method will not return a new instance of L<AnyEvent::XMPP::IM::Message>.
=cut
sub make_reply {
my ($self, $msg) = @_;
unless ($msg) {
$msg = $self->new ();
}
$msg->{connection} = $self->{connection};
$msg->to ($self->from);
$msg->type ($self->type);
$msg
}
=item B<is_connected ()>
This method returns 1 when the message is "connected".
That means: It returns 1 when you can call the C<send> method
without a connection argument. (It will also return only 1 when
the connection that is referenced by this message is still
connected).
=cut
sub is_connected {
my ($self) = @_;
$self->{connection}->is_connected
}
=item B<send ([$connection])>
This method send this message. If C<$connection>
is defined it will set the connection of this
message object before it is send.
=cut
sub send {
my ($self, $connection) = @_;
$self->{connection} = $connection if $connection;
my @add;
push @add, (subject => $self->{subjects})
if %{$self->{subjects} || {}};
push @add, (thread => $self->thread)
if $self->thread;
push @add, (from => $self->from)
if $self->from;
push @add, (id => $self->id)
if $self->id;
$self->{connection}->send_message (
$self->to, $self->type, $self->{create_cbs},
body => $self->{bodies},
@add
);
}
=item B<type ([$type])>
This method returns the type of the message, which
is either undefined or one of the following values:
'chat', 'error', 'groupchat', 'headline', 'normal'
If the C<$type> argument is defined it will set the type
of this message.
=cut
sub type {
my ($self, $type) = @_;
$self->{type} = $type
if defined $type;
$self->{type}
}
=item B<thread ([$thread])>
This method returns the thread id of this message,
which might be undefined.
If you want to set the threadid simply pass the C<$thread>
argument.
=cut
sub thread {
my ($self, $thread) = @_;
$self->{thread} = $thread
if defined $thread;
$self->{thread}
}
=item B<lang ([$lang])>
This returns the default language tag of this message,
which can be undefined.
To set the language tag pass the C<$lang> argument, which
should be the new default language tag.
If you do not want to specify any language pass the empty
string as language tag.
=cut
sub lang {
my ($self, $lang) = @_;
$self->{lang} = $lang
if defined $lang;
$self->{lang}
}
=item B<subject ([$lang])>
This method returns the subject of this message.
If the C<$lang> argument is defined a subject of that
language will be returned or undef.
If the C<$lang> argument is undefined this method will
return either the subject in the default language.
=cut
sub subject {
my ($self, $lang) = @_;
if (defined $lang) {
return $self->{subjects}->{$lang}
}
return $self->{subjects}->{$self->{lang}};
undef
}
=item B<any_subject ([$lang])>
This method will try to find any subject on the message with the
following try order of languagetags:
1. $lang argument if one passed
2. default language
3. subject without any language tag
4. subject with the 'en' language tag
5. any subject from any language
=cut
sub any_subject {
my ($self, $lang) = @_;
if (defined $lang) {
return $self->{subjects}->{$lang}
if defined $self->{subjects}->{$lang};
}
return $self->{subjects}->{$self->{lang}}
if defined $self->{subjects}->{$self->{lang}};
return $self->{subjects}->{''}
if defined $self->{subjects}->{''};
return $self->{subjects}->{en}
if defined $self->{subjects}->{en};
return $self->{subjects}->{$_} for (keys %{$self->{subjects}});
return undef;
}
=item B<add_subject ($subject, [$lang], [$subject2, $lang2, ...])>
This method adds the subject C<$subject> with the optional
language tag C<$lang> to this message. If no C<$lang>
argument is passed the default language for this message will be used.
Further subject => lang pairs can passed to this function like this:
$msg->add_subject ('foobar' => undef, "barfooo" => "de");
=cut
sub add_subject {
my $self = shift;
while (@_) {
my $subj = shift;
my $lang = shift;
$self->{subjects}->{$lang || $self->{lang} || ''} = $subj;
}
$self
}
=item B<subjects>
This method returns a list of key value pairs
with the language tag as key and the subject as value.
The subject which has the empty string as key has no
language attached.
=cut
sub subjects {
%{$_[0]->{subjects} || {}}
}
=item B<body ([$lang])>
This method returns the body of this message.
If the C<$lang> argument is defined a body of that
language will be returned or undef.
If the C<$lang> argument is undefined this method will
return either the body in the default language.
=cut
sub body {
my ($self, $lang) = @_;
if (defined $lang) {
return $self->{bodies}->{$lang}
} else {
return $self->{bodies}->{$self->{lang}}
if defined $self->{bodies}->{$self->{lang}};
}
undef
}
=item B<any_body ([$lang])>
This method will try to find any body on the message with the
following try order of languagetags:
1. $lang argument if one passed
2. default language
3. body without any language tag
4. body with the 'en' language tag
5. any body from any language
=cut
sub any_body {
my ($self, $lang) = @_;
if (defined $lang) {
return $self->{bodies}->{$lang}
if defined $self->{bodies}->{$lang};
}
return $self->{bodies}->{$self->{lang}}
if defined $self->{bodies}->{$self->{lang}};
return $self->{bodies}->{''}
if defined $self->{bodies}->{''};
return $self->{bodies}->{en}
if defined $self->{bodies}->{en};
return $self->{bodies}->{$_} for (keys %{$self->{bodies}});
return undef;
}
=item B<add_body ($body, [$lang], [$body2, $lang2, ...])>
This method adds the body C<$body> with the optional
language tag C<$lang> to this message. If no C<$lang>
argument is passed the default language for this message will be used.
Further body => lang pairs can passed to this function like this:
$msg->add_body ('foobar' => undef, "barfooo" => "de");
=cut
sub add_body {
my $self = shift;
while (@_) {
my $body = shift;
my $lang = shift;
$self->{bodies}->{$lang || $self->{lang} || ''} = $body;
}
$self
}
=item B<bodies>
This method returns a list of key value pairs
with the language tag as key and the body as value.
The body which has the empty string as key has no
language attached.
=cut
sub bodies {
%{$_[0]->{bodies} || {}}
}
=item B<append_creation ($create_cb)>
This method allows the user to append custom XML stuff to the message
when it is sent. This is an example:
my $msg =
AnyEvent::XMPP::IM::Message->new (
body => "Test!",
to => "test@test.tld",
);
$msg->append_creation (sub {
my ($w) = @_;
$w->startTag (['http://test.namespace','test']);
$w->characters ("TEST");
$w->endTag;
});
$msg->send ($con);
This should send a message stanza similar to this:
=cut
sub append_creation {
my ($self, $cb) = @_;
push @{$self->{create_cbs}}, $cb;
}
=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; # End of AnyEvent::XMPP
|