Commit e4695e88 e4695e885cac8cf698113e080a12062a20145d83 by Alain Magloire

for mail/*

- implemented header
- implemented printall
- little change on print
- little bugs in util.c fix
- free(list) after calling util_expand_list();
- Range is now working "2-4" or "2 - 4" or "2 3-5 6" should work.
Various little bugs fixes.

for the mailbox/*:
- little cleanup and buglets fix.
1 parent 18aebf95
/* GNU mailutils - a suite of utilities for electronic mail
Copyright (C) 1999 Free Software Foundation, Inc.
Copyright (C) 1999, 2000 Free Software Foundation, Inc.
This program is free software; you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
......@@ -25,9 +25,7 @@ int
mail_from (int argc, char **argv)
{
if (argc > 1)
{
return util_msglist_command (mail_from, argc, argv);
}
else
{
message_t msg;
......@@ -41,9 +39,11 @@ mail_from (int argc, char **argv)
if (col)
columns = strtol (col, NULL, 10) - 5;
if (mailbox_get_message (mbox, cursor, &msg) != 0 ||
message_get_header (msg, &hdr) != 0)
if (mailbox_get_message (mbox, cursor, &msg) != 0)
{
fprintf (stderr, "Could not read message %d\n", cursor);
return 1;
}
froml = columns / 3;
subjl = columns * 2 / 3;
......@@ -60,6 +60,7 @@ mail_from (int argc, char **argv)
return 1;
}
message_get_header (msg, &hdr);
header_get_value (hdr, MU_HEADER_FROM, from, froml, NULL);
header_get_value (hdr, MU_HEADER_SUBJECT, subj, subjl, NULL);
......
/* GNU mailutils - a suite of utilities for electronic mail
Copyright (C) 1999 Free Software Foundation, Inc.
Copyright (C) 1999, 2000 Free Software Foundation, Inc.
This program is free software; you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
......@@ -24,6 +24,34 @@
int
mail_headers (int argc, char **argv)
{
printf ("Function not implemented in %s line %d\n", __FILE__, __LINE__);
if (argc > 1)
return util_msglist_command (mail_headers, argc, argv);
else
{
message_t msg;
header_t hdr;
stream_t is;
char buffer[BUFSIZ];
off_t off = 0;
size_t n = 0;
if (mailbox_get_message (mbox, cursor, &msg) != 0)
{
printf ("Could not read message %d\n", cursor);
return 1;
}
message_get_header (msg, &hdr);
header_get_stream (hdr, &is);
while (stream_read (is, buffer, sizeof (buffer) - 1, off, &n) == 0
&& n != 0)
{
buffer[n] = '\0';
printf ("%s", buffer);
off += n;
}
return 0;
}
return 1;
}
......
/* GNU mailutils - a suite of utilities for electronic mail
Copyright (C) 1999 Free Software Foundation, Inc.
Copyright (C) 1999, 2000 Free Software Foundation, Inc.
This program is free software; you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
......@@ -64,12 +64,17 @@ parse_opt (int key, char *arg, struct argp_state *state)
args->file = arg;
else
{
int len;
char *home = getenv("HOME");
int len = strlen (home) + strlen ("/mbox") + 1;
if (home == NULL)
{
fprintf (stderr, "No Home!\n");
home = (char *)"";
}
len = strlen (home) + strlen ("/mbox") + 1;
args->file = malloc(len * sizeof (char));
strcpy (args->file, home);
strcat (args->file, "/mbox");
free (home);
}
break;
case 'p':
......@@ -159,7 +164,7 @@ main (int argc, char **argv)
/* mail_from (2, from); */
/* FIXME: this is bad form */
for (cursor=1; cursor < total; cursor++)
for (cursor = 1; cursor <= total; cursor++)
mail_from (1, from);
cursor = realcursor;
......@@ -191,4 +196,3 @@ main (int argc, char **argv)
add_history (cmd);
}
}
......
/* GNU mailutils - a suite of utilities for electronic mail
Copyright (C) 1999 Free Software Foundation, Inc.
Copyright (C) 1999, 2000 Free Software Foundation, Inc.
This program is free software; you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
......@@ -25,44 +25,49 @@
int
mail_print (int argc, char **argv)
{
if (argc > 1)
return util_msglist_command (mail_print, argc, argv);
else
{
message_t mesg;
header_t hdr;
body_t body;
stream_t stream;
char buffer[BUFSIZ];
off_t off = 0;
size_t n = 0;
if (argc > 1)
return util_msglist_command (mail_print, argc, argv);
else if (mailbox_get_message (mbox, cursor, &mesg) != 0)
printf ("Couldn't read message %d\n", cursor);
else if (message_get_header (mesg, &hdr) != 0)
printf ("Couldn't read message header on message %d\n", cursor);
else if (message_get_body (mesg, &body) != 0)
printf ("Couldn't read message body from message %d\n", cursor);
else if (body_get_stream (body, &stream) != 0)
printf ("Couldn't get stream for message %d\n", cursor);
else
if (mailbox_get_message (mbox, cursor, &mesg) != 0)
{
char *buf = NULL;
size_t len = 0;
buf = malloc (80 * sizeof (char));
if (header_get_value (hdr, MU_HEADER_FROM, buf, 80, NULL) == 0)
printf ("Could not read message %d\n", cursor);
return 1;
}
message_get_header (mesg, &hdr);
if (header_get_value (hdr, MU_HEADER_FROM, buffer, sizeof (buffer),
NULL) == 0)
{
printf ("From: %s\n", buf);
printf ("From: %s\n", buffer);
/* free (buf); */
}
if (header_get_value (hdr, MU_HEADER_SUBJECT, buf, 80,NULL) == 0)
if (header_get_value (hdr, MU_HEADER_SUBJECT, buffer, sizeof (buffer),
NULL) == 0)
{
printf ("Subject: %s\n", buf);
printf ("Subject: %s\n", buffer);
/* free (buf); */
}
free (buf);
body_size (body, &len);
buf = malloc ((len+1) * sizeof(char));
memset (buf, '\0', len+1);
stream_read (stream, buf, len, 0, NULL);
printf ("\n%s\n", buf);
free (buf);
}
message_get_body (mesg, &body);
body_get_stream (body, &stream);
while (stream_read (stream, buffer, sizeof (buffer) - 1, off, &n) == 0
&& n != 0)
{
buffer[n] = '\0';
printf ("%s", buffer);
off += n;
}
return 0;
}
return 1;
}
......
/* GNU mailutils - a suite of utilities for electronic mail
Copyright (C) 1999 Free Software Foundation, Inc.
Copyright (C) 1999, 2000 Free Software Foundation, Inc.
This program is free software; you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
......@@ -25,6 +25,29 @@
int
mail_printall (int argc, char **argv)
{
printf ("Function not implemented in %s line %d\n", __FILE__, __LINE__);
if (argc > 1)
return util_msglist_command (mail_printall, argc, argv);
else
{
message_t msg;
stream_t is;
char buffer[BUFSIZ];
off_t off = 0;
size_t n = 0;
if (mailbox_get_message (mbox, cursor, &msg) != 0)
return 1;
message_get_stream (msg, &is);
while (stream_read (is, buffer, sizeof (buffer) - 1, off, &n) == 0
&& n != 0)
{
buffer[n] = '\0';
printf ("%s", buffer);
off += n;
}
return 0;
}
return 1;
}
......
/* GNU mailutils - a suite of utilities for electronic mail
Copyright (C) 1999 Free Software Foundation, Inc.
Copyright (C) 1999, 2000 Free Software Foundation, Inc.
This program is free software; you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
......@@ -30,6 +30,7 @@ util_expand_msglist (const int argc, char **argv, int **list)
{
int i = 0, lc = 0;
int undelete = 0;
int hyphen = 0;
if (util_command_get (argv[0]) == util_command_get ("undelete"))
undelete = 1;
......@@ -43,7 +44,7 @@ util_expand_msglist (const int argc, char **argv, int **list)
}
else if (!strcmp (argv[i], "-"))
{
*list[lc++] = i; /* FIXME: previous [un]deleted message */
hyphen = 1; /* FIXME: previous [un]deleted message */
}
else if (!strcmp (argv[i], "."))
{
......@@ -61,8 +62,8 @@ util_expand_msglist (const int argc, char **argv, int **list)
{
free (*list);
*list = malloc (total * sizeof(int));
for (i=0; i < total; i++)
*list[i] = i+1;
for (i = 0; i < total; i++)
*list[i] = i + 1;
return total;
}
else if (argv[i][0] == '/')
......@@ -80,19 +81,44 @@ util_expand_msglist (const int argc, char **argv, int **list)
}
else
{
int j = 0, hyphen = 0;
int j;
int len = strlen (argv[i]);
for (j=0; j < len; j++)
for (j = 0; j < len; j++)
if (argv[i][j] == '-')
{
hyphen = 1;
if (!hyphen)
*list[lc++] = atoi (argv[i]);
else
break;
}
if (hyphen)
{
if (j != len) /* One argument "x-y". */
{
*list[lc++] = i; /* FIXME: range in argv[i] (x-y) */
int x, y;
char *arg = strdup (argv[i]);
arg[j] = '\0';
x = atoi (arg);
y = atoi (&(arg[j + 1]));
/* In this case, we also have to realloc() the list. */
*list = realloc (*list, (argc + 2) * sizeof (int));
for (; x <= y; x++, lc++)
*list[lc] = x;
free (arg);
}
else if (i == 3) /* 3 arguments "x" "-" "y". */
{
int x, y;
x = *list[lc - 1];
y = atoi (argv[i]);
for (; x <= y; x++, lc++)
*list[lc] = x;
}
else /* Badly form. */
*list[lc++] = atoi (argv[i]);
hyphen = 0;
}
else
*list[lc++] = atoi(argv[i]);
}
}
return lc;
......@@ -146,17 +172,18 @@ int
util_msglist_command (int (*func)(int, char**), int argc, char **argv)
{
int i;
int *list;
int *list = NULL;
int status = 0;
int number = util_expand_msglist (argc, argv, &list);
realcursor = cursor;
for (i = 0; i <= number; i++)
for (i = 0; i < number; i++)
{
cursor = list[i];
if (func (1, argv) != 0)
status = 1;
}
free (list);
cursor = realcursor;
return status;
......
......@@ -657,11 +657,13 @@ pop_get_message (mailbox_t mbox, size_t msgno, message_t *pmsg)
/* Create the message. */
{
message_t msg;
stream_t is;
message_t msg = NULL;
stream_t is = NULL;
if ((status = message_create (&msg, mpm)) != 0
|| (status = stream_create (&is, MU_STREAM_READ, mpm)) != 0)
{
message_destroy (&msg, mpm);
stream_destroy (&is, mpm);
free (mpm);
return status;
}
......@@ -673,11 +675,13 @@ pop_get_message (mailbox_t mbox, size_t msgno, message_t *pmsg)
/* Create the header. */
{
header_t header;
stream_t stream;
header_t header = NULL;
stream_t stream = NULL;
if ((status = header_create (&header, NULL, 0, mpm)) != 0
|| (status = stream_create (&stream, MU_STREAM_READ, mpm)) != 0)
{
header_destroy (&header, mpm);
stream_destroy (&stream, mpm);
message_destroy (&(mpm->message), mpm);
free (mpm);
return status;
......@@ -706,11 +710,13 @@ pop_get_message (mailbox_t mbox, size_t msgno, message_t *pmsg)
/* Create the body and its stream. */
{
stream_t stream;
body_t body;
body_t body = NULL;
stream_t stream = NULL;
if ((status = body_create (&body, mpm)) != 0
|| (status = stream_create (&stream, MU_STREAM_READ, mpm)) != 0)
{
body_destroy (&body, mpm);
stream_destroy (&stream, mpm);
message_destroy (&(mpm->message), mpm);
free (mpm);
return status;
......
......@@ -1090,11 +1090,13 @@ unix_get_message (mailbox_t mbox, size_t msgno, message_t *pmsg)
/* Set the header. */
{
header_t header;
stream_t stream;
header_t header = NULL;
stream_t stream = NULL;
if ((status = header_create (&header, NULL, 0, mum)) != 0
|| (status = stream_create (&stream, MU_STREAM_READ, mum)) != 0)
{
stream_destroy (&stream, mum);
header_destroy (&header, mum);
message_destroy (&msg, mum);
return status;
}
......@@ -1125,12 +1127,14 @@ unix_get_message (mailbox_t mbox, size_t msgno, message_t *pmsg)
/* Prepare the body. */
{
body_t body;
stream_t stream;
body_t body = NULL;
stream_t stream = NULL;
int flags = MU_STREAM_READ;
if ((status = body_create (&body, mum)) != 0
|| (status = stream_create (&stream, flags, mum)) != 0)
{
body_destroy (&body, mum);
stream_destroy (&stream, mum);
message_destroy (&msg, mum);
return status;
}
......