--- mailman_manager.module.orig	2008-12-27 03:43:48.000000000 +0100
+++ mailman_manager.module	2008-12-27 04:50:23.000000000 +0100
@@ -1,28 +1,25 @@
 <?php
-  // $Id: mailman_manager.module,v 1.33.2.1 2008/12/21 06:01:16 taniwha Exp $
-  /**
-   * @file
-   * Allows users to subscribe to Mailman mailing lists via a form in
-   * their user profile.  List of mailing lists is defined by administrator.
-   * Module maintains a list of user subscriptions and passwords.
-   * Module sends requests for subscription changes to Mailman request address.
-   */
-
-  /**
-   * Implementation of hook_help().
-   */
+// $Id: mailman_manager.module,v 1.33.2.1 2008/12/21 06:01:16 taniwha Exp $
+
+/**
+ * @file
+ * Allows users to subscribe to Mailman mailing lists via a form in
+ * their user profile.  List of mailing lists is defined by administrator.
+ * Module maintains a list of user subscriptions and passwords.
+ * Module sends requests for subscription changes to Mailman request address.
+ */
+
+/**
+ * Implementation of hook_help().
+ */
 function mailman_manager_help($path, $arg) {
   switch ($path) {
-  case 'admin/modules#description':
-    return t('Utilities related to Mailman manager, subscription system and database for Mailman mailing lists.');
-    break;
-  case 'admin/help#mailman_manager':
-      
-    $output = '<p>'. t('Utilities related to Mailman manager, subscription system and database for Mailman mailing lists.') .'</p>';
-    $output .= '<p>'. t("This tool works using the mailman email interface. All commands sent by this module are also BCC'ed to the list admin. One can optionally specify the web interface and/or the web archive of each list. The module can not know if a user is previously subscribed but allows the user to specify a password which can then be used to hook into an existing account.") .'</p>';
-      
-    return $output;
-    break;
+    case 'admin/modules#description':
+      return t('Utilities related to Mailman manager, subscription system and database for Mailman mailing lists.');
+    case 'admin/help#mailman_manager':
+      $output = '<p>'. t('Utilities related to Mailman manager, subscription system and database for Mailman mailing lists.') .'</p>';
+      $output .= '<p>'. t("This tool works using the mailman email interface. All commands sent by this module are also BCC'ed to the list admin. One can optionally specify the web interface and/or the web archive of each list. The module can not know if a user is previously subscribed but allows the user to specify a password which can then be used to hook into an existing account.") .'</p>';
+      return $output;
   }
 }
 
@@ -33,7 +30,7 @@
   $links = array();
 
   if ($type == 'page' && user_access('access content')) {
-    //    $links[] = l(t('mailing %list', array('%list' => format_plural(_mailman_manager_get_count(), 'list', 'lists'))), 'mailman_manager', array('title' => t('Subscribe to mailing %list', array('%list' => format_plural(_mailman_manager_get_count(), 'list', 'lists')))));
+    // $links[] = l(t('mailing %list', array('%list' => format_plural(_mailman_manager_get_count(), 'list', 'lists'))), 'mailman_manager', array('title' => t('Subscribe to mailing %list', array('%list' => format_plural(_mailman_manager_get_count(), 'list', 'lists')))));
     $links[] = l(t('Mailing Lists'), 'mailman_manager', array('title' => t('Subscribe to mailing lists')));
   }
 
