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