--- mailman_groups.module	2009-09-23 13:05:51.000000000 -0400
+++ /var/www/afceanova/drupal/sites/all/modules/mailman_groups/mailman_groups.module	2010-08-19 21:51:07.000000000 -0400
@@ -57,8 +57,10 @@ function _mailman_groups_getlists() {
 function _mailman_groups_getsynclists($gid) {
   $lists = array();
 
-  $tname = 'mailman_groups';
-  $result = db_query("SELECT lid FROM {$tname} WHERE gid = $gid AND sync");
+  //$tname = 'mailman_groups';
+  //$result = db_query("SELECT lid FROM {$tname} WHERE gid = $gid AND sync");
+  
+  $result = db_query("SELECT lid FROM {mailman_groups} WHERE gid = $gid AND sync");
 
   while ($link = db_fetch_array($result)) {
     $lists[] = $link['lid'];
@@ -281,44 +283,69 @@ function _mailman_groups_alter_mailman_m
 }
 
 /**
- * Trap the event of a user joining a Group, and subscribe to all linked
+ * Trap the event of a user joining or leaving a Group, and subscribe/unsubscribe to all linked
  * Mailing Lists which have "sync" set.
  *
- * NB: Subscribe is done as 'digest' - to do: make configurable.
- *
- * @todo Investigate and consider using OG Hooks or "Actions".
  */
-function _mailman_groups_alter_og_confirm_subscribe(&$form, $form_state) {
-  // If the form is being posted, do our update.
-  $post = $form_state['post'];
-  if ($post['confirm']) {
-    // Get gid from the form and array of "sync" Lists for this gid.
-    $gid = $form['gid']['#value'];
-    $lists = _mailman_groups_getsynclists($gid);
-
-    // Loop through lists.
-    foreach ($lists as $n => $lid) {
-      global $user;
-      // Need to get the corresponding list object and convert to array.
-      if (!_mailman_groups_checkfunc('mailman_manager_get_list')) {
-        return;
-      }
-      $list = get_object_vars(mailman_manager_get_list($lid));
-      $msg = sprintf('Subscribing your email address %s to Group Mailing List <em>%s</em>.', $user->mail, $list['name']);
-      drupal_set_message($msg);
-
-      // Do the subscribe via User Mailman Register if available, otherwise use Mailman Manager function.
-      if (is_callable('_user_mailman_register_subscribe_update')) {
-        _user_mailman_register_subscribe_update($user, $list, 3, 0, $user->mail, NULL);
+ 
+function mailman_groups_og($op, $gid, $uid, $args) {
+  $account = user_load($uid);
+  switch ($op) {
+    case 'user insert': 
+    // Fall through to next case
+    case 'user update':
+      $lists = _mailman_groups_getsynclists($gid);
+      foreach ($lists as $n => $lid) {
+        // Need to get the corresponding list object and convert to array.
+        if (!_mailman_groups_checkfunc('mailman_manager_get_list')) {
+          return;
+        }
+        $list = get_object_vars(mailman_manager_get_list($lid));
+        $msg = t('Subscribing %mail to Group Mailing List %listname.', array(
+          '%mail' => $account->mail,
+          '%listname' => $list['name'],
+          ));
+        drupal_set_message($msg);
+  
+        // Do the subscribe via User Mailman Register if available, otherwise use Mailman Manager function.
+        if (is_callable('_user_mailman_register_subscribe_update')) {
+          _user_mailman_register_subscribe_update($account, $list, 3, 0, $account->mail, NULL);
+        }
+        else {
+          if (!_mailman_groups_checkfunc('_mailman_manager_subscribe')) {
+            return;
+          }
+          _mailman_manager_subscribe($account->uid, $lid, $account->mail, 'nodigest', '');
+        }
       }
-      else {
-        if (!_mailman_groups_checkfunc('_mailman_manager_subscribe')) {
+    break;
+    case 'user delete':
+      $lists = _mailman_groups_getsynclists($gid);
+      foreach ($lists as $n => $lid) {
+        // Need to get the corresponding list object and convert to array.
+        if (!_mailman_groups_checkfunc('mailman_manager_get_list')) {
           return;
         }
-        _mailman_manager_subscribe($user, $lid, $user->mail, 'nodigest', '');
+        $list = get_object_vars(mailman_manager_get_list($lid));      
+        // mailman_manager doesn't have an API function for unsubscribing. 
+        // So, we need to pull some data directly from the db
+        $query = 'SELECT * FROM {mailman_users} WHERE uid = %d AND lid = %d';
+        $result = db_query($query, $account->uid, $lid);
+        $subscrip = db_fetch_object($result);
+        if (isset($subscrip->lpass)) {
+        _mailman_manager_update_subscriptions($uid, $lid, 0, 0, $account->mail, $subscrip->lpass);
+        $msg = t('Removing %mail from Group Mailing List %listname.', array(
+          '%mail' => $subscrip->lmail,
+          '%listname' => $list['name'],
+          ));
+        drupal_set_message($msg);
+        }        
       }
-    }
-  }
+
+      
+    default:
+    // Do Nothing      
+  }  
 }
 
 /**