@@ -52,44 +49,51 @@
  */
 function mailman_manager_menu() {
   $items = array();
-  $items['mailman_manager'] = array('title' => 'Mailing Lists',
-                                    'page callback' => 'mailman_manager_page',
-                                    'access arguments' => array('access mailman_manager'),
-                                    'type' => MENU_NORMAL_ITEM);
-    
-  $items['admin/settings/mailman_manager'] = array('title' => 'Mailing Lists',
-                                                   'access arguments' => array('administer mailman_manager'),
-                                                   'description' => 'Allow users to subscribe and change their subscriptions to Mailman mailing lists.',
-                                                   'page callback' => '_mailman_manager_admin_display',
-                                                   'type' => MENU_NORMAL_ITEM);
-  
-  $items['admin/settings/mailman_manager/add'] = array('title' => 'Add New',
-                                                       'access arguments' => array('administer mailman_manager'),
-                                                       'description' => 'Add new Mailman mailing lists.',
-                                                       'page callback' => 'drupal_get_form',
-                                                       'page arguments' => array('mailman_manager_admin_form', NULL),
-                                                       'type' => MENU_NORMAL_ITEM);
-  $items['admin/settings/mailman_manager/edit/%'] = array('title' => 'Edit Mailing list',
-                                                          'access arguments' => array('administer mailman_manager'),
-                                                          'description' => 'Edit Mailman mailing lists.',
-                                                          'page callback' => 'drupal_get_form',
-                                                          'page arguments' => array('mailman_manager_admin_form', 4),
-                                                          'type' => MENU__CALLBACK);
-  $items['admin/settings/mailman_manager/delete/%'] = array('title' => 'Delete Mailing list',
-                                                          'access arguments' => array('administer mailman_manager'),
-                                                          'description' => 'Add new Mailman mailing lists.',
-                                                          'page callback' => 'drupal_get_form',
-                                                          'page arguments' => array('mailman_manager_admin_delete_form', 4),
-                                                          'type' => MENU_CALLBACK);
-  
+  $items['mailman_manager'] = array(
+    'title' => 'Mailing Lists',
+    'page callback' => 'mailman_manager_page',
+    'access arguments' => array('access mailman_manager'),
+    'type' => MENU_NORMAL_ITEM,
+  );
+  $items['admin/settings/mailman_manager'] = array(
+    'title' => 'Mailing Lists',
+    'access arguments' => array('administer mailman_manager'),
+    'description' => 'Allow users to subscribe and change their subscriptions to Mailman mailing lists.',
+    'page callback' => '_mailman_manager_admin_display',
+    'type' => MENU_NORMAL_ITEM,
+  );
+  $items['admin/settings/mailman_manager/add'] = array(
+    'title' => 'Add New',
+    'access arguments' => array('administer mailman_manager'),
+    'description' => 'Add new Mailman mailing lists.',
+    'page callback' => 'drupal_get_form',
+    'page arguments' => array('mailman_manager_admin_form', NULL),
+    'type' => MENU_NORMAL_ITEM,
+  );
+  $items['admin/settings/mailman_manager/edit/%'] = array(
+    'title' => 'Edit Mailing list',
+    'access arguments' => array('administer mailman_manager'),
+    'description' => 'Edit Mailman mailing lists.',
+    'page callback' => 'drupal_get_form',
+    'page arguments' => array('mailman_manager_admin_form', 4),
+    'type' => MENU_CALLBACK,
+  );
+  $items['admin/settings/mailman_manager/delete/%'] = array(
+    'title' => 'Delete Mailing list',
+    'access arguments' => array('administer mailman_manager'),
+    'description' => 'Add new Mailman mailing lists.',
+    'page callback' => 'drupal_get_form',
+    'page arguments' => array('mailman_manager_admin_delete_form', 4),
+    'type' => MENU_CALLBACK,
+  );
   $items['user/%user/mailman_manager'] = array(
-						    'title' => 'Mailing Lists',
-						    'page callback' => 'drupal_get_form',
-						    'page arguments' => array('_mailman_manager_user_form',1),
-						    'type' => MENU_LOCAL_TASK,
-						    'access arguments' => array('access mailman_manager'),
-						    'weight' => 9,
-						    );
+    'title' => 'Mailing Lists',
+    'access arguments' => array('access mailman_manager'),
+    'page callback' => 'drupal_get_form',
+    'page arguments' => array('_mailman_manager_user_form', 1),
+    'type' => MENU_LOCAL_TASK,
+    'weight' => 9,
+  );
   return $items;
 }
 
@@ -106,7 +110,7 @@
   }
 }
 
