--- comment_notify.module.orig	2009-03-05 15:49:58.000000000 +0530
+++ comment_notify.module	2009-03-05 15:50:17.000000000 +0530
@@ -373,70 +373,85 @@
     $sent_to[] = $author->mail;
   }
  
-  //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;
+  $thread = db_result(db_query("SELECT thread FROM {comments} WHERE cid = %d", $cid));
 
-    if ($alert->notify == COMMENT_NOTIFY_COMMENT && $alert->cid != $comment->pid) {
-      break;
-    }
-
-    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)) .'#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' => drupal_substr($base_url, drupal_strlen('http://')),
-          '!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;
+  $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 coma from $format_string
+  $format_string = substr_replace($format_string, "", -1, 1 );
 
-      if ($alert->uid != 0) {
-        $watchdog_message = 'Notified: <a href="!url">@user_mail</a>';
-      }
-      else {
-        $watchdog_message = 'Notified @user_mail';
+  if(!empty($parent_thread_ids)) {
+    // 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->cid != $comment->pid && $mail !=$author->mail) {
+        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;
+
+          if ($alert->uid != 0) {
+            $watchdog_message = 'Notified: <a href="!url">@user_mail</a>';
+          }
+          else {
+            $watchdog_message = 'Notified @user_mail';
+          }
+
+          // Add an entry to the watchdog log.
+          watchdog('comment_notify', $watchdog_message, array('!url' => url('user/'. $alert->uid .'/edit'), '@user_mail' => $mail),
+            WATCHDOG_NOTICE, l(t('source comment'), 'node/'. $nid, array('fragment' => 'comment-'. $alert->cid)));
+
+          // revert to previous (site default) locale
+          $language = $initial_language;
+        }
       }
-
-      watchdog('comment_notify', $watchdog_message, array('!url' => url('user/'. $alert->uid .'/edit'), '@user_mail' => $mail),
-        WATCHDOG_NOTICE, l(t('source comment'), 'node/'. $nid, array('fragment' => 'comment-'. $alert->cid)));
-
-      // Add an entry to the watchdog log.
-
-      // revert to previous (site default) locale
-      $language = $initial_language;
     }
   }
 }
