--- spamass-milter-0.3.1/spamass-milter.h.orig	2006-03-23 23:07:55.000000000 +0100
+++ spamass-milter-0.3.1/spamass-milter.h	2007-07-24 10:18:43.000000000 +0200
@@ -1,6 +1,6 @@
 //-*-c++-*-
 //
-//  $Id: spamass-milter.h,v 1.23 2005/04/07 02:04:24 dnelson Exp $
+//  $Id: spamass-milter.h,v 1.2 2007/07/24 08:18:43 zz Exp zz $
 //
 //  Main include file for SpamAss-Milter
 //
@@ -108,6 +108,9 @@
   int     numrcpt();	/* total RCPT TO: recpients */
   int     set_numrcpt();	/* increment total RCPT count */
   int     set_numrcpt(const int);	/* set total RCPT count to n */
+  int     cnt_hdr_subj();	/* total Subject: headers */
+  int     set_cnt_hdr_subj();	/* increment total subject count */
+  int     set_cnt_hdr_subj(const int);	/* set total subject count to n */
   string::size_type set_spam_status(const string&);
   string::size_type set_spam_flag(const string&);
   string::size_type set_spam_report(const string&);
@@ -147,6 +150,9 @@
   
   // Counter to keep track of the number of recipients
   int    _numrcpt;
+  
+  // Counter to keep track of the number of subject header lines
+  int    _cnt_hdr_subj ;
 
   // The list of recipients for the current message
   list <string> recipients;
--- spamass-milter-0.3.1/spamass-milter.cpp.orig	2007-07-23 15:51:41.000000000 +0200
+++ spamass-milter-0.3.1/spamass-milter.cpp	2007-07-25 15:00:42.000000000 +0200
@@ -1,6 +1,6 @@
 // 
 //
-//  $Id: spamass-milter.cpp,v 1.90 2006/03/23 21:41:36 dnelson Exp $
+//  $Id: spamass-milter.cpp,v 1.4 2007/07/25 13:00:42 zz Exp zz $
 //
 //  SpamAss-Milter 
 //    - a rather trivial SpamAssassin Sendmail Milter plugin
@@ -127,7 +127,7 @@
 
 // }}} 
 
-static const char Id[] = "$Id: spamass-milter.cpp,v 1.90 2006/03/23 21:41:36 dnelson Exp $";
+static const char Id[] = "$Id: spamass-milter.cpp,v 1.4 2007/07/25 13:00:42 zz Exp zz $";
 
 struct smfiDesc smfilter =
   {
@@ -369,6 +369,8 @@
 
 	string newstring;
 	string::size_type oldsize;
+	bool is_hdr_subj ;
+	int cnt_hdr_subj ;
 
 	debug(D_UORI, "u_or_i: looking at <%s>", header);
 	debug(D_UORI, "u_or_i: oldstring: <%s>", oldstring.c_str());
@@ -377,23 +379,49 @@
 	debug(D_UORI, "u_or_i: newstring: <%s>", newstring.c_str());
 
 	oldsize = callsetter(*assassin,setter)(newstring);
+
+	is_hdr_subj =
+	    (cmp_nocase_partial("Subject", header) == 0) ? true : false ;
+
+	cnt_hdr_subj = assassin->cnt_hdr_subj() ;
       
 	if (!dontmodify)
 	{
 		if (newstring != oldstring)
 		{
-			/* change if old one was present, append if non-null */
-			char* cstr = const_cast<char*>(newstring.c_str());
-			if (oldsize > 0)
-			{
-				debug(D_UORI, "u_or_i: changing");
-				smfi_chgheader(ctx, header, 1, newstring.size() > 0 ? 
-					cstr : NULL );
-			} else if (newstring.size() > 0)
+		    /* change if old one was present, append if non-null */
+		    char* cstr = const_cast<char*>(newstring.c_str());
+		    if ( (oldsize > 0) || (is_hdr_subj) && (cnt_hdr_subj >= 1) )
+		    {
+			debug(D_UORI, "u_or_i: changing");
+			smfi_chgheader(
+			    ctx,
+			    header,
+			    1, /* only the first */
+			    newstring.size() > 0 ?  cstr : NULL
+			) ;
+		    } else if (newstring.size() > 0)
+		    {
+			    debug(D_UORI, "u_or_i: inserting");
+			    smfi_addheader(ctx, header, cstr);
+		    }
+
+		    if ( (is_hdr_subj) && (cnt_hdr_subj > 1) )
+		    {
+			/* delete extraneous subject: headers */
+			int i ;
+			for ( i=2 ; i<=assassin->cnt_hdr_subj() ; i++ )
 			{
-				debug(D_UORI, "u_or_i: inserting");
-				smfi_addheader(ctx, header, cstr);
-			}
+			    debug(D_UORI, "u_or_i: removing extra subject");
+			    smfi_chgheader(
+				ctx,
+				header,
+				i,
+				NULL
+			    );
+			} /* for hdridx 2..cnt_hdr_subj  */
+		    }  /* subject: header count > 1 */
+
 		} else
 		{
 			debug(D_UORI, "u_or_i: no change");
@@ -1100,7 +1128,14 @@
 
   // Subject: should be stored
   if ( cmp_nocase_partial("Subject", headerf) == 0 )
-    assassin->set_subject(headerv);
+  {
+    assassin->set_cnt_hdr_subj() ;
+    if ( assassin->cnt_hdr_subj() == 1 )
+    {
+      /* only the first */
+      assassin->set_subject(headerv);
+    }
+  }
 
   // assemble header to be written to SpamAssassin
   string header = string(headerf) + ": " + headerv + "\r\n";
@@ -1294,7 +1329,9 @@
   error(false),
   running(false),
   connected(false),
-  _numrcpt(0)
+  _numrcpt(0),
+  // count no. of subject: headers
+  _cnt_hdr_subj(0)
 {
 }
 
@@ -1725,6 +1762,26 @@
   return _numrcpt;
 }
 
+int
+SpamAssassin::cnt_hdr_subj()
+{
+  return _cnt_hdr_subj ;
+}
+
+int
+SpamAssassin::set_cnt_hdr_subj()
+{
+  _cnt_hdr_subj++ ;
+  return _cnt_hdr_subj ;
+}
+
+int
+SpamAssassin::set_cnt_hdr_subj(const int val)
+{
+  _cnt_hdr_subj = val ;
+  return _cnt_hdr_subj ;
+}
+
 //
 // set the values of the different SpamAssassin
 // fields in our element. Returns former size of field
