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.
Showing
6 changed files
with
103 additions
and
71 deletions
1 | 2008-10-28 Sergey Poznyakoff <gray@gnu.org.ua> | 1 | 2008-10-28 Sergey Poznyakoff <gray@gnu.org.ua> |
2 | 2 | ||
3 | Change mailer creation mechanism. | ||
4 | |||
5 | * include/mailutils/mailer.h (mu_mailer_create_from_url): New | ||
6 | proto. | ||
7 | * mailbox/mailer.c (mu_mailer_create_from_url): New | ||
8 | function. | ||
9 | (mu_mailer_create): Rewrite using mu_mailer_create_from_url. | ||
10 | * libproto/mailer/prog.c (_url_prog_init): Do not call | ||
11 | mu_url_init. | ||
12 | (url_to_argv): Reflect changes to mu_url functions. | ||
13 | * libproto/mailer/url_sendmail.c (_url_sendmail_init): Do not call | ||
14 | mu_url_init. | ||
15 | * libproto/mailer/url_smtp.c (_url_smtp_init): Likewise. | ||
16 | |||
3 | Minor fixes. | 17 | Minor fixes. |
4 | 18 | ||
5 | * mailbox/mailbox.c (_create_mailbox0): Take care not to destroy | 19 | * mailbox/mailbox.c (_create_mailbox0): Take care not to destroy | ... | ... |
1 | /* GNU Mailutils -- a suite of utilities for electronic mail | 1 | /* GNU Mailutils -- a suite of utilities for electronic mail |
2 | Copyright (C) 1999, 2000, 2001, 2005, | 2 | Copyright (C) 1999, 2000, 2001, 2005, |
3 | 2007 Free Software Foundation, Inc. | 3 | 2007, 2008 Free Software Foundation, Inc. |
4 | 4 | ||
5 | This library is free software; you can redistribute it and/or | 5 | This library is free software; you can redistribute it and/or |
6 | modify it under the terms of the GNU Lesser General Public | 6 | modify it under the terms of the GNU Lesser General Public |
... | @@ -31,6 +31,8 @@ extern "C" { | ... | @@ -31,6 +31,8 @@ extern "C" { |
31 | 31 | ||
32 | /* A url of NULL will cause the default to be used. */ | 32 | /* A url of NULL will cause the default to be used. */ |
33 | extern int mu_mailer_create (mu_mailer_t *, const char *url); | 33 | extern int mu_mailer_create (mu_mailer_t *, const char *url); |
34 | extern int mu_mailer_create_from_url (mu_mailer_t *pmailer, mu_url_t url); | ||
35 | |||
34 | extern void mu_mailer_destroy (mu_mailer_t *); | 36 | extern void mu_mailer_destroy (mu_mailer_t *); |
35 | extern int mu_mailer_open (mu_mailer_t, int flags); | 37 | extern int mu_mailer_open (mu_mailer_t, int flags); |
36 | extern int mu_mailer_close (mu_mailer_t); | 38 | extern int mu_mailer_close (mu_mailer_t); | ... | ... |
... | @@ -39,7 +39,6 @@ | ... | @@ -39,7 +39,6 @@ |
39 | #include <registrar0.h> | 39 | #include <registrar0.h> |
40 | 40 | ||
41 | static int _url_prog_init (mu_url_t); | 41 | static int _url_prog_init (mu_url_t); |
42 | static int _url_pipe_init (mu_url_t); | ||
43 | static int _mailer_prog_init (mu_mailer_t); | 42 | static int _mailer_prog_init (mu_mailer_t); |
44 | 43 | ||
45 | static struct _mu_record _prog_record = | 44 | static struct _mu_record _prog_record = |
... | @@ -64,11 +63,8 @@ mu_record_t mu_prog_record = &_prog_record; | ... | @@ -64,11 +63,8 @@ mu_record_t mu_prog_record = &_prog_record; |
64 | static int | 63 | static int |
65 | _url_prog_init (mu_url_t url) | 64 | _url_prog_init (mu_url_t url) |
66 | { | 65 | { |
67 | int status = mu_url_init (url, 0, "prog"); | 66 | /* not valid in a prog url */ |
68 | if (status) | 67 | if (url->passwd || url->auth || url->host || url->port) |
69 | return status; | ||
70 | /* not valid in a sendmail url */ | ||
71 | if (url->user || url->passwd || url->auth || url->host || url->port) | ||
72 | return EINVAL; | 68 | return EINVAL; |
73 | if (!url->path) | 69 | if (!url->path) |
74 | return EINVAL; | 70 | return EINVAL; |
... | @@ -283,11 +279,11 @@ url_to_argv (mu_url_t url, mu_message_t msg, | ... | @@ -283,11 +279,11 @@ url_to_argv (mu_url_t url, mu_message_t msg, |
283 | int rc; | 279 | int rc; |
284 | mu_vartab_t vtab; | 280 | mu_vartab_t vtab; |
285 | struct ex_rcpt ex_rcpt; | 281 | struct ex_rcpt ex_rcpt; |
286 | const char *query; | 282 | char **query; |
287 | char *cmdargs; | 283 | size_t i; |
288 | int argc; | 284 | size_t argc; |
289 | char **argv; | 285 | char **argv; |
290 | 286 | ||
291 | ex_rcpt.msg = msg; | 287 | ex_rcpt.msg = msg; |
292 | ex_rcpt.addr = to; | 288 | ex_rcpt.addr = to; |
293 | ex_rcpt.string = NULL; | 289 | ex_rcpt.string = NULL; |
... | @@ -295,26 +291,28 @@ url_to_argv (mu_url_t url, mu_message_t msg, | ... | @@ -295,26 +291,28 @@ url_to_argv (mu_url_t url, mu_message_t msg, |
295 | mu_vartab_define_exp (vtab, "sender", _expand_sender, NULL, from); | 291 | mu_vartab_define_exp (vtab, "sender", _expand_sender, NULL, from); |
296 | mu_vartab_define_exp (vtab, "rcpt", _expand_rcpt, _free_rcpt, &ex_rcpt); | 292 | mu_vartab_define_exp (vtab, "rcpt", _expand_rcpt, _free_rcpt, &ex_rcpt); |
297 | 293 | ||
298 | rc = mu_url_sget_query (url, &query); | 294 | rc = mu_url_sget_query (url, &argc, &query); |
299 | if (rc) | 295 | if (rc) |
300 | return rc; | 296 | return rc; |
297 | |||
298 | argv = calloc (argc + 1, sizeof (argv[0])); | ||
299 | if (!argv) | ||
300 | return ENOMEM; | ||
301 | |||
302 | for (i = 0; i < argc; i++) | ||
303 | { | ||
304 | if ((rc = mu_vartab_expand (vtab, query[i], &argv[i]))) | ||
305 | { | ||
306 | mu_argcv_free (i, argv); | ||
307 | mu_vartab_destroy (&vtab); | ||
308 | return rc; | ||
309 | } | ||
310 | } | ||
311 | argv[i] = NULL; | ||
301 | 312 | ||
302 | rc = mu_vartab_expand (vtab, query, &cmdargs); | ||
303 | mu_vartab_destroy (&vtab); | 313 | mu_vartab_destroy (&vtab); |
304 | if (rc) | ||
305 | return rc; | ||
306 | |||
307 | rc = mu_argcv_get_np (cmdargs, strlen (cmdargs), | ||
308 | "&", NULL, | ||
309 | 0, &argc, &argv, NULL); | ||
310 | free (cmdargs); | ||
311 | if (rc) | ||
312 | return rc; | ||
313 | argv = realloc (argv, (argc + 2) * sizeof (argv[0])); | ||
314 | memmove (argv + 1, argv, (argc + 1) * sizeof (argv[0])); | ||
315 | mu_url_aget_path (url, &argv[0]); | ||
316 | 314 | ||
317 | *pargc = argc + 1; | 315 | *pargc = argc; |
318 | *pargv = argv; | 316 | *pargv = argv; |
319 | return 0; | 317 | return 0; |
320 | } | 318 | } |
... | @@ -327,8 +325,26 @@ prog_send_message (mu_mailer_t mailer, mu_message_t msg, mu_address_t from, | ... | @@ -327,8 +325,26 @@ prog_send_message (mu_mailer_t mailer, mu_message_t msg, mu_address_t from, |
327 | int argc; | 325 | int argc; |
328 | char **argv; | 326 | char **argv; |
329 | int status; | 327 | int status; |
328 | const char *command; | ||
330 | 329 | ||
331 | status = url_to_argv(mailer->url, msg, from, to, &argc, &argv); | 330 | status = mu_url_sget_path (mailer->url, &command); |
331 | if (status && status != MU_ERR_NOENT) | ||
332 | { | ||
333 | MU_DEBUG1 (mailer->debug, MU_DEBUG_ERROR, | ||
334 | "cannot get path from URL: %s\n", | ||
335 | mu_strerror (status)); | ||
336 | return status; | ||
337 | } | ||
338 | status = mu_progmailer_set_command (pm, command); | ||
339 | if (status) | ||
340 | { | ||
341 | MU_DEBUG1 (mailer->debug, MU_DEBUG_ERROR, | ||
342 | "cannot set progmailer command: %s\n", | ||
343 | mu_strerror (status)); | ||
344 | return status; | ||
345 | } | ||
346 | |||
347 | status = url_to_argv (mailer->url, msg, from, to, &argc, &argv); | ||
332 | if (status) | 348 | if (status) |
333 | { | 349 | { |
334 | MU_DEBUG1 (mailer->debug, MU_DEBUG_ERROR, | 350 | MU_DEBUG1 (mailer->debug, MU_DEBUG_ERROR, | ... | ... |
... | @@ -37,13 +37,6 @@ | ... | @@ -37,13 +37,6 @@ |
37 | #include <registrar0.h> | 37 | #include <registrar0.h> |
38 | #include <url0.h> | 38 | #include <url0.h> |
39 | 39 | ||
40 | static void url_sendmail_destroy (mu_url_t purl); | ||
41 | |||
42 | static void | ||
43 | url_sendmail_destroy (mu_url_t url MU_ARG_UNUSED) | ||
44 | { | ||
45 | } | ||
46 | |||
47 | /* | 40 | /* |
48 | Sendmail URL: | 41 | Sendmail URL: |
49 | sendmail:/path/to/sendmail | 42 | sendmail:/path/to/sendmail |
... | @@ -52,22 +45,16 @@ url_sendmail_destroy (mu_url_t url MU_ARG_UNUSED) | ... | @@ -52,22 +45,16 @@ url_sendmail_destroy (mu_url_t url MU_ARG_UNUSED) |
52 | int | 45 | int |
53 | _url_sendmail_init (mu_url_t url) | 46 | _url_sendmail_init (mu_url_t url) |
54 | { | 47 | { |
55 | int status = mu_url_init (url, 0, "sendmail"); | ||
56 | if (status) | ||
57 | return status; | ||
58 | |||
59 | url->_destroy = url_sendmail_destroy; | ||
60 | |||
61 | /* not valid in a sendmail url */ | 48 | /* not valid in a sendmail url */ |
62 | if (url->user || url->passwd || url->auth || url->query | 49 | if (url->user || url->passwd || url->auth || url->qargc |
63 | || url->host || url->port) | 50 | || url->host || url->port) |
64 | return EINVAL; | 51 | return EINVAL; |
65 | 52 | ||
66 | if (url->path == 0) | 53 | if (url->path == 0) |
67 | if ((url->path = strdup (_PATH_SENDMAIL)) == 0) | 54 | if ((url->path = strdup (_PATH_SENDMAIL)) == 0) |
68 | status = ENOMEM; | 55 | return ENOMEM; |
69 | 56 | ||
70 | return status; | 57 | return 0; |
71 | } | 58 | } |
72 | 59 | ||
73 | #endif /* ENABLE_SENDMAIL */ | 60 | #endif /* ENABLE_SENDMAIL */ | ... | ... |
... | @@ -32,10 +32,6 @@ | ... | @@ -32,10 +32,6 @@ |
32 | int | 32 | int |
33 | _url_smtp_init (mu_url_t url) | 33 | _url_smtp_init (mu_url_t url) |
34 | { | 34 | { |
35 | int status = mu_url_init (url, MU_SMTP_PORT, "smtp"); | ||
36 | if (status) | ||
37 | return status; | ||
38 | |||
39 | /* host isn't optional */ | 35 | /* host isn't optional */ |
40 | if (!url->host) | 36 | if (!url->host) |
41 | return EINVAL; | 37 | return EINVAL; |
... | @@ -44,9 +40,12 @@ _url_smtp_init (mu_url_t url) | ... | @@ -44,9 +40,12 @@ _url_smtp_init (mu_url_t url) |
44 | for the ESMTP authentication */ | 40 | for the ESMTP authentication */ |
45 | 41 | ||
46 | /* all other fields must be NULL */ | 42 | /* all other fields must be NULL */ |
47 | if (url->path || url->query) | 43 | if (url->path || url->qargc) |
48 | return EINVAL; | 44 | return EINVAL; |
49 | 45 | ||
46 | if (url->port == 0) | ||
47 | url->port = MU_SMTP_PORT; | ||
48 | |||
50 | return 0; | 49 | return 0; |
51 | } | 50 | } |
52 | 51 | ... | ... |
... | @@ -89,28 +89,21 @@ mu_mailer_get_url_default (const char **url) | ... | @@ -89,28 +89,21 @@ mu_mailer_get_url_default (const char **url) |
89 | } | 89 | } |
90 | 90 | ||
91 | int | 91 | int |
92 | mu_mailer_create (mu_mailer_t * pmailer, const char *name) | 92 | mu_mailer_create_from_url (mu_mailer_t *pmailer, mu_url_t url) |
93 | { | 93 | { |
94 | mu_record_t record; | 94 | mu_record_t record; |
95 | 95 | ||
96 | if (pmailer == NULL) | 96 | if (mu_registrar_lookup_url (url, MU_FOLDER_ATTRIBUTE_FILE, &record, |
97 | return MU_ERR_OUT_PTR_NULL; | 97 | NULL) == 0) |
98 | |||
99 | if (name == NULL) | ||
100 | mu_mailer_get_url_default (&name); | ||
101 | |||
102 | if (mu_registrar_lookup (name, MU_FOLDER_ATTRIBUTE_FILE, &record, NULL) == 0) | ||
103 | { | 98 | { |
104 | int (*m_init) (mu_mailer_t) = NULL; | 99 | int (*m_init) (mu_mailer_t) = NULL; |
105 | int (*u_init) (mu_url_t) = NULL; | ||
106 | 100 | ||
107 | mu_record_get_mailer (record, &m_init); | 101 | mu_record_get_mailer (record, &m_init); |
108 | mu_record_get_url (record, &u_init); | 102 | if (m_init) |
109 | if (m_init && u_init) | ||
110 | { | 103 | { |
111 | int status; | 104 | int status; |
112 | mu_url_t url; | ||
113 | mu_mailer_t mailer; | 105 | mu_mailer_t mailer; |
106 | int (*u_init) (mu_url_t) = NULL; | ||
114 | 107 | ||
115 | /* Allocate memory for mailer. */ | 108 | /* Allocate memory for mailer. */ |
116 | mailer = calloc (1, sizeof (*mailer)); | 109 | mailer = calloc (1, sizeof (*mailer)); |
... | @@ -124,29 +117,50 @@ mu_mailer_create (mu_mailer_t * pmailer, const char *name) | ... | @@ -124,29 +117,50 @@ mu_mailer_create (mu_mailer_t * pmailer, const char *name) |
124 | return status; | 117 | return status; |
125 | } | 118 | } |
126 | 119 | ||
127 | /* Parse the url, it may be a bad one and we should bail out if this | 120 | status = m_init (mailer); |
128 | failed. */ | 121 | if (status) |
129 | if ((status = mu_url_create (&url, name)) != 0 | ||
130 | || (status = u_init (url)) != 0) | ||
131 | { | 122 | { |
132 | mu_mailer_destroy (&mailer); | 123 | mu_mailer_destroy (&mailer); |
133 | return status; | 124 | return status; |
134 | } | 125 | } |
135 | mailer->url = url; | ||
136 | 126 | ||
137 | status = m_init (mailer); | 127 | mu_record_get_url (record, &u_init); |
138 | if (status) | 128 | if (u_init && (status = u_init (url)) != 0) |
139 | mu_mailer_destroy (&mailer); | 129 | { |
140 | else | 130 | mu_mailer_destroy (&mailer); |
141 | *pmailer = mailer; | 131 | return status; |
132 | } | ||
133 | |||
134 | mailer->url = url; | ||
135 | *pmailer = mailer; | ||
142 | 136 | ||
143 | return status; | 137 | return status; |
144 | } | 138 | } |
145 | } | 139 | } |
146 | 140 | ||
147 | return MU_ERR_MAILER_BAD_URL; | 141 | return MU_ERR_MAILER_BAD_URL; |
148 | } | 142 | } |
149 | 143 | ||
144 | int | ||
145 | mu_mailer_create (mu_mailer_t * pmailer, const char *name) | ||
146 | { | ||
147 | int status; | ||
148 | mu_url_t url; | ||
149 | |||
150 | if (name == NULL) | ||
151 | mu_mailer_get_url_default (&name); | ||
152 | |||
153 | status = mu_url_create (&url, name); | ||
154 | if (status) | ||
155 | return status; | ||
156 | status = mu_url_parse (url); | ||
157 | if (status == 0) | ||
158 | status = mu_mailer_create_from_url (pmailer, url); | ||
159 | if (status) | ||
160 | mu_url_destroy (&url); | ||
161 | return status; | ||
162 | } | ||
163 | |||
150 | void | 164 | void |
151 | mu_mailer_destroy (mu_mailer_t * pmailer) | 165 | mu_mailer_destroy (mu_mailer_t * pmailer) |
152 | { | 166 | { | ... | ... |
-
Please register or sign in to post a comment