Index: mailhandler.module
===================================================================
RCS file: /cvs/drupal-contrib/contributions/modules/mailhandler/mailhandler.module,v
retrieving revision 1.69
diff -u -F^f -r1.69 mailhandler.module
--- mailhandler.module	17 Nov 2004 18:00:13 -0000	1.69
+++ mailhandler.module	18 Nov 2004 00:51:53 -0000
@@ -1,5 +1,5 @@
 <?php
-// $Id: mailhandler.module,v 1.69 2004/11/17 18:00:13 javanaut Exp $
+// $Id: mailhandler.module,v 1.67.2.1 2004/10/21 18:03:23 weitzman Exp $
 
 /**
  * Retrieve all msgs from a given mailbox and process them.
@@ -45,31 +45,43 @@ function mailhandler_retrieve($mailbox) 
       $node = mailhandler_authenticate($node, $header, $origbody, $mailbox);
 
       // we need to change the current user
-      mailhandler_switch_user($node->uid);
-      // modules may override node elements before submitting. they do so by returning the node.
-      foreach (module_list() as $name) {
-        if (module_hook($name, "mailhandler")) {
-          $function = $name. "_mailhandler";
-          if (!($node = $function($node, $result, $i, $header, $mailbox))) {
-            // Exit if a module has handled the submitted data.
-            break;
+      $mail_user = mailhandler_switch_user($node->uid);
+      
+      // don't process if user not allowed to post via mail
+      if (user_access('post by mail', $mail_user)) {
+        // modules may override node elements before submitting. they do so by returning the node.
+        foreach (module_list() as $name) {
+          if (module_hook($name, "mailhandler")) {
+            $function = $name. "_mailhandler";
+            if (!($node = $function($node, $result, $i, $header, $mailbox))) {
+              // Exit if a module has handled the submitted data.
+              break;
+            }
           }
         }
-      }
-
-      if ($node) {
-        if ($node->type == "comment") {
-          mailhandler_comment_submit($node, $header, $mailbox, $origbody);
-        }
-        else {
-          mailhandler_node_submit($node, $header, $mailbox, $origbody);
+  
+        if ($node) {
+          if ($node->type == "comment") {
+            mailhandler_comment_submit($node, $header, $mailbox, $origbody);
+          }
+          else {
+            mailhandler_node_submit($node, $header, $mailbox, $origbody);
+          }
         }
       }
+      else {
+        drupal_set_message(t('You are not allowed to post via email from this address'));
+        mailhandler_set_message(t('You are not allowed to post via email from this address'), 'error');
+      }
+      
       // don't delete while we're only getting new messages
       if($mailbox["delete_after_read"]) {
         imap_delete($result, $i);
       }
-
+      
+      // send email response if warranted
+      mailhandler_email_response($header, $mailbox, $origbody);
+      
       // switch back to original user
       mailhandler_switch_user();
     }
@@ -292,6 +304,7 @@ function mailhandler_switch_user($newuid
   else {
     $user = $orig_user;
   }
+  return $user;
 }
 
 /**
@@ -379,23 +392,147 @@ function mailhandler_get_mime_type(&$str
   return "TEXT/PLAIN";
 }
 
+/*
+ * Initialize a globally scoped array to hold messages.
+ * Each set of messages will only exist for the amount of
+ * time it takes to process a mail message.
+ */
+$_mailhandler_messages = array();
+
+/**
+ * Set a message for the mail sender to see.
+ *
+ * The message is stored in the globally scoped $_mailhandler_messages array.
+ *
+ * If called with no arguments, this function returns all set messages without
+ * clearing them.
+ *
+ * @param $message The text message to send to the user
+ * @param $type    The message type, typically 'info', 'error' or 'debug'
+ *
+ * @return The current array of all message arrays.
+ */
+function mailhandler_set_message($message = NULL, $type = 'info') {
+  global $_mailhandler_messages;
+  
+  if (isset($message)) {
+    if (!is_array($_mailhandler_messages)) {
+      $_mailhandler_messages = array();
+    }
+
+    if (!isset($_mailhandler_messages[$type])) {
+      $_mailhandler_messages[$type] = array();
+    }
+
+    $_mailhandler_messages[$type][] = $message;
+  }
+
+  return $_mailhandler_messages;
+}
+
+/**
+ * Return all messages that have been set.
+ *
+ * As a side effect, this function clears the message queue.
+ */
+function mailhandler_get_messages() {
+  global $_mailhandler_messages;
+  
+  $messages = mailhandler_set_message();
+  $_mailhandler_messages = array();
+
+  return $messages;
+}
+
+/**
+ * Sends an email to the sender indicating errors that occurred and/or
+ * information about processing that went on.
+ *
+ * @param header   A string of headers to add to the email
+ * @param mailbox  The mailbox currently being processed
+ * @param origbody The original body of the message that was processed (just the first text part)
+ *
+ * @return nothing
+ */
+function mailhandler_email_response($header, $mailbox, $origbody) {
+  global $user;
+  $msg = '';
+  $errs = false;
+  
+  $messages = mailhandler_get_messages();
+  if (count($messages)) {
+    if($user->alternate_mailhandler_email && valid_email_address($user->alternate_mailhandler_email)) {
+      $recipient = $user->alternate_mailhandler_email;
+    }
+    else {
+      $recipient = mailhandler_get_fromaddress($header, $mailbox);
+    }
+    $sitemail = variable_get("site_mail", ini_get("sendmail_from"));
+    $headers = "From: $sitemail\nReply-to: $sitemail\nX-Mailer: Drupal\nReturn-path: $sitemail\nErrors-to: $sitemail";
+    
+    // send errors to anonymous posters who encountered errors or any valid user who enabled send_mailhandler_errors checkbox
+    if (isset($messages['error']) && is_array($messages['error']) && (!$user->uid || $user->send_mailhandler_errors)) {
+      // $recipient really refers to the mail header which is authoritative for authentication
+      $error_body = implode('\n', $messages['error']);
+      $msg .= variable_get("mailhandler_error_title", t('There were errors with your post'))."\n".$error_body."\n";
+      $errs = true;
+    }
+    
+    if (isset($messages['info']) && is_array($messages['info']) && $user->send_mailhandler_info) {
+      // $recipient really refers to the mail header which is authoritative for authentication
+      $info_body = implode("\n", $messages['info']);
+      $msg .= variable_get("mailhandler_info_title", t('Here is info about your post'))."\n".$info_body."\n";
+    }
+    
+    if($msg) {
+      $msg .= t("\n\nYou sent:\n\nFrom: %f\nSubject: %t\nBody:\n%b", array ("%f" => $recipient, "%t" => $header->subject, "%b" => $origbody));
+    
+      if($errs) {
+        watchdog('mailhandler', "Sending error email to sender $recipient");
+        user_mail($recipient, t(variable_get("mailhandler_error_subject", "Email submission to %site_name failed - %subj"), array ("%site_name" => variable_get("site_name", "Drupal"), "%subj" => $header->subject)), $msg, $headers);
+      }
+      else {
+        watchdog('mailhandler', "Sending info email to sender $recipient");
+        user_mail($recipient, t(variable_get("mailhandler_info_subject", "Email submission to %site_name information - %subj"), array ("%site_name" => variable_get("site_name", "Drupal"), "%subj" => $header->subject)), $msg, $headers);
+      }
+    }
+  }
+}
+
 //
 // MODULE HOOKS
 //