-function _mailman_manager_user_form(&$form_state,$account) {
+function _mailman_manager_user_form(&$form_state, $account) {
   $form = array();
   $output = '<div class="mailman_manager">';
   $output .= '<p>'. t('The following email address will be subscribed to the email lists:') .'<br /><strong>'. $account->mail .'</strong><br />';
@@ -120,38 +124,43 @@
     $status = 0;
     foreach ($lists as $list) {
       $form['list'. trim($list['lid'])] = array(
-                                                        '#type' => 'fieldset',
-                                                        '#title' => $list['name'],
-                                                        '#collapsible' => TRUE
-                                                        );
+        '#type' => 'fieldset',
+        '#title' => $list['name'],
+        '#collapsible' => TRUE
+      );
 
       $subscrip = _mailman_manager_get_subscriptions($account->uid, $list['lid']);
       if ($subscrip['lstatus'] == 0) {
-        $options = array('2' => t('Subscribe for digest (receive emails in a bundle)'),
-                         '3' => t('Subscribe for all Mail (normal delivery)'));
+        $options = array(
+          '2' => t('Subscribe for digest (receive emails in a bundle)'),
+          '3' => t('Subscribe for all Mail (normal delivery)')
+        );
         $status++;
       }
       else {
-        $options = array('0' => t('Unsubscribe'),
-                         '1' => t('No mail (temporarily disable delivery)'),
-                         '2' => t('Subscribe for digest (receive emails in a bundle)'),
-                         '3' => t('Subscribe for all Mail (normal delivery)'),
-                         '4' => t('Mail password'));
+        $options = array(
+          '0' => t('Unsubscribe'),
+          '1' => t('No mail (temporarily disable delivery)'),
+          '2' => t('Subscribe for digest (receive emails in a bundle)'),
+          '3' => t('Subscribe for all Mail (normal delivery)'),
+          '4' => t('Mail password')
+        );
       }
       $subscrip = _mailman_manager_get_subscriptions($account->uid, $list['lid']);
       $form['list'. trim($list['lid'])]['options'. trim($list['lid'])] = array(
-                                                                                       '#type' => 'radios',
-                                                                                       '#title' => t('Change your subscription'),
-                                                                                       '#options' => $options,
-                                                                                       '#default_value' => $subscrip['lstatus']
-                                                                                       );
+        '#type' => 'radios',
+        '#title' => t('Change your subscription'),
+        '#options' => $options,
+        '#default_value' => $subscrip['lstatus']
+      );
       if ($subscrip['lstatus'] == 0) {
-        $form['list'. trim($list['lid'])]['pass'. trim($list['lid'])] = array('#type' => 'textfield',
-                                                                                      '#title' => t('Password for %listname (optional)', array('%listname' => $list['name'])),
-                                                                                      '#size' => 15,
-                                                                                      '#required' => FALSE,
-                                                                                      '#default_value' => t($subscrip['lpass'])
-                                                                                      );
+        $form['list'. trim($list['lid'])]['pass'. trim($list['lid'])] = array(
+          '#type' => 'textfield',
+          '#title' => t('Password for %listname (optional)', array('%listname' => $list['name'])),
+          '#size' => 15,
+          '#required' => FALSE,
+          '#default_value' => t($subscrip['lpass'])
+        );
       }
       if ($list['web'] || $list['webarch']) {
         $link_output = '<p>'. t('Visit') .': ';
@@ -175,22 +184,24 @@
     }
     $output .= '</div>';
     $form['lists']['oldemail'] = array(
-				       '#type' => 'hidden',
-				       '#default_value' => $subscrip['lmail']
-				       );
+      '#type' => 'hidden',
+      '#default_value' => $subscrip['lmail'],
+    );
     $form['lists']['newemail'] = array(
-				       '#type' => 'hidden',
-				       '#default_value' => $account->mail
-				       );
-    
-  }
-  $form['mman_info'] = array('#type' => 'markup', '#value' => $output, '#weight' => -1);
+      '#type' => 'hidden',
+      '#default_value' => $account->mail,
+    );
+  }
+  $form['mman_info'] = array(
+    '#type' => 'markup',
+    '#value' => $output,
+    '#weight' => -1,
+  );
   $form['submit'] = array(
-                          '#type' => 'submit',
-                          '#value' => t('Save')
-                          );
+    '#type' => 'submit',
+    '#value' => t('Save')
+  );
   return $form;
-
 }
 
 /**
@@ -201,44 +212,43 @@
  */
 function _mailman_manager_user_form_submit($form, &$form_state) {
   if (!is_numeric($uid = arg(1))) {
-      return false;
+    return FALSE;
   }
   $account=user_load($uid);
   $lists = _mailman_manager_get_lists();
   foreach ($lists as $list) {
     $subscrip = _mailman_manager_get_subscriptions($account->uid, $list['lid']);
     $listno = 'options'. trim($list['lid']);
-    $query = "SELECT * FROM {mailman_users} WHERE uid = %d AND lid = %d";
+    $query = 'SELECT * FROM {mailman_users} WHERE uid = %d AND lid = %d';
     $result = db_query($query, $account->uid, $list['lid']);
     $subscrip = db_fetch_array($result);
     if ($form_state['values']['pass'. trim($list['lid'])] == '') {
       $password = $subscrip['lpass'];
-    } 
+    }
     else {
       $password = $form_state['values']['pass'. trim($list['lid'])];
     }
     $form_state['values']['pass'. trim($list['lid'])] = '';
     if ($subscrip['lstatus'] != $form_state['values'][$listno]) {
       _mailman_manager_update_subscriptions($account->uid, $list['lid'], $form_state['values'][$listno],
-					    $subscrip['lstatus'], $account->mail, $password);   
+        $subscrip['lstatus'], $account->mail, $password);
     }
   }
   drupal_set_message(t('Your mailing list subscriptions have been updated'));
-
 }
 
 function mailman_manager_admin_delete_form(&$form_state, $lid) {
   $form = array();
   $list = mailman_manager_get_list($lid);
   $form['lid'] = array(
-                       '#type' => 'hidden', 
-                       '#value' => $lid
-                       );
+    '#type' => 'hidden',
+    '#value' => $lid,
+  );
   $form['message'] = array('#type' => 'markup', '#value' => t('Are you sure?'));
   $form['submit'] = array(
-                          '#type' => 'submit',
-                          '#value' => t('Delete !listname', array('!listname' => $list->name))
-                          );
+    '#type' => 'submit',
+    '#value' => t('Delete !listname', array('!listname' => $list->name)),
+  );
   return $form;
 }
 
