comparison src/header.c @ 10:26e34ae9a3e3

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