#!/usr/bin/perl -I. # # This software is copyright (c) 2001-2002 Positive Networks, Inc. All rights reserved. # # This software is the proprietary and confidential property of Positive Networks, Inc. # Possession, transmission, publication, or use of this software is prohibited except by prior written consent. # use strict; use Pos::Webtop::Web::TreeNode; use Pos::Webtop::Web::ContentModule (); use Pos::Webtop::OcxLauncher (); use Pos::Webtop::Web::ContentModule::Mail::Pop3Proxy; use Pos::Webtop::Web::ContentModule::Mail::ImapProxy; use Pos::Webtop::Web::ContentModule::Mail::ExchangeProxy; use Pos::Webtop::Web::ContentModule::Mail::AttachmentFile; use URI::Escape; my $outgoingDirectory = "/data/webtop/outgoing/"; package Pos::Webtop::Web::ContentModule::Mail; our @ISA = qw( Pos::Webtop::Web::ContentModule ); sub new { my $this = shift; my $class = ref($this) || $this; my ($self) = @{{@_}}{qw/self/}; if ( not defined $self ) { $self = {}; bless($self,$class); } # setup base class my %args = @_; $args{'self'} = $self; Pos::Webtop::Web::ContentModule->new( %args ); return $self; } sub getUserMainHtml { my $self = shift; my $toolBox = $self->getToolBox(); my $client = $toolBox->getClient(); my $cgi = $toolBox->getCgi(); my $db = $toolBox->getWebtopDbReader(); my $action = $cgi->param('action'); my $html = ""; my $p = $toolBox->getPage(); my $type = $self->getUserEmailType(db => $db); my $noPage = $cgi->param('noPage'); if (not defined($noPage)) { $noPage = 0; } if (!$noPage) { $html .= $p->getHeaderHtml(title => "Webtop: Mail") . "
" . $toolBox->getDomJs; } if (not defined($type)) { $html .= "You do not have Email settings configured."; } elsif ( $type eq 'OWA') { my @res = $db->query(sql => " select owa_server from w_user_email_settings_owa where user_id = '" . $client->getUserId . "' "); if (scalar(@res) == 0) { return undef; } my $server = $res[0]; #$html .= Pos::Webtop::OcxLauncher::getLauncherHtml( tb=> $toolBox ); $html .= " Web Mail Access

"; } else { if (not defined($action) or $action eq 'login') { $html .= $self->actionLogin(type => $type, db => $db); } elsif ($action eq 'viewEmail') { $html .= $self->actionViewEmail(type => $type, db => $db, noPage => $noPage); } elsif ($action eq 'deleteEmail') { $html .= $self->actionDeleteEmail(type => $type, db => $db, noPage => $noPage); } elsif ($action eq 'createFolderPrompt') { $html .= $self->actionCreateFolderPrompt(type => $type, db => $db, noPage => $noPage); } elsif ($action eq 'createFolderExecute') { $html .= $self->actionCreateFolderExecute(type => $type, db => $db, noPage => $noPage); } elsif ($action eq 'moveToFolderPrompt') { $html .= $self->actionMoveToFolderPrompt(type => $type, db => $db, noPage => $noPage); } elsif ($action eq 'moveToFolderExecute') { $html .= $self->actionMoveToFolderExecute(type => $type, db => $db, noPage => $noPage); } elsif ($action eq 'composeEmail') { $html .= $self->actionComposeEmail(type => $type, db => $db, noPage => $noPage); } elsif ($action eq 'addAttachment') { $html .= $self->actionAddAttachment(type => $type, db => $db, noPage => $noPage); } elsif ($action eq 'sendEmail') { $html .= $self->actionSendEmail(type => $type, db => $db, noPage => $noPage); } elsif ($action eq 'downloadEmailAttachment') { $html .= $self->actionDownloadEmailAttachment(type => $type, db => $db, noPage => $noPage); } elsif ($action eq 'getEmailFrame') { $html .= $self->actionGetEmailFrame(type => $type, db => $db, noPage => $noPage); } else { $html .= 'invalid action ' . $action; } } if (!$noPage) { $html .= "
" . $p->getFooterHtml; } return $html; } sub getUserPreviewHtml { my $self = shift; my $toolBox = $self->getToolBox(); my $client = $toolBox->getClient(); my $cgi = $toolBox->getCgi(); my $db = $toolBox->getWebtopDbReader(); my $ret = ""; my @res; my $type = $self->getUserEmailType(db => $db); if (not defined($type)) { return undef; } if ($type eq 'POP3') { $ret .= " Access your email:
" . $client->getFormGetCode(text => "Login...", values => [ { name => 'module', value => 'Mail' } ], tagAttributes => [ { name => 'class', value => 'CLMPREVIEWTD4' } ]) . "

