--- troll.module	2009-08-03 11:47:19.000000000 -0700
+++ troll.module.patched	2010-04-12 18:32:22.000000000 -0700
@@ -16,86 +16,13 @@
  * Implementation of hook_boot().
  */
 function troll_boot() {
-  $ip = ip_address();
-  if (troll_is_blacklisted()) {
-    $alt_page = variable_get('troll_blacklist_alt_page', 0);
-    if ($alt_page) {
-      switch ($alt_page) {
-        case 'blank':
-          watchdog('troll', 'Alternate blank page displayed to @ip', array('@ip' => $ip), WATCHDOG_INFO);
-          exit;
-          break;
-        case '404':
-          // drupal_not_found() can make a mess in admin logs with watchdog()
-          drupal_set_header('HTTP/1.0 404 Not Found');
-          drupal_set_title(t('Page not found'));
-          watchdog('troll', 'Alternate 404 page displayed to @ip', array('@ip' => $ip), WATCHDOG_INFO);
-          print theme('page', '');
-          exit;
-          break;
-        case 'redirect':
-          // The default value of troll_blacklist_alt_url should not be an
-          // empty string because then we redirect to ourselves; so use 127.0.0.1.
-          $url = variable_get('troll_blacklist_alt_url', 'http://127.0.0.1');
-          watchdog('troll', '@ip redirected to @url', array('@ip' => $ip, '@url' => $url), WATCHDOG_INFO);
-          header('Location: '. $url);
-          exit;
-          break;
-      }
-    }
-
-    $req_mod = variable_get('troll_blacklist_mod_requests', 0);
-    if ($req_mod == 'notice_post_drop' && !empty($_POST)) {
-      $_POST = array();
-      include_once('includes/common.inc');
-      drupal_set_message(t('Your data submission was ignored because you are visiting from a blacklisted location.'), 'warning');
-      watchdog('troll', 'Data submission ignored with notification to user from @ip', array('@ip' => $ip), WATCHDOG_DEBUG);
-    }
-    elseif ($req_mod == 'silent_post_drop') {
-      $_POST = array();
-      watchdog('troll', 'Data submission ignored silently from @ip', array('@ip' => $ip), WATCHDOG_DEBUG);
-    }
-
-    if (variable_get('troll_blacklist_stutter', 0)) {
-      sleep(rand(1, 5));
-      watchdog('troll', 'Page load stuttered from @ip', array('@ip' => $ip), WATCHDOG_DEBUG);
-    }
-  }
-
-  global $user;
-
-  if ($user->uid) {
-    $track = db_result(db_query("SELECT COUNT(ip_address) FROM {troll_ip_track} WHERE uid = %d AND ip_address = '%s'", $user->uid, $ip));
-    if (!empty($track)) {
-      // A record for this IP exists. Update accessed timestamp.
-      db_query("UPDATE {troll_ip_track} SET accessed = %d WHERE uid = %d AND ip_address = '%s'", time(), $user->uid, $ip);
-    }
-    else {
-      // Insert new IP record for user.
-      db_query("INSERT INTO {troll_ip_track} (uid, ip_address, created, accessed) VALUES (%d, '%s', %d, %d)", $user->uid, $ip, time(), time());
-    }
-  }
-
-  if (variable_get('troll_enable_ip_ban', 1)) {
-    $domain = db_result(db_query_range("SELECT domain_name FROM {troll_ip_ban} WHERE (expires > %d OR expires = 0) AND ip_address = '%s'", time(), $ip, 0, 1));
-    if ($domain !== FALSE) {
-      global $base_url;
-      watchdog('troll', 'IP Ban: @addr Domain: @domain', array('@addr' => $ip, '@domain' => $domain), WATCHDOG_INFO);
-      $troll_ip_ban_redirect = variable_get('troll_ip_ban_redirect', '');
-      if (empty($troll_ip_ban_redirect)) {
-        include_once('includes/common.inc');
-        $page = drupal_get_path('module', 'troll') .'/blocked.html';
-      }
-      else {
-        $page = $troll_ip_ban_redirect;
-      }
-      header('Location: '. $base_url .'/'. $page);
-      exit();
-    }
-  }
+  _troll_handle_request();
 }
 
 function troll_init() {
+  
+  _troll_handle_request();
+  
   if (variable_get('troll_enable_phpids', 0)) {
     // drupal_get_path does not exist at this point in bootstrapping.
     $phpids_dir = dirname(drupal_get_filename('module', 'troll')) .'/php-ids/lib/';
@@ -587,3 +514,92 @@ function _troll_dnsbl_default_servers() 
     'sbl-xbl.spamhaus.org'
   )));
 }
