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