Skip to content

Commit

Permalink
LMTP:
Browse files Browse the repository at this point in the history
* change parameters order and make recipients and retcodes a clist
* retcodes are optional now
* return the error code of the last error
* updated example in smtpsend
  • Loading branch information
matthias-lay committed Apr 19, 2017
1 parent 07296ae commit 3f184b2
Show file tree
Hide file tree
Showing 3 changed files with 61 additions and 26 deletions.
58 changes: 45 additions & 13 deletions src/low-level/smtp/mailsmtp.c
Original file line number Diff line number Diff line change
Expand Up @@ -448,31 +448,63 @@ int mailsmtp_data_message(mailsmtp * session,
}
}

int mailsmtp_status(int smtpstatus)
{
switch(smtpstatus) {
case 250:
return MAILSMTP_NO_ERROR;

case 552:
return MAILSMTP_ERROR_EXCEED_STORAGE_ALLOCATION;

case 554:
return MAILSMTP_ERROR_TRANSACTION_FAILED;

case 451:
return MAILSMTP_ERROR_IN_PROCESSING;

case 452:
return MAILSMTP_ERROR_INSUFFICIENT_SYSTEM_STORAGE;

case 0:
return MAILSMTP_ERROR_STREAM;

default:
return MAILSMTP_ERROR_UNEXPECTED_CODE;
}

}

/* lmtp data command
* on end of data transaction lmtp return a status for
* every recipient it delivered. the status is stored in
* a carray retcodes. the array must have the size of recips */
* on the end of data transaction lmtp returns a status for
* every recipient it delivered.
* recipient_list holds the rcpts from smtp_rcpt()
* the last error is returned to the caller
* if retcodes is not NULL, the individual rcpt-stati are stored there
* */
int maillmtp_data_message(mailsmtp * session,
carray * retcodes,
const char * message,
size_t size)
size_t size,
clist * recipient_list,
clist * retcodes)
{
int r;
int r, ret = MAILSMTP_NO_ERROR;
unsigned int i = 0;

if ((!retcodes) || (0 == carray_count(retcodes)))
return MAILSMTP_ERROR_IN_PROCESSING;
clistiter *iter;

r = send_data(session, message, size);

if (r == -1)
return MAILSMTP_ERROR_STREAM;

while (i < carray_count(retcodes)){
r = read_response(session);
carray_set(retcodes, i++, &r);
for (iter = clist_begin(recipient_list); iter; iter = clist_next(iter)) {
r = read_response(session);
if (MAILSMTP_NO_ERROR != mailsmtp_status(r))
ret = mailsmtp_status(r);
if (retcodes)
clist_append(retcodes, r);
}
return MAILSMTP_NO_ERROR;
return ret;
}

int mailsmtp_data_message_quit(mailsmtp * session,
Expand Down
8 changes: 4 additions & 4 deletions src/low-level/smtp/mailsmtp.h
Original file line number Diff line number Diff line change
Expand Up @@ -107,10 +107,10 @@ int mailsmtp_data_message(mailsmtp * session,

LIBETPAN_EXPORT
int maillmtp_data_message(mailsmtp * session,
carray *ret,
const char * message,
size_t size);

const char * message,
size_t size,
clist * recipient_list,
clist * retcodes);

LIBETPAN_EXPORT
int mailsmtp_data_message_quit(mailsmtp * session,
Expand Down
21 changes: 12 additions & 9 deletions tests/smtpsend.c
Original file line number Diff line number Diff line change
Expand Up @@ -146,12 +146,12 @@ void release(struct mem_message *message) {

int send_message(char *data, size_t len, char**rcpts) {
int s = -1;
int ret;
int ret, i;
char **r;
int esmtp = 0;
mailsmtp *smtp = NULL;
int recipcount = 0;
carray *retcodes = NULL;
clist *retcodes = NULL;
clist *recipients = clist_new();

if ((smtp = mailsmtp_new(0, NULL)) == NULL) {
perror("mailsmtp_new");
Expand Down Expand Up @@ -234,8 +234,9 @@ int send_message(char *data, size_t len, char**rcpts) {
mailsmtp_rcpt(smtp, *r))) != MAILSMTP_NO_ERROR) {
fprintf(stderr, "mailsmtp_rcpt: %s: %s\n", *r, mailsmtp_strerror(ret));
goto error;
}else{
clist_append(recipients, *r);
}
recipcount++;
}

/* message */
Expand All @@ -244,12 +245,13 @@ int send_message(char *data, size_t len, char**rcpts) {
goto error;
}
if (smtp_lmtp){
retcodes = carray_new(recipcount);
carray_set_size(retcodes, recipcount);
if ((ret = maillmtp_data_message(smtp, retcodes, data, len)) != MAILSMTP_NO_ERROR) {
fprintf(stderr, "maillmtp_data: %s\n", mailsmtp_strerror(ret));
retcodes = clist_new();
if ((ret = maillmtp_data_message(smtp, data, len, recipients, retcodes)) != MAILSMTP_NO_ERROR) {
fprintf(stderr, "maillmtp_data: %s (%d)\n", mailsmtp_strerror(ret), ret);
goto error;
}
for (i = 0; i < clist_count(retcodes); i++)
fprintf(stderr, "recipient %s returned: %d\n", clist_nth_data(recipients, i), clist_nth_data(retcodes, i));
} else if ((ret = mailsmtp_data_message(smtp, data, len)) != MAILSMTP_NO_ERROR) {
fprintf(stderr, "mailsmtp_data_message: %s\n", mailsmtp_strerror(ret));
goto error;
Expand All @@ -261,7 +263,8 @@ int send_message(char *data, size_t len, char**rcpts) {
if (smtp != NULL)
mailsmtp_free(smtp);
if (retcodes != NULL)
carray_free(retcodes);
clist_free(retcodes);
clist_free(recipients);
if (s >= 0)
close(s);
return -1;
Expand Down

0 comments on commit 3f184b2

Please sign in to comment.