--- mailman_groups.module-6.x-1.1	2010-01-07 13:20:12.000000000 +1000
+++ mailman_groups.module	2010-01-07 13:23:04.000000000 +1000
@@ -322,6 +322,52 @@
 }
 
 /**
+ * Trap the event of a user leaving a Group, and unsubscribe to all linked
+ * Mailing Lists which have "sync" set.
+ *
+ * NB: no check done yet for lists that are linked to multiple groups.
+ *
+ * @todo Investigate and consider using OG Hooks or "Actions".
+ */
+function _mailman_groups_alter_og_confirm_unsubscribe(&$form, $form_state) {
+
+  // If the form is being posted, do our update.
+  $post = $form_state['post'];
+  if ($post['confirm']) {
+    // Get group_node from the form and array of "sync" Lists for this gid.
+    $group_node = $form['group_node']['#value'];
+    $gid = $group_node->nid;
+    $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('Unsubscribing your email address %s from Group Mailing List <em>%s</em>.', $user->mail, $list['name']);
+      drupal_set_message($msg);
+
+      // Do the unsubscribe via User Mailman Register if available, otherwise use Mailman Manager function.
+      if (is_callable('_user_mailman_register_subscribe_update')) {
+        $oldstatus = db_result(db_query ('SELECT lstatus FROM {mailman_users} WHERE uid = %d AND lid = %d', $user->uid, $lid));
+        _user_mailman_register_subscribe_update($user, $list, array('new_status' => -1, 'old_status' => $oldstatus), $user->mail, 0, NULL);
+      }
+      else {
+        if (!_mailman_groups_checkfunc('_mailman_manager_update_subscriptions')) {
+          return;
+        }
+        $oldstatus = db_result(db_query ('SELECT lstatus FROM {mailman_users} WHERE uid = %d AND lid = %d', $user->uid, $lid));
+	_mailman_manager_update_subscriptions($user, $lid, -1, $oldstatus, $user->mail, '');
+      }
+    }
+  }
+}
+
+
+/**
  * Check if current user is a member of the Group linked to list $lid.
  *
  * Because this may get called repeatedly for many lists, we cache the result
@@ -422,7 +468,7 @@
  */
 function mailman_groups_form_alter(&$form, $form_state, $form_id) {
   // Define an array of form_ids for forms we want to modify.
-  $forms = array('user_profile_form', 'user_register', 'mailman_manager_admin_form', 'og_confirm_subscribe');
+  $forms = array('user_profile_form', 'user_register', 'mailman_manager_admin_form', 'og_confirm_subscribe', 'og_confirm_unsubscribe');
   // If this is not one of the forms we want to process, quick exit.
   if (!in_array($form_id, $forms)) {
     return;

