Code Search for Developers
 
 
  

process_compose.html from AlphaMail at Krugle


Show process_compose.html syntax highlighted

% # vim:set syntax=mason:
<%args>
$subject => ''
$body => ''
$to => 0
$cc => 0
$bcc => 0
@file => 0
$spellcheck => 0
$save => 0
$address => 0
$send => 0
$cancel => 0
$remove => 0
$spelling_checked => 0
</%args>
% if($send && $prefs->get('skipsendreport') && !$error) {
%    $m->redirect(build_url("$base/mail/index.html", { message => "Your message was sent." }));
% } elsif($send) {
   <& /mail/header.mhtml, title => "Process compose", folder => 'none' &>
   <p>
      <a href="<% $base %>/mail/index.html">Return to message index</a>
   </p>
%    if($error) {
%       if(@sent_to) {
      <p>Your message was sent to <% @sent_to |h %></p>
%       } else {
      <p>Your message was <b>not</b> sent due to an error. Contact Help.</p>
%       }
%       if($sent_folder ne 'none' && !$saved) {
      <p>A copy of your message was <b>not</b> saved in your sent folder due to an error. Contact help.</p>
%       }
%    } else {
%       local $" = ", ";
      <p>Your message has been sent to the following addresses:</p>
      <p><% "@sent_to" |h %></p>
%    }
   <& /mail/footer.mhtml &>
% } else {
   <& /mail/compose.html, message => $error &>
% } 
<%init>
$m->redirect("$base/mail/index.html") if($cancel);
my $user = Apache2::SiteControl->getCurrentUser($r);
if(!$user || !$user->isa('Apache2::SiteControl::User')) {
   $logger->error("Unable to find a user for the given session key. r, m:" . Dumper($r, $m));
   $m->redirect("$base");
}
my $prefs;
my $username;
my $error = 0;
my $sent = 0;
my $saved = 0;
my $entity;
my $folder;
my $c;
my $dir;
my $book;
my $sent_folder;
my $delete_drafts;
my @sent_to = (); # Result of actual send
my @not_sent_to = (); # Result of actual send
my $composition;
my ($sec,$min,$hour,$mday,$mon,$year,$wday,$yday,$isdst);

$subject = "(none)" if(!$subject);

eval { # Initialize variables
   $prefs = new AlphaMail::Preferences($user, 
                                      $user->getAttribute('mx'), $config);
   $username = $user->getUsername();
   $folder = $user->getAttribute('lastfolder') || 'inbox';
   $c = new AlphaMail::Middleware($user->getAttribute('imap'), $user->getUsername(), $user->getAttribute('password'), $config);
   $dir = $config->getComposeDir($user);
   $book = new AlphaMail::AddressBook($user->getUsername(), $user->getAttribute('mx'), $config);
   $sent_folder = $prefs->get('sent_folder', $config->get("default_sent", 'none'));
   $delete_drafts = $prefs->get('delete_drafts', 0);
   @sent_to = (); # Result of actual send
   $composition = new AlphaMail::Message($user, $config);
   $composition->load();
   ($sec,$min,$hour,$mday,$mon,$year,$wday,$yday,$isdst) = localtime(time);
}; 
if($@) {
   $logger->error($user->getUsername() . ": Compose processing failed: $@");
   $m->redirect(build_url("$base/mail/compose.html", { message => 'Unable to process compose. Please try again. If it fails again, please contact support.'}));
}

