#!/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
";
}
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 . "
";
$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 . "
";
}
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 . "
";
}
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 .= "
";
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;