Commit 4ccdee87 4ccdee87a2ae063a7b87f13b9e5b23704931ac84 by Alain Magloire

attribute.c body.c header.c io.c mbx_unix.c message.c mime.c

 	net.c tcp.c transcode.c url_pop.c include/private/io0.h
 	include/private/url0.h include/public/attribute.h
 	include/public/io.h include/public/mailbox.h

all function *_init() rename to *_create().
1 parent bea16f22
......@@ -254,8 +254,7 @@ attribute_copy (attribute_t dest, attribute_t src)
}
int
string_to_attribute (const char *buffer, size_t len,
attribute_t *pattr, void *owner)
string_to_attribute (const char *buffer, attribute_t *pattr, void *owner)
{
char *sep;
int status;
......@@ -265,9 +264,11 @@ string_to_attribute (const char *buffer, size_t len,
return status;
/* Set the attribute */
if (len > 7 && strncasecmp (buffer, "Status:", 7) == 0)
if (strncasecmp (buffer, "Status:", 7) == 0)
{
sep = strchr(buffer, ':'); /* pass the ':' */
sep++;
while (*sep == ' ') sep++; /* glob spaces */
if (strchr (sep, 'R') != NULL || strchr (sep, 'r') != NULL)
attribute_set_read (*pattr);
if (strchr (sep, 'O') != NULL || strchr (sep, 'o') != NULL)
......@@ -280,3 +281,40 @@ string_to_attribute (const char *buffer, size_t len,
return 0;
}
int
attribute_to_string (attribute_t attr, char *buffer, size_t len, size_t *pn)
{
char status[32];
char a[8];
size_t i;
*status = *a = '\0';
if (attribute_is_seen (attr))
strcat (a, "R");
if (attribute_is_answered (attr))
strcat (a, "A");
if (attribute_is_flagged (attr))
strcat (a, "F");
if (attribute_is_read (attr))
strcat (a, "O");
if (*a != '\0')
{
strcpy (status, "Status: ");
strcat (status, a);
strcat (status, "\n");
}
i = strlen (status);
if (buffer && len != 0)
{
strncpy (buffer, status, len - 1);
buffer[len - 1] = '\0';
i = strlen (buffer);
}
if (pn)
*pn = i;
return 0;
}
......
......@@ -82,7 +82,7 @@ body_get_stream (body_t body, stream_t *pstream)
/* lazy floating body it is created when
* doing the first body_write call
*/
status = stream_create (&stream, body);
status = stream_create (&stream, 0, body);
if (status != 0)
return status;
stream_set_read (stream, body_read, body);
......
......@@ -68,7 +68,7 @@ header_create (header_t *ph, const char *blurb, size_t len, void *owner)
header_parse (h, (char *)blurb, len);
status = stream_create (&(h->stream), h);
status = stream_create (&(h->stream), 0, h);
if (status != 0)
return status;
......
......@@ -35,6 +35,8 @@ extern "C" {
struct _stream
{
void *owner;
int flags;
void (*_destroy) __P ((void *));
int (*_get_fd) __P ((stream_t, int *));
int (*_read) __P ((stream_t, char *, size_t, off_t, size_t *));
int (*_write) __P ((stream_t, const char *, size_t, off_t, size_t *));
......
......@@ -92,22 +92,9 @@ struct _url
/* MMDF */
/* POP3 */
struct _url_pop;
typedef struct _url_pop * url_pop_t;
struct _url_pop
{
/* we use the fields from url_t */
/* user, passwd, host, port */
char * auth;
int (*_get_auth) __P ((const url_pop_t, char *, size_t, size_t *));
};
#define MU_POP_PORT 110
/* pop*/
extern int url_pop_get_auth (const url_t, char *, size_t, size_t *);
/* UNIX MBOX */
#ifdef __cplusplus
......
......@@ -67,8 +67,10 @@ extern int attribute_is_equal __P ((attribute_t att1, attribute_t att2));
extern int attribute_copy __P ((attribute_t dst,
attribute_t src));
extern int string_to_attribute __P ((const char *buf, size_t len,
extern int string_to_attribute __P ((const char *buf,
attribute_t *pattr, void *owner));
extern int attribute_to_string __P ((attribute_t attr, char *buf,
size_t len, size_t *));
extern int attribute_get_owner __P ((attribute_t attr, void **owner));
#ifdef __cplusplus
}
......
......@@ -35,9 +35,16 @@ extern "C" { /*}*/
struct _stream;
typedef struct _stream *stream_t;
extern int stream_create __P ((stream_t *, void *owner));
/* stream will be destroy on stream_destroy */
#define MU_STREAM_NO_CHECK 1
extern int stream_create __P ((stream_t *, int flags, void *owner));
extern void stream_destroy __P ((stream_t *, void *owner));
extern int stream_set_destroy __P ((stream_t,
void (*_destroy) __P ((void *)),
void *owner));
extern int stream_set_fd __P ((stream_t,
int (*_get_fd)(stream_t, int *),
void *owner));
......
......@@ -81,7 +81,7 @@ extern int mailbox_is_updated __P ((mailbox_t));
extern int mailbox_scan __P ((mailbox_t, size_t msgno, size_t *count));
/* mailbox size ? */
extern int mailbox_size __P ((mailbox_t, off_t size));
extern int mailbox_size __P ((mailbox_t, off_t *size));
extern int mailbox_get_url __P ((mailbox_t, url_t *));
......
......@@ -22,7 +22,7 @@
#include <stdio.h>
int
stream_create (stream_t *pstream, void *owner)
stream_create (stream_t *pstream, int flags, void *owner)
{
stream_t stream;
if (pstream == NULL || owner == NULL)
......@@ -31,6 +31,7 @@ stream_create (stream_t *pstream, void *owner)
if (stream == NULL)
return ENOMEM;
stream->owner = owner;
stream->flags = flags;
*pstream = stream;
return 0;
}
......@@ -41,13 +42,30 @@ stream_destroy (stream_t *pstream, void *owner)
if (pstream && *pstream)
{
stream_t stream = *pstream;
if (stream->owner == owner)
if (stream->_destroy)
stream->_destroy (owner);
if ((stream->flags & MU_STREAM_NO_CHECK) || stream->owner == owner)
free (stream);
*pstream = NULL;
}
}
int
stream_set_destroy (stream_t stream, void (*_destroy) (void *),
void *owner)
{
if (stream == NULL)
return EINVAL;
if (stream->owner != owner)
return EACCES;
stream->_destroy = _destroy;
return 0;
}
int
stream_set_fd (stream_t stream, int (*_get_fd) (stream_t, int *), void *owner)
{
if (stream == NULL)
......
......@@ -908,7 +908,7 @@ mailbox_unix_readstream (stream_t is, char *buffer, size_t buflen,
mailbox_unix_message_t mum;
size_t nread = 0;
if (is == NULL || (mum = (mailbox_unix_message_t)is->owner) == NULL)
if (is == NULL || (mum = is->owner) == NULL)
return EINVAL;
if (buffer == NULL || buflen == 0)
......@@ -1167,7 +1167,9 @@ mailbox_unix_get_message (mailbox_t mbox, size_t msgno, message_t *pmsg)
message_destroy (&msg, mum);
return status;
}
status = stream_create (&stream, mum);
message_set_body (msg, body, mum);
status = stream_create (&stream, 0, mum);
if (status != 0)
{
message_destroy (&msg, mum);
......@@ -1177,9 +1179,7 @@ mailbox_unix_get_message (mailbox_t mbox, size_t msgno, message_t *pmsg)
stream_set_fd (stream, mailbox_unix_getfd, mum);
body_set_stream (body, stream, mum);
body_set_size (body, mailbox_unix_body_size, mum);
/* set the line */
body_set_lines (body, mailbox_unix_body_lines, mum);
message_set_body (msg, body, mum);
/* set the attribute */
status = message_set_attribute (msg, mum->new_attr, mum);
......@@ -1293,7 +1293,7 @@ mailbox_unix_append_message (mailbox_t mbox, message_t msg)
}
static int
mailbox_unix_size (mailbox_t mbox, off_t *size)
mailbox_unix_size (mailbox_t mbox, off_t *psize)
{
mailbox_unix_data_t mud;
struct stat st;
......@@ -1310,7 +1310,8 @@ mailbox_unix_size (mailbox_t mbox, off_t *size)
/* oops !! */
if (fstat (fd, &st) != 0)
return errno;
*size = st.st_size;
if (psize)
*psize = st.st_size;
return 0;
}
......
......@@ -47,7 +47,7 @@ message_create (message_t *pmsg, void *owner)
msg = calloc (1, sizeof (*msg));
if (msg == NULL)
return ENOMEM;
status = stream_create (&stream, msg);
status = stream_create (&stream, 0, msg);
if (status != 0)
{
free (msg);
......@@ -188,7 +188,7 @@ message_get_stream (message_t msg, stream_t *pstream)
{
stream_t stream;
int status;
status = stream_create (&stream, msg);
status = stream_create (&stream, 0, msg);
if (status != 0)
return status;
stream_set_read (stream, message_read, msg);
......
......@@ -449,7 +449,7 @@ int mime_get_part(mime_t mime, int part, message_t *msg)
message_set_header(mime_part->msg, mime_part->hdr, mime_part);
header_size(mime_part->hdr, &hsize);
if ( ( ret = body_create(&body, mime_part) ) == 0 ) {
if ( ( ret = stream_create(&stream, mime_part) ) == 0 ) {
if ( ( ret = stream_create(&stream, 0, mime_part) ) == 0 ) {
body_set_size (body, _mime_body_size, mime_part);
stream_set_read(stream, _mime_message_read, mime_part);
body_set_stream(body, stream, mime_part);
......@@ -521,7 +521,7 @@ int mime_unencapsulate(mime_t mime, message_t msg, message_t *newmsg)
if ( ( ret = message_create(&(mime_part->msg), mime_part) ) == 0) {
message_set_header(mime_part->msg, mime_part->hdr, mime_part);
if ( ( ret = body_create(&body, mime_part) ) == 0 ) {
if ( ( ret = stream_create(&stream, mime_part) ) == 0 ) {
if ( ( ret = stream_create(&stream, 0, mime_part) ) == 0 ) {
stream_set_read(stream, _mime_message_read, mime_part);
body_set_size (body, _mime_body_size, mime_part);
body_set_stream( body, stream, mime_part);
......
......@@ -17,6 +17,7 @@
#include <stdlib.h>
#include <string.h>
#include <errno.h>
#include <net0.h>
......@@ -42,7 +43,7 @@ int net_api_create(net_t *net, net_t parent, const char *type)
}
if ( i == napis )
return ENOTSUP;
if ( ret = ( _netreg[i].create(&(n->data), &(n->api)) ) != 0 )
if ( (ret = ( _netreg[i].create(&(n->data), &(n->api)) )) != 0 )
free(n);
n->parent = parent;
n->net_reg = &_netreg[i];
......
......@@ -154,7 +154,7 @@ static int _tcp_new(void *netdata, net_t parent, void **data)
tcp->host = NULL;
tcp->port = -1;
tcp->state = TCP_STATE_INIT;
stream_create(&tcp->stream, tcp);
stream_create(&tcp->stream, 0, tcp);
stream_set_read(tcp->stream, _tcp_read, tcp);
stream_set_write(tcp->stream, _tcp_write, tcp);
stream_set_fd(tcp->stream, _tcp_get_fd, tcp);
......
......@@ -61,7 +61,7 @@ int transcode_create(transcoder_t *ptc, char *encoding)
if ( i == NUM_TRANSCODERS )
return ENOTSUP;
stream_create(&tc->ustream, tc );
stream_create(&tc->ustream, 0, tc );
stream_set_read(tc->ustream, tclist[i].decode_read, tc );
stream_set_write(tc->ustream, tclist[i].encode_write, tc );
tc->destroy = tclist[i].destroy;
......
......@@ -19,7 +19,6 @@
#include <registrar.h>
#include <errno.h>
#include <cpystr.h>
#include <stdlib.h>
#include <string.h>
#include <errno.h>
......@@ -33,27 +32,6 @@ struct url_registrar _url_pop_registrar =
url_pop_create, url_pop_destroy
};
static int get_auth (const url_pop_t up, char *s, size_t len, size_t *);
static int
get_auth (const url_pop_t up, char *s, size_t len, size_t *n)
{
size_t i;
if (up)
return EINVAL;
i = _cpystr (s, up->auth, len);
if (n)
*n = i;
return 0;
}
int
(url_pop_get_auth) (const url_t url, char *auth, size_t len, size_t *n)
{
return (url) ?
((url_pop_t)(url->data))->_get_auth(url->data, auth, len, n) : EINVAL;
}
static void
url_pop_destroy (url_t *purl)
{
......@@ -64,12 +42,6 @@ url_pop_destroy (url_t *purl)
free (url->user);
free (url->passwd);
free (url->host);
if (url->data)
{
url_pop_t up = url->data;
free (up->auth);
}
free (url->data);
free (url);
*purl = NULL;
}
......@@ -77,28 +49,25 @@ url_pop_destroy (url_t *purl)
/*
POP URL
pop://<user>;AUTH=<auth>@<host>:<port>
pop://[<user>;AUTH=<auth>@]<host>[:<port>]
*/
static int
url_pop_create (url_t *purl, const char *name)
{
const char *host_port, *indexe;
struct url_registrar *ureg = &_url_pop_registrar;
size_t len, scheme_len = strlen (ureg->scheme);
size_t scheme_len = strlen (ureg->scheme);
url_t url;
url_pop_t up;
/* reject the obvious */
if (name == NULL || strncmp (ureg->scheme, name, scheme_len) != 0
|| (host_port = strchr (name, '@')) == NULL
|| (len = strlen (name)) < 9 /* 6(scheme)+1(user)+1(@)+1(host)*/)
return ENOMEM;
if (name == NULL || strncmp (ureg->scheme, name, scheme_len) != 0)
return EINVAL;
/* do I need to decode url encoding '% hex hex' ? */
url = calloc(1, sizeof (*url));
url->data = up = calloc(1, sizeof(*up));
up->_get_auth = get_auth;
if (url == NULL)
return ENOMEM;
/* TYPE */
url->_create = _url_pop_registrar._create;
......@@ -114,24 +83,22 @@ url_pop_create (url_t *purl, const char *name)
name += scheme_len; /* pass the scheme */
host_port = strchr (name, '@');
if (host_port == NULL)
host_port= name;
/* looking for "user;auth=auth-enc" */
for (indexe = name; indexe != host_port; indexe++)
{
/* Auth ? */
if (*indexe == ';')
{
/* make sure it the token */
/* make sure it's the token */
if (strncasecmp(indexe + 1, "auth=", 5) == 0)
break;
}
}
if (indexe == name)
{
url_pop_destroy (&url);
return -1;
}
/* USER */
url->user = malloc(indexe - name + 1);
if (url->user == NULL)
......@@ -142,29 +109,29 @@ url_pop_create (url_t *purl, const char *name)
((char *)memcpy(url->user, name, indexe - name))[indexe - name] = '\0';
/* AUTH */
if ((host_port - indexe) <= 6 /*strlen(";AUTH=")*/)
if (indexe == host_port)
{
/* Use default AUTH '*' */
up->auth = malloc (1 + 1);
if (up->auth)
url->passwd = malloc (1 + 1);
if (url->passwd)
{
up->auth[0] = '*';
up->auth[1] = '\0';
url->passwd[0] = '*';
url->passwd[1] = '\0';
}
}
else
{
/* move pass AUTH= */
indexe += 6;
up->auth = malloc (host_port - indexe + 1);
if (up->auth)
url->passwd = malloc (host_port - indexe + 1);
if (url->passwd)
{
((char *)memcpy (up->auth, indexe, host_port - indexe))
((char *)memcpy (url->passwd, indexe, host_port - indexe))
[host_port - indexe] = '\0';
}
}
if (up->auth == NULL)
if (url->passwd == NULL)
{
url_pop_destroy (&url);
return -1;
......