Commit fe05b96e fe05b96ef0ab681127f22559483f4367144658e1 by Sam Roberts

address_aget_email() - like _get_email() but mallocs.

address_createv() - like address_create(), but takes an array of strings.
1 parent d1843ea4
......@@ -85,6 +85,23 @@ The return value is @code{0} on success and a code number on error conditions:
@end table
@end deftypefun
@deftypefun int address_createv (address_t *@var{addr}, const char *@var{sv}, size_t @var{len})
This function allocates and initializes @var{addr} by parsing the
array of pointers to RFC822 address-lists in @var{sv}. If @var{len} is
@code{-1}, then @var{sv} must be null-terminated in the fashion of @var{argv},
otherwise @var{len} is the length of the array.
The return value is @code{0} on success and a code number on error conditions:
@table @code
@ADDRESSEINVAL
@ADDRESSENOMEM
@ADDRESSEPARSE
@end table
@end deftypefun
@deftypefun void address_destroy (address_t *@var{addr})
The @var{addr} is destroyed.
@end deftypefun
......@@ -96,6 +113,23 @@ address is the plain email address, correctly quoted, suitable for
using in an smtp dialog, for example, or as the address part of
a contact book entry.
Note that the entry may be valid, but be a group name. In this case success
is returned, but the length of the address is @code{0}.
The return value is @code{0} on success and a code number on error conditions:
@table @code
@ADDRESSEINVAL
@ADDRESSENOENT
@end table
@end deftypefun
@deftypefun int address_aget_email (address_t *@var{addr}, size_t @var{no}, char** @var{bufp})
As above, but mallocs the email address, if present,
and write a pointer to it into @var{bufp}. @var{bufp} will be @code{NULL}
if there is no email address to return.
The return value is @code{0} on success and a code number on error conditions:
@table @code
@ADDRESSEINVAL
......
......@@ -36,6 +36,7 @@ struct _address;
typedef struct _address *address_t;
extern int address_create __P ((address_t *, const char *));
extern int address_createv __P ((address_t *, const char *v[], size_t));
extern int address_create0 __P ((address_t *, const char *));
extern void address_destroy __P ((address_t *));
......@@ -52,11 +53,15 @@ extern int address_get_comments
extern int address_get_route
__P ((address_t, size_t, char *, size_t, size_t *));
extern int address_aget_email
__P ((address_t, size_t, char **));
extern int address_is_group
__P ((address_t, size_t, int*));
extern int address_to_string __P ((address_t, char *, size_t, size_t *));
extern int address_get_count __P ((address_t, size_t *));
extern int address_get_email_count __P ((address_t, size_t *));
#ifdef __cplusplus
}
......
......@@ -31,7 +31,7 @@
#include <mailutils/mutil.h>
#include <address0.h>
/* Get email address from rfc822 address. */
/* Get email addresses from rfc822 address. */
int
address_create (address_t *a, const char *s)
{
......@@ -43,7 +43,7 @@ address_create (address_t *a, const char *s)
return EINVAL;
*a = NULL;
status = parse822_address_list (a, (char*) s);
status = parse822_address_list (a, s);
if (status == 0)
{
/* And address-list may contain 0 addresses but parse correctly.
......@@ -61,6 +61,56 @@ address_create (address_t *a, const char *s)
return status;
}
/* Get email addresses from array of rfc822 addresses. */
int
address_createv (address_t *a, const char *sv[], size_t len)
{
int status = 0;
size_t buflen = 0;
char* buf = 0;
int i;
if(!sv)
return EINVAL;
if(len == -1)
{
const char** vp = sv;
len = 0;
for(len = 0; *vp; vp++, len++)
;
}
if(len == 0)
return EINVAL;
for(i = 0; i < len; i++)
buflen += strlen(sv[i]);
buflen += (len - 1) * strlen(", ");
buflen += 1; /* termination null */
buf = malloc(buflen);
if(!buf)
return ENOMEM;
for(i = 0, buf[0] = '\0'; i < len; i++)
{
if(i != 0)
strcat(buf, ", ");
strcat(buf, sv[i]);
}
status = address_create(a, buf);
free(buf);
return status;
}
void
address_destroy (address_t *paddress)
{
......@@ -157,6 +207,31 @@ address_get_email (address_t addr, size_t no, char *buf, size_t len, size_t *n)
}
int
address_aget_email (address_t addr, size_t no, char **buf)
{
size_t j;
int status = ENOENT;
if (addr == NULL || buf == NULL)
return EINVAL;
for (j = 1; addr; addr = addr->next, j++)
{
if (j == no)
{
status = 0;
*buf = NULL;
if (addr->email)
{
*buf = strdup (addr->email);
if (!*buf)
status = ENOMEM;
}
break;
}
}
return status;
}
int
address_get_local_part (address_t addr, size_t no, char *buf, size_t len, size_t *n)
{
size_t i, j;
......@@ -219,6 +294,14 @@ address_get_route (address_t addr, size_t no, char *buf, size_t len, size_t *n)
return status;
}
static int
_address_is_group (address_t addr)
{
if (addr->personal && !addr->local_part && !addr->domain)
return 1;
return 0;
}
int
address_is_group (address_t addr, size_t no, int* yes)
{
......@@ -233,9 +316,7 @@ address_is_group (address_t addr, size_t no, int* yes)
status = 0;
if(yes)
{
*yes = 0;
if(addr->personal && !addr->local_part && !addr->domain)
*yes = 1;
*yes = _address_is_group(addr);
}
break;
}
......@@ -267,3 +348,18 @@ address_get_count (address_t addr, size_t *pcount)
*pcount = j;
return 0;
}
int
address_get_email_count (address_t addr, size_t *pcount)
{
size_t j;
for (j = 0; addr; addr = addr->next)
{
if(!_address_is_group(addr))
j++;
}
if (pcount)
*pcount = j;
return 0;
}
......