1 | use strict; |
---|
2 | use warnings; |
---|
3 | |
---|
4 | package BarnOwl::Message::Zephyr; |
---|
5 | |
---|
6 | use constant WEBZEPHYR_PRINCIPAL => "daemon/webzephyr.mit.edu"; |
---|
7 | use constant WEBZEPHYR_CLASS => "webzephyr"; |
---|
8 | use constant WEBZEPHYR_OPCODE => "webzephyr"; |
---|
9 | |
---|
10 | use base qw( BarnOwl::Message ); |
---|
11 | |
---|
12 | sub strip_realm { |
---|
13 | my $sender = shift; |
---|
14 | my $realm = BarnOwl::zephyr_getrealm(); |
---|
15 | $sender =~ s/\@\Q$realm\E$//; |
---|
16 | return $sender; |
---|
17 | } |
---|
18 | |
---|
19 | sub principal_realm { |
---|
20 | my $principal = shift; |
---|
21 | my ($user, $realm) = split(/@/,$principal); |
---|
22 | return $realm; |
---|
23 | } |
---|
24 | |
---|
25 | sub login_type { |
---|
26 | return (shift->zsig eq "") ? "(PSEUDO)" : ""; |
---|
27 | } |
---|
28 | |
---|
29 | sub login_extra { |
---|
30 | my $m = shift; |
---|
31 | return undef if (!$m->is_loginout); |
---|
32 | my $s = lc($m->host); |
---|
33 | $s .= " " . $m->login_tty if defined $m->login_tty; |
---|
34 | return $s; |
---|
35 | } |
---|
36 | |
---|
37 | sub long_sender { |
---|
38 | my $m = shift; |
---|
39 | return $m->zsig; |
---|
40 | } |
---|
41 | |
---|
42 | sub context { |
---|
43 | return shift->class; |
---|
44 | } |
---|
45 | |
---|
46 | sub subcontext { |
---|
47 | return shift->instance; |
---|
48 | } |
---|
49 | |
---|
50 | sub login_tty { |
---|
51 | my ($m) = @_; |
---|
52 | return undef if (!$m->is_loginout); |
---|
53 | return undef if (!defined($m->fields)); |
---|
54 | return $m->fields->[2]; |
---|
55 | } |
---|
56 | |
---|
57 | sub login_host { |
---|
58 | my ($m) = @_; |
---|
59 | return undef if (!$m->is_loginout); |
---|
60 | return undef if (!defined($m->fields)); |
---|
61 | return $m->fields->[0]; |
---|
62 | } |
---|
63 | |
---|
64 | sub zwriteline { return shift->{"zwriteline"}; } |
---|
65 | |
---|
66 | sub is_ping { return (lc(shift->opcode) eq "ping"); } |
---|
67 | |
---|
68 | sub is_mail { |
---|
69 | my ($m) = @_; |
---|
70 | return ((lc($m->class) eq "mail") && $m->is_private); |
---|
71 | } |
---|
72 | |
---|
73 | sub pretty_sender { |
---|
74 | my ($m) = @_; |
---|
75 | return strip_realm($m->sender); |
---|
76 | } |
---|
77 | |
---|
78 | sub pretty_recipient { |
---|
79 | my ($m) = @_; |
---|
80 | return strip_realm($m->recipient); |
---|
81 | } |
---|
82 | |
---|
83 | # Portion of the reply command that preserves the context |
---|
84 | sub context_reply_cmd { |
---|
85 | my $mclass = shift; |
---|
86 | my $minstance = shift; |
---|
87 | my @class; |
---|
88 | if (lc($mclass) ne "message") { |
---|
89 | @class = ('-c', $mclass); |
---|
90 | } |
---|
91 | my @instance; |
---|
92 | if (lc($minstance) ne "personal") { |
---|
93 | @instance = ('-i', $minstance); |
---|
94 | } |
---|
95 | return (@class, @instance); |
---|
96 | } |
---|
97 | |
---|
98 | sub personal_context { |
---|
99 | my ($m) = @_; |
---|
100 | return BarnOwl::quote(context_reply_cmd($m->class, $m->instance)); |
---|
101 | } |
---|
102 | |
---|
103 | sub short_personal_context { |
---|
104 | my ($m) = @_; |
---|
105 | if(lc($m->class) eq 'message') |
---|
106 | { |
---|
107 | if(lc($m->instance) eq 'personal') |
---|
108 | { |
---|
109 | return ''; |
---|
110 | } else { |
---|
111 | return $m->instance; |
---|
112 | } |
---|
113 | } else { |
---|
114 | return $m->class; |
---|
115 | } |
---|
116 | } |
---|
117 | |
---|
118 | # These are arguably zephyr-specific |
---|
119 | sub class { return shift->{"class"}; } |
---|
120 | sub instance { return shift->{"instance"}; } |
---|
121 | sub realm { return shift->{"realm"}; } |
---|
122 | sub opcode { return shift->{"opcode"}; } |
---|
123 | sub host { return shift->{"hostname"}; } |
---|
124 | sub hostname { return shift->{"hostname"}; } |
---|
125 | sub header { return shift->{"header"}; } |
---|
126 | sub auth { return shift->{"auth"}; } |
---|
127 | sub fields { return shift->{"fields"}; } |
---|
128 | sub zsig { return shift->{"zsig"}; } |
---|
129 | |
---|
130 | sub zephyr_cc { |
---|
131 | my $self = shift; |
---|
132 | return $1 if $self->body =~ /^\s*cc:\s+([^\n]+)/i; |
---|
133 | return undef; |
---|
134 | } |
---|
135 | |
---|
136 | sub replycmd { |
---|
137 | my $self = shift; |
---|
138 | my $sender = shift; |
---|
139 | $sender = 0 unless defined $sender; |
---|
140 | my ($class, $instance, $to, $cc); |
---|
141 | if($self->is_outgoing) { |
---|
142 | return $self->{zwriteline}; |
---|
143 | } |
---|
144 | |
---|
145 | if($sender && $self->opcode eq WEBZEPHYR_OPCODE) { |
---|
146 | $class = WEBZEPHYR_CLASS; |
---|
147 | $instance = $self->pretty_sender; |
---|
148 | $instance =~ s/-webzephyr$//; |
---|
149 | $to = WEBZEPHYR_PRINCIPAL; |
---|
150 | } elsif($self->class eq WEBZEPHYR_CLASS |
---|
151 | && $self->is_loginout) { |
---|
152 | $class = WEBZEPHYR_CLASS; |
---|
153 | $instance = $self->instance; |
---|
154 | $to = WEBZEPHYR_PRINCIPAL; |
---|
155 | } elsif($self->is_loginout) { |
---|
156 | $class = 'MESSAGE'; |
---|
157 | $instance = 'PERSONAL'; |
---|
158 | $to = $self->sender; |
---|
159 | } elsif($sender && !$self->is_private) { |
---|
160 | # Possible future feature: (Optionally?) include the class and/or |
---|
161 | # instance of the message being replied to in the instance of the |
---|
162 | # outgoing personal reply |
---|
163 | $class = 'MESSAGE'; |
---|
164 | $instance = 'PERSONAL'; |
---|
165 | $to = $self->sender; |
---|
166 | } else { |
---|
167 | $class = $self->class; |
---|
168 | $instance = $self->instance; |
---|
169 | if ($self->recipient eq '' || $self->recipient =~ /^@/) { |
---|
170 | $to = $self->recipient; |
---|
171 | } else { |
---|
172 | $to = $self->sender; |
---|
173 | $cc = $self->zephyr_cc(); |
---|
174 | } |
---|
175 | } |
---|
176 | |
---|
177 | my @cmd; |
---|
178 | if(lc $self->opcode eq 'crypt' and ( not $sender or $self->is_private)) { |
---|
179 | # Responses to zcrypted messages should be zcrypted, so long as we |
---|
180 | # aren't switching to personals |
---|
181 | @cmd = ('zcrypt'); |
---|
182 | } else { |
---|
183 | @cmd = ('zwrite'); |
---|
184 | } |
---|
185 | |
---|
186 | push @cmd, context_reply_cmd($class, $instance); |
---|
187 | |
---|
188 | if ($to ne '') { |
---|
189 | $to = strip_realm($to); |
---|
190 | if (defined $cc and not $sender) { |
---|
191 | my @cc = grep /^[^-]/, ($to, split /\s+/, $cc); |
---|
192 | my %cc = map {$_ => 1} @cc; |
---|
193 | # this isn't quite right - it doesn't strip off the |
---|
194 | # user if the message was addressed to them by fully qualified |
---|
195 | # name |
---|
196 | delete $cc{strip_realm(BarnOwl::zephyr_getsender())}; |
---|
197 | @cc = keys %cc; |
---|
198 | |
---|
199 | my $sender_realm = principal_realm($self->sender); |
---|
200 | if (BarnOwl::zephyr_getrealm() ne $sender_realm) { |
---|
201 | @cc = map { |
---|
202 | if($_ !~ /@/) { |
---|
203 | "${_}\@${sender_realm}"; |
---|
204 | } else { |
---|
205 | $_; |
---|
206 | } |
---|
207 | } @cc; |
---|
208 | } |
---|
209 | push @cmd, '-C', @cc; |
---|
210 | } else { |
---|
211 | if(BarnOwl::getvar('smartstrip') eq 'on') { |
---|
212 | $to = BarnOwl::zephyr_smartstrip_user($to); |
---|
213 | } |
---|
214 | push @cmd, $to; |
---|
215 | } |
---|
216 | } |
---|
217 | return BarnOwl::quote(@cmd); |
---|
218 | } |
---|
219 | |
---|
220 | sub replysendercmd { |
---|
221 | my $self = shift; |
---|
222 | return $self->replycmd(1); |
---|
223 | } |
---|
224 | |
---|
225 | # Logging |
---|
226 | sub log_header { |
---|
227 | my ($m) = @_; |
---|
228 | my $class = $m->class; |
---|
229 | my $instance = $m->instance; |
---|
230 | my $opcode = $m->opcode; |
---|
231 | my $timestr = $m->time; |
---|
232 | my $host = $m->host; |
---|
233 | my $sender = $m->pretty_sender; |
---|
234 | my $zsig = $m->zsig; |
---|
235 | my $rtn = "Class: $class Instance: $instance"; |
---|
236 | $rtn .= " Opcode: $opcode" unless !defined $opcode || $opcode eq ''; |
---|
237 | $rtn .= "\nTime: $timestr Host: $host" |
---|
238 | . "\nFrom: $zsig <$sender>"; |
---|
239 | return $rtn; |
---|
240 | } |
---|
241 | |
---|
242 | 1; |
---|