"; } elsif ($type eq 'IMAP') { $ret .= " Access your email:
" . $client->getFormGetCode(text => "Login...", values => [ { name => 'module', value => 'Mail' } ], tagAttributes => [ { name => 'class', value => 'CLMPREVIEWTD4' } ]) . "

"; } elsif ($type eq 'EXCHANGE') { $ret .= " Access you email:
" . $client->getFormGetCode(text => "Login...", values => [ { name => 'module', value => 'Mail' } ], tagAttributes => [ { name => 'class', value => 'CLMPREVIEWTD4' } ]) . "

"; } elsif ($type eq 'OWA') { @res = $db->query(sql => " select owa_server from w_user_email_settings_owa where user_id = '" . $client->getUserId . "' "); if (scalar(@res) == 0) { return undef; } my $server = $res[0]; $ret .= " Web Mail Access

"; } else { $ret = undef; } return $ret; } # (pure virtual defined in Pos::Webtop::Web::ContentModule) sub main { my $self = shift; my $toolBox = $self->getToolBox(); my $client = $toolBox->getClient(); my $cgi = $toolBox->getCgi(); print $self->getUserMainHtml; return 1; } # (pure virtual defined in Pos::Webtop::Web::ContentModule) sub getPreviewHtml { my $self = shift; my $toolBox = $self->getToolBox(); my $client = $toolBox->getClient(); my $cgi = $toolBox->getCgi(); my $ret = undef; my $preview = undef; $ret = $self->getPreviewHeader(); $preview = $self->getUserPreviewHtml; if (not defined($preview)) { return undef; } else { $ret .= $preview; } $ret .= $self->getPreviewFooter(); return $ret; } sub getUserEmailType { my $self = shift; my $toolBox = $self->getToolBox(); my $client = $toolBox->getClient(); my $cgi = $toolBox->getCgi(); my ($db) = @{{@_}}{qw/db/}; my @res = $db->query(sql => " select wes.email_setting_name from w_email_settings wes, w_user_email_settings wues where wues.email_setting_id = wes.email_setting_id AND wues.user_id = '" . $client->getUserId . "' "); if (scalar(@res) == 0) { return undef; } return $res[0]; } sub getUserEmailParams { my $self = shift; my $toolBox = $self->getToolBox(); my $client = $toolBox->getClient(); my $cgi = $toolBox->getCgi(); my $ret = undef; my ($type,$db) = @{{@_}}{qw/type db/}; my %emailParams; if ($type eq 'POP3') { my @res = $db->query(sql => " select smtp_server, pop3_server, username, password from w_user_email_settings_pop3 where user_id = '" . $client->getUserId . "' "); if (scalar(@res) > 0) { my ($smtp,$pop3,$username,$password) = @{ $res[0] }; $emailParams{'SMTP_SERVER'} = $smtp; $emailParams{'POP3_SERVER'} = $pop3; $emailParams{'USERNAME'} = $username; $emailParams{'PASSWORD'} = $password; } } elsif ($type eq 'IMAP') { my @res = $db->query(sql => " select smtp_server, imap_server, username, password from w_user_email_settings_imap where user_id = '" . $client->getUserId . "' "); if (scalar(@res) > 0) { my ($smtp,$imap,$username,$password) = @{ $res[0] }; $emailParams{'SMTP_SERVER'} = $smtp; $emailParams{'IMAP_SERVER'} = $imap; $emailParams{'USERNAME'} = $username; $emailParams{'PASSWORD'} = $password; } } elsif ($type eq 'EXCHANGE') { my @res = $db->query(sql => " select exchange_server, username, password, exchange_server_name from w_user_email_settings_exchange where user_id = '" . $client->getUserId . "' "); if (scalar(@res) > 0) { my ($exchange,$username,$password,$exchangeName) = @{ $res[0] }; $emailParams{'EXCHANGE_SERVER'} = $exchange; $emailParams{'USERNAME'} = $username; $emailParams{'PASSWORD'} = $password; $emailParams{'EXCHANGE_SERVER_NAME'} = $exchangeName; } } elsif ($type eq 'OWA') { %emailParams = undef; } my $password = $cgi->param('password'); if (defined($password)) { $emailParams{'PASSWORD'} = $password; } return %emailParams; } sub getMailProxyFromType { my $self = shift; my $toolBox = $self->getToolBox(); my $client = $toolBox->getClient(); my $cgi = $toolBox->getCgi(); my ($db, $type, $emailParamsSc) = @{{@_}}{qw/db type emailParams/}; my $mailProxy = undef; my %emailParams = %$emailParamsSc; if ($type eq 'POP3') { $mailProxy = Pos::Webtop::Web::ContentModule::Mail::Pop3Proxy->new( tb => $toolBox, db => $db, smtpServer => $emailParams{'SMTP_SERVER'}, pop3Server => $emailParams{'POP3_SERVER'}, username => $emailParams{'USERNAME'}, password => $emailParams{'PASSWORD'}); } elsif ($type eq 'IMAP') { $mailProxy = Pos::Webtop::Web::ContentModule::Mail::ImapProxy->new( tb => $toolBox, db => $db, smtpServer => $emailParams{'SMTP_SERVER'}, imapServer => $emailParams{'IMAP_SERVER'}, username => $emailParams{'USERNAME'}, password => $emailParams{'PASSWORD'}); } elsif ($type eq 'EXCHANGE') { $mailProxy = Pos::Webtop::Web::ContentModule::Mail::ExchangeProxy->new( tb => $toolBox, db => $db, exchangeServer => $emailParams{'EXCHANGE_SERVER'}, username => $emailParams{'USERNAME'}, password => $emailParams{'PASSWORD'}, exchangeServerName => $emailParams{'EXCHANGE_SERVER_NAME'}); } return $mailProxy; } sub actionLogin { my $self = shift; my $toolBox = $self->getToolBox(); my $client = $toolBox->getClient(); my $cgi = $toolBox->getCgi(); my $ret = ""; my ($type,$db) = @{{@_}}{qw/type db/}; my %emailParams = $self->getUserEmailParams(type => $type, db => $db); my $username = $cgi->param('username'); if (defined($username)) { $emailParams{'USERNAME'} = $username; } else { $username = $emailParams{'USERNAME'}; } my $mailProxy = $self->getMailProxyFromType(type => $type, db => $db, emailParams => \%emailParams); # XXX -- wording if (!%emailParams or not defined($mailProxy)) { return "Your mail service does not support logins."; } my $prompt = 1; my $password = $emailParams{'PASSWORD'}; # If they have a password defined we should go ahead and try to login if (defined($password)) { if (not defined($mailProxy->login())) { $ret = "
You could not be logged in.
"; } else { $mailProxy->cacheCredentials(username => $username, password => $password); $prompt = 0; } } if ($prompt) { my $box = $toolBox->createBox(title => "Login", width => '100'); $ret .= "
" . $client->getFormInputs( values => [ { name => 'action', value => 'login' } ]) . " " . $box->getHeaderHtml . "
Username:
Password:
" . $box->getFooterHtml . "
"; } else { $ret = $self->getMajorLayout(mailProxy => $mailProxy); } return $ret; } sub getMajorLayout { my $self = shift; my $toolBox = $self->getToolBox(); my $client = $toolBox->getClient(); my $cgi = $toolBox->getCgi(); my ($mailProxy,$requestedFolder) = @{{@_}}{qw/mailProxy requestedFolder/}; my $ret = ""; if (not defined($requestedFolder) or length($requestedFolder) == 0) { $requestedFolder = $client->getPersistedValue(name => 'requestedFolder'); if (not defined($requestedFolder) or length($requestedFolder) == 0) { if ($mailProxy->doesFolderExist(folderName => 'INBOX')) { $requestedFolder = 'INBOX'; } elsif ($mailProxy->doesFolderExist(folderName => 'Inbox')) { $requestedFolder = 'Inbox'; } else { $requestedFolder = 'Inbox'; } } } # XXX: optimize $mailProxy->checkEmail; my @folders = $mailProxy->getFolders(); $client->setPersistedValue( name => 'requestedFolder', value => $requestedFolder); $ret .= "
" . $client->getFormInputs(values => [ { name => 'action', value => 'getEmailFrame' }, { name => 'noPage', value => '1' }, { name => 'requestedFolder', value => $requestedFolder } ]) . "
" . $client->getFormInputs(values => [ { name => 'action', value => 'viewEmail' }, { name => 'noPage', value => '1' }, { name => 'requestedFolder', value => $requestedFolder }, { name => 'emailId', value => '' } ]) . "
" . $client->getFormInputs(values => [ { name => 'action', value => '' } ]) . "
" . $client->getFormInputs(); $ret .= "
Folders
" . $self->getFoldersInSupportedFormat(folders => \@folders, js => 'browseToFolder') . "
Loading...
"; return $ret; } sub actionGetEmailFrame { my $self = shift; my $toolBox = $self->getToolBox(); my $client = $toolBox->getClient(); my $cgi = $toolBox->getCgi(); my ($type,$db) = @{{@_}}{qw/type db/}; my ($requestedFolder) = $cgi->param('requestedFolder'); my $ret = ""; my %emailParams = $self->getUserEmailParams(type => $type, db => $db); my $mailProxy = $self->getMailProxyFromType(type => $type, db => $db, emailParams => \%emailParams); $mailProxy->checkEmail; my @emails = $mailProxy->getPreviewEmails(max => 30, folder => $requestedFolder); my $link = "main.pl?pVwsi=" . $client->getSessionId . "&pVwsh=" . $client->getSessionHash . "&pVmodule=Mail&&requestedFolder=" . HTML::Entities::encode($requestedFolder) . "&noPage=1&action=getEmailFrame"; $ret .= "Content-type: text/html\n\n"; $ret .= $toolBox->getDomJs . " "; my $newCount = 0; my $count = 0; if (scalar(@emails) > 0) { foreach my $email (@emails) { if (not defined($email)) { next; } my ($emailId,$msgId,$to,$from,$subject,$date,$isNew) = @{ $email }; $count++; if ($from =~ /(.*) <(.*)>/) { ($from) = split /\ "; } } else { $ret .= " "; } $ret .="
 
 From  Subject  Received
