--- mailhandler.admin.inc.orig	2008-10-26 22:43:24.000000000 +0100
+++ mailhandler.admin.inc	2008-12-27 17:06:51.000000000 +0100
@@ -1,13 +1,30 @@
 <?php
+// $Id
 
 /**
  * Return a form for editing or creating an individual mailbox.
  */
 function mailhandler_add_edit_mailbox($form_state, $mid, $clone = FALSE) {
 
-  if ($mid && !$form_state['post']) {
-  	$edit = mailhandler_get_mailbox($mid);
-  }
+  $edit = $mid ? mailhandler_get_mailbox($mid) : array(
+    'mail' => '',
+    'mailto' => '',
+    'imap' => 1,
+    'domain' => '',
+    'port' => '',
+    'name' => '',
+    'pass' => '',
+    'extraimap' => '',
+    'mime' => 'TEXT/HTML,TEXT/PLAIN',
+    'security' => 0,
+    'replies' => 1,
+    'fromheader' => '',
+    'commands' => '',
+    'sigseparator' => '',
+    'delete_after_read' => 1,
+    'enabled' => 1,
+    'format' => FILTER_FORMAT_DEFAULT,
+  );
 
   if ($clone) {
     unset($mid);
@@ -185,7 +202,7 @@
   // Test POP/IMAP settings are valid in all cases, and test connection if requested
   if ($mailbox_appears_ok && $form_state['clicked_button']['#value'] == t('Test connection')) {
     // Call the test function
-    mailhandler_test_mailbox($form_state);
+    mailhandler_test_mailbox($form_state['values']);
 
     // We don't want to save, so we set an 'invisible error' that means the form is not submitted
     form_set_error('mailhandler');
@@ -258,9 +275,8 @@
 /**
  * Test connection to a mailbox, and output results as a message
  */
-function mailhandler_test_mailbox(&$form_state) {
+function mailhandler_test_mailbox($mailbox) {
 
-  $mailbox = $form_state['values'];
   $result = mailhandler_open_mailbox($mailbox);
 
   if ($result) {
@@ -275,8 +291,6 @@
       form_set_error('mailhandler', t('Mailhandler could not access local folder: %m', array('%m' => $mailbox['mail'])));
     }
   }
-
-  return;
 }
 
 
@@ -289,10 +303,10 @@
     $rows[] = array(
       '<a href="mailto:'. $mailbox->mail .'">'. $mailbox->mail .'</a>',
       $mailbox->folder ? check_plain($mailbox->folder) : '',
-      l(t('Retrieve'), 'admin/content/mailhandler/retrieve/'.$mailbox->mid, array('title' => t('Retrieve and process pending e-mails in this mailbox')), $destination),
-      l(t('Edit'), 'admin/content/mailhandler/edit/'.$mailbox->mid, array('title' => t('Edit this mailbox configuration')), $destination),
-      l(t('Clone'), 'admin/content/mailhandler/clone/'.$mailbox->mid, array('title' => t('Add a new mailbox, using this mailbox configuration as a template')), $destination),
-      l(t('Delete'), 'admin/content/mailhandler/delete/'.$mailbox->mid, array('title' => t('Delete this mailbox')), $destination),
+      l(t('Retrieve'), 'admin/content/mailhandler/retrieve/'.$mailbox->mid, array('title' => t('Retrieve and process pending e-mails in this mailbox'))),
+      l(t('Edit'), 'admin/content/mailhandler/edit/'.$mailbox->mid, array('title' => t('Edit this mailbox configuration'))),
+      l(t('Clone'), 'admin/content/mailhandler/clone/'.$mailbox->mid, array('title' => t('Add a new mailbox, using this mailbox configuration as a template'))),
+      l(t('Delete'), 'admin/content/mailhandler/delete/'.$mailbox->mid, array('title' => t('Delete this mailbox'))),
     );
   }
 
--- mailhandler.install.orig	2008-10-26 18:32:40.000000000 +0100
+++ mailhandler.install	2008-12-27 17:04:10.000000000 +0100
@@ -1,4 +1,5 @@
 <?php
+// $Id
 
 /**
  * Implementation of hook_install().
@@ -149,7 +150,8 @@
  */
 function mailhandler_requirements($phase) {
   // Ensure translations don't break at install time
-  $t = get_t();$has_imap = function_exists('imap_open');
+  $t = get_t();
+  $has_imap = function_exists('imap_open');
   
   $requirements['mailhandler'] = array(
     'title' => $t('IMAP'),
@@ -163,4 +165,4 @@
 
 /**
 
-**/
\ No newline at end of file
+**/
--- mailhandler.retrieve.inc.orig	2008-12-27 17:02:54.000000000 +0100
+++ mailhandler.retrieve.inc	2008-12-27 17:03:11.000000000 +0100
@@ -1,14 +1,12 @@
 <?php
-
+// $Id
 
 function mailhandler_admin_retrieve($mid) {
 
   // Retrieve mailbox details for specified mailbox number
   $mailbox = mailhandler_get_mailbox($mid);
-
   // Check to see if there are new messages requiring retrieval
   $messages = mailhandler_get_unread_messages($mailbox);
-
   // If messages available, set batch and begin processing
   if ($messages) {
     foreach ($messages as $message_number) {
@@ -245,7 +243,7 @@
   if (isset($threading) && $threading = rtrim(ltrim($threading, '<'), '>')) { //strip angle brackets
     if ($threading) $node->threading = $threading;
     parse_str($threading, $tmp);
-    if ($tmp['host']) {
+    if (!empty($tmp['host'])) {
       $tmp['host'] = ltrim($tmp['host'], '@'); // strip unnecessary @ from 'host' element
     }
     foreach ($tmp as $key => $value) {
@@ -254,12 +252,12 @@
   }
 
   // Prepend the default commands for this mailbox
-  if ($mailbox['commands']) {
+  if (!empty($mailbox['commands'])) {
     $body = trim($mailbox['commands']) ."\n". $body;
   }
 
   // Set a default type if none provided
-  if (!$node->type) $node->type = mailhandler_default_type();
+  if (empty($node->type)) $node->type = mailhandler_default_type();
 
   // Apply defaults to the $node object, and allow modules to add default values
   require_once($base_path . 'modules/node/node.pages.inc');
@@ -344,7 +342,7 @@
       $node->title .= $subjectarr[$i]->text;
   }
 
-  $node->date = format_date($header->udate, 'custom', 'Y-m-d H:i:s O');
+  $node->date = $node->changed = $node->created = format_date($header->udate, 'custom', 'Y-m-d H:i:s O');
   $node->format = $mailbox['format'];
 
   return $node;
@@ -386,10 +384,12 @@
  */
 function mailhandler_comment_submit($node, $header, $mailbox, $origbody) {
   global $user;
-  if (!$node->subject) $node->subject = $node->title;
-  if (!$node->comment) $node->comment = $node->body;
+
+  if (empty($node->subject)) $node->subject = $node->title;
+  if (empty($node->comment)) $node->comment = $node->body;
   // We want the comment to have the email time, not the current time
   $node->timestamp = $node->created;
+
   // comment_save gets an array
   $edit = (array)$node;
 
@@ -528,7 +528,6 @@
   }
 }
 
-
 /**
  * If available, use the mail header specified in mailbox config. otherwise use From: header
  */
@@ -560,6 +559,7 @@
   if (isset($uid)) {
     session_save_session(FALSE);
     $user = user_load(array('uid' => $uid));
+    $user->hostname = '';
   }
   // retrieve the initial user, can be called multiple times
   else if (count($orig_user)) {
@@ -592,7 +592,6 @@
     $number_of_messages = imap_num_msg($result);
     for ($i = 1; $i <= $number_of_messages; $i++) {
       $header = imap_header($result, $i);
-
       // only process new messages
       if (!$mailbox['delete_after_read'] && $header->Unseen != 'U' && $header->Recent != 'N') {
         continue;