+
+/**
+ * Performs troll check and modifies response behavior as necessary
+ */ 
+function _troll_handle_request() {
+  static $request_handled = FALSE;
+  
+  if (!$request_handled) {
+    $ip = ip_address();
+    if (troll_is_blacklisted()) {
+      $alt_page = variable_get('troll_blacklist_alt_page', 0);
+      if ($alt_page) {
+        switch ($alt_page) {
+          case 'blank':
+            watchdog('troll', 'Alternate blank page displayed to @ip', array('@ip' => $ip), WATCHDOG_INFO);
+            exit;
+            break;
+          case '404':
+            // drupal_not_found() can make a mess in admin logs with watchdog()
+            drupal_set_header('HTTP/1.0 404 Not Found');
+            drupal_set_title(t('Page not found'));
+            watchdog('troll', 'Alternate 404 page displayed to @ip', array('@ip' => $ip), WATCHDOG_INFO);
+            print theme('page', '');
+            exit;
+            break;
+          case 'redirect':
+            // The default value of troll_blacklist_alt_url should not be an
+            // empty string because then we redirect to ourselves; so use 127.0.0.1.
+            $url = variable_get('troll_blacklist_alt_url', 'http://127.0.0.1');
+            watchdog('troll', '@ip redirected to @url', array('@ip' => $ip, '@url' => $url), WATCHDOG_INFO);
+            header('Location: '. $url);
+            exit;
+            break;
+        }
+      }
+
+      $req_mod = variable_get('troll_blacklist_mod_requests', 0);
+      if ($req_mod == 'notice_post_drop' && !empty($_POST)) {
+        $_POST = array();
+        include_once('includes/common.inc');
+        drupal_set_message(t('Your data submission was ignored because you are visiting from a blacklisted location.'), 'warning');
+        watchdog('troll', 'Data submission ignored with notification to user from @ip', array('@ip' => $ip), WATCHDOG_DEBUG);
+      }
+      elseif ($req_mod == 'silent_post_drop') {
+        $_POST = array();
+        watchdog('troll', 'Data submission ignored silently from @ip', array('@ip' => $ip), WATCHDOG_DEBUG);
+      }
+
+      if (variable_get('troll_blacklist_stutter', 0)) {
+        sleep(rand(1, 5));
+        watchdog('troll', 'Page load stuttered from @ip', array('@ip' => $ip), WATCHDOG_DEBUG);
+      }
+    }
+
+    global $user;
+
+    if ($user->uid) {
+      $track = db_result(db_query("SELECT COUNT(ip_address) FROM {troll_ip_track} WHERE uid = %d AND ip_address = '%s'", $user->uid, $ip));
+      if (!empty($track)) {
+        // A record for this IP exists. Update accessed timestamp.
+        db_query("UPDATE {troll_ip_track} SET accessed = %d WHERE uid = %d AND ip_address = '%s'", time(), $user->uid, $ip);
+      }
+      else {
+        // Insert new IP record for user.
+        db_query("INSERT INTO {troll_ip_track} (uid, ip_address, created, accessed) VALUES (%d, '%s', %d, %d)", $user->uid, $ip, time(), time());
+      }
+    }
+
+    if (variable_get('troll_enable_ip_ban', 1)) {
+      $domain = db_result(db_query_range("SELECT domain_name FROM {troll_ip_ban} WHERE (expires > %d OR expires = 0) AND ip_address = '%s'", time(), $ip, 0, 1));
+      if ($domain !== FALSE) {
+        global $base_url;
+        watchdog('troll', 'IP Ban: @addr Domain: @domain', array('@addr' => $ip, '@domain' => $domain), WATCHDOG_INFO);
+        $troll_ip_ban_redirect = variable_get('troll_ip_ban_redirect', '');
+        if (empty($troll_ip_ban_redirect)) {
+          include_once('includes/common.inc');
+          $page = drupal_get_path('module', 'troll') .'/blocked.html';
+        }
+        else {
+          $page = $troll_ip_ban_redirect;
+        }
+        header('Location: '. $base_url .'/'. $page);
+        exit();
+      }
+    }
+    
+    $request_handled = TRUE;
+  }
+}
