Index: mailhandler.module
===================================================================
RCS file: /cvs/drupal-contrib/contributions/modules/mailhandler/mailhandler.module,v
retrieving revision 1.72.2.2
diff -u -p -r1.72.2.2 mailhandler.module
--- mailhandler.module	20 Apr 2005 17:00:34 -0000	1.72.2.2
+++ mailhandler.module	30 Jun 2005 23:43:49 -0000
@@ -45,6 +45,11 @@ function mailhandler_retrieve($mailbox) 
       // check if mail originates from an authenticated user
       $node = mailhandler_authenticate($node, $header, $origbody, $mailbox);
 
+      // we need to change the current user
+      // this has to be done here to allow modules
+      // to create users
+      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")) {
@@ -56,11 +61,6 @@ function mailhandler_retrieve($mailbox) 
         }
       }
 
-      // we need to change the current user
-      // this has to be done here to allow modules
-      // to create users
-      mailhandler_switch_user($node->uid);
-
       if ($node) {
         if ($node->type == "comment") {
           mailhandler_comment_submit($node, $header, $mailbox, $origbody);
@@ -74,7 +74,7 @@ function mailhandler_retrieve($mailbox) 
         imap_delete($result, $i);
       }
 
-      // switch back to original user
+      // switch back to cron user
       mailhandler_switch_user();
     }
     imap_close($result, CL_EXPUNGE);
