Commit 5059666f 5059666f66472e2ff2c4a96bee47f5527abbf254 by Alain Magloire

header.c mailer.c mbx_unix.c mime.c tcp.c

 	include/public/header.h include/public/mailer.h

add dirty/ugly support for header_set_value()
1 parent 4f2b8ab8
......@@ -24,6 +24,7 @@
#include <io0.h>
#include <string.h>
#include <stdlib.h>
#include <stdio.h>
#include <errno.h>
static int header_parse (header_t h, char *blurb, int len);
......@@ -113,11 +114,14 @@ header_parse (header_t header, char *blurb, int len)
return 0;
header->blurb_len = len;
header->blurb = calloc (1, header->blurb_len + 1);
header->blurb = calloc (header->blurb_len + 1, 1);
if (header->blurb == NULL)
return ENOMEM;
memcpy (header->blurb, blurb, header->blurb_len);
free (header->hdr);
header->hdr = NULL;
header->hdr_count = 0;
for (header_start = header->blurb;; header_start = ++header_end)
{
char *fn, *fn_end, *fv, *fv_end;
......@@ -195,27 +199,68 @@ header_parse (header_t header, char *blurb, int len)
header->hdr_count++;
} /* for (header_start ...) */
#if 0
header->blurb_len -= len;
if (header->blurb_len <= 0)
{
free (header->blurb);
free (header->hdr);
return EINVAL;
}
/* always add the separtor LF */
header->blurb [header->blurb_len] = '\n';
header->blurb_len++;
#endif
return 0;
return 0;
}
/* FIXME: grossly inneficient, to many copies and reallocating
* This all header business need a good rewrite.
*/
int
header_set_value (header_t h, const char *fn, const char *fv,
size_t n, int replace)
header_set_value (header_t header, const char *fn, const char *fv, int replace)
{
(void)h; (void)fn; (void)fv; (void)n; (void)replace;
return ENOSYS;
char *blurb;
size_t len;
if (header == NULL || fn == NULL || fv == NULL)
return EINVAL;
/* Easy approach: if replace, overwrite the field-{namve,value}
* and readjust the pointers by calling header_parse ()
* this is wastefull and bad, we're just fragmenting the memory
* it can be done better. But that may imply a rewite of the headers
* So for another day.
*/
{
size_t name_len;
size_t i;
size_t fn_len;
size_t fv_len;
len = header->blurb_len;
for (name_len = strlen (fn), i = 0; i < header->hdr_count; i++)
{
fn_len = header->hdr[i].fn_end - header->hdr[i].fn;
fv_len = header->hdr[i].fv_end - header->hdr[i].fv;
if (fn_len == name_len &&
strncasecmp (header->hdr[i].fn, fn, fn_len) == 0)
{
if (replace)
{
blurb = header->blurb;
memmove (header->hdr[i].fn, header->hdr[i + 1].fn,
header->hdr[header->hdr_count - 1].fv_end
- header->hdr[i + 1].fn + 1 + 1);
/* readjust the pointers if move */
len -= fn_len + fv_len + 2;
i--;
blurb = header->blurb;
header_parse (header, blurb, len);
free (blurb);
}
}
}
}
/* and it's getting worse, we free/malloc at will */
len = strlen (fn) + strlen (fv) + 1 + 1 + 1 + 1;
blurb = calloc (header->blurb_len + len, 1);
if (blurb == NULL)
return ENOMEM;
sprintf (blurb, "%s: %s\n", fn, fv);
memcpy (blurb + len - 1, header->blurb, header->blurb_len);
free (header->blurb);
header_parse (header, blurb, len + header->blurb_len);
free (blurb);
return 0;
}
int
......
......@@ -72,9 +72,9 @@ extern int header_create __P ((header_t *, const char *blurb,
extern void header_destroy __P ((header_t *, void *owner));
extern int header_set_value __P ((header_t, const char *fn,
const char *fv, size_t n, int replace));
extern int header_get_value __P ((header_t, const char *fn, char *fv,
size_t len, size_t *nwritten));
const char *fv, int replace));
extern int header_get_value __P ((header_t, const char *fn, char *buf,
size_t buflen, size_t *nwritten));
extern int header_entry_count __P ((header_t, size_t *num));
extern int header_entry_name __P ((header_t, size_t num, char *buf,
size_t buflen, size_t *total));
......
......@@ -38,6 +38,7 @@ struct _mailer;
typedef struct _mailer *mailer_t;
extern int mailer_create __P ((mailer_t *, message_t));
extern int mailer_destroy __P ((mailer_t *));
extern int mailer_connect __P ((mailer_t, char *host));
extern int mailer_disconnect __P ((mailer_t));
extern int mailer_send_header __P ((mailer_t, message_t));
......
......@@ -35,7 +35,8 @@ int _mailer_sock_connect(char *host, int port);
char *_mailer_find_mailbox(char *addr);
int _mailer_send_command(mailer_t ml, message_t msg, int cmd);
char *nb_fgets(char *buf, int size, int s);
char *nb_fprintf(int s, char *format, ...);
const char *nb_fprintf(int s, const char *format, ...);
static int _mailer_rctp(mailer_t ml, char *str);
#define nb_read read
#define nb_write write
......@@ -76,15 +77,15 @@ mailer_destroy(mailer_t *pml)
return (0);
}
int
mailer_connect(mailer_t ml, char *host)
int
mailer_connect(mailer_t ml, char *host)
{
if (!ml || !host)
return (EINVAL);
if ((ml->socket = _mailer_sock_connect(host, 25)) < 0)
return (-1);
do
do
{
nb_fgets(ml->line_buf, MAILER_LINE_BUF_SIZE, ml->socket); /* read header line */
} while ( strlen(ml->line_buf) > 4 && *(ml->line_buf+3) == '-');
......@@ -92,8 +93,8 @@ mailer_connect(mailer_t ml, char *host)
return (0);
}
int
mailer_disconnect(mailer_t ml)
int
mailer_disconnect(mailer_t ml)
{
if (!ml || (ml->socket != -1))
return (EINVAL);
......@@ -102,7 +103,7 @@ mailer_disconnect(mailer_t ml)
return (0);
}
int
int
mailer_send_header(mailer_t ml, message_t msg)
{
header_t hdr;
......@@ -138,7 +139,8 @@ mailer_send_header(mailer_t ml, message_t msg)
int
mailer_send_message(mailer_t ml, message_t msg)
{
int status, len = 0, data_len = 0, consumed = 0;
int status, data_len = 0;
size_t consumed = 0, len = 0;
char *data, *p, *q;
if (!ml || !msg || (ml->socket == -1))
......@@ -227,13 +229,13 @@ _mailer_find_mailbox(char *addr)
{
char *p, *c;
p = addr;
if ( (c = strchr( p, '<')) != 0)
if ( (c = strchr( p, '<')) != 0)
{
p = c+1;
if ( c = strchr( p, '>'))
if ( (c = strchr( p, '>')) )
*c = '\0';
}
else if ( (c = strchr( p, '(' )) != 0 )
else if ( (c = strchr( p, '(' )) != 0 )
{
--c;
while ( c > p && *c && isspace( *c ) ) {
......@@ -244,14 +246,14 @@ _mailer_find_mailbox(char *addr)
return p;
}
int
static int
_mailer_rctp(mailer_t ml, char *str)
{
char *p, *c = NULL, *q = NULL;
for (q = p = str; q && *p; p = q+1)
{
if ( q = strchr( p, ','))
if ( (q = strchr( p, ',')) )
*q = '\0';
while ( p && *p && isspace( *p ) )
p++;
......@@ -270,10 +272,10 @@ int
_mailer_send_command(mailer_t ml, message_t msg, int cmd)
{
header_t hdr;
char *p, *c = NULL, *q = NULL;
char *p;
char str[128];
size_t str_len;
char *success = "250";
const char *success = "250";
switch (cmd)
{
......@@ -320,7 +322,7 @@ _mailer_send_command(mailer_t ml, message_t msg, int cmd)
}
char *
nb_fgets( char *buf, int size, int s )
nb_fgets( char *buf, int size, int s )
{
static char *buffer[25];
char *p, *b, *d;
......@@ -331,9 +333,9 @@ nb_fgets( char *buf, int size, int s )
return 0;
bytes = i = strlen( p = b = buffer[s] );
*( d = buf ) = '\0';
for ( ; i-- > 0; p++ )
for ( ; i-- > 0; p++ )
{
if ( *p == '\n' )
if ( *p == '\n' )
{
char c = *( p+1 );
......@@ -346,19 +348,19 @@ nb_fgets( char *buf, int size, int s )
}
flags = fcntl( s, F_GETFL );
fcntl( s, F_SETFL, O_NONBLOCK );
while ( bytes <= size )
while ( bytes <= size )
{
fd_set fds;
FD_ZERO( &fds );
FD_SET( s, &fds );
select( s+1, &fds, 0, 0, 0 ); /* we really don't care what it returns */
if ( ( i = nb_read( s, p, BUFFSIZE - bytes ) ) == -1 )
if ( ( i = nb_read( s, p, BUFFSIZE - bytes ) ) == -1 )
{
*b = '\0';
return 0;
}
else if ( i == 0 )
}
else if ( i == 0 )
{
*( p+1 ) = '\0';
strcat( d, b );
......@@ -368,9 +370,9 @@ nb_fgets( char *buf, int size, int s )
}
*( p+i ) = '\0';
bytes += i;
for ( ; i-- > 0; p++ )
for ( ; i-- > 0; p++ )
{
if ( *p == '\n' )
if ( *p == '\n' )
{
char c = *( p+1 );
......@@ -382,7 +384,7 @@ nb_fgets( char *buf, int size, int s )
return buf;
}
}
if ( bytes == BUFFSIZE )
if ( bytes == BUFFSIZE )
{
memcpy( d, b, BUFFSIZE );
d += BUFFSIZE;
......@@ -397,8 +399,8 @@ nb_fgets( char *buf, int size, int s )
return buf;
}
char *
nb_fprintf( int s, char *format, ... )
const char *
nb_fprintf( int s, const char *format, ... )
{
char buf[MAILER_LINE_BUF_SIZE];
va_list vl;
......
......@@ -770,37 +770,6 @@ mailbox_unix_expunge (mailbox_t mbox)
goto bailout;
stream_flush (mbox->stream);
/* we need to readjust the pointers */
{
size_t dlast;
for (j = dirty, dlast = mud->messages_count - 1;
j < mud->messages_count; j++)
{
/* clear all the references,
* any attach messages been already destroy above
*/
mum = mud->umessages[j];
if (mum->new_attr && attribute_is_deleted (mum->new_attr))
{
memmove (mud->umessages + j, mud->umessages + j + 1,
(dlast - dirty) * sizeof (mum));
mum->header_from = mum->header_from_end = 0;
mum->header_status = mum->header_status_end = 0;
mum->body = mum->body_end = 0;
mum->header_lines = mum->body_lines = 0;
attribute_destroy (&(mum->new_attr));
mud->umessages[dlast] = mum;
dlast--;
mum = mud->umessages[j];
}
mum->header_from = mum->header_from_end = 0;
mum->header_status = mum->header_status_end = 0;
mum->body = mum->body_end = 0;
mum->header_lines = mum->body_lines = 0;
}
mailbox_unix_scan0 (mbox, dirty, NULL, 0);
}
/* Don't remove the tmp mbox in case of errors */
remove (tmpmbox);
......@@ -813,6 +782,37 @@ mailbox_unix_expunge (mailbox_t mbox)
fclose (tempfile);
sigprocmask (SIG_UNBLOCK, &sigset, 0);
/* we need to readjust the pointers */
if (status == 0)
{
size_t dlast;
for (j = dirty, dlast = mud->messages_count - 1;
j < mud->messages_count; j++)
{
/* clear all the references,
* any attach messages been already destroy above
*/
mum = mud->umessages[j];
if (mum->new_attr && attribute_is_deleted (mum->new_attr))
{
memmove (mud->umessages + j, mud->umessages + j + 1,
(dlast - dirty) * sizeof (mum));
mum->header_from = mum->header_from_end = 0;
mum->header_status = mum->header_status_end = 0;
mum->body = mum->body_end = 0;
mum->header_lines = mum->body_lines = 0;
attribute_destroy (&(mum->new_attr));
mud->umessages[dlast] = mum;
dlast--;
mum = mud->umessages[j];
}
mum->header_from = mum->header_from_end = 0;
mum->header_status = mum->header_status_end = 0;
mum->body = mum->body_end = 0;
mum->header_lines = mum->body_lines = 0;
}
mailbox_unix_scan0 (mbox, dirty, NULL, 0);
}
return status;
}
......
......@@ -74,9 +74,9 @@ static int _mime_append_part(mime_t mime, message_t msg, int body_offset, int bo
mime->header_length = 0;
if ( ( ret = header_get_value(mime_part->hdr, "Content-Type", NULL, 0, &size) ) != 0 || size == 0 ) {
if ( _mime_is_multipart_digest(mime) )
header_set_value(mime_part->hdr, "Content-Type", "message/rfc822", 0, 0);
header_set_value(mime_part->hdr, "Content-Type", "message/rfc822", 0);
else
header_set_value(mime_part->hdr, "Content-Type", "text/plain", 0, 0);
header_set_value(mime_part->hdr, "Content-Type", "text/plain", 0);
}
}
mime_part->body_len = body_len;
......
......@@ -25,6 +25,7 @@
#include <sys/socket.h>
#include <netinet/in.h>
#include <arpa/inet.h>
#include <unistd.h>
#include <io0.h>
#include <tcp.h>
......@@ -126,10 +127,10 @@ static int _tcp_get_fd(stream_t stream, int *fd)
static int _tcp_read(stream_t stream, char *buf, size_t buf_size, off_t offset, size_t *br)
{
struct _tcp_instance *tcp = stream->owner;
int bytes;
int bytes;
offset = offset;
if ( br == NULL )
if ( br == NULL )
return EINVAL;
*br = 0;
if ( ( bytes = recv(tcp->fd, buf, buf_size, 0) ) == -1 ) {
......@@ -143,10 +144,10 @@ static int _tcp_read(stream_t stream, char *buf, size_t buf_size, off_t offset,
static int _tcp_write(stream_t stream, const char *buf, size_t buf_size, off_t offset, size_t *bw)
{
struct _tcp_instance *tcp = stream->owner;
int bytes;
int bytes;
offset = offset;
if ( bw == NULL )
if ( bw == NULL )
return EINVAL;
*bw = 0;
if ( ( bytes = send(tcp->fd, buf, buf_size, 0) ) == -1 ) {
......