Index: listhandler.module
===================================================================
RCS file: /cvs/drupal-contrib/contributions/modules/listhandler/listhandler.module,v
retrieving revision 1.85
diff -u -r1.85 listhandler.module
--- listhandler.module	27 Oct 2008 20:09:29 -0000	1.85
+++ listhandler.module	31 May 2009 20:54:17 -0000
@@ -6,6 +6,9 @@
  * Listhandler main module.
  */
 
+define('ERR_LISTHANDLER_NO_HOOK_AVAILABLE', -99);
+define('ERR_LISTHANDLER_WRONG_HOOK', -98);
+
 /**
  * To do:
  *   Does attachment handling screw up druapl_html_to_text or vise-versa?
@@ -347,11 +350,6 @@
     $to = $mbox[1];
     $edit["tid"] = $mbox[2];
 
-    // The below is required for EZMLM
-    // It sets the Return Path to the Listhandler subscribed email address (rather than the webserver)
-    // so that EZMLM (and everybody else) knows that we sent it
-    $mid['Return-Path'] = $edit['mail'];
-
     // We add tokens to the MID. Replies to comments sent by listhandler can thus be classified easier.
     $msgid = "<listhandler=". $edit["mid"] ."&site=". $dir . $_SERVER["SERVER_NAME"] ."&nid=". $edit["nid"] ."&pid=". $edit["pid"] ."&cid=".  $edit["cid"] ."&uid=". $user->uid ."&tid=".  $edit["tid"] ."&". md5($edit["nid"] . $edit["cid"]) ."@". $dir . strtolower($_SERVER["SERVER_NAME"]) .">";
     $mid['Message-Id'] = $msgid;
@@ -391,6 +389,40 @@
       }
     }
 
+    //Before we send the email determine if user is part of the mailing list or not
+    $exist = listhandler_extend('listhandler_user', 'exists', $user, $to);
+    if (is_bool($exist))
+    {
+      if ($exist == true)
+      {
+        //if user exists, change nothing and send email as planned.
+      }
+      else
+      {
+        watchdog('listhandler', "User '%user' not member of mailing list '%mailinglist'.", array("%user" => $user->name, "%mailinglist" => $to), WATCHDOG_NOTICE);
+        //if user is not in the email list, send email with the anonymous account.
+        $edit['name'] = variable_get('anonymous', 'Anonymous');
+        $edit['mail'] = variable_get('listhandler_from', '');
+      }
+    }
+    else
+    { //value is not boolean. It must be a negative numeric value (error) or ERR_LISTHANDLER_NO_HOOK_AVAILABLE 
+      if ($exist == ERR_LISTHANDLER_NO_HOOK_AVAILABLE)
+      {
+        //do nothing. No hooks available that change the result. Which is ok.
+      }
+      else
+      {
+        watchdog('listhandler', "User '%user' not found in mailing list '%mailinglist'.", array("%user" => $user->name, "%mailinglist" => $to), WATCHDOG_NOTICE);
+      }
+    } 
+ 
+    // The below is required for EZMLM
+    // It sets the Return Path to the Listhandler subscribed email address (rather than the webserver)
+    // so that EZMLM (and everybody else) knows that we sent it
+    $mid['Return-Path'] = $edit['mail'];
+
+
     // send email
     $params = array(
       'body' => $body,
@@ -673,3 +705,34 @@
   $tests = file_scan_directory($dir, '\.test$');
   return array_keys($tests);
 }
+
+
+/**
+* run the listhandler_extend hook, returns specified things depending on the operation
+* @param $hook_name is the name of the hook to call, eg "listhandler_user"
+* @param $op is the action that a module should run, eg "exists"
+* @param $data is listhandler data
+* @param $mailinglist string. Is the mailing list we want to check for
+* @return a return code depending on $hook_name and $op.
+, where true or false is a successfull call, and a numeric value < 0 indicates some sort of error.
+*
+* In general there can be potentially many hooks implemented, but only one hook should be 
+* available to handle mailing list $mailinglist.
+
+* Available usecases:
+* -listhandler_extend("listhandler_user", "exists", $user, $mailinglist) 
+* where user is the user objects we want to check.
+* returnvalues of hook: either boolean true or false, ERR_LISTHANDLER_WRONG_HOOK,
+* and any numeric value < 0 indicates some sort of error.
+*
+*/
+function listhandler_extend($hook_name, $op = null, $data, $mailinglist) {
+
+  foreach (module_implements($hook_name) as $module) {
+    $result = module_invoke($module, $hook_name, $op, $data, $mailinglist);
+    if($result != ERR_LISTHANDLER_WRONG_HOOK)
+      return $result;
+  }
+  // at this point we did not find any hook that matched the mailing list.
+  return ERR_LISTHANDLER_NO_HOOK_AVAILABLE;
+}
