diff -up -r mailfix-6.x-1.6/mailfix.domains.inc mailfix-6.x-2.x-dev/mailfix.domains.inc
--- mailfix-6.x-1.6/mailfix.domains.inc	2009-04-17 07:13:37.000000000 -0700
+++ mailfix-6.x-2.x-dev/mailfix.domains.inc	2010-03-01 10:02:47.000000000 -0800
@@ -108,7 +108,8 @@ function mailfix_add_domain_form_validat
  */
 function mailfix_add_domain_form_submit($form, &$form_state) {
   db_query(
-    "INSERT INTO {mailfix_domains} (domain_name, default_quota) VALUES ('%s', %d)",
+    "INSERT INTO {mailfix_domains} (site_id, domain_name, default_quota) VALUES (%d, '%s', %d)",
+    variable_get('mailfix_site_id', FALSE),
     $form_state['values']['domain_name'],
     $form_state['values']['default_quota']
   );
@@ -117,12 +118,12 @@ function mailfix_add_domain_form_submit(
 
   if ($domain->domain_id > 0) {
     // Clean out any wrongly associated uid, as this field is a unique index
-    $sql = "DELETE FROM {mailfix_users} WHERE uid IN (SELECT uid FROM {users} u WHERE u.mail LIKE '%@%s')";
-    db_query($sql, $domain->domain_name);
+    $sql = "DELETE FROM {mailfix_users} WHERE site_id = %d AND uid IN (SELECT uid FROM {users} u WHERE u.mail LIKE '%@%s')";
+    db_query($sql, array(variable_get('mailfix_site_id', FALSE), $domain->domain_name));
     
     // Populate mailfix_users
-    $sql = "INSERT INTO {mailfix_users} (uid, domain_id) SELECT uid, %d AS domain_id FROM {users} WHERE mail LIKE '%@%s'";
-    db_query($sql, $domain->domain_id, $domain->domain_name);
+    $sql = "INSERT INTO {mailfix_users} (site_id, uid, domain_id) SELECT %d as site_id, uid, %d AS domain_id FROM {users} WHERE mail LIKE '%@%s'";
+    db_query($sql, array(variable_get('mailfix_site_id', FALSE), $domain->domain_id, $domain->domain_name));
     }
   // How many Mailfix profiles were created?
   $sql = "SELECT count(uid) AS users FROM {mailfix_users} WHERE domain_id = %d";
@@ -162,7 +163,7 @@ function mailfix_edit_domain_form(&$form
   $form['domain_label'] = array(
     '#type' => 'item',
     '#title' => t('Domain name'),
-    '#description' => t('This is the registered domain name for which this Drupal server is intended to manage mail.'),
+    '#description' => t('This is the registered domain name for which this Drupal site is intended to manage mail.'),
     '#value' => $domain->domain_name,
   );
   $form['default_quota'] = array(
@@ -198,8 +199,8 @@ function mailfix_edit_domain_form_submit
     $form_state['values']['default_quota'],
     $form_state['values']['domain_id']
   );
-  $sql = "SELECT * FROM {mailfix_domains} WHERE domain_id = %d ";
-  $domain = db_fetch_object(db_query($sql, $form_state['values']['domain_name']));
+  $sql = "SELECT * FROM {mailfix_domains} WHERE site_id = %d AND domain_id = %d ";
+  $domain = db_fetch_object(db_query($sql, array(variable_get('mailfix_site_id', FALSE), $form_state['values']['domain_name'])));
   // Alert user of update operation.
   drupal_set_message(t('Default quota for domain %domain_name updated.', array(
       '%domain_name' => $form_state['values']['domain_name'], 
@@ -214,9 +215,10 @@ function mailfix_domains_overview() {
   // Render domains list.
   $sql = 'SELECT d.domain_id, d.domain_name, d.default_quota, count(u.uid) AS domain_users '
     .'FROM {mailfix_domains} d LEFT JOIN {mailfix_users} u ON d.domain_id = u.domain_id '
+    .'WHERE d.site_id = %d '
     .'GROUP BY  d.domain_id, d.domain_name, d.default_quota '
     .'ORDER BY d.domain_name';
-  $result = db_query($sql);
+  $result = db_query($sql, variable_get('mailfix_site_id', FALSE));
   $header = array(
     t('Domains'), 
     t('Current users'), 
diff -up -r mailfix-6.x-1.6/mailfix.info mailfix-6.x-2.x-dev/mailfix.info
--- mailfix-6.x-1.6/mailfix.info	2009-04-17 07:26:47.000000000 -0700
+++ mailfix-6.x-2.x-dev/mailfix.info	2010-11-13 08:07:37.000000000 -0800
@@ -6,8 +6,8 @@ core = 6.x
 php = 5.1
 
 ; Information added by drupal.org packaging script on 2009-04-17
-version = "6.x-1.6"
+version = "6.x-2.x-dev"
 core = "6.x"
 project = "mailfix"
-datestamp = "1239978407"
+datestamp = "1289668044"
 
diff -up -r mailfix-6.x-1.6/mailfix.install mailfix-6.x-2.x-dev/mailfix.install
--- mailfix-6.x-1.6/mailfix.install	2008-11-29 01:55:27.000000000 -0800
+++ mailfix-6.x-2.x-dev/mailfix.install	2010-03-01 10:30:26.000000000 -0800
@@ -4,8 +4,8 @@
   * @file
   * Mailfix installer
   *
-  * This file create the following tables: 
-  *   mailfix_domains, mailfix_users
+  * This file creates the following tables: 
+  *   mailfix_domains, mailfix_users, mailfix_aliases
   */
 
 /**
@@ -14,93 +14,273 @@
 function mailfix_install() {
   // Create tables
   drupal_install_schema('mailfix');
+  if (!variable_get('mailfix_site_id', FALSE)) {
+    $result = db_fetch_array(db_query('SELECT MAX(`site_id`) as max_site_id FROM {mailfix_users}'));
+    variable_set('mailfix_site_id', ( $result['max_site_id'] ? ( $result['max_site_id'] + 1 ) : 1 ) );
+  }
 } // mailfix_install
 
 /**
  * Implementation of hook_schema().
  */
 function mailfix_schema() {
-  $schema['mailfix_domains'] = array(
-    'description' => t('Domains managed by this Drupal server'),
+  $schema = array();
+  if (mailfix_table_exists('{mailfix_domains}')< 1) {
+    $schema['mailfix_domains'] = array(
+      'description' => t('Domains managed by this Drupal server'),
+      'fields' => array(
+	'domain_id' => array(
+	  'type' => 'serial',
+	  'size' => 'normal',
+	  'not null' => TRUE,
+	  'description' => t('Primary Key'),
+	),
+	'domain_name' => array(
+	  'type' => 'varchar',
+	  'length' => 150,
+	  'not null' => TRUE,
+	  'description' => t('Domain name, e.g. example.com'),
+	),
+	'site_id' => array(
+	  'type' => 'int',
+	  'size' => 'normal',
+	  'not null' => TRUE,
+	  'description' => t('Site that owns this domain'),
+	  'default' => 0,
+	),
+	'default_quota' => array(
+	  'type' => 'int',
+	  'size' => 'big',
+	  'not null' => TRUE,
+	  'default' => 104857600,
+	  'description' => t('Default storage quota for newly created users'),
+	),
+      ),
+      'unique keys' => array('domain_name' => array('domain_name')),
+      'primary key' => array('domain_id'),
+    );
+  }
+  if (mailfix_table_exists('{mailfix_aliases}')< 1) {
+    $schema['mailfix_aliases'] = array(
+      'description' => t('Aliases'),
+      'fields' => array(
+	'pri_key' => array(
+	  'type' => 'serial',
+	  'size' => 'normal',
+	  'not null' => TRUE,
+	  'description' => t('Primary Key'),
+	),
+	'alias' => array(
+	  'type' => 'varchar',
+	  'length' => 128,
+	  'not null' => TRUE,
+	  'default' => '',
+	  'description' => t('The user portion of user@alias.tld '),
+	),
+	'maps_to_uid' => array(
+	  'type' => 'int',
+	  'not null' => TRUE,
+	  'description' => t('id of the mailfix_user uid this alias should match to'),
+	  'default' => 0,
+	),
+	'maps_to_siteid' => array(
+	  'type' => 'int',
+	  'not null' => TRUE,
+	  'description' => t('siteid of the mailfix_user this alias should match to'),
+	  'default' => 0,
+	),
+      ),
+      'primary key' => array('pri_key'),
+    );
+  }
+  if (mailfix_table_exists('{mailfix_users}') < 1) {
+  $schema['mailfix_users'] = array(
+      'description' => t('Virtual email users mapping to Drupal accounts'),
+      'fields' => array(
+	'site_id' => array(
+	  'type' => 'int',
+	  'size' => 'normal',
+	  'not null' => TRUE,
+	  'default' => 0,
+	  'description' => t('Domain ID'),
+	),
+	'uid' => array(
+	  'type' => 'int',
+	  'not null' => TRUE,
+	  'description' => t('Drupal user ID'),
+	),
+	'status' => array(
+	  'type' => 'int',
+	  'not null' => TRUE,
+	  'description' => t("Copy of User\'s status to control imap/pop/smtp login ability"),
+	  'default' => 0,
+	),
+	'pass' => array(
+	  'type' => 'varchar',
+	  'length' => 32,
+	  'not null' => TRUE,
+	  'default' => '',
+	  'description' => "Copy of User\'s password (md5 hash) from users table",
+	),
+	'mail' => array(
+	  'type' => 'varchar',
+	  'length' => 64,
+	  'not null' => FALSE,
+	  'default' => '',
+	  'description' => "Copy of User's email address from users table",
+	),
+	'domain_id' => array(
+	  'type' => 'int',
+	  'size' => 'normal',
+	  'not null' => TRUE,
+	  'default' => 0,
+	  'description' => t('Domain ID'),
+	),
+	'quota' => array(
+	  'type' => 'int',
+	  'size' => 'big',
+	  'not null' => TRUE,
+	  'default' => 104857600,
+	  'description' => t('Mail quota, default is 100MB'),
+	),
+	'forward' => array(
+	  'type' => 'text',
+	  'size' => 'normal',
+	  'not null' => FALSE,
+	  'default' => NULL,
+	  'description' => t('Forward mail to this list of comma-separated addresses'),
+	),
+	'incoming_bcc' => array(
+	  'type' => 'text',
+	  'size' => 'normal',
+	  'not null' => FALSE,
+	  'default' => NULL,
+	  'description' => t('Comma-delimited list of BCC recipients for incoming mail'),
+	),
+	'outgoing_bcc' => array(
+	  'type' => 'text',
+	  'size' => 'normal',
+	  'not null' => FALSE,
+	  'default' => NULL,
+	  'description' => t('Comma-delimited list of BCC recipients for outgoing mail'),
+	),
+	'autoresponder' => array(
+	  'type' => 'text',
+	  'size' => 'normal',
+	  'not null' => FALSE,
+	  'description' => t('Autoresponder message'),
+	),
+	'aresp_start' => array(
+	  'type' => 'datetime',
+	  'not null' => FALSE,
+	  'description' => t('Autoresponder start date'),
+	),
+	'aresp_end' => array(
+	  'type' => 'datetime',
+	  'not null' => FALSE,
+	  'description' => t('Autoresponder end date'),
+	),
+      ),
+      'indexes' => array('idx_mailfix_users_domain_id' => array('domain_id')),
+      'primary key' => array('site_id','uid'),
+    );
+  }
+  return $schema;
+} //mailfix_schema
+
+/**
+ * Implementation of hook_uninstall().
+ */
+function mailfix_uninstall() {
+  //Remove tables
+  if (db_prefix_tables('{mailfix_users}') == 'mailfix_users' ) {
+    drupal_uninstall_schema('mailfix');
+    drupal_set_message(t('Mailfix module uninstalled successfully. However, the mailfix site ID is still stored in the system variables to facilitate later re-installation'));
+  } else {
+    drupal_set_message(t('Mailfix module uninstalled successfully. However, shared schema tables were detected and those tables have not be uninstalled. Also, the mailfix site ID is still stored in the system variables to facilitate later re-installation'));
+  }
+  drupal_set_message('db pre tables: ' . db_prefix_tables('{mailfix_users}'), 'status', TRUE);
+  //variable_del('mailfix_site_id');
+} // mailfix_uninstall
+
+
+function mailfix_table_exists($tablename) {
+    $prefixed = db_prefix_tables($tablename);    
+    $prefixed = explode('.',$prefixed);
+    $prefixed = array_reverse($prefixed, FALSE);
+
+    /*if(!$database) {
+        $res = mysql_query("SELECT DATABASE()");
+        $database = mysql_result($res, 0);
+    }*/
+
+    $sql = "SELECT COUNT(*) AS count FROM information_schema.tables WHERE table_name = '%s' ";
+    if ($prefixed[1]) {
+      $sql .= " AND table_schema = '%s'";
+    }
+    $result = db_fetch_array(db_query($sql, $prefixed));
+
+    return $result['count'];
+
+}
+
+/**
+ * Implementation of hook_update_N()
+ */
+//**todo  copy field descriptions from above
+function mailfix_update_6101() {
+  $ret = array();
+
+  $schema['mailfix_aliases'] = array(
+    'description' => t('Aliases'),
     'fields' => array(
-      'domain_id' => array(
+      'pri_key' => array(
         'type' => 'serial',
         'size' => 'normal',
         'not null' => TRUE,
         'description' => t('Primary Key'),
       ),
-      'domain_name' => array(
+      'alias' => array(
         'type' => 'varchar',
-        'length' => 150,
-        'not null' => TRUE,
-        'description' => t('Domain name, e.g. example.com'),
-      ),
-      'default_quota' => array(
-        'type' => 'int',
-        'size' => 'big',
+        'length' => 128,
         'not null' => TRUE,
-        'default' => 104857600,
-        'description' => t('Default storage quota for newly created users'),
+        'default' => '',
+        'description' => t('The user portion of user@alias.tld '),
       ),
-    ),
-    'unique keys' => array('domain_name' => array('domain_name')),
-    'primary key' => array('domain_id'),
-  );
-  $schema['mailfix_users'] = array(
-    'description' => t('Virtual email users mapping to Drupal accounts'),
-    'fields' => array(
-      'uid' => array(
+      'maps_to_uid' => array(
         'type' => 'int',
         'not null' => TRUE,
-        'description' => t('Drupal user ID'),
+        'description' => t('id of the mailfix_user uid this alias should match to'),
+        'default' => '',
       ),
-      'domain_id' => array(
+      'maps_to_siteid' => array(
         'type' => 'int',
-        'size' => 'normal',
         'not null' => TRUE,
+        'description' => t('siteid of the mailfix_user this alias should match to'),
         'default' => 0,
-        'description' => t('Domain ID'),
-      ),
-      'quota' => array(
-        'type' => 'int',
-        'size' => 'big',
-        'not null' => TRUE,
-        'default' => 104857600,
-        'description' => t('Mail quota, default is 100MB'),
-      ),
-      'forward' => array(
-        'type' => 'text',
-        'size' => 'normal',
-        'not null' => FALSE,
-        'default' => NULL,
-        'description' => t('Forward mail to this list of comma-separated addresses'),
-      ),
-      'incoming_bcc' => array(
-        'type' => 'text',
-        'size' => 'normal',
-        'not null' => FALSE,
-        'default' => NULL,
-        'description' => t('Comma-delimited list of BCC recipients for incoming mail'),
-      ),
-      'outgoing_bcc' => array(
-        'type' => 'text',
-        'size' => 'normal',
-        'not null' => FALSE,
-        'default' => NULL,
-        'description' => t('Comma-delimited list of BCC recipients for outgoing mail'),
       ),
     ),
-    'indexes' => array('idx_mailfix_users_domain_id' => array('domain_id')),
-    'primary key' => array('uid'),
+    'primary key' => array('pri_key'),
   );
-  return $schema;
-} //mailfix_schema
 
-/**
- * Implementation of hook_uninstall().
- */
-function mailfix_uninstall() {
-  // Remove tables
-  drupal_uninstall_schema('mailfix');
-  drupal_set_message(t('Mailfix module uninstalled successfully.'));
-} // mailfix_uninstall
+  db_create_table($ret, 'mailfix_aliases', $schema['mailfix_aliases']);
+  db_add_field($ret, 'mailfix_domains', 'site_id', array('type' => 'int', 'default' => 0 , 'not null' => TRUE));  
+  db_add_field($ret, 'mailfix_domains', 'maps_to', array('type' => 'int', 'default' => 0 , 'not null' => FALSE)); 
+  db_add_field($ret, 'mailfix_users', 'site_id', array('type' => 'int', 'default' => 0 , 'not null' => TRUE));
+  db_add_field($ret, 'mailfix_users', 'status', array('type' => 'int', 'default' => 0 , 'not null' => TRUE, 'description' => t('Copy of user status to control imap/pop/smtp login ability'),));
+  db_add_field($ret, 'mailfix_users', 'autoresponder', array('type' => 'text', 'size' => 'normal', 'not null' => FALSE));
+  db_add_field($ret, 'mailfix_users', 'pass', array('type' => 'varchar', 'length' => '32', 'not null' => TRUE, 'default' => '', 'description' => t('Copy of email from users table'),));
+  db_add_field($ret, 'mailfix_users', 'mail', array('type' => 'varchar', 'length' => '64', 'not null' => TRUE, 'default' => '', 'description' => t('Copy of password from users table'),));
+  db_add_field($ret, 'mailfix_users', 'aresp_start', array('type' => 'datetime', 'not null' => FALSE));
+  db_add_field($ret, 'mailfix_users', 'aresp_end', array('type' => 'datetime', 'not null' => FALSE));
+  db_drop_primary_key($ret, 'mailfix_users');
+  db_add_primary_key($ret, 'mailfix_users', array('uid', 'site_id'));
+  if (!variable_get('mailfix_site_id', FALSE)) {
+    $result = db_fetch_array(db_query('SELECT MAX(`site_id`) as max_site_id FROM {mailfix_users}'));
+    variable_set('mailfix_site_id', ( $result['max_site_id'] ? ( $result['max_site_id'] + 1 ) : 1 ) );
+  }
+  db_query('UPDATE {mailfix_domains} SET site_id = %d WHERE site_id = 0', variable_get('mailfix_site_id'));
+  db_query('UPDATE {mailfix_users} u, {mailfix_domains} d SET u.site_id = d.site_id WHERE u.domain_id = d.domain_id');
+  db_query('UPDATE {mailfix_users} mu, {users} du SET mu.status = du.status, mu.pass = du.pass, mu.mail = du.mail WHERE mu.uid = du.uid and mu.site_id = %d ', variable_get('mailfix_site_id'));
+  return $ret;
+}
diff -up -r mailfix-6.x-1.6/mailfix.module mailfix-6.x-2.x-dev/mailfix.module
--- mailfix-6.x-1.6/mailfix.module	2009-04-10 00:22:27.000000000 -0700
+++ mailfix-6.x-2.x-dev/mailfix.module	2010-03-01 11:42:58.000000000 -0800
@@ -17,7 +17,7 @@ function mailfix_help($path, $arg) {
   $output = '';
   switch ($path) {
     case 'admin/help#mailfix':
-      $output = t('<p>The Mailfix module is intended to offer mail accounts to Drupal users. This module offers the following benefits:</p><ul><li>You can manage multiple domains for mail delivery</li><li>Drupal and mail accounts share the same password, thus allowing users to manage their own credentials.</li><li>Users can access their mailbox using POP3, POP3S, IMAP and IMAPS</li><li>Quota support, with per-domain defaults and individual user settings</li><li>Mail forwarding (very useful for disabled accounts)</li><li>Silent BCC monitoring of specific accounts</li><li>Watchdog auditing of Mailfix events</li></ul><p>Once you have registered one or more Mailfix domains, new and existing users who\'s mail address matches any of those will have a <em>Mailfix settings</em> section in their account profile, filled with default mail quota. You will be able to configure individual settings for automatic forwarding and silent BCC monitoring.</p><p>Detailed instructions to configure the Postfix mail server are available at <a href="http://www.howtoforge.com/drupal-plus-postfix-integration-under-ubuntu-8.04">HowtoForge</a></p>');
+      $output = t('<p>The Mailfix module is intended to offer mail accounts to Drupal users. This module offers the following benefits:</p><ul><li>You can manage multiple domains for mail delivery</li><li>Drupal and mail accounts share the same password, thus allowing users to manage their own credentials.</li><li>Users can access their mailbox using POP3, POP3S, IMAP and IMAPS</li><li>Quota support, with per-domain defaults and individual user settings</li><li>Mail redirection (very useful for disabled accounts)</li><li>Silent BCC monitoring of specific accounts</li><li>Watchdog auditing of Mailfix events</li></ul><p>Once you have registered one or more Mailfix domains, new and existing users who\'s mail address matches any of those will have a <em>Mailfix settings</em> section in their account profile, filled with default mail quota. You will be able to configure individual settings for automatic redirection and silent BCC monitoring.</p><p>Detailed instructions to configure the Postfix mail server are available at <a href="http://www.howtoforge.com/drupal-plus-postfix-integration-under-ubuntu-8.04">HowtoForge</a></p>');
       break;
     case 'admin/settings/mailfix':
       $output = t('This is the list of currently registered Mailfix domains. When you delete a domain, all associated Mailfix user profiles will be deleted.');
@@ -83,21 +83,21 @@ function mailfix_menu() {
  * Implementation of hook_user().
  */
 function mailfix_user($op, &$edit, &$user, $category = NULL) {
-  if (user_access('administer users')) {
+  //if (user_access('administer users')) {
     switch ($op) {
       case 'view':
         return mailfix_view_user($user);
       case 'form':
         return mailfix_form_user($user);
       case 'insert':
-      case 'update':
+      case 'after_update':
         return mailfix_verify_user($edit, $user);
       case 'delete':
         // Delete from {mailfix_users} table
-        db_query('DELETE FROM {mailfix_users} WHERE uid = %d', $user->uid);
+        db_query('DELETE FROM {mailfix_users} WHERE site_id = %d AND uid = %d', array(variable_get('mailfix_site_id', FALSE), $user->uid));
         break;
     }
-  }
+  //}
 } // mailfix_user
 
 /**
@@ -113,36 +113,82 @@ function mailfix_verify_user(&$edit, &$u
   if (!empty($edit['mail'])) {
     $domain_id = _mailfix_verify_domain($edit['mail']);
     if ($domain_id > 0) { // new email matches one registered domain
+      $result = db_fetch_array(db_query('SELECT * FROM {mailfix_users} WHERE site_id = %d AND uid = %d AND domain_id = %d', array(variable_get('mailfix_site_id', FALSE), $user->uid, $domain_id)));
+      $mailfix_users_fields = array();
+      $mailfix_users_fields = drupal_get_schema(db_prefix_tables('{mailfix_users}'));
+      $mailfix_users_fields = array_keys($mailfix_users_fields['fields']);
+      //drupal_set_message('<pre>' . print_r($mailfix_users_fields, TRUE) . '</pre>', 'status' , FALSE);
+      foreach ($mailfix_users_fields as $key => $value) {
+         $edit[$value] = ($edit[$value] || $edit[$value] == '') ? $edit[$value] : $result[$value];
+      }
+      if (!isset($edit['aliases'])) {
+	$result = db_fetch_array(db_query('SELECT * FROM {mailfix_aliases} WHERE maps_to_uid = %d AND maps_to_siteid = %d', $user->uid, variable_get('mailfix_site_id', FALSE)));
+	//$mailfix_users_fields = drupal_get_schema('mailfix_users');
+	//$mailfix_users_fields = array_keys($mailfix_users_fields['fields']);
+	drupal_set_message('<pre> Aliases not set: ' . print_r($result, TRUE) . '</pre>', 'status' , FALSE);
+	//foreach ($mailfix_users_fields as $key => $value) {
+	  //$edit[$value] = ($edit[$value] || $edit[$value] == '') ? $edit[$value] : $result[$value];
+	//}
+      }
+      //foreach ($edit as $key => $value) {
+      //}
       if (empty($edit['quota'])) {  // quota has been modified
-        $result = db_fetch_object(db_query('SELECT default_quota FROM {mailfix_domains} WHERE domain_id = %d', $domain_id));
+        $result = db_fetch_object(db_query('SELECT default_quota FROM {mailfix_domains} WHERE site_id = %d AND domain_id = %d', array(variable_get('mailfix_site_id', FALSE), $domain_id)));
         $edit['quota'] = $result->default_quota;
       }
-      db_query("DELETE FROM {mailfix_users} WHERE uid = %d", $user->uid);
-      db_query("INSERT INTO {mailfix_users} (uid, domain_id, quota, forward, incoming_bcc, outgoing_bcc) "
-        ."VALUES (%d, %d, %d, '%s', '%s', '%s')", 
+      db_query("DELETE FROM {mailfix_users} WHERE site_id = %d AND uid = %d AND domain_id = %d", array(variable_get('mailfix_site_id', FALSE), $user->uid, $domain_id));
+
+      // ***todo*** convert this to use drupal_write_record('mailfix_users', &$edit, FALSE );
+      db_query("INSERT INTO {mailfix_users} (site_id, uid, domain_id, forward, autoresponder, aresp_start, aresp_end, quota, incoming_bcc, outgoing_bcc, status) "
+        ."VALUES (%d, %d, %d, '%s', '%s', '%d-%d-%d', '%d-%d-%d', '%s', '%s', '%s', %d)", 
         array( 
+          variable_get('mailfix_site_id', FALSE),
           $user->uid,
           $domain_id,
+          $edit['redirection'],
+          $edit['autoresponder'],
+          $edit['aresp_start']['year'],           $edit['aresp_start']['month'],           $edit['aresp_start']['day'],
+          $edit['aresp_end']['year'],           $edit['aresp_end']['month'],           $edit['aresp_end']['day'],
           $edit['quota'],
-          $edit['forward'],
           $edit['incoming_bcc'],
           $edit['outgoing_bcc'],
+          $user->status,
         )
       );
+      db_query("DELETE FROM {mailfix_aliases} WHERE maps_to_siteid = %d AND maps_to_uid = %d", array(variable_get('mailfix_site_id', FALSE), $user->uid));
+      $aliases_arr = explode("\n", $edit['aliases']);
+      sort($aliases_arr);
+      foreach($aliases_arr as $aliases_val) {
+	if (strlen($aliases_val) > 0 ){
+	  db_query("INSERT INTO {mailfix_aliases} (maps_to_siteid, maps_to_uid, alias) "
+	    ."VALUES (%d, %d, '%s')", 
+	    array( 
+	      variable_get('mailfix_site_id', FALSE),
+	      $user->uid,
+	      trim($aliases_val),
+	    )
+	  );
+	}
+      }
+      //drupal_set_message('<pre> ' . print_r($user, TRUE) . '</pre>' , 'status', FALSE);
+
+      db_query('UPDATE {mailfix_users} mu, {users} du SET mu.mail = du.mail, mu.pass = du.pass WHERE mu.uid = du.uid and mu.site_id = %d ', variable_get('mailfix_site_id', FALSE));
+     
       watchdog(
         'mailfix', 
-        'Mailfix profile for %user_name updated. Forwarding: %fwd, BCC monitoring: %bcc', 
+        'Mailfix profile for %user_name updated. Redirection: %fwd, BCC monitoring: %bcc, Autoresponder: %arsp',
         array(
           '%user_name' => $user->name,
-          '%fwd' => (empty($edit['forward']) ? t('no') : t('yes')),
+          '%fwd' => (empty($edit['redirection']) ? t('no') : t('yes')),
           '%bcc' => ((empty($edit['incoming_bcc']) || empty($edit['outgoing_bcc'])) ? t('no') : t('yes')),
+          '%arsp' => (empty($edit['redirection']) ? t('no') : t('yes')),
         ), 
         WATCHDOG_NOTICE, 
         l('view', 'admin/user/user/'. $user->uid)
       );
     }
     else {  // new email doesn't match any registered domain
-      db_query("DELETE FROM {mailfix_users} WHERE uid = %d", $user->uid);
+      db_query("DELETE FROM {mailfix_users} WHERE site_id = %d AND uid = %d", array(variable_get('mailfix_site_id', FALSE), $user->uid));
       watchdog(
         'mailfix', 
         'Mailfix profile for %user_name deleted.', 
@@ -169,8 +215,8 @@ function mailfix_verify_user(&$edit, &$u
 function _mailfix_verify_domain($email) {
   $domain = drupal_substr(strrchr($email, "@"), 1); // extract domain
   $domain_id = 0;
-  $sql = "SELECT domain_id FROM {mailfix_domains} WHERE domain_name = '%s'";
-  if ($result = db_fetch_object(db_query($sql, $domain))) {
+  $sql = "SELECT domain_id FROM {mailfix_domains} WHERE site_id = %d AND domain_name = '%s'";
+  if ($result = db_fetch_object(db_query($sql, array(variable_get('mailfix_site_id', FALSE), $domain)))) {
     $domain_id = $result->domain_id;
   }
   return $domain_id;
@@ -188,10 +234,10 @@ function mailfix_form_user_validate($for
   if (!is_numeric($form_state['values']['quota'])) {
     form_set_error('mail storage quota', t('The mail storage quota must be an integer value. Provided value: %quota', array('%quota' => $form_state['values']['quota'])));
   }
-  // forward field is not required, thus validate only if filled
-  if (!empty($form_state['values']['forward'])) { 
-    // forward field is received as 1 address per line
-    $emails = explode("\n", $form_state['values']['forward']);
+  // redirection field is not required, thus validate only if filled
+  if (!empty($form_state['values']['redirection'])) { 
+    // redirection field is received as 1 address per line
+    $emails = explode("\n", $form_state['values']['redirection']);
     $isvalid = TRUE;
     foreach ($emails as &$email) {  // by reference as some cleanup may apply
       $email = trim($email);
@@ -200,12 +246,13 @@ function mailfix_form_user_validate($for
       }
     }
     if (!$isvalid) {  // one or more illegal email addresses
-      form_set_error('forward email recipients', t('The specified Forward destination(s) contains illegal email address(es). Spaces or any other special characters except dash (-) and underscore (_) are not allowed. If multiple recipients are specified, you must define one email address per line.'));
+      form_set_error('redirection email recipients', t('The specified Redirection destination(s) contains illegal email address(es). Spaces or any other special characters except dash (-) and underscore (_) are not allowed. If multiple recipients are specified, you must define one email address per line.'));
     }
-    else { // clean addresses, thus forward field must be prepared as comma delimited string
-      $form_state['values']['forward'] = implode(",", $emails);
+    else { // clean addresses, thus redirection field must be prepared as comma delimited string
+      $form_state['values']['redirection'] = implode(",", $emails);
     }
   }
+
   // incoming_bcc field is not required, thus validate only if filled
   if (!empty($form_state['values']['incoming_bcc'])) { 
     // incoming_bcc field is received as 1 address per line
@@ -220,7 +267,7 @@ function mailfix_form_user_validate($for
     if (!$isvalid) {  // one or more illegal emai addresses
       form_set_error('incoming BCC email recipients', t('The specified Incoming BCC destination(s) contains illegal email address(es). Spaces or any other special characters except dash (-) and underscore (_) are not allowed. If multiple recipients are specified, you must define one email address per line.'));
     }
-    else { // clean addresses, thus forward field must be prepared as comma delimited string
+    else { // clean addresses, thus redirection field must be prepared as comma delimited string
       $form_state['values']['incoming_bcc'] = implode(",", $emails);
     }
   }
@@ -238,7 +285,7 @@ function mailfix_form_user_validate($for
     if (!$isvalid) {  // one or more illegal emai addresses
       form_set_error('outgoing BCC email recipients', t('The specified Outgoing BCC destination(s) contains illegal email address(es). Spaces or any other special characters except dash (-) and underscore (_) are not allowed. If multiple recipients are specified, you must define one email address per line.'));
     }
-    else { // clean addresses, thus forward field must be prepared as comma delimited string
+    else { // clean addresses, thus redirection field must be prepared as comma delimited string
       $form_state['values']['outgoing_bcc'] = implode(",", $emails);
     }
   }
@@ -253,58 +300,118 @@ function mailfix_form_user($user) {
   $fields = array();
   if (_mailfix_get_user($user) == 1) {
     // Create the outer content
-    $fields['mailfix'] = array(
-      '#type' => 'fieldset',
-      '#title' => t('Mailfix Settings'),
-      '#description' => t('<p>If you <em>update</em> an account\'s e-mail, the Mailfix module will drop any existing Mailfix profile. Then it will verify if the new address matches a registered domain. If a match is found, a Mailfix profile will be created with domain\'s default settings. If no match is found, no Mailfix profile will be created.</p><p>If you <em>block</em> an account, it will stop receiveing mails and BCC monitoring will be turned off. However, if the <em>forward</em> field is set, incoming mails can still be forwarded to someone else (e.g. a supervisor).</p><p>If you <em>delete</em> an account, the associated Mailfix profile will be deleted as well.</p>'),
-    );
     // Mailfix settings
-    $fields['mailfix']['domain_name'] = array(
-      '#type' => 'item',
-      '#title' => t('Mail domain'),
-      '#value' => $user->mailfix['domain_name'],
-    );
-    $fields['mailfix']['quota'] = array(
-      '#type' => 'textfield',
-      '#title' => t('Quota limit'),
-      '#description' => t('This is the quota limit for %username in bytes. Useful information: 1kB = 1024 bytes, 1MB = 1048576 bytes, 1GB = 1073741824 bytes', array('%username' => $user->name)),
-      '#default_value' => $user->mailfix['quota'],
-      '#maxlength' => 19,  // MySQL bigint max unsigned value: 9223372036854775808
-      '#size' => 20,  
-      '#required' => TRUE, 
-    );
-    $fields['mailfix']['forward'] = array(
-      '#title' => t('Forward destination(s)'),
-      '#description' => t('If defined, incoming mails will no longer be delivered to %email. Instead, they will be forwarded to given destination(s). You can specify multiple recipients (one per line). Automatic forwarding is useful in scenarios such as vacations, temporary leave or disabled accounts.', array('%email' => $user->mail)),
-      '#type' => 'textarea',
-      '#rows' => 3, 
-      '#default_value' => str_replace(",", "\n", $user->mailfix['forward']),
-      '#required' => FALSE, 
-    );
-    $fields['mailfix']['silent_bcc'] = array(
-      '#type' => 'fieldset',
-      '#collapsible' => TRUE,
-      '#collapsed' => TRUE,
-      '#title' => t('Silent BCC Monitoring'),
-      '#description' => t('You can specify silent monitoring of incoming and outgoing messages for this account. Silent monitoring is useful to audit activity of specific users. You can specify multiple recipients (one per line).'),
-    );
-    $fields['mailfix']['silent_bcc']['incoming_bcc'] = array(
-      '#title' => t('Incoming messages'),
-      '#description' => t('If defined, any incoming mail for %email will be copied as <acronym title="Blind Carbon Copy">BCC</acronym> to given destinations.', array('%email' => $user->mail)),
-      '#type' => 'textarea',
-      '#rows' => 3, 
-      '#default_value' => str_replace(",", "\n", $user->mailfix['incoming_bcc']),
-      '#required' => FALSE, 
-    );
-    $fields['mailfix']['silent_bcc']['outgoing_bcc'] = array(
-      '#title' => t('Outgoing messages'),
-      '#description' => t('If defined, any outgoing mail from %email will be copied as <acronym title="Blind Carbon Copy">BCC</acronym> to given destinations.', array('%email' => $user->mail)),
-      '#type' => 'textarea',
-      '#rows' => 3, 
-      '#default_value' => str_replace(",", "\n", $user->mailfix['outgoing_bcc']),
-      '#required' => FALSE, 
-    );
-    $fields['#validate'][] = 'mailfix_form_user_validate';
+    if ( (user_access('administer users')) || ( (user_access('mailfix own aliases')) || (user_access('mailfix own autoresponder')) || (user_access('mailfix own redirection')) ) && ($GLOBALS['user']->uid == $user->uid) ){  //($GLOBALS['user']->uid == 1) ||
+      $fields['mailfix'] = array(
+	'#type' => 'fieldset',
+	'#title' => t('Mail Account Settings'),
+	'#collapsible' => TRUE,
+	'#collapsed' => TRUE,
+      );
+    }
+    if ( (user_access('administer users')) || ( (user_access('mailfix own aliases')) && ($GLOBALS['user']->uid == $user->uid)) ){ 
+      $fields['mailfix']['aliases'] = array(
+	'#type' => 'fieldset',
+	'#collapsible' => TRUE,
+	'#collapsed' => TRUE,
+	'#title' => t('Aliases'),
+	'#description' => t('You can specify additional aliases for this mailbox. For example, adding "johndoe" here will make johndoe@%email a valid address', array('%email' => $user->mail)),
+      );
+      $fields['mailfix']['aliases']['aliases'] = array(
+	'#title' => t('Aliases'),
+	'#type' => 'textarea',
+	'#rows' => 3,
+	'#default_value' =>  $user->mailfix['aliases'],
+	'#required' => FALSE,
+      );
+    }
+    if ( (user_access('administer users')) || ( (user_access('mailfix own autoresponder')) && ($GLOBALS['user']->uid == $user->uid)) ){ 
+      $fields['mailfix']['autoresponder'] = array(
+	'#type' => 'fieldset',
+	'#collapsible' => TRUE,
+	'#collapsed' => TRUE,
+	'#title' => t('Autoresponder'),
+	'#description' => t('You can specify messages to be automatically sent in response to any inbound email'),
+      );
+      $fields['mailfix']['autoresponder']['autoresponder'] = array(
+	'#title' => t('Autoresponder Message'),
+	'#description' => t('If defined, incoming messages to %email will automatically be replied to with this message.', array('%email' => $user->mail)),
+	'#type' => 'textarea',
+	'#rows' => 3,
+	'#default_value' => str_replace(",", "\n", $user->mailfix['autoresponder']),
+	'#required' => FALSE,
+      );
+      $fields['mailfix']['autoresponder']['aresp_start'] = array(
+	'#title' => t('Autoresponder Start'),
+	'#description' => t('Set a start date for this autoresponder message to begin.'),
+	'#type' => 'date',
+	'#required' => FALSE,
+	'#default_value' => ( strtotime($user->mailfix['aresp_start']) ? date_parse($user->mailfix['aresp_start']) : FALSE ), //$user->mailfix['aresp_start'] ,
+      );
+      $fields['mailfix']['autoresponder']['aresp_end'] = array(
+	'#title' => t('Autoresponder End'),
+	'#description' => t('Set an end date for this autoresponder message to begin. Setting an end date earlier than today\'s date will disable the autoresponder'),
+	'#type' => 'date',
+	'#required' => FALSE,
+	'#default_value' => ( strtotime($user->mailfix['aresp_end']) ? date_parse($user->mailfix['aresp_end']) : FALSE ),
+      );
+    }
+    if ( (user_access('administer users')) || ( (user_access('mailfix own redirection')) && ($GLOBALS['user']->uid == $user->uid) ) ){ 
+      $fields['mailfix']['redirection'] = array(
+	'#type' => 'fieldset',
+	'#collapsible' => TRUE,
+	'#collapsed' => TRUE,
+	'#title' => t('Redirection'),
+      );
+      $fields['mailfix']['redirection']['redirection'] = array(
+	'#title' => t('Redirect to Other Address(es)'),
+	'#description' => t('If defined, incoming mails will no longer be delivered to %email. Instead, they will be redirected to given destination(s). You can specify multiple recipients (one per line). Automatic redirection is useful in scenarios such as vacations, temporary leave or disabled accounts.', array('%email' => $user->mail)),
+	'#type' => 'textarea',
+	'#rows' => 3, 
+	'#default_value' => str_replace(",", "\n", $user->mailfix['redirection']),
+	'#required' => FALSE, 
+      );
+    }
+    if ( (user_access('administer users')) ) {
+      $fields['mailfix']['admin_full'] = array(
+	  '#type' => 'fieldset',
+	'#collapsible' => TRUE,
+	'#collapsed' => TRUE,
+	'#title' => t('Status, Quotas, and Monitoring'),
+	'#description' => t('<p>If you <em>update</em> an account\'s e-mail, the Mailfix module will drop any existing Mailfix profile. Then it will verify if the new address matches a registered domain. If a match is found, a Mailfix profile will be created with domain\'s default settings. If no match is found, no Mailfix profile will be created.</p><p>If you <em>block</em> an account, it will stop receiveing mails and BCC monitoring will be turned off. However, if the <em>redirection</em> field is set, incoming mails can still be redirected to someone else (e.g. a supervisor).</p><p>If you <em>delete</em> an account, the associated Mailfix profile will be deleted as well.</p>'),
+      );
+      $fields['mailfix']['admin_full']['domain_name'] = array(
+	'#type' => 'item',
+	'#title' => t('Mail Domain'),
+	'#value' => $user->mailfix['domain_name'],
+      );
+      $fields['mailfix']['admin_full']['quota'] = array(
+	'#type' => 'textfield',
+	'#title' => t('Quota limit'),
+	'#description' => t('This is the quota limit for %username in bytes. Useful information: 1kB = 1024 bytes, 1MB = 1048576 bytes, 1GB = 1073741824 bytes', array('%username' => $user->name)),
+	'#default_value' => $user->mailfix['quota'],
+	'#maxlength' => 19,  // MySQL bigint max unsigned value: 9223372036854775808
+	'#size' => 20,  
+	'#required' => TRUE, 
+      );
+      $fields['mailfix']['admin_full']['incoming_bcc'] = array(
+	'#title' => t('Incoming messages'),
+	'#description' => t('If defined, any incoming mail for %email will be silently copied as <acronym title="Blind Carbon Copy">BCC</acronym> to given destinations. Silent monitoring is useful to audit activity of specific users. You can specify multiple recipients (one per line).', array('%email' => $user->mail)),
+	'#type' => 'textarea',
+	'#rows' => 3, 
+	'#default_value' => str_replace(",", "\n", $user->mailfix['incoming_bcc']),
+	'#required' => FALSE, 
+      );
+      $fields['mailfix']['admin_full']['outgoing_bcc'] = array(
+	'#title' => t('Outgoing messages'),
+	'#description' => t('If defined, any outgoing mail from %email will be copied as <acronym title="Blind Carbon Copy">BCC</acronym> to given destinations.', array('%email' => $user->mail)),
+	'#type' => 'textarea',
+	'#rows' => 3, 
+	'#default_value' => str_replace(",", "\n", $user->mailfix['outgoing_bcc']),
+	'#required' => FALSE, 
+      );
+      $fields['#validate'][] = 'mailfix_form_user_validate';
+    }
   }
   return $fields;
 } //  mailfix_form_user
@@ -316,6 +423,7 @@ function mailfix_form_user($user) {
  *    User object passed by reference.
  */
 function mailfix_view_user(&$user) {
+  //drupal_set_message('<pre>'.print_r($user, TRUE).'</pre>', 'status', TRUE);
   // Extract current mailfix settings for this user
   _mailfix_get_user($user);
 
@@ -339,11 +447,11 @@ function mailfix_view_user(&$user) {
       '#title' => t('Quota limit'),
       '#value' => ($user->mailfix['quota'] == 0) ? t('unlimited') : format_size($user->mailfix['quota']),
     );
-    if (!empty($user->mailfix['forward'])) {
-      $user->content['mailfix']['forward'] = array(
+    if (!empty($user->mailfix['redirection'])) {
+      $user->content['mailfix']['redirection'] = array(
         '#type' => 'user_profile_item',
-        '#title' => t('Forward mail to'),
-        '#value' => $user->mailfix['forward'],
+        '#title' => t('Redirect mail to'),
+        '#value' => $user->mailfix['redirection'],
       );
     }
     if (!empty($user->mailfix['incoming_bcc'])) {
@@ -372,21 +480,33 @@ function mailfix_view_user(&$user) {
  *    1 if user mailfix exists, 0 if not.
  */
 function _mailfix_get_user(&$user) {
-  $sql = 'SELECT u.uid, u.domain_id, u.quota, u.forward, u.incoming_bcc, u.outgoing_bcc, d.domain_name '
+  $sql = 'SELECT u.uid, u.domain_id, u.quota, u.forward, u.autoresponder, u.aresp_start, u.aresp_end, u.incoming_bcc, u.outgoing_bcc, d.domain_name '
     .'FROM {mailfix_domains} d JOIN {mailfix_users} u ON d.domain_id = u.domain_id '
-    .'WHERE u.uid = %d ';
+    .'WHERE u.uid = %d AND d.site_id = %d';
   $found = 0;
-  if ($result = db_fetch_object(db_query($sql, $user->uid))) {
+  if ($result = db_fetch_object(db_query($sql, array($user->uid, variable_get('mailfix_site_id', FALSE))))) {
     $user->mailfix = array(
       'domain_id' => $result->domain_id,
       'domain_name' => $result->domain_name,
       'quota' => $result->quota,
-      'forward' => $result->forward,
+      'redirection' => $result->forward,
       'incoming_bcc' => $result->incoming_bcc,
       'outgoing_bcc' => $result->outgoing_bcc,
+      'autoresponder' => $result->autoresponder,
+      'aresp_start' => $result->aresp_start,
+      'aresp_end' => $result->aresp_end,
     );
     $found = 1;
+    $sql = 'SELECT a.alias '
+      .'FROM {mailfix_aliases} a  '
+      .'WHERE a.maps_to_uid = %d AND a.maps_to_siteid = %d '
+      .'ORDER BY alias ASC';
+    $result = db_query($sql, array($user->uid, variable_get('mailfix_site_id', FALSE)));
+    while ($row = db_fetch_array($result)) {
+      $user->mailfix['aliases'] .= $row['alias'] . "\n";
+    };
   };
+  drupal_set_message('<pre>' . print_r($row, TRUE) . '</pre>', 'status', TRUE);
   return $found;
 } // _mailfix_get_user
 
@@ -397,16 +517,66 @@ function _mailfix_get_user(&$user) {
  *    User object passed by reference to include mailfix settings.
  */
 function _mailfix_set_user(&$edit) {
-  db_query("DELETE FROM {mailfix_users} WHERE uid = %d", $edit['uid']);
-  db_query("INSERT INTO {mailfix_users} (uid, domain_id, quota, forward, incoming_bcc, outgoing_bcc) "
-    ."VALUES (%d, %d, %d, '%s', '%s', '%s')", 
+  global $user;
+  drupal_set_message('<pre>'.print_r($user, TRUE).'</pre>', 'status', TRUE);
+
+  db_query("DELETE FROM {mailfix_users} WHERE site_id = %d AND uid = %d", array(variable_get('mailfix_site_id', FALSE), $edit['uid']));
+  db_query("INSERT INTO {mailfix_users} (site_id, uid, domain_id, quota, forward, incoming_bcc, outgoing_bcc, autoresponder, status) "
+    ."VALUES (%d, %d, %d, %d, '%s', '%s', '%s', %s, %d)", 
     array( 
+      variable_get('mailfix_site_id', FALSE),
       $edit['uid'],
       $edit['domain_id'],
       $edit['quota'],
-      $edit['forward'],
+      $edit['redirection'],
       $edit['incoming_bcc'],
       $edit['outgoing_bcc'],
+      $edit['autoresponder'],
+      $user->status,
     )
   );
+  db_query('UPDATE {mailfix_users} mu, {users} du SET mu.mail = du.mail, mu.pass = du.pass WHERE mu.uid = du.uid and mu.site_id = %d ', variable_get('mailfix_site_id', FALSE));
+  db_query("DELETE FROM {mailfix_aliases} WHERE maps_to_siteid = %d AND maps_to_uid = %d", array(variable_get('mailfix_site_id', FALSE), $user->uid));
+  $aliases_arr = explode("\n", $edit['aliases']);
+  sort($aliases_arr);
+  foreach($aliases_arr as $aliases_val) {
+    if (strlen($aliases_val) > 0 ){
+      db_query("INSERT INTO {mailfix_aliases} (maps_to_siteid, maps_to_uid, alias) "
+	."VALUES (%d, %d, '%s')", 
+	array( 
+	  variable_get('mailfix_site_id', FALSE),
+	  $user->uid,
+	  trim($aliases_val),
+	)
+      );
+    }
+  }
 }
+
+/* 
+ *  Implementation of hook_perm()
+ */
+function mailfix_perm() {
+  return array('mailfix own aliases', 'mailfix own autoresponder', 'mailfix own redirection');
+}
+
+
+/**** todo:
+- postfix autoresponder setup or php script to trigger and log autoresponder activity ( to prevent autoresponder wars)
+- cron entries to do actual autoresponding
+- cron entries to guarantee email and pass sync even if non-api user add/edit/delete
+- autoresponder edit validation warning if start date later than end date
+- create mysql  pgsql views to remove the mail and pass column copying from 
+- domain alias administration (postponed)
+X user alias administration
+X postfix alias table configs (done?)
+- make roundcube respect drupal blocks/deletes
+X adjust /etc/postfix/drupal-*.cf queries for multisite setup
+- check validation of (and add userproofing for) aliases, redirection, in and out bcc addresses
+- allow quotas to be entered with kb, mb, etc at end
+- disconnect drupal site login status from mail receive / access status
+X FIX STATUS = 0 on update problem (done)
+- functionalize duplicate ops in module
+- dovecot delivery limit same message deliver only once
+- $mailfix_users_fields array schema not retrieved / accuracy?
+*/
Only in mailfix-6.x-2.x-dev/: mailfix.module.save
