source: perl/lib/BarnOwl/Message.pm @ 9410677

Last change on this file since 9410677 was e08487a, checked in by Jason Gross <jgross@mit.edu>, 7 years ago
Moved log generation code to perl.
  • Property mode set to 100644
File size: 5.8 KB
Line 
1use strict;
2use warnings;
3
4package BarnOwl::Message;
5
6use BarnOwl::Message::Admin;
7use BarnOwl::Message::AIM;
8use BarnOwl::Message::Generic;
9use BarnOwl::Message::Loopback;
10use BarnOwl::Message::Zephyr;
11
12sub 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
21sub type        { return shift->{"type"}; }
22sub direction   { return shift->{"direction"}; }
23sub time        { return shift->{"time"}; }
24sub unix_time   { return shift->{"unix_time"}; }
25sub id          { return shift->{"id"}; }
26sub body        { return shift->{"body"}; }
27sub sender      { return shift->{"sender"}; }
28sub recipient   { return shift->{"recipient"}; }
29sub login       { return shift->{"login"}; }
30sub is_private  { return shift->{"private"}; }
31
32sub is_login    { return shift->login eq "login"; }
33sub is_logout   { return shift->login eq "logout"; }
34sub is_loginout { my $m=shift; return ($m->is_login or $m->is_logout); }
35sub is_incoming { return (shift->{"direction"} eq "in"); }
36sub is_outgoing { return (shift->{"direction"} eq "out"); }
37
38sub is_deleted  { return shift->{"deleted"}; }
39
40sub is_admin    { return (shift->{"type"} eq "admin"); }
41sub is_generic  { return (shift->{"type"} eq "generic"); }
42sub is_zephyr   { return (shift->{"type"} eq "zephyr"); }
43sub is_aim      { return (shift->{"type"} eq "AIM"); }
44sub is_jabber   { return (shift->{"type"} eq "jabber"); }
45sub is_icq      { return (shift->{"type"} eq "icq"); }
46sub is_yahoo    { return (shift->{"type"} eq "yahoo"); }
47sub is_msn      { return (shift->{"type"} eq "msn"); }
48sub is_loopback { return (shift->{"type"} eq "loopback"); }
49
50# These are overridden by appropriate message types
51sub is_ping     { return 0; }
52sub is_mail     { return 0; }
53sub is_personal { return BarnOwl::message_matches_filter(shift, "personal"); }
54sub class       { return undef; }
55sub instance    { return undef; }
56sub realm       { return undef; }
57sub opcode      { return undef; }
58sub header      { return undef; }
59sub host        { return undef; }
60sub hostname    { return undef; }
61sub auth        { return undef; }
62sub fields      { return undef; }
63sub zsig        { return undef; }
64sub zwriteline  { return undef; }
65sub login_host  { return undef; }
66sub 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.
71sub replycmd         { return shift->{replycmd}};
72sub replysendercmd   { return shift->{replysendercmd}};
73
74sub pretty_sender    { return shift->sender; }
75sub pretty_recipient { return shift->recipient; }
76
77# Override if you want a context (instance, network, etc.) on personals
78sub personal_context { return ""; }
79# extra short version, for use where space is especially tight
80# (eg, the oneline style)
81sub short_personal_context { return ""; }
82
83sub delete_and_expunge {
84    my ($m) = @_;
85    &BarnOwl::command("delete-and-expunge --quiet --id " . $m->id);
86}
87
88sub delete {
89    my ($m) = @_;
90    &BarnOwl::command("delete --id ".$m->id);
91}
92
93sub 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
99sub 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
119=head2 log MESSAGE
120
121Returns the text that should be written to a file to log C<MESSAGE>.
122
123=cut
124
125sub log {
126    my ($m) = @_;
127    return $m->log_header . "\n\n" . $m->log_body . "\n\n";
128}
129
130=head2 log_header MESSAGE
131
132Returns the header of the message, for logging purposes.
133If you override L<BarnOwl::Message::log>, this method is not called.
134
135=cut
136
137sub 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
148Returns the body of the message, for logging purposes.
149If you override L<BarnOwl::Message::log>, this method is not called.
150
151=cut
152
153sub 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
164# Populate the annoying legacy global variables
165sub legacy_populate_global {
166    my ($m) = @_;
167    $BarnOwl::direction  = $m->direction ;
168    $BarnOwl::type       = $m->type      ;
169    $BarnOwl::id         = $m->id        ;
170    $BarnOwl::class      = $m->class     ;
171    $BarnOwl::instance   = $m->instance  ;
172    $BarnOwl::recipient  = $m->recipient ;
173    $BarnOwl::sender     = $m->sender    ;
174    $BarnOwl::realm      = $m->realm     ;
175    $BarnOwl::opcode     = $m->opcode    ;
176    $BarnOwl::zsig       = $m->zsig      ;
177    $BarnOwl::msg        = $m->body      ;
178    $BarnOwl::time       = $m->time      ;
179    $BarnOwl::host       = $m->host      ;
180    $BarnOwl::login      = $m->login     ;
181    $BarnOwl::auth       = $m->auth      ;
182    if ($m->fields) {
183        @BarnOwl::fields = @{$m->fields};
184        @main::fields = @{$m->fields};
185    } else {
186        @BarnOwl::fields = undef;
187        @main::fields = undef;
188    }
189}
190
191sub smartfilter {
192    die("smartfilter not supported for this message\n");
193}
194
195# Display fields -- overridden by subclasses when needed
196sub login_type {""}
197sub login_extra {""}
198sub long_sender {""}
199
200# The context in which a non-personal message was sent, e.g. a chat or
201# class
202sub context {""}
203
204# Some indicator of context *within* $self->context. e.g. the zephyr
205# instance
206sub subcontext {""}
207
208
2091;
Note: See TracBrowser for help on using the repository browser.