summaryrefslogtreecommitdiff
path: root/lib/JWebmail/Model/Driver/MockMaildir.pm
blob: e8956edf3eebc2bb30c1c4972fecb83a7c93a166 (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
package JWebmail::Model::Driver::MockMaildir;

use Mojo::Base -base;

use Mojo::JSON 'decode_json';


has user    => sub { $ENV{USER} };
has maildir => 't/';
has extractor => 'perl';


our %EXTRACTORS = (
    perl => 'perl lib/JWebmail/Model/Driver/QMailAuthuser/Extract.pm',
    rust => 'extract/target/debug/jwebmail-extract',
);

use constant {
    VALID_USER => 'me@mockmaildir.com',
    VALID_PW   => '12345',
};

sub communicate {
    my $self = shift;
    my %args = @_;

    if ($args{mode} eq 'auth') {
        return ("", 0) if $args{user} eq VALID_USER && $args{password} eq VALID_PW;
        return ("", 1);
    }

    my $mail_user = 'maildir';
    my $exec = $EXTRACTORS{$self->extractor} . ' ' . join(' ', map { $_ =~ s/(['\\])/\\$1/g; "'$_'" } ($self->maildir, $self->user, $mail_user, $args{mode}, @{$args{args}}));

    my $pid = open(my $reader, '-|', $exec)
        or die 'failed to create subprocess';

    my $input = <$reader>;

    waitpid($pid, 0);
    my $rc = $? >> 8;

    my $resp;
    if ($rc == 3 || $rc == 0) {
        eval { $resp = decode_json $input; };
        if (my $err = $@) { $resp = {error => "decoding error '$err'"}; $rc ||= 1; };
    }
    elsif ($rc) {
        $resp = {error => "qmail-authuser returned code: $rc"};
    }

    return ($resp, $rc);
}


1