masqmail-0.2

view src/header.c @ 14:a8f3424347dc

replaced number 0 with character \0 where appropriate
author meillo@marmaro.de
date Wed, 29 Oct 2008 21:21:26 +0100
parents 08114f7dcc23
children f671821d8222
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 ,
23 {"Sender", HEAD_SENDER,}
24 ,
25 {"To", HEAD_TO,}
26 ,
27 {"Cc", HEAD_CC,}
28 ,
29 {"Bcc", HEAD_BCC,}
30 ,
31 {"Date", HEAD_DATE,}
32 ,
33 {"Message-Id", HEAD_MESSAGE_ID,}
34 ,
35 {"Reply-To", HEAD_REPLY_TO,}
36 ,
37 {"Subject", HEAD_SUBJECT,}
38 ,
39 {"Return-Path", HEAD_RETURN_PATH,}
40 ,
41 {"Envelope-To", HEAD_ENVELOPE_TO,}
42 ,
43 {"Received", HEAD_RECEIVED}
44 ,
45 };
47 /* this was borrowed from exim and slightly changed */
48 gchar*
49 rec_timestamp()
50 {
51 static gchar buf[64];
52 int len;
54 time_t now = time(NULL);
55 struct tm *t = localtime(&now);
57 int diff_hour, diff_min;
58 struct tm local;
59 struct tm *gmt;
61 memcpy(&local, t, sizeof(struct tm));
62 gmt = gmtime(&now);
63 diff_min = 60 * (local.tm_hour - gmt->tm_hour) + local.tm_min - gmt->tm_min;
64 if (local.tm_year != gmt->tm_year)
65 diff_min += (local.tm_year > gmt->tm_year) ? 1440 : -1440;
66 else if (local.tm_yday != gmt->tm_yday)
67 diff_min += (local.tm_yday > gmt->tm_yday) ? 1440 : -1440;
68 diff_hour = diff_min / 60;
69 diff_min = abs(diff_min - diff_hour * 60);
71 len = strftime(buf, sizeof(buf), "%a, ", &local);
72 g_snprintf(buf + len, sizeof(buf) - len, "%02d ", local.tm_mday);
73 len += strlen(buf + len);
74 len += strftime(buf + len, sizeof(buf) - len, "%b %Y %H:%M:%S", &local);
75 g_snprintf(buf + len, sizeof(buf) - len, " %+03d%02d", diff_hour, diff_min);
77 return buf;
78 }
80 /* finds list of headers matching id
81 if id == HEAD_UNKNOWN and header == NULL finds all unknown headers
82 else finds all headers matching header
83 */
84 GList*
85 find_header(GList * hdr_list, header_id id, gchar * hdr_str)
86 {
87 GList *found_list = NULL;
88 GList *node;
90 if ((id != HEAD_UNKNOWN) || (hdr_str == NULL)) {
91 foreach(hdr_list, node) {
92 header *hdr = (header *) (node->data);
93 if (hdr->id == id)
94 found_list = g_list_append(found_list, hdr);
95 }
96 } else {
97 foreach(hdr_list, node) {
98 header *hdr = (header *) (node->data);
99 gchar buf[64], *q = buf, *p = hdr->header;
101 while (*p != ':' && q < buf + 63 && *p)
102 *(q++) = *(p++);
103 *q = 0;
105 if (strcasecmp(buf, hdr_str) == 0)
106 found_list = g_list_append(found_list, hdr);
107 }
108 }
109 return found_list;
110 }
112 void
113 header_unfold(header * hdr)
114 {
115 gchar *tmp_hdr = g_malloc(strlen(hdr->header));
116 gchar *p = hdr->header, *q = tmp_hdr;
117 gboolean flag = FALSE;
119 while (*p) {
120 if (*p != '\n')
121 *(q++) = *p;
122 else
123 flag = TRUE;
124 p++;
125 }
126 *(q++) = '\n';
128 if (flag) {
129 gchar *new_hdr;
131 g_free(hdr->header);
132 new_hdr = g_strdup(tmp_hdr);
133 g_free(tmp_hdr);
134 hdr->value = new_hdr + (hdr->value - hdr->header);
135 hdr->header = new_hdr;
136 }
137 }
139 #define MAX_HDR_LEN 72
140 void
141 header_fold(header * hdr)
142 {
143 gint len = strlen(hdr->header);
144 gchar *p, *q;
145 /* size is probably overestimated, but so we are on the safe side */
146 gchar *tmp_hdr = g_malloc(len + 2 * len / MAX_HDR_LEN);
148 p = hdr->header;
149 q = tmp_hdr;
151 if (p[len - 1] == '\n')
152 p[len - 1] = 0;
154 while (*p) {
155 gint i, l;
156 gchar *pp;
158 /* look forward and find potential break points */
159 i = 0;
160 l = -1;
161 pp = p;
162 while (*pp && (i < MAX_HDR_LEN)) {
163 if ((*pp == ' ') || (*pp == '\t'))
164 l = i;
165 pp++;
166 i++;
167 }
168 if (!*pp)
169 l = pp - p; /* take rest, if EOS found */
171 if (l == -1) {
172 /* no potential break point was found within MAX_HDR_LEN so advance further until the next */
173 while (*pp && *pp != ' ' && *pp != '\t') {
174 pp++;
175 i++;
176 }
177 l = i;
178 }
180 /* copy */
181 i = 0;
182 while (i < l) {
183 *(q++) = *(p++);
184 i++;
185 }
186 *(q++) = '\n';
187 *(q++) = *(p++); /* this is either space, tab or 0 */
188 }
189 {
190 gchar *new_hdr;
192 g_free(hdr->header);
193 new_hdr = g_strdup(tmp_hdr);
194 g_free(tmp_hdr);
195 hdr->value = new_hdr + (hdr->value - hdr->header);
196 hdr->header = new_hdr;
197 }
198 }
200 header*
201 create_header(header_id id, gchar * fmt, ...)
202 {
203 gchar *p;
204 header *hdr;
205 va_list args;
206 va_start(args, fmt);
208 if ((hdr = g_malloc(sizeof(header)))) {
210 hdr->id = id;
211 hdr->header = g_strdup_vprintf(fmt, args);
212 hdr->value = NULL;
214 p = hdr->header;
215 while (*p && *p != ':')
216 p++;
217 if (*p)
218 hdr->value = p + 1;
219 }
221 va_end(args);
222 return hdr;
223 }
225 void
226 destroy_header(header * hdr)
227 {
228 if (hdr) {
229 if (hdr->header)
230 g_free(hdr->header);
231 g_free(hdr);
232 }
233 }
235 header*
236 copy_header(header * hdr)
237 {
238 header *new_hdr = NULL;
240 if (hdr) {
241 if ((new_hdr = g_malloc(sizeof(header)))) {
242 new_hdr->id = hdr->id;
243 new_hdr->header = g_strdup(hdr->header);
244 new_hdr->value = new_hdr->header + (hdr->value - hdr->header);
245 }
246 }
247 return new_hdr;
248 }
250 header*
251 get_header(gchar * line)
252 {
253 gchar *p = line;
254 gchar buf[64], *q = buf;
255 gint i;
256 header *hdr;
258 while (*p && (*p != ':') && (q < buf + 63))
259 *(q++) = *(p++);
260 *q = 0;
262 if (*p != ':')
263 return NULL;
265 hdr = g_malloc(sizeof(header));
267 hdr->value = NULL;
268 p++;
270 while (*p && (*p == ' ' || *p == '\t'))
271 p++;
272 hdr->value = p;
274 for (i = 0; i < HEAD_NUM_IDS; i++) {
275 if (strcasecmp(header_names[i].header, buf) == 0)
276 break;
277 }
278 hdr->id = (header_id) i;
279 hdr->header = g_strdup(line);
280 hdr->value = hdr->header + (hdr->value - line);
282 DEBUG(4) debugf("header: %d = %s", hdr->id, hdr->header);
284 return hdr;
285 }