diff options
author | Jannis M. Hoffmann <jannis.hoffmann@rwth-aachen.de> | 2022-05-01 21:58:10 +0200 |
---|---|---|
committer | Jannis M. Hoffmann <jannis.hoffmann@rwth-aachen.de> | 2022-05-01 21:58:10 +0200 |
commit | 883a3c9379de774a3035986e90614decd8626207 (patch) | |
tree | 3e75f3d82f560f446f670faff78c927eacbfa63e /lib/JWebmail | |
parent | 8387d8eb466e1187ee6caeaeb773d67652797731 (diff) |
merged read and raw routes
Diffstat (limited to 'lib/JWebmail')
-rw-r--r-- | lib/JWebmail/Controller/Webmail.pm | 54 | ||||
-rw-r--r-- | lib/JWebmail/Model/ReadMails/MockJSON.pm | 2 | ||||
-rw-r--r-- | lib/JWebmail/Plugin/Helper.pm | 10 |
3 files changed, 30 insertions, 36 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 diff --git a/lib/JWebmail/Model/ReadMails/MockJSON.pm b/lib/JWebmail/Model/ReadMails/MockJSON.pm index 8918040..f77cd2a 100644 --- a/lib/JWebmail/Model/ReadMails/MockJSON.pm +++ b/lib/JWebmail/Model/ReadMails/MockJSON.pm @@ -98,7 +98,7 @@ sub show { return $mail; } else { - die 'unkown mail-id'; + die 'unknown mail-id'; } } diff --git a/lib/JWebmail/Plugin/Helper.pm b/lib/JWebmail/Plugin/Helper.pm index 3281d26..be147fe 100644 --- a/lib/JWebmail/Plugin/Helper.pm +++ b/lib/JWebmail/Plugin/Helper.pm @@ -113,7 +113,7 @@ my $render_text_plain = sub { my $render_text_html = sub { my $c_ = shift; - return '<iframe src="' . $c_->url_for('rawid', id => $c_->stash('id'))->query(body => 'html') . '" class=html-mail></iframe>'; + return '<iframe src="' . $c_->url_for('read', id => $c_->stash('id'))->query(body => 'html') . '" class=html-mail></iframe>'; }; @@ -355,7 +355,7 @@ Helper - Functions used as helpers in controller and templates and additional va L<JWebmail::Helper> provides useful helper functions and validator cheks and filter for L<JWebmail::Controller::All> and various templates. -=head1 FUNCTIONS +=head1 HELPERS =head2 mail_line @@ -393,7 +393,7 @@ This is not registered by default. =head2 paginate -A helper for calculationg page bounds. +A helper for calculating page bounds. Takes the total number of items as argument. @@ -426,7 +426,7 @@ The output is valid html and should not be escaped. =head2 session_passwd -A helper used to set and get the session password. The behaivour can be altered by +A helper used to set and get the session password. The behavior can be altered by setting the config variable C<< session => {secure => 's3d'} >>. $app->helper(session_passwd => \&JWebmail::Plugin::Helper::session_passwd); @@ -454,7 +454,7 @@ data is stored on the server. Additionally the password is encrypted by an one-t =head1 DEPENDENCIES -Mojolicious, Crypt::Random and optianally Digest::HMAC_MD5. +Mojolicious and optionally Digest::HMAC_MD5, Crypt::URandom. =head1 SEE ALSO |