" . HTML::Entities::encode($from) . "  " . HTML::Entities::encode($subject) . "  " . HTML::Entities::encode($date) . "
No Emails.
"; } sub actionViewEmail { my $self = shift; my $toolBox = $self->getToolBox(); my $client = $toolBox->getClient(); my $cgi = $toolBox->getCgi(); my ($type,$db,$noPage) = @{{@_}}{qw/type db noPage/}; my $ret = ""; if (defined($noPage) and $noPage == 1) { $ret .= "Content-type: text/html\n\n"; $ret .= " "; } my $emailId = $cgi->param('emailId'); my %emailParams = $self->getUserEmailParams(type => $type, db => $db); my $mailProxy = $self->getMailProxyFromType(type => $type, db => $db, emailParams => \%emailParams); if (not defined($emailId)) { return $ret; } # XXX -- wording if (!%emailParams or not defined($mailProxy)) { return "Your mail service does not support logins."; } if (not defined($mailProxy->login())) { return "
You could not be logged in.
"; } my %emailInformation = $mailProxy->getEmail(emailId => $emailId); my $attachments = $emailInformation{'EMAIL_ATTACHMENTS'}; if (not defined($emailInformation{'EMAIL_SUBJECT'})) { $emailInformation{'EMAIL_SUBJECT'} = ""; } if (not defined($emailInformation{'TO'})) { $emailInformation{'EMAIL_TO'} = ""; } my %hash = %{ $mailProxy->getAdjacentEmails(emailId => $emailId) }; my $prevId = defined($hash{'PREV'}) ? $hash{'PREV'} : ""; my $nextId = defined($hash{'NEXT'}) ? $hash{'NEXT'} : ""; $prevId =~ s/\\/\\\\/g; $nextId =~ s/\\/\\\\/g; $ret .= $toolBox->getDomJs . "
"; $ret .= " " . $client->getFormInputs(values => [ { name => 'origFrom', value => $emailInformation{'EMAIL_FROM'} }, { name => 'origTo', value => $emailInformation{'EMAIL_TO'} }, { name => 'origSubject', value => $emailInformation{'EMAIL_SUBJECT'} }, { name => 'origBody', value => $emailInformation{'EMAIL_BODY'} }, { name => 'emailId', value => $emailId } ]) . " "; if (scalar(@{ $attachments })) { my $counter = 0; $ret .= " "; } my $body = HTML::Entities::encode($emailInformation{'EMAIL_BODY'}); # If we're using quoted-printable, =[hex] be translated into the corresponding ascii character if ($emailInformation{'EMAIL_BODY_ENCODING'} eq 'quoted-printable') { # Need a faster way to do this some day. while ( $body =~ /(.*)=([ABCDEF1234567890]{2})(.*)/msi ) { $body = $1 . chr(hex($2)) . $3; } $body =~ s/=(\r|)$//gms; } $body =~ s/\n/
/g; $body =~ s/\t/    /g; $ret .= "
 From:  " . HTML::Entities::encode($emailInformation{'EMAIL_FROM'}) . "
 To:  " . HTML::Entities::encode($emailInformation{'EMAIL_TO'}) . "
 Subject:  " . HTML::Entities::encode($emailInformation{'EMAIL_SUBJECT'}) . "
 Received:  " . HTML::Entities::encode($emailInformation{'EMAIL_DATE'}) . "
 Attachments: "; foreach my $attachment (@{ $attachments }) { my %hash = %{ $attachment }; my $desc = $hash{'ATTACHMENT_DESC'}; my $body = $hash{'ATTACHMENT_BODY'}; if (not defined($desc)) { $desc = 'attachment'; } my $kb = length($body) / 1024; my $fmt = sprintf ("%.2f", $kb); $ret .= " " . $client->getFormGetCode(text => $desc, values => [ { name => 'action', value => 'downloadEmailAttachment' }, { name => 'noPage', value => 1 }, { name => 'emailId', value => $emailId }, { name => 'attachmentId', value => $counter }]) . " ($fmt KB)
