]> diplodocus.org Git - nmh/blob - sbr/message_id.c
forwsbr.c: Move interface declaration to own forwsbr.h.
[nmh] / sbr / message_id.c
1 /* message_id.c -- construct the body of a Message-ID or Content-ID
2 * header field
3 *
4 * This code is Copyright (c) 2012, by the authors of nmh. See the
5 * COPYRIGHT file in the root directory of the nmh distribution for
6 * complete copyright information.
7 */
8
9 #include <h/mh.h>
10 #include "m_rand.h"
11 #include "message_id.h"
12 #include <sys/time.h> /* for gettimeofday() */
13 #include "base64.h"
14
15
16 static enum {
17 NMH_MESSAGE_ID_LOCALNAME,
18 NMH_MESSAGE_ID_RANDOM
19 } message_id_style = NMH_MESSAGE_ID_LOCALNAME;
20 static char message_id_[BUFSIZ];
21
22
23 /* Convert name of message id style to integer value and store it. */
24 int
25 save_message_id_style (const char *value)
26 {
27 if (! strcasecmp (value, "localname")) {
28 message_id_style = NMH_MESSAGE_ID_LOCALNAME;
29 return 0;
30 }
31 if (! strcasecmp (value, "random")) {
32 message_id_style = NMH_MESSAGE_ID_RANDOM;
33 return 0;
34 }
35 return 1;
36 }
37
38
39 char *
40 message_id (time_t tclock, int content_id)
41 {
42 switch (message_id_style) {
43 case NMH_MESSAGE_ID_LOCALNAME: {
44 #define P(fmt) \
45 snprintf(message_id_, sizeof message_id_, \
46 (fmt), (int)getpid(), (long)tclock, LocalName(1))
47
48 if (content_id)
49 P("<%d.%ld.%%d@%s>");
50 else
51 P("<%d.%ld@%s>");
52 #undef P
53 break;
54 }
55
56 case NMH_MESSAGE_ID_RANDOM: {
57 /* Use a sequence of digits divisible by 3 because that will
58 expand to base64 without any waste. Must be shorter than 58,
59 see below. */
60 unsigned char rnd[9];
61 /* The part after the '@' is divided into thirds. The base64
62 encoded string will be 4/3 the size of rnd. */
63 size_t one_third = sizeof rnd * 4/3/3;
64
65 if (m_rand (rnd, sizeof rnd) == 0) {
66 struct timeval now;
67 /* All we really need is 4 * [sizeof rnd/3] + 2, as long as
68 the base64 encoding stays shorter than 76 bytes so embedded
69 newlines aren't necessary. But use double the sizeof rnd
70 just to be safe. */
71 unsigned char rnd_base64[2 * sizeof rnd];
72 unsigned char *cp;
73 int i;
74
75 writeBase64 (rnd, sizeof rnd, rnd_base64);
76
77 for (i = strlen ((const char *) rnd_base64) - 1;
78 i > 0 && iscntrl (rnd_base64[i]);
79 --i) {
80 /* Remove trailing newline. rnd_base64 had better be
81 shorter than 76 characters, so don't bother to look for
82 embedded newlines. */
83 rnd_base64[i] = '\0';
84 }
85
86 /* Try to make the base64 string look a little more like a
87 hostname by replacing + with - and / with _. */
88 for (cp = rnd_base64; *cp; ++cp) {
89 if (*cp == '+') {
90 *cp = '-';
91 } else if (*cp == '/') {
92 *cp = '_';
93 }
94 }
95
96 /* gettimeofday() and getpid() shouldn't fail on POSIX platforms. */
97 gettimeofday (&now, 0);
98
99 /* The format string inserts a couple of dots, for the benefit
100 of spam filters that want to see a message id with a final
101 part that resembles a hostname. */
102 #define P(fmt) \
103 snprintf(message_id_, sizeof message_id_, (fmt), \
104 getpid(), (long)now.tv_sec, (long)now.tv_usec, \
105 (int)one_third, rnd_base64, \
106 (int)one_third, &rnd_base64[one_third], \
107 (int)one_third, &rnd_base64[2*one_third])
108
109 if (content_id)
110 P("<%d-%ld.%06ld%%d@%.*s.%.*s.%.*s>");
111 else
112 P("<%d-%ld.%06ld@%.*s.%.*s.%.*s>");
113 #undef P
114 }
115
116 break;
117 }
118 }
119
120 return message_id_;
121 }