| 1 | use strict; | ||
| 2 | use warnings; | ||
| 3 | |||
| 4 | package Bot::Net::Message; | ||
| 5 | use base qw/ Class::Accessor::Fast /; | ||
| 6 | |||
| 7 | use Carp; | ||
| 8 | use English; | ||
| 9 | use Scalar::Util qw/ blessed reftype /; | ||
| 10 | |||
| 11 | __PACKAGE__->mk_accessors( | ||
| 12 | qw/ | ||
| 13 | sender_nick sender_host | ||
| 14 | _recipient_nicks _recipient_groups | ||
| 15 | _message | ||
| 16 | public | ||
| 17 | / | ||
| 18 | ); | ||
| 19 | |||
| 20 | =head1 NAME | ||
| 21 | |||
| 22 | Bot::Net::Message - encapsulate messages to and from bots | ||
| 23 | |||
| 24 | =head1 SYNOPSIS | ||
| 25 | |||
| 26 | my $message = Bot::Net::Message->new({ | ||
| 27 | sender_nick => $from_nick, | ||
| 28 | sender_host => $from_host, | ||
| 29 | recipient_nicks => $to_nick, | ||
| 30 | message => $message, | ||
| 31 | public => 0, | ||
| 32 | }); | ||
| 33 | |||
| 34 | =head1 DESCRIPTION | ||
| 35 | |||
| 36 | Just a simple class for encapsulating bot messages. | ||
| 37 | |||
| 38 | =head1 METHODS | ||
| 39 | |||
| 40 | =head2 new HASHREF | ||
| 41 | |||
| 42 | Create a new message. The C<HASHREF> keys can be the name of any of the accessor methods described for this class. The values are the values to assign to each key. | ||
| 43 | |||
| 44 | =cut | ||
| 45 | |||
| 46 | sub new { | ||
| 47 | my $class = shift; | ||
| 48 | my $args = shift; | ||
| 49 | |||
| 50 | my $self = bless {}, $class; | ||
| 51 | |||
| 52 | for my $key (keys %$args) { | ||
| 53 | if (my $method = $self->can($key)) { | ||
| 54 | $method->($self, $args->{$key}); | ||
| 55 | } | ||
| 56 | else { | ||
| 57 | croak "Unknown accessor $key used in creating a message."; | ||
| 58 | } | ||
| 59 | } | ||
| 60 | |||
| 61 | return $self; | ||
| 62 | } | ||
| 63 | |||
| 64 | =head2 sender_nick [ NICK ] | ||
| 65 | |||
| 66 | This accessor contains the nick of the message's sender. Pass an argument to change the value. | ||
| 67 | |||
| 68 | =head2 sender_host [ HOST ] | ||
| 69 | |||
| 70 | This accessor contains the host of the message's sender. Pass an argument to change the value. | ||
| 71 | |||
| 72 | =head2 recipient_nicks [ NICKS ] | ||
| 73 | |||
| 74 | This accessor returns a reference to an array of nicks that recieved the message. This is generally only set of L</private> returns a true value. You may pass a nick or array of nicks to set this value. | ||
| 75 | |||
| 76 | =cut | ||
| 77 | |||
| 78 | sub recipient_nicks { | ||
| 79 | my $self = shift; | ||
| 80 | |||
| 81 | if (@_) { | ||
| 82 | my $reftype = reftype $_[0]; | ||
| 83 | if (defined $reftype and $reftype eq 'ARRAY' and not blessed $_[0]) { | ||
| 84 | $self->_recipient_nicks([ @{ $_[0] } ]); | ||
| 85 | } | ||
| 86 | |||
| 87 | else { | ||
| 88 | $self->_recipient_nicks([ @_ ]); | ||
| 89 | } | ||
| 90 | } | ||
| 91 | |||
| 92 | return $self->_recipient_nicks; | ||
| 93 | } | ||
| 94 | |||
| 95 | =head2 recipient_groups [ GROUPS ] | ||
| 96 | |||
| 97 | This returns the groups that recieved a public message. This should be set only when L</public> returns true. You may pass a group or an array of groups to set here. | ||
| 98 | |||
| 99 | =cut | ||
| 100 | |||
| 101 | sub recipient_groups { | ||
| 102 | my $self = shift; | ||
| 103 | |||
| 104 | if (@_) { | ||
| 105 | my $reftype = reftype $_[0]; | ||
| 106 | if (defined $reftype and $reftype eq 'ARRAY' and not blessed $_[0]) { | ||
| 107 | $self->_recipient_groups([ @{ $_[0] } ]); | ||
| 108 | } | ||
| 109 | |||
| 110 | else { | ||
| 111 | $self->_recipient_groups([ @_ ]); | ||
| 112 | } | ||
| 113 | } | ||
| 114 | |||
| 115 | return $self->_recipient_groups; | ||
| 116 | } | ||
| 117 | |||
| 118 | =head2 message [ MESSAGE ] | ||
| 119 | |||
| 120 | This accessor returns the message that was sent. Pass a parameter or array of parameters to alter the message. If an array is passed, the elements of the array will be joined using C<$,> (i.e., C<$OUTPUT_FIELD_SEPARATOR> for those who use L<English>). | ||
| 121 | |||
| 122 | =cut | ||
| 123 | |||
| 124 | sub message { | ||
| 125 | my $self = shift; | ||
| 126 | |||
| 127 | if (@_) { | ||
| 128 | $self->_message( join $OUTPUT_FIELD_SEPARATOR||'', @_ ); | ||
| 129 | } | ||
| 130 | |||
| 131 | return $self->_message; | ||
| 132 | } | ||
| 133 | |||
| 134 | =head2 public [ BOOL ] | ||
| 135 | |||
| 136 | Returns true if the message sent was sent to a chat group rather than directly to a set of nicks. Pass a true or false value in to alter the value. If this is set to true, then L</private> will be set to false and vice versa. | ||
| 137 | |||
| 138 | =head2 private [ BOOL ] | ||
| 139 | |||
| 140 | Returns true if the message was sent to a specific set of nicks rather than to a chat group. Pass a true or false value to alter the value. The opposite value will be returned by L</public>. | ||
| 141 | |||
| 142 | =cut | ||
| 143 | |||
| 144 | sub private { | ||
| 145 | my $self = shift; | ||
| 146 | |||
| 147 | if (@_) { | ||
| 148 | $self->public( !$_[0] ); | ||
| 149 | } | ||
| 150 | |||
| 151 | return !$self->public; | ||
| 152 | } | ||
| 153 | |||
| 154 | =head1 AUTHORS | ||
| 155 | |||
| 156 | Andrew Sterling Hanenkamp C<< <hanenkamp@cpan.org> >> | ||
| 157 | |||
| 158 | =head1 COPYRIGHT AND LICENSE | ||
| 159 | |||
| 160 | Copyright 2007 Boomer Consulting, Inc. All Rights Reserved. | ||
| 161 | |||
| 162 | This program is free software and may be modified and distributed under the same terms as Perl itself. | ||
| 163 | |||
| 164 | =cut | ||
| 165 | |||
| 166 | 1; |