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 297 298 299 300 301 302 303 304 305 306 307 308 309 310
|
package CiderWebmail::Controller::Root;
use strict;
use warnings;
use base 'Catalyst::Controller';
use version;
use List::Util qw(reduce);
use Time::HiRes;
use Try::Tiny::SmartCatch;
use Petal::TranslationService::Gettext;
#
# Sets the actions in this controller to be registered with no prefix
# so they function identically to actions created in MyApp.pm
#
__PACKAGE__->config->{namespace} = '';
=head1 NAME
CiderWebmail::Controller::Root - Root Controller for CiderWebmail
=head1 DESCRIPTION
[enter your description here]
=head1 METHODS
=cut
=head2 auto
Only logged in users may use this product.
=cut
sub auto : Private {
my ($self, $c) = @_;
DB::enable_profile() if $ENV{NYTPROF};
$Petal::I18N::Domain = 'CiderWebmail';
my $translation_service = Petal::TranslationService::Gettext->new(
domain => 'CiderWebmail',
locale_dir => $c->config->{root} . '/locale',
target_lang => $c->config->{language} || 'en',
);
$c->stash->{translation_service} = $translation_service;
my $langs = join '|', $c->langs;
if (( $c->request->headers->header('Accept-Language') or '') =~ m/^($langs)/ixm) {
$c->stash->{language} = $1;
} else {
$c->stash->{language} = $c->config->{language} || 'en';
}
$c->stash->{timestamp} = Time::HiRes::time();
if ($c->sessionid and $c->session->{'username'} and $c->req->cookie('password')) {
$c->stash->{server} = $c->session->{server};
if ($c->authenticate({id => $c->session->{'username'}, password => CiderWebmail::Util::decrypt($c, { string => $c->req->cookie('password')->value }) })) {
#IMAPClient setup
$c->stash->{imapclient}->Ranges(1);
$c->stash({
uri_mailboxes => $c->uri_for('/mailboxes'),
uri_addressbook => $c->uri_for('/addressbook'),
uri_logout => $c->uri_for('/logout'),
});
$c->stash({ uri_managesieve => $c->uri_for('/managesieve') }) if ($c->config->{managesieve}->{mode} eq 'on');
$c->stash({ uri_vacation => $c->uri_for('/managesieve') }) if ($c->config->{managesieve}->{mode} eq 'vacation');
return 1;
}
}
# Give the user a chance to authenticate
$c->forward('login');
return 0;
}
=head2 login
Login action.
Private action that auto forwards to so we can prepend it do a login on any URI and on successful login show the requested page.
=cut
sub login : Private {
my ( $self, $c ) = @_;
my $server = $c->config->{server};
$c->stash({ template => 'login.xml' });
$c->stash->{server} = $c->req->param('server') if not ($server and %$server) and $c->req->param('server');
$c->stash({ server => "$server->{host}:$server->{port}" }) if $server and %$server;
my %user_data = (
username => $c->req->param('username'),
password => $c->req->param('password'),
server => $c->stash->{server},
);
if ($user_data{username} and $user_data{password}) {
#Only Mail::IMAPClient > 3.32 supports literals in the LOGIN command
if (version->parse($Mail::IMAPClient::VERSION) >= version->parse('3.32')) {
utf8::encode($user_data{username});
utf8::encode($user_data{password});
}
try sub {
$c->authenticate(\%user_data);
},
catch_when qr/^\w+\s+NO/ => sub { #TODO the backend should return a status instead of parsing it here
$c->response->code(403);
$c->stash->{message} = 'Invalid username or password.';
},
catch_when qr/Unable to connect to/i => sub {
$c->response->code(500);
$c->stash->{message} = 'Unable to login, IMAP backend unreachable.';
},
catch_default sub {
$c->response->code(500);
$c->stash->{message} = 'Unable to login';
warn "Unable to login, backend reported '$_ / $!'";
};
#abort unless we have a successfull login
return unless defined $c->user;
$c->session->{$_} = $user_data{$_} foreach qw(username server); # save for repeated IMAP authentication
$c->res->cookies->{$_} = { expires => '+1d', value => CiderWebmail::Util::encrypt($c, { string => $user_data{$_} }) } foreach qw(password); # save for repeated IMAP authentication
my @supported = $c->stash->{imapclient}->capability;
foreach(qw/ SORT /) {
my $capability = $_;
unless( grep { $_ eq $capability } @supported ) {
$c->stash({ message => "Your IMAP Server does not advertise the $_ capability" }); #TODO I18N
return;
}
}
return $c->res->redirect($c->req->uri);
}
return;
}
=head2 logout
Logout action: drops the current session and logs out the user.
Redirects to / so we can start a new session.
=cut
sub logout : Local {
my ( $self, $c ) = @_;
$c->res->cookies->{'password'} = { expires => '-1y', value => 'none' };
$c->logout;
$c->delete_session('logged out');
return $c->res->redirect($c->uri_for('/'));
}
=head2 index
Redirect to the INBOX view
=cut
sub index : Private {
my ( $self, $c ) = @_;
my $model = $c->model('IMAPClient');
CiderWebmail::Util::add_foldertree_to_stash($c);
my $inbox;
my $folders = $c->stash->{folder_tree}{folders};
if (@$folders > 1) {
$_->{name} =~ /\Ainbox\z/xmi and $inbox = $_ foreach @$folders; # try to find a folder named INBOX
$inbox ||= reduce { $a->{name} lt $b->{name} ? $a : $b } @$folders; # no folder named INBOX
}
else {
$inbox = $folders->[0]; # only one folder, so this must be INBOX
}
return $c->res->redirect($inbox->{uri_view});
}
=head2 mailboxes
Lists the folders of this user. Used by AJAX to update the folder tree.
=cut
sub mailboxes : Local {
my ( $self, $c ) = @_;
CiderWebmail::Util::add_foldertree_to_stash($c);
my $tree = $c->stash->{folder_tree};
CiderWebmail::Util::add_foldertree_uris($c, {
path => undef,
folders => $tree->{folders},
uris => [
{action => 'view', uri => ''},
{action => 'create_subfolder', uri => 'create_subfolder'},
{action => 'delete', uri => 'delete'},
]
});
$c->stash({
template => 'mailboxes.xml',
uri_create_folder => $c->uri_for('create_folder'),
});
return;
}
=head2 create_folder
Create a top level folder
=cut
sub create_folder : Local {
my ( $self, $c ) = @_;
if (my $name = $c->req->param('name')) {
$c->model('IMAPClient')->create_mailbox({name => $name});
return $c->res->redirect($c->uri_for('mailboxes'));
}
CiderWebmail::Util::add_foldertree_to_stash($c);
$c->stash({
template => 'create_mailbox.xml',
});
return;
}
=head2 error
Display an error message found on the stash
=cut
sub error : Private {
my ( $self, $c ) = @_;
$c->response->status(500);
$c->stash({
template => 'error.xml',
});
return;
}
=head2 render
Attempt to render a view, if needed.
=cut
sub render : ActionClass('RenderView') {}
=head2 end
Cleanup after a request is rendered
=cut
sub end : Private {
my ($self, $c) = @_;
$c->forward('render');
$c->model('IMAPClient')->disconnect() unless $ENV{CIDERWEBMAIL_NODISCONNECT}; # disconnect but not for some tests that still need the connection
DB::disable_profile() if $ENV{NYTPROF};
return;
}
=head1 AUTHOR
Stefan Seifert
Mathias Reitinger <mathias.reitinger@loop0.org>
=head1 LICENSE
This library is free software, you can redistribute it and/or modify
it under the same terms as Perl itself.
=cut
1;
|