@@ -119,38 +119,28 @@ function mailhandler_comment_post($edit)
   global $user;
 
   if (user_access('post comments') && node_comment_mode($edit['nid']) == 2) {
-
-    // Validate the comment's subject.  If not specified, extract
-    // one from the comment's body.
-    $edit['subject'] = strip_tags($edit['subject']);
-
-    if ($edit['subject'] == '') {
-      $edit['subject'] = truncate_utf8(strip_tags($edit['comment']), 29);
-    }
-
-    if (!form_get_errors()) {
+      // Removed for auto-compatibility
+      // if (!form_get_errors()) {
       // Check for duplicate comments.  Note that we have to use the
       // validated/filtered data to perform such check.
-
-      $duplicate = db_result(db_query("SELECT COUNT(cid) FROM {comments} WHERE pid = %d AND nid = %d AND subject = '%s' AND comment = '%s'", $edit["pid"], $edit["nid"], $edit['subject'], $edit['comment']), 0);
+      $duplicate = db_result(db_query("SELECT COUNT(cid) FROM {comments} WHERE pid = %d AND nid = %d AND subject = '%s' AND comment = '%s'", $edit['pid'], $edit['nid'], $edit['subject'], $edit['comment']), 0);
       if ($duplicate != 0) {
-        watchdog('warning', t('Comment: duplicate %subject.', array('%subject' => '<em>'. $edit["subject"] .'</em>')));
-        return array(t('Duplicate subject'), t('Duplicate subject'));
+        watchdog('content', t('Comment: duplicate %subject.', array('%subject' => theme('placeholder', $edit['subject']))), WATCHDOG_WARNING);
       }
 
-      if ($edit["cid"]) {
+      if ($edit['cid']) {
         // Update the comment in the database.  Note that the update
         // query will fail if the comment isn't owned by the current
         // user.
-        db_query("UPDATE {comments} SET subject = '%s', comment = '%s', format = '%s' WHERE cid = %d AND uid = '$user->uid'", $edit['subject'], $edit['comment'], $edit['format'], $edit["cid"]);
+        db_query("UPDATE {comments} SET subject = '%s', comment = '%s', format = '%s' WHERE cid = %d AND uid = %d", $edit['subject'], $edit['comment'], $edit['format'], $edit['cid'], $user->uid);
 
         _comment_update_node_statistics($edit['nid']);
 
         // Allow modules to respond to the updating of a comment.
         module_invoke_all('comment', 'update', $edit);
 
-        // Add entry to the watchdog log.
-        watchdog('special', t('Comment: updated %subject.', array('%subject' => '<em>'. $edit['subject'] .'</em>')), l(t('view'), 'node/'. $edit['nid'], NULL, NULL, 'comment-'. $edit['cid']));
+        // Add an entry to the watchdog log.
+        watchdog('content', t('Comment: updated %subject.', array('%subject' => theme('placeholder', $edit['subject']))), WATCHDOG_NOTICE, l(t('view'), 'node/'. $edit['nid'], NULL, NULL, 'comment-'. $edit['cid']));
       }
       else {
         // Add the comment to database.
@@ -164,30 +154,21 @@ function mailhandler_comment_post($edit)
 
         $users = serialize(array(0 => $score));
 
-        /*
-        ** Here we are building the thread field.  See the comment
-        ** in comment_render().
-        */
-
+        // Here we are building the thread field.  See the comment
+        // in comment_render().
         if ($edit['pid'] == 0) {
-          /*
-          ** This is a comment with no parent comment (depth 0): we start
-          ** by retrieving the maximum thread level.
-          */
-
+          // This is a comment with no parent comment (depth 0): we start
+          // by retrieving the maximum thread level.
           $max = db_result(db_query('SELECT MAX(thread) FROM {comments} WHERE nid = %d', $edit['nid']));
 
           // Strip the "/" from the end of the thread.
           $max = rtrim($max, '/');
 
-          /*
-          ** Next, we increase this value by one.  Note that we can't
-          ** use 1, 2, 3, ... 9, 10, 11 because we order by string and
-          ** 10 would be right after 1.  We use 1, 2, 3, ..., 9, 91,
-          ** 92, 93, ... instead.  Ugly but fast.
-          */
-
-          $decimals = (string)substr($max, 0, strlen($max) - 1);
+          // Next, we increase this value by one.  Note that we can't
+          // use 1, 2, 3, ... 9, 10, 11 because we order by string and
+          // 10 would be right after 1.  We use 1, 2, 3, ..., 9, 91,
+          // 92, 93, ... instead.  Ugly but fast.
+          $decimals = (string) substr($max, 0, strlen($max) - 1);
           $units = substr($max, -1, 1);
           if ($units) {
             $units++;
@@ -201,26 +182,24 @@ function mailhandler_comment_post($edit)
           }
 
           // Finally, build the thread field for this new comment.
-          $thread = "$decimals$units/";
+          $thread = $decimals . $units .'/';
         }
         else {
-          /*
-          ** This is comment with a parent comment: we increase
-          ** the part of the thread value at the proper depth.
-          */
+          // This is comment with a parent comment: we increase
+          // the part of the thread value at the proper depth.
 
           // Get the parent comment:
           $parent = db_fetch_object(db_query('SELECT * FROM {comments} WHERE cid = %d', $edit['pid']));
 
           // Strip the "/" from the end of the parent thread.
-          $parent->thread = (string)rtrim((string)$parent->thread, '/');
+          $parent->thread = (string) rtrim((string) $parent->thread, '/');
 
           // Get the max value in _this_ thread.
           $max = db_result(db_query("SELECT MAX(thread) FROM {comments} WHERE thread LIKE '%s.%%' AND nid = %d", $parent->thread, $edit['nid']));
 
           if ($max == '') {
             // First child of this parent.
-            $thread = "$parent->thread.1/";
+            $thread = $parent->thread .'.1/';
           }
           else {
             // Strip the "/" at the end of the thread.
@@ -231,13 +210,10 @@ function mailhandler_comment_post($edit)
             $parent_depth = count(explode('.', $parent->thread));
             $last = $parts[$parent_depth];
 
-            /*
-            ** Next, we increase this value by one.  Note that we can't
-            ** use 1, 2, 3, ... 9, 10, 11 because we order by string and
-            ** 10 would be right after 1.  We use 1, 2, 3, ..., 9, 91,
-            ** 92, 93, ... instead.  Ugly but fast.
-            */
-
+            // Next, we increase this value by one.  Note that we can't
+            // use 1, 2, 3, ... 9, 10, 11 because we order by string and
+            // 10 would be right after 1.  We use 1, 2, 3, ..., 9, 91,
+            // 92, 93, ... instead.  Ugly but fast.
             $decimals = (string)substr($last, 0, strlen($last) - 1);
             $units = substr($last, -1, 1);
             $units++;
@@ -246,12 +222,12 @@ function mailhandler_comment_post($edit)
             }
 
             // Finally, build the thread field for this new comment.
-            $thread = "$parent->thread.". $decimals.$units .'/';
+            $thread = $parent->thread .'.'. $decimals . $units .'/';
           }
         }
 
 
-        $edit["cid"] = db_next_id("{comments}_cid");
+        $edit['cid'] = db_next_id('{comments}_cid');
         $edit['timestamp'] = time();
 
         if ($edit['uid'] = $user->uid) {
@@ -259,7 +235,7 @@ function mailhandler_comment_post($edit)
         }
 
 
-        db_query("INSERT INTO {comments} (cid, nid, pid, uid, subject, comment, format, hostname, timestamp, status, score, users, thread, name, mail, homepage) VALUES (%d, %d, %d, %d, '%s', '%s', %d, '%s', %d, %d, %d, '%s', '%s', '%s', '%s', '%s')", $edit["cid"], $edit["nid"], $edit["pid"], $edit['uid'], $edit['subject'], $edit['comment'], $edit['format'], $_SERVER['REMOTE_ADDR'], $edit['timestamp'], $status, $score, $users, $thread, $edit["name"], $edit['mail'], $edit["homepage"]);
+        db_query("INSERT INTO {comments} (cid, nid, pid, uid, subject, comment, format, hostname, timestamp, status, score, users, thread, name, mail, homepage) VALUES (%d, %d, %d, %d, '%s', '%s', %d, '%s', %d, %d, %d, '%s', '%s', '%s', '%s', '%s')", $edit['cid'], $edit['nid'], $edit['pid'], $edit['uid'], $edit['subject'], $edit['comment'], $edit['format'], $_SERVER['REMOTE_ADDR'], $edit['timestamp'], $status, $score, $users, $thread, $edit['name'], $edit['mail'], $edit['homepage']);
 
         _comment_update_node_statistics($edit['nid']);
 
@@ -267,27 +243,29 @@ function mailhandler_comment_post($edit)
         module_invoke_all('comment', 'insert', $edit);
 
         // Add an entry to the watchdog log.
-        watchdog('special', t('Comment: added %subject.', array('%subject' => '<em>'. $edit['subject'] .'</em>')), l(t('view'), 'node/'. $edit['nid'], NULL, NULL, 'comment-'. $edit['cid']));
+        watchdog('content', t('Comment: added %subject.', array('%subject' => theme('placeholder', $edit['subject']))), WATCHDOG_NOTICE, l(t('view'), 'node/'. $edit['nid'], NULL, NULL, 'comment-'. $edit['cid']));
       }
 
       // Clear the cache so an anonymous user can see his comment being added.
       cache_clear_all();
 
-      // Explain the moderation queue if necessary, and then
+      // Explain the approval queue if necessary, and then
       // redirect the user to the node he's commenting on.
-      if ($status == 1) {
-        drupal_set_message(t('Your comment has been queued for moderation by site administrators and will be published after approval.'));
-        //drupal_goto('node/'. $edit['nid']);
-        //scratched for auto-compatibility
+      // Removed for auto-compatibility
+      /* 
+       if ($status == 1) {
+         drupal_set_message(t('Your comment has been queued for moderation by site administrators and will be published after approval.'));
+         drupal_goto('node/'. $edit['nid']);
       }
       else {
-        //drupal_goto('node/'. $edit['nid'] .'#comment-'. $edit['cid']);
-        //scratched for auto-compatibility
+         Removed for auto-compatibility
+         drupal_goto('node/'. $edit['nid'] .'#comment-'. $edit['cid']);
       }
     }
     else {
       print theme('page', comment_preview($edit));
     }
+    */
   }
   else {
     $txt = t('Comment: unauthorized comment submitted or comment submitted to a closed node %subject.', array('%subject' => '<em>'. $edit['subject'] .'</em>'));
@@ -474,19 +452,30 @@ function mailhandler_authenticate($node,
 }
 
 /**
- * Switch from origininal user to mail submision user and back.
+ * Switch from original user to mail submision user and back.
+ *
+ * Note: You first need to run mailhandler_switch_user without
+ * argument to store the current user. Call mailhandler_switch_user
+ * without argument to set the user back to the original user.
+ *
+ * @param $uid The user ID to switch to
  *
  */
-function mailhandler_switch_user($newuid = false) {
+function mailhandler_switch_user($uid = NULL) {
   global $user;
-  static $orig_user;
+  static $orig_user = array();
 
-  if ($newuid) {
-    $orig_user = $user;
-    $user = user_load(array('uid' => $newuid));
+  if (isset($uid)) {
+    $user = user_load(array('uid' => $uid));
   }
+  // retrieve the initial cron user, can be called multiple times
+  else if (count($orig_user)) {
+    $user = array_shift($orig_user);
+    array_unshift($orig_user, $user);
+  }
+  // store the initial cron user
   else {
-    $user = $orig_user;
+    $orig_user[] = $user;
   }
 }
 
@@ -544,11 +533,10 @@ function mailhandler_get_part($stream, $
       }
       $text = imap_fetchbody($stream, $msg_number, $part_number);
       if ($structure->encoding == 3) {
-        return imap_base64($text);
+        return drupal_convert_to_utf8(imap_base64($text), $encoding);
       }
       else if ($structure->encoding == 4) {
-        return quoted_printable_decode($text);
-        // return imap_qprint($text);
+        return drupal_convert_to_utf8(quoted_printable_decode($text), $encoding);
       }
       else {
         return drupal_convert_to_utf8($text, $encoding);
@@ -605,10 +593,14 @@ function mailhandler_user($type, &$edit,
  *
  */
 function mailhandler_cron() {
+  // store the original cron user
+  mailhandler_switch_user();
   $result = db_query("SELECT * FROM {mailhandler} WHERE enabled = '1' ORDER BY mail");
   while ($mailbox = db_fetch_array($result)) {
     mailhandler_retrieve($mailbox);
   }
+  // revert to the original cron user
+  mailhandler_switch_user();
 }
 
 function mailhandler_perm() {
@@ -681,8 +673,12 @@ function mailhandler_admin() {
       $output = mailhandler_display();
       break;
     case "retrieve":
+      // store the original user
+      mailhandler_switch_user();
       drupal_set_message(mailhandler_retrieve(mailhandler_get_mailbox($id)));
       $output = mailhandler_display();
+      // revert to the original user
+      mailhandler_switch_user();
       break;
     case "Delete":
       $output = mailhandler_delete_mailbox($edit);
Index: mailhandler.mysql
===================================================================
RCS file: /cvs/drupal-contrib/contributions/modules/mailhandler/mailhandler.mysql,v
retrieving revision 1.7.2.1
diff -u -p -r1.7.2.1 mailhandler.mysql
--- mailhandler.mysql	15 Apr 2005 18:56:46 -0000	1.7.2.1
+++ mailhandler.mysql	30 Jun 2005 23:43:49 -0000
@@ -21,3 +21,4 @@ ALTER TABLE mailhandler ADD mailto varch
 ALTER TABLE mailhandler ADD delete_after_read tinyint unsigned not null default '1';
 ALTER TABLE mailhandler ADD extraimap varchar(255) not null;
 ALTER TABLE mailhandler ADD format int(4) NOT NULL default '0';
+ALTER TABLE mailhandler CHANGE sigseparator sigseparator TEXT NOT NULL default '';
