--- simplenews_roles.module	2008-04-13 14:33:17.000000000 +0200
+++ simplenews_roles.module.new	2009-07-14 06:05:31.000000000 +0200
@@ -2,6 +2,13 @@
 // $Id: simplenews_roles.module,v 1.1.2.3 2007/10/18 04:40:31 robroy Exp $
 
 /**
+ * Implementation of hook_init().
+ */
+function simplenews_roles_init() {
+  module_load_include('module', 'simplenews', 'simplenews');
+}
+
+/**
  * Implementation of hook_cron().
  */
 function simplenews_roles_cron() {
@@ -77,11 +84,7 @@
 }
 
 function simplenews_roles_theme() {
-	return array(
-		'simplenews_newsletter_footer' => array(
-			'arguments' => array('format' => NULL, 'hash' => NULL, 'test' => NULL, 'langcode' => NULL),
-		),
-	);
+	return array('simplenews_roles_newsletter_footer' => array('arguments' => array('format' => NULL, 'hash' => NULL, 'test' => NULL, 'langcode' => NULL)));
 }
 
 // It is pointless to have an unsubscribe link for node based emails
@@ -104,14 +107,20 @@
   switch ($op) {
     case 'after_update':
     case 'insert':
-      $newroles = array_keys($edit['roles']);
+      $newroles = array();
+      // this array is not always set for all operations, hence the check
+      if ( is_array($edit['roles']) )
+        $newroles = array_keys($edit['roles']);
       $oldroles = isset($account->roles) ? array_keys($account->roles) : array();
       $roles = array_unique(array_merge($newroles, $oldroles, array(DRUPAL_AUTHENTICATED_RID)));
-      
+     
       foreach(variable_get('simplenews_roles_tids_rids', array()) as $tid => $rids) {
-      	if(count(array_intersect($rids, $roles)) > 0) {
+      	if(count(array_intersect($rids, $roles)) > 0)
+          // if there are "new" roles for the user for this newsletter, then sync the user
           simplenews_roles_update_subscriptions($tid, $rids);
-        }
+        else
+         // if the user has no role that matches the current subscription, unsubscribe
+         simplenews_unsubscribe_user($account->mail, $tid, FALSE);
       }
       break;
   }
