--- comment_notify.module	2007-11-03 12:39:02.000000000 +0000
+++ comment_notify.module.new	2008-01-06 22:25:45.000000000 +0000
@@ -126,27 +126,12 @@
 
   $op = isset($_POST['op']) ? $_POST['op'] : '';
 
-  /*
-      if ( $user->comment_notify_mailalert ) {
-  
-      }
-  */
-  
-
   if ($op == t('Preview comment')) {
       drupal_set_message(
         t('ATTENTION: your comment is NOT YET posted - please click the post button to confirm your post' )
         );
 
 
-/*      $form['submitextra']['privatemsg_allow'] = array(
-          '#type' => 'checkbox',
-          '#title' => t('Allow private messages'),
-          '#default_value' => isset($edit['privatemsg_allow']) ? $edit['privatemsg_allow'] : 1,
-          '#description' => t('Check this box to allow users to send you private messages.')
-          );
-*/
-    #$form['submitextra']['preview'] = array('#type' => 'button', '#value' => t('Preview comment'), '#weight' => -19);
   
     //extra submit button on top
     if (!form_get_errors() && ((variable_get('comment_preview', COMMENT_PREVIEW_REQUIRED) == COMMENT_PREVIEW_OPTIONAL) || ($op == t('Preview comment')) || ($op == t('Post comment')))) {
@@ -164,21 +149,13 @@
   }
 
     
-#  drupal_set_message(' user:'. $user->uid .' setting: '.$user->comment_notify_mailalert );
-#  print_r($user);
-
- //print_r($form);
- 
-
-
   if ( ($user->uid == 0) || ( variable_get("comment_notify_regged_checkbox", TRUE) ) ) {
     $form['notify'] = array(
       '#type' => 'checkbox',
       '#title' => t('Notify me of follow-up comments posted here.'), 
       '#default_value' => ($user->uid != 0)?($user->comment_notify_mailalert):variable_get("comment_notify_default_anon_mailalert", TRUE)
       );
-  } else
-  {
+    } else {
     $form['notify'] = array(
     '#type' => 'hidden',
     '#title' => t('Mail me updates to this comment.'), 
@@ -243,11 +220,20 @@
   switch ($op) {
     case 'disable':
       $key=$arg;
+
+            if($GLOBALS['db_type'] == 'pgsql') {
+                db_query("UPDATE {$comments} c, users u 
+                        SET c.notify = 0 
+                        WHERE u.uid = c.uid AND 
+                        md5(c.mail || cast(coalesce(u.mail,u.init) AS TEXT) 
+                            || c.uid || c.name || c.nid) = '%s'", $arg);
+            }
+            else {
       db_query("UPDATE {comments} c , users u
                  SET c.notify = 0 
                WHERE u.uid=c.uid
                  AND md5( concat( c.mail,ifnull(u.mail,u.init),c.uid,c.name,c.nid ) ) = '%s'",$arg);
-
+            }
 
       drupal_set_message(t("Your comment follow up notification for this post was disabled. Thanks"));
       $title = t('Disabled comment follow up notification feature for this node');
@@ -346,6 +332,9 @@
     $commtext= $comment->comment;
     $commsubj= $comment->subject;
    
+    if(strlen($commtext) > 100) {
+        $commtext = substr($commtext,0 ,100) . '....';
+    }
     
 
     $node = node_load($nid);
@@ -356,7 +345,19 @@
 
 #   drupal_set_message ("send 1 for $nid");
 
+    if($GLOBALS['db_type'] == 'pgsql') {
+        $nid = (int) $nid;
+        $sql = "SELECT DISTINCT c.cid,u.init,c.uid,c.name,c.nid,c.notify,c.status,
+            CAST(if(length(c.mail)<1, coalesce(u.mail,u.init),c.mail) AS text) as mymail,
+            c.uid,c.name AS cid, 
+            md5(c.mail || CAST(coalesce(u.mail, u.init) AS TEXT) || c.uid || c.name || c.nid ) as mymd5 
+                FROM comments c 
+                LEFT OUTER JOIN users u on u.uid=c.uid 
+                WHERE nid = $nid AND c.notify = 1 AND c.status = 0";
+        $result  = db_query($sql);
         
+    }
+    else {
     $result = db_query('SELECT DISTINCT c.cid,u.init,c.uid,c.name,c.nid ,IF(length(c.mail)<1, ifnull(u.mail,u.init),c.mail) mail,c.uid,c.name,max(cid) cid,md5( concat( c.mail,ifnull(u.mail,u.init),c.uid,c.name,c.nid ) ) mymd5
     FROM {comments} c LEFT OUTER JOIN {users} u ON u.uid=c.uid
     WHERE nid=%d  AND notify=1 AND c.status=0
@@ -364,15 +365,18 @@
       AND IF(LENGTH(c.mail)<1, ifnull(u.mail,u.init),c.mail) like \'%@%.%\'
     GROUP BY IF(LENGTH(c.mail)<1, ifnull(u.mail,u.init),c.mail),c.name',$nid
     );
-
+    }
   #      drupal_set_message ("send 2");
     
     $count=0;
+    $sent_to = array();
     
     while ($alert = db_fetch_object($result)) 
 	{
-		if( $commname != $alert->name )
+        //Don't send an email to someone who wrote the comment (they may be replying to their own etc)
+        if($alert->mymail != $comment->mail && !in_array($alert->mymail, $sent_to)) 
 		{
+            $sent_to[] = $alert->mymail;
 			if (function_exists('locale') && $languages[$user->language]) 
 			{
 				$locale = $user->language;
@@ -401,9 +405,10 @@
 			);
 
 		 
-			$headers = array();
-			
-			drupal_mail('comment_notify_mail',$alert->mail, $subject, $message, $from, $headers);
+            // header of 'To' required - remember email 101 (message headers vs body)
+            $headers = array('To' => $alert->mymail);
+            $to = $alert->mail;
+            $r = drupal_mail('comment_notify_mail',$to, $subject, $message, $from, $headers);
 			$count++;
 
 			if ( ($alert->uid)!=0 ) 
@@ -423,12 +428,15 @@
    
   }
   
-  if( $count > 0 )
+    if( $count > 0 ) {
 	  drupal_set_message ("Thanks for your comment! I just sent out $count follow-up mail notifications!");
-  
 }
+}
+
 
+function comment_notify_mail($key, &$message, $params) {
 
+}
 
 function comment_notify_settings() {
   $form['comment_notify_settings'] = array();
