comparison src/prepstdreply.c @ 745:5edfa9eef6c7

Add $random0$ through $random5$ substitutions
author Ben Schmidt
date Mon, 20 Sep 2010 02:31:03 +1000
parents b00eb39643c1
children ecb991e41a4c
comparison
equal deleted inserted replaced
744:926f184eec44 745:5edfa9eef6c7
192 size_t i, len; 192 size_t i, len;
193 int infd, outfd, mailfd; 193 int infd, outfd, mailfd;
194 char *listaddr, *listdelim, *tmp, *retstr = NULL; 194 char *listaddr, *listdelim, *tmp, *retstr = NULL;
195 char *listfqdn, *line, *utfline, *utfsub, *utfsub2; 195 char *listfqdn, *line, *utfline, *utfsub, *utfsub2;
196 char *str = NULL; 196 char *str = NULL;
197 char **moredata;
197 char *headers[10] = { NULL }; /* relies on NULL to flag end */ 198 char *headers[10] = { NULL }; /* relies on NULL to flag end */
198 199
199 if ((infd = open_listtext(listdir, filename)) < 0) { 200 if ((infd = open_listtext(listdir, filename)) < 0) {
200 return NULL; 201 return NULL;
201 } 202 }
204 listdelim = getlistdelim(listdir); 205 listdelim = getlistdelim(listdir);
205 listfqdn = genlistfqdn(listaddr); 206 listfqdn = genlistfqdn(listaddr);
206 207
207 do { 208 do {
208 tmp = random_str(); 209 tmp = random_str();
209 myfree(retstr); 210 if (retstr)
211 myfree(retstr);
210 retstr = concatstr(3, listdir, "/queue/", tmp); 212 retstr = concatstr(3, listdir, "/queue/", tmp);
211 myfree(tmp); 213 myfree(tmp);
212 214
213 outfd = open(retstr, O_RDWR|O_CREAT|O_EXCL, S_IRUSR|S_IWUSR); 215 outfd = open(retstr, O_RDWR|O_CREAT|O_EXCL, S_IRUSR|S_IWUSR);
214 216
217 if(outfd < 0) { 219 if(outfd < 0) {
218 log_error(LOG_ARGS, "Could not open std mail %s", retstr); 220 log_error(LOG_ARGS, "Could not open std mail %s", retstr);
219 myfree(listaddr); 221 myfree(listaddr);
220 myfree(listdelim); 222 myfree(listdelim);
221 myfree(listfqdn); 223 myfree(listfqdn);
224 myfree(retstr);
222 return NULL; 225 return NULL;
223 } 226 }
224 227
228 moredata = mymalloc(2*(tokencount+6) * sizeof(char *));
229 for (i=0; i<2*tokencount; i++) {
230 moredata[i] = data[i];
231 }
232 for (i=0; i<6; i++) {
233 moredata[2*(tokencount+i)] = mystrdup("randomN");
234 moredata[2*(tokencount+i)][6] = '0' + i;
235 moredata[2*(tokencount+i)+1] = random_str();
236 }
237 tokencount += 6;
238
225 tmp = substitute(from, listaddr, listdelim, 239 tmp = substitute(from, listaddr, listdelim,
226 tokencount, data); 240 tokencount, moredata);
227 headers[0] = concatstr(2, "From: ", tmp); 241 headers[0] = concatstr(2, "From: ", tmp);
228 myfree(tmp); 242 myfree(tmp);
229 tmp = substitute(to, listaddr, listdelim, 243 tmp = substitute(to, listaddr, listdelim,
230 tokencount, data); 244 tokencount, moredata);
231 headers[1] = concatstr(2, "To: ", tmp); 245 headers[1] = concatstr(2, "To: ", tmp);
232 myfree(tmp); 246 myfree(tmp);
233 headers[2] = genmsgid(listfqdn); 247 headers[2] = genmsgid(listfqdn);
234 chomp(headers[2]); 248 chomp(headers[2]);
235 headers[3] = gendatestr(); 249 headers[3] = gendatestr();
239 headers[6] = mystrdup("Content-Type: text/plain; charset=utf-8"); 253 headers[6] = mystrdup("Content-Type: text/plain; charset=utf-8");
240 headers[7] = mystrdup("Content-Transfer-Encoding: 8bit"); 254 headers[7] = mystrdup("Content-Transfer-Encoding: 8bit");
241 255
242 if(replyto) { 256 if(replyto) {
243 tmp = substitute(replyto, listaddr, listdelim, 257 tmp = substitute(replyto, listaddr, listdelim,
244 tokencount, data); 258 tokencount, moredata);
245 headers[8] = concatstr(2, "Reply-To: ", tmp); 259 headers[8] = concatstr(2, "Reply-To: ", tmp);
246 myfree(tmp); 260 myfree(tmp);
247 } 261 }
248 262
249 for(;;) { 263 for(;;) {
263 if (*line == ' ' || *line == '\t') { 277 if (*line == ' ' || *line == '\t') {
264 /* line beginning with linear whitespace is a 278 /* line beginning with linear whitespace is a
265 continuation of previous header line */ 279 continuation of previous header line */
266 utfsub = unistr_escaped_to_utf8(line); 280 utfsub = unistr_escaped_to_utf8(line);
267 str = substitute(utfsub, listaddr, listdelim, 281 str = substitute(utfsub, listaddr, listdelim,
268 tokencount, data); 282 tokencount, moredata);
269 myfree(utfsub); 283 myfree(utfsub);
270 len = strlen(str); 284 len = strlen(str);
271 str[len] = '\n'; 285 str[len] = '\n';
272 if(writen(outfd, str, len+1) < 0) { 286 if(writen(outfd, str, len+1) < 0) {
273 log_error(LOG_ARGS, "Could not write std mail"); 287 log_error(LOG_ARGS, "Could not write std mail");
274 myfree(str); 288 myfree(str);
275 myfree(line); 289 myfree(line);
276 myfree(listaddr); 290 myfree(retstr);
277 myfree(listdelim); 291 retstr = NULL;
278 myfree(listfqdn); 292 goto freeandreturn;
279 return NULL;
280 } 293 }
281 myfree(str); 294 myfree(str);
282 } else { 295 } else {
283 tmp = line; 296 tmp = line;
284 len = 0; 297 len = 0;
306 } 319 }
307 } 320 }
308 utfsub = unistr_escaped_to_utf8(tmp); 321 utfsub = unistr_escaped_to_utf8(tmp);
309 *tmp = '\0'; 322 *tmp = '\0';
310 utfsub2 = substitute(utfsub, listaddr, listdelim, 323 utfsub2 = substitute(utfsub, listaddr, listdelim,
311 tokencount, data); 324 tokencount, moredata);
312 myfree(utfsub); 325 myfree(utfsub);
313 if (strncasecmp(line, "Subject:", len) == 0) { 326 if (strncasecmp(line, "Subject:", len) == 0) {
314 tmp = unistr_utf8_to_header(utfsub2); 327 tmp = unistr_utf8_to_header(utfsub2);
315 myfree(utfsub2); 328 myfree(utfsub2);
316 str = concatstr(2, line, tmp); 329 str = concatstr(2, line, tmp);
323 str[len] = '\n'; 336 str[len] = '\n';
324 if(writen(outfd, str, len+1) < 0) { 337 if(writen(outfd, str, len+1) < 0) {
325 log_error(LOG_ARGS, "Could not write std mail"); 338 log_error(LOG_ARGS, "Could not write std mail");
326 myfree(str); 339 myfree(str);
327 myfree(line); 340 myfree(line);
328 myfree(listaddr); 341 myfree(retstr);
329 myfree(listdelim); 342 retstr = NULL;
330 myfree(listfqdn); 343 goto freeandreturn;
331 return NULL;
332 } 344 }
333 myfree(str); 345 myfree(str);
334 } 346 }
335 myfree(line); 347 myfree(line);
336 } 348 }
341 if(writen(outfd, headers[i], len+1) < 0) { 353 if(writen(outfd, headers[i], len+1) < 0) {
342 log_error(LOG_ARGS, "Could not write std mail"); 354 log_error(LOG_ARGS, "Could not write std mail");
343 if (line) 355 if (line)
344 myfree(line); 356 myfree(line);
345 myfree(str); 357 myfree(str);
346 myfree(listaddr); 358 myfree(retstr);
347 myfree(listdelim); 359 retstr = NULL;
348 myfree(listfqdn); 360 goto freeandreturn;
349 return NULL;
350 } 361 }
351 } 362 }
352 363
353 /* end the headers */ 364 /* end the headers */
354 if(writen(outfd, "\n", 1) < 0) { 365 if(writen(outfd, "\n", 1) < 0) {
355 log_error(LOG_ARGS, "Could not write std mail"); 366 log_error(LOG_ARGS, "Could not write std mail");
356 myfree(str); 367 myfree(str);
357 if (line) 368 if (line)
358 myfree(line); 369 myfree(line);
359 myfree(listaddr); 370 myfree(retstr);
360 myfree(listdelim); 371 retstr = NULL;
361 myfree(listfqdn); 372 goto freeandreturn;
362 return NULL;
363 } 373 }
364 374
365 if (line) { 375 if (line) {
366 str = concatstr(2, line, "\n"); 376 str = concatstr(2, line, "\n");
367 myfree(line); 377 myfree(line);
397 str = concatstr(2,utfline,str); 407 str = concatstr(2,utfline,str);
398 myfree(tmp); 408 myfree(tmp);
399 if(writen(outfd,str,strlen(str)) < 0) { 409 if(writen(outfd,str,strlen(str)) < 0) {
400 myfree(str); 410 myfree(str);
401 myfree(utfline); 411 myfree(utfline);
402 myfree(listaddr);
403 myfree(listdelim);
404 myfree(listfqdn);
405 log_error(LOG_ARGS, "Could not write std mail"); 412 log_error(LOG_ARGS, "Could not write std mail");
406 return NULL; 413 myfree(retstr);
414 retstr = NULL;
415 goto freeandreturn;
407 } 416 }
408 myfree(str); 417 myfree(str);
409 i++; 418 i++;
410 } 419 }
411 close(mailfd); 420 close(mailfd);
416 log_error(LOG_ARGS, "Bad $originalmailNNN$ substitution"); 425 log_error(LOG_ARGS, "Bad $originalmailNNN$ substitution");
417 } 426 }
418 myfree(utfline); 427 myfree(utfline);
419 } else { 428 } else {
420 str = substitute(utfline, listaddr, listdelim, 429 str = substitute(utfline, listaddr, listdelim,
421 tokencount, data); 430 tokencount, moredata);
422 myfree(utfline); 431 myfree(utfline);
423 if(writen(outfd, str, strlen(str)) < 0) { 432 if(writen(outfd, str, strlen(str)) < 0) {
424 myfree(str); 433 myfree(str);
425 myfree(listaddr);
426 myfree(listdelim);
427 myfree(listfqdn);
428 log_error(LOG_ARGS, "Could not write std mail"); 434 log_error(LOG_ARGS, "Could not write std mail");
429 return NULL; 435 myfree(retstr);
436 retstr = NULL;
437 goto freeandreturn;
430 } 438 }
431 myfree(str); 439 myfree(str);
432 } 440 }
433 441
434 str = mygetline(infd); 442 str = mygetline(infd);
435 } 443 }
436 444
437 fsync(outfd); 445 fsync(outfd);
438 close(outfd); 446 close(outfd);
439 447
448 freeandreturn:
440 myfree(listaddr); 449 myfree(listaddr);
441 myfree(listdelim); 450 myfree(listdelim);
442 myfree(listfqdn); 451 myfree(listfqdn);
443 452
453 for (i=tokencount-6; i<tokencount; i++) {
454 myfree(moredata[2*i]);
455 myfree(moredata[2*i+1]);
456 }
457 myfree(moredata);
458
444 return retstr; 459 return retstr;
445 } 460 }