package JWebmail::Model::ReadMails::MockMaildir; use Mojo::Base JWebmail::Model::ReadMails::QMailAuthuser; use Mojo::JSON 'decode_json'; use constant { VALID_USER => 'me@mockmaildir.com', VALID_PW => '12345', }; has user => sub { $ENV{USER} }; has maildir => 't/'; has extractor => 'perl'; our %EXTRACTORS = ( perl => 'perl script/qmauth.pl', rust => 'extract/target/debug/jwebmail-extract', ); sub verify_user { my $self = shift; my $auth = shift; return $auth->{user} eq VALID_USER && $auth->{password} eq VALID_PW; } sub build_and_run { my $self = shift; my $auth = shift; my $mode = shift; my $args = shift; my $mail_user = 'maildir'; my $exec = $EXTRACTORS{$self->extractor} . ' ' . join(' ', map { my $x = s/(['\\])/\\$1/gr; "'$x'" } ($self->maildir, $self->user, $mail_user, $mode, @$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"}; } die "error $resp" if $rc; return $resp; } 1