1 | use strict; |
---|
2 | use warnings; |
---|
3 | |
---|
4 | package BarnOwl::Message; |
---|
5 | |
---|
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 | |
---|
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"}; } |
---|
24 | sub unix_time { return shift->{"unix_time"}; } |
---|
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; } |
---|
53 | sub is_personal { return shift->is_private; } |
---|
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 | |
---|
83 | sub delete_and_expunge { |
---|
84 | my ($m) = @_; |
---|
85 | &BarnOwl::command("delete-and-expunge --quiet --id " . $m->id); |
---|
86 | } |
---|
87 | |
---|
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 | |
---|
119 | # Populate the annoying legacy global variables |
---|
120 | sub legacy_populate_global { |
---|
121 | my ($m) = @_; |
---|
122 | $BarnOwl::direction = $m->direction ; |
---|
123 | $BarnOwl::type = $m->type ; |
---|
124 | $BarnOwl::id = $m->id ; |
---|
125 | $BarnOwl::class = $m->class ; |
---|
126 | $BarnOwl::instance = $m->instance ; |
---|
127 | $BarnOwl::recipient = $m->recipient ; |
---|
128 | $BarnOwl::sender = $m->sender ; |
---|
129 | $BarnOwl::realm = $m->realm ; |
---|
130 | $BarnOwl::opcode = $m->opcode ; |
---|
131 | $BarnOwl::zsig = $m->zsig ; |
---|
132 | $BarnOwl::msg = $m->body ; |
---|
133 | $BarnOwl::time = $m->time ; |
---|
134 | $BarnOwl::host = $m->host ; |
---|
135 | $BarnOwl::login = $m->login ; |
---|
136 | $BarnOwl::auth = $m->auth ; |
---|
137 | if ($m->fields) { |
---|
138 | @BarnOwl::fields = @{$m->fields}; |
---|
139 | @main::fields = @{$m->fields}; |
---|
140 | } else { |
---|
141 | @BarnOwl::fields = undef; |
---|
142 | @main::fields = undef; |
---|
143 | } |
---|
144 | } |
---|
145 | |
---|
146 | sub smartfilter { |
---|
147 | die("smartfilter not supported for this message\n"); |
---|
148 | } |
---|
149 | |
---|
150 | # Display fields -- overridden by subclasses when needed |
---|
151 | sub login_type {""} |
---|
152 | sub login_extra {""} |
---|
153 | sub long_sender {""} |
---|
154 | |
---|
155 | # The context in which a non-personal message was sent, e.g. a chat or |
---|
156 | # class |
---|
157 | sub context {""} |
---|
158 | |
---|
159 | # Some indicator of context *within* $self->context. e.g. the zephyr |
---|
160 | # instance |
---|
161 | sub subcontext {""} |
---|
162 | |
---|
163 | |
---|
164 | 1; |
---|