"; $counter++; } $ret .= "
"; if (defined($prevId) and length($prevId)) { $ret .= " "; } if (defined($nextId) and length($nextId)) { $ret .= " "; } $ret .= "
$body
"; $mailProxy->markEmailAsRead(emailId => $emailId); return $ret; } sub actionDownloadEmailAttachment { my $self = shift; my $toolBox = $self->getToolBox(); my $client = $toolBox->getClient(); my $cgi = $toolBox->getCgi(); my ($type,$db) = @{{@_}}{qw/type db/}; my $ret = ""; my %emailParams = $self->getUserEmailParams(type => $type, db => $db); my $mailProxy = $self->getMailProxyFromType(type => $type, db => $db, emailParams => \%emailParams); # XXX -- wording if (!%emailParams or not defined($mailProxy)) { return "Your mail service does not support logins."; } my $emailId = $cgi->param('emailId'); my $attachmentId = $cgi->param('attachmentId'); my %emailInformation = $mailProxy->getEmail(emailId => $emailId); my $attachments = $emailInformation{'EMAIL_ATTACHMENTS'}; my $counter = 0; my $contentType = undef; my $contentEncoding = undef; my $disposition = undef; my $desc = undef; my $body = undef; foreach my $attachment (@{ $attachments }) { my %hash = %{ $attachment }; $contentType = $hash{'ATTACHMENT_CONTENT_TYPE'}; $contentEncoding = $hash{'ATTACHMENT_CONTENT_ENCODING'}; $disposition = $hash{'ATTACHMENT_DISPOSITION'}; $desc = $hash{'ATTACHMENT_DESC'}; $body = $hash{'ATTACHMENT_BODY'}; if (not defined($desc)) { $desc = 'attachment'; } if (not defined($contentType)) { $contentType = 'text/plain'; } else { my @info = split /;/, $contentType; $contentType = $info[0]; } if (defined($disposition)) { $disposition =~ s/"//g; $disposition =~ s/attachment/inline/g; } if ($counter == $attachmentId) { last; } $counter++; } if (not defined($desc) or not defined($body)) { $ret .= "Content-Type: text/html\r\n\r\nnot found"; } else { print STDERR "sending type=$contentType disp=$disposition encoding=$contentEncoding\n"; $ret .= "Content-Type: $contentType\r\n"; if (defined($disposition)) { $ret .= "Content-Disposition: $disposition\r\n"; } if (defined($contentEncoding)) { # If it's base64, decode it. if ($contentEncoding =~ /base64/i) { $body = MIME::Base64::decode_base64($body); } } $ret .= "\r\n"; $ret .= $body; } return $ret; } sub actionDeleteEmail { my $self = shift; my $toolBox = $self->getToolBox(); my $client = $toolBox->getClient(); my $cgi = $toolBox->getCgi(); my ($type,$db) = @{{@_}}{qw/type db/}; my $ret = ""; my @emails = $cgi->param('checkedItems'); my %emailParams = $self->getUserEmailParams(type => $type, db => $db); my $mailProxy = $self->getMailProxyFromType(type => $type, db => $db, emailParams => \%emailParams); # XXX -- wording if (!%emailParams or not defined($mailProxy)) { return "Your mail service does not support logins."; } if (not defined($mailProxy->login())) { return "
You could not be logged in.
"; } if (defined($cgi->param('emailId'))) { push(@emails, $cgi->param('emailId')); } foreach my $emailId (@emails) { $mailProxy->deleteEmail(emailId => $emailId); } return $self->getMajorLayout(mailProxy => $mailProxy, requestedFolder => $cgi->param('folder')); } sub actionCreateFolderPrompt { my $self = shift; my $toolBox = $self->getToolBox(); my $client = $toolBox->getClient(); my $cgi = $toolBox->getCgi(); my ($type,$db) = @{{@_}}{qw/type db/}; my $ret = ""; my %emailParams = $self->getUserEmailParams(type => $type, db => $db); my $mailProxy = $self->getMailProxyFromType(type => $type, db => $db, emailParams => \%emailParams); # XXX -- wording if (!%emailParams or not defined($mailProxy)) { return "Your mail service does not support logins."; } my $box = $toolBox->createBox(title => "Create New Folder", width => "285"); my @folders = $mailProxy->getFolders(); $ret .= $toolBox->getDomJs . "

