summaryrefslogtreecommitdiff
path: root/lib/JWebmail/Controller/Webmail.pm
diff options
context:
space:
mode:
Diffstat (limited to 'lib/JWebmail/Controller/Webmail.pm')
-rw-r--r--lib/JWebmail/Controller/Webmail.pm54
1 files changed, 24 insertions, 30 deletions
diff --git a/lib/JWebmail/Controller/Webmail.pm b/lib/JWebmail/Controller/Webmail.pm
index f0d45b3..d01abae 100644
--- a/lib/JWebmail/Controller/Webmail.pm
+++ b/lib/JWebmail/Controller/Webmail.pm
@@ -1,10 +1,15 @@
package JWebmail::Controller::Webmail;
-use Mojo::Base 'Mojolicious::Controller';
+use Mojo::Base Mojolicious::Controller;
+
+use List::Util 'first';
use Mojolicious::Types;
-use constant S_USER => 'user'; # Key for user name in active session
+use constant {
+ S_USER => 'user', # Key for user name in active session
+ ST_AUTH => 'auth',
+};
# no action has been taken, display login page
@@ -35,7 +40,7 @@ sub auth {
my $authConf = {user => $user, password => $pw};
$authConf->{challenge} = $self->app->secrets->[0] if $self->config->{session}{secure} eq 'cram';
- $self->stash(auth => $self->users->Auth($authConf));
+ $self->stash(ST_AUTH() => $self->users->Auth($authConf));
return 1;
}
@@ -118,7 +123,7 @@ sub displayheaders {
no warnings 'experimental::smartmatch';
my $self = shift;
- my $auth = $self->stash('auth');
+ my $auth = $self->stash(ST_AUTH);
my $folders = _time { $self->users->folders($auth) } $self, 'user folders';
@@ -136,6 +141,7 @@ sub displayheaders {
my $search = $v->optional('search')->param;
if ($v->has_error) {
+ local $" = ' ';
$self->render(template => 'error', error => "errors in @{ $v->failed }", status => 400);
return;
}
@@ -178,7 +184,7 @@ sub readmail {
my $mid = $self->stash('id');
- my $auth = $self->stash('auth');
+ my $auth = $self->stash(ST_AUTH);
my $mail;
eval { $mail = $self->users->show($auth, $mid) };
@@ -190,26 +196,14 @@ sub readmail {
die $@;
}
- $self->render(msg => $mail);
-}
-
-
-sub raw {
- my $self = shift;
-
- my $mid = $self->stash('id');
-
- my $auth = $self->stash('auth');
-
- my $mail = $self->users->show($auth, $mid);
-
+ # select a single body element
my $v = $self->validation;
- $v->optional('body')->like(qr/\w+/);
+ my $type = $v->optional('body')->like(qr(^[\w\-/; ]+$)a)->param;
return if $v->has_error;
- if (my $type = $self->param('body')) {
+ if ($type) {
if ($mail->{head}{content_type} =~ '^multipart/') {
- my ($content) = grep {$_->{head}{content_type} =~ $type} @{ $mail->{body} };
+ my $content = first {$_->{head}{content_type} =~ $type} @{ $mail->{body} };
$self->render(text => $content->{body});
}
elsif ($mail->{head}{content_type} =~ $type) {
@@ -218,10 +212,13 @@ sub raw {
else {
$self->reply->not_found;
}
+ return;
}
- else {
- $self->render(json => $mail);
- }
+
+ $self->respond_to(
+ html => {msg => $mail},
+ json => {json => $mail}
+ );
}
@@ -242,7 +239,7 @@ sub sendmail {
bcc => scalar $v->optional('bcc', 'not_empty')->check('mail_line')->every_param,
reply => scalar $v->optional('back_to', 'not_empty')->check('mail_line')->param,
attach => scalar $v->optional('attach', 'non_empty_ul')->upload->param,
- from => scalar $self->session(S_USER),
+ from => scalar $self->stash(ST_AUTH)->{user},
);
$mail{attach_type} = Mojolicious::Types->new->file_type($mail{attach}->filename) if $mail{attach};
@@ -283,7 +280,7 @@ sub move {
return;
}
- my $auth = $self->stash('auth');
+ my $auth = $self->stash(ST_AUTH);
my $folders = $self->users->folders($auth);
my $mm = $self->every_param('mail');
@@ -351,6 +348,7 @@ Provides an overview over messages.
=head2 readmail
Displays a single mail.
+Can also be displayed as JSON or only one body part as the appropriate content type.
=head2 writemail
@@ -364,10 +362,6 @@ Sends a mail written in writemail.
Moves mails between mail forlders.
-=head2 raw
-
-Displays the mail raw, ready to be downloaded.
-
=head1 DEPENCIES
Mojolicious and File::Type