masqmail

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