" . $box->getHeaderHtml; $ret .= " " . $client->getFormInputs(values => [ { name => 'action', value => 'createFolderExecute' }, { name => 'parentName', value => '' }, { name => 'folderName', value => '' }]) . "
Folder name:
Select the parent folder
" . $self->getFoldersInSupportedFormat(folders => \@folders, js => 'updateParent') . "
"; $ret .= $box->getFooterHtml . "
"; } sub actionCreateFolderExecute { my $self = shift; my $toolBox = $self->getToolBox(); my $client = $toolBox->getClient(); my $cgi = $toolBox->getCgi(); my ($type,$db) = @{{@_}}{qw/type db/}; my $ret = ""; my $folderName = $cgi->param('folderName'); my %emailParams = $self->getUserEmailParams(type => $type, db => $db); my $mailProxy = $self->getMailProxyFromType(type => $type, db => $db, emailParams => \%emailParams); # XXX -- wording if (!%emailParams or not defined($mailProxy)) { return "Your mail service does not support logins."; } if (not defined($folderName) or length($folderName) == 0) { $ret = "
You did not specify a valid folder name.
"; $ret .= $self->actionCreateFolderPrompt(type => $type, db => $db); } elsif ($mailProxy->doesFolderExist(folderName => $folderName)) { $ret = "
A folder by that name already exists.
"; $ret .= $self->actionCreateFolderPrompt(type => $type, db => $db); } elsif (!($mailProxy->createFolder(folderName => $folderName))) { $ret = "
The folder '" . HTML::Entities::encode($folderName) . "' could not be created.
"; $ret .= $self->actionCreateFolderPrompt(type => $type, db => $db); } else { $ret = $self->getMajorLayout(mailProxy => $mailProxy); } return $ret; } sub actionMoveToFolderPrompt { my $self = shift; my $toolBox = $self->getToolBox(); my $client = $toolBox->getClient(); my $cgi = $toolBox->getCgi(); my ($type,$db) = @{{@_}}{qw/type db/}; my $ret = ""; my @checkedItems = $cgi->param('checkedItems'); my %emailParams = $self->getUserEmailParams(type => $type, db => $db); my $mailProxy = $self->getMailProxyFromType(type => $type, db => $db, emailParams => \%emailParams); # XXX -- wording if (!%emailParams or not defined($mailProxy)) { return "Your mail service does not support logins."; } my $box = $toolBox->createBox(title => "Move Emails to Folder...", width => "500"); $ret .= $toolBox->getDomJs . "

