summaryrefslogtreecommitdiff
path: root/lib/JWebmail/Model/Driver/QMailAuthuser/Extract.pm
blob: a59e265880e0fd9c0f4ae4029bb2622adf382738 (plain)
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
#!/usr/bin/env perl
package JWebmail::Model::Driver::QMailAuthuser::Extract;

use v5.18;
use strict;
use warnings;
use utf8;

use POSIX ();
use JSON::PP;
use Carp;
use List::Util 'min';
use Encode v2.88 'decode';

#use open IO => ':encoding(UTF-8)', ':std';
no warnings 'experimental::smartmatch';

use Mail::Box::Manager;

use constant ROOT_MAILDIR => '.';


sub main {
    my ($maildir) = shift(@ARGV) =~ m/(.*)/;
    my ($su)      = shift(@ARGV) =~ m/(.*)/;
    my ($user)    = shift(@ARGV) =~ m/([[:alpha:]]+)/;
    my $mode      = shift @ARGV; _ok($mode =~ m/([[:alpha:]-]{1,20})/);
    my @args = @ARGV;

    delete $ENV{PATH};

    my $netfehcom_uid = getpwnam($su);
    #$> = $netfehcom_uid;
    die "won't stay as root" if $netfehcom_uid == 0;
    POSIX::setuid($netfehcom_uid);
    if ($!) {
        warn 'error setting uid';
        exit(1);
    }

    my $folder = Mail::Box::Manager->new->open(
        folder => "$maildir/$user/",
        type => 'maildir',
        access => 'rw',
    );

    my $reply = do {
        given ($mode) {
            when('list')      { list($folder, @args) }
            when('read-mail') { read_mail($folder, @args) }
            when('count')     { count_messages($folder, @args) }
            when('search')    { search($folder, @args) }
            when('folders')   { folders($folder, @args) }
            when('move')      { move($folder, @args) }
            default           { {error => 'unkown mode', mode => $mode} }
        }
    };
    $folder->close;

    print(encode_json $reply);
    if (ref $reply eq 'HASH' && $reply->{error}) {
        exit 3;
    }
}


sub _sort_mails {
    my $sort = shift // '';
    my $reverse = 1;

    if ($sort =~ m/^!/) {
        $reverse = -1;
        $sort = substr $sort, 1;
    }

    given ($sort) {
        when ('date')    { return sub { ($a->timestamp <=> $b->timestamp) * $reverse } }
        when ('sender')  { return sub { ($a->from->[0] cmp $b->from->[0]) * $reverse } }
        when ('subject') { return sub { ($a->subject cmp $b->subject) * $reverse } }
        when ('size')    { return sub { ($a->size <=> $b->size) * $reverse } }
        when ('')        { return sub { ($a->timestamp <=> $b->timestamp) * $reverse } }
        default          { warn "unkown sort-verb '$sort'"; return sub { ($a->timestamp <=> $b->timestamp) * $reverse } }
    }
}


sub _ok {
    if (!shift) {
        carp 'verify failed';
        exit 4;
    }
}


sub list {
    my ($f, $start, $end, $sortby, $folder) = @_;
    $folder = ".$folder";

    _ok($start =~ m/^\d+$/);
    _ok($end =~ m/^\d+$/);
    _ok(0 <= $start && $start <= $end);
    _ok($sortby =~ m/^(!?\w+|\w*)$/n);
    _ok($folder ~~ [$f->listSubFolders, ROOT_MAILDIR]);

    $f = $f->openSubFolder($folder) if $folder ne ROOT_MAILDIR;

    return [] if $start == $end;

    my $sref = _sort_mails($sortby);
    my @msgs = $f->messages;
    @msgs = sort { &$sref } @msgs;
    @msgs = @msgs[$start..min($#msgs, $end)];

    my @msgs2;

    for my $msg (@msgs) {
        my $msg2 = {
            mid  => $msg->messageId,
            size => $msg->size,
            new  => $msg->label('seen'),
            head => {
                subject       => decode('MIME-Header', $msg->subject),
                from          => _addresses($msg->from),
                to            => _addresses($msg->to),
                cc            => _addresses($msg->cc),
                bcc           => _addresses($msg->bcc),
                date          => _iso8601_utc($msg->timestamp),
                content_type  => ''.$msg->contentType,
            },
        };
        push @msgs2, $msg2;
    }

    return \@msgs2;
}


sub count_messages {
    my ($f, $folder) = @_;
    $folder = ".$folder";

    _ok($folder ~~ [$f->listSubFolders, ROOT_MAILDIR]);

    $f = $f->openSubFolder($folder) if $folder ne ROOT_MAILDIR;

    return {
        count => scalar($f->messages('ALL')),
        size => $f->size,
        new => 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 {
    [map { {address => $_->address, name => _unquote(decode('MIME-Header', $_->phrase))} } @_]
}


sub read_mail {
    my ($folder, $mid) = @_;

    my $msg = $folder->find($mid);
    return {error => 'no such message', mid => $mid} unless $msg;
    return {
        size => $msg->size,
        head => {
            subject      => decode('MIME-Header', $msg->subject),
            from         => _addresses($msg->from),
            to           => _addresses($msg->to),
            cc           => _addresses($msg->cc),
            bcc          => _addresses($msg->bcc),
            date         => _iso8601_utc($msg->timestamp),
            content_type => ''. $msg->contentType,
        },
        body => do {
            if ($msg->isMultipart) {
                [map {{type => ''. $_->contentType, val => '' . $_->decoded}} $msg->body->parts]
            }
            else {
                '' . $msg->body->decoded
            }
        },
    }
}


sub search {
    my ($f, $search_pattern, $folder) = @_;
    $folder = ".$folder";

    $f = $f->openSubFolder($folder) if $folder ne ROOT_MAILDIR;

    my @msgs = $f->messages(sub {
        my $m = shift;

        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 = {
            size => $msg->size,
            mid  => $msg->messageId,
            head => {
                subject      => decode('MIME-Header', $msg->subject),
                from         => _addresses($msg->from),
                to           => _addresses($msg->to),
                cc           => _addresses($msg->cc),
                bcc          => _addresses($msg->bcc),
                date         => _iso8601_utc($msg->timestamp),
                content_type => ''. $msg->contentType,
            },
        };
        push @msgs2, $msg2;
    }

    return \@msgs2;
}


sub folders {
    my $f = shift;

    return [grep { $_ =~ m/^\./ && $_ =~ s/\.// } $f->listSubFolders];
}


sub move {
    my ($f, $mid, $dst) = @_;
    $dst = ".$dst";

    _ok($dst ~~ [$f->listSubFolders, ROOT_MAILDIR]);

    $f->moveMessage($dst, $dst->find($mid));
}


main() if !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.

=head1 ARGUMENTS

  prog <maildir> <system-user> <mail-user> <mode> <args...>

=head2 Modes

  list <start> <end> <sort-by> <folder>
  count <folder>
  read-mail <mid>
  search <pattern> <folder>
  folders
  move <mid> <dst-folder>

All arguments must be supplied for a given mode even if empty (as '').

=head1 DEPENDENCIES

Currently Mail::Box::Manager does all the hard work.

=head1 SEE ALSO

L<JWebmail::Model::Driver::QMailAuthuser>

=cut