[ee183be] | 1 | use strict; |
---|
| 2 | use warnings; |
---|
| 3 | |
---|
| 4 | package BarnOwl::Message; |
---|
| 5 | |
---|
[fd8dfe7] | 6 | use BarnOwl::Message::Admin; |
---|
| 7 | use BarnOwl::Message::AIM; |
---|
| 8 | use BarnOwl::Message::Generic; |
---|
| 9 | use BarnOwl::Message::Loopback; |
---|
| 10 | use BarnOwl::Message::Zephyr; |
---|
| 11 | |
---|
[ee183be] | 12 | sub new { |
---|
| 13 | my $class = shift; |
---|
| 14 | my %args = (@_); |
---|
| 15 | if($class eq __PACKAGE__ && $args{type}) { |
---|
| 16 | $class = "BarnOwl::Message::" . ucfirst $args{type}; |
---|
| 17 | } |
---|
| 18 | return bless {%args}, $class; |
---|
| 19 | } |
---|
| 20 | |
---|
| 21 | sub type { return shift->{"type"}; } |
---|
| 22 | sub direction { return shift->{"direction"}; } |
---|
| 23 | sub time { return shift->{"time"}; } |
---|
[d1ae4a4] | 24 | sub unix_time { return shift->{"unix_time"}; } |
---|
[ee183be] | 25 | sub id { return shift->{"id"}; } |
---|
| 26 | sub body { return shift->{"body"}; } |
---|
| 27 | sub sender { return shift->{"sender"}; } |
---|
| 28 | sub recipient { return shift->{"recipient"}; } |
---|
| 29 | sub login { return shift->{"login"}; } |
---|
| 30 | sub is_private { return shift->{"private"}; } |
---|
| 31 | |
---|
| 32 | sub is_login { return shift->login eq "login"; } |
---|
| 33 | sub is_logout { return shift->login eq "logout"; } |
---|
| 34 | sub is_loginout { my $m=shift; return ($m->is_login or $m->is_logout); } |
---|
| 35 | sub is_incoming { return (shift->{"direction"} eq "in"); } |
---|
| 36 | sub is_outgoing { return (shift->{"direction"} eq "out"); } |
---|
| 37 | |
---|
| 38 | sub is_deleted { return shift->{"deleted"}; } |
---|
| 39 | |
---|
| 40 | sub is_admin { return (shift->{"type"} eq "admin"); } |
---|
| 41 | sub is_generic { return (shift->{"type"} eq "generic"); } |
---|
| 42 | sub is_zephyr { return (shift->{"type"} eq "zephyr"); } |
---|
| 43 | sub is_aim { return (shift->{"type"} eq "AIM"); } |
---|
| 44 | sub is_jabber { return (shift->{"type"} eq "jabber"); } |
---|
| 45 | sub is_icq { return (shift->{"type"} eq "icq"); } |
---|
| 46 | sub is_yahoo { return (shift->{"type"} eq "yahoo"); } |
---|
| 47 | sub is_msn { return (shift->{"type"} eq "msn"); } |
---|
| 48 | sub is_loopback { return (shift->{"type"} eq "loopback"); } |
---|
| 49 | |
---|
| 50 | # These are overridden by appropriate message types |
---|
| 51 | sub is_ping { return 0; } |
---|
| 52 | sub is_mail { return 0; } |
---|
[0adbce1] | 53 | sub is_personal { return BarnOwl::message_matches_filter(shift, "personal"); } |
---|
[ee183be] | 54 | sub class { return undef; } |
---|
| 55 | sub instance { return undef; } |
---|
| 56 | sub realm { return undef; } |
---|
| 57 | sub opcode { return undef; } |
---|
| 58 | sub header { return undef; } |
---|
| 59 | sub host { return undef; } |
---|
| 60 | sub hostname { return undef; } |
---|
| 61 | sub auth { return undef; } |
---|
| 62 | sub fields { return undef; } |
---|
| 63 | sub zsig { return undef; } |
---|
| 64 | sub zwriteline { return undef; } |
---|
| 65 | sub login_host { return undef; } |
---|
| 66 | sub login_tty { return undef; } |
---|
| 67 | |
---|
| 68 | # This is for back-compat with old messages that set these properties |
---|
| 69 | # New protocol implementations are encourages to user override these |
---|
| 70 | # methods. |
---|
| 71 | sub replycmd { return shift->{replycmd}}; |
---|
| 72 | sub replysendercmd { return shift->{replysendercmd}}; |
---|
| 73 | |
---|
| 74 | sub pretty_sender { return shift->sender; } |
---|
| 75 | sub pretty_recipient { return shift->recipient; } |
---|
| 76 | |
---|
| 77 | # Override if you want a context (instance, network, etc.) on personals |
---|
| 78 | sub personal_context { return ""; } |
---|
| 79 | # extra short version, for use where space is especially tight |
---|
| 80 | # (eg, the oneline style) |
---|
| 81 | sub short_personal_context { return ""; } |
---|
| 82 | |
---|
[a130fc5] | 83 | sub delete_and_expunge { |
---|
| 84 | my ($m) = @_; |
---|
| 85 | &BarnOwl::command("delete-and-expunge --quiet --id " . $m->id); |
---|
| 86 | } |
---|
| 87 | |
---|
[ee183be] | 88 | sub delete { |
---|
| 89 | my ($m) = @_; |
---|
| 90 | &BarnOwl::command("delete --id ".$m->id); |
---|
| 91 | } |
---|
| 92 | |
---|
| 93 | sub undelete { |
---|
| 94 | my ($m) = @_; |
---|
| 95 | &BarnOwl::command("undelete --id ".$m->id); |
---|
| 96 | } |
---|
| 97 | |
---|
| 98 | # Serializes the message into something similar to the zwgc->vt format |
---|
| 99 | sub serialize { |
---|
| 100 | my ($this) = @_; |
---|
| 101 | my $s; |
---|
| 102 | for my $f (keys %$this) { |
---|
| 103 | my $val = $this->{$f}; |
---|
| 104 | if (ref($val) eq "ARRAY") { |
---|
| 105 | for my $i (0..@$val-1) { |
---|
| 106 | my $aval; |
---|
| 107 | $aval = $val->[$i]; |
---|
| 108 | $aval =~ s/\n/\n$f.$i: /g; |
---|
| 109 | $s .= "$f.$i: $aval\n"; |
---|
| 110 | } |
---|
| 111 | } else { |
---|
| 112 | $val =~ s/\n/\n$f: /g; |
---|
| 113 | $s .= "$f: $val\n"; |
---|
| 114 | } |
---|
| 115 | } |
---|
| 116 | return $s; |
---|
| 117 | } |
---|
| 118 | |
---|
[7f463cf] | 119 | =head2 log MESSAGE |
---|
| 120 | |
---|
| 121 | Returns the text that should be written to a file to log C<MESSAGE>. |
---|
| 122 | |
---|
| 123 | =cut |
---|
| 124 | |
---|
| 125 | sub log { |
---|
| 126 | my ($m) = @_; |
---|
| 127 | return $m->log_header . "\n\n" . $m->log_body . "\n\n"; |
---|
| 128 | } |
---|
| 129 | |
---|
| 130 | =head2 log_header MESSAGE |
---|
| 131 | |
---|
| 132 | Returns the header of the message, for logging purposes. |
---|
| 133 | If you override L<BarnOwl::Message::log>, this method is not called. |
---|
| 134 | |
---|
| 135 | =cut |
---|
| 136 | |
---|
| 137 | sub log_header { |
---|
| 138 | my ($m) = @_; |
---|
| 139 | my $sender = $m->sender; |
---|
| 140 | my $recipient = $m->recipient; |
---|
| 141 | my $timestr = $m->time; |
---|
| 142 | return "From: <$sender> To: <$recipient>\n" |
---|
| 143 | . "Time: $timestr"; |
---|
| 144 | } |
---|
| 145 | |
---|
| 146 | =head2 log_body MESSAGE |
---|
| 147 | |
---|
| 148 | Returns the body of the message, for logging purposes. |
---|
| 149 | If you override L<BarnOwl::Message::log>, this method is not called. |
---|
| 150 | |
---|
| 151 | =cut |
---|
| 152 | |
---|
| 153 | sub log_body { |
---|
| 154 | my ($m) = @_; |
---|
| 155 | if ($m->is_loginout) { |
---|
| 156 | return uc($m->login) |
---|
| 157 | . $m->login_type |
---|
| 158 | . ($m->login_extra ? ' at ' . $m->login_extra : ''); |
---|
| 159 | } else { |
---|
| 160 | return $m->body; |
---|
| 161 | } |
---|
| 162 | } |
---|
| 163 | |
---|
[eea7bed4] | 164 | =head2 log_filenames MESSAGE |
---|
| 165 | |
---|
| 166 | Returns a list of filenames to which this message should be logged. |
---|
| 167 | The filenames should be relative to the path returned by |
---|
| 168 | C<log_base_path>. See L<BarnOwl::Logging::get_filenames> for the |
---|
| 169 | specification of valid filenames, and for what happens if this |
---|
| 170 | method returns an invalid filename. |
---|
| 171 | |
---|
| 172 | =cut |
---|
| 173 | |
---|
| 174 | sub log_filenames { |
---|
| 175 | my ($m) = @_; |
---|
| 176 | my $filename = lc($m->type); |
---|
| 177 | $filename = "unknown" if !defined $filename || $filename eq ''; |
---|
| 178 | if ($m->is_incoming && $m->pretty_sender) { |
---|
| 179 | $filename .= ":" . $m->pretty_sender; |
---|
| 180 | } elsif ($m->is_outgoing && $m->pretty_recipient) { |
---|
| 181 | $filename .= ":" . $m->pretty_recipient; |
---|
| 182 | } |
---|
| 183 | return ($filename); |
---|
| 184 | } |
---|
| 185 | |
---|
| 186 | =head2 log_to_all_file MESSAGE |
---|
| 187 | |
---|
| 188 | There is an C<all> file. This method determines if C<MESSAGE> |
---|
| 189 | should get logged to it, in addition to any files returned by |
---|
| 190 | C<log_filenames>. |
---|
| 191 | |
---|
| 192 | It defaults to returning true if and only if C<MESSAGE> is outgoing. |
---|
| 193 | |
---|
| 194 | =cut |
---|
| 195 | |
---|
| 196 | sub log_to_all_file { |
---|
| 197 | my ($m) = @_; |
---|
| 198 | return $m->is_outgoing; |
---|
| 199 | } |
---|
| 200 | |
---|
| 201 | =head2 log_base_path MESSAGE |
---|
| 202 | |
---|
| 203 | Returns the base path for logging, the folder in which all messages |
---|
| 204 | of this class get logged. |
---|
| 205 | |
---|
| 206 | Defaults to the BarnOwl variable C<logpath>. |
---|
| 207 | |
---|
| 208 | =cut |
---|
| 209 | |
---|
| 210 | sub log_base_path { |
---|
| 211 | return BarnOwl::getvar('logpath'); |
---|
| 212 | } |
---|
| 213 | |
---|
[ee183be] | 214 | # Populate the annoying legacy global variables |
---|
| 215 | sub legacy_populate_global { |
---|
| 216 | my ($m) = @_; |
---|
| 217 | $BarnOwl::direction = $m->direction ; |
---|
| 218 | $BarnOwl::type = $m->type ; |
---|
| 219 | $BarnOwl::id = $m->id ; |
---|
| 220 | $BarnOwl::class = $m->class ; |
---|
| 221 | $BarnOwl::instance = $m->instance ; |
---|
| 222 | $BarnOwl::recipient = $m->recipient ; |
---|
| 223 | $BarnOwl::sender = $m->sender ; |
---|
| 224 | $BarnOwl::realm = $m->realm ; |
---|
| 225 | $BarnOwl::opcode = $m->opcode ; |
---|
| 226 | $BarnOwl::zsig = $m->zsig ; |
---|
| 227 | $BarnOwl::msg = $m->body ; |
---|
| 228 | $BarnOwl::time = $m->time ; |
---|
| 229 | $BarnOwl::host = $m->host ; |
---|
| 230 | $BarnOwl::login = $m->login ; |
---|
| 231 | $BarnOwl::auth = $m->auth ; |
---|
| 232 | if ($m->fields) { |
---|
| 233 | @BarnOwl::fields = @{$m->fields}; |
---|
| 234 | @main::fields = @{$m->fields}; |
---|
| 235 | } else { |
---|
| 236 | @BarnOwl::fields = undef; |
---|
| 237 | @main::fields = undef; |
---|
| 238 | } |
---|
| 239 | } |
---|
| 240 | |
---|
| 241 | sub smartfilter { |
---|
| 242 | die("smartfilter not supported for this message\n"); |
---|
| 243 | } |
---|
| 244 | |
---|
| 245 | # Display fields -- overridden by subclasses when needed |
---|
| 246 | sub login_type {""} |
---|
| 247 | sub login_extra {""} |
---|
| 248 | sub long_sender {""} |
---|
| 249 | |
---|
| 250 | # The context in which a non-personal message was sent, e.g. a chat or |
---|
| 251 | # class |
---|
| 252 | sub context {""} |
---|
| 253 | |
---|
| 254 | # Some indicator of context *within* $self->context. e.g. the zephyr |
---|
| 255 | # instance |
---|
| 256 | sub subcontext {""} |
---|
| 257 | |
---|
| 258 | |
---|
| 259 | 1; |
---|