@@ -246,12 +256,12 @@
   if (!user_access('administer mailman_manager')) {
     return '';
   }
-  $result = db_query("DELETE FROM {mailman_users} WHERE lid = %d", $form_state['values']['lid']);
-  
-  if ($result && db_query("DELETE FROM {mailman_lists} WHERE lid = %d", $form_state['values']['lid'])) {
+  $result = db_query('DELETE FROM {mailman_users} WHERE lid = %d', $form_state['values']['lid']);
+
+  if ($result && db_query('DELETE FROM {mailman_lists} WHERE lid = %d', $form_state['values']['lid'])) {
     watchdog('mailman man', 'Successfully deleted Mailman list %listid', array('%listid' => $list['lid']), WATCHDOG_NOTICE);
     drupal_goto('admin/settings/mailman_manager');
-  }  
+  }
   else {
     watchdog('mailman man', 'Mailman list %listid deletion failed', array('%listid' => $list['lid']), WATCHDOG_ERROR);
     drupal_set_message('Error deleting Mailing list', 'error');
@@ -259,84 +269,79 @@
 }
 
 function mailman_manager_get_list($id) {
-  return db_fetch_object(db_query("SELECT * FROM {mailman_lists} WHERE lid = %d", $id));
+  return db_fetch_object(db_query('SELECT * FROM {mailman_lists} WHERE lid = %d', $id));
 }
 
 
-function mailman_manager_admin_form(&$form_state, $lid = false) {
+function mailman_manager_admin_form(&$form_state, $lid = NULL) {
   $form = array();
   if ($lid) {
     $list = mailman_manager_get_list($lid);
     $form['lid'] = array(
-                         '#type' => 'hidden',
-                         '#value' => $lid
-                         );
+      '#type' => 'hidden',
+      '#value' => $lid,
+    );
   }
 
   $form['name'] = array(
-                        '#title' => 'Mailing List name',
-                        '#default_value' => $list->name,
-                        '#type' => 'textfield',
-                        '#required' => true,
-                        );
+    '#title' => 'Mailing List name',
+    '#default_value' => !empty($list->name) ? $list->name : '',
+    '#type' => 'textfield',
+    '#required' => TRUE,
+  );
   $form['reqaddress'] = array(
-                              '#title' => t("Mailing List 'request' address"),
-                              '#default_value' => $list->command,
-                              '#description' => t('Commands are sent here'),
-                              '#type' => 'textfield',
-                              '#required' => true
-                              );
+    '#title' => t("Mailing List 'request' address"),
+    '#default_value' => !empty($list->command) ? $list->command : '',
+    '#description' => t('Commands are sent here'),
+    '#type' => 'textfield',
+    '#required' => TRUE,
+  );
   $form['adminaddress'] = array(
-                                '#title' => t("Mailing List 'admin' address"),
-                                '#default_value' => $list->admin,
-                                '#description' =>  t('(Optional. admin is used for BCC)'),
-                                '#type' => 'textfield',
-                                );
+    '#title' => t("Mailing List 'admin' address"),
+    '#default_value' => !empty($list->admin) ? $list->admin : '',
+    '#description' => t('(Optional. admin is used for BCC)'),
+    '#type' => 'textfield',
+  );
   $form['webaddress'] = array(
-                              '#title' => t('Mailing list web address for users'), 
-                              '#default_value' => $list->web,
-                              '#description' => t('http://...' ) . t("Leave empty if hidden."),
-                              '#type' => 'textfield',
-                              );
+    '#title' => t('Mailing list web address for users'),
+    '#default_value' => !empty($list->web) ? $list->web : '',
+    '#description' => t('http://...' ) . t('Leave empty if hidden.'),
+    '#type' => 'textfield',
+  );
   $form['webarchive'] = array(
-                              '#title' => t('Mailing list web archive address for users'),
-                              '#default_value' => $list->webarch,
-                              '#description' => t('http://.... ') .  t('Leave empty if hidden.'),
-                              '#type' => 'textfield',
-                              );
-  
+    '#title' => t('Mailing list web archive address for users'),
+    '#default_value' => !empty($list->webarch) ? $list->webarch : '',
+    '#description' => t('http://.... ') . t('Leave empty if hidden.'),
+    '#type' => 'textfield',
+  );
   $form['Submit'] = array(
-                          '#type' => 'submit',
-                          '#value' => ($lid ? t('Save') : t('Add')),
-                          );
+    '#type' => 'submit',
+    '#value' => ($lid ? t('Save') : t('Add')),
+  );
+
   return $form;
