masqmail

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