# Fix up incoming parameters
eval {
   $logger->debug($user->getUsername() . ": Looking up address lists in to, cc, and bcc");
   $to = Encode::decode_utf8($to);
   $cc = Encode::decode_utf8($cc);
   $bcc = Encode::decode_utf8($bcc);
   $to = $book->dealias($to, $user->getAttribute('mx', ''));
   $cc = $book->dealias($cc, $user->getAttribute('mx', ''));
   $bcc = $book->dealias($bcc, $user->getAttribute('mx', ''));

   # Process attachment fields:
   my $attachments = $composition->getAttachments;
   my $nattach = scalar(@{$attachments});
   # 1. Check to see what attachments should be deleted, if any:
   for(my $i = $nattach-1; $i >= 0; $i--) {
      my $fldnm = "attachment_$i";
      if(!$ARGS{$fldnm}) {
         $composition->removeAttachment($i);
      }
   }

   # 2. Process all of the incoming "file" fields:
   eval {
      use Apache2::Upload;
      use Apache2::Request;
      $logger->debug($user->getUsername() . ": Creating request object");
      my $req = Apache2::Request->new($r);
      # Get the file
      $logger->debug($user->getUsername() . ": Doing upload");
      my @uploads = $req->upload('file');
      for my $upload (@uploads) {
         $logger->debug($user->getUsername() . ": Getting filename");
         my $filename = Encode::decode_utf8($upload->filename);
         $logger->debug($user->getUsername() . ": got $filename");
         my $tmploc;
         my $size;

         next if($filename =~ m/^\s*$/);
         if($upload->size() == 0) {
            $error = "Attachment of $filename failed. Zero length file."; 
            next;
         }

         # sanitize the filename
         $filename =~ s!^.*[/\\]([^\\/]*)$!$1!;
         $filename =~ s/^\.+//g;
         $filename = encode_utf8mimewords($filename);
         $tmploc = "$dir/" . $username . "_attachment_" . $filename;
         $logger->debug($user->getUsername() . ": Saving in $tmploc");
      
         if(-f $tmploc) {
            unlink $tmploc;
         }
         $upload->link($tmploc);
         $logger->debug($user->getUsername() . ": Adding $filename at $tmploc as attachment.");
         my $cnt = $composition->addAttachment($filename, $tmploc);
         if($cnt && $composition->getSize > $config->get('max_message_size')) {
            $error = "Attachment failed. Maximum message size would be exceeded. Try using file sharing instead."; 
            $composition->removeAttachment($cnt-1);
         }
         $logger->debug($user->getUsername() . ": Added $filename at $tmploc to active composition");
      }
   };
   if($@) {
      $logger->error($user->getUsername() . ": Unable to get attachment: $@");
      $error = "$@";
   }

   $composition->setAddresses($to, $cc, $bcc);
   $composition->setSubject($subject = Encode::decode_utf8($subject));
   $composition->setBody($body = Encode::decode_utf8($body));
   $composition->save();
};
if($@ || $error) {
   $error = "internal state error" if(!$error);
   $logger->error($user->getUsername() . ": Unable to properly save composition: $@");
   $m->redirect(build_url("$base/mail/compose.html", { message => $error}));
}

if($save) {
   eval {
      $composition->save_draft($c, $prefs->get('drafts_folder', $config->get("default_drafts", 'none')));
   };
   if($@) {
      $error = "Unable to save draft: $@";
      $logger->error($user->getUsername() . ": Unable to save draft: $@");
   } else {
      $m->redirect(build_url("$base/mail/compose.html", { message => 'Draft saved.' }));
   }
} elsif($send) {
   if($prefs->get('spellcheckonsend', 0) && !$spelling_checked) {
      $m->redirect("$base/mail/check_spelling.html");
   }
   # Check session timeout first.
   my $timeout = $config->get('session_timeout', 300);
   my $lasttime = $user->getAttribute('lasttime');
   if(defined($lasttime)) {
      if(tv_interval( $lasttime, [gettimeofday]) > $timeout) {
         $user->setAttribute($r, 'intended_dest', "$base/mail/process_compose.html");
         $user->setAttribute($r, 'intended_args', [ %ARGS ]);
         $m->redirect("$base/mail/verify_password.html");
      }
   }
   $user->setAttribute($r, 'lasttime', [ gettimeofday ]);

   $logger->debug($user->getUsername() . ": Attempting to send a message.");
   if($to || $cc || $bcc) {
      eval {
         # Pass send the local file for temp dump, so we can save to sent folder
         my $result;
         $logger->debug($user->getUsername() . ": Sending. Saving tmp in $dir");
         $result = $composition->send($config, "$dir/${username}_compose.msg");
         @sent_to = @{$result->{sent_to}};
         @not_sent_to = @{$result->{not_sent}};
         $error = $result->{error};
         map { $_ = mimewords_to_utf8($_) } @sent_to;
         map { $_ = mimewords_to_utf8($_) } @not_sent_to;
      };

      if(scalar(@sent_to)) { # If we managed to send it somewhere...
         # Mark message as replied or forwarded...
         if($composition->isReply() || $composition->isForward()) {
            eval {
               my ($m, $n, $o) = $composition->getMsgref;
               if($composition->isReply()) {
                  $logger->debug($user->getUsername() . ": marking message answered: $m, $n, $o");
                  $c->markReplied($m, $n, $o);
               } else {
                  $logger->debug($user->getUsername() . ": marking message fowarded: $m, $n, $o");
                  $c->setFlag($m, $n, $o, 'W', 1);
               }
            };
            if($@) {
               $logger->error($user->getUsername() . ": Problem marking message: $@");
               # Reconnect
               $c = new AlphaMail::Middleware($user->getAttribute('imap'), $user->getUsername(), $user->getAttribute('password'), $config);
            }
         }

         if($sent_folder ne 'none' && -r "$dir/${username}_compose.msg") {
            eval {
               $logger->debug($user->getUsername() . ": Saving to $sent_folder folder");
               $c->appendMessage($sent_folder, "$dir/${username}_compose.msg");
               $saved = 1;
            };
            if($@) {
               $logger->error($user->getUsername() . ": Message not saved in $sent_folder");
               $error = "Unable to save message to $sent_folder!";
            }
         } else {
            $logger->error($user->getUsername() . ": No tmp file to use for saving to Sent");
         }
      }

      if($error || scalar(@sent_to) == 0 || scalar(@not_sent_to) > 0) {
         local $" = ", ";
         my $msg = encode_entities((scalar(@sent_to) ? "This message was sent to @sent_to. ":"") . "The address(es) @not_sent_to were invalid. Please check those addresses (and perhaps your Auto-BCC in settings) and re-send.");
         $logger->error($user->getUsername() . ": The SMTP system indicated '$error'");
         $composition->setAddresses("@not_sent_to", "", "");
         $composition->save();
         $m->redirect(build_url("$base/mail/compose.html", { message => $msg }));
      } elsif($composition->{isresume}) { # Delete the draft
         $composition->remove_draft($user, $config) if($delete_drafts);
      }

      unlink("$dir/${username}_compose.msg");
      $composition->save();
   } else {
      # Pretend you didn't just try to send...makes error code easier in 
      # HTML section.
      $send = 0; 
      $error = "You must specify a recipient!";
   }
} 

