1package AnyEvent::XMPP::Ext::MUC::Message;
2use strict;
3use AnyEvent::XMPP::Namespaces qw/xmpp_ns/;
4use AnyEvent::XMPP::Util qw/bare_jid res_jid/;
5use AnyEvent::XMPP::IM::Message;
6
7our @ISA = qw/AnyEvent::XMPP::IM::Message/;
8
9=head1 NAME
10
11AnyEvent::XMPP::Ext::MUC::Message - A room message
12
13=head1 SYNOPSIS
14
15=head1 DESCRIPTION
16
17This message represents a message from a MUC room. It is
18derived from L<AnyEvent::XMPP::IM::Message>. (You can use the
19methods from that class to access it for example).
20
21Also the methods like eg. C<make_reply> return a
22L<AnyEvent::XMPP::Ext::MUC::Message>.
23
24=head1 METHODS
25
26=over 4
27
28=item B<new (%args)>
29
30This constructor takes the same arguments that the constructor for
31L<AnyEvent::XMPP::IM::Message> takes.
32
33=cut
34
35sub new {
36   my $this = shift;
37   my $class = ref($this) || $this;
38   my $self = $class->SUPER::new (@_);
39   $self->{connection} = $self->{room}->{connection};
40   $self
41}
42
43sub from_node {
44   my ($self, $node) = @_;
45   $self->SUPER::from_node ($node);
46}
47
48=item B<room>
49
50Returns the chatroom in which' context this message
51was sent.
52
53=cut
54
55sub room { $_[0]->{room} }
56
57=item B<send ([$room])>
58
59This method send this message. If C<$room>
60is defined it will set the connection of this
61message object before it is send.
62
63=cut
64
65sub send {
66   my ($self, $room) = @_;
67
68   if ($room) {
69      $self->{room} = $room;
70      $self->{connection} = $self->{room}->{connection};
71   }
72
73   my @add;
74   push @add, (subject => $self->{subjects})
75      if %{$self->{subjects} || {}};
76   push @add, (thread => $self->thread)
77      if $self->thread;
78   push @add, (from => $self->from)
79      if defined $self->from;
80
81   $self->{connection}->send_message (
82      $self->to, $self->type, $self->{create_cbs},
83      body => $self->{bodies},
84      @add
85   );
86}
87
88=item B<make_reply ([$msg])>
89
90This method returns a new instance of L<AnyEvent::XMPP::Ext::MUC::Message>.
91The destination address, connection and type of the returned message
92object will be set.
93
94If C<$msg> is defined and an instance of L<AnyEvent::XMPP::Ext::MUC::Message>
95the destination address, connection and type of C<$msg> will be changed
96and this method will not return a new instance of L<AnyEvent::XMPP::Ext::MUC::Message>.
97
98If C<$self> is a message of type 'groupchat' the C<to> attribute
99will be set to the bare JID of the room for the reply.
100
101=cut
102
103sub make_reply {
104   my ($self, $msg) = @_;
105
106   unless ($msg) {
107      $msg = $self->new (room => $self->room);
108   }
109
110   $msg->{connection} = $self->{connection};
111   $msg->{room}       = $self->{room};
112
113   if ($self->type eq 'groupchat') {
114      $msg->to (bare_jid $self->from);
115
116   } else {
117      $msg->to ($self->from);
118   }
119   $msg->type ($self->type);
120
121   $msg
122}
123
124=item B<from_nick>
125
126This method returns the nickname of the source
127of this message.
128
129=cut
130
131sub from_nick {
132   my ($self) = @_;
133   res_jid ($self->from)
134}
135
136=item B<is_private>
137
138This method returns true when the message was not directed to the
139room, but privately to you.
140
141=cut
142
143sub is_private {
144   my ($self) = @_;
145   $self->type ne 'groupchat'
146}
147
148=back
149
150=head1 AUTHOR
151
152Robin Redeker, C<< <elmex at ta-sa.org> >>, JID: C<< <elmex at jabber.org> >>
153
154=head1 COPYRIGHT & LICENSE
155
156Copyright 2007, 2008 Robin Redeker, all rights reserved.
157
158This program is free software; you can redistribute it and/or modify it
159under the same terms as Perl itself.
160
161=cut
162
1631;
164