" . $box->getHeaderHtml; $ret .= " " . $client->getFormInputs(values => [ { name => 'action', value => 'moveToFolderExecute' }, { name => 'folderName', value => '' } ]) . " "; foreach my $item (@checkedItems) { my %emailInformation = $mailProxy->getEmail(emailId => $item); if (!%emailInformation) { next; } $ret .= " "; } my @folders = $mailProxy->getFolders; $ret .= "
 From  Subject  Received
" . HTML::Entities::encode($emailInformation{'EMAIL_FROM'}) . "  " . HTML::Entities::encode($emailInformation{'EMAIL_SUBJECT'}) . " " . HTML::Entities::encode($emailInformation{'EMAIL_DATE'}) . "
Select the folder you wish to move to:
" . $self->getFoldersInSupportedFormat(folders => \@folders, js => 'updateFolderPath') . "
"; $ret .= $box->getFooterHtml . "
"; } sub actionMoveToFolderExecute { my $self = shift; my $toolBox = $self->getToolBox(); my $client = $toolBox->getClient(); my $cgi = $toolBox->getCgi(); my ($type,$db) = @{{@_}}{qw/type db/}; my $ret = ""; my $folderName = $cgi->param('folderName'); my @checkedItems = $cgi->param('checkedItems'); my %emailParams = $self->getUserEmailParams(type => $type, db => $db); my $mailProxy = $self->getMailProxyFromType(type => $type, db => $db, emailParams => \%emailParams); # XXX -- wording if (!%emailParams or not defined($mailProxy)) { return "Your mail service does not support logins."; } if (not defined($folderName) or length($folderName) == 0) { $ret = "
You did not specify a valid folder name.
"; $ret .= $self->actionMoveToFolderPrompt(type => $type, db => $db); } elsif (!($mailProxy->moveToFolder(emails => \@checkedItems, folderName => $folderName))) { $ret = "
The emails could not be moved to '" . HTML::Entities::encode($folderName) . "'.
"; $ret .= $self->actionMoveToFolderPrompt(type => $type, db => $db); } else { $ret = $self->getMajorLayout(mailProxy => $mailProxy); } return $ret; } sub actionComposeEmail { my $self = shift; my $toolBox = $self->getToolBox(); my $client = $toolBox->getClient(); my $cgi = $toolBox->getCgi(); my ($type,$db) = @{{@_}}{qw/type db/}; my $ret = ""; my %emailParams = $self->getUserEmailParams(type => $type, db => $db); my $mailProxy = $self->getMailProxyFromType(type => $type, db => $db, emailParams => \%emailParams); # XXX -- wording if (!%emailParams or not defined($mailProxy)) { return "Your mail service does not support logins."; } my $box = $toolBox->createBox(title => "Compose Email", width => "500"); my $outgoingId = ((defined($cgi->param('outgoingMailId')))?$cgi->param('outgoingMailId'):$mailProxy->getNextOutgoingMailId); my $to = ((defined($cgi->param('to')))?$cgi->param('to'):""); my $cc = ((defined($cgi->param('cc')))?$cgi->param('cc'):""); my $bcc = ((defined($cgi->param('bcc')))?$cgi->param('bcc'):""); my $subject = ((defined($cgi->param('subject')))?$cgi->param('subject'):""); my $body = ((defined($cgi->param('body')))?$cgi->param('body'):""); my $composeType = $cgi->param('composeType'); if (not defined($subject)) { $subject = ""; } if (defined($composeType) and $composeType eq 'reply') { if (defined($cgi->param('origBody'))) { $body = $cgi->param('origBody'); $body =~ s/^/> /gm; } if (defined($cgi->param('origFrom'))) { $to = $cgi->param('origFrom'); # needs to match: # blah # email@home.com # /(?:.*?<(.*)>|(.*))/ $to =~ s/(?:.*?<(.*)>|(.*))/$1$2/; } if (defined($cgi->param('origSubject'))) { $subject = "Re: " . $cgi->param('origSubject'); } } elsif (defined($composeType) and $composeType eq 'forward') { if (defined($cgi->param('origBody'))) { $body = $cgi->param('origBody'); $body =~ s/^/> /gm; } if (defined($cgi->param('origSubject'))) { $subject = "Fw: " . $cgi->param('origSubject'); } } $ret .= "
" . $box->getHeaderHtml; $ret .= " " . $client->getFormInputs(values => [ { name => 'action', value => 'sendEmail', }, { name => 'outgoingMailId', value => $outgoingId }]) . " "; my $temporaryFile = $outgoingDirectory . $client->getSessionId . '-' . $client->getUserId . '-' . $outgoingId; my $attachmentFile = Pos::Webtop::Web::ContentModule::Mail::AttachmentFile->new(); my $attachments = $attachmentFile->getAttachments(file => $temporaryFile); if (defined($attachments) and scalar(@{ $attachments }) > 0) { $ret .= " "; } $ret .= "
 To:  
 Cc:  
 Bcc:  
 Attachments: "; foreach my $attachment (@{ $attachments }) { my %hash = %{ $attachment }; my $fileName = $hash{'ATTACHMENT_FILENAME'}; my $fileSize = $hash{'ATTACHMENT_SIZE'} / 1024; $fileSize = sprintf("%.2f", $fileSize); $ret .= "  " . HTML::Entities::encode($fileName) . " ($fileSize KB)
