mail.remote now working as an SMTP forwarder.
Showing
4 changed files
with
202 additions
and
76 deletions
... | @@ -3,7 +3,7 @@ ACLOCAL_AMFLAGS = -I m4 | ... | @@ -3,7 +3,7 @@ ACLOCAL_AMFLAGS = -I m4 |
3 | 3 | ||
4 | SUBDIRS = include doc m4 lib MySql mailbox frm from pop3d imap4d \ | 4 | SUBDIRS = include doc m4 lib MySql mailbox frm from pop3d imap4d \ |
5 | mail sieve scripts libmu_scm guimb messages comsat readmsg \ | 5 | mail sieve scripts libmu_scm guimb messages comsat readmsg \ |
6 | mail.local dotlock | 6 | mail.local mail.remote dotlock |
7 | 7 | ||
8 | EXTRA_DIST = mailutils.spec mailutils.spec.in README-alpha COPYING.FDL | 8 | EXTRA_DIST = mailutils.spec mailutils.spec.in README-alpha COPYING.FDL |
9 | 9 | ... | ... |
... | @@ -412,5 +412,8 @@ AC_OUTPUT(Makefile mailutils.spec | ... | @@ -412,5 +412,8 @@ AC_OUTPUT(Makefile mailutils.spec |
412 | MySql/Makefile | 412 | MySql/Makefile |
413 | comsat/Makefile | 413 | comsat/Makefile |
414 | mail.local/Makefile | 414 | mail.local/Makefile |
415 | mail.remote/Makefile | ||
416 | mh/Makefile | ||
415 | dotlock/Makefile | 417 | dotlock/Makefile |
416 | ) | 418 | ) |
419 | ... | ... |
1 | /* FIXME: the will no really work until I've completed the smtp mailer_t. | 1 | /* GNU mailutils - a suite of utilities for electronic mail |
2 | Until then, its just place holder code. | 2 | Copyright (C) 1999, 2000, 2001 Free Software Foundation, Inc. |
3 | */ | 3 | |
4 | This program is free software; you can redistribute it and/or modify | ||
5 | it under the terms of the GNU General Public License as published by | ||
6 | the Free Software Foundation; either version 2, or (at your option) | ||
7 | any later version. | ||
8 | |||
9 | This program is distributed in the hope that it will be useful, | ||
10 | but WITHOUT ANY WARRANTY; without even the implied warranty of | ||
11 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | ||
12 | GNU General Public License for more details. | ||
13 | |||
14 | You should have received a copy of the GNU General Public License | ||
15 | along with this program; if not, write to the Free Software | ||
16 | Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA. */ | ||
17 | |||
18 | #ifdef HAVE_CONFIG_H | ||
19 | # include <config.h> | ||
20 | #endif | ||
4 | 21 | ||
5 | #include <sys/types.h> | ||
6 | #include <sys/stat.h> | 22 | #include <sys/stat.h> |
23 | #include <sys/types.h> | ||
7 | 24 | ||
8 | #include <errno.h> | 25 | #include <errno.h> |
9 | #include <stdio.h> | 26 | #include <stdio.h> |
... | @@ -12,108 +29,212 @@ | ... | @@ -12,108 +29,212 @@ |
12 | #include <unistd.h> | 29 | #include <unistd.h> |
13 | 30 | ||
14 | #include <mailutils/address.h> | 31 | #include <mailutils/address.h> |
32 | #include <mailutils/argp.h> | ||
15 | #include <mailutils/debug.h> | 33 | #include <mailutils/debug.h> |
16 | #include <mailutils/errno.h> | 34 | #include <mailutils/errno.h> |
17 | #include <mailutils/list.h> | 35 | #include <mailutils/list.h> |
18 | #include <mailutils/mailer.h> | 36 | #include <mailutils/mailer.h> |
37 | #include <mailutils/mutil.h> | ||
19 | #include <mailutils/message.h> | 38 | #include <mailutils/message.h> |
20 | #include <mailutils/registrar.h> | 39 | #include <mailutils/registrar.h> |
21 | #include <mailutils/stream.h> | 40 | #include <mailutils/stream.h> |
22 | 41 | ||
23 | #define C(X) {int e; if((e = X) != 0) { \ | 42 | const char *argp_program_version = "mail.remote (" PACKAGE ") " VERSION; |
24 | fprintf(stderr, "%s failed: %s\n", #X, mu_errstring(e)); \ | 43 | const char *argp_program_bug_address = "<bug-mailutils@gnu.org>"; |
25 | exit(1); } } | 44 | static char doc[] = |
26 | 45 | "GNU mail.remote -- pseudo-sendmail interface for mail delivery\n" | |
27 | const char USAGE[] = | 46 | "\v" |
28 | "usage: mailer [-hd] [-m mailer] [-f from] [to]..." | ||
29 | ; | ||
30 | const char HELP[] = | ||
31 | " -h print this helpful message\n" | ||
32 | " -m a mailer URL (default is \"sendmail:\")\n" | ||
33 | " -f the envelope from address (default is from user environment)\n" | ||
34 | " to a list of envelope to addresses (default is from message)\n" | ||
35 | "\n" | 47 | "\n" |
36 | "An RFC2822 formatted message is read from stdin and delivered using\n" | 48 | "An RFC2822 formatted message is read from stdin and delivered using\n" |
37 | "the mailer.\n" | 49 | "the mailer. This utility can be used as a drop-in replacement\n" |
50 | "for /bin/sendmail to forward mail directly to an SMTP gateway.\n" | ||
51 | "\n" | ||
52 | "The default mailer is \"sendmail:\", which is not supported by this\n" | ||
53 | "utility (it is intended to be used when you don't have a working\n" | ||
54 | "sendmail). You should specify your SMTP gateway by specifying\n" | ||
55 | "a --mailer as something like \"smtp://mail.example.com\". This would\n" | ||
56 | "normally be added to your user-specific configuration file,\n" | ||
57 | " ~/.mailutils/mailutils,\n" | ||
58 | "or the global configuration file,\n" | ||
59 | " /etc/mailutils.rc,\n" | ||
60 | "with a line such as:\n" | ||
61 | " :mailer --mailer=smtp://mail.example.com\n" | ||
62 | "\n" | ||
63 | "If not explicitly specified, the default from address is derived from the\n" | ||
64 | "\"From:\" field in the message, if present, or the default user's email\n" | ||
65 | "address if not present.\n" | ||
66 | "\n" | ||
67 | "If not explicitly specified, the default to addresses are derived from the\n" | ||
68 | "\"To:\", \"Cc:\", and \"Bcc:\" fields in the message.\n" | ||
69 | "\n" | ||
70 | "If --debug is specified, the envelope commands in the SMTP protocol\n" | ||
71 | "transaction will be printed to stdout. If specified more than once,\n" | ||
72 | "the data part of the protocol transaction will also be printed to stdout.\n" | ||
38 | ; | 73 | ; |
39 | 74 | ||
40 | int | 75 | static struct argp_option options[] = { |
41 | main (int argc, char *argv[]) | 76 | {"from", 'f', "ADDR", 0, "Override the default from address\n"}, |
77 | {"debug", 'd', NULL, 0, "Enable debugging output"}, | ||
78 | { 0, 'o', "OPT", OPTION_HIDDEN, "Ignored for sendmail compatibility"}, | ||
79 | {0} | ||
80 | }; | ||
81 | |||
82 | static int optdebug; | ||
83 | static const char* optfrom; | ||
84 | |||
85 | static error_t | ||
86 | parse_opt (int key, char *arg, struct argp_state *state) | ||
42 | { | 87 | { |
43 | int opt; | 88 | switch (key) |
44 | int optdebug = 0; | ||
45 | char* optmailer = "sendmail:"; | ||
46 | char* optfrom = 0; | ||
47 | |||
48 | stream_t in = 0; | ||
49 | message_t msg = 0; | ||
50 | mailer_t mailer = 0; | ||
51 | address_t from = 0; | ||
52 | address_t to = 0; | ||
53 | |||
54 | while((opt = getopt(argc, argv, "hdm:f:")) != -1) | ||
55 | { | ||
56 | switch(opt) | ||
57 | { | 89 | { |
58 | case 'h': | 90 | case 'f': |
59 | printf("%s\n%s", USAGE, HELP); | 91 | optfrom = arg; |
60 | return 0; | 92 | break; |
61 | case 'd': | 93 | |
62 | optdebug++; | 94 | case 'd': |
63 | break; | 95 | optdebug++; |
64 | case 'm': | 96 | break; |
65 | optmailer = optarg; | 97 | |
66 | break; | 98 | case 'o': |
67 | case 'f': | 99 | break; |
68 | optfrom = optarg; | 100 | |
69 | break; | 101 | default: |
70 | default: | 102 | return ARGP_ERR_UNKNOWN; |
71 | fprintf(stderr, "%s\n", USAGE); | ||
72 | break; | ||
73 | } | 103 | } |
74 | } | 104 | return 0; |
105 | } | ||
106 | |||
107 | static struct argp argp = { | ||
108 | options, | ||
109 | parse_opt, | ||
110 | "[TO-ADDR]...", | ||
111 | doc, | ||
112 | }; | ||
113 | |||
114 | static const char *capa[] = { | ||
115 | "common", | ||
116 | "mailer", | ||
117 | "address", | ||
118 | "license", | ||
119 | NULL | ||
120 | }; | ||
121 | |||
122 | int | ||
123 | main (int argc, char **argv) | ||
124 | { | ||
125 | int status = 0; | ||
126 | int optind = 0; | ||
127 | |||
128 | stream_t in = 0; | ||
129 | message_t msg = 0; | ||
130 | mailer_t mailer = 0; | ||
131 | address_t from = 0; | ||
132 | address_t to = 0; | ||
133 | |||
134 | int mailer_flags = 0; | ||
75 | 135 | ||
76 | /* Register mailers. */ | 136 | /* Register mailers. */ |
77 | { | 137 | { |
78 | list_t bookie; | 138 | list_t bookie; |
79 | registrar_get_list (&bookie); | 139 | registrar_get_list (&bookie); |
80 | C( list_append (bookie, smtp_record) ) | 140 | list_append (bookie, smtp_record); |
81 | C( list_append (bookie, sendmail_record) ) | ||
82 | } | 141 | } |
83 | 142 | ||
84 | if(optfrom) | 143 | mu_argp_parse (&argp, &argc, &argv, 0, capa, &optind, NULL); |
85 | { | ||
86 | C( address_create(&from, optfrom) ) | ||
87 | } | ||
88 | 144 | ||
89 | if(argv[optind]) | 145 | if (optfrom) |
90 | { | 146 | { |
91 | char** av = argv + optind; | 147 | if ((status = address_create (&from, optfrom))) |
148 | { | ||
149 | fprintf (stderr, "Parsing from addresses failed: %s\n", | ||
150 | mu_errstring (status)); | ||
151 | goto end; | ||
152 | } | ||
153 | } | ||
92 | 154 | ||
93 | C( address_createv(&to, (const char**) av, -1) ) | 155 | if (argv[optind]) |
94 | } | 156 | { |
157 | char **av = argv + optind; | ||
158 | |||
159 | if ((status = address_createv (&to, (const char **) av, -1))) | ||
160 | { | ||
161 | fprintf (stderr, "Parsing to addresses failed: %s\n", | ||
162 | mu_errstring (status)); | ||
163 | goto end; | ||
164 | } | ||
165 | } | ||
95 | 166 | ||
96 | C( stdio_stream_create(&in, stdin, 0) ) | 167 | if ((status = stdio_stream_create (&in, stdin, 0))) |
168 | { | ||
169 | fprintf (stderr, "Failed: %s\n", mu_errstring (status)); | ||
170 | goto end; | ||
171 | } | ||
97 | 172 | ||
98 | C( stream_open(in) ) | 173 | if ((status = stream_open (in))) |
174 | { | ||
175 | fprintf (stderr, "Opening stdin failed: %s\n", mu_errstring (status)); | ||
176 | goto end; | ||
177 | } | ||
99 | 178 | ||
100 | C( message_create (&msg, NULL) ) | 179 | if ((status = message_create (&msg, NULL))) |
180 | { | ||
181 | fprintf (stderr, "Failed: %s\n", mu_errstring (status)); | ||
182 | goto end; | ||
183 | } | ||
101 | 184 | ||
102 | C( message_set_stream(msg, in, NULL) ) | 185 | if ((status = message_set_stream (msg, in, NULL))) |
186 | { | ||
187 | fprintf (stderr, "Failed: %s\n", | ||
188 | mu_errstring (status)); | ||
189 | goto end; | ||
190 | } | ||
103 | 191 | ||
104 | C( mailer_create(&mailer, optmailer) ) | 192 | if ((status = mailer_create (&mailer, NULL))) |
193 | { | ||
194 | const char *url = NULL; | ||
195 | mailer_get_url_default (&url); | ||
196 | fprintf (stderr, "Creating mailer '%s' failed: %s\n", | ||
197 | url, mu_errstring (status)); | ||
198 | goto end; | ||
199 | } | ||
105 | 200 | ||
106 | if(optdebug) | 201 | if (optdebug) |
107 | { | 202 | { |
108 | mu_debug_t debug; | 203 | mu_debug_t debug; |
109 | mailer_get_debug (mailer, &debug); | 204 | mailer_get_debug (mailer, &debug); |
110 | mu_debug_set_level (debug, MU_DEBUG_TRACE | MU_DEBUG_PROT); | 205 | mu_debug_set_level (debug, MU_DEBUG_TRACE | MU_DEBUG_PROT); |
111 | } | ||
112 | 206 | ||
113 | C( mailer_open(mailer, 0) ) | 207 | if (optdebug > 1) |
208 | mailer_flags = MAILER_FLAG_DEBUG_DATA; | ||
209 | } | ||
114 | 210 | ||
115 | C( mailer_send_message(mailer, msg, from, to) ) | 211 | if ((status = mailer_open (mailer, mailer_flags))) |
212 | { | ||
213 | const char *url = NULL; | ||
214 | mailer_get_url_default (&url); | ||
215 | fprintf (stderr, "Opening mailer '%s' failed: %s\n", | ||
216 | url, mu_errstring (status)); | ||
217 | goto end; | ||
218 | } | ||
116 | 219 | ||
117 | return 0; | 220 | if ((status = mailer_send_message (mailer, msg, from, to))) |
118 | } | 221 | { |
222 | fprintf (stderr, "Sending message failed: %s\n", mu_errstring (status)); | ||
223 | goto end; | ||
224 | } | ||
225 | |||
226 | if ((status = mailer_close (mailer))) | ||
227 | { | ||
228 | fprintf (stderr, "Closing mailer failed: %s\n", mu_errstring (status)); | ||
229 | goto end; | ||
230 | } | ||
119 | 231 | ||
232 | end: | ||
233 | |||
234 | address_destroy (&from); | ||
235 | address_destroy (&to); | ||
236 | stream_destroy (&in, NULL); | ||
237 | mailer_destroy (&mailer); | ||
238 | |||
239 | return status ? 1 : 0; | ||
240 | } | ... | ... |
-
Please register or sign in to post a comment