diff options
author | Sergey Poznyakoff <gray@gnu.org.ua> | 2010-09-16 15:32:56 +0300 |
---|---|---|
committer | Sergey Poznyakoff <gray@gnu.org.ua> | 2010-09-16 15:50:40 +0300 |
commit | 23321cf7f5561b96d8bf970da217c8968e0175c8 (patch) | |
tree | 81155fcc329c7a3fe0865c95475a00d87be08ef7 | |
parent | 546a1c78efa1bad5536d94e4722d6ac2ef62a685 (diff) | |
download | mailutils-23321cf7f5561b96d8bf970da217c8968e0175c8.tar.gz mailutils-23321cf7f5561b96d8bf970da217c8968e0175c8.tar.bz2 |
If a message cannot be sent, save it in dead.mail.
* mail/send.c (msg_to_pipe): Return status code.
(save_dead_message, send_message): New functions, extracted from
mail_send0.
(mail_send0): Call save_dead_message if sending failed.
-rw-r--r-- | mail/send.c | 192 |
1 files changed, 113 insertions, 79 deletions
diff --git a/mail/send.c b/mail/send.c index 6538a49c2..71d7b0325 100644 --- a/mail/send.c +++ b/mail/send.c | |||
@@ -23,7 +23,7 @@ | |||
23 | #include <fcntl.h> | 23 | #include <fcntl.h> |
24 | 24 | ||
25 | static int isfilename (const char *); | 25 | static int isfilename (const char *); |
26 | static void msg_to_pipe (const char *cmd, mu_message_t msg); | 26 | static int msg_to_pipe (const char *cmd, mu_message_t msg); |
27 | 27 | ||
28 | 28 | ||
29 | /* Additional message headers */ | 29 | /* Additional message headers */ |
@@ -335,6 +335,82 @@ fill_body (mu_message_t msg, FILE *file) | |||
335 | return 0; | 335 | return 0; |
336 | } | 336 | } |
337 | 337 | ||
338 | static int | ||
339 | save_dead_message (compose_env_t *env) | ||
340 | { | ||
341 | if (mailvar_get (NULL, "save", mailvar_type_boolean, 0) == 0) | ||
342 | { | ||
343 | FILE *fp = fopen (getenv ("DEAD"), | ||
344 | mailvar_get (NULL, "appenddeadletter", | ||
345 | mailvar_type_boolean, 0) == 0 ? | ||
346 | "a" : "w"); | ||
347 | |||
348 | if (!fp) | ||
349 | { | ||
350 | util_error (_("Cannot open file %s: %s"), getenv ("DEAD"), | ||
351 | strerror (errno)); | ||
352 | return 1; | ||
353 | } | ||
354 | else | ||
355 | { | ||
356 | char *buf = NULL; | ||
357 | size_t n; | ||
358 | rewind (env->file); | ||
359 | while (getline (&buf, &n, env->file) > 0) | ||
360 | fputs (buf, fp); | ||
361 | fclose (fp); | ||
362 | free (buf); | ||
363 | } | ||
364 | } | ||
365 | return 0; | ||
366 | } | ||
367 | |||
368 | static int | ||
369 | send_message (mu_message_t msg) | ||
370 | { | ||
371 | char *sendmail; | ||
372 | int status; | ||
373 | |||
374 | if (mailvar_get (&sendmail, "sendmail", mailvar_type_string, 0) == 0) | ||
375 | { | ||
376 | if (sendmail[0] == '/') | ||
377 | status = msg_to_pipe (sendmail, msg); | ||
378 | else | ||
379 | { | ||
380 | mu_mailer_t mailer; | ||
381 | |||
382 | status = mu_mailer_create (&mailer, sendmail); | ||
383 | if (status == 0) | ||
384 | { | ||
385 | if (mailvar_get (NULL, "verbose", mailvar_type_boolean, 0) == 0) | ||
386 | { | ||
387 | mu_debug_t debug = NULL; | ||
388 | mu_mailer_get_debug (mailer, &debug); | ||
389 | mu_debug_set_level (debug, | ||
390 | MU_DEBUG_LEVEL_UPTO (MU_DEBUG_PROT)); | ||
391 | } | ||
392 | status = mu_mailer_open (mailer, MU_STREAM_RDWR); | ||
393 | if (status == 0) | ||
394 | { | ||
395 | status = mu_mailer_send_message (mailer, msg, NULL, NULL); | ||
396 | mu_mailer_close (mailer); | ||
397 | } | ||
398 | else | ||
399 | util_error (_("Cannot open mailer: %s"), mu_strerror (status)); | ||
400 | mu_mailer_destroy (&mailer); | ||
401 | } | ||
402 | else | ||
403 | util_error (_("Cannot create mailer: %s"), | ||
404 | mu_strerror (status)); | ||
405 | } | ||
406 | } | ||
407 | else | ||
408 | { | ||
409 | util_error (_("Variable sendmail not set: no mailer")); | ||
410 | status = ENOSYS; | ||
411 | } | ||
412 | return status; | ||
413 | } | ||
338 | 414 | ||
339 | /* mail_send0(): shared between mail_send() and mail_reply(); | 415 | /* mail_send0(): shared between mail_send() and mail_reply(); |
340 | 416 | ||
@@ -468,41 +544,16 @@ mail_send0 (compose_env_t * env, int save_to) | |||
468 | free (buf); | 544 | free (buf); |
469 | } | 545 | } |
470 | 546 | ||
471 | /* If interrupted dump the file to dead.letter. */ | 547 | /* If interrupted, dump the file to dead.letter. */ |
472 | if (int_cnt) | 548 | if (int_cnt) |
473 | { | 549 | { |
474 | if (mailvar_get (NULL, "save", mailvar_type_boolean, 0) == 0) | 550 | save_dead_message (env); |
475 | { | ||
476 | FILE *fp = fopen (getenv ("DEAD"), | ||
477 | mailvar_get (NULL, "appenddeadletter", | ||
478 | mailvar_type_boolean, 0) == 0 ? | ||
479 | "a" : "w"); | ||
480 | |||
481 | if (!fp) | ||
482 | { | ||
483 | util_error (_("Cannot open file %s: %s"), getenv ("DEAD"), | ||
484 | strerror (errno)); | ||
485 | } | ||
486 | else | ||
487 | { | ||
488 | char *buf = NULL; | ||
489 | size_t n; | ||
490 | rewind (env->file); | ||
491 | while (getline (&buf, &n, env->file) > 0) | ||
492 | fputs (buf, fp); | ||
493 | fclose (fp); | ||
494 | free (buf); | ||
495 | } | ||
496 | } | ||
497 | |||
498 | fclose (env->file); | 551 | fclose (env->file); |
499 | remove (filename); | 552 | remove (filename); |
500 | free (filename); | 553 | free (filename); |
501 | return 1; | 554 | return 1; |
502 | } | 555 | } |
503 | 556 | ||
504 | fclose (env->file); /* FIXME: freopen would be better */ | ||
505 | |||
506 | /* In mailx compatibility mode, ask for Cc and Bcc after editing | 557 | /* In mailx compatibility mode, ask for Cc and Bcc after editing |
507 | the body of the message */ | 558 | the body of the message */ |
508 | if (mailvar_get (NULL, "mailx", mailvar_type_boolean, 0) == 0) | 559 | if (mailvar_get (NULL, "mailx", mailvar_type_boolean, 0) == 0) |
@@ -518,9 +569,9 @@ mail_send0 (compose_env_t * env, int save_to) | |||
518 | file = fopen (filename, "r"); | 569 | file = fopen (filename, "r"); |
519 | if (file != NULL) | 570 | if (file != NULL) |
520 | { | 571 | { |
521 | mu_mailer_t mailer; | ||
522 | mu_message_t msg = NULL; | 572 | mu_message_t msg = NULL; |
523 | int rc; | 573 | int rc; |
574 | int status = 0; | ||
524 | 575 | ||
525 | mu_message_create (&msg, NULL); | 576 | mu_message_create (&msg, NULL); |
526 | mu_message_set_header (msg, env->header, NULL); | 577 | mu_message_set_header (msg, env->header, NULL); |
@@ -559,11 +610,10 @@ mail_send0 (compose_env_t * env, int save_to) | |||
559 | { | 610 | { |
560 | /* Pipe to a cmd. */ | 611 | /* Pipe to a cmd. */ |
561 | if (env->outfiles[i][0] == '|') | 612 | if (env->outfiles[i][0] == '|') |
562 | msg_to_pipe (&(env->outfiles[i][1]), msg); | 613 | status = msg_to_pipe (env->outfiles[i] + 1, msg); |
563 | /* Save to a file. */ | 614 | /* Save to a file. */ |
564 | else | 615 | else |
565 | { | 616 | { |
566 | int status; | ||
567 | mu_mailbox_t mbx = NULL; | 617 | mu_mailbox_t mbx = NULL; |
568 | status = mu_mailbox_create_default (&mbx, | 618 | status = mu_mailbox_create_default (&mbx, |
569 | env->outfiles[i]); | 619 | env->outfiles[i]); |
@@ -583,63 +633,36 @@ mail_send0 (compose_env_t * env, int save_to) | |||
583 | } | 633 | } |
584 | if (status) | 634 | if (status) |
585 | util_error (_("Cannot create mailbox %s: %s"), | 635 | util_error (_("Cannot create mailbox %s: %s"), |
586 | env->outfiles[i], mu_strerror (status)); | 636 | env->outfiles[i], |
637 | mu_strerror (status)); | ||
587 | } | 638 | } |
588 | } | 639 | } |
589 | } | 640 | } |
590 | 641 | ||
591 | /* Do we need to Send the message on the wire? */ | 642 | /* Do we need to Send the message on the wire? */ |
592 | if (compose_header_get (env, MU_HEADER_TO, NULL) | 643 | if (status == 0 && |
593 | || compose_header_get (env, MU_HEADER_CC, NULL) | 644 | (compose_header_get (env, MU_HEADER_TO, NULL) || |
594 | || compose_header_get (env, MU_HEADER_BCC, NULL)) | 645 | compose_header_get (env, MU_HEADER_CC, NULL) || |
646 | compose_header_get (env, MU_HEADER_BCC, NULL))) | ||
595 | { | 647 | { |
596 | char *sendmail; | 648 | status = send_message (msg); |
597 | if (mailvar_get (&sendmail, "sendmail", | 649 | if (status) |
598 | mailvar_type_string, 0) == 0) | 650 | save_dead_message (env); |
599 | { | ||
600 | if (sendmail[0] == '/') | ||
601 | msg_to_pipe (sendmail, msg); | ||
602 | else | ||
603 | { | ||
604 | int status = mu_mailer_create (&mailer, sendmail); | ||
605 | if (status == 0) | ||
606 | { | ||
607 | if (mailvar_get (NULL, "verbose", | ||
608 | mailvar_type_boolean, 0) == 0) | ||
609 | { | ||
610 | mu_debug_t debug = NULL; | ||
611 | mu_mailer_get_debug (mailer, &debug); | ||
612 | mu_debug_set_level (debug, | ||
613 | MU_DEBUG_LEVEL_UPTO (MU_DEBUG_PROT)); | ||
614 | } | ||
615 | status = mu_mailer_open (mailer, MU_STREAM_RDWR); | ||
616 | if (status == 0) | ||
617 | { | ||
618 | mu_mailer_send_message (mailer, msg, | ||
619 | NULL, NULL); | ||
620 | mu_mailer_close (mailer); | ||
621 | } | ||
622 | else | ||
623 | util_error (_("Cannot open mailer: %s"), | ||
624 |