msgid.c
5.13 KB
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
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
/* GNU Mailutils -- a suite of utilities for electronic mail
Copyright (C) 1999-2003, 2005-2007, 2009-2012, 2014 Free Software
Foundation, Inc.
This library is free software; you can redistribute it and/or
modify it under the terms of the GNU Lesser General Public
License as published by the Free Software Foundation; either
version 3 of the License, or (at your option) any later version.
This library 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
Lesser General Public License for more details.
You should have received a copy of the GNU Lesser General
Public License along with this library. If not, see
<http://www.gnu.org/licenses/>. */
#if HAVE_CONFIG_H
# include <config.h>
#endif
#include <sys/time.h>
#include <errno.h>
#include <unistd.h>
#include <stdlib.h>
#include <string.h>
#include <mailutils/cctype.h>
#include <mailutils/message.h>
#include <mailutils/header.h>
#include <mailutils/datetime.h>
#include <mailutils/util.h>
#include <mailutils/io.h>
#include <mailutils/envelope.h>
#include <mailutils/errno.h>
#include <mailutils/argcv.h>
#define ST_INIT 0
#define ST_MSGID 1
static int
strip_message_id (const char *msgid, char **pval)
{
const char *p;
char *q;
int state;
*pval = malloc (strlen (msgid) + 1);
if (!*pval)
return ENOMEM;
state = ST_INIT;
for (p = msgid, q = *pval; *p; p++)
{
switch (state)
{
case ST_INIT:
if (*p == '<')
{
*q++ = *p;
state = ST_MSGID;
}
else if (mu_isspace (*p))
*q++ = *p;
break;
case ST_MSGID:
*q++ = *p;
if (*p == '>')
state = ST_INIT;
break;
}
}
*q = 0;
return 0;
}
static int
get_msgid_header (mu_header_t hdr, const char *name, char **val)
{
const char *p;
int status = mu_header_sget_value (hdr, name, &p);
if (status)
return status;
return strip_message_id (p, val);
}
/* rfc2822:
The "References:" field will contain the contents of the parent's
"References:" field (if any) followed by the contents of the parent's
"Message-ID:" field (if any). If the parent message does not contain
a "References:" field but does have an "In-Reply-To:" field
containing a single message identifier, then the "References:" field
will contain the contents of the parent's "In-Reply-To:" field
followed by the contents of the parent's "Message-ID:" field (if
any). If the parent has none of the "References:", "In-Reply-To:",
or "Message-ID:" fields, then the new message will have no
References:" field. */
int
mu_rfc2822_references (mu_message_t msg, char **pstr)
{
char *argv[3] = { NULL, NULL, NULL };
mu_header_t hdr;
int rc;
rc = mu_message_get_header (msg, &hdr);
if (rc)
return rc;
get_msgid_header (hdr, MU_HEADER_MESSAGE_ID, &argv[1]);
if (get_msgid_header (hdr, MU_HEADER_REFERENCES, &argv[0]))
get_msgid_header (hdr, MU_HEADER_IN_REPLY_TO, &argv[0]);
if (argv[0] && argv[1])
{
rc = mu_argcv_join (2, argv, " ", mu_argcv_escape_no, pstr);
free (argv[0]);
free (argv[1]);
}
else if (argv[0])
*pstr = argv[0];
else if (argv[1])
*pstr = argv[1];
else
rc = MU_ERR_FAILURE;
return rc;
}
int
mu_rfc2822_msg_id (int subpart, char **pval)
{
char date[4+2+2+2+2+2+1];
time_t t = time (NULL);
struct tm *tm = localtime (&t);
char *host;
char *p;
mu_strftime (date, sizeof date, "%Y%m%d%H%M%S", tm);
mu_get_host_name (&host);
if (subpart)
{
struct timeval tv;
gettimeofday (&tv, NULL);
mu_asprintf (&p, "<%s.%lu.%d@%s>",
date,
(unsigned long) getpid (),
subpart,
host);
}
else
mu_asprintf (&p, "<%s.%lu@%s>", date, (unsigned long) getpid (), host);
free (host);
*pval = p;
return 0;
}
#define COMMENT "Your message of "
/*
The "In-Reply-To:" field will contain the contents of the "Message-
ID:" field of the message to which this one is a reply (the "parent
message"). If there is more than one parent message, then the "In-
Reply-To:" field will contain the contents of all of the parents'
"Message-ID:" fields. If there is no "Message-ID:" field in any of
the parent messages, then the new message will have no "In-Reply-To:"
field.
*/
int
mu_rfc2822_in_reply_to (mu_message_t msg, char **pstr)
{
const char *argv[] = { NULL, NULL, NULL, NULL, NULL };
mu_header_t hdr;
int rc;
int idx = 0;
rc = mu_message_get_header (msg, &hdr);
if (rc)
return rc;
if (mu_header_sget_value (hdr, MU_HEADER_DATE, &argv[idx + 1]))
{
mu_envelope_t envelope = NULL;
mu_message_get_envelope (msg, &envelope);
mu_envelope_sget_date (envelope, &argv[idx + 1]);
}
if (argv[idx + 1])
{
argv[idx] = COMMENT;
idx = 2;
}
if (mu_header_sget_value (hdr, MU_HEADER_MESSAGE_ID, &argv[idx]) == 0)
{
if (idx > 1)
{
argv[idx + 1] = argv[idx];
argv[idx] = "\n\t";
idx++;
}
idx++;
}
if (idx > 1)
rc = mu_argcv_join (idx, (char**) argv, "", mu_argcv_escape_no, pstr);
else
rc = MU_ERR_FAILURE;
return rc;
}