masqmail

annotate src/header.c @ 0:08114f7dcc23

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