masqmail

annotate src/header.c @ 302:2ffcd38ccf53

improved unused function header_fold()
author markus schnalke <meillo@marmaro.de>
date Thu, 09 Dec 2010 17:50:25 -0300
parents 55c530a83d51
children 3e3c280ca5b2
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@302 108 char *src = hdr->header;
meillo@302 109 char *dest = src;
meillo@302 110 char *p;
meillo@0 111
meillo@302 112 p = strchr(src, '\n');
meillo@302 113 if (!p || !p[1]) {
meillo@302 114 /* no folded header */
meillo@302 115 return;
meillo@302 116 }
meillo@0 117
meillo@302 118 while (*src) {
meillo@302 119 if (*src == '\n') {
meillo@302 120 /* ignore */
meillo@302 121 src++;
meillo@302 122 } else {
meillo@302 123 /* copy */
meillo@302 124 *(dest++) = *(src++);
meillo@302 125 }
meillo@302 126 }
meillo@302 127 *(dest++) = '\n';
meillo@302 128 *(dest++) = '\0';
meillo@0 129 }
meillo@0 130
meillo@0 131 #define MAX_HDR_LEN 72
meillo@10 132 void
meillo@10 133 header_fold(header * hdr)
meillo@0 134 {
meillo@10 135 gint len = strlen(hdr->header);
meillo@10 136 gchar *p, *q;
meillo@301 137 gchar *tmp_hdr;
meillo@301 138 int valueoffset;
meillo@301 139
meillo@301 140 if (len < MAX_HDR_LEN) {
meillo@301 141 /* we don't need to do anything */
meillo@301 142 return;
meillo@301 143 }
meillo@301 144
meillo@301 145 /* the position in hdr->header where the value part starts */
meillo@301 146 valueoffset = hdr->value - hdr->header;
meillo@301 147
meillo@301 148 /* TODO: size is only calculated roughly */
meillo@10 149 /* size is probably overestimated, but so we are on the safe side */
meillo@301 150 /* (as much as we already have + chars inserted per break * number
meillo@301 151 of breaks + some more) */
meillo@301 152 tmp_hdr = g_malloc(len + 2 * (len/MAX_HDR_LEN) + 10);
meillo@0 153
meillo@10 154 p = hdr->header;
meillo@10 155 q = tmp_hdr;
meillo@0 156
meillo@301 157 if (p[len - 1] == '\n') {
meillo@15 158 p[len - 1] = '\0';
meillo@301 159 }
meillo@0 160
meillo@10 161 while (*p) {
meillo@10 162 gint i, l;
meillo@10 163 gchar *pp;
meillo@0 164
meillo@10 165 /* look forward and find potential break points */
meillo@10 166 i = 0;
meillo@10 167 l = -1;
meillo@10 168 pp = p;
meillo@10 169 while (*pp && (i < MAX_HDR_LEN)) {
meillo@301 170 if ((*pp == ' ') || (*pp == '\t')) {
meillo@10 171 l = i;
meillo@301 172 }
meillo@10 173 pp++;
meillo@10 174 i++;
meillo@10 175 }
meillo@301 176 if (!*pp) {
meillo@10 177 l = pp - p; /* take rest, if EOS found */
meillo@301 178 }
meillo@0 179
meillo@10 180 if (l == -1) {
meillo@301 181 /* no potential break point was found within
meillo@301 182 MAX_HDR_LEN so advance further until the next */
meillo@10 183 while (*pp && *pp != ' ' && *pp != '\t') {
meillo@10 184 pp++;
meillo@10 185 i++;
meillo@10 186 }
meillo@10 187 l = i;
meillo@10 188 }
meillo@10 189
meillo@10 190 /* copy */
meillo@10 191 i = 0;
meillo@10 192 while (i < l) {
meillo@10 193 *(q++) = *(p++);
meillo@10 194 i++;
meillo@10 195 }
meillo@10 196 *(q++) = '\n';
meillo@15 197 *(q++) = *(p++); /* this is either space, tab or 0 */
meillo@301 198 /* *(q++) = '\t'; */
meillo@10 199 }
meillo@10 200
meillo@301 201 g_free(hdr->header);
meillo@301 202 hdr->header = tmp_hdr;
meillo@301 203 hdr->value = hdr->header + valueoffset;
meillo@0 204 }
meillo@0 205
meillo@10 206 header*
meillo@10 207 create_header(header_id id, gchar * fmt, ...)
meillo@0 208 {
meillo@10 209 gchar *p;
meillo@10 210 header *hdr;
meillo@10 211 va_list args;
meillo@10 212 va_start(args, fmt);
meillo@0 213
meillo@301 214 /* g_malloc() calls exit on failure */
meillo@301 215 hdr = g_malloc(sizeof(header));
meillo@0 216
meillo@301 217 hdr->id = id;
meillo@301 218 hdr->header = g_strdup_vprintf(fmt, args);
meillo@301 219 hdr->value = NULL;
meillo@0 220
meillo@301 221 /* value shall point to the first non-whitespace char in the
meillo@301 222 value part of the header line (i.e. after the first colon) */
meillo@301 223 p = strchr(hdr->header, ':');
meillo@301 224 if (p) {
meillo@301 225 p++;
meillo@301 226 while (*p == ' ' || *p == '\t' || *p == '\n') {
meillo@10 227 p++;
meillo@301 228 }
meillo@301 229 hdr->value = (*p) ? p : NULL;
meillo@10 230 }
meillo@0 231
meillo@301 232 DEBUG(3) debugf("create_header(): hdr: `%s'\n", hdr->header);
meillo@301 233 DEBUG(3) debugf("create_header(): val: `%s'\n", hdr->value);
meillo@301 234
meillo@10 235 va_end(args);
meillo@10 236 return hdr;
meillo@0 237 }
meillo@0 238
meillo@10 239 void
meillo@10 240 destroy_header(header * hdr)
meillo@0 241 {
meillo@10 242 if (hdr) {
meillo@301 243 if (hdr->header) {
meillo@10 244 g_free(hdr->header);
meillo@301 245 }
meillo@10 246 g_free(hdr);
meillo@10 247 }
meillo@0 248 }
meillo@0 249
meillo@10 250 header*
meillo@10 251 copy_header(header * hdr)
meillo@0 252 {
meillo@10 253 header *new_hdr = NULL;
meillo@0 254
meillo@10 255 if (hdr) {
meillo@301 256 new_hdr = g_malloc(sizeof(header));
meillo@301 257 new_hdr->id = hdr->id;
meillo@301 258 new_hdr->header = g_strdup(hdr->header);
meillo@301 259 new_hdr->value = new_hdr->header + (hdr->value - hdr->header);
meillo@10 260 }
meillo@10 261 return new_hdr;
meillo@0 262 }
meillo@0 263
meillo@10 264 header*
meillo@10 265 get_header(gchar * line)
meillo@0 266 {
meillo@10 267 gchar *p = line;
meillo@10 268 gchar buf[64], *q = buf;
meillo@10 269 gint i;
meillo@10 270 header *hdr;
meillo@0 271
meillo@301 272 while (*p && (*p != ':') && (q < buf+sizeof(buf)-1)) {
meillo@10 273 *(q++) = *(p++);
meillo@301 274 }
meillo@15 275 *q = '\0';
meillo@0 276
meillo@301 277 if (*p != ':') {
meillo@10 278 return NULL;
meillo@301 279 }
meillo@0 280
meillo@10 281 hdr = g_malloc(sizeof(header));
meillo@0 282
meillo@10 283 hdr->value = NULL;
meillo@10 284 p++;
meillo@0 285
meillo@301 286 while (*p && (*p == ' ' || *p == '\t')) {
meillo@10 287 p++;
meillo@301 288 }
meillo@10 289 hdr->value = p;
meillo@301 290 /* Note: an empty value can also mean that it's only the first part
meillo@301 291 of a folded header line */
meillo@0 292
meillo@10 293 for (i = 0; i < HEAD_NUM_IDS; i++) {
meillo@301 294 if (strcasecmp(header_names[i].header, buf) == 0) {
meillo@10 295 break;
meillo@301 296 }
meillo@10 297 }
meillo@10 298 hdr->id = (header_id) i;
meillo@10 299 hdr->header = g_strdup(line);
meillo@10 300 hdr->value = hdr->header + (hdr->value - line);
meillo@10 301
meillo@235 302 DEBUG(4) debugf("header: %d = %s", hdr->id, hdr->header);
meillo@301 303 /* Note: This only outputs the first line if the header is folded */
meillo@10 304
meillo@10 305 return hdr;
meillo@0 306 }