1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
|
#!/usr/bin/env perl
package JWebmail::QMAuth;
use v5.18;
use warnings;
use utf8;
use Carp;
use Encode v2.88 'decode';
use JSON::PP;
use List::Util 'min';
use POSIX 'setuid';
#use open IO => ':encoding(UTF-8)', ':std';
use Mail::Box::Maildir;
package JWebmail::QMAuth::Message::Head::Complete {
use parent 'Mail::Message::Head::Complete';
use File::Basename;
sub createMessageId {
my $self = shift;
my ($mid) = scalar(fileparse($self->message->filename)) =~ /(.+):/;
return $mid || $self->SUPER::createMessageId;
}
}
sub main {
my ($maildir, $su, $user, $mode, @args) = @ARGV;
delete $ENV{PATH};
my $netfehcom_uid = getpwnam($su);
die "won't stay as root" if $netfehcom_uid == 0;
setuid($netfehcom_uid);
if ($!) {
warn 'error setting uid';
exit(1);
}
my $folder = Mail::Box::Maildir->new(
folder => "$maildir/$user/",
type => 'maildir',
access => 'rw',
head_type => 'JWebmail::QMAuth::Message::Head::Complete',
);
my $reply = do {
if ($mode eq 'list') { list($folder, @args) }
elsif ($mode eq 'read') { read_mail($folder, @args) }
elsif ($mode eq 'count') { count_messages($folder, @args) }
elsif ($mode eq 'search') { search($folder, @args) }
elsif ($mode eq 'folders') { folders($folder, @args) }
elsif ($mode eq 'move') { move($folder, @args) }
else { {error => 'unknown mode', mode => $mode} }
};
$folder->close;
print(encode_json $reply);
if (ref $reply eq 'HASH' && $reply->{error}) {
exit 3;
}
}
sub _sort_mails {
my ($sort) = @_;
my $reverse = '';
if ($sort =~ /^!/) {
$reverse = 1;
$sort = substr $sort, 1;
}
my $sortsub = do {
if ($sort eq 'date') { sub { $a->timestamp <=> $b->timestamp } }
elsif ($sort eq 'sender') { sub { $a->from->[0] cmp $b->from->[0] } }
elsif ($sort eq 'subject') { sub { $a->subject cmp $b->subject } }
elsif ($sort eq 'size') { sub { $a->size <=> $b->size } }
elsif ($sort eq '') { sub { $a->timestamp <=> $b->timestamp } }
else {
warn "unknown sort-verb '$sort'";
sub { $a->timestamp <=> $b->timestamp }
}
};
return $reverse ? sub { $sortsub->() * -1 } : $sortsub;
}
sub _ok {
if (!shift) {
carp 'verify failed';
exit 4;
}
}
sub _get_mime_head_info {
my ($msg) = @_;
return {
content_maintype => $msg->body->mimeType->mediaType,
content_subtype => $msg->body->mimeType->subType,
content_disposition => ''.$msg->body->disposition,
filename => $msg->body->dispositionFilename,
};
}
sub _get_head_info {
my ($msg) = @_;
return {
date => _iso8601_utc($msg->timestamp),
from => _addresses($msg->from),
sender => _addresses($msg->sender),
to => _addresses($msg->to),
cc => _addresses($msg->cc),
bcc => _addresses($msg->bcc),
subject => decode('MIME-Header', $msg->subject),
comments => $msg->get('comments'),
keywords => $msg->get('keywords'),
mime => _get_mime_head_info($msg),
};
}
sub list {
my ($f, $folder, $start, $end, $sortby) = @_;
_ok($start =~ /^\d+$/aa);
_ok($end =~ /^\d+$/aa);
_ok(0 <= $start && $start <= $end);
_ok($sortby =~ /^(?:!?\w+|)$/aa);
_ok(!$folder || grep { $_ eq ".$folder" } $f->listSubFolders);
$f = $f->openSubFolder(".$folder") if $folder;
return [] if $start == $end;
my $sref = _sort_mails($sortby);
my @msgs = $f->messages;
@msgs = sort { &$sref } @msgs;
@msgs = @msgs[$start..min($#msgs, $end-1)];
return [map {
{
byte_size => $_->size,
unread => !$_->label('seen'),
date_received => $_->guessTimestamp,
message_handle => $_->messageId,
head => _get_head_info($_),
}
} @msgs];
}
sub count_messages {
my ($f, $folder) = @_;
$f = $f->openSubFolder(".$folder") if $folder;
return {
total_mails => scalar $f->messages('ALL'),
byte_size => $f->size,
unread_mails => scalar $f->messages('!seen'),
}
}
sub _iso8601_utc {
my @date_time = gmtime(shift);
$date_time[5] += 1900;
$date_time[4]++;
return sprintf('%6$04d-%5$02d-%4$02dT%3$02d:%2$02d:%1$02dZ', @date_time);
}
sub _unquote { my $x = shift; [$x =~ m/"(.*?)"(?<!\\)/]->[0] || $x }
sub _addresses {
return undef unless @_;
[map { {address => $_->address, name => _unquote(decode('MIME-Header', $_->phrase))} } @_]
}
sub _get_body {
my ($msg) = @_;
if ($msg->isNested) {
my $nested = $msg->body->nested;
return {
head => _get_head_info($nested),
body => _get_body($nested),
};
}
elsif ($msg->isMultipart) {
return {
preamble => ''.$msg->body->preamble,
parts => [map { { head => _get_mime_head_info($_), body => _get_body($_) } } $msg->parts],
epilogue => $msg->body->epilogue,
}
}
else {
return ''.$msg->decoded;
}
}
sub read_mail {
my ($f, $folder, $mid) = @_;
$f = $folder->openSubFolder(".$folder") if $folder;
my $msg = $f->find($mid);
return {error => 'no such message', mid => $mid} unless $msg;
return {
head => _get_head_info($msg),
body => _get_body($msg),
}
}
sub search {
my ($f, $search_pattern, $folder) = @_;
$f = $f->openSubFolder(".$folder") if $folder;
my @msgs = $f->messages(sub {
my ($m) = @_;
return scalar(grep { $_->decoded =~ /$search_pattern/ || (decode('MIME-Header', $_->subject)) =~ /$search_pattern/ } $m->body->parts)
if $m->isMultipart;
$m->body->decoded =~ /$search_pattern/ || (decode('MIME-Header', $m->subject)) =~ /$search_pattern/;
});
my @msgs2;
for my $msg (@msgs) {
my $msg2 = {
head => _get_head_info($msg),
body => '',
};
push @msgs2, $msg2;
}
return \@msgs2;
}
sub folders {
my $f = shift;
return [map { s/^\.//r } $f->listSubFolders(check => 1)];
}
sub move {
my ($f, $mid, $from, $to) = @_;
$f = $f->openSubFolder(".$from") if $from;
$f->find($mid)->moveTo($to ? ".$to" : $to);
return 1;
}
main unless caller;
1
__END__
=encoding utf-8
=head1 NAME
JWebmail::Model::Driver::QMailAuthuser::Extract - Maildir reader
=head1 SYNOPSIS
Extract delivers information about emails.
Runs with elevated priviliges.
=head1 DESCRIPTION
This programm is started by qmail-authuser with elevated priviliges after
a succsessful login.
Input directives are provided as command line arguments.
Output is delivered via STDOUT and log information via STDERR.
For the implemented interface see the README file.
=head1 DEPENDENCIES
Currently Mail::Box::Manager does all the hard work.
=head1 SEE ALSO
L<JWebmail::Model::ReadMails::QMailAuthuser>
=cut
|