Nico Golde:
[apps/madmutt.git] / sendlib.c
index 666f4ec..5cdfcf4 100644 (file)
--- a/sendlib.c
+++ b/sendlib.c
 
 #define _SENDLIB_C 1
 
+#if HAVE_CONFIG_H
+# include "config.h"
+#endif
+
 #include "mutt.h"
 #include "mutt_curses.h"
 #include "rfc2047.h"
@@ -1810,9 +1814,102 @@ const char *mutt_fqdn(short may_hide_host)
   return p;
 }
 
+static char mutt_normalized_char(char c) {
+  if (isalnum(c))
+    return c;
+  if (strchr(".!#$%&'*+-/=?^_`{|}~",c))
+    return c;
+  return '.'; /* normalized character (we're stricter than RFC2822, 3.6.4) */
+}
+
+static void mutt_gen_localpart(char * buf, unsigned int len, char * fmt) {
+  time_t now;
+  struct tm *tm;
+  char tmp[SHORT_STRING];
+
+  *buf = '\0';
+
+  now = time (NULL);
+  tm = gmtime (&now);
+
+  for (;*fmt;++fmt) {
+    if (*fmt == '%') {
+      switch (fmt[1]) {
+      case 0:
+        return;
+      case 'd':
+        snprintf(tmp,sizeof(tmp),"%02d",tm->tm_mday);
+        safe_strncat(buf,len,tmp,2);
+        break;
+      case 'h':
+        snprintf(tmp,sizeof(tmp),"%02d",tm->tm_hour);
+        safe_strncat(buf,len,tmp,2);
+        break;
+      case 'm':
+        snprintf(tmp,sizeof(tmp),"%02d",tm->tm_mon+1);
+        safe_strncat(buf,len,tmp,2);
+        break;
+      case 'M':
+        snprintf(tmp,sizeof(tmp),"%02d",tm->tm_min);
+        safe_strncat(buf,len,tmp,2);
+        break;
+      case 'O':
+        snprintf(tmp,sizeof(tmp),"%lo",(unsigned long)now);
+        safe_strncat(buf,len,tmp,strlen(tmp));
+        break;
+      case 'p':
+        snprintf(tmp,sizeof(tmp),"%u",(unsigned int)getpid());
+        safe_strncat(buf,len,tmp,strlen(tmp));
+        break;
+      case 'P':
+        snprintf(tmp,sizeof(tmp),"%c",MsgIdPfx);
+        MsgIdPfx = (MsgIdPfx == 'Z') ? 'A' : MsgIdPfx + 1;
+        safe_strncat(buf,len,tmp,1);
+        break;
+      case 'r':
+        snprintf(tmp,sizeof(tmp),"%u",(unsigned int)rand());
+        safe_strncat(buf,len,tmp,strlen(tmp));
+        break;
+      case 'R':
+        snprintf(tmp,sizeof(tmp),"%x",(unsigned int)rand());
+        safe_strncat(buf,len,tmp,strlen(tmp));
+        break;
+      case 's':
+        snprintf(tmp,sizeof(tmp),"%02d",tm->tm_sec);
+        safe_strncat(buf,len,tmp,2);
+        break;
+      case 'T':
+        snprintf(tmp,sizeof(tmp),"%u",(unsigned int)now);
+        safe_strncat(buf,len,tmp,strlen(tmp));
+        break;
+      case 'X':
+        snprintf(tmp,sizeof(tmp),"%x",(unsigned int)now);
+        safe_strncat(buf,len,tmp,strlen(tmp));
+        break;
+      case 'Y':
+        snprintf(tmp,sizeof(tmp),"%04d",tm->tm_year+1900); /* this will break in the year 10000 ;-) */
+        safe_strncat(buf,len,tmp,4);
+        break;
+      case '%':
+        safe_strncat(buf,len,"%",1);
+        break;
+      default:
+        safe_strncat(buf,len,".",1); /* invalid formats are replaced by '.' */
+      } /* switch */
+      ++fmt;
+    } else {
+      char c;
+      c = mutt_normalized_char(*fmt); /* @todo: filter out invalid characters */
+      safe_strncat(buf,len,&c,1);
+    }
+  }
+}
+
 char *mutt_gen_msgid (void)
 {
   char buf[SHORT_STRING];
+  char localpart[SHORT_STRING];
+  unsigned int localpart_length;
   time_t now;
   struct tm *tm;
   const char *fqdn;
@@ -1822,10 +1919,11 @@ char *mutt_gen_msgid (void)
   if(!(fqdn = mutt_fqdn(0)))
     fqdn = NONULL(Hostname);
 
-  snprintf (buf, sizeof (buf), "<%d%02d%02d%02d%02d%02d.G%c%d@%s>",
-           tm->tm_year + 1900, tm->tm_mon + 1, tm->tm_mday, tm->tm_hour,
-           tm->tm_min, tm->tm_sec, MsgIdPfx, getpid (), fqdn);
-  MsgIdPfx = (MsgIdPfx == 'Z') ? 'A' : MsgIdPfx + 1;
+  localpart_length = sizeof(buf) - strlen(fqdn) - 4; /* the 4 characters are '<', '@', '>' and '\0' */
+
+  mutt_gen_localpart(localpart,localpart_length,MsgIdFormat);
+
+  snprintf(buf,sizeof(buf),"<%s@%s>",localpart,fqdn);
   return (safe_strdup (buf));
 }
 
