Commit d431f390 d431f39038af88f281095b3330383d747f4578ae by Alain Magloire

massage the address parsing, still very week.

1 parent 5f77ecdc
......@@ -115,5 +115,6 @@ main(int argc, char **argv)
printf ("%s\n", buf);
}
mailbox_close (mbox);
mailbox_destroy (&mbox);
return 0;
}
......
SUBDIR = mailutils
pkginclude_HEADERS = \
address.h \
attribute.h \
auth.h \
body.h \
debug.h \
folder.h \
header.h \
iterator.h \
list.h \
......
......@@ -38,10 +38,14 @@ typedef struct _address *address_t;
extern int address_create __P ((address_t *, const char *));
extern void address_destroy __P ((address_t *));
extern int address_get_email __P ((address_t, char *, size_t, size_t *));
extern int address_get_personal __P ((address_t, char *, size_t, size_t *));
extern int address_get_comments __P ((address_t, char *, size_t, size_t *));
extern int address_get_address __P ((address_t, char *, size_t, size_t *));
extern int address_get_email __P ((address_t, size_t, char *,
size_t, size_t *));
extern int address_get_personal __P ((address_t, size_t, char *,
size_t, size_t *));
extern int address_get_comments __P ((address_t, size_t, char *,
size_t, size_t *));
extern int address_to_string __P ((address_t, char *, size_t, size_t *));
extern int address_get_count __P ((address_t, size_t *));
#ifdef _cplusplus
}
......
......@@ -12,6 +12,7 @@ lib_LTLIBRARIES = libmailbox.la
EXTRA_DIST = mbx_mboxscan.c
libmailbox_la_SOURCES = \
address.c \
attachment.c \
attribute.c \
auth.c \
......@@ -19,6 +20,7 @@ bio.c \
body.c \
debug.c \
file_stream.c \
folder.c \
header.c \
iterator.c \
list.c \
......
......@@ -125,8 +125,8 @@ gettoken (const char **ptr)
/* Note: This again as for header.c an awfull way of doing things.
Meaning I need a correct rfc822 Parser. This one does not
understand group. There is not doubt a better way to do this. */
int
address_create (address_t *paddress, const char *addr)
static int
address_parse (address_t *paddress, const char **paddr)
{
const char *p;
struct token *t, *tok, *last;
......@@ -134,18 +134,17 @@ address_create (address_t *paddress, const char *addr)
struct token *comments = NULL;
struct token *start_comments = NULL;
address_t address;
address_t head;
int in_comment = 0;
int status = 0;
tok = last = NULL;
address = calloc (1, sizeof (*address));
address = *paddress = calloc (1, sizeof (*address));
if (address == NULL)
return ENOMEM;
head = address;
/* Read address, remove comments right away. */
p = addr;
p = *paddr;
while ((t = gettoken(&p)) != NULL && (t->word[0] != ',' || in_comment))
{
if (t->word[0] == '(' || t->word[0] == ')' || in_comment)
......@@ -174,13 +173,8 @@ address_create (address_t *paddress, const char *addr)
last = t;
}
address->addr = strdup (addr);
if (address->addr == NULL)
{
address_destroy (&address);
status = ENOMEM;
goto freenodes;
}
if (t != NULL)
free (t);
/* Put extracted address into email */
t = brace ? brace->next : tok;
......@@ -275,8 +269,6 @@ address_create (address_t *paddress, const char *addr)
}
}
*paddress = head;
/* Free list of tokens. */
freenodes:
for (t = tok; t; t = last)
......@@ -290,6 +282,41 @@ address_create (address_t *paddress, const char *addr)
free (t);
}
*paddr = p;
return status;
}
int
address_create (address_t *paddress, const char *addr)
{
address_t address = NULL;
address_t current = NULL;
address_t head = NULL;
const char *p = addr;
int status = 0;
if (paddress == NULL)
return EINVAL;
if (addr)
{
while (*addr != 0)
{
status = address_parse (&address, &addr);
if (status == 0)
{
if (head == NULL)
{
head = address;
head->addr = strdup (p);
}
else
current->next = address;
current = address;
p = addr;
}
}
}
*paddress = head;
return status;
}
......@@ -318,43 +345,72 @@ address_destroy (address_t *paddress)
}
int
address_get_personal (address_t addr, char *buf, size_t len, size_t *n)
address_get_personal (address_t addr, size_t no, char *buf, size_t len,
size_t *n)
{
size_t i;
size_t i, j;
int status = EINVAL;
if (addr == NULL)
return EINVAL;
i = _cpystr (buf, addr->personal, len);
if (n)
*n = i;
return 0;
for (j = 1; addr; addr = addr->next, j++)
{
if (j == no)
{
i = _cpystr (buf, addr->personal, len);
if (n)
*n = i;
status = 0;
break;
}
}
return status;
}
int
address_get_comments (address_t addr, char *buf, size_t len, size_t *n)
address_get_comments (address_t addr, size_t no, char *buf, size_t len,
size_t *n)
{
size_t i;
size_t i, j;
int status = EINVAL;
if (addr == NULL)
return EINVAL;
i = _cpystr (buf, addr->comments, len);
if (n)
*n = i;
return 0;
for (j = 1; addr; addr = addr->next, j++)
{
if (j == no)
{
i = _cpystr (buf, addr->comments, len);
if (n)
*n = i;
status = 0;
break;
}
}
return status;
}
int
address_get_email (address_t addr, char *buf, size_t len, size_t *n)
address_get_email (address_t addr, size_t no, char *buf, size_t len, size_t *n)
{
size_t i;
size_t i, j;
int status = EINVAL;
if (addr == NULL)
return EINVAL;
i = _cpystr (buf, addr->email, len);
if (n)
*n = i;
return 0;
for (j = 1; addr; addr = addr->next, j++)
{
if (j == no)
{
i = _cpystr (buf, addr->email, len);
if (n)
*n = i;
status = 0;
break;
}
}
return status;
}
int
address_get_address (address_t addr, char *buf, size_t len, size_t *n)
address_to_string (address_t addr, char *buf, size_t len, size_t *n)
{
size_t i;
if (addr == NULL)
......@@ -365,26 +421,13 @@ address_get_address (address_t addr, char *buf, size_t len, size_t *n)
return 0;
}
#if 0
int
main (int argc, char **argv)
address_get_count (address_t addr, size_t *pcount)
{
address_t addr;
int i;
address_create (&addr, argv[1]);
for (i = 0; addr; addr = addr->next, i++)
{
printf ("%d\n", i);
if (addr->addr)
printf ("Address |%s|\n", addr->addr);
if (addr->comments)
printf ("Comments |%s|\n", addr->comments);
if (addr->personal)
printf ("Personal |%s|\n", addr->personal);
if (addr->email)
printf ("Email |%s|\n", addr->email);
}
size_t j;
for (j = 0; addr; addr = addr->next, j++)
;
if (pcount)
*pcount = j;
return 0;
}
#endif
......
......@@ -510,8 +510,11 @@ fill_blurb (header_t header)
/* parse it. */
status = header_parse (header, header->temp_blurb, header->temp_blurb_len);
free (header->temp_blurb);
header->temp_blurb = NULL;
if (header->temp_blurb)
{
free (header->temp_blurb);
header->temp_blurb = NULL;
}
header->temp_blurb_len = 0;
return status;
}
......
noinst_HEADERS = \
address0.h \
attribute0.h \
auth0.h \
bio.h \
body0.h \
debug0.h \
folder0.h \
header0.h \
iterator0.h \
list0.h \
......
......@@ -83,6 +83,21 @@ extern int mailer_rdlock __P ((mailer_t));
extern int mailer_wrlock __P ((mailer_t));
extern int mailer_unlock __P ((mailer_t));
#define MAILER_NOTIFY(mailer, type) \
if (mailer->observer) observer_notify (mailer->observer, type)
/* Moro(?)ic kluge. */
#define MAILER_DEBUG0(mailer, type, format) \
if (mailer->debug) debug_print (mailer->debug, type, format)
#define MAILER_DEBUG1(mailer, type, format, arg1) \
if (mailer->debug) debug_print (mailer->debug, type, format, arg1)
#define MAILER_DEBUG2(mailer, type, format, arg1, arg2) \
if (mailer->debug) debug_print (mailer->debug, type, format, arg1, arg2)
#define MAILER_DEBUG3(mailer, type, format, arg1, arg2, arg3) \
if (mailer->debug) debug_print (mailer->debug, type, format, arg1, arg2, arg3)
#define MAILER_DEBUG4(mailer, type, format, arg1, arg2, arg3, arg4) \
if (mailer->debug) debug_print (mailer->debug, type, format, arg1, arg2, arg3, arg4)
#ifdef _cplusplus
}
#endif
......
......@@ -236,7 +236,7 @@ mbox_destroy (mailbox_t mailbox)
size_t i;
mbox_data_t mud = mailbox->data;
MAILBOX_DEBUG1 (mailbox, MU_DEBUG_TRACE,
"mbox_destroy (%s/%s)\n", mud->name);
"mbox_destroy (%s)\n", mud->name);
mailbox_wrlock (mailbox);
for (i = 0; i < mud->umessages_count; i++)
{
......
......@@ -146,6 +146,7 @@ observable_destroy (observable_t *pobservable, void *owner)
free (event);
}
}
iterator_destroy (&iterator);
}
list_destroy (&((*pobservable)->list));
free (*pobservable);
......
......@@ -136,6 +136,7 @@ sendmail_open (mailer_t mailer, int flags)
return errno;
}
sendmail->path = path;
MAILER_DEBUG1 (mailer, MU_DEBUG_TRACE, "sendmail (%s)\n", sendmail->path);
return 0;
}
......@@ -167,7 +168,7 @@ sendmail_send_message (mailer_t mailer, const char *from, const char *rcpt,
char **argvec = NULL;
argvec = realloc (argvec, argc * (sizeof (*argvec)));
argvec[0] = sendmail->path;
argvec[0] = strdup (sendmail->path);
/* do not treat '.' as message terminator*/
argvec[1] = strdup ("-oi");
argvec[2] = strdup ("-t");
......@@ -235,7 +236,11 @@ sendmail_send_message (mailer_t mailer, const char *from, const char *rcpt,
else
status = errno;
for (argc = 0; argvec[argc]; argc++)
free (argvec[argc]);
{
MAILER_DEBUG1 (mailer, MU_DEBUG_TRACE, "%s ", argvec[argc]);
free (argvec[argc]);
}
MAILER_DEBUG0 (mailer, MU_DEBUG_TRACE, "\n");
free (argvec);
close (tunnel[0]);
if (status != 0)
......
......@@ -301,6 +301,7 @@ smtp_open (mailer_t mailer, int flags)
smtp->state = SMTP_OPEN;
case SMTP_OPEN:
MAILER_DEBUG2 (mailer, MU_DEBUG_PROT, "smtp_open (%s:%d)\n", smtp->localhost, port);
status = stream_open (mailer->stream, smtp->mailhost, port,
mailer->flags);
CHECK_EAGAIN (smtp, status);
......@@ -310,6 +311,7 @@ smtp_open (mailer_t mailer, int flags)
/* Swallow the greetings. */
status = smtp_read_ack (smtp);
CHECK_EAGAIN (smtp, status);
MAILER_DEBUG0 (mailer, MU_DEBUG_PROT, smtp->buffer);
if (smtp->buffer[0] != '2')
{
stream_close (mailer->stream);
......@@ -317,6 +319,7 @@ smtp_open (mailer_t mailer, int flags)
}
status = smtp_writeline (smtp, "EHLO %s\r\n", smtp->localhost);
CHECK_ERROR (smtp, status);
MAILER_DEBUG0 (mailer, MU_DEBUG_PROT, smtp->buffer);
smtp->state = SMTP_EHLO;
case SMTP_EHLO:
......@@ -328,6 +331,7 @@ smtp_open (mailer_t mailer, int flags)
case SMTP_EHLO_ACK:
status = smtp_read_ack (smtp);
CHECK_EAGAIN (smtp, status);
MAILER_DEBUG0 (mailer, MU_DEBUG_PROT, smtp->buffer);
if (smtp->buffer[0] != '2')
{
smtp->extended = 0;
......@@ -354,6 +358,7 @@ smtp_open (mailer_t mailer, int flags)
{
status = smtp_read_ack (smtp);
CHECK_EAGAIN (smtp, status);
MAILER_DEBUG0 (mailer, MU_DEBUG_PROT, smtp->buffer);
if (smtp->buffer[0] != '2')
{
stream_close (mailer->stream);
......@@ -379,6 +384,7 @@ smtp_close (mailer_t mailer)
case SMTP_NO_STATE:
status = smtp_writeline (smtp, "Quit\r\n");
CHECK_ERROR (smtp, status);
MAILER_DEBUG0 (mailer, MU_DEBUG_PROT, smtp->buffer);
smtp->state = SMTP_QUIT;
case SMTP_QUIT:
......@@ -389,6 +395,7 @@ smtp_close (mailer_t mailer)
case SMTP_QUIT_ACK:
status = smtp_read_ack (smtp);
CHECK_EAGAIN (smtp, status);
MAILER_DEBUG0 (mailer, MU_DEBUG_PROT, smtp->buffer);
default:
break;
......@@ -528,6 +535,7 @@ smtp_send_message(mailer_t mailer, const char *from, const char *rcpt,
free (smtp->from);
smtp->from = NULL;
CHECK_ERROR (smtp, status);
MAILER_DEBUG0 (mailer, MU_DEBUG_PROT, smtp->buffer);
smtp->state = SMTP_MAIL_FROM;
case SMTP_MAIL_FROM:
......@@ -538,6 +546,7 @@ smtp_send_message(mailer_t mailer, const char *from, const char *rcpt,
case SMTP_MAIL_FROM_ACK:
status = smtp_read_ack (smtp);
CHECK_EAGAIN (smtp, status);
MAILER_DEBUG0 (mailer, MU_DEBUG_PROT, smtp->buffer);
if (smtp->buffer[0] != '2')
{
stream_close (mailer->stream);
......@@ -563,6 +572,7 @@ smtp_send_message(mailer_t mailer, const char *from, const char *rcpt,
status = smtp_writeline (smtp, "RCPT TO: %s\r\n", buf);
free (buf);
CHECK_ERROR (smtp, status);
MAILER_DEBUG0 (mailer, MU_DEBUG_PROT, smtp->buffer);
smtp->state = SMTP_RCPT_TO;
}
......@@ -576,6 +586,7 @@ smtp_send_message(mailer_t mailer, const char *from, const char *rcpt,
char *p;
status = smtp_read_ack (smtp);
CHECK_EAGAIN (smtp, status);
MAILER_DEBUG0 (mailer, MU_DEBUG_PROT, smtp->buffer);
if (smtp->buffer[0] != '2')
{
stream_close (mailer->stream);
......@@ -601,6 +612,7 @@ smtp_send_message(mailer_t mailer, const char *from, const char *rcpt,
smtp->to = NULL;
status = smtp_writeline (smtp, "DATA\r\n");
CHECK_ERROR (smtp, status);
MAILER_DEBUG0 (mailer, MU_DEBUG_PROT, smtp->buffer);
smtp->state = SMTP_DATA;
}
......@@ -612,6 +624,7 @@ smtp_send_message(mailer_t mailer, const char *from, const char *rcpt,
case SMTP_DATA_ACK:
status = smtp_read_ack (smtp);
CHECK_EAGAIN (smtp, status);
MAILER_DEBUG0 (mailer, MU_DEBUG_PROT, smtp->buffer);
if (smtp->buffer[0] != '3')
{
stream_close (mailer->stream);
......@@ -664,6 +677,7 @@ smtp_send_message(mailer_t mailer, const char *from, const char *rcpt,
case SMTP_SEND_ACK:
status = smtp_read_ack (smtp);
CHECK_EAGAIN (smtp, status);
MAILER_DEBUG0 (mailer, MU_DEBUG_PROT, smtp->buffer);
if (smtp->buffer[0] != '2')
{
stream_close (mailer->stream);
......