Commit 01dabb5c 01dabb5cb4cc1d4c849432dd45967ddfc27ab015 by Sergey Poznyakoff

Change mailer creation mechanism.

* include/mailutils/mailer.h (mu_mailer_create_from_url): New
proto.
* mailbox/mailer.c (mu_mailer_create_from_url): New
function.
(mu_mailer_create): Rewrite using mu_mailer_create_from_url.
* libproto/mailer/prog.c (_url_prog_init): Do not call
mu_url_init.
(url_to_argv): Reflect changes to mu_url functions.
* libproto/mailer/url_sendmail.c (_url_sendmail_init): Do not call
mu_url_init.
* libproto/mailer/url_smtp.c (_url_smtp_init): Likewise.
1 parent f3106927
2008-10-28 Sergey Poznyakoff <gray@gnu.org.ua>
Change mailer creation mechanism.
* include/mailutils/mailer.h (mu_mailer_create_from_url): New
proto.
* mailbox/mailer.c (mu_mailer_create_from_url): New
function.
(mu_mailer_create): Rewrite using mu_mailer_create_from_url.
* libproto/mailer/prog.c (_url_prog_init): Do not call
mu_url_init.
(url_to_argv): Reflect changes to mu_url functions.
* libproto/mailer/url_sendmail.c (_url_sendmail_init): Do not call
mu_url_init.
* libproto/mailer/url_smtp.c (_url_smtp_init): Likewise.
Minor fixes.
* mailbox/mailbox.c (_create_mailbox0): Take care not to destroy
......
/* GNU Mailutils -- a suite of utilities for electronic mail
Copyright (C) 1999, 2000, 2001, 2005,
2007 Free Software Foundation, Inc.
2007, 2008 Free Software Foundation, Inc.
This library is free software; you can redistribute it and/or
modify it under the terms of the GNU Lesser General Public
......@@ -31,6 +31,8 @@ extern "C" {
/* A url of NULL will cause the default to be used. */
extern int mu_mailer_create (mu_mailer_t *, const char *url);
extern int mu_mailer_create_from_url (mu_mailer_t *pmailer, mu_url_t url);
extern void mu_mailer_destroy (mu_mailer_t *);
extern int mu_mailer_open (mu_mailer_t, int flags);
extern int mu_mailer_close (mu_mailer_t);
......
......@@ -39,7 +39,6 @@
#include <registrar0.h>
static int _url_prog_init (mu_url_t);
static int _url_pipe_init (mu_url_t);
static int _mailer_prog_init (mu_mailer_t);
static struct _mu_record _prog_record =
......@@ -64,11 +63,8 @@ mu_record_t mu_prog_record = &_prog_record;
static int
_url_prog_init (mu_url_t url)
{
int status = mu_url_init (url, 0, "prog");
if (status)
return status;
/* not valid in a sendmail url */
if (url->user || url->passwd || url->auth || url->host || url->port)
/* not valid in a prog url */
if (url->passwd || url->auth || url->host || url->port)
return EINVAL;
if (!url->path)
return EINVAL;
......@@ -283,11 +279,11 @@ url_to_argv (mu_url_t url, mu_message_t msg,
int rc;
mu_vartab_t vtab;
struct ex_rcpt ex_rcpt;
const char *query;
char *cmdargs;
int argc;
char **query;
size_t i;
size_t argc;
char **argv;
ex_rcpt.msg = msg;
ex_rcpt.addr = to;
ex_rcpt.string = NULL;
......@@ -295,26 +291,28 @@ url_to_argv (mu_url_t url, mu_message_t msg,
mu_vartab_define_exp (vtab, "sender", _expand_sender, NULL, from);
mu_vartab_define_exp (vtab, "rcpt", _expand_rcpt, _free_rcpt, &ex_rcpt);
rc = mu_url_sget_query (url, &query);
rc = mu_url_sget_query (url, &argc, &query);
if (rc)
return rc;
argv = calloc (argc + 1, sizeof (argv[0]));
if (!argv)
return ENOMEM;
for (i = 0; i < argc; i++)
{
if ((rc = mu_vartab_expand (vtab, query[i], &argv[i])))
{
mu_argcv_free (i, argv);
mu_vartab_destroy (&vtab);
return rc;
}
}
argv[i] = NULL;
rc = mu_vartab_expand (vtab, query, &cmdargs);
mu_vartab_destroy (&vtab);
if (rc)
return rc;
rc = mu_argcv_get_np (cmdargs, strlen (cmdargs),
"&", NULL,
0, &argc, &argv, NULL);
free (cmdargs);
if (rc)
return rc;
argv = realloc (argv, (argc + 2) * sizeof (argv[0]));
memmove (argv + 1, argv, (argc + 1) * sizeof (argv[0]));
mu_url_aget_path (url, &argv[0]);
*pargc = argc + 1;
*pargc = argc;
*pargv = argv;
return 0;
}
......@@ -327,8 +325,26 @@ prog_send_message (mu_mailer_t mailer, mu_message_t msg, mu_address_t from,
int argc;
char **argv;
int status;
const char *command;
status = url_to_argv(mailer->url, msg, from, to, &argc, &argv);
status = mu_url_sget_path (mailer->url, &command);
if (status && status != MU_ERR_NOENT)
{
MU_DEBUG1 (mailer->debug, MU_DEBUG_ERROR,
"cannot get path from URL: %s\n",
mu_strerror (status));
return status;
}
status = mu_progmailer_set_command (pm, command);
if (status)
{
MU_DEBUG1 (mailer->debug, MU_DEBUG_ERROR,
"cannot set progmailer command: %s\n",
mu_strerror (status));
return status;
}
status = url_to_argv (mailer->url, msg, from, to, &argc, &argv);
if (status)
{
MU_DEBUG1 (mailer->debug, MU_DEBUG_ERROR,
......
......@@ -37,13 +37,6 @@
#include <registrar0.h>
#include <url0.h>
static void url_sendmail_destroy (mu_url_t purl);
static void
url_sendmail_destroy (mu_url_t url MU_ARG_UNUSED)
{
}
/*
Sendmail URL:
sendmail:/path/to/sendmail
......@@ -52,22 +45,16 @@ url_sendmail_destroy (mu_url_t url MU_ARG_UNUSED)
int
_url_sendmail_init (mu_url_t url)
{
int status = mu_url_init (url, 0, "sendmail");
if (status)
return status;
url->_destroy = url_sendmail_destroy;
/* not valid in a sendmail url */
if (url->user || url->passwd || url->auth || url->query
if (url->user || url->passwd || url->auth || url->qargc
|| url->host || url->port)
return EINVAL;
if (url->path == 0)
if ((url->path = strdup (_PATH_SENDMAIL)) == 0)
status = ENOMEM;
return ENOMEM;
return status;
return 0;
}
#endif /* ENABLE_SENDMAIL */
......
......@@ -32,10 +32,6 @@
int
_url_smtp_init (mu_url_t url)
{
int status = mu_url_init (url, MU_SMTP_PORT, "smtp");
if (status)
return status;
/* host isn't optional */
if (!url->host)
return EINVAL;
......@@ -44,9 +40,12 @@ _url_smtp_init (mu_url_t url)
for the ESMTP authentication */
/* all other fields must be NULL */
if (url->path || url->query)
if (url->path || url->qargc)
return EINVAL;
if (url->port == 0)
url->port = MU_SMTP_PORT;
return 0;
}
......
......@@ -89,28 +89,21 @@ mu_mailer_get_url_default (const char **url)
}
int
mu_mailer_create (mu_mailer_t * pmailer, const char *name)
mu_mailer_create_from_url (mu_mailer_t *pmailer, mu_url_t url)
{
mu_record_t record;
if (pmailer == NULL)
return MU_ERR_OUT_PTR_NULL;
if (name == NULL)
mu_mailer_get_url_default (&name);
if (mu_registrar_lookup (name, MU_FOLDER_ATTRIBUTE_FILE, &record, NULL) == 0)
if (mu_registrar_lookup_url (url, MU_FOLDER_ATTRIBUTE_FILE, &record,
NULL) == 0)
{
int (*m_init) (mu_mailer_t) = NULL;
int (*u_init) (mu_url_t) = NULL;
mu_record_get_mailer (record, &m_init);
mu_record_get_url (record, &u_init);
if (m_init && u_init)
if (m_init)
{
int status;
mu_url_t url;
mu_mailer_t mailer;
int (*u_init) (mu_url_t) = NULL;
/* Allocate memory for mailer. */
mailer = calloc (1, sizeof (*mailer));
......@@ -124,29 +117,50 @@ mu_mailer_create (mu_mailer_t * pmailer, const char *name)
return status;
}
/* Parse the url, it may be a bad one and we should bail out if this
failed. */
if ((status = mu_url_create (&url, name)) != 0
|| (status = u_init (url)) != 0)
status = m_init (mailer);
if (status)
{
mu_mailer_destroy (&mailer);
return status;
}
mailer->url = url;
status = m_init (mailer);
if (status)
mu_mailer_destroy (&mailer);
else
*pmailer = mailer;
mu_record_get_url (record, &u_init);
if (u_init && (status = u_init (url)) != 0)
{
mu_mailer_destroy (&mailer);
return status;
}
mailer->url = url;
*pmailer = mailer;
return status;
}
}
return MU_ERR_MAILER_BAD_URL;
}
int
mu_mailer_create (mu_mailer_t * pmailer, const char *name)
{
int status;
mu_url_t url;
if (name == NULL)
mu_mailer_get_url_default (&name);
status = mu_url_create (&url, name);
if (status)
return status;
status = mu_url_parse (url);
if (status == 0)
status = mu_mailer_create_from_url (pmailer, url);
if (status)
mu_url_destroy (&url);
return status;
}
void
mu_mailer_destroy (mu_mailer_t * pmailer)
{
......