-  
 }
 
 function mailman_manager_admin_form_validate($form, &$form_state) {
-  //Check name is unique
-  $result = db_query("SELECT COUNT(*) from {mailman_lists} WHERE name = '%s'". ($form_state['values']['lid'] ? ' AND lid <> %d' : ''), $form_state['values']['name'], $form_state['values']['lid']);
+  // Check name is unique
+  $lid = !empty($form_state['values']['lid']) ? $form_state['values']['lid'] : 0;
+  $result = db_query("SELECT COUNT(*) from {mailman_lists} WHERE name = '%s'". ($lid ? ' AND lid <> %d' : ''), $form_state['values']['name'], $lid);
   $num_rows = db_result($result);
   if ($num_rows != '0') {
     form_set_error('name', 'Name already in use');
   }
-  
   if ($form_state['values']['reqaddress'] && !valid_email_address($form_state['values']['reqaddress'])) {
     form_set_error('reqaddress', t('"%reqaddress" is not a valid email address', array('%reqaddress' => $form_state['values']['reqaddress'])));
   }
-  
   if ($form_state['values']['adminaddress'] && !valid_email_address($form_state['values']['adminaddress'])) {
     form_set_error('adminaddress', t('"%adminaddress" is not a valid email address', array('%adminaddress' => $form_state['values']['adminaddress'])));
   }
-  
   if ($form_state['values']['webaddress'] && !valid_url($form_state['values']['webaddress'])) {
     form_set_error('webaddress', t('"%webaddress" is not a valid url', array('%webaddress' => $form_state['values']['webaddress'])));
   }
-  
   if ($form_state['values']['webarchive'] && !valid_url($form_state['values']['webarchive'])) {
     form_set_error('webarchive', t('"%webarchive" is not a valid url', array('%webarchive' => $form_state['values']['webarchive'])));
   }
-    
 }
 
 /**
@@ -346,40 +351,39 @@
   if (!user_access('administer mailman_manager')) {
     return '';
   }
-  if ($form_state['values']['lid']) {
-    //Update existin
+  if (!empty($form_state['values']['lid'])) {
+    // Update existing
     $query = "UPDATE {mailman_lists} SET name = '%s', command = '%s', admin = '%s', web = '%s', webarch = '%s' WHERE lid = %d";
-    $result = db_query($query, 
-                       $form_state['values']['name'], 
-                       $form_state['values']['reqaddress'], 
-                       $form_state['values']['adminaddress'], 
-                       $form_state['values']['webaddress'], 
-                       $form_state['values']['webarchive'],
-                       $form_state['values']['lid']
-                       );
+    $result = db_query($query,
+      $form_state['values']['name'],
+      $form_state['values']['reqaddress'],
+      $form_state['values']['adminaddress'],
+      $form_state['values']['webaddress'],
+      $form_state['values']['webarchive'],
+      $form_state['values']['lid']);
     if ($result) {
-      drupal_goto('admin/settings/mailman_manager');
-      exit;
+      $form_state['redirect'] = 'admin/settings/mailman_manager';
     }
-  } 
+  }
   else {
-    //Create new
+    // Create new
     $query = "INSERT INTO {mailman_lists} (name, command, admin, web, webarch) VALUES ('%s', '%s', '%s', '%s', '%s')";
-    if (db_query($query, 
-                 $form_state['values']['name'], 
-                 $form_state['values']['reqaddress'], 
-                 $form_state['values']['adminaddress'], 
-                 $form_state['values']['webaddress'], 
-                 $form_state['values']['webarchive'])) {
+    $result = db_query($query,
+      $form_state['values']['name'],
+      $form_state['values']['reqaddress'],
+      $form_state['values']['adminaddress'],
+      $form_state['values']['webaddress'],
+      $form_state['values']['webarchive']);
+    if ($result) {
       $message = 'New Mailman list %name successfully created';
       watchdog('mailman man', $message, array('%name' => $name), WATCHDOG_NOTICE);
-      drupal_goto('admin/settings/mailman_manager');
-    } 
+      $form_state['redirect'] = 'admin/settings/mailman_manager';
+    }
     else {
-      $message = 'Error in creating new Mailman list %name';
-      watchdog('mailman man', $message, array('%name' => $name), WATCHDOG_ERROR);
-      return t($message, array('%name' => $name));;
-    }  
+      $message = t('Error in creating new Mailman list %name', array('%name' => $name));
+      watchdog('mailman man', $message, WATCHDOG_ERROR);
+      return $message;
+    }
   }
 }
 
@@ -390,43 +394,37 @@
   if (!user_access('administer mailman_manager')) {
     return '';
   }
-  $output = '';
-  
-  $headers = array(array('data' => t('ID'), 'field' => 'lid', 'sort' => 'asc'), 
-                   array('data' => t('Name'), 'field' => 'name'),
-                   array('data' => t('Request Address'), 'field' => 'command'),
-                   );
-  
-    
-  $query = "SELECT * FROM {mailman_lists}";
-  $query .= tablesort_sql($headers);
-  
+
+  $headers = array(array('data' => t('ID'), 'field' => 'lid', 'sort' => 'asc'),
+    array('data' => t('Name'), 'field' => 'name'),
+    array('data' => t('Request Address'), 'field' => 'command'),
+  );
+
+  $query = 'SELECT * FROM {mailman_lists} '. tablesort_sql($headers);
+
   $num_per_page = 15;
   $result = pager_query($query, $num_per_page);
   $lists = array();
   while ($list = db_fetch_array($result)) {
     unset($list['web']);
-    unset($list['webarch']);    
-    unset($list['admin']);    
+    unset($list['webarch']);
+    unset($list['admin']);
     $list['edit'] = l(t('Edit'), 'admin/settings/mailman_manager/edit/'. $list['lid']);
     $list['delete'] = l(t('Delete'), 'admin/settings/mailman_manager/delete/'. $list['lid']);
     $lists[] = $list;
-  }           
+  }
 
-  $output .= theme('table', $headers, $lists);
-  $output .= theme('pager', array(), $num_per_page);
-  
+  $output = theme('table', $headers, $lists) . theme('pager', array(), $num_per_page);
   $output .= l(t('Add new mailing list'), 'admin/settings/mailman_manager/add');
 
-  return $output;  
-  
+  return $output;
 }
 
 /**
  * Return array of objects of current mailing lists.
  */
 function _mailman_manager_get_lists() {
-  $result = db_query("SELECT * FROM {mailman_lists} ORDER BY name");
+  $result = db_query('SELECT * FROM {mailman_lists} ORDER BY name');
   $lists = array();
   while ($list = db_fetch_array($result)) {
     $lists[] = $list;
@@ -438,18 +436,19 @@
  * Return array of user's subscriptions to mailing lists.
  */
 function _mailman_manager_get_subscriptions($uid, $lid) {
-  $query = "SELECT * FROM {mailman_users} WHERE uid = %d AND lid = %d";
-  $result = db_query($query, $uid, $lid);
-  $result = db_fetch_array($result);
-  //If there are no entries for this user then set subscriptions to zero;
-  //and return no mail button option for display;
-  if (!$result || count($result) == 0) {
-    $query = "INSERT INTO {mailman_users} (uid, lid, lstatus) VALUES (%d, %d, %d)";
-    db_query($query, $uid, $lid, 0);    
-    $result = array('uid' => $uid, 'lid' => $lid, 'lstatus' => 0);
+  $subscription = db_fetch_array(db_query('SELECT * FROM {mailman_users} WHERE uid = %d AND lid = %d', $uid, $lid));
+  // If there are no entries for this user then set subscriptions to zero
+  // and return no mail button option for display
+  if (empty($subscription)) {
+    db_query('INSERT INTO {mailman_users} (uid, lid, lstatus) VALUES (%d, %d, %d)', $uid, $lid, 0);
+    $subscription = array(
+      'uid' => $uid,
+      'lid' => $lid,
+      'lstatus' => 0,
+    );
   }
 
-  return $result;
+  return $subscription;
 }
 
 /**
@@ -457,74 +456,66 @@
  */
 function _mailman_manager_update_subscriptions($uid, $lid, $lstatus, $oldstatus, $mail, $password) {
   // Do not update status for 'mail password':
-  
-  
   if ($lstatus != 4) {
-    $query = "UPDATE {mailman_users} SET lstatus = %d WHERE uid = %d AND lid = %d";
-    db_query($query, $lstatus, $uid, $lid);
+    db_query ('UPDATE {mailman_users} SET lstatus = %d WHERE uid = %d AND lid = %d', $lstatus, $uid, $lid);
   }
-        
-        
+
   switch ($lstatus) {
-    
     // Unsubscribe selected;
-  case 0:
-    $command = 'unsubscribe '. $password .' address='. $mail;
-    _mailman_manager_setdelivery($uid, $lid, $mail, $command);  
-    watchdog('mailman man', 'User %uid unsubscribed from list %lid', array('%lid' => $lid, '%uid' => $uid), WATCHDOG_NOTICE);
-    break;
-  
-  
+    case 0:
+      $command = 'unsubscribe '. $password .' address='. $mail;
+      _mailman_manager_setdelivery($uid, $lid, $mail, $command);
+      watchdog('mailman man', 'User %uid unsubscribed from list %lid', array('%lid' => $lid, '%uid' => $uid), WATCHDOG_NOTICE);
+      break;
+
     // No email selected;
-  case 1:
-    $command = 'set authenticate '. $password .' address='. $mail ."\n";
-    $command .= 'set delivery off'; 
-    _mailman_manager_setdelivery($uid, $lid, $mail, $command);  
-    watchdog('mailman man', 'Subscription to list %lid for user %uid changed to no mail', array('%lid' => $lid, '%uid' => $uid), WATCHDOG_NOTICE);
-    break;
+    case 1:
+      $command = 'set authenticate '. $password .' address='. $mail ."\n";
+      $command .= 'set delivery off';
+      _mailman_manager_setdelivery($uid, $lid, $mail, $command);
+      watchdog('mailman man', 'Subscription to list %lid for user %uid changed to no mail', array('%lid' => $lid, '%uid' => $uid), WATCHDOG_NOTICE);
+      break;
 
     //Digest selected;
-  case 2: 
-    if ($oldstatus == 0) {
-      _mailman_manager_subscribe($uid, $lid, $mail, 'digest', $password);
-    } 
-    else {
-      $command = 'set authenticate '. $password .' address='. $mail ."\n";
-      $command .= "set delivery on\n"; 
-      $command .= "set digest plain"; 
-      _mailman_manager_setdelivery($uid, $lid, $mail, $command);  
-      watchdog('mailman man', 'Subscription to list %lid for user %uid changed to digest', array('%lid' => $lid, '%uid' => $uid), WATCHDOG_NOTICE);
-    }    
-    break;     
-  
+    case 2:
+      if ($oldstatus == 0) {
+        _mailman_manager_subscribe($uid, $lid, $mail, 'digest', $password);
+      }
+      else {
+        $command = 'set authenticate '. $password .' address='. $mail ."\n";
+        $command .= "set delivery on\n";
+        $command .= 'set digest plain';
+        _mailman_manager_setdelivery($uid, $lid, $mail, $command);
+        watchdog('mailman man', 'Subscription to list %lid for user %uid changed to digest', array('%lid' => $lid, '%uid' => $uid), WATCHDOG_NOTICE);
+      }
+      break;
+
     //All mail selected;
-  case 3: 
-    if ($oldstatus == 0) {
-      _mailman_manager_subscribe($uid, $lid, $mail, 'nodigest', $password);
-    } 
-    else {
-      $command = 'set authenticate '. $password .' address='. $mail ."\n";
-      $command .= "set delivery on\n"; 
-      $command .= "set digest off"; 
-      _mailman_manager_setdelivery($uid, $lid, $mail, $command);
-      watchdog('mailman man', 'Subscription to list %lid for user %uid changed to all mail', array('%lid' => $lid, '%uid' => $uid), WATCHDOG_NOTICE);
-    }
-    break;
-  
-    //mail pwd selected;
-  case 4: 
-    if ($oldstatus == 0) {
-      return;
-    } 
-    else {
-      $command = 'password address='. $mail ."\n";
-      _mailman_manager_setdelivery($uid, $lid, $mail, $command);
-      watchdog('mailman man', 'Password for list %lid for user %uid sent by mail', array('%lid' => $lid, '%uid' => $uid), WATCHDOG_NOTICE);
-    }
-    break;
+    case 3:
+      if ($oldstatus == 0) {
+        _mailman_manager_subscribe($uid, $lid, $mail, 'nodigest', $password);
+      }
+      else {
+        $command = 'set authenticate '. $password .' address='. $mail ."\n";
+        $command .= "set delivery on\n";
+        $command .= 'set digest off';
+        _mailman_manager_setdelivery($uid, $lid, $mail, $command);
+        watchdog('mailman man', 'Subscription to list %lid for user %uid changed to all mail', array('%lid' => $lid, '%uid' => $uid), WATCHDOG_NOTICE);
+      }
+      break;
 
+    //mail pwd selected;
+    case 4:
+      if ($oldstatus == 0) {
+        return;
+      }
+      else {
+        $command = 'password address='. $mail ."\n";
+        _mailman_manager_setdelivery($uid, $lid, $mail, $command);
+        watchdog('mailman man', 'Password for list %lid for user %uid sent by mail', array('%lid' => $lid, '%uid' => $uid), WATCHDOG_NOTICE);
+      }
+      break;
   }
-  return;
 }
 
 /**
@@ -537,16 +528,16 @@
   $list = db_fetch_array($result);
   $commandaddress = $list['command'];
   $adminaddress = $list['admin'];
-  if ($password == "") {
+  if ($password == '') {
     $password = _mailman_manager_rand_str(5);
-    $password .= trim($uid) . substr($mail, 0, 1);
+    $password .= trim($uid) . drupal_substr($mail, 0, 1);
   }
   $params['command'] = 'subscribe '. $password .' '. $digest .' address='. trim($mail);
 
   // If the adminaddress was given, use BCC
-  if ($adminaddress != "") {
+  if ($adminaddress != '') {
     $params['bcc'] = $adminaddress;
-  } 
+  }
   $mailsuccess = drupal_mail('mailman_manager', 'subscribe', $commandaddress, language_default(), $params);
   $query = "UPDATE {mailman_users} SET lmail = '%s', lpass = '%s' WHERE uid = %d AND lid = %d";
   if ($mailsuccess['result'] && db_query($query, $mail, $password, $uid, $lid)) {
@@ -554,16 +545,15 @@
   }
   else {
     watchdog('mailman man', 'New subscription to list %lid for user %uid failed.',  array('%lid' => $lid, '%uid' => $uid), WATCHDOG_ERROR);
-  }    
-  return; 
+  }
 }
 
 /**
  * Update settings for a subscription by sending request email to Mailman.
- */  
+ */
 function _mailman_manager_setdelivery($uid, $lid, $mail, $command) {
   $params = array('command' => $command);
-  $query = "SELECT * FROM {mailman_lists} WHERE lid = %d";
+  $query = 'SELECT * FROM {mailman_lists} WHERE lid = %d';
   $result = db_query($query, $lid);
   $list = db_fetch_array($result);
   $commandaddress = $list['command'];
@@ -575,7 +565,6 @@
   }
   watchdog('mailman man', 'Mail command sent to Mailman to: %commandaddress. Command: %command', array('%commandaddress' => $commandaddress, '%command' => $command), WATCHDOG_NOTICE);
   drupal_mail('mailman_manager', 'notify', $commandaddress, language_default(), $params);
-  return; 
 }
 
 function mailman_manager_mail($key, &$message, $params) {
@@ -590,9 +579,9 @@
  * Generation of five character random text string for Mailman password.
  */
 function _mailman_manager_rand_str($size) {
-  $feed = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz";
+  $feed = 'ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz';
   for ($i = 0; $i < $size; $i++) {
-    $rand_str .= substr($feed, rand(0, strlen($feed) - 1), 1);
+    $rand_str .= drupal_substr($feed, rand(0, drupal_strlen($feed) - 1), 1);
   }
   return $rand_str;
 }
--- mailman_manager.install.orig	2008-12-27 03:45:04.000000000 +0100
+++ mailman_manager.install	2008-12-27 03:51:28.000000000 +0100
@@ -1,5 +1,11 @@
 <?php
 // $Id: mailman_manager.install,v 1.12 2008/12/21 05:51:01 taniwha Exp $
+
+/**
+ * @file
+ * Mailman Manager install/schema hooks.
+ */
+
 /**
  * Implementation of hook_install().
  */
@@ -23,96 +29,95 @@
   $schema['mailman_lists'] = array(
     'description' => t('Stores specific information for mailiman lists.'),
     'fields' => array(
-      'lid'     => array(
+      'lid' => array(
         'type' => 'serial',
         'unsigned' => TRUE,
         'not null' => TRUE,
-        'description' => t("The list lid.")
-        ),
+        'description' => t('The list id.'),
+      ),
       'name' => array(
         'type' => 'varchar',
         'length' => 48,
         'not null' => TRUE,
         'default' => '',
         'description' => t('Mailing List name'),
-        ),
+      ),
       'command' => array(
         'type' => 'varchar',
         'length' => 72,
         'not null' => TRUE,
         'default' => '',
         'description' => t("Mailing List 'request' address"),
-        ),
+      ),
       'admin' => array(
         'type' => 'varchar',
         'length' => 48,
         'default' => '',
         'description' => t("Mailing List 'admin' address"),
-        ), 
+      ),
       'web' => array(
         'type' => 'varchar',
         'length' => 255,
         'default' => '',
         'description' => t('Mailing list web address for users.'),
-        ),
+      ),
       'webarch' => array(
         'type' => 'varchar',
         'length' => 255,
         'default' => '',
         'description' => t('Mailing list web archive address for users'),
-        ),
       ),
