Commit 21370dbf 21370dbf8bf74a3b02cdb50bf8c2dfef60642244 by Sergey Poznyakoff

Fixed indentation.

(smtp_address_add): New function. A wrapper around
 address_union.
(_smtp_set_rcpt): Rewritten based on address_union call.
1 parent 79b0dc3d
Showing 1 changed file with 101 additions and 124 deletions
...@@ -50,19 +50,18 @@ ...@@ -50,19 +50,18 @@
50 #include <mailer0.h> 50 #include <mailer0.h>
51 #include <registrar0.h> 51 #include <registrar0.h>
52 52
53 static struct _record _smtp_record = 53 static struct _record _smtp_record = {
54 {
55 MU_SMTP_SCHEME, 54 MU_SMTP_SCHEME,
56 _url_smtp_init, /* url init. */ 55 _url_smtp_init, /* url init. */
57 NULL, /* Mailbox init. */ 56 NULL, /* Mailbox init. */
58 &_mailer_smtp_init, /* Mailer init. */ 57 &_mailer_smtp_init, /* Mailer init. */
59 NULL, /* Folder init. */ 58 NULL, /* Folder init. */
60 NULL, /* No need for a back pointer. */ 59 NULL, /* No need for a back pointer. */
61 NULL, /* _is_scheme method. */ 60 NULL, /* _is_scheme method. */
62 NULL, /* _get_url method. */ 61 NULL, /* _get_url method. */
63 NULL, /* _get_mailbox method. */ 62 NULL, /* _get_mailbox method. */
64 NULL, /* _get_mailer method. */ 63 NULL, /* _get_mailer method. */
65 NULL /* _get_folder method. */ 64 NULL /* _get_folder method. */
66 }; 65 };
67 /* We export : url parsing and the initialisation of 66 /* We export : url parsing and the initialisation of
68 the mailbox, via the register entry/record. */ 67 the mailbox, via the register entry/record. */
...@@ -75,7 +74,7 @@ struct _smtp ...@@ -75,7 +74,7 @@ struct _smtp
75 char *localhost; 74 char *localhost;
76 75
77 /* IO buffering. */ 76 /* IO buffering. */
78 char *buffer; /* Must be freed. */ 77 char *buffer; /* Must be freed. */
79 size_t buflen; 78 size_t buflen;
80 79
81 char *ptr; 80 char *ptr;
...@@ -89,19 +88,20 @@ struct _smtp ...@@ -89,19 +88,20 @@ struct _smtp
89 SMTP_ENV_RCPT, SMTP_MAIL_FROM, SMTP_MAIL_FROM_ACK, SMTP_RCPT_TO, 88 SMTP_ENV_RCPT, SMTP_MAIL_FROM, SMTP_MAIL_FROM_ACK, SMTP_RCPT_TO,
90 SMTP_RCPT_TO_ACK, SMTP_DATA, SMTP_DATA_ACK, SMTP_SEND, SMTP_SEND_ACK, 89 SMTP_RCPT_TO_ACK, SMTP_DATA, SMTP_DATA_ACK, SMTP_SEND, SMTP_SEND_ACK,
91 SMTP_SEND_DOT 90 SMTP_SEND_DOT
92 } state; 91 }
92 state;
93 93
94 int extended; 94 int extended;
95 95
96 char* mail_from; 96 char *mail_from;
97 address_t rcpt_to; /* Destroy this if not the same as argto below. */ 97 address_t rcpt_to; /* Destroy this if not the same as argto below. */
98 address_t rcpt_bcc; 98 address_t rcpt_bcc;
99 size_t rcpt_to_count; 99 size_t rcpt_to_count;
100 size_t rcpt_bcc_count; 100 size_t rcpt_bcc_count;
101 size_t rcpt_index; 101 size_t rcpt_index;
102 size_t rcpt_count; 102 size_t rcpt_count;
103 int bccing; 103 int bccing;
104 message_t msg; /* Destroy this if not same argmsg. */ 104 message_t msg; /* Destroy this if not same argmsg. */
105 105
106 off_t offset; 106 off_t offset;
107 107
...@@ -111,7 +111,7 @@ struct _smtp ...@@ -111,7 +111,7 @@ struct _smtp
111 address_t argto; 111 address_t argto;
112 }; 112 };
113 113
114 typedef struct _smtp * smtp_t; 114 typedef struct _smtp *smtp_t;
115 115
116 static void smtp_destroy (mailer_t); 116 static void smtp_destroy (mailer_t);
117 static int smtp_open (mailer_t, int); 117 static int smtp_open (mailer_t, int);
...@@ -122,8 +122,8 @@ static int smtp_readline (smtp_t); ...@@ -122,8 +122,8 @@ static int smtp_readline (smtp_t);
122 static int smtp_read_ack (smtp_t); 122 static int smtp_read_ack (smtp_t);
123 static int smtp_write (smtp_t); 123 static int smtp_write (smtp_t);
124 124
125 static int _smtp_set_from (smtp_t, message_t , address_t); 125 static int _smtp_set_from (smtp_t, message_t, address_t);
126 static int _smtp_set_rcpt (smtp_t, message_t , address_t); 126 static int _smtp_set_rcpt (smtp_t, message_t, address_t);
127 127
128 /* Useful little macros, since these are very repetitive. */ 128 /* Useful little macros, since these are very repetitive. */
129 129
...@@ -173,9 +173,9 @@ CLEAR_STATE (smtp_t smtp) ...@@ -173,9 +173,9 @@ CLEAR_STATE (smtp_t smtp)
173 as that which is ongoing. Check this. */ 173 as that which is ongoing. Check this. */
174 static int 174 static int
175 smtp_check_send_resumption (smtp_t smtp, 175 smtp_check_send_resumption (smtp_t smtp,
176 message_t msg, address_t from, address_t to) 176 message_t msg, address_t from, address_t to)
177 { 177 {
178 if(smtp->state == SMTP_NO_STATE) 178 if (smtp->state == SMTP_NO_STATE)
179 return 0; 179 return 0;
180 180
181 /* FIXME: state should be one of the "send" states if its not 181 /* FIXME: state should be one of the "send" states if its not
...@@ -248,7 +248,7 @@ _mailer_smtp_init (mailer_t mailer) ...@@ -248,7 +248,7 @@ _mailer_smtp_init (mailer_t mailer)
248 if (mailer->data == NULL) 248 if (mailer->data == NULL)
249 return ENOMEM; 249 return ENOMEM;
250 250
251 smtp->mailer = mailer; /* Back pointer. */ 251 smtp->mailer = mailer; /* Back pointer. */
252 smtp->state = SMTP_NO_STATE; 252 smtp->state = SMTP_NO_STATE;
253 253
254 mailer->_destroy = smtp_destroy; 254 mailer->_destroy = smtp_destroy;
...@@ -267,11 +267,11 @@ _mailer_smtp_init (mailer_t mailer) ...@@ -267,11 +267,11 @@ _mailer_smtp_init (mailer_t mailer)
267 } 267 }
268 268
269 static void 269 static void
270 smtp_destroy(mailer_t mailer) 270 smtp_destroy (mailer_t mailer)
271 { 271 {
272 smtp_t smtp = mailer->data; 272 smtp_t smtp = mailer->data;
273 273
274 CLEAR_STATE(smtp); 274 CLEAR_STATE (smtp);
275 275
276 /* Not our responsability to close. */ 276 /* Not our responsability to close. */
277 277
...@@ -334,7 +334,7 @@ smtp_open (mailer_t mailer, int flags) ...@@ -334,7 +334,7 @@ smtp_open (mailer_t mailer, int flags)
334 } 334 }
335 /* Fetch our local host name. */ 335 /* Fetch our local host name. */
336 336
337 status = mu_get_host_name(&smtp->localhost); 337 status = mu_get_host_name (&smtp->localhost);
338 338
339 if (status != 0) 339 if (status != 0)
340 { 340 {
...@@ -346,20 +346,22 @@ smtp_open (mailer_t mailer, int flags) ...@@ -346,20 +346,22 @@ smtp_open (mailer_t mailer, int flags)
346 346
347 /* allocate a working io buffer. */ 347 /* allocate a working io buffer. */
348 if (smtp->buffer == NULL) 348 if (smtp->buffer == NULL)
349 { 349 {
350 smtp->buflen = 512; /* Initial guess. */ 350 smtp->buflen = 512; /* Initial guess. */
351 smtp->buffer = malloc (smtp->buflen + 1); 351 smtp->buffer = malloc (smtp->buflen + 1);
352 if (smtp->buffer == NULL) 352 if (smtp->buffer == NULL)
353 { 353 {
354 CHECK_ERROR (smtp, ENOMEM); 354 CHECK_ERROR (smtp, ENOMEM);
355 } 355 }
356 smtp->ptr = smtp->buffer; 356 smtp->ptr = smtp->buffer;
357 } 357 }
358 358
359 /* Create a TCP stack if one is not given. */ 359 /* Create a TCP stack if one is not given. */
360 if (mailer->stream == NULL) 360 if (mailer->stream == NULL)
361 { 361 {
362 status = tcp_stream_create (&mailer->stream, smtp->mailhost, port, mailer->flags); 362 status =
363 tcp_stream_create (&mailer->stream, smtp->mailhost, port,
364 mailer->flags);
363 CHECK_ERROR (smtp, status); 365 CHECK_ERROR (smtp, status);
364 stream_setbufsiz (mailer->stream, BUFSIZ); 366 stream_setbufsiz (mailer->stream, BUFSIZ);
365 } 367 }
...@@ -412,7 +414,7 @@ smtp_open (mailer_t mailer, int flags) ...@@ -412,7 +414,7 @@ smtp_open (mailer_t mailer, int flags)
412 } 414 }
413 415
414 case SMTP_HELO: 416 case SMTP_HELO:
415 if (!smtp->extended) /* FIXME: this will always be false! */ 417 if (!smtp->extended) /* FIXME: this will always be false! */
416 { 418 {
417 status = smtp_write (smtp); 419 status = smtp_write (smtp);
418 CHECK_EAGAIN (smtp, status); 420 CHECK_EAGAIN (smtp, status);
...@@ -437,7 +439,7 @@ smtp_open (mailer_t mailer, int flags) ...@@ -437,7 +439,7 @@ smtp_open (mailer_t mailer, int flags)
437 break; 439 break;
438 } 440 }
439 441
440 CLEAR_STATE(smtp); 442 CLEAR_STATE (smtp);
441 443
442 return 0; 444 return 0;
443 } 445 }
...@@ -486,7 +488,7 @@ message_set_header_value (message_t msg, const char *field, const char *value) ...@@ -486,7 +488,7 @@ message_set_header_value (message_t msg, const char *field, const char *value)
486 } 488 }
487 489
488 static int 490 static int
489 message_has_bcc(message_t msg) 491 message_has_bcc (message_t msg)
490 { 492 {
491 int status; 493 int status;
492 header_t header = NULL; 494 header_t header = NULL;
...@@ -547,7 +549,7 @@ smtp_send_message (mailer_t mailer, message_t argmsg, address_t argfrom, ...@@ -547,7 +549,7 @@ smtp_send_message (mailer_t mailer, message_t argmsg, address_t argfrom,
547 smtp_t smtp = NULL; 549 smtp_t smtp = NULL;
548 int status; 550 int status;
549 551
550 if(mailer == NULL) 552 if (mailer == NULL)
551 return EINVAL; 553 return EINVAL;
552 554
553 smtp = mailer->data; 555 smtp = mailer->data;
...@@ -572,7 +574,7 @@ smtp_send_message (mailer_t mailer, message_t argmsg, address_t argfrom, ...@@ -572,7 +574,7 @@ smtp_send_message (mailer_t mailer, message_t argmsg, address_t argfrom,
572 CHECK_ERROR (smtp, status); 574 CHECK_ERROR (smtp, status);
573 575
574 /* Clear the Bcc: field if we found one. */ 576 /* Clear the Bcc: field if we found one. */
575 if (message_has_bcc(smtp->argmsg)) 577 if (message_has_bcc (smtp->argmsg))
576 { 578 {
577 smtp->msg = NULL; 579 smtp->msg = NULL;
578 status = message_create_copy (&smtp->msg, smtp->argmsg); 580 status = message_create_copy (&smtp->msg, smtp->argmsg);
...@@ -630,11 +632,11 @@ smtp_send_message (mailer_t mailer, message_t argmsg, address_t argfrom, ...@@ -630,11 +632,11 @@ smtp_send_message (mailer_t mailer, message_t argmsg, address_t argfrom,
630 CHECK_ERROR (smtp, status); 632 CHECK_ERROR (smtp, status);
631 633
632 /* Add the Bcc: field back in for recipient. */ 634 /* Add the Bcc: field back in for recipient. */
633 if(smtp->bccing) 635 if (smtp->bccing)
634 { 636 {
635 status = message_set_header_value (smtp->msg, MU_HEADER_BCC, to); 637 status = message_set_header_value (smtp->msg, MU_HEADER_BCC, to);
636 CHECK_ERROR (smtp, status); 638 CHECK_ERROR (smtp, status);
637 } 639 }
638 640
639 status = smtp_writeline (smtp, "RCPT TO: %s\r\n", to); 641 status = smtp_writeline (smtp, "RCPT TO: %s\r\n", to);
640 642
...@@ -686,18 +688,20 @@ smtp_send_message (mailer_t mailer, message_t argmsg, address_t argfrom, ...@@ -686,18 +688,20 @@ smtp_send_message (mailer_t mailer, message_t argmsg, address_t argfrom,
686 smtp->offset = 0; 688 smtp->offset = 0;
687 smtp->state = SMTP_SEND; 689 smtp->state = SMTP_SEND;
688 690
689 if((smtp->mailer->flags & MAILER_FLAG_DEBUG_DATA) == 0) 691 if ((smtp->mailer->flags & MAILER_FLAG_DEBUG_DATA) == 0)
690 MAILER_DEBUG0 (smtp->mailer, MU_DEBUG_PROT, "> (data...)\n"); 692 MAILER_DEBUG0 (smtp->mailer, MU_DEBUG_PROT, "> (data...)\n");
691 693
692 case SMTP_SEND: 694 case SMTP_SEND:
693 { 695 {
694 stream_t stream; 696 stream_t stream;
695 size_t n = 0; 697 size_t n = 0;
696 char data[256] = ""; 698 char data[256] = "";
699
697 /* We may be here after an EAGAIN so check if we have something 700 /* We may be here after an EAGAIN so check if we have something
698 in the buffer and flush it. */ 701 in the buffer and flush it. */
699 status = smtp_write (smtp); 702 status = smtp_write (smtp);
700 CHECK_EAGAIN (smtp, status); 703 CHECK_EAGAIN (smtp, status);
704
701 message_get_stream (smtp->msg, &stream); 705 message_get_stream (smtp->msg, &stream);
702 while ((status = stream_readline (stream, data, sizeof (data) - 1, 706 while ((status = stream_readline (stream, data, sizeof (data) - 1,
703 smtp->offset, &n)) == 0 && n > 0) 707 smtp->offset, &n)) == 0 && n > 0)
...@@ -862,21 +866,29 @@ _smtp_set_from (smtp_t smtp, message_t msg, address_t from) ...@@ -862,21 +866,29 @@ _smtp_set_from (smtp_t smtp, message_t msg, address_t from)
862 return status; 866 return status;
863 } 867 }
864 868
869 int
870 smtp_address_add (address_t *paddr, const char *value)
871 {
872 address_t addr = NULL;
873 int status;
874
875 status = address_create (&addr, value);
876 if (status)
877 return status;
878 status = address_union (paddr, addr);
879 address_destroy (&addr);
880 return status;
881 }
882
865 static int 883 static int
866 _smtp_set_rcpt (smtp_t smtp, message_t msg, address_t to) 884 _smtp_set_rcpt (smtp_t smtp, message_t msg, address_t to)
867 { 885 {
868 int status = 0; 886 int status = 0;
869 header_t header = NULL; 887 header_t header = NULL;
870 char *toaddrv[3] = { NULL, NULL }; 888 char *value;
871 char *bccaddr = NULL; 889
872 address_t rcpt_to = NULL;
873 address_t rcpt_bcc = NULL;
874
875 /* Get RCPT_TO from TO, or the message. */ 890 /* Get RCPT_TO from TO, or the message. */
876 891
877 /* FIXME: even if there are TO addresses on the command-line, I have
878 to blank out the Bcc: header in the message!
879 */
880 if (to) 892 if (to)
881 { 893 {
882 /* Use the specified address_t. */ 894 /* Use the specified address_t. */
...@@ -889,103 +901,71 @@ _smtp_set_rcpt (smtp_t smtp, message_t msg, address_t to) ...@@ -889,103 +901,71 @@ _smtp_set_rcpt (smtp_t smtp, message_t msg, address_t to)
889 smtp->rcpt_to = to; 901 smtp->rcpt_to = to;
890 address_get_count (smtp->rcpt_to, &smtp->rcpt_to_count); 902 address_get_count (smtp->rcpt_to, &smtp->rcpt_to_count);
891 903
892 return status; 904 if (status)
905 return status;
893 } 906 }
894 907
895 if ((status = message_get_header (msg, &header))) 908 if ((status = message_get_header (msg, &header)))
896 return status; 909 return status;
897 910
898 status = header_aget_value (header, MU_HEADER_TO, toaddrv + 0); 911 status = header_aget_value (header, MU_HEADER_TO, &value);
899 912
900 if (status && status != ENOENT) 913 if (status == 0)
914 {
915 smtp_address_add (&smtp->rcpt_to, value);
916 free (value);
917 }
918 else if (status && status != ENOENT)
901 goto end; 919 goto end;
902 920
903 status = header_aget_value (header, MU_HEADER_CC, toaddrv + 1); 921 status = header_aget_value (header, MU_HEADER_CC, &value);
904 922
905 if (status && status != ENOENT) 923 if (status == 0)
924 {
925 smtp_address_add (&smtp->rcpt_to, value);
926 free (value);
927 }
928 else if (status && status != ENOENT)
906 goto end; 929 goto end;
907 930
908 status = header_aget_value (header, MU_HEADER_BCC, &bccaddr); 931 status = header_aget_value (header, MU_HEADER_BCC, &value);
909 932 if (status == 0)
910 if (status && status != ENOENT) 933 {
934 smtp_address_add (&smtp->rcpt_bcc, value);
935 free (value);
936 }
937 else if (status && status != ENOENT)
911 goto end; 938 goto end;
912 939
913 status = 0;
914
915 {
916 const char **av = (const char **) toaddrv;
917 int tostatus = address_createv (&rcpt_to, av, 2);
918
919 /* Any error other than EMPTY_ADDRESS is fatal */
920 if (tostatus)
921 {
922 if (tostatus == MU_ERR_EMPTY_ADDRESS)
923 tostatus = MU_ERR_MAILER_NO_RCPT_TO;
924 else
925 goto end;
926 }
927
928 if (!bccaddr)
929 status = MU_ERR_EMPTY_ADDRESS;
930 else
931 {
932 status = address_create (&rcpt_bcc, bccaddr);
933
934 if (status)
935 {
936 if (status == MU_ERR_EMPTY_ADDRESS)
937 status = MU_ERR_MAILER_NO_RCPT_TO;
938 else
939 goto end;
940 }
941 }
942
943 /* If both to and bcc are empty, it is fatal */
944 if (status == MU_ERR_EMPTY_ADDRESS && tostatus == status)
945 goto end;
946
947 /* otherwise, at least rcpt_to or rcpt_bcc is defined */
948 status = 0;
949 }
950
951 /* If to or bcc is present, the must be OK. */ 940 /* If to or bcc is present, the must be OK. */
952 if (rcpt_to && (status = mailer_check_to (rcpt_to))) 941 if (smtp->rcpt_to && (status = mailer_check_to (smtp->rcpt_to)))
953 goto end; 942 goto end;
954 943
955 if (rcpt_bcc && (status = mailer_check_to (rcpt_bcc))) 944 if (smtp->rcpt_bcc && (status = mailer_check_to (smtp->rcpt_bcc)))
956 goto end; 945 goto end;
957 946
958 end: 947 end:
959 948
960 if (toaddrv[0])
961 free (toaddrv[0]);
962
963 if (toaddrv[1])
964 free (toaddrv[1]);
965
966 if (bccaddr)
967 free (bccaddr);
968
969 if (status) 949 if (status)
970 { 950 {
971 address_destroy (&rcpt_to); 951 address_destroy (&smtp->rcpt_to);
972 address_destroy (&rcpt_bcc); 952 address_destroy (&smtp->rcpt_bcc);
973 } 953 }
974 else 954 else
975 { 955 {
976 smtp->rcpt_to = rcpt_to;
977
978 if (smtp->rcpt_to) 956 if (smtp->rcpt_to)
979 address_get_count (smtp->rcpt_to, &smtp->rcpt_to_count); 957 address_get_count (smtp->rcpt_to, &smtp->rcpt_to_count);
980 958
981 smtp->rcpt_bcc = rcpt_bcc;
982
983 if (smtp->rcpt_bcc) 959 if (smtp->rcpt_bcc)
984 address_get_count (smtp->rcpt_bcc, &smtp->rcpt_bcc_count); 960 address_get_count (smtp->rcpt_bcc, &smtp->rcpt_bcc_count);
961
962 if (smtp->rcpt_to_count + smtp->rcpt_bcc_count == 0)
963 status = MU_ERR_MAILER_NO_RCPT_TO;
985 } 964 }
986 965
987 return status; 966 return status;
988 } 967 }
968
989 /* C99 says that a conforming implementations of snprintf () 969 /* C99 says that a conforming implementations of snprintf ()
990 should return the number of char that would have been call 970 should return the number of char that would have been call
991 but many GNU/Linux && BSD implementations return -1 on error. 971 but many GNU/Linux && BSD implementations return -1 on error.
...@@ -1026,11 +1006,8 @@ smtp_writeline (smtp_t smtp, const char *format, ...) ...@@ -1026,11 +1006,8 @@ smtp_writeline (smtp_t smtp, const char *format, ...)
1026 while (len > 0 && isspace (smtp->buffer[len - 1])) 1006 while (len > 0 && isspace (smtp->buffer[len - 1]))
1027 len--; 1007 len--;
1028 1008
1029 if ( 1009 if ((smtp->state != SMTP_SEND && smtp->state != SMTP_SEND_DOT)
1030 (smtp->state != SMTP_SEND && smtp->state != SMTP_SEND_DOT) 1010 || smtp->mailer->flags & MAILER_FLAG_DEBUG_DATA)
1031 ||
1032 smtp->mailer->flags & MAILER_FLAG_DEBUG_DATA
1033 )
1034 { 1011 {
1035 MAILER_DEBUG2 (smtp->mailer, MU_DEBUG_PROT, "> %.*s\n", len, 1012 MAILER_DEBUG2 (smtp->mailer, MU_DEBUG_PROT, "> %.*s\n", len,
1036 smtp->buffer); 1013 smtp->buffer);
......