Mercurial > masqmail
comparison 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 |
comparison
equal
deleted
inserted
replaced
300:d04894d0fc64 | 301:55c530a83d51 |
---|---|
47 struct tm *gmt; | 47 struct tm *gmt; |
48 | 48 |
49 memcpy(&local, t, sizeof(struct tm)); | 49 memcpy(&local, t, sizeof(struct tm)); |
50 gmt = gmtime(&now); | 50 gmt = gmtime(&now); |
51 diff_min = 60 * (local.tm_hour - gmt->tm_hour) + local.tm_min - gmt->tm_min; | 51 diff_min = 60 * (local.tm_hour - gmt->tm_hour) + local.tm_min - gmt->tm_min; |
52 if (local.tm_year != gmt->tm_year) | 52 if (local.tm_year != gmt->tm_year) { |
53 diff_min += (local.tm_year > gmt->tm_year) ? 1440 : -1440; | 53 diff_min += (local.tm_year > gmt->tm_year) ? 1440 : -1440; |
54 else if (local.tm_yday != gmt->tm_yday) | 54 } else if (local.tm_yday != gmt->tm_yday) { |
55 diff_min += (local.tm_yday > gmt->tm_yday) ? 1440 : -1440; | 55 diff_min += (local.tm_yday > gmt->tm_yday) ? 1440 : -1440; |
56 } | |
56 diff_hour = diff_min / 60; | 57 diff_hour = diff_min / 60; |
57 diff_min = abs(diff_min - diff_hour * 60); | 58 diff_min = abs(diff_min - diff_hour * 60); |
58 | 59 |
59 len = strftime(buf, sizeof(buf), "%a, ", &local); | 60 len = strftime(buf, sizeof(buf), "%a, ", &local); |
60 g_snprintf(buf + len, sizeof(buf) - len, "%02d ", local.tm_mday); | 61 g_snprintf(buf + len, sizeof(buf) - len, "%02d ", local.tm_mday); |
73 find_header(GList * hdr_list, header_id id, gchar * hdr_str) | 74 find_header(GList * hdr_list, header_id id, gchar * hdr_str) |
74 { | 75 { |
75 GList *found_list = NULL; | 76 GList *found_list = NULL; |
76 GList *node; | 77 GList *node; |
77 | 78 |
78 if ((id != HEAD_UNKNOWN) || (hdr_str == NULL)) { | 79 if ((id != HEAD_UNKNOWN) || !hdr_str) { |
79 foreach(hdr_list, node) { | 80 foreach(hdr_list, node) { |
80 header *hdr = (header *) (node->data); | 81 header *hdr = (header *) (node->data); |
81 if (hdr->id == id) | 82 if (hdr->id == id) { |
82 found_list = g_list_append(found_list, hdr); | 83 found_list = g_list_append(found_list, hdr); |
83 } | 84 } |
84 } else { | 85 } |
85 foreach(hdr_list, node) { | 86 return found_list; |
86 header *hdr = (header *) (node->data); | 87 } |
87 gchar buf[64], *q = buf, *p = hdr->header; | 88 |
88 | 89 foreach(hdr_list, node) { |
89 while (*p != ':' && q < buf + 63 && *p) | 90 header *hdr = (header *) (node->data); |
90 *(q++) = *(p++); | 91 gchar buf[64], *q = buf, *p = hdr->header; |
91 *q = '\0'; | 92 |
92 | 93 while (*p != ':' && q < buf+sizeof(buf)-1 && *p) { |
93 if (strcasecmp(buf, hdr_str) == 0) | 94 *(q++) = *(p++); |
94 found_list = g_list_append(found_list, hdr); | 95 } |
96 *q = '\0'; | |
97 | |
98 if (strcasecmp(buf, hdr_str) == 0) { | |
99 found_list = g_list_append(found_list, hdr); | |
95 } | 100 } |
96 } | 101 } |
97 return found_list; | 102 return found_list; |
98 } | 103 } |
99 | 104 |
103 gchar *tmp_hdr = g_malloc(strlen(hdr->header)); | 108 gchar *tmp_hdr = g_malloc(strlen(hdr->header)); |
104 gchar *p = hdr->header, *q = tmp_hdr; | 109 gchar *p = hdr->header, *q = tmp_hdr; |
105 gboolean flag = FALSE; | 110 gboolean flag = FALSE; |
106 | 111 |
107 while (*p) { | 112 while (*p) { |
108 if (*p != '\n') | 113 if (*p != '\n') { |
109 *(q++) = *p; | 114 *(q++) = *p; |
110 else | 115 } else { |
111 flag = TRUE; | 116 flag = TRUE; |
117 } | |
112 p++; | 118 p++; |
113 } | 119 } |
114 *(q++) = '\n'; | 120 *(q++) = '\n'; |
115 | 121 |
116 if (flag) { | 122 if (flag) { |
128 void | 134 void |
129 header_fold(header * hdr) | 135 header_fold(header * hdr) |
130 { | 136 { |
131 gint len = strlen(hdr->header); | 137 gint len = strlen(hdr->header); |
132 gchar *p, *q; | 138 gchar *p, *q; |
139 gchar *tmp_hdr; | |
140 int valueoffset; | |
141 | |
142 if (len < MAX_HDR_LEN) { | |
143 /* we don't need to do anything */ | |
144 return; | |
145 } | |
146 | |
147 /* the position in hdr->header where the value part starts */ | |
148 valueoffset = hdr->value - hdr->header; | |
149 | |
150 /* TODO: size is only calculated roughly */ | |
133 /* size is probably overestimated, but so we are on the safe side */ | 151 /* size is probably overestimated, but so we are on the safe side */ |
134 gchar *tmp_hdr = g_malloc(len + 2 * len / MAX_HDR_LEN); | 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); | |
135 | 155 |
136 p = hdr->header; | 156 p = hdr->header; |
137 q = tmp_hdr; | 157 q = tmp_hdr; |
138 | 158 |
139 if (p[len - 1] == '\n') | 159 if (p[len - 1] == '\n') { |
140 p[len - 1] = '\0'; | 160 p[len - 1] = '\0'; |
161 } | |
141 | 162 |
142 while (*p) { | 163 while (*p) { |
143 gint i, l; | 164 gint i, l; |
144 gchar *pp; | 165 gchar *pp; |
145 | 166 |
146 /* look forward and find potential break points */ | 167 /* look forward and find potential break points */ |
147 i = 0; | 168 i = 0; |
148 l = -1; | 169 l = -1; |
149 pp = p; | 170 pp = p; |
150 while (*pp && (i < MAX_HDR_LEN)) { | 171 while (*pp && (i < MAX_HDR_LEN)) { |
151 if ((*pp == ' ') || (*pp == '\t')) | 172 if ((*pp == ' ') || (*pp == '\t')) { |
152 l = i; | 173 l = i; |
174 } | |
153 pp++; | 175 pp++; |
154 i++; | 176 i++; |
155 } | 177 } |
156 if (!*pp) | 178 if (!*pp) { |
157 l = pp - p; /* take rest, if EOS found */ | 179 l = pp - p; /* take rest, if EOS found */ |
180 } | |
158 | 181 |
159 if (l == -1) { | 182 if (l == -1) { |
160 /* no potential break point was found within MAX_HDR_LEN so advance further until the next */ | 183 /* no potential break point was found within |
184 MAX_HDR_LEN so advance further until the next */ | |
161 while (*pp && *pp != ' ' && *pp != '\t') { | 185 while (*pp && *pp != ' ' && *pp != '\t') { |
162 pp++; | 186 pp++; |
163 i++; | 187 i++; |
164 } | 188 } |
165 l = i; | 189 l = i; |
171 *(q++) = *(p++); | 195 *(q++) = *(p++); |
172 i++; | 196 i++; |
173 } | 197 } |
174 *(q++) = '\n'; | 198 *(q++) = '\n'; |
175 *(q++) = *(p++); /* this is either space, tab or 0 */ | 199 *(q++) = *(p++); /* this is either space, tab or 0 */ |
176 } | 200 /* *(q++) = '\t'; */ |
177 { | 201 } |
178 gchar *new_hdr; | 202 |
179 | 203 g_free(hdr->header); |
180 g_free(hdr->header); | 204 hdr->header = tmp_hdr; |
181 new_hdr = g_strdup(tmp_hdr); | 205 hdr->value = hdr->header + valueoffset; |
182 g_free(tmp_hdr); | |
183 hdr->value = new_hdr + (hdr->value - hdr->header); | |
184 hdr->header = new_hdr; | |
185 } | |
186 } | 206 } |
187 | 207 |
188 header* | 208 header* |
189 create_header(header_id id, gchar * fmt, ...) | 209 create_header(header_id id, gchar * fmt, ...) |
190 { | 210 { |
191 gchar *p; | 211 gchar *p; |
192 header *hdr; | 212 header *hdr; |
193 va_list args; | 213 va_list args; |
194 va_start(args, fmt); | 214 va_start(args, fmt); |
195 | 215 |
196 if ((hdr = g_malloc(sizeof(header)))) { | 216 /* g_malloc() calls exit on failure */ |
197 | 217 hdr = g_malloc(sizeof(header)); |
198 hdr->id = id; | 218 |
199 hdr->header = g_strdup_vprintf(fmt, args); | 219 hdr->id = id; |
200 hdr->value = NULL; | 220 hdr->header = g_strdup_vprintf(fmt, args); |
201 | 221 hdr->value = NULL; |
202 p = hdr->header; | 222 |
203 while (*p && *p != ':') | 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') { | |
204 p++; | 229 p++; |
205 if (*p) | 230 } |
206 hdr->value = p + 1; | 231 hdr->value = (*p) ? p : NULL; |
207 } | 232 } |
233 | |
234 DEBUG(3) debugf("create_header(): hdr: `%s'\n", hdr->header); | |
235 DEBUG(3) debugf("create_header(): val: `%s'\n", hdr->value); | |
208 | 236 |
209 va_end(args); | 237 va_end(args); |
210 return hdr; | 238 return hdr; |
211 } | 239 } |
212 | 240 |
213 void | 241 void |
214 destroy_header(header * hdr) | 242 destroy_header(header * hdr) |
215 { | 243 { |
216 if (hdr) { | 244 if (hdr) { |
217 if (hdr->header) | 245 if (hdr->header) { |
218 g_free(hdr->header); | 246 g_free(hdr->header); |
247 } | |
219 g_free(hdr); | 248 g_free(hdr); |
220 } | 249 } |
221 } | 250 } |
222 | 251 |
223 header* | 252 header* |
224 copy_header(header * hdr) | 253 copy_header(header * hdr) |
225 { | 254 { |
226 header *new_hdr = NULL; | 255 header *new_hdr = NULL; |
227 | 256 |
228 if (hdr) { | 257 if (hdr) { |
229 if ((new_hdr = g_malloc(sizeof(header)))) { | 258 new_hdr = g_malloc(sizeof(header)); |
230 new_hdr->id = hdr->id; | 259 new_hdr->id = hdr->id; |
231 new_hdr->header = g_strdup(hdr->header); | 260 new_hdr->header = g_strdup(hdr->header); |
232 new_hdr->value = new_hdr->header + (hdr->value - hdr->header); | 261 new_hdr->value = new_hdr->header + (hdr->value - hdr->header); |
233 } | |
234 } | 262 } |
235 return new_hdr; | 263 return new_hdr; |
236 } | 264 } |
237 | 265 |
238 header* | 266 header* |
241 gchar *p = line; | 269 gchar *p = line; |
242 gchar buf[64], *q = buf; | 270 gchar buf[64], *q = buf; |
243 gint i; | 271 gint i; |
244 header *hdr; | 272 header *hdr; |
245 | 273 |
246 while (*p && (*p != ':') && (q < buf + 63)) | 274 while (*p && (*p != ':') && (q < buf+sizeof(buf)-1)) { |
247 *(q++) = *(p++); | 275 *(q++) = *(p++); |
276 } | |
248 *q = '\0'; | 277 *q = '\0'; |
249 | 278 |
250 if (*p != ':') | 279 if (*p != ':') { |
251 return NULL; | 280 return NULL; |
281 } | |
252 | 282 |
253 hdr = g_malloc(sizeof(header)); | 283 hdr = g_malloc(sizeof(header)); |
254 | 284 |
255 hdr->value = NULL; | 285 hdr->value = NULL; |
256 p++; | 286 p++; |
257 | 287 |
258 while (*p && (*p == ' ' || *p == '\t')) | 288 while (*p && (*p == ' ' || *p == '\t')) { |
259 p++; | 289 p++; |
290 } | |
260 hdr->value = p; | 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 */ | |
261 | 294 |
262 for (i = 0; i < HEAD_NUM_IDS; i++) { | 295 for (i = 0; i < HEAD_NUM_IDS; i++) { |
263 if (strcasecmp(header_names[i].header, buf) == 0) | 296 if (strcasecmp(header_names[i].header, buf) == 0) { |
264 break; | 297 break; |
298 } | |
265 } | 299 } |
266 hdr->id = (header_id) i; | 300 hdr->id = (header_id) i; |
267 hdr->header = g_strdup(line); | 301 hdr->header = g_strdup(line); |
268 hdr->value = hdr->header + (hdr->value - line); | 302 hdr->value = hdr->header + (hdr->value - line); |
269 | 303 |
270 DEBUG(4) debugf("header: %d = %s", hdr->id, hdr->header); | 304 DEBUG(4) debugf("header: %d = %s", hdr->id, hdr->header); |
305 /* Note: This only outputs the first line if the header is folded */ | |
271 | 306 |
272 return hdr; | 307 return hdr; |
273 } | 308 } |