Index: sites/ondertussen.nl/modules/contrib/emf/plugins/emf_mailchimp/emf_mailchimp.api.inc
===================================================================
--- sites/ondertussen.nl/modules/contrib/emf/plugins/emf_mailchimp/emf_mailchimp.api.inc	(revision )
+++ sites/ondertussen.nl/modules/contrib/emf/plugins/emf_mailchimp/emf_mailchimp.api.inc	(revision )
@@ -22,12 +22,25 @@
  * @return
  *   Boolean; TRUE if user is subscribed. FALSE if not.
  */
-function emf_mailchimp_api_subscribe($email, $fields, $lid) {
+function emf_mailchimp_api_subscribe($email, $fields, $lid)
+{
   // mailchimp subscribe can't handle blank merge_var arrays
   if (empty($fields)) {
     $fields = array('');
   }
 
+  // Check if the mail is on one of the lists first
+  $lists = _emf_mailchimp_api_lists_for_email($email);
+
+  if (!is_array($lists)) {
+    // An error occured; keep the request
+    return FALSE;
+  }
+  elseif (in_array($lid, $lists)) {
+    // This user is already a member of this list. Remove the request.
+    return TRUE;
+  }
+
   // do api call
   $result = _emf_mailchimp_api_call('listSubscribe', array($lid, $email, $fields, 'html', FALSE, TRUE));
   if (!$result) return FALSE;
@@ -45,9 +58,29 @@
  * @return
  *   Boolean; TRUE if user is subscribed. FALSE if not.
  */
-function emf_mailchimp_api_unsubscribe($email, $lid) {
+function emf_mailchimp_api_unsubscribe($email, $lid)
+{
+  // Check if the mail is on one of the lists first
+  $lists = _emf_mailchimp_api_lists_for_email($email);
+
+  if (is_array($lists) && (!count($lists))) {
+    // User is not on a list, so remove this request (the unsubscribes must have crossed)
+    return TRUE;
+  }
+  elseif (!$lists) {
+    // An error occured
+    return FALSE;
+  }
+  elseif (!in_array($lid, $lists)) {
+    // Not a member of this list
+    return FALSE;
+  }
+
+  $defaultdelete = FALSE; // Completely delete the member from your list instead of just unsubscribing
+  $sendgoodbye = FALSE; // Need to send a goodbye mail ?
+  
   // do api call
-  $result = _emf_mailchimp_api_call('listUnsubscribe', array($lid, $email));
+  $result = _emf_mailchimp_api_call('listUnsubscribe', array($lid, $email, $defaultdelete, $sendgoodbye));
 
   if (!$result) return FALSE;
 
@@ -249,4 +282,47 @@
     return date('Y-m-d H:i:s', $timestamp);
   }
   return 0;
-}
\ No newline at end of file
+}
+
+
+/**
+ * Retrieve all List Ids a member is subscribed to.
+ *
+ * @param $email
+ *   String; The email-address to check and retreive the lists for
+ * @return
+ *   Array; Result array.
+ */
+function _emf_mailchimp_api_lists_for_email($email){
+   // fetching api key
+  $api_key = variable_get('emf_mailchimp_api_key', '');
+
+  // if no api key is specified, return false
+  if (empty($api_key)) return FALSE;
+
+  // do api call
+  $mc = new MCAPI($api_key);
+  $mc->setTimeout(10);
+
+  $params = array();
+  $params["email_address"] = $email;
+  $result =  $mc->callServer("listsForEmail", $params);
+
+  // if api result code is not 'ok', return false and write log
+  if ($mc->errorCode) {
+    // If the user is not on the list, return an empty array
+    if (($mc->errorCode == 232) ||
+        ($mc->errorCode == 233) ||
+        ($mc->errorCode == 230) ||
+        ($mc->errorCode == 231) ||
+        ($mc->errorCode == 214) ||
+        ($mc->errorCode == 215)) return array();
+
+    watchdog('emf_mailchimp', 'Code - '. $mc->errorCode .', Message - '. $mc->errorMessage, array(), WATCHDOG_ERROR);
+    return FALSE;
+  }
+
+  if (!$result) return array();
+
+  return $result;
+}
