summaryrefslogtreecommitdiffstats
path: root/messageid.c
blob: d96a5d4467102b2bd40819031772ce928890a827 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
/*
 * Copyright (C) 2021 Kevin J. McCarthy <kevin@8t8.us>
 *
 *     This program is free software; you can redistribute it and/or modify
 *     it under the terms of the GNU General Public License as published by
 *     the Free Software Foundation; either version 2 of the License, or
 *     (at your option) any later version.
 *
 *     This program is distributed in the hope that it will be useful,
 *     but WITHOUT ANY WARRANTY; without even the implied warranty of
 *     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *     GNU General Public License for more details.
 *
 *     You should have received a copy of the GNU General Public License
 *     along with this program; if not, write to the Free Software
 *     Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.
 */

#if HAVE_CONFIG_H
# include "config.h"
#endif

#include "mutt.h"
#include "mutt_random.h"

static char MsgIdPfx = 'A';

typedef struct msg_id_data
{
  time_t now;
  struct tm tm;
  const char *fqdn;
} MSG_ID_DATA;

static const char *id_format_str (char *dest, size_t destlen, size_t col,
                                  int cols, char op, const char *src,
                                  const char *fmt, const char *ifstring,
                                  const char *elsestring,
                                  void *data, format_flag flags)
{
  MSG_ID_DATA *id_data = (MSG_ID_DATA *)data;
  char tmp[STRING];
  unsigned char r_raw[3];
  unsigned char r_out[4 + 1];
  unsigned char z_raw[12]; /* 32 bit timestamp, plus 64 bit randomness */
  unsigned char z_out[16 + 1];

  switch (op)
  {
    case 'r':
      mutt_random_bytes ((char *)r_raw, sizeof(r_raw));
      mutt_to_base64_safeurl (r_out, r_raw, sizeof(r_raw), sizeof(r_out));
      mutt_format_s (dest, destlen, fmt, (const char *)r_out);
      break;

    case 'x':
      /* hex encoded random byte */
      mutt_random_bytes ((char *)r_raw, sizeof(r_raw[0]));
      snprintf (dest, destlen, "%02x", r_raw[0]);
      break;

    case 'z':
      /* Convert the four least significant bytes of our timestamp and put it in
         localpart, with proper endianness (for humans) taken into account. */
      for (int i = 0; i < 4; i++)
        z_raw[i] = (uint8_t) (id_data->now >> (3-i)*8u);
      mutt_random_bytes ((char *)z_raw + 4, sizeof(z_raw) - 4);
      mutt_to_base64_safeurl (z_out, z_raw, sizeof(z_raw), sizeof(z_out));
      mutt_format_s (dest, destlen, fmt, (const char *)z_out);
      break;

    case 'Y':
      snprintf (tmp, sizeof (tmp), "%%%sd", fmt);
      snprintf (dest, destlen, tmp, id_data->tm.tm_year + 1900);
      break;
    case 'm':
      snprintf (tmp, sizeof (tmp), "%%%sd", fmt);
      snprintf (dest, destlen, tmp, id_data->tm.tm_mon + 1);
      break;
    case 'd':
      snprintf (tmp, sizeof (tmp), "%%%sd", fmt);
      snprintf (dest, destlen, tmp, id_data->tm.tm_mday);
      break;
    case 'H':
      snprintf (tmp, sizeof (tmp), "%%%sd", fmt);
      snprintf (dest, destlen, tmp, id_data->tm.tm_hour);
      break;
    case 'M':
      snprintf (tmp, sizeof (tmp), "%%%sd", fmt);
      snprintf (dest, destlen, tmp, id_data->tm.tm_min);
      break;
    case 'S':
      snprintf (tmp, sizeof (tmp), "%%%sd", fmt);
      snprintf (dest, destlen, tmp, id_data->tm.tm_sec);
      break;

    case 'c':
      snprintf (dest, destlen, "%c", MsgIdPfx);
      MsgIdPfx = (MsgIdPfx == 'Z') ? 'A' : MsgIdPfx + 1;
      break;

    case 'p':
      snprintf (tmp, sizeof (tmp), "%%%su", fmt);
      snprintf (dest, destlen, tmp, (unsigned int)getpid ());
      break;

    case 'f':
      mutt_format_s (dest, destlen, fmt, id_data->fqdn);
      break;
  }

  return (src);
}

char *mutt_gen_msgid (void)
{
  MSG_ID_DATA id_data;
  BUFFER *buf, *tmp;
  const char *fmt;
  char *rv;

  id_data.now = time (NULL);
  memcpy (&id_data.tm, gmtime (&id_data.now), sizeof(id_data.tm));
  if (!(id_data.fqdn = mutt_fqdn(0)))
    id_data.fqdn = NONULL(Hostname);

  fmt = MessageIdFormat;
  if (!fmt)
    fmt = "<%z@%f>";

  buf = mutt_buffer_pool_get ();
  mutt_FormatString (buf->data, buf->dsize, 0, buf->dsize,
                     fmt, id_format_str, &id_data, 0);
  mutt_buffer_fix_dptr (buf);

  /* this is hardly a thorough check, but at least make sure
   * we have the angle brackets. */
  if (!mutt_buffer_len (buf) ||
      (*buf->data != '<') || (*(buf->dptr - 1) != '>'))
  {
    tmp = mutt_buffer_pool_get ();
    if (!mutt_buffer_len (buf) || *buf->data != '<')
      mutt_buffer_addch (tmp, '<');
    mutt_buffer_addstr (tmp, mutt_b2s (buf));
    if (!mutt_buffer_len (buf) || *(buf->dptr - 1) != '>')
      mutt_buffer_addch (tmp, '>');
    mutt_buffer_strcpy (buf, mutt_b2s (tmp));
    mutt_buffer_pool_release (&tmp);
  }

  rv = safe_strdup (mutt_b2s (buf));
  mutt_buffer_pool_release (&buf);
  return rv;
}