--- comment_notify.module.orig	2009-03-08 00:52:51.000000000 +0530
+++ comment_notify.module	2009-03-28 16:52:31.000000000 +0530
@@ -418,77 +418,93 @@
     drupal_mail('comment_notify', 'comment_notify_mail', $author->mail, $language, $message);
     $sent_to[] = $author->mail;
   }
+  $thread = db_result(db_query("SELECT thread FROM {comments} WHERE cid = %d", $cid));
+  $parent_thread_ids = array();
+  $parent_thread_ids[] = $nid;
+  $format_string = null;
+  $thread_arr = explode('.', $thread);
+  // $thread_arr contains the exploded thread value. 
+  // Get values from the string thread, 3 at a time and replace the third value(.) from substring 
+  // and then attach / at the end. keep the value in the array $parent_thread_ids[].
+  // Format string is formed according to the number of thread id's.
+  for($i = count($thread_arr) - 1, $j = 3; $i > 0; $i--, $j+=3)
+  {
+    $thread_id = mb_substr($thread, 0, $j);
+    $thread_id = substr_replace($thread_id, "", -1, 1 ) ."/";
+    $parent_thread_ids[] = $thread_id;
+    $format_string .= "'%s',";
+  }
+  // Replace the last comma from $format_string
+  $format_string = substr_replace($format_string, "", -1, 1 );
 