+    ),
     'primary key' => array('lid'),
-    );
+  );
 
   $schema['mailman_users'] = array(
     'description' => t('Stores subscription information for users.'),
     'fields' => array(
-      'uid'     => array(
+      'uid' => array(
         'type' => 'int',
         'disp-width' => 10,
         'not null' => TRUE,
         'default' => 0,
-        'description' => t("The list id.")
-        ),
-      'lid'     => array(
+        'description' => t('User id.'),
+      ),
+      'lid' => array(
         'type' => 'int',
         'disp-width' => 10,
         'not null' => TRUE,
         'default' => 0,
-        'description' => t("The list id.")
-        ),
-      'lstatus'     => array(
+        'description' => t('The list id.'),
+      ),
+      'lstatus' => array(
         'type' => 'int',
         'disp-width' => 10,
         'not null' => TRUE,
         'default' => 0,
-        'description' => t("The list lid.")
-        ),
+        'description' => t('Subscription status.'),
+      ),
       'lmail' => array(
         'type' => 'varchar',
         'length' => 36,
         'not null' => TRUE,
         'default' => '',
         'description' => t('Current user subscribed email.'),
-        ),
+      ),
       'lpass' => array(
         'type' => 'varchar',
         'length' => 36,
         'not null' => TRUE,
         'default' => '',
         'description' => t('Current user list password.'),
-        ),
       ),
+    ),
     'primary key' => array('uid', 'lid'),
-    );
+  );
 
   return $schema;
 }
 
 function mailman_manager_update_6001() {
-  $ret = array();
   // Rebuild menu cache
   module_rebuild_cache();
-  return $ret;
+  return array();
 }
