Commit a94d1cca a94d1cca434bfddec216710a4863aecdbb2522e3 by Alain Magloire

_cpystr.c attribute.c attribute.h attribute0.h auth.c auth.h

 	cpystr.h locker.c locker.h message.c message.h message0.h
 	registrar.c registrar.h registrar0.h rfc822.c url.c url.h
 	url0.h url_imap.c url_mail.c url_mbox.c url_mdir.c url_mh.c
 	url_mmdf.c url_pop.c url_unix.c
still young in the implementation.
1 parent bd507cf1
/* 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 Library 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 Library General Public License for more details.
You should have received a copy of the GNU Library General Public License
along with this program; if not, write to the Free Software
Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA. */
#include <cpystr.h>
#include <string.h>
size_t
_cpystr (char *dst, const char *src, size_t size)
{
size_t len = src ? strlen (src) : 0 ;
if (dst == NULL || size == 0)
return len;
if (len >= size)
len = size - 1;
memcpy (dst, src, len);
dst[len] = '\0';
return len;
}
/* 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 Library 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 Library General Public License for more details.
You should have received a copy of the GNU Library General Public License
along with this program; if not, write to the Free Software
Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA. */
#include <attribute0.h>
#include <sys/types.h>
#include <stdlib.h>
#include <errno.h>
int
attribute_init (attribute_t *pattr)
{
attribute_t attr;
if (pattr == NULL)
return EINVAL;
attr = calloc (1, sizeof(*attr));
if (attr == NULL)
return ENOMEM;
*pattr = attr;
return 0;
}
void
attribute_destroy (attribute_t *pattr)
{
if (pattr && *pattr)
{
attribute_t attr = *pattr;
/* no owner we can free */
if (! attr->message)
free (*pattr);
}
return;
}
int
attribute_set_seen (attribute_t attr)
{
if (attr == NULL)
return EINVAL;
attr->flag |= MU_ATTRIBUTE_SEEN;
return 0;
}
int
attribute_set_answered (attribute_t attr)
{
if (attr == NULL)
return EINVAL;
attr->flag|= MU_ATTRIBUTE_ANSWERED;
attr->flag |= MU_ATTRIBUTE_SEEN;
return 0;
}
int
attribute_set_flagged (attribute_t attr)
{
if (attr == NULL)
return EINVAL;
attr->flag |= MU_ATTRIBUTE_FLAGGED;
attr->flag |= MU_ATTRIBUTE_SEEN;
return 0;
}
int
attribute_set_read (attribute_t attr)
{
if (attr == NULL)
return EINVAL;
attr->flag |= MU_ATTRIBUTE_READ;
attr->flag |= MU_ATTRIBUTE_SEEN;
return 0;
}
int
attribute_set_deleted (attribute_t attr)
{
if (attr == NULL)
return EINVAL;
attr->flag |= MU_ATTRIBUTE_DELETED;
return 0;
}
int
attribute_set_draft (attribute_t attr)
{
if (attr == NULL)
return EINVAL;
attr->flag |= MU_ATTRIBUTE_DRAFT;
return 0;
}
int
attribute_set_recent (attribute_t attr)
{
if (attr == NULL)
return EINVAL;
attr->flag |= MU_ATTRIBUTE_RECENT;
return 0;
}
int
attribute_is_seen (attribute_t attr)
{
if (attr == NULL)
return 0;
return attr->flag & MU_ATTRIBUTE_SEEN;
}
int
attribute_is_answered (attribute_t attr)
{
if (attr == NULL)
return 0;
return attr->flag & MU_ATTRIBUTE_ANSWERED;
}
int
attribute_is_flagged (attribute_t attr)
{
if (attr == NULL)
return 0;
return attr->flag & MU_ATTRIBUTE_FLAGGED;
}
int
attribute_is_read (attribute_t attr)
{
if (attr == NULL)
return 0;
return attr->flag & MU_ATTRIBUTE_READ;
}
int
attribute_is_deleted (attribute_t attr)
{
if (attr == NULL)
return 0;
return attr->flag & MU_ATTRIBUTE_DELETED;
}
int
attribute_is_draft (attribute_t attr)
{
if (attr == NULL)
return 0;
return attr->flag & MU_ATTRIBUTE_DRAFT;
}
int
attribute_is_recent (attribute_t attr)
{
if (attr == NULL)
return 0;
return attr->flag & MU_ATTRIBUTE_RECENT;
}
int
attribute_unset_seen (attribute_t attr)
{
if (attr == NULL)
return 0;
attr->flag ^= MU_ATTRIBUTE_SEEN;
attr->flag ^= MU_ATTRIBUTE_ANSWERED;
attr->flag ^= MU_ATTRIBUTE_FLAGGED;
attr->flag ^= MU_ATTRIBUTE_READ;
return 0;
}
int
attribute_unset_answered (attribute_t attr)
{
if (attr == NULL)
return 0;
attr->flag ^= MU_ATTRIBUTE_ANSWERED;
return 0;
}
int
attribute_unset_flagged (attribute_t attr)
{
if (attr == NULL)
return 0;
attr->flag ^= MU_ATTRIBUTE_FLAGGED;
return 0;
}
int
attribute_unset_read (attribute_t attr)
{
if (attr == NULL)
return 0;
attr->flag ^= MU_ATTRIBUTE_READ;
return 0;
}
int
attribute_unset_deleted (attribute_t attr)
{
if (attr == NULL)
return 0;
attr->flag ^= MU_ATTRIBUTE_DELETED;
return 0;
}
int
attribute_unset_draft (attribute_t attr)
{
if (attr == NULL)
return 0;
attr->flag ^= MU_ATTRIBUTE_DRAFT;
return 0;
}
int
attribute_unset_recent (attribute_t attr)
{
if (attr == NULL)
return 0;
attr->flag ^= MU_ATTRIBUTE_RECENT;
return 0;
}
/* 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 Library 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 Library General Public License for more details.
You should have received a copy of the GNU Library General Public License
along with this program; if not, write to the Free Software
Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA. */
#ifndef _ATTRIBUTE_H
#define _ATTRIBUTE_H
#ifdef __cplusplus
extern "C" {
#endif
#ifndef __P
# ifdef __STDC__
# define __P(args) args
# else
# define __P(args) ()
# endif
#endif /*__P */
struct _attribute;
typedef struct _attribute * attribute_t;
extern int attribute_init __P ((attribute_t *));
extern void attribute_destroy __P ((attribute_t *));
extern int attribute_is_seen __P ((attribute_t));
extern int attribute_is_answered __P ((attribute_t));
extern int attribute_is_flagged __P ((attribute_t));
extern int attribute_is_deleted __P ((attribute_t));
extern int attribute_is_draft __P ((attribute_t));
extern int attribute_is_recent __P ((attribute_t));
extern int attribute_is_read __P ((attribute_t));
extern int attribute_set_seen __P ((attribute_t));
extern int attribute_set_answered __P ((attribute_t));
extern int attribute_set_flagged __P ((attribute_t));
extern int attribute_set_deleted __P ((attribute_t));
extern int attribute_set_draft __P ((attribute_t));
extern int attribute_set_recent __P ((attribute_t));
extern int attribute_set_read __P ((attribute_t));
extern int attribute_unset_seen __P ((attribute_t));
extern int attribute_unset_answered __P ((attribute_t));
extern int attribute_unset_flagged __P ((attribute_t));
extern int attribute_unset_deleted __P ((attribute_t));
extern int attribute_unset_draft __P ((attribute_t));
extern int attribute_unset_recent __P ((attribute_t));
extern int attribute_unset_read __P ((attribute_t));
#ifdef __cplusplus
}
#endif
#endif /* _ATTRIBUTE_H */
/* 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 Library 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 Library General Public License for more details.
You should have received a copy of the GNU Library General Public License
along with this program; if not, write to the Free Software
Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA. */
#ifndef _ATTRIBUTE0_H
#define _ATTRIBUTE0_H
#include <attribute.h>
#include <sys/types.h>
#ifdef __cplusplus
extern "C" {
#endif
#ifndef __P
# ifdef __STDC__
# define __P(args) args
# else
# define __P(args) ()
# endif
#endif /*__P */
#define MU_ATTRIBUTE_SEEN ((int)1)
#define MU_ATTRIBUTE_ANSWERED (MU_ATTRIBUTE_SEEN << 1)
#define MU_ATTRIBUTE_FLAGGED (MU_ATTRIBUTE_ANSWERED << 1)
#define MU_ATTRIBUTE_DELETED (MU_ATTRIBUTE_FLAGGED << 1)
#define MU_ATTRIBUTE_DRAFT (MU_ATTRIBUTE_DELETED << 1)
#define MU_ATTRIBUTE_RECENT (MU_ATTRIBUTE_DRAFT << 1)
#define MU_ATTRIBUTE_READ (MU_ATTRIBUTE_RECENT << 1)
struct _attribute
{
size_t flag;
void *message;
};
#ifdef __cplusplus
}
#endif
#endif /* _ATTRIBUTE0_H */
/* 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 Library 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 Library General Public License for more details.
You should have received a copy of the GNU Library General Public License
along with this program; if not, write to the Free Software
Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA. */
#include <auth.h>
#include <cpystr.h>
#include <errno.h>
#include <sys/types.h>
#include <string.h>
#include <stdlib.h>
struct _auth
{
char *login;
char *passwd;
uid_t owner;
gid_t group;
mode_t mode;
};
int
auth_init (auth_t *pauth)
{
auth_t auth;
if (pauth == NULL)
return EINVAL;
auth = calloc (1, sizeof (*auth));
if (auth == NULL)
return ENOMEM;
auth->owner = auth->group = -1;
auth->mode = 0600;
*pauth = auth;
return 0;
}
void
auth_destroy (auth_t *pauth)
{
if (pauth && *pauth)
{
auth_t auth = *pauth;
free (auth->login);
free (auth->passwd);
free (auth);
*pauth = NULL;
}
}
/* login/passwd */
int
auth_get_login (auth_t auth, char *login, size_t len, size_t *n)
{
size_t nwrite = 0;
if (auth == NULL)
return EINVAL;
nwrite = _cpystr (login, auth->login, len);
if (n)
*n = nwrite;
return 0;
}
int
auth_set_login (auth_t auth, const char *login, size_t len)
{
char *log = NULL;
if (auth == NULL)
return EINVAL;
if (login != NULL)
{
log = calloc (len + 1, sizeof (char));
if (log == NULL)
return ENOMEM;
memcpy (log, login, len);
}
free (auth->login);
auth->login = log;
return 0;
}
int
auth_get_passwd (auth_t auth, char *passwd, size_t len, size_t *n)
{
size_t nwrite = 0;
if (auth == NULL)
return EINVAL;
nwrite = _cpystr (passwd, auth->passwd, len);
if (n)
*n = nwrite;
return 0;
}
int
auth_set_passwd (auth_t auth, const char *passwd, size_t len)
{
char *pass = NULL;
if (auth == NULL)
return EINVAL;
if (passwd != NULL)
{
char *pass = calloc (len + 1, sizeof (char));
if (pass == NULL)
return ENOMEM;
memcpy (pass, passwd, len);
}
free (auth->passwd);
auth->passwd = pass;
return 0;
}
/* owner and group */
int
auth_get_owner (auth_t auth, uid_t *powner)
{
if (auth == NULL)
return EINVAL;
if (powner)
*powner = auth->owner;
return 0;
}
int
auth_set_owner (auth_t auth, uid_t owner)
{
if (auth == NULL)
return 0;
auth->owner = owner;
return 0;
}
int
auth_get_group (auth_t auth, gid_t *pgroup)
{
if (auth == NULL)
return EINVAL;
if (pgroup)
*pgroup = auth->group;
return 0;
}
int
auth_set_group (auth_t auth, gid_t group)
{
if (auth == NULL)
return EINVAL;
auth->group = group;
return 0;
}
int
auth_get_mode (auth_t auth, mode_t *pmode)
{
if (auth == NULL)
return EINVAL;
if (pmode)
*pmode = auth->mode;
return 0;
}
int
auth_set_mode (auth_t auth, mode_t mode)
{
if (auth == NULL)
return EINVAL;
auth->mode = mode;
return 0;
}
/* 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 Library 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 Library General Public License for more details.
You should have received a copy of the GNU Library General Public License
along with this program; if not, write to the Free Software
Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA. */
#ifndef _AUTH_H
#define _AUTH_H
#include <sys/types.h>
#ifndef __P
#ifdef __STDC__
#define __P(args) args
#else
#define __P(args) ()
#endif
#endif /*__P */
#ifdef _cpluscplus
extern "C" {
#endif
/* forward declaration */
struct _auth;
typedef struct _auth *auth_t;
extern int auth_init __P ((auth_t *));
extern void auth_destroy __P ((auth_t *));
/* login/passwd */
extern int auth_get_login __P ((auth_t, char *login,
size_t len, size_t *n));
extern int auth_set_login __P ((auth_t, const char *login, size_t len));
extern int auth_get_passwd __P ((auth_t, char *passwd,
size_t len, size_t *n));
extern int auth_set_passwd __P ((auth_t, const char *passwd, size_t len));
/* owner group mode*/
extern int auth_get_owner __P ((auth_t, uid_t *uid));
extern int auth_set_owner __P ((auth_t, uid_t uid));
extern int auth_get_group __P ((auth_t, gid_t *gid));
extern int auth_set_group __P ((auth_t, gid_t gid));
extern int auth_get_mode __P ((auth_t, mode_t *mode));
extern int auth_set_mode __P ((auth_t, mode_t mode));
#ifdef _cpluscplus
}
#endif
#endif /* _AUTH_H */
/* 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 Library 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 Library General Public License for more details.
You should have received a copy of the GNU Library General Public License
along with this program; if not, write to the Free Software
Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA. */
#ifndef _CPYSTR_H
#define _CPYSTR_H
#include <sys/types.h>
#ifdef __cplusplus
extern "C" {
#endif
#ifndef __P
# if __STDC__
# define __P(x) x
# else
# define __P(x)
# endif
#endif
extern size_t _cpystr __P ((char *dst, const char *src, size_t size));
#ifdef __cplusplus
}
#endif
#endif
/* 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 Library 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 Library General Public License for more details.
You should have received a copy of the GNU Library General Public License
along with this program; if not, write to the Free Software
Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA. */
#include <locker.h>
#include <errno.h>
#include <sys/types.h>
#include <stdlib.h>
#include <string.h>
/*
* Waiting for Brian E. to implement this.
*/
struct _locker
{
char *filename;
size_t filename_len;
int lock;
};
int
locker_init (locker_t *plocker, char *filename, size_t len)
{
locker_t l;
if (plocker == NULL)
return EINVAL;
if (filename == NULL || len == 0)
return EINVAL;
l = malloc (sizeof(*l));
if (l == NULL)
return ENOMEM;
l->filename = calloc (len + 1, sizeof(char));
if (l->filename == NULL)
{
free (l);
return ENOMEM;
}
memcpy (l->filename, filename, len);
l->lock = 0;
l->filename_len = len;
*plocker = l;
return 0;
}
void
locker_destroy (locker_t *plocker)
{
if (plocker && *plocker)
{
free ((*plocker)->filename);
free (*plocker);
*plocker = NULL;
}
}
int
locker_lock (locker_t locker, int flags)
{
(void)flags;
if (locker == NULL)
return EINVAL;
locker->lock++;
return 0;
}
int
locker_touchlock (locker_t locker)
{
if (locker == NULL)
return EINVAL;
return 0;
}
int
locker_unlock (locker_t locker)
{
if (locker == NULL)
return EINVAL;
locker->lock--;
return 0;
}
/* 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 Library 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 Library General Public License for more details.
You should have received a copy of the GNU Library General Public License
along with this program; if not, write to the Free Software
Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA. */
#ifndef _LOCKER_H
#define _LOCKER_H
#include <sys/types.h>
#ifdef __cplusplus
extern "C" {
#endif
#ifndef __P
# ifdef __STDC__
# define __P(args) args
# else
# define __P(args) ()
# endif
#endif /*__P */
struct _locker;
typedef struct _locker *locker_t;
extern int locker_init __P ((locker_t *, char *filename, size_t len));
extern void locker_destroy __P ((locker_t *));
#define MU_LOCKER_RDLOCK 0
#define MU_LOCKER_WRLOCK 1
extern int locker_lock __P ((locker_t, int flag));
extern int locker_touchlock __P ((locker_t));
extern int locker_unlock __P ((locker_t));
#ifdef __cplusplus
}
#endif
#endif /* _MAILBOX_H */
/* 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 Library 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 Library General Public License for more details.
You should have received a copy of the GNU Library General Public License
along with this program; if not, write to the Free Software
Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA. */
#include <header0.h>
#include <attribute0.h>
#include <message0.h>
#include <mailbox0.h>
#include <errno.h>
#include <stdio.h>
#include <stdlib.h>
int
message_init (message_t *pmsg)
{
message_t msg;
if (pmsg == NULL)
return EINVAL;
msg = calloc (1, sizeof (*msg));
if (msg == NULL)
return ENOMEM;
*pmsg = msg;
return 0;
}
void
message_destroy (message_t *pmsg)
{
if (pmsg && *pmsg)
{
message_t msg = *pmsg;
/*
* The message has an mailbox owner
* let the mailbox destroy when it finishes
*/
if (msg->mailbox)
{
*pmsg = NULL;
return;
}
/* is the header own by us ? */
if (msg->header->message == msg)
msg->header->message = NULL;
header_destroy (&(msg->header));
/* is the attribute own by us ? */
if (msg->attribute->message == msg)
msg->attribute->message = NULL;
attribute_destroy (&(msg->attribute));
if (msg->body)
{
body_t body = msg->body;
if (body->file)
fclose (body->file);
free (body->content);
}
free (msg->body);
if (msg->part_num > 0)
{
size_t i;
for (i = 0; i < msg->part_num; i++)
message_destroy (&(msg->parts[i]));
}
free (msg->parts);
free (msg);
*pmsg = NULL;
}
}
int
message_get_header (message_t msg, header_t *phdr)
{
int err = 0;
int nread, n = 0;
char *pbuf = NULL, *tbuf = NULL;
char buf [BUFSIZ];
int off = 0;
if (phdr == NULL || msg == NULL)
return EINVAL;
if (msg->header != NULL)
{
*phdr = msg->header;
return 0;
}
if (msg->mailbox == NULL)
return EINVAL;
do
{
nread = mailbox_get_header (msg->mailbox, msg->num, buf, sizeof(buf),
off, &err);
if (err == EAGAIN || err == EINPROGRESS)
continue;
else if (err != 0)
{
free (pbuf);
return err;
}
off += nread;
tbuf = realloc (pbuf, off);
if (tbuf == NULL)
{
free (pbuf);
return ENOMEM;
}
else
pbuf = tbuf;
memcpy (pbuf + n, buf, nread);
n = nread;
} while (nread > 0);
err = header_init (&(msg->header), pbuf, off, MU_HEADER_RFC822);
if (err == 0)
{
/* we own it */
msg->header->message = msg;
*phdr = msg->header;
}
free (pbuf);
return err;
}
int
message_set_header (message_t msg, header_t hdr)
{
if (msg == NULL || msg->mailbox == NULL)
return EINVAL;
if (msg->header)
{
if (msg->header->message == msg)
msg->header->message = NULL;
header_destroy (&(msg->header));
}
msg->header = hdr;
return 0;
}
ssize_t
message_get_content (message_t msg, char *buf, size_t buflen,
off_t off, int *err)
{
if (msg == NULL || msg->mailbox == NULL)
{
if (err)
*err = EINVAL;
return -1;
}
return mailbox_get_body (msg->mailbox, msg->num, buf, buflen, off, err);
}
int
message_set_content (message_t msg, char *buf, size_t buflen)
{
(void)msg;(void)buf; (void)buflen;
return ENOSYS;
}
int
message_size (message_t msg, size_t *size)
{
if (msg == NULL || size == NULL)
return EINVAL;
if (msg->mailbox)
{
size_t hs, bs;
int status;
status = mailbox_get_size (msg->mailbox, msg->num, &hs, &bs);
if (status == 0)
{
if (size)
*size = hs + bs;
}
return status;
}
return ENOSYS;
}
/* 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 Library 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 Library General Public License for more details.
You should have received a copy of the GNU Library General Public License
along with this program; if not, write to the Free Software
Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA. */
#ifndef _MESSAGE_H
#define _MESSAGE_H
#include <header.h>
#include <attribute.h>
#include <sys/types.h>
#ifndef __P
# ifdef __STDC__
# define __P(args) args
# else
# define __P(args) ()
# endif
#endif /* __P */
#ifdef _cpluscplus
extern "C" {
#endif
/* forward declaration */
struct _message;
typedef struct _message *message_t;
extern int message_init __P ((message_t *));
extern void message_destroy __P ((message_t *));
extern int message_get_header __P ((message_t, header_t *));
extern int message_set_header __P ((message_t, header_t));
extern ssize_t message_get_content __P ((message_t, char *,
size_t, off_t, int *));
extern int message_set_content __P ((message_t, char *, size_t));
extern int message_is_multipart __P ((message_t));
extern int message_get_part_count __P ((message_t, size_t *));
extern int message_get_part __P ((message_t, size_t, message_t *));
extern int message_add_part __P ((message_t, message_t));
extern int message_get_size __P ((message_t, size_t *));
extern int message_get_attribute __P ((message_t, attribute_t *));
extern int message_set_attribute __P ((message_t, attribute_t));
#ifdef _cpluscplus
}
#endif
#endif /* _MESSAGE_H */
/* 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 Library 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 Library General Public License for more details.
You should have received a copy of the GNU Library General Public License
along with this program; if not, write to the Free Software
Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA. */
#ifndef _MESSAGE0_H
#define _MESSAGE0_H
#include <attribute.h>
#include <header.h>
#include <message.h>
#include <mailbox.h>
#include <sys/types.h>
#include <stdio.h>
#ifdef _cpluscplus
extern "C" {
#endif
struct _body
{
FILE *file;
char *content;
size_t content_len;
};
typedef struct _body * body_t;
/* forward declaration */
struct _message
{
mailbox_t mailbox;
header_t header;
message_t *parts;
size_t part_num;
body_t body;
size_t num;
attribute_t attribute;
int (*_get_header) __P ((message_t msg, header_t *hdr));
int (*_set_header) __P ((message_t msg, header_t hdr));
int (*_get_attribute) __P ((message_t msg, attribute_t *attr));
int (*_set_attribute) __P ((message_t msg, attribute_t attr));
int (*_get_content) __P ((message_t msg, char *buf, size_t len,
size_t *nread, off_t offset));
int (*_set_content) __P ((message_t msg, char *buf, size_t len));
int (*_size) __P ((message_t msg, size_t *size));
int (*_clone) __P ((message_t msg, message_t *cmsg));
};
#ifdef _cpluscplus
}
#endif
#endif /* _MESSAGE_H */
/* 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 Library 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 Library General Public License for more details.
You should have received a copy of the GNU Library General Public License
along with this program; if not, write to the Free Software
Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA. */
#ifdef HAVE_CONFIG_H
#include <config.h>
#endif
#include <registrar0.h>
#include <stdlib.h>
#include <string.h>
#include <errno.h>
/*
Builtin mailbox types.
A circular list is use for the builtin.
Proper locking is not done when accessing the list.
FIXME: not thread-safe. */
static struct _registrar registrar [] = {
{ NULL, NULL, 0, &registrar[1] }, /* sentinel, head list */
{ &_url_mbox_registrar, &_mailbox_mbox_registrar, 0, &registrar[2] },
{ &_url_unix_registrar, &_mailbox_unix_registrar, 0, &registrar[3] },
{ &_url_maildir_registrar, &_mailbox_maildir_registrar, 0, &registrar[4] },
{ &_url_mmdf_registrar, &_mailbox_mmdf_registrar, 0, &registrar[5] },
{ &_url_pop_registrar, &_mailbox_pop_registrar, 0, &registrar[6] },
{ &_url_imap_registrar, &_mailbox_imap_registrar, 0, &registrar[0] },
};
static void
free_ureg (struct url_registrar *ureg)
{
if (ureg)
{
free (ureg->scheme);
free (ureg);
}
}
static void
free_mreg (struct mailbox_registrar *mreg)
{
if (mreg)
{
free (mreg->name);
free (mreg);
}
}
int
registrar_add (struct url_registrar *new_ureg,
struct mailbox_registrar *new_mreg, int *id)
{
struct _registrar *entry;
struct url_registrar *ureg = NULL;
struct mailbox_registrar *mreg;
/* Must registrar a mailbox */
if (new_mreg == NULL)
return EINVAL;
/* Mailbox */
mreg = calloc (1, sizeof (*mreg));
if (mreg == NULL)
return ENOMEM;
if (new_mreg->name)
{
mreg->name = strdup (new_mreg->name);
if (mreg->name == NULL)
{
free (mreg);
return ENOMEM;
}
}
mreg->_init = new_mreg->_init;
mreg->_destroy = new_mreg->_destroy;
/* URL */
if (new_ureg)
{
ureg = calloc (1, sizeof (*ureg));
if (ureg == NULL)
{
free_mreg (mreg);
return ENOMEM;
}
if (new_ureg->scheme)
{
ureg->scheme = strdup (new_ureg->scheme);
if (ureg->scheme == NULL)
{
free_mreg (mreg);
free_ureg (ureg);
return ENOMEM;
}
}
ureg->_init = new_ureg->_init;
ureg->_destroy = new_ureg->_destroy;
}
/* Register them to the list */
entry = calloc (1, sizeof (*entry));
if (entry == NULL)
{
free_mreg (mreg);
free_ureg (ureg);
return ENOMEM;
}
entry->ureg = ureg;
entry->mreg = mreg;
entry->is_allocated = 1;
entry->next = registrar->next;
registrar->next = entry;
if (id)
*id = (int)entry;
return 0;
}
int
registrar_remove (int id)
{
struct _registrar *current, *previous;
for (previous = registrar, current = registrar->next;
current != registrar;
previous = current, current = current->next)
{
if ((int)current == id)
{
previous->next = current->next;
if (current->is_allocated)
{
free_ureg (current->ureg);
free_mreg (current->mreg);
}
free (current);
return 0;;
}
}
return EINVAL;
}
int
registrar_get (int id,
struct url_registrar **ureg, struct mailbox_registrar **mreg)
{
struct _registrar *current;
for (current = registrar->next; current != registrar;
current = current->next)
{
if ((int)current == id)
{
if (mreg)
*mreg = current->mreg;
if (ureg)
*ureg = current->ureg;
return 0;
}
}
return EINVAL;
}
int
registrar_list (struct url_registrar **ureg, struct mailbox_registrar **mreg,
int *id, registrar_t *reg)
{
struct _registrar *current;
if (reg == NULL)
return EINVAL;
current = *reg;
if (current == NULL)
current = registrar;
if (current->next == registrar)
return -1;
if (ureg)
*ureg = current->ureg;
if (mreg)
*mreg = current->mreg;
if (id)
*id = (int)current;
*reg = current->next;
return 0;
}
/* 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 Library 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 Library General Public License for more details.
You should have received a copy of the GNU Library General Public License
along with this program; if not, write to the Free Software
Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA. */
#ifndef _REGISTRAR_H
#define _REGISTRAR_H
#include <sys/types.h>
#include <url.h>
#include <mailbox.h>
#ifndef __P
# ifdef __STDC__
# define __P(args) args
# else
# define __P(args) ()
# endif
#endif /*__P */
#ifdef _cpluscplus
extern "C" {
#endif
struct url_registrar
{
char *scheme;
int (*_init) __P ((url_t *, const char * name));
void (*_destroy) __P ((url_t *));
};
struct mailbox_registrar
{
char *name;
int (*_init) __P ((mailbox_t *, const char *name));
void (*_destroy) __P ((mailbox_t *));
};
struct _registrar;
typedef struct _registrar* registrar_t;
/* mailbox registration */
extern int registrar_add __P ((struct url_registrar *ureg,
struct mailbox_registrar *mreg, int *id));
extern int registrar_remove __P ((int id));
extern int registrar_get __P ((int id, struct url_registrar **ureg,
struct mailbox_registrar **mreg));
extern int registrar_list __P ((struct url_registrar **ureg,
struct mailbox_registrar **mreg,
int *id, registrar_t *reg));
#ifdef _cpluscplus
}
#endif
#endif /* _REGISTRAR_H */
/* 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 Library 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 Library General Public License for more details.
You should have received a copy of the GNU Library General Public License
along with this program; if not, write to the Free Software
Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA. */
#ifndef _REGISTRAR0_H
#define _REGISTRAR0_H
#include <registrar.h>
#ifdef __cplusplus
extern "C" {
#endif
#ifndef __P
# ifdef __STDC__
# define __P(args) args
# else
# define __P(args) ()
# endif
#endif /*__P */
/*
Builtin mailbox types.
A circular list is use for the builtin.
Proper locking is not done when accessing the list.
FIXME: not thread-safe. */
struct _registrar
{
struct url_registrar *ureg;
struct mailbox_registrar *mreg;
int is_allocated;
struct _registrar *next;
};
/* IMAP */
extern struct mailbox_registrar _mailbox_imap_registrar;
extern struct url_registrar _url_imap_registrar;
/* MBOX */
extern struct mailbox_registrar _mailbox_mbox_registrar;
extern struct url_registrar _url_mbox_registrar;
/* MAILTO */
extern struct mailbox_registrar _mailbox_mailto_registrar;
extern struct url_registrar _url_mailto_registrar;
/* MDIR */
extern struct mailbox_registrar _mailbox_maildir_registrar;
extern struct url_registrar _url_maildir_registrar;
/* MMDF */
extern struct mailbox_registrar _mailbox_mmdf_registrar;
extern struct url_registrar _url_mmdf_registrar;
/* UNIX */
extern struct mailbox_registrar _mailbox_unix_registrar;
extern struct url_registrar _url_unix_registrar;
/* POP */
extern struct mailbox_registrar _mailbox_pop_registrar;
extern struct url_registrar _url_pop_registrar;
#ifdef __cplusplus
}
#endif
#endif /* _REGISTRAR0_H */
/* 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 Library 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 Library General Public License for more details.
You should have received a copy of the GNU Library General Public License
along with this program; if not, write to the Free Software
Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA. */
#ifdef HAVE_CONFIG_H
# include "config.h"
#endif
#include <header0.h>
#include <string.h>
#include <stdlib.h>
#include <errno.h>
static int rfc822_parse (header_t h, const char *blurb, size_t len);
static int rfc822_set_value (header_t h, const char *fn, const char *fb,
size_t n, int replace);
ssize_t rfc822_get_data (header_t h, char *buf, size_t buflen,
off_t off, int *err);
static int rfc822_get_value (header_t h, const char *fn,
char *fb, size_t len, size_t *n);
struct _rfc822
{
char *blurb;
size_t blurb_len;
size_t hdr_count;
struct _hdr *hdr;
};
typedef struct _rfc822 * rfc822_t;
int
rfc822_init (header_t *ph, const char *blurb, size_t len)
{
header_t h;
int status;
h = calloc (1, sizeof (*h));
if (h == NULL)
return ENOMEM;
h->_init = rfc822_init;
h->_destroy = rfc822_destroy;
h->_parse = rfc822_parse;
h->_get_value = rfc822_get_value;
h->_set_value = rfc822_set_value;
h->_get_data = rfc822_get_data;
status = h->_parse (h, blurb, len);
if (status != 0)
free (h);
*ph = h;
return status;
}
void
rfc822_destroy (header_t *ph)
{
if (ph && *ph)
{
header_t h = *ph;
/* own by a message */
if (h->message)
{
*ph = NULL;
return;
}
if (h->data)
{
rfc822_t rfc = (rfc822_t)h->data;
free (rfc->hdr);
free (rfc->blurb);
free (rfc);
}
free (h);
*ph = NULL;
}
}
static int
rfc822_parse (header_t h, const char *blurb, size_t len)
{
rfc822_t rfc;
char *header_end;
char *header_start;
char *header_start2;
struct _hdr *hdr;
if (h == NULL || blurb == NULL || len == 0)
return EINVAL;
rfc = calloc (1, sizeof (*rfc));
if (rfc == NULL)
return ENOMEM;
rfc->blurb = calloc (1, len);
if (rfc->blurb == NULL)
{
free (rfc);
return ENOMEM;
}
rfc->blurb_len = len;
memcpy (rfc->blurb, blurb, len);
for (header_start = rfc->blurb;; header_start = ++header_end)
{
/* get a header, a header is :
field-name ':' field-body1
[ ' ' '\t' field-body2 ] '\r' '\n'
*/
for (header_start2 = header_start;;header_start2 = ++header_end)
{
header_end = memchr (header_start2, '\n', len);
if (header_end == NULL)
break;
else
{
len -= (header_end - header_start2 + 1);
if (len == 0)
{
header_end = NULL;
break;
}
if (header_end[1] != ' '
&& header_end[1] != '\t')
break; /* new header break the inner for */
}
/* *header_end = ' '; smash LF */
}
if (header_end == NULL)
break; /* bail out */
hdr = realloc (rfc->hdr, (rfc->hdr_count + 1) * sizeof (*hdr));
if (hdr == NULL)
{
free (rfc->blurb);
free (rfc->hdr);
free (rfc);
return ENOMEM;
}
rfc->hdr = hdr;
rfc->hdr_count++;
/* Treats unix "From " specially */
if ((header_end - header_start >= 5)
&& strncmp (header_start, "From ", 5) == 0)
{
rfc->hdr[rfc->hdr_count - 1].fn = header_start;
rfc->hdr[rfc->hdr_count - 1].fn_end = header_start + 6;
rfc->hdr[rfc->hdr_count - 1].fv = header_start + 6;
rfc->hdr[rfc->hdr_count - 1].fv_end = header_end;
}
else
{
char *colon = memchr (header_start, ':', header_end - header_start);
if (colon == NULL)
{
/* Houston we have a problem */
free (rfc->blurb);
free (rfc->hdr);
free (rfc);
return EINVAL;
}
rfc->hdr[rfc->hdr_count - 1].fn = header_start;
rfc->hdr[rfc->hdr_count - 1].fn_end = colon;
/* skip leading spaces */
while (*(++colon) == ' ');
rfc->hdr[rfc->hdr_count - 1].fv = colon;
rfc->hdr[rfc->hdr_count - 1].fv_end = header_end;
}
}
h->data = rfc;
return 0;
}
static int
rfc822_set_value (header_t h, const char *fn, const char *fv,
size_t n, int replace)
{
(void)h; (void)fn; (void)fv; (void)n; (void)replace;
return ENOSYS;
}
static int
rfc822_get_value (header_t h, const char *fn, char *fv, size_t len, size_t *n)
{
size_t i = 0;
size_t j = 0;
size_t name_len, fn_len, fv_len;
rfc822_t rfc;
if (h == NULL || fn == NULL || (rfc = (rfc822_t)h->data) == NULL)
return EINVAL;
for (name_len = strlen (fn), i = 0; i < rfc->hdr_count; i++)
{
fn_len = rfc->hdr[i].fn_end - rfc->hdr[i].fn;
if (fn_len == name_len && memcmp (rfc->hdr[i].fn, fn, fn_len) == 0)
{
fv_len = rfc->hdr[i].fv_end - rfc->hdr[i].fv;
j = (len < fv_len) ? len : fv_len;
if (fv)
memcpy (fv, rfc->hdr[i].fv, j);
break;
}
}
if (fv)
fv[j] = '\0'; /* null terminated */
if (n)
*n = fv_len;
return 0;
}
ssize_t
rfc822_get_data (header_t h, char *buf, size_t buflen, off_t off, int *err)
{
rfc822_t rfc;
ssize_t len;
if (h == NULL || (rfc = (rfc822_t)h->data) == NULL)
{
if (err)
*err = EINVAL;
return -1;
}
len = rfc->blurb_len - off;
if ((rfc->blurb_len - off) > 0)
{
if (buf)
{
len = (buflen < (size_t)len) ? buflen : len;
memcpy (buf, rfc->blurb + off, len);
}
}
else
len = 0;
return len;
}
/* 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 Library 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 Library General Public License for more details.
You should have received a copy of the GNU Library General Public License
along with this program; if not, write to the Free Software
Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA. */
#ifdef HAVE_CONFIG_H
# include <config.h>
#endif
#include <url0.h>
#include <registrar.h>
#include <cpystr.h>
#include <string.h>
#include <stdlib.h>
#include <errno.h>
/* Forward prototypes */
static int get_scheme (const url_t, char *, size_t, size_t *);
static int get_user (const url_t, char *, size_t, size_t *);
static int get_passwd (const url_t, char *, size_t, size_t *);
static int get_host (const url_t, char *, size_t, size_t *);
static int get_port (const url_t, long *);
static int get_path (const url_t, char *, size_t, size_t *);
static int get_query (const url_t, char *, size_t, size_t *);
static int get_id (const url_t, int *);
int
url_init (url_t * purl, const char *name)
{
int status = EINVAL;
struct url_registrar *ureg;
struct mailbox_registrar *mreg;
registrar_t reg = NULL;
size_t name_len;
int id;
/* Sanity checks */
if (name == NULL || *name == '\0')
return status;
name_len = strlen (name);
/* Search for a known scheme */
while (registrar_list (&ureg, &mreg, &id, &reg) == 0)
{
size_t scheme_len;
if (ureg && ureg->scheme &&
name_len > (scheme_len = strlen (ureg->scheme)) &&
memcmp (name, ureg->scheme, scheme_len) == 0)
{
status = 0;
break;
}
}
/* Found one initialize it */
if (status == 0)
{
status = ureg->_init (purl, name);
if (status == 0)
{
url_t url = *purl;
url->id = id;
if (url->_get_id == NULL)
url->_get_id = get_id;
if (url->_get_scheme == NULL)
url->_get_scheme = get_scheme;
if (url->_get_user == NULL)
url->_get_user = get_user;
if (url->_get_passwd == NULL)
url->_get_passwd = get_passwd;
if (url->_get_host == NULL)
url->_get_host = get_host;
if (url->_get_port == NULL)
url->_get_port = get_port;
if (url->_get_path == NULL)
url->_get_path = get_path;
if (url->_get_query == NULL)
url->_get_query = get_query;
}
}
return status;
}
void
url_destroy (url_t *purl)
{
if (purl && *purl)
{
struct url_registrar *ureg;
int id;
url_get_id (*purl, &id);
registrar_get (id, &ureg, NULL);
ureg->_destroy(purl);
(*purl) = NULL;
}
}
int (url_get_scheme) (const url_t url, char *scheme, size_t len, size_t *n)
{
return (url) ? url->_get_scheme(url, scheme, len, n) : EINVAL;
}
int (url_get_user) (const url_t url, char *user, size_t len, size_t *n)
{
return (url) ? url->_get_user(url, user, len, n) : EINVAL;
}
int (url_get_passwd) (const url_t url, char *passwd, size_t len, size_t *n)
{
return (url) ? url->_get_passwd(url, passwd, len, n) : EINVAL;
}
int (url_get_host) (const url_t url, char *host, size_t len, size_t *n)
{
return (url) ? url->_get_host(url, host, len, n) : EINVAL;
}
int (url_get_port) (const url_t url, long *port)
{
return (url) ? url->_get_port(url, port) : EINVAL;
}
int (url_get_path) (const url_t url, char *path, size_t len, size_t *n)
{
return (url) ? url->_get_path(url, path, len, n) : EINVAL;
}
int (url_get_query) (const url_t url, char *query, size_t len, size_t *n)
{
return (url) ? url->_get_query(url, query, len, n) : EINVAL;
}
int (url_get_id) (const url_t url, int *id)
{
return (url) ? url->_get_id (url, id) : EINVAL;
}
/* Simple stub functions they all call _cpystr */
static int
get_scheme (const url_t u, char *s, size_t len, size_t *n)
{
size_t i;
if (u == NULL)
return EINVAL;
i = _cpystr (s, u->scheme, len);
if (n)
*n = i;
return 0;
}
static int
get_user (const url_t u, char *s, size_t len, size_t *n)
{
size_t i;
if (u == NULL)
return EINVAL;
i = _cpystr (s, u->user, len);
if (n)
*n = i;
return 0;
}
/* FIXME: We should not store passwd in clear, but rather
have a simple encoding, and decoding mechanism */
static int
get_passwd (const url_t u, char *s, size_t len, size_t *n)
{
size_t i;
if (u == NULL)
return EINVAL;
i = _cpystr (s, u->passwd, len);
if (n)
*n = i;
return 0;
}
static int
get_host (const url_t u, char *s, size_t len, size_t *n)
{
size_t i;
if (u == NULL)
return EINVAL;
i = _cpystr (s, u->host, len);
if (n)
*n = i;
return 0;
}
static int
get_port (const url_t u, long * p)
{
*p = u->port;
return 0;
}
static int
get_path (const url_t u, char *s, size_t len, size_t *n)
{
size_t i;
if (u == NULL)
return EINVAL;
i = _cpystr(s, u->path, len);
if (n)
*n = i;
return 0;
}
static int
get_query (const url_t u, char *s, size_t len, size_t *n)
{
size_t i;
if (u == NULL)
return EINVAL;
i = _cpystr(s, u->query, len);
if (n)
*n = i;
return 0;
}
static int
get_id (const url_t u, int *id)
{
if (id)
*id = u->id;
return 0;
}
/* 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 Library 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 Library General Public License for more details.
You should have received a copy of the GNU Library General Public License
along with this program; if not, write to the Free Software
Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA. */
#ifndef _URL_H
#define _URL_H 1
#include <sys/types.h>
#ifdef __cplusplus
extern "C" {
#endif
#ifndef __P
# if __STDC__
# define __P(args) args
# else
# define __P(args) ()
# endif
#endif /*!__P */
/* forward declaration */
struct _url;
typedef struct _url * url_t;
extern int url_init __P ((url_t *, const char *name));
extern void url_destroy __P ((url_t *));
extern int url_get_id __P ((const url_t, int *id));
extern int url_get_scheme __P ((const url_t, char *sch,
size_t, size_t *));
extern int url_get_mscheme __P ((const url_t, char **, size_t *));
extern int url_get_user __P ((const url_t, char *usr,
size_t, size_t *));
extern int url_get_muser __P ((const url_t, char **, size_t *));
extern int url_get_passwd __P ((const url_t, char *passwd,
size_t, size_t *));
extern int url_get_mpasswd __P ((const url_t, char **, size_t *));
extern int url_get_host __P ((const url_t, char *host,
size_t, size_t *));
extern int url_get_mhost __P ((const url_t, char **, size_t *));
extern int url_get_port __P ((const url_t, long *port));
extern int url_get_path __P ((const url_t, char *path,
size_t, size_t *));
extern int url_get_mpath __P ((const url_t, char **, size_t *));
extern int url_get_query __P ((const url_t, char *qeury,
size_t, size_t *));
extern int url_get_mquery __P ((const url_t, char **, size_t *));
#ifdef __cplusplus
}
#endif
#endif /* URL_H */
/* 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 Library 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 Library General Public License for more details.
You should have received a copy of the GNU Library General Public License
along with this program; if not, write to the Free Software
Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA. */
#ifndef _URL0_H
#define _URL0_H 1
#include <url.h>
#ifdef __cplusplus
extern "C" {
#endif
#ifndef __P
# if __STDC__
# define __P(args) args
# else
# define __P(args) ()
# endif
#endif /*!__P */
struct _url
{
/* Data */
char *scheme;
char *user;
char *passwd; /* encoded ?? */
char *host;
long port;
char *path;
char *query;
int id;
void *data;
int (*_init) __P ((url_t *url, const char *name));
void (*_destroy) __P ((url_t *url));
/* Methods */
int (*_get_id) __P ((const url_t, int *id));
int (*_get_scheme) __P ((const url_t, char *scheme,
size_t len, size_t *n));
int (*_get_mscheme) __P ((const url_t, char **, size_t *n));
int (*_get_user) __P ((const url_t, char *user,
size_t len, size_t *n));
int (*_get_muser) __P ((const url_t, char **, size_t *n));
int (*_get_passwd) __P ((const url_t, char *passwd,
size_t len, size_t *n));
int (*_get_mpasswd) __P ((const url_t, char **, size_t *n));
int (*_get_host) __P ((const url_t, char *host,
size_t len, size_t *n));
int (*_get_mhost) __P ((const url_t, char **, size_t *n));
int (*_get_port) __P ((const url_t, long *port));
int (*_get_path) __P ((const url_t, char *path,
size_t len, size_t *n));
int (*_get_mpath) __P ((const url_t, char **, size_t *n));
int (*_get_query) __P ((const url_t, char *query,
size_t len, size_t *n));
int (*_get_mquery) __P ((const url_t, char **, size_t *n));
};
/* IMAP */
/* Mailto */
/* UNIX MBOX */
/* Maildir */
/* 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
}
#endif
#endif /* URL_H */
/* 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 Library 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 Library General Public License for more details.
You should have received a copy of the GNU Library General Public License
along with this program; if not, write to the Free Software
Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA. */
#include <url0.h>
#include <registrar.h>
#include <errno.h>
static int url_imap_init (url_t *purl, const char *name);
static void url_imap_destroy (url_t *purl);
struct url_registrar _url_imap_registrar =
{
"imap://",
url_imap_init, url_imap_destroy
};
static void
url_imap_destroy (url_t *purl)
{
(void)purl;
return;
}
static int
url_imap_init (url_t *purl, const char *name)
{
(void)purl; (void)name;
return ENOSYS;
}
/* 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 Library 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 Library General Public License for more details.
You should have received a copy of the GNU Library General Public License
along with this program; if not, write to the Free Software
Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA. */
#include <url0.h>
#include <registrar.h>
#include <errno.h>
static int url_mailto_init (url_t *purl, const char *name);
static void url_mailto_destroy (url_t *purl);
struct url_registrar _url_mailto_registrar =
{
"mailto:",
url_mailto_init, url_mailto_destroy
};
static void
url_mailto_destroy (url_t *purl)
{
(void)purl;
return;
}
static int
url_mailto_init (url_t *purl, const char *name)
{
(void)purl; (void)name;
return ENOSYS;
}
/* 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 Library 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 Library General Public License for more details.
You should have received a copy of the GNU Library General Public License
along with this program; if not, write to the Free Software
Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA. */
#include <url0.h>
#include <registrar.h>
#include <errno.h>
#include <stdlib.h>
#include <string.h>
static void url_mbox_destroy (url_t *purl);
static int url_mbox_init (url_t *purl, const char *name);
struct url_registrar _url_mbox_registrar =
{
"/",
url_mbox_init, url_mbox_destroy
};
static void
url_mbox_destroy (url_t *purl)
{
if (purl && *purl)
{
url_t url = *purl;
free (url->scheme);
free (url->path);
free (url);
*purl = NULL;
}
}
/*
UNIX box
*/
static int
url_mbox_init (url_t *purl, const char *name)
{
url_t url;
struct url_registrar *ureg = &_url_mbox_registrar;
/* reject the obvious */
if (name == NULL || *name == '\0')
return EINVAL;
/* FIXME: do I need to decode url encoding '% hex hex' ? */
url = calloc(1, sizeof (*url));
if (url == NULL)
return ENOMEM;
/* TYPE */
url->_init = ureg->_init;
url->_destroy = ureg->_destroy;
/* SCHEME */
url->scheme = strdup (ureg->scheme);
if (url->scheme == NULL)
{
ureg->_destroy (&url);
return ENOMEM;
}
/* PATH */
url->path = strdup (name);
if (url->path == NULL)
{
ureg->_destroy (&url);
return ENOMEM;
}
*purl = url;
return 0;
}
/* 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 Library 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 Library General Public License for more details.
You should have received a copy of the GNU Library General Public License
along with this program; if not, write to the Free Software
Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA. */
#include <url0.h>
#include <registrar.h>
#include <errno.h>
#include <stdlib.h>
#include <string.h>
static void url_maildir_destroy (url_t *purl);
static int url_maildir_init (url_t *purl, const char *name);
struct url_registrar _url_maildir_registrar =
{
"maildir:",
url_maildir_init, url_maildir_destroy
};
static void
url_maildir_destroy (url_t *purl)
{
if (purl && *purl)
{
url_t url = *purl;
free (url->scheme);
free (url->path);
free (url);
*purl = NULL;
}
}
/*
MAILDIR URL
maildir:
*/
static int
url_maildir_init (url_t *purl, const char *name)
{
url_t url;
struct url_registrar *ureg = &_url_maildir_registrar;
size_t len, scheme_len = strlen (ureg->scheme);
/* reject the obvious */
if (name == NULL || strncmp (ureg->scheme, name, scheme_len) != 0
|| (len = strlen (name)) < (scheme_len + 1) /* (scheme)+1(path)*/)
return EINVAL;
/* do I need to decode url encoding '% hex hex' ? */
url = calloc(1, sizeof (*url));
if (url == NULL)
return EINVAL;
/* TYPE */
url->_init = ureg->_init;
url->_destroy = ureg->_destroy;
/* SCHEME */
url->scheme = strdup (ureg->scheme);
if (url->scheme == NULL)
{
ureg->_destroy (&url);
return ENOMEM;
}
/* PATH */
name += scheme_len; /* pass the scheme */
url->path = strdup (name);
if (url->path == NULL)
{
ureg->_destroy (&url);
return ENOMEM;
}
*purl = url;
return 0;
}
/* 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 Library 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 Library General Public License for more details.
You should have received a copy of the GNU Library General Public License
along with this program; if not, write to the Free Software
Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA. */
#include <url0.h>
#include <registrar.h>
#include <errno.h>
#include <stdlib.h>
#include <string.h>
static void url_mh_destroy (url_t *purl);
static int url_mh_init (url_t *purl, const char *name);
struct url_registrar _url_mh_registrar =
{
"mh:",
url_mh_init, url_mh_destroy
};
static void
url_mh_destroy (url_t *purl)
{
if (purl && *purl)
{
url_t url = *purl;
free (url->scheme);
free (url->path);
free (url);
*purl = NULL;
}
}
/*
MH URL
mh:
*/
static int
url_mh_init (url_t *purl, const char *name)
{
url_t url;
struct url_registrar *ureg = &_url_mh_registrar;
size_t len, scheme_len = strlen (ureg->scheme);
/* reject the obvious */
if (name == NULL || strncmp (ureg->scheme, name, scheme_len) != 0
|| (len = strlen (name)) < (scheme_len + 1) /* (scheme)+1(path)*/)
return EINVAL;
/* do I need to decode url encoding '% hex hex' ? */
url = calloc(1, sizeof (*url));
if (url == NULL)
return ENOMEM;
/* TYPE */
url->_init = ureg->_init;
url->_destroy = ureg->_destroy;
/* SCHEME */
url->scheme = strdup (ureg->scheme);
if (url->scheme == NULL)
{
ureg->_destroy (&url);
return ENOMEM;
}
/* PATH */
name += scheme_len; /* pass the scheme */
url->path = strdup (name);
if (url->path == NULL)
{
ureg->_destroy (&url);
return ENOMEM;
}
*purl = url;
return 0;
}
/* 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 Library 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 Library General Public License for more details.
You should have received a copy of the GNU Library General Public License
along with this program; if not, write to the Free Software
Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA. */
#include <url0.h>
#include <registrar.h>
#include <errno.h>
#include <stdlib.h>
#include <string.h>
static void url_mmdf_destroy (url_t *purl);
static int url_mmdf_init (url_t *purl, const char *name);
struct url_registrar _url_mmdf_registrar =
{
"mmdf:",
url_mmdf_init, url_mmdf_destroy
};
static void
url_mmdf_destroy (url_t *purl)
{
if (purl && *purl)
{
url_t url = *purl;
free (url->scheme);
free (url->path);
free (url);
*purl = NULL;
}
}
/*
MMDF URL
mmdf:
*/
static int
url_mmdf_init (url_t *purl, const char *name)
{
url_t url;
struct url_registrar *ureg = &_url_mmdf_registrar;
size_t len, scheme_len = strlen (ureg->scheme);
/* reject the obvious */
if (name == NULL || strncmp (ureg->scheme, name, scheme_len) != 0
|| (len = strlen (name)) < 8 /* 7(scheme)+1(path)*/)
return EINVAL;
/* do I need to decode url encoding '% hex hex' ? */
url = calloc(1, sizeof (*url));
if (url == NULL)
return ENOMEM;
/* TYPE */
url->_init = ureg->_init;
url->_destroy = ureg->_destroy;
/* SCHEME */
url->scheme = strdup (ureg->scheme);
if (url->scheme == NULL)
{
ureg->_destroy (&url);
return ENOMEM;
}
/* PATH */
name += scheme_len; /* pass the scheme */
url->path = strdup (name);
if (url->path == NULL)
{
ureg->_destroy (&url);
return ENOMEM;
}
*purl = url;
return 0;
}
/* 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 Library 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 Library General Public License for more details.
You should have received a copy of the GNU Library General Public License
along with this program; if not, write to the Free Software
Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA. */
#include <url0.h>
#include <registrar.h>
#include <errno.h>
#include <cpystr.h>
#include <stdlib.h>
#include <string.h>
#include <errno.h>
static void url_pop_destroy (url_t *purl);
static int url_pop_init (url_t *purl, const char *name);
struct url_registrar _url_pop_registrar =
{
"pop://",
url_pop_init, 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)
{
if (purl && *purl)
{
url_t url = *purl;
free (url->scheme);
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;
}
}
/*
POP URL
pop://<user>;AUTH=<auth>@<host>:<port>
*/
static int
url_pop_init (url_t *purl, const char *name)
{
const char *host_port, *index;
struct url_registrar *ureg = &_url_pop_registrar;
size_t len, 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;
/* 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;
/* TYPE */
url->_init = _url_pop_registrar._init;
url->_destroy = _url_pop_registrar._destroy;
/* SCHEME */
url->scheme = strdup ("pop://");
if (url->scheme == NULL)
{
url_pop_destroy (&url);
return ENOMEM;
}
name += scheme_len; /* pass the scheme */
/* looking for "user;auth=auth-enc" */
for (index = name; index != host_port; index++)
{
/* Auth ? */
if (*index == ';')
{
/* make sure it the token */
if (strncasecmp(index + 1, "auth=", 5) == 0)
break;
}
}
if (index == name)
{
url_pop_destroy (&url);
return -1;
}
/* USER */
url->user = malloc(index - name + 1);
if (url->user == NULL)
{
url_pop_destroy (&url);
return -1;
}
((char *)memcpy(url->user, name, index - name))[index - name] = '\0';
/* AUTH */
if ((host_port - index) <= 6 /*strlen(";AUTH=")*/)
{
/* Use default AUTH '*' */
up->auth = malloc (1 + 1);
if (up->auth)
{
up->auth[0] = '*';
up->auth[1] = '\0';
}
}
else
{
/* move pass AUTH= */
index += 6;
up->auth = malloc (host_port - index + 1);
if (up->auth)
{
((char *)memcpy (up->auth, index, host_port - index))
[host_port - index] = '\0';
}
}
if (up->auth == NULL)
{
url_pop_destroy (&url);
return -1;
}
/* HOST:PORT */
index = strchr (++host_port, ':');
if (index == NULL)
{
url->host = strdup (host_port);
url->port = MU_POP_PORT;
}
else
{
long p = strtol(index + 1, NULL, 10);
url->host = malloc (index - host_port + 1);
if (url->host)
{
((char *)memcpy (url->host, host_port, index - host_port))
[index - host_port]='\0';
url->port = (p == 0) ? MU_POP_PORT : p;
}
}
if (url->host == NULL)
{
url_pop_destroy (&url);
return ENOMEM;
}
*purl = url;
return 0;
}
/* 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 Library 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 Library General Public License for more details.
You should have received a copy of the GNU Library General Public License
along with this program; if not, write to the Free Software
Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA. */
#include <url0.h>
#include <registrar.h>
#include <errno.h>
#include <stdlib.h>
#include <string.h>
static void url_unix_destroy (url_t *purl);
static int url_unix_init (url_t *purl, const char *name);
struct url_registrar _url_unix_registrar =
{
"unix:",
url_unix_init, url_unix_destroy
};
static void
url_unix_destroy (url_t *purl)
{
if (purl && *purl)
{
url_t url = *purl;
free (url->scheme);
free (url->path);
free (url);
*purl = NULL;
}
}
/*
UNIX Mbox
unix:/path
*/
static int
url_unix_init (url_t *purl, const char *name)
{
url_t url;
struct url_registrar *ureg = &_url_unix_registrar;
size_t len, scheme_len = strlen (ureg->scheme);
/* reject the obvious */
if (name == NULL || strncmp (ureg->scheme, name, scheme_len) != 0
|| (len = strlen (name)) < scheme_len + 1 /* 7(scheme)+1(path)*/)
return EINVAL;
/* do I need to decode url encoding '% hex hex' ? */
url = calloc(1, sizeof (*url));
if (url == NULL)
return ENOMEM;
/* TYPE */
url->_init = ureg->_init;
url->_destroy = ureg->_destroy;
/* SCHEME */
url->scheme = strdup (ureg->scheme);
if (url->scheme == NULL)
{
ureg->_destroy (&url);
return ENOMEM;
}
/* PATH */
name += scheme_len; /* pass the scheme */
url->path = strdup (name);
if (url->path == NULL)
{
ureg->_destroy (&url);
return ENOMEM;
}
*purl = url;
return 0;
}