+/* normalized character (we're stricter than RFC2822, 3.6.4) */
+static char mutt_normalized_char(char c)
+{
+ return (isalnum(c) || strchr(".!#$%&'*+-/=?^_`{|}~", c)) ? c : '.';
+}
+
+static void mutt_gen_localpart(char *buf, unsigned int len, const char *fmt)
+{
+#define APPEND_FMT(fmt, arg) \
+ if (len > 1) { \
+ int snlen = snprintf(buf, len, fmt, arg); \
+ buf += snlen; \
+ len -= snlen; \
+ }
+
+#define APPEND_BYTE(c) \
+ if (len > 1) { \
+ *buf++ = c; \
+ *buf = '\0'; \
+ len--; \
+ }
+
+ time_t now;
+ struct tm *tm;
+
+ now = time (NULL);
+ tm = gmtime (&now);
+
+ while (*fmt) {
+ static char MsgIdPfx = 'A';
+ int c = *fmt++;
+
+ if (c != '%') {
+ APPEND_BYTE(mutt_normalized_char(c));
+ continue;
+ }
+
+ switch (*fmt++) {
+ case 0:
+ return;
+ case 'd':
+ APPEND_FMT("%02d", tm->tm_mday);
+ break;
+ case 'h':
+ APPEND_FMT("%02d", tm->tm_hour);
+ break;
+ case 'm':
+ APPEND_FMT("%02d", tm->tm_mon + 1);
+ break;
+ case 'M':
+ APPEND_FMT("%02d", tm->tm_min);
+ break;
+ case 'O':
+ APPEND_FMT("%lo", (unsigned long)now);
+ break;
+ case 'p':
+ APPEND_FMT("%u", (unsigned int)getpid());
+ break;
+ case 'P':
+ APPEND_FMT("%c", MsgIdPfx);
+ MsgIdPfx = (MsgIdPfx == 'Z') ? 'A' : MsgIdPfx + 1;
+ break;
+ case 'r':
+ APPEND_FMT("%u", (unsigned int)rand());
+ break;
+ case 'R':
+ APPEND_FMT("%x", (unsigned int)rand());
+ break;
+ case 's':
+ APPEND_FMT("%02d", tm->tm_sec);
+ break;
+ case 'T':
+ APPEND_FMT("%u", (unsigned int) now);
+ break;
+ case 'X':
+ APPEND_FMT("%x", (unsigned int) now);
+ break;
+ case 'Y': /* this will break in the year 10000 ;-) */
+ APPEND_FMT("%04d", tm->tm_year + 1900);
+ break;
+ case '%':
+ APPEND_BYTE('%');
+ break;
+ default: /* invalid formats are replaced by '.' */
+ APPEND_BYTE('.');
+ m_strncat(buf, len, ".", 1);
+ }
+ }
+
+ *buf = '\0';
+
+#undef APPEND_BYTE
+#undef APPEND_FMT
+}
+
+static char *mutt_gen_msgid (void)