-function mailhandler_user($type, &$edit, &$user) {
+function mailhandler_user($type, $edit, &$user, $category = NULL) {
   // for now, just show the first mailbox address to user.
-  switch ($type) {
-    case "view_private":
-      if (user_access("maintain personal blog")) {
+  $data = "";
+  
+  if (user_access("post by mail") && $category == 'account') {
+    switch ($type) {
+      case "view":
         $result = db_fetch_array(db_query("SELECT * FROM {mailhandler} WHERE enabled = '1' ORDER BY mail"));
         if ($result["security"] == 1) {
-          return form_item(t("Mail Handler"), t("You may post to <i>%sn</i> by sending an e-mail to <i>%e</i>. Be sure to include your password at the top of your e-mail body (e.g. <i>pass=mypassword</i>).", array ("%sn" => variable_get("site_name", "Drupal"), "%a" => url("blog/$user->uid"), "%e" => $result["mail"])));
+          $data .= form_item(t("Mail Handler Usage"), t("You may post to <i>%sn</i> by sending an e-mail to <i>%e</i>. Be sure to include your password at the top of your e-mail body (e.g. <i>pass=mypassword</i>).", array ("%sn" => variable_get("site_name", "Drupal"), "%a" => url("blog/$user->uid"), "%e" => $result["mail"])));
         }
         else {
-          return form_item(t("Mail Handler"), t("You may post to <i>%sn</i> by sending an e-mail to <i>%e</i>.", array ("%sn" => variable_get("site_name", "Drupal"), "%a" => url("blog/$user->uid"), "%e" => $result["mail"])));
+          $data .= form_item(t("Mail Handler Usage"), t("You may post to <i>%sn</i> by sending an e-mail to <i>%e</i>.", array ("%sn" => variable_get("site_name", "Drupal"), "%a" => url("blog/$user->uid"), "%e" => $result["mail"])));
         }
-      }
+        break;
+        
+      case "form":
+        $data .= form_checkbox(t('Send Error Replies'), 'send_mailhandler_errors', 1, $user->send_mailhandler_errors, t('Should error messages from processing posts by email be sent to the sending email address?'));
+        $data .= form_checkbox(t('Send Info Replies'), 'send_mailhandler_info', 1, $user->send_mailhandler_info, t('Should informational messages from processing posts by email be sent to the sending email address?'));
+        $data .= form_textfield(t('Alternate Email Address'), 'alternate_mailhandler_email', $edit['alternate_mailhandler_email'] ? $edit['alternate_mailhandler_email'] : $user->alternate_mailhandler_email, 50, 255, t('If specified, send all error/informational messages to this address instead of the address they came from.'));
+        break;
+        
+      case "validate":
+        if (strlen($edit['alternate_mailhandler_email']) > 0 && !valid_email_address($edit['alternate_mailhandler_email'])) {
+          form_set_error('alternate_mailhandler_email', t('The e-mail address %mail is not valid.', array('%mail' => "<em>".$edit['alternate_mailhandler_email']."</em>")));
+        }
+        break;
+    }
+    $data_items[] = array('title' => t('Mail Handler'), 'data' => $data, 'weight' => 2);
   }
+  return $data_items;
 }
 
 /**
@@ -410,7 +547,7 @@ function mailhandler_cron() {
 }
 
 function mailhandler_perm() {
-  return array("administer mailhandler");
+  return array("administer mailhandler","post by mail");
 }
 
 function mailhandler_menu($may_cache) {
@@ -712,4 +849,16 @@ function mailhandler_help($section = 'ad
   return $output;
 }
 
+function mailhandler_settings() {
+  
+  $frmout = form_textfield(t('Error Reply Subject'), "mailhandler_error_subject", variable_get("mailhandler_error_subject", t('Email submission to %site_name failed - %subj')), 50, 255, t("This is the email subject line used when system reply emails are sent reporting processing errors. <br /><strong>%site_name</strong> and <strong>%subj</strong> are replaced by your site's name and the subject of the originally posted email."));
+  $frmout .= form_textarea(t('Error Reply Title'), "mailhandler_error_title", variable_get("mailhandler_error_title", t('There were errors with your post')), 55, 5, t("This is the title of the errors section used when system reply emails are sent reporting processing errors."));
+  $out = form_group(t('Errors'), $frmout);
+  
+  $frmout = form_textfield(t('Information Reply Subject'), "mailhandler_info_subject", variable_get("mailhandler_info_subject", t('Email submission to %site_name information - %subj')), 50, 255, t("This is the email subject line used when system reply emails sent containing reporting processing information. <br /><strong>%site_name</strong> and <strong>%subj</strong> are replaced by your site's name and the subject of the originally posted email."));
+  $frmout .= form_textarea(t('Information Reply Title'), "mailhandler_info_title", variable_get("mailhandler_info_title", t('Here is info about your post')), 55, 5, t("This is the title of the information section used when system reply emails sent containing reporting processing information."));
+  $out .= form_group(t('Information'), $frmout);
+  
+  return $out;
+}
 ?>