"; } $ret .= "
 Subject:  
   
   
"; $ret .= $box->getFooterHtml . "
"; return $ret; } sub actionAddAttachment { my $self = shift; my $toolBox = $self->getToolBox(); my $client = $toolBox->getClient(); my $cgi = $toolBox->getCgi(); my ($type,$db) = @{{@_}}{qw/type db/}; my $ret = ""; my %emailParams = $self->getUserEmailParams(type => $type, db => $db); my $mailProxy = $self->getMailProxyFromType(type => $type, db => $db, emailParams => \%emailParams); # XXX -- wording if (!%emailParams or not defined($mailProxy)) { return "Your mail service does not support logins."; } my $outgoingId = $cgi->param('outgoingMailId'); # Now we shall add the attachment to the temporary file my $temporaryFile = $outgoingDirectory . $client->getSessionId . '-' . $client->getUserId . '-' . $outgoingId; my $boundary = $client->getSessionId . $client->getUserId . $outgoingId; my $fd; my $filename = $cgi->param('attachedFile'); my $handle = $cgi->upload('attachedFile'); my $attachmentFile = Pos::Webtop::Web::ContentModule::Mail::AttachmentFile->new(); if (defined($attachmentFile)) { $attachmentFile->append( file => $temporaryFile, boundary => $boundary, inputHandle => $handle, inputFileName => $filename); } return $self->actionComposeEmail(type => $type, db => $db); } sub actionSendEmail { my $self = shift; my $toolBox = $self->getToolBox(); my $client = $toolBox->getClient(); my $cgi = $toolBox->getCgi(); my ($type,$db) = @{{@_}}{qw/type db/}; my $ret = ""; my %emailParams = $self->getUserEmailParams(type => $type, db => $db); my $mailProxy = $self->getMailProxyFromType(type => $type, db => $db, emailParams => \%emailParams); # XXX -- wording if (!%emailParams or not defined($mailProxy)) { return "Your mail service does not support logins."; } my $outgoingId = $cgi->param('outgoingMailId'); my $temporaryFile = $outgoingDirectory . $client->getSessionId . '-' . $client->getUserId . '-' . $outgoingId; my $attachmentFile = Pos::Webtop::Web::ContentModule::Mail::AttachmentFile->new(); my $attachments = $attachmentFile->getAttachments(file => $temporaryFile); if (not defined($mailProxy->sendEmail(to => $cgi->param('to'), cc => $cgi->param('cc'), bcc => $cgi->param('bcc'), subject => $cgi->param('subject'), body => $cgi->param('body'), attachments => $attachments))) { $ret .= "
The mail message could not be transmitted
"; $ret .= $self->actionComposeEmail(type => $type, db => $db); } else { # We no longer need the attachments file $temporaryFile =~ /(.*)/; $temporaryFile = $1; unlink($temporaryFile); $ret .= "
Mail sent successfully
"; $ret .= $self->getMajorLayout(mailProxy => $mailProxy); } return $ret; } # Internal methods sub getFoldersInSupportedFormat { my $self = shift; my ($folders, $js) = @{{@_}}{qw/folders js/}; # Convert folders into a tree my $tree = Pos::Webtop::Web::TreeNode->new(); foreach my $folder (@{ $folders }) { my ($folderName, $folderEmailNewCount) = @{ $folder }; my @nodes = split /\\/, $folderName; my $parent = $tree; my $count = 0; foreach my $nodeName (@nodes) { my $appendFrontText = ""; my $appendBackText = ""; $count++; if ($folderEmailNewCount and $count == scalar(@nodes)) { $appendFrontText = ""; $appendBackText = "($folderEmailNewCount)"; } my $encode = HTML::Entities::encode($folderName); $encode =~ s/\\/\\\\/g; $encode =~ s/\"/\\\"/g; $encode =~ s/\'/\\\'/g; my $new = Pos::Webtop::Web::TreeNode->new(text => $nodeName, appendFrontText => $appendFrontText, appendBackText => $appendBackText, js => "$js(\"$encode\");"); $new = $parent->pushNode(node => $new); $parent = $new; } } return $tree->sprout(); } 1;