masqmail
view src/header.c @ 301:55c530a83d51
refactoring
author | markus schnalke <meillo@marmaro.de> |
---|---|
date | Thu, 09 Dec 2010 15:42:02 -0300 |
parents | 92063f90f9be |
children | 2ffcd38ccf53 |
line source
1 /* MasqMail
2 Copyright (C) 2000 Oliver Kurth
4 This program is free software; you can redistribute it and/or modify
5 it under the terms of the GNU General Public License as published by
6 the Free Software Foundation; either version 2 of the License, or
7 (at your option) any later version.
9 This program is distributed in the hope that it will be useful,
10 but WITHOUT ANY WARRANTY; without even the implied warranty of
11 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 GNU General Public License for more details.
14 You should have received a copy of the GNU General Public License
15 along with this program; if not, write to the Free Software
16 Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
17 */
18 #include "masqmail.h"
20 header_name header_names[] = {
21 {"From", HEAD_FROM,},
22 {"Sender", HEAD_SENDER,},
23 {"To", HEAD_TO,},
24 {"Cc", HEAD_CC,},
25 {"Bcc", HEAD_BCC,},
26 {"Date", HEAD_DATE,},
27 {"Message-Id", HEAD_MESSAGE_ID,},
28 {"Reply-To", HEAD_REPLY_TO,},
29 {"Subject", HEAD_SUBJECT,},
30 {"Return-Path", HEAD_RETURN_PATH,},
31 {"Envelope-To", HEAD_ENVELOPE_TO,},
32 {"Received", HEAD_RECEIVED},
33 };
35 /* this was borrowed from exim and slightly changed */
36 gchar*
37 rec_timestamp()
38 {
39 static gchar buf[64];
40 int len;
42 time_t now = time(NULL);
43 struct tm *t = localtime(&now);
45 int diff_hour, diff_min;
46 struct tm local;
47 struct tm *gmt;
49 memcpy(&local, t, sizeof(struct tm));
50 gmt = gmtime(&now);
51 diff_min = 60 * (local.tm_hour - gmt->tm_hour) + local.tm_min - gmt->tm_min;
52 if (local.tm_year != gmt->tm_year) {
53 diff_min += (local.tm_year > gmt->tm_year) ? 1440 : -1440;
54 } else if (local.tm_yday != gmt->tm_yday) {
55 diff_min += (local.tm_yday > gmt->tm_yday) ? 1440 : -1440;
56 }
57 diff_hour = diff_min / 60;
58 diff_min = abs(diff_min - diff_hour * 60);
60 len = strftime(buf, sizeof(buf), "%a, ", &local);
61 g_snprintf(buf + len, sizeof(buf) - len, "%02d ", local.tm_mday);
62 len += strlen(buf + len);
63 len += strftime(buf + len, sizeof(buf) - len, "%b %Y %H:%M:%S", &local);
64 g_snprintf(buf + len, sizeof(buf) - len, " %+03d%02d", diff_hour, diff_min);
66 return buf;
67 }
69 /* finds list of headers matching id
70 if id == HEAD_UNKNOWN and header == NULL finds all unknown headers
71 else finds all headers matching header
72 */
73 GList*
74 find_header(GList * hdr_list, header_id id, gchar * hdr_str)
75 {
76 GList *found_list = NULL;
77 GList *node;
79 if ((id != HEAD_UNKNOWN) || !hdr_str) {
80 foreach(hdr_list, node) {
81 header *hdr = (header *) (node->data);
82 if (hdr->id == id) {
83 found_list = g_list_append(found_list, hdr);
84 }
85 }
86 return found_list;
87 }
89 foreach(hdr_list, node) {
90 header *hdr = (header *) (node->data);
91 gchar buf[64], *q = buf, *p = hdr->header;
93 while (*p != ':' && q < buf+sizeof(buf)-1 && *p) {
94 *(q++) = *(p++);
95 }
96 *q = '\0';
98 if (strcasecmp(buf, hdr_str) == 0) {
99 found_list = g_list_append(found_list, hdr);
100 }
101 }
102 return found_list;
103 }
105 void
106 header_unfold(header * hdr)
107 {
108 gchar *tmp_hdr = g_malloc(strlen(hdr->header));
109 gchar *p = hdr->header, *q = tmp_hdr;
110 gboolean flag = FALSE;
112 while (*p) {
113 if (*p != '\n') {
114 *(q++) = *p;
115 } else {
116 flag = TRUE;
117 }
118 p++;
119 }
120 *(q++) = '\n';
122 if (flag) {
123 gchar *new_hdr;
125 g_free(hdr->header);
126 new_hdr = g_strdup(tmp_hdr);
127 g_free(tmp_hdr);
128 hdr->value = new_hdr + (hdr->value - hdr->header);
129 hdr->header = new_hdr;
130 }
131 }
133 #define MAX_HDR_LEN 72
134 void
135 header_fold(header * hdr)
136 {
137 gint len = strlen(hdr->header);
138 gchar *p, *q;
139 gchar *tmp_hdr;
140 int valueoffset;
142 if (len < MAX_HDR_LEN) {
143 /* we don't need to do anything */
144 return;
145 }
147 /* the position in hdr->header where the value part starts */
148 valueoffset = hdr->value - hdr->header;
150 /* TODO: size is only calculated roughly */
151 /* size is probably overestimated, but so we are on the safe side */
152 /* (as much as we already have + chars inserted per break * number
153 of breaks + some more) */
154 tmp_hdr = g_malloc(len + 2 * (len/MAX_HDR_LEN) + 10);
156 p = hdr->header;
157 q = tmp_hdr;
159 if (p[len - 1] == '\n') {
160 p[len - 1] = '\0';
161 }
163 while (*p) {
164 gint i, l;
165 gchar *pp;
167 /* look forward and find potential break points */
168 i = 0;
169 l = -1;
170 pp = p;
171 while (*pp && (i < MAX_HDR_LEN)) {
172 if ((*pp == ' ') || (*pp == '\t')) {
173 l = i;
174 }
175 pp++;
176 i++;
177 }
178 if (!*pp) {
179 l = pp - p; /* take rest, if EOS found */
180 }
182 if (l == -1) {
183 /* no potential break point was found within
184 MAX_HDR_LEN so advance further until the next */
185 while (*pp && *pp != ' ' && *pp != '\t') {
186 pp++;
187 i++;
188 }
189 l = i;
190 }
192 /* copy */
193 i = 0;
194 while (i < l) {
195 *(q++) = *(p++);
196 i++;
197 }
198 *(q++) = '\n';
199 *(q++) = *(p++); /* this is either space, tab or 0 */
200 /* *(q++) = '\t'; */
201 }
203 g_free(hdr->header);
204 hdr->header = tmp_hdr;
205 hdr->value = hdr->header + valueoffset;
206 }
208 header*
209 create_header(header_id id, gchar * fmt, ...)
210 {
211 gchar *p;
212 header *hdr;
213 va_list args;
214 va_start(args, fmt);
216 /* g_malloc() calls exit on failure */
217 hdr = g_malloc(sizeof(header));
219 hdr->id = id;
220 hdr->header = g_strdup_vprintf(fmt, args);
221 hdr->value = NULL;
223 /* value shall point to the first non-whitespace char in the
224 value part of the header line (i.e. after the first colon) */
225 p = strchr(hdr->header, ':');
226 if (p) {
227 p++;
228 while (*p == ' ' || *p == '\t' || *p == '\n') {
229 p++;
230 }
231 hdr->value = (*p) ? p : NULL;
232 }
234 DEBUG(3) debugf("create_header(): hdr: `%s'\n", hdr->header);
235 DEBUG(3) debugf("create_header(): val: `%s'\n", hdr->value);
237 va_end(args);
238 return hdr;
239 }
241 void
242 destroy_header(header * hdr)
243 {
244 if (hdr) {
245 if (hdr->header) {
246 g_free(hdr->header);
247 }
248 g_free(hdr);
249 }
250 }
252 header*
253 copy_header(header * hdr)
254 {
255 header *new_hdr = NULL;
257 if (hdr) {
258 new_hdr = g_malloc(sizeof(header));
259 new_hdr->id = hdr->id;
260 new_hdr->header = g_strdup(hdr->header);
261 new_hdr->value = new_hdr->header + (hdr->value - hdr->header);
262 }
263 return new_hdr;
264 }
266 header*
267 get_header(gchar * line)
268 {
269 gchar *p = line;
270 gchar buf[64], *q = buf;
271 gint i;
272 header *hdr;
274 while (*p && (*p != ':') && (q < buf+sizeof(buf)-1)) {
275 *(q++) = *(p++);
276 }
277 *q = '\0';
279 if (*p != ':') {
280 return NULL;
281 }
283 hdr = g_malloc(sizeof(header));
285 hdr->value = NULL;
286 p++;
288 while (*p && (*p == ' ' || *p == '\t')) {
289 p++;
290 }
291 hdr->value = p;
292 /* Note: an empty value can also mean that it's only the first part
293 of a folded header line */
295 for (i = 0; i < HEAD_NUM_IDS; i++) {
296 if (strcasecmp(header_names[i].header, buf) == 0) {
297 break;
298 }
299 }
300 hdr->id = (header_id) i;
301 hdr->header = g_strdup(line);
302 hdr->value = hdr->header + (hdr->value - line);
304 DEBUG(4) debugf("header: %d = %s", hdr->id, hdr->header);
305 /* Note: This only outputs the first line if the header is folded */
307 return hdr;
308 }