Commit 956a3c65 956a3c65217d28de4ba7901906ce8f11277486a2 by Sergey Poznyakoff

Sent empty messages only if nullbody is set

1 parent 9740641b
Showing 1 changed file with 113 additions and 89 deletions
...@@ -307,6 +307,43 @@ compose_destroy (compose_env_t * env) ...@@ -307,6 +307,43 @@ compose_destroy (compose_env_t * env)
307 } 307 }
308 } 308 }
309 309
310 static int
311 fill_body (mu_message_t msg, FILE *file)
312 {
313 mu_body_t body = NULL;
314 mu_stream_t stream = NULL;
315 off_t offset = 0;
316 char *buf = NULL;
317 size_t n = 0;
318 mu_message_get_body (msg, &body);
319 mu_body_get_stream (body, &stream);
320
321 while (getline (&buf, &n, file) >= 0)
322 {
323 size_t len = strlen (buf);
324 mu_stream_write (stream, buf, len, offset, &n);
325 offset += len;
326 }
327
328 if (buf)
329 free (buf);
330
331 if (offset == 0)
332 {
333 if (util_getenv (NULL, "nullbody", Mail_env_boolean, 0) == 0)
334 {
335 char *str;
336 if (util_getenv (&str, "nullbodymsg", Mail_env_string, 0) == 0)
337 util_error ("%s\n", _(str));
338 }
339 else
340 return 1;
341 }
342
343 return 0;
344 }
345
346
310 /* mail_send0(): shared between mail_send() and mail_reply(); 347 /* mail_send0(): shared between mail_send() and mail_reply();
311 348
312 If the variable "record" is set, the outgoing message is 349 If the variable "record" is set, the outgoing message is
...@@ -492,123 +529,110 @@ mail_send0 (compose_env_t * env, int save_to) ...@@ -492,123 +529,110 @@ mail_send0 (compose_env_t * env, int save_to)
492 { 529 {
493 mu_mailer_t mailer; 530 mu_mailer_t mailer;
494 mu_message_t msg = NULL; 531 mu_message_t msg = NULL;
495 532 int rc;
533
496 mu_message_create (&msg, NULL); 534 mu_message_create (&msg, NULL);
497 535
498 mu_message_set_header (msg, env->header, NULL); 536 mu_message_set_header (msg, env->header, NULL);
499 537
500 /* Fill the body. */ 538 /* Fill the body. */
501 { 539 rc = fill_body (msg, file);
502 mu_body_t body = NULL;
503 mu_stream_t stream = NULL;
504 off_t offset = 0;
505 char *buf = NULL;
506 size_t n = 0;
507 mu_message_get_body (msg, &body);
508 mu_body_get_stream (body, &stream);
509 while (getline (&buf, &n, file) >= 0)
510 {
511 size_t len = strlen (buf);
512 mu_stream_write (stream, buf, len, offset, &n);
513 offset += len;
514 }
515
516 if (offset == 0)
517 util_error (_("Null message body; hope that's ok\n"));
518 if (buf)
519 free (buf);
520 }
521
522 fclose (file); 540 fclose (file);
523 541
524 /* Save outgoing message */ 542 if (rc == 0)
525 if (save_to)
526 { 543 {
527 char *tmp = compose_header_get (env, MU_HEADER_TO, NULL); 544 /* Save outgoing message */
528 mu_address_t addr = NULL; 545 if (save_to)
529
530 mu_address_create (&addr, tmp);
531 mu_address_aget_email (addr, 1, &savefile);
532 mu_address_destroy (&addr);
533 if (savefile)
534 { 546 {
535 char *p = strchr (savefile, '@'); 547 char *tmp = compose_header_get (env, MU_HEADER_TO, NULL);
536 if (p) 548 mu_address_t addr = NULL;
537 *p = 0; 549
550 mu_address_create (&addr, tmp);
551 mu_address_aget_email (addr, 1, &savefile);
552 mu_address_destroy (&addr);
553 if (savefile)
554 {
555 char *p = strchr (savefile, '@');
556 if (p)
557 *p = 0;
558 }
538 } 559 }
539 } 560 util_save_outgoing (msg, savefile);
540 util_save_outgoing (msg, savefile); 561 if (savefile)
541 if (savefile) 562 free (savefile);
542 free (savefile);
543 563
544 /* Check if we need to save the message to files or pipes. */ 564 /* Check if we need to save the message to files or pipes. */
545 if (env->outfiles) 565 if (env->outfiles)
546 {
547 int i;
548 for (i = 0; i < env->nfiles; i++)
549 { 566 {
550 /* Pipe to a cmd. */ 567 int i;
551 if (env->outfiles[i][0] == '|') 568 for (i = 0; i < env->nfiles; i++)
552 msg_to_pipe (&(env->outfiles[i][1]), msg);
553 /* Save to a file. */
554 else
555 { 569 {
556 int status; 570 /* Pipe to a cmd. */
557 mu_mailbox_t mbx = NULL; 571 if (env->outfiles[i][0] == '|')
558 status = mu_mailbox_create_default (&mbx, env->outfiles[i]); 572 msg_to_pipe (&(env->outfiles[i][1]), msg);
559 if (status == 0) 573 /* Save to a file. */
574 else
560 { 575 {
561 status = mu_mailbox_open (mbx, MU_STREAM_WRITE 576 int status;
562 | MU_STREAM_CREAT); 577 mu_mailbox_t mbx = NULL;
578 status = mu_mailbox_create_default (&mbx,
579 env->outfiles[i]);
563 if (status == 0) 580 if (status == 0)
564 { 581 {
565 status = mu_mailbox_append_message (mbx, msg); 582 status = mu_mailbox_open (mbx, MU_STREAM_WRITE
566 if (status) 583 | MU_STREAM_CREAT);
567 util_error (_("Cannot append message: %s"), 584 if (status == 0)
568 mu_strerror (status)); 585 {
569 mu_mailbox_close (mbx); 586 status = mu_mailbox_append_message (mbx, msg);
587 if (status)
588 util_error (_("Cannot append message: %s"),
589 mu_strerror (status));
590 mu_mailbox_close (mbx);
591 }
592 mu_mailbox_destroy (&mbx);
570 } 593 }
571 mu_mailbox_destroy (&mbx); 594 if (status)
595 util_error (_("Cannot create mailbox %s: %s"),
596 env->outfiles[i], mu_strerror (status));
572 } 597 }
573 if (status)
574 util_error (_("Cannot create mailbox %s: %s"),
575 env->outfiles[i], mu_strerror (status));
576 } 598 }
577 } 599 }
578 }
579 600
580 /* Do we need to Send the message on the wire? */ 601 /* Do we need to Send the message on the wire? */
581 if (compose_header_get (env, MU_HEADER_TO, NULL) 602 if (compose_header_get (env, MU_HEADER_TO, NULL)
582 || compose_header_get (env, MU_HEADER_CC, NULL) 603 || compose_header_get (env, MU_HEADER_CC, NULL)
583 || compose_header_get (env, MU_HEADER_BCC, NULL)) 604 || compose_header_get (env, MU_HEADER_BCC, NULL))
584 {
585 char *sendmail;
586 if (util_getenv (&sendmail, "sendmail", Mail_env_string, 0) == 0)
587 { 605 {
588 int status = mu_mailer_create (&mailer, sendmail); 606 char *sendmail;
589 if (status == 0) 607 if (util_getenv (&sendmail, "sendmail", Mail_env_string, 0)
608 == 0)
590 { 609 {
591 if (util_getenv (NULL, "verbose", Mail_env_boolean, 0) 610 int status = mu_mailer_create (&mailer, sendmail);
592 == 0)
593 {
594 mu_debug_t debug = NULL;
595 mu_mailer_get_debug (mailer, &debug);
596 mu_debug_set_level (debug,
597 MU_DEBUG_TRACE | MU_DEBUG_PROT);
598 }
599 status = mu_mailer_open (mailer, MU_STREAM_RDWR);
600 if (status == 0) 611 if (status == 0)
601 { 612 {
602 mu_mailer_send_message (mailer, msg, NULL, NULL); 613 if (util_getenv (NULL, "verbose", Mail_env_boolean, 0)
603 mu_mailer_close (mailer); 614 == 0)
615 {
616 mu_debug_t debug = NULL;
617 mu_mailer_get_debug (mailer, &debug);
618 mu_debug_set_level (debug,
619 MU_DEBUG_TRACE |
620 MU_DEBUG_PROT);
621 }
622 status = mu_mailer_open (mailer, MU_STREAM_RDWR);
623 if (status == 0)
624 {
625 mu_mailer_send_message (mailer, msg, NULL, NULL);
626 mu_mailer_close (mailer);
627 }
628 mu_mailer_destroy (&mailer);
604 } 629 }
605 mu_mailer_destroy (&mailer); 630 if (status != 0)
631 msg_to_pipe (sendmail, msg);
606 } 632 }
607 if (status != 0) 633 else
608 msg_to_pipe (sendmail, msg); 634 util_error (_("Variable sendmail not set: no mailer"));
609 } 635 }
610 else
611 util_error (_("Variable sendmail not set: no mailer"));
612 } 636 }
613 mu_message_destroy (&msg, NULL); 637 mu_message_destroy (&msg, NULL);
614 remove (filename); 638 remove (filename);
......