Index: user_status.module
===================================================================
RCS file: /cvs/drupal-contrib/contributions/modules/user_status/user_status.module,v
retrieving revision 1.9.2.8
diff -u -p -r1.9.2.8 user_status.module
--- user_status.module	24 Jan 2008 05:09:46 -0000	1.9.2.8
+++ user_status.module	23 Mar 2008 09:50:56 -0000
@@ -146,6 +146,14 @@ function user_status_settings_form() {
     '#rows' => 10,
     '#description' => t('In addition to the variables described above, %login_url (a one-time login URL) is available in the notification message. If the "%reset_password" box is checked, you may also use %password.', array('%reset_password' => t('Reset password when account is activated'))),
   );
+  $form['activate_status']['user_status_activated_notify_list'] = array(
+    '#type' => 'textarea',
+    '#title' => t('Additional users to notify when an account is activated'),
+    '#default_value' => variable_get('user_status_activated_notify_list', ''),
+    '#description' => t('Comma-separated list of either email addresses or usernames to notify whenever an account is activated.'),
+    '#cols' => 72,
+    '#rows' => 1,
+  );
 
   $form['block_status'] = array(
     '#type' => 'fieldset',
@@ -172,6 +180,15 @@ function user_status_settings_form() {
     '#cols' => 72,
     '#rows' => 5,
   );
+  $form['block_status']['user_status_blocked_notify_list'] = array(
+    '#type' => 'textarea',
+    '#title' => t('Additional users to notify when an account is blocked'),
+    '#default_value' => variable_get('user_status_blocked_notify_list', ''),
+    '#description' => t('Comma-separated list of either email addresses or usernames to notify whenever an account is blocked.'),
+    '#cols' => 72,
+    '#rows' => 1,
+  );
+
 
   $form['delete_status'] = array(
     '#type' => 'fieldset',
@@ -198,11 +215,43 @@ function user_status_settings_form() {
     '#cols' => 72,
     '#rows' => 5,
   );
+  $form['delete_status']['user_status_deleted_notify_list'] = array(
+    '#type' => 'textarea',
+    '#title' => t('Additional users to notify when an account is deleted'),
+    '#default_value' => variable_get('user_status_deleted_notify_list', ''),
+    '#description' => t('Comma-separated list of either email addresses or usernames to notify whenever an account is deleted.'),
+    '#cols' => 72,
+    '#rows' => 1,
+  );
+  $form['#validate']['user_status_settings_validate'] = array();
 
   return system_settings_form($form);
 }
 
 /**
+ * Form validation handler to check the notification list settings.
+ */
+function user_status_settings_validate($form_id, $form_values) {
+  foreach (array('activated', 'blocked', 'deleted') as $key) {
+    $errors = array();
+    $name = 'user_status_'. $key .'_notify_list';
+    if (!empty($form_values[$name])) {
+      foreach (explode(',', $form_values[$name]) as $value) {
+        $value = trim($value);
+        if (!db_num_rows(db_query("SELECT uid FROM {users} WHERE LOWER(name) = LOWER('%s')", $value))) {
+          if (!valid_email_address($value)) {
+            $errors[] = check_plain($value);
+          }
+        }
+      }
+      if (!empty($errors)) {
+        form_set_error($name, t('The following values are neither existing user names on this site, nor valid e-mail addresses:' . theme('item_list', $errors)));
+      }
+    }
+  }
+}
+
+/**
  * Sends user an email based on the account modification
  *
  * @param string $op
@@ -230,7 +279,7 @@ function user_status_user($op, &$edit, &
             $variables = _user_status_get_variables($user, FALSE);
             $subject = strtr(variable_get('user_status_blocked_subject', user_status_blocked_subject()), $variables);
             $body = strtr(variable_get('user_status_blocked_body', user_status_blocked_body()), $variables);
-            return drupal_mail('user_status_blocked', $user->mail, $subject, $body, $from, $headers);
+            return _user_status_notify('blocked', $user, $subject, $body, $from, $headers);
           }
           break;
 
@@ -241,7 +290,7 @@ function user_status_user($op, &$edit, &
             $variables = _user_status_get_variables($user, TRUE);
             $subject = strtr(variable_get('user_status_activated_subject', user_status_activated_subject()), $variables);
             $body = strtr(variable_get('user_status_activated_body', user_status_activated_body()), $variables);
-            return drupal_mail('user_status_active', $user->mail, $subject, $body, $from, $headers);
+            return _user_status_notify('activated', $user, $subject, $body, $from, $headers);
           }
           break;
         } // end status switch
@@ -256,7 +305,7 @@ function user_status_user($op, &$edit, &
         $variables = _user_status_get_variables($user, FALSE);
         $subject = strtr(variable_get('user_status_deleted_subject', user_status_deleted_subject()), $variables);
         $body = strtr(variable_get('user_status_deleted_body', user_status_deleted_body()), $variables);
-        return drupal_mail('user_status_deleted', $user->mail, $subject, $body, $from, $headers);
+        return _user_status_notify('deleted', $user, $subject, $body, $from, $headers);
       }
     }
     break;
@@ -265,6 +314,62 @@ function user_status_user($op, &$edit, &
 }
 
 /**
+ * Helper that generates the notifcation emails for a given user action.
+ */
+function _user_status_notify($keyword, $user, $subject, $body, $from, $headers) {
+  $mail_key = 'user_status_'. $keyword;
+  // First, notify the user.
+  _user_status_mail($mail_key, $user->mail, $subject, $body, $from, $headers);
+  // Then, see if we're configured to notify anyone else.
+  $mail_key .= '_notify_list';
+  $notify_list = variable_get($mail_key, '');
+  if (!empty($notify_list)) {
+    foreach (explode(',', $notify_list) as $value) {
+      $value = trim($value);
+      $query = db_query("SELECT mail FROM {users} WHERE LOWER(name) = LOWER('%s')", $value);
+      $user = db_fetch_object($query);
+      if (!empty($user)) {
+        _user_status_mail($mail_key, $user->mail, $subject, $body, $from, $headers);
+      }
+      elseif (valid_email_address($value)) {
+        _user_status_mail($mail_key, $value, $subject, $body, $from, $headers);
+      }
+      else {
+        watchdog('user_status', t('Value %value in %setting_name is no longer a valid user.', array('%value' => $value, '%setting_name' => $mail_key)), WATCHDOG_ERROR);
+      }
+    }
+  }
+}
+
+/**
+ * Helper that actually sends a given notification email and logs it.
+ */
+function _user_status_mail($mailkey, $mail, $subject, $body, $from, $headers) {
+  drupal_mail($mailkey, $mail, $subject, $body, $from, $headers);
+  watchdog('user_status', t('Sent %message_type message to %mail', array('%message_type' => _user_status_get_text_from_key($mailkey), '%mail' => $mail)));
+}
+
+/**
+ * Return the human-readable text for a given mail key.
+ */
+function _user_status_get_text_from_key($mailkey) {
+  switch ($mailkey) {
+    case 'user_status_activated':
+      return t('User activated');
+    case 'user_status_activated_notify_list':
+      return t('User activated notification list');
+    case 'user_status_blocked':
+      return t('User blocked');
+    case 'user_status_blocked_notify_list':
+      return t('User blocked notification list');
+    case 'user_status_deleted':
+      return t('User deleted');
+    case 'user_status_deleted_notify_list':
+      return t('User deleted notification list');
+  }
+}
+
+/**
  * Return an array of substitution variables for email bodies and subjects.
  *
  * @param $user
