root/trunk/lib/whatbot/Message.pm

Revision 240, 4.4 KB (checked in by oz, 2 months ago)

Additional testing support

  • Property svn:executable set to *
Line 
1###########################################################################
2# whatbot/Message.pm
3###########################################################################
4# whatbot message object, created for each incoming and outgoing message.
5###########################################################################
6# the whatbot project - http://www.whatbot.org
7###########################################################################
8
9use MooseX::Declare;
10
11class whatbot::Message extends whatbot::Component {
12    use Encode;
13
14    has 'from'          => ( is => 'rw', isa => 'Str', required => 1 );
15    has 'to'            => ( is => 'rw', isa => 'Str', required => 1 );
16    has 'content'       => ( is => 'rw', isa => 'Str', required => 1, trigger => \&check_content );
17    has 'timestamp'     => ( is => 'rw', isa => 'Int', default => sub { time } );
18    has 'is_direct'     => ( is => 'rw', isa => 'Int', default => 0 );
19    has 'me'            => ( is => 'rw', isa => 'Str' );
20    has 'origin'        => ( is => 'rw', isa => 'Str' );
21    has 'invisible'     => ( is => 'rw', isa => 'Bool', default => 0 );
22
23    method BUILD ($) {
24        my $me = $self->me;
25
26            # Determine if the message is talking about me
27        if ( defined $me ) {
28                if ( $self->content =~ /, ?$me[\?\!\. ]*?$/i ) {
29                        my $content = $self->content;
30                        $content =~ s/, ?$me[\?\!\. ]*?$//i;
31                        $self->content($content);
32                        $self->is_direct(1);
33                       
34                } elsif ( $self->content =~ /^$me[\:\,\- ]+/i ) {
35                        my $content = $self->content;
36                        $content =~ s/^$me[\:\,\- ]+//i;
37                        $self->content($content);
38                        $self->is_direct(1);
39                       
40                } elsif ( $self->content =~ /^$me \-+ /i ) {
41                        my $content = $self->content;
42                        $content =~ s/^$me \-+ //i;
43                        $self->content($content);
44                        $self->is_direct(1);
45                       
46                }
47        }
48
49        $self->is_direct(1) if ( $self->is_private );
50    }
51
52    method content_utf8 {
53        return Encode::encode_utf8( $self->content );
54    }
55
56    method is_private {
57        return ( $self->me ? ( $self->to eq $self->me ) : 0 );
58    }
59
60    method check_content ( Str $content, $? ) {
61        $content =~ s/^\s+//;
62        $content =~ s/\s+$//;
63        $self->{'content'} = $content;
64    }
65
66    method reply ( HashRef $overrides? ) {
67        my $message = whatbot::Message->new({
68            'from'    => $self->me,
69            'to'      => ( $self->is_private ? $self->from : $self->to ),
70            'me'      => $self->me,
71            'content' => '',
72        });
73        foreach my $key ( keys %$overrides ) {
74            $message->$key( $overrides->{$key} );
75        }
76        return $message;
77    }
78}
79
801;
81
82=pod
83
84=head1 NAME
85
86whatbot::Message - Wrapper class for whatbot message passing
87
88=head1 SYNOPSIS
89
90 use whatbot::Message;
91 
92 my $message = whatbot::Message->new(
93    'from'    => $me,
94    'to'      => 'a_user',
95    'content' => 'test message'
96 );
97
98=head1 DESCRIPTION
99
100whatbot::Message is a container class for incoming and outgoing messages. Each
101whatbot component, when sending or receiving a message via a whatbot::IO class
102will pass these objects, and messages sent through a whatbot::Command is
103encouraged to use these objects. Messages sent without this class will be
104converted during the IO transaction.
105
106=head1 PUBLIC ACCESSORS
107
108=over 4
109
110=item from
111
112User or entity the message is from
113
114=item to
115
116User or entity the message is to
117
118=item content
119
120Content of the message
121
122=item timestamp
123
124Timestamp of the message, in unix time.
125
126=item is_private
127
128Boolean (1/0), if the message was private or posted in a public channel.
129
130=item is_direct
131
132Boolean (1/0), if the message called the bot out by name
133
134=item invisible
135
136Boolean (1/0), if this message should not be processed by seen or other monitors.
137
138=item me
139
140String value of the bot's username.
141
142=item origin
143
144String containing the IO target signature, name:to. So, for an IRC server called
145irc.example.org, channel #foo, this would be IRC_irc.example.org:#foo.
146
147=back
148
149=head1 METHODS
150
151=over 4
152
153=item reply( \%overrides )
154
155Generate a whatbot::Message in reply to the current message. If is_private is
156true, to will be set to the originator, otherwise, it will be set to the public
157context for public IO. Optionally handles an override hashref to preset fields,
158similar to the new constructor.
159
160=back
161
162=head1 INHERITANCE
163
164=over 4
165
166=item whatbot::Component
167
168=over 4
169
170=item whatbot::Message
171
172=back
173
174=back
175
176=head1 LICENSE/COPYRIGHT
177
178Be excellent to each other and party on, dudes.
179
180=cut
Note: See TracBrowser for help on using the browser.