eval {
   $composition->save();
}; 
if($@) {
   $logger->error($user->getUsername() . ": SAVE FAILED: $@");
}

if($spellcheck) {
   $m->redirect("$base/mail/check_spelling.html");
} elsif($address) {
   $m->redirect("$base/mail/addresslist.html");
}

</%init>
<%once>
use AlphaMail::HTMLHelper qw(build_url);
use Time::HiRes qw( gettimeofday tv_interval );
use Encode;
use AlphaMail::MailTool qw(encode_utf8addresses encode_utf8address encode_utf8mimewords mimewords_to_utf8);
use HTML::Entities qw(encode_entities);
our $VERSION = $AlphaMail::VERSION;
our $logger = Log::Log4perl->get_logger('alphamail');
our $base = $config->get('base_uri');
</%once>




See more files for this project here

AlphaMail

AlphaMail is an accelerated web mail interface with a C++ middleware layer that is more effective than an IMAP proxy which is a highly scalable (10k+ users). The interface includes modern features, Section 508 compliance, and universal browser support.

Project homepage: http://sourceforge.net/projects/alphamail
Programming language(s): C++,Java,JavaScript,Perl
License: other

  addressbook/
    edit.mhtml
    import.mhtml
    import_complete.html
    import_csv.html
    import_csv_save.html
    import_fname.html
    import_imho.html
    import_lname.html
    index.html
    lists.mhtml
    process_edit.html
    process_lists.html
    process_take.html
    take.html
  admin/
    autohandler
    guide.html
    index.html
    quotas.jpg
    show_stats.mhtml
    test.mhtml
  docs/
    faq/
      attach1.gif
      attach2.gif
      attach3.gif
      colors.gif
      dates1.gif
      drafts.gif
      filter.gif
      index.html
      notrash1.gif
      notrash2.gif
      notrash3.gif
      other.html
      sort.gif
      timeout.gif
    index.html
    new_features.html
  settings/
    add_filter.html
    altfolder_update.html
    altprefix_update.html
    edit_folders.html
    export.mhtml
    export_mail.html
    filters.mhtml
    folders.mhtml
    general.mhtml
    index.html
    share.mhtml
    sharing_agree.html
    signatures.mhtml
    update_folderlist.html
    update_general.html
    update_signatures.html
  viewers/
    excel.html
    html.html
    targz.html
    text.html
    word.html
    zip.html
  address_mail.html
  addresslist.html
  autohandler
  check_spelling.html
  compose.html
  feedback.html
  first_login.html
  fix_spelling.html
  folderlist.mhtml
  footer.mhtml
  get_attachment.html
  header.mhtml
  help.html
  index.html
  logout.html
  menu.mhtml
  other_folders.html
  process_compose.html
  process_first_login.html
  process_messages.html
  process_read_message.html
  process_search.html
  quota.html
  quota_graph.html
  raw.html
  read.html
  remove_attachment.html
  renew_session.html
  resume_compose.html
  sanitized.html
  share_options.html
  share_upload.html
  verify_password.html
  view_attachment.html