-  //Get the list of commenters to notify
-  $result = db_query("SELECT DISTINCT c.cid, c.uid, c.name, c.nid, c.mail AS cmail, u.mail AS umail, u.init AS uinit, c.uid, c.name, cn.notify, cn.notify_hash
-    FROM {comments} c INNER JOIN {comment_notify} cn on c.cid = cn.cid LEFT OUTER JOIN {users} u ON c.uid = u.uid
-    WHERE nid = %d AND cn.notify > 0 AND c.status = 0 AND (u.status = 1 OR u.uid = 0)", $nid
-  );
-  // TODO? the original big query had stuff making sure the mail was populated and contained .+@.+ Perhaps check for that here and set notify = 0 if that is the case for this cid
-
-  while ($alert = db_fetch_object($result)) {
-    $umail = empty($alert->umail) ? $alert->uinit : $alert->umail;
-    $mail = empty($alert->cmail) ? $umail : $alert->cmail;
-
-    if ($alert->notify == COMMENT_NOTIFY_COMMENT && $alert->cid != $comment->pid) {
-      continue;
-    }
-    if ($mail != $comment_mail && !in_array($mail, $sent_to) && $alert->uid != $comment->uid) {
-      $message = array();
-      if (!empty($alert->uid)) {
-        $recipient_user = user_load(array('uid' => $alert->uid));
-        $language = user_preferred_language($recipient_user);
-      }
-      else {
-        $language = language_default();
+  if(!empty($format_string)) {
+    // Get the list of commenters to notify
+    $result = db_query("SELECT DISTINCT c.cid, c.uid, c.name, c.nid, c.mail AS cmail, u.mail AS umail, u.init AS uinit, c.uid, c.name, cn.notify,   cn.notify_hash FROM {comments} c INNER JOIN {comment_notify} cn ON c.cid = cn.cid LEFT OUTER JOIN {users} u ON c.uid = u.uid WHERE nid = %d  AND c.status = 0 AND (u.status = 1 OR u.uid = 0) AND (c.thread IN (".$format_string.") OR cn.notify = 1) ", $parent_thread_ids);
+    // TODO? the original big query had stuff making sure the mail was populated and contained .+@.+ Perhaps check for that here and set notify = 0 if that is the case for this cid
+
+    while ($alert = db_fetch_object($result)) {
+      $umail = empty($alert->umail) ? $alert->uinit : $alert->umail;
+      $mail = empty($alert->cmail) ? $umail : $alert->cmail;
+      if ($alert->notify == COMMENT_NOTIFY_COMMENT && $alert->cid != $comment->pid) {
+        continue;
       }
+      if ($mail != $comment_mail && !in_array($mail, $sent_to) && $alert->uid != $comment->uid) {
+        $message = array();
+        if (!empty($alert->uid)) {
+          $recipient_user = user_load(array('uid' => $alert->uid));
+          $language = user_preferred_language($recipient_user);
+        }
+        else {
+          $language = language_default();
+        }
+
+        $message['subject'] = t('!site :: new comment for your post.', array('!site' => variable_get('site_name', 'drupal')));
+        $message['body'] = t(
+          variable_get('comment_notify_default_mailtext', DEFAULT_MAILTEXT),
+          array(
+            '!commname' => $comment->name,
+            '!commtext' => $comment->comment,
+            '!commsubj' => $comment->subject,
+            '!comment_url' => url('node/'. $nid, array('absolute' => TRUE, 'fragment' => 'comment-'. $cid)),
+            '!node_title' =>  $node->title,
+            '!node_teaser' => $node->teaser,
+            '!mission' => variable_get('site_mission', ''),
+            '!node_body' =>  $node->body,
+            '!name' => $alert->name,
+            '!site' => variable_get('site_name', 'drupal'),
+            '!uri' => $base_url,
+            '!uri_brief' => preg_replace('!^https?://!', '', $base_url),
+            '!date' => format_date(time()),
+            '!login_uri' => url('user', array('absolute' => TRUE)),
+            '!edit_uri' => url('user/'. $alert->uid .'/edit', array('absolute' => TRUE)),
+            '!link1' => url('comment_notify/disable/'. $alert->notify_hash, array('absolute' => TRUE))
+          )
+        );      
+        drupal_mail('comment_notify', 'comment_notify_mail', $mail, $language, $message);
+        $sent_to[] = $mail;
+
+        // Make the mail link to user's /edit, unless it's an anonymous user.
+        if ($alert->uid != 0) {
+          $user_mail = l($mail, 'user/'. $alert->uid .'/edit');
+        }
+        else {
+          $user_mail = check_plain($mail);
+        }
+
+        // Add an entry to the watchdog log.
+        watchdog(
+          'comment_notify',
+          'Notified: !user_mail',
+          array('!user_mail' => $user_mail),
+          WATCHDOG_NOTICE,
+          l(t('source comment'), 'node/'. $nid, array(
+            'fragment' => 'comment-'. $alert->cid,
+          ))
+        );
 
-      $message['subject'] = t('!site :: new comment for your post.', array('!site' => variable_get('site_name', 'drupal')));
-      $message['body'] = t(
-        variable_get('comment_notify_default_mailtext', DEFAULT_MAILTEXT),
-        array(
-          '!commname' => $comment->name,
-          '!commtext' => $comment->comment,
-          '!commsubj' => $comment->subject,
-          '!comment_url' => url('node/'. $nid, array('absolute' => TRUE, 'fragment' => 'comment-'. $cid)),
-          '!node_title' =>  $node->title,
-          '!node_teaser' => $node->teaser,
-          '!mission' => variable_get('site_mission', ''),
-          '!node_body' =>  $node->body,
-          '!name' => $alert->name,
-          '!site' => variable_get('site_name', 'drupal'),
-          '!uri' => $base_url,
-          '!uri_brief' => preg_replace('!^https?://!', '', $base_url),
-          '!date' => format_date(time()),
-          '!login_uri' => url('user', array('absolute' => TRUE)),
-          '!edit_uri' => url('user/'. $alert->uid .'/edit', array('absolute' => TRUE)),
-          '!link1' => url('comment_notify/disable/'. $alert->notify_hash, array('absolute' => TRUE))
-        )
-      );
-      drupal_mail('comment_notify', 'comment_notify_mail', $mail, $language, $message);
-      $sent_to[] = $mail;
-
-      // Make the mail link to user's /edit, unless it's an anonymous user.
-      if ($alert->uid != 0) {
-        $user_mail = l($mail, 'user/'. $alert->uid .'/edit');
-      }
-      else {
-        $user_mail = check_plain($mail);
+        // revert to previous (site default) locale
+        $language = $initial_language;
       }
-
-      // Add an entry to the watchdog log.
-      watchdog(
-        'comment_notify',
-        'Notified: !user_mail',
-        array('!user_mail' => $user_mail),
-        WATCHDOG_NOTICE,
-        l(t('source comment'), 'node/'. $nid, array(
-          'fragment' => 'comment-'. $alert->cid,
-        ))
-      );
-
-      // revert to previous (site default) locale
-      $language = $initial_language;
     }
   }
   // Record that a notification was sent for this comment so that
