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 { |
---|
84 | my ($m) = @_; |
---|
85 | &BarnOwl::command("delete --id ".$m->id); |
---|
86 | } |
---|
87 | |
---|
88 | sub undelete { |
---|
89 | my ($m) = @_; |
---|
90 | &BarnOwl::command("undelete --id ".$m->id); |
---|
91 | } |
---|
92 | |
---|
93 | # Serializes the message into something similar to the zwgc->vt format |
---|
94 | sub serialize { |
---|
95 | my ($this) = @_; |
---|
96 | my $s; |
---|
97 | for my $f (keys %$this) { |
---|
98 | my $val = $this->{$f}; |
---|
99 | if (ref($val) eq "ARRAY") { |
---|
100 | for my $i (0..@$val-1) { |
---|
101 | my $aval; |
---|
102 | $aval = $val->[$i]; |
---|
103 | $aval =~ s/\n/\n$f.$i: /g; |
---|
104 | $s .= "$f.$i: $aval\n"; |
---|
105 | } |
---|
106 | } else { |
---|
107 | $val =~ s/\n/\n$f: /g; |
---|
108 | $s .= "$f: $val\n"; |
---|
109 | } |
---|
110 | } |
---|
111 | return $s; |
---|
112 | } |
---|
113 | |
---|
114 | # Populate the annoying legacy global variables |
---|
115 | sub legacy_populate_global { |
---|
116 | my ($m) = @_; |
---|
117 | $BarnOwl::direction = $m->direction ; |
---|
118 | $BarnOwl::type = $m->type ; |
---|
119 | $BarnOwl::id = $m->id ; |
---|
120 | $BarnOwl::class = $m->class ; |
---|
121 | $BarnOwl::instance = $m->instance ; |
---|
122 | $BarnOwl::recipient = $m->recipient ; |
---|
123 | $BarnOwl::sender = $m->sender ; |
---|
124 | $BarnOwl::realm = $m->realm ; |
---|
125 | $BarnOwl::opcode = $m->opcode ; |
---|
126 | $BarnOwl::zsig = $m->zsig ; |
---|
127 | $BarnOwl::msg = $m->body ; |
---|
128 | $BarnOwl::time = $m->time ; |
---|
129 | $BarnOwl::host = $m->host ; |
---|
130 | $BarnOwl::login = $m->login ; |
---|
131 | $BarnOwl::auth = $m->auth ; |
---|
132 | if ($m->fields) { |
---|
133 | @BarnOwl::fields = @{$m->fields}; |
---|
134 | @main::fields = @{$m->fields}; |
---|
135 | } else { |
---|
136 | @BarnOwl::fields = undef; |
---|
137 | @main::fields = undef; |
---|
138 | } |
---|
139 | } |
---|
140 | |
---|
141 | sub smartfilter { |
---|
142 | die("smartfilter not supported for this message\n"); |
---|
143 | } |
---|
144 | |
---|
145 | # Display fields -- overridden by subclasses when needed |
---|
146 | sub login_type {""} |
---|
147 | sub login_extra {""} |
---|
148 | sub long_sender {""} |
---|
149 | |
---|
150 | # The context in which a non-personal message was sent, e.g. a chat or |
---|
151 | # class |
---|
152 | sub context {""} |
---|
153 | |
---|
154 | # Some indicator of context *within* $self->context. e.g. the zephyr |
---|
155 | # instance |
---|
156 | sub subcontext {""} |
---|
157 | |
---|
158 | |
---|
159 | 1; |
---|