--- /tmp/listhandler/listhandler.module	2011-02-01 23:13:33.000000000 +0100
+++ listhandler.module	2011-02-13 17:49:49.000000000 +0100
@@ -115,6 +115,13 @@
     '#options' => array(0 => t('Links at the bottom of emails'), 1 => t('Attachments in emails')),
     '#default_value' => variable_get('listhandler_attachments_as_link', 0),
     '#description' => t('Send attachments as links or MIME attachments. It only affects mails generated by forum posts.'));
+  $form['listhandler_accountmap'] = array(
+    '#type' => 'textfield',
+    '#maxlength' => 60,
+    '#autocomplete_path' => 'user/autocomplete',
+    '#title' => t('Account map'),
+    '#default_value' => variable_get('listhandler_accountmap', ''),
+    '#description' => t('The account to set as author of nodes created from Anonymous user mails. If empty, a new account will be created for every anonymous email with the above "Account status" setting.'));
   $form['listhandler_accountstatus'] = array(
     '#type' => 'radios',
     '#title' => t('Account status'),
@@ -166,6 +173,7 @@
   $output = '';
   $output .= drupal_render($form['listhandler_from']);
   $output .= drupal_render($form['listhandler_strip_title']);
+  $output .= drupal_render($form['listhandler_accountmap']);
   $output .= drupal_render($form['listhandler_accountstatus']);
   $output .= drupal_render($form['listhandler_attachments_as_link']);
   $output .= drupal_render($form['listhandler_htmltotext']);
@@ -188,6 +196,18 @@
   return $output;
 }
 
+function listhandler_admin_settings_validate($form, &$form_state) {
+     // Validate the "authored by" field.
+    if (!empty($form_state['values']['listhandler_accountmap']) && !($account = user_load(array('name' => $form_state['values']['listhandler_accountmap'])))) {
+      // The use of empty() is mandatory in the context of usernames
+      // as the empty string denotes the anonymous user. In case we
+      // are dealing with an anonymous user we set the user ID to 0.
+      form_set_error('name', t('The username %name does not exist.', array('%name' => $form_state['values']['listhandler_accountmap'])));
+    }
+}
+
+ /**
+  * Writes configuration settings as Drupal variables and entries in
 /**
  * Writes configuration settings as Drupal variables and entries in
  * the listhandler and mailhandler tables
@@ -519,23 +539,29 @@
   if ($from_name == '') {
     $from_name = $from_address;
   }
-  // check if name is available
-  if (db_result(db_query("SELECT COUNT(name) FROM {users} WHERE LOWER(name) = LOWER('%s')", $from_name)) > 0) {
-    $node->body = t('Message from !n !a', array('!n' => $from_name, '!a' => $from_address)) ."\n\n". $node->body;
-    watchdog('listhandler', "Cannot create account: The name '!s' is already taken.", array("!s" => $from_name), WATCHDOG_WARNING);
-  }
-  // check if address is valid
-  elseif (!valid_email_address($from_address)) {
+  $accountmap = variable_get('listhandler_accountmap', '');
+  if (!empty($accountmap) && $from_user = user_load(array('name' => $accountmap))) {
     $node->body = t('Message from !n !a', array('!n' => $from_name, '!a' => $from_address)) ."\n\n". $node->body;
-    watchdog('listhandler', "Cannot create account: The email address '!s' is not valid.", array("!s" => $from_address), WATCHDOG_WARNING);
-  }
-  // create account
-  else {
-    $empty_account = new stdClass();
-    $from_user = user_save($empty_account, array('name' => $from_name, 'pass' => user_password(), 'init' => $from_address,  'mail' => $from_address, 'roles' => array(DRUPAL_AUTHENTICATED_RID), 'status' => variable_get('listhandler_accountstatus', 0)), 'account');
-    watchdog('listhandler', 'Created account for !fa, with roles !rid', array('!fa' => $from_address, '!rid' => implode(', ', $from_user->roles)), WATCHDOG_NOTICE);
-    mailhandler_switch_user($from_user->uid);
-  }
+    watchdog('listhandler', t("Mail from '!s' mapped to '!m' account.", array("!s" => $from_name, "!m" => $accountmap)));
+  } else {
+   // check if name is available
+   if (db_result(db_query("SELECT COUNT(name) FROM {users} WHERE LOWER(name) = LOWER('%s')", $from_name)) > 0) {
+     $node->body = t('Message from !n !a', array('!n' => $from_name, '!a' => $from_address)) ."\n\n". $node->body;
+     watchdog('listhandler', "Cannot create account: The name '!s' is already taken.", array("!s" => $from_name), WATCHDOG_WARNING);
+   }
+   // check if address is valid
+   elseif (!valid_email_address($from_address)) {
+     $node->body = t('Message from !n !a', array('!n' => $from_name, '!a' => $from_address)) ."\n\n". $node->body;
+     watchdog('listhandler', "Cannot create account: The email address '!s' is not valid.", array("!s" => $from_address), WATCHDOG_WARNING);
+   }
+   // create account
+   else {
+     $empty_account = new stdClass();
+     $from_user = user_save($empty_account, array('name' => $from_name, 'pass' => user_password(), 'init' => $from_address,  'mail' => $from_address, 'roles' => array(DRUPAL_AUTHENTICATED_RID), 'status' => variable_get('listhandler_accountstatus', 0)), 'account');
+     watchdog('listhandler', 'Created account for !fa, with roles !rid', array('!fa' => $from_address, '!rid' => implode(', ', $from_user->roles)), WATCHDOG_NOTICE);
+     mailhandler_switch_user($from_user->uid);
+   }
+ }
   $node->uid = $from_user->uid;
   $node->name = $from_user->name;
 