@@ -2376,40 +2474,42 @@ int mutt_bounce_message (FILE *fp, HEADER *h, ADDRESS *to)
 /* given a list of addresses, return a list of unique addresses */
 ADDRESS *mutt_remove_duplicates (ADDRESS *addr)
 {
-  ADDRESS *top = NULL;
+  ADDRESS *top = addr;
+  ADDRESS **last = &top;
   ADDRESS *tmp;
-  
-  if ((top = addr) == NULL)
-    return (NULL);
-  addr = addr->next;
-  top->next = NULL;
+  int dup;
+
   while (addr)
   {
-    tmp = top;
-    do {
-      if (addr->mailbox && tmp->mailbox &&
+    for (tmp = top, dup = 0; tmp && tmp != addr; tmp = tmp->next)
+    {
+      if (tmp->mailbox && addr->mailbox && 
          !ascii_strcasecmp (addr->mailbox, tmp->mailbox))
       {
-       /* duplicate address, just ignore it */
-       tmp = addr;
-       addr = addr->next;
-       tmp->next = NULL;
-       rfc822_free_address (&tmp);
-      }
-      else if (!tmp->next)
-      {
-       /* unique address.  add it to the list */
-       tmp->next = addr;
-       addr = addr->next;
-       tmp = tmp->next;
-       tmp->next = NULL;
-       tmp = NULL; /* so we exit the loop */
+       dup = 1;
+       break;
       }
-      else
-       tmp = tmp->next;
-    } while (tmp);
-  }
+    }
+    
+    if (dup)
+    {
+      dprint (2, (debugfile, "mutt_remove_duplicates: Removing %s\n",
+                 addr->mailbox));
+      
+      *last = addr->next;
 
+      addr->next = NULL;
+      rfc822_free_address(&addr);
+      
+      addr = *last;
+    }
+    else 
+    {
+      last = &addr->next;
+      addr = addr->next;
+    }
+  }
+  
   return (top);
 }
 
@@ -2504,6 +2604,8 @@ int mutt_write_fcc (const char *path, HEADER *hdr, const char *msgid, int post,
       if (PgpSignAs && *PgpSignAs)
         fprintf (msg->fp, "<%s>", PgpSignAs);
     }
+    if (hdr->security & INLINE)
+      fputc ('I', msg->fp);
     fputc ('\n', msg->fp);
   }
 
@@ -2522,6 +2624,8 @@ int mutt_write_fcc (const char *path, HEADER *hdr, const char *msgid, int post,
        if (SmimeDefaultKey && *SmimeDefaultKey)
            fprintf (msg->fp, "<%s>", SmimeDefaultKey);
     }
+    if (hdr->security & INLINE)
+      fputc ('I', msg->fp);
     fputc ('\n', msg->fp);
   }