Commit 1cee2a45 1cee2a4592e502a9d49991074b23afd29d4cdd33 by Alain Magloire

from/from.c

new file
1 parent 39429508
......@@ -103,327 +103,83 @@ This document was produced for version @value{VERSION} of @sc{gnu}
@menu
* Introduction:: GNU @sc{Mailutils} Programmer's manual.
* Maildrop:: Creating a maildrop.
* URL:: Unified Ressource Locator.
* Maildrop Macros:: Maildrop Helper Macros.
* Mailbox:: Mailbox API.
* Mailbox Macros:: Mailbox Helper Macros.
* Mailer:: Protocol Use to Send Mail.
* Maildrop:: Creating a Maildrop.
* URL:: Unified Ressource Locator.
* Headers:: Headers API.
* Headers Macros:: Header helper Macros.
* Headers Parsed:: Alternatives for Headers.
* Headers Regex:: ERE with Headers.
* Mime:: Mime API.
* Encoding:: Encoding API.
* RFC1522:: RFC1522
* Quoted Printable:: QP Encoding
* Base64:: B64 Encoding
* Reporting Bugs:: Reporting Bugs.
* Acknowledgement:: Thanks and Credits.
* Concept Index:: Topics in this manual.
* Index:: All @sc{Mailutils} functions.
* Concept Index:: Topics in this Manual.
* Index:: All @sc{Mailutils} Functions.
@end menu
@node Introduction, Maildrop, Top, Top
@node Introduction, Mailbox, Top, Top
@comment node-name, next, previous, up
@chapter Introduction
@cindex Introduction
@sc{gnu} @sc{Mailutils} offers a general purpose library aimed to provide
a rich set of functions for accessing different mail formats and maildrops.
a rich set of functions for accessing different mailbox formats and mailers.
For example writing a simple from command that will list @var{From} and
@var{Subject} of a folder.
@example
/* sfrom, Simple From */
#include <stdio.h>
#include <unistd.h>
#include <string.h>
#include <mailutils.h>
#define MAILDIR "/usr/spool/mail"
#define DATA_SIZE 4096
int main (int argc, char ** argv)
@{
char buffer[DATA_SIZE];
char * data;
maildrop_t mdrop;
mailbox_t mbox;
header_t rfc822;
int status, id, msg_no;
int size = DATA_SIZE;
if (argc == 2)
strncpy (buffer, argv[1], sizeof(buffer);
else
@{
char * maildir = getenv ("MAILDIR");
char * logname = getlogin();
snprintf (buffer, sizeof (buffer), "%s/%s",
(maildir) ? maildir : MAILDIR,
(logname) ? logname : "nobody");
@}
status = maildrop_create (&mdrop, NULL, buffer, 0);
if (status != 0)
@{
fprintf (stderr, "maildrop_create(%s), %s\n",
buffer, strerror(status));
exit (EXIT_FAILURE);
@}
maildrop_get_mailbox (mdrop, &mbox);
status = mailbox_open (mbox);
if (status != 0)
@{
fprintf (stderr, "mailbox_open(%s), %s\n",
buffer, strerror(status));
exit (EXIT_FAILURE);
@}
header_create (HEADER_RFC822 | HEADER_RFC1522, &rfc822);
data = xmalloc (size);
mailbox_stat (mbox, &msg_no, NULL);
for (id = 0; id < msg_no; id++)
@{
mailbox_get_header_size (mbox, id, &hd_size));
if (hd_size > size)
@{
data = xrealloc (data, hd_size);
size = hd_size;
@}
mailbox_get_header (mbox, id, data, size);
header_get_value(rfc822, "From", buffer, sizeof (buffer);
printf ("%s \t", buffer);
header_get_value(rfc822, "Subject", buffer, sizeof (buffer);
printf ("%s\n", buffer);
@}
free (data);
header_destroy (rfc822);
return maildrop_destroy (mdrop);
@}
@include sfrom.c.texi
@end example
@node Maildrop, URL, Introduction, Top
@comment node-name, next, previous, up
@chapter Maildrop
@cindex Maildrop
For sending or receiving mail you must create a maildrop indicating the
address/folder, this is done by calling @code{maildrop_create} and
giving it an @var{url}.
@findex maildrop_create
@findex maildrop_destroy
@example
int maildrop_create (maildrop_t * @var{mid}, const char * @var{url}
char * @var{address}, int @var{flags})
int maildrop_destroy (maildrop_t * @var{mid})
@end example
@noindent
@var{mid} will contain maildrop id,
@var{url} a url object,
@var{addr} is a string that contains the maildrop address,
@var{flags} specify execution flags.
@example
struct _maildrop
@{
/* PRIVATE */
url_t url;
mailbox_t mbox;
/* PUBLIC */
int (*_get_mailbox) (maildrop_t, mailbox_t *);
int (*_get_url) (maildrop_t, url_t *);
@};
typedef struct _maildrop * maildrop_t;
@end example
@node URL, Maildrop Macros, Maildrop, Top
@comment node-name, next, previous, up
@subsection URL
@cindex URL
See rfc2368, rfc2369, rfc2384
@node Maildrop Macros, Mailbox, URL, Top
@comment node-name, next, previous, up
@subsection Maildrop Macros
@cindex Maildrop Macros
The predefined macros --taken directly from @file{mailutils.h}--are:
@findex maildrop_get_mailbox
@findex maildrop_get_url
@example
__inline__ int maildrop_get_mailbox (maildrop_t mdrop, mailbox_t * mbox)
@{ *mbox = mdrop->mbox; return (*mbox) 0 : EINVAL; @}
__inline__ int maildrop_get_url (maildrop_t mdrop, url_t * url)
@{ *url = mdrop->url; return (*url) 0 : EINVAL; @}
@end example
@node Mailbox, Mailbox Macros, Maildrop Macros, Top
@node Mailbox, Mailer, Introduction, Top
@comment node-name, next, previous, up
@chapter Mailbox
@cindex Mailbox
Those are different formats to access a folder.
@table @samp
@item POP3
Post Office Protocol, Not Implemented.
@item IMAP
Not Implemented
@item QMAIL
Not Implemented
@item UNIX
Sketchy
@end table
@findex mailbox_create
@findex mailbox_destroy
@example
int mailbox_create (mailbox_t * @var{mailbox}, int @var{flags})
int maildrop_destroy (mailbox_t * @var{mailbox})
@end example
@include mailbox.texi
All implementation shall provide the minimum set of the API :
@example
struct _mailbox
@{
/* Private Data */
.....
/* Public API */
int (*_open) (url_t);
int (*_close) (mailbox_t);
int (*_set_header) (mailbox_t, size_t *, char *, size_t);
int (*_get_header) (mailbox_t, size_t, char *, size_t);
int (*_get_header_size) (mailbox_t, size_t, size_t *);
int (*_delete) (mailbox_t, size_t);
int (*_undelete) (mailbox_t, size_t);
int (*_set_body) (mailbox_t, size_t, char *, size_t);
int (*_get_body) (mailbox_t, size_t, char *, size_t);
int (*_get_body_size) (mailbox_t, size_t, size_t *);
int (*_set_timeout) (mailbox_t, size_t);
int (*_get_timeout) (mailbox_t, size_t *);
int (*_send_envelop) (mailbox_t, size_t, size_t);
int (*_stat) (mailbox_t, size_t *, size_t *);
int (*_update) (mailbox_t);
@};
typedef struct _mailbox * mailbox_t;
@end example
@node Mailbox Macros, Headers, Mailbox, Top
@node Mailer, Maildrop , Mailbox, Top
@comment node-name, next, previous, up
@subsection Mailbox Macros
@cindex Mailbox Macros
@chapter Mailer
@cindex Mailer
@findex maildrop_get_header
@findex maildrop_get_header_size
@example
__inline__ int mailbox_get_header (mailbox_t mbox, int id, char * hdr, int sz)
@{ return mbox->_get_header (mbox, id, hdr, sz); @}
@include mailer.texi
__inline__ int mailbox_get_header_size (mailbox_t mbox, int id, int * sz)
@{ return mbox->_get_header_size (mbox, id, sz); @}
@end example
@node Headers, Headers Macros, Mailbox Macros, Top
@node Maildrop , URL, Mailer, Top
@comment node-name, next, previous, up
@chapter Headers
@cindex Headers
So far we plan support for RFC822 and RFC1522;
@example
int header_create (int flags, header_t * header);
int header_destroy (header_t header);
struct _header
@{
/* Private */
...
/* Public */
char * (*_get_value) (header_t, char *);
@};
typedef struct _header * header_t;
@end example
@node Headers Macros, Headers Parsed, Headers, Top
@comment node-name, next, previous, up
@subsection Headers Macros
@cindex Headers Macros
@chapter Maildrop
@cindex Maildrop
@example
__inline__ int header_get_value (header_t hdr, char * h, char buf, size_t sz)
@{ return hdr->_get_value (hdr, h, buf, sz); @}
@end example
@include maildrop.texi
@node Headers Parsed, Headers Regex, Headers Macros, Top
@node URL, Headers , Maildrop, Top
@comment node-name, next, previous, up
@subsection Headers Parsed
@cindex Headers Parsed
@chapter URL
@cindex URL
Return a hash table, a dictionnary, ???
@include url.texi
@node Headers Regex, Mime, Headers Parsed, Top
@node Headers, Mime, URL , Top
@comment node-name, next, previous, up
@subsection Headers Parsed
@cindex Headers Parsed
@chapter Headers
@cindex Headers
Using regular expression ?
@include headers.texi
@node Mime, Encoding, Headers Regex, Top
@node Mime , Encoding, Headers, Top
@comment node-name, next, previous, up
@chapter Mime
@subsection Mime
@cindex Mime
Mime stuff in the Body.
@include mime.texi
@node Encoding, RFC1522, Mime, Top
@node Encoding, Reporting Bugs, Mime, Top
@comment node-name, next, previous, up
@chapter Encoding
@cindex Encoding
@node RFC1522, Quoted Printable, Encoding, Top
@comment node-name, next, previous, up
@subsection RFC1522
@cindex RFC1522
@node Quoted Printable, Base64, RFC1522, Top
@comment node-name, next, previous, up
@subsection Quoted Printable
@cindex Quoted Printable
@node Base64, Reporting Bugs, Quoted Printable, Top
@comment node-name, next, previous, up
@subsection Base64
@cindex Base64
@include encoding.texi
@node Reporting Bugs, Acknowledgement, Base64, Top
@node Reporting Bugs, Acknowledgement, Encoding, Top
@comment node-name, next, previous, up
@chapter Reporting Bugs
@cindex Reporting Bugs
......
/* GNU mailutils - a suite of utilities for electronic mail
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
the Free Software Foundation; either version 2, or (at your option)
any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program; if not, write to the Free Software
Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA. */
/**
*
* Created as an example of using libmailbox
* Sean 'Shaleh' Perry <shaleh@debian.org>, 1999
* Alain Magloire alainm@gnu.org
*
**/
#ifdef HAVE_CONFIG_H
# include <config.h>
#endif
#include <mailbox.h>
#include <sys/types.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#ifdef HAVE_PATHS_H
# include <paths.h>
#endif
#include <paths.h>
#ifndef _PATH_MAILDIR
# define _PATH_MAILDIR "/var/spool/mail"
#endif
#ifndef VERSION
# define VERSION "unknow"
#endif
#include "getopt.h"
/* Short options. */
static char const short_options[] =
"H:s:m:hv";
static int opt_from;
static int opt_to;
static int opt_cc;
static int opt_date;
static int opt_subject;
static int opt_status_new;;
static int opt_status_read;;
static int opt_status_old;;
static int opt_size;
static int opt_number;
static int opt_mailbox;
/* long options equivalence */
static struct option long_options[] =
{
{"date", no_argument, &opt_date, 1},
{"from", no_argument, &opt_from, 1},
{"to", no_argument, &opt_to, 1},
{"cc", no_argument, &opt_cc, 1},
{"subject", no_argument, &opt_subject, 1},
{"header", required_argument, NULL, 'H'},
{"status", required_argument, NULL, 's'},
{"size", no_argument, &opt_size, 1},
{"number", no_argument, &opt_number, 1},
{"mailbox", required_argument, &opt_mailbox, 'm'},
{"help", no_argument, NULL, 'h'},
{"version", no_argument, NULL, 'v'},
{NULL, 0, NULL, 0}
};
/* program name */
static char *program;
static void
usage (int type)
{
switch (type)
{
case 1:
printf("%s (GNU mailutils), version %s\n", program, VERSION);
break;
case 2:
/* Fall Through */
default:
printf ("Usage: %s [OPTION] [mailbox]\n", program);
printf ("Retrieve information from a mailbox\n\
\n\
--date equivalent to --header=Date\n\
--from equivalent to --header=From\n\
--to equivalent to --header=To\n\
--cc equivalent to --header=Cc\n\
--subject equivalent to --header=Subject\n\
--header=HEADER Specify the header to display\n\
HEADER is 'Date', 'From', 'To', 'Cc', 'Subject'\n\
default is --header=From --header=Subject\n\
--status=STATUS Display only message with a given status\n\
STATUS is 'new', 'read', 'unread'\n\
defaut is --status=new --status=read --status=unread\n\
--size Display mailbox size\n\
--number Display total of number of messages\n\
--mailbox=MAILBOX Specify another mailbox\n\
MAILBOX is url(pop://pop.server/user),
a file /var/mail/user, or user.
-v, --version Print version information and exit\n\
-h, --help Show this message\n\
\n\
Mailbox :
$MAIL is check for the default mailbox path, if not set
the environ variables $USER or $LOGNAME are use in the default mail spool.\n\
\n\
Report bugs to <bug-mailutils@gnu.org>.\n");
}
}
int
main(int argc, char *argv[])
{
mailbox_t mbox;
size_t rvalue, i;
size_t count = 0, size;
char *user = NULL;
char *mailbox_name = NULL;
int opt;
char buffer[BUFSIZ];
/* set program name */
program = argv[0];
if (program && strrchr (program, '/'))
{
program = strrchr (program, '/') + 1;
}
while ((opt = getopt_long (argc, argv, short_options, long_options, NULL))
!= -1)
{
switch (opt)
{
case 'H':
if (strcasecmp (optarg, "From") == 0)
opt_from = 1;
else if (strcasecmp (optarg, "To") == 0)
opt_to = 1;
else if (strcasecmp (optarg, "Cc") == 0)
opt_cc = 1;
else if (strcasecmp (optarg, "Date") == 0)
opt_date = 1;
else if (strcasecmp (optarg, "Subject") == 0)
opt_subject = 1;
else
{
fprintf (stderr, "Unknown header\n");
}
break;
case 's':
if (strcasecmp (optarg, "new") == 0)
opt_status_new = 1;
else if (strcasecmp (optarg, "read") == 0)
opt_status_read = 1;
else if (strcasecmp (optarg, "old") == 0)
opt_status_old = 1;
else
{
fprintf (stderr, "Unknown status\n");
}
break;
case 'm':
mailbox_name = optarg;
break;
case 'v':
usage (1);
exit (0);
break;
case 'h':
usage (2);
exit (0);
break;
default:
usage (2);
exit (1);
break;
}
}
/* have an argument */
if (optind > 0)
{
mailbox_name = argv[optind];
/* is it a URL */
if (strchr (mailbox_name, ':') == NULL)
{
/* is it a filename */
if (mailbox_name[0] != '/')
{
user = mailbox_name; /* a user name */
mailbox_name = NULL;
}
}
}
else if (getenv ("MAIL"))
{
mailbox_name = getenv ("MAIL");
}
else
{
user = (getenv ("LOGNAME")) ? getenv ("LOGNAME") : getenv ("USER");
if (user == NULL)
{
fprintf (stderr, "who am I?\n");
exit (1);
}
}
if (user)
{
snprintf (buffer, sizeof(buffer), "%s/%s", _PATH_MAILDIR, user);
mailbox_name = buffer;
}
if (mailbox_init (&mbox, mailbox_name, 0) != 0
|| mailbox_open (mbox, MU_MB_RDONLY) != 0)
{
fprintf (stderr, "could not open\n");
exit (2);
}
mailbox_scan (mbox, &count);
for(i = 0; i < count; ++i) {
rvalue = mailbox_get_header (mbox, i, 0, buffer, sizeof (buffer), &size);
if (rvalue != 0)
{
fprintf (stderr, "header %s\n", strerror (rvalue));
exit(2);
}
printf("%s\n", buffer);
}
mailbox_close(mbox);
mailbox_destroy(&mbox);
exit(0);
}