]> diplodocus.org Git - nmh/blobdiff - sbr/message_id.c
print_sw.c: Move interface to own file.
[nmh] / sbr / message_id.c
index ce65ed9c9688a9c541ccf189df19089f2e6527a4..1e29051d9a3917a004fab76f410dc312b3d1eb2e 100644 (file)
@@ -1,5 +1,4 @@
-/*
- * message-id.c -- construct the body of a Message-ID or Content-ID
+/* message_id.c -- construct the body of a Message-ID or Content-ID
  *                 header field
  *
  * This code is Copyright (c) 2012, by the authors of nmh.  See the
@@ -7,10 +6,12 @@
  * complete copyright information.
  */
 
-#include <h/mh.h>
-#include <unistd.h>    /* for getpid() */
-#include <sys/time.h>  /* for gettimeofday() */
-#include <stdio.h>
+#include "h/mh.h"
+#include "h/mts.h"
+#include "m_rand.h"
+#include "message_id.h"
+#include <sys/time.h>
+#include "base64.h"
 
 
 static enum {
@@ -22,35 +23,38 @@ static char message_id_[BUFSIZ];
 
 /* Convert name of message id style to integer value and store it. */
 int
-save_message_id_style (const char *value) {
+save_message_id_style (const char *value)
+{
   if (! strcasecmp (value, "localname")) {
     message_id_style = NMH_MESSAGE_ID_LOCALNAME;
     return 0;
-  } else if (! strcasecmp (value, "random")) {
+  }
+  if (! strcasecmp (value, "random")) {
     message_id_style = NMH_MESSAGE_ID_RANDOM;
     return 0;
-  } else {
-    return 1;
   }
+  return 1;
 }
 
 
 char *
-message_id (time_t tclock, int content_id) {
+message_id (time_t tclock, int content_id)
+{
   switch (message_id_style) {
     case NMH_MESSAGE_ID_LOCALNAME: {
-      char *format = content_id  ?  "<%d.%ld.%%d@%s>"  :  "<%d.%ld@%s>";
-
-      snprintf (message_id_, sizeof message_id_, format,
-                (int) getpid (), (long) tclock, LocalName (1));
-
+#define P(fmt) \
+    snprintf(message_id_, sizeof message_id_, \
+        (fmt), (int)getpid(), (long)tclock, LocalName(1))
+
+            if (content_id)
+                P("<%d.%ld.%%d@%s>");
+            else
+                P("<%d.%ld@%s>");
+#undef P
       break;
     }
 
     case NMH_MESSAGE_ID_RANDOM: {
-      char *format = content_id
-        ?  "<%d-%ld.%06ld%%d@%.*s.%.*s.%.*s>"
-        :  "<%d-%ld.%06ld@%.*s.%.*s.%.*s>";
       /* Use a sequence of digits divisible by 3 because that will
          expand to base64 without any waste.  Must be shorter than 58,
          see below. */
@@ -96,11 +100,18 @@ message_id (time_t tclock, int content_id) {
         /* The format string inserts a couple of dots, for the benefit
            of spam filters that want to see a message id with a final
            part that resembles a hostname. */
-        snprintf (message_id_, sizeof message_id_, format,
-                  getpid (), (long) now.tv_sec, (long) now.tv_usec,
-                  one_third, rnd_base64,
-                  one_third, &rnd_base64[one_third],
-                  one_third, &rnd_base64[2*one_third]);
+#define P(fmt) \
+    snprintf(message_id_, sizeof message_id_, (fmt), \
+        getpid(), (long)now.tv_sec, (long)now.tv_usec, \
+        (int)one_third, rnd_base64, \
+        (int)one_third, &rnd_base64[one_third], \
+        (int)one_third, &rnd_base64[2*one_third])
+
+            if (content_id)
+                P("<%d-%ld.%06ld%%d@%.*s.%.*s.%.*s>");
+            else
+                P("<%d-%ld.%06ld@%.*s.%.*s.%.*s>");
+#undef P
       }
 
       break;