send.c
4.53 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
/* GNU Mailutils -- a suite of utilities for electronic mail
Copyright (C) 2010-2012, 2014-2017 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 3, 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, see <http://www.gnu.org/licenses/>. */
#if defined(HAVE_CONFIG_H)
# include <config.h>
#endif
#include <mailutils/mailutils.h>
#include "mu.h"
static int read_recipients;
static mu_address_t rcpt_addr;
static mu_address_t from_addr;
static void
send_address_add (mu_address_t *paddr, const char *value)
{
mu_address_t addr = NULL;
int rc;
rc = mu_address_create (&addr, value);
if (rc)
{
mu_error (_("%s: %s"), value, mu_strerror (rc));
exit (1);
}
MU_ASSERT (mu_address_union (paddr, addr));
mu_address_destroy (&addr);
}
char send_docstring[] = N_("send a message");
static char send_args_doc[] = "URL-or-HOST [FILE]";
static void
set_from_address (struct mu_parseopt *po, struct mu_option *opt,
char const *arg)
{
MU_ASSERT (mu_address_create_null (&from_addr));
send_address_add (&from_addr, arg);
}
static void
set_rcpt_address (struct mu_parseopt *po, struct mu_option *opt,
char const *arg)
{
send_address_add (&rcpt_addr, arg);
}
static struct mu_option send_options[] = {
{ "from", 'F', N_("ADDRESS"), MU_OPTION_DEFAULT,
N_("send mail from this ADDRESS"),
mu_c_string, NULL, set_from_address },
{ "rcpt", 'T', N_("ADDRESS"), MU_OPTION_DEFAULT,
N_("send mail to this ADDRESS"),
mu_c_string, NULL, set_rcpt_address },
{ "read-recipients", 't', NULL, MU_OPTION_DEFAULT,
N_("read recipients from the message"),
mu_c_bool, &read_recipients },
MU_OPTION_END
};
int
main (int argc, char **argv)
{
char *infile;
mu_stream_t instr;
mu_message_t msg;
size_t count;
mu_url_t urlhint, url;
mu_mailer_t mailer;
MU_AUTH_REGISTER_ALL_MODULES ();
MU_ASSERT (mu_address_create_null (&rcpt_addr));
mu_register_all_mailer_formats ();
mu_action_getopt (&argc, &argv, send_options, send_docstring, send_args_doc);
if (argc < 1)
{
mu_error (_("not enough arguments"));
return 1;
}
infile = argv[1];
if (infile)
MU_ASSERT (mu_file_stream_create (&instr, infile,
MU_STREAM_READ|MU_STREAM_SEEK));
else
MU_ASSERT (mu_stdio_stream_create (&instr, MU_STDIN_FD,
MU_STREAM_READ|MU_STREAM_SEEK));
MU_ASSERT (mu_stream_to_message (instr, &msg));
mu_stream_unref (instr);
mu_address_get_count (rcpt_addr, &count);
if (count == 0)
read_recipients = 1;
if (read_recipients)
{
int rc;
mu_header_t header;
const char *value;
MU_ASSERT (mu_message_get_header (msg, &header));
rc = mu_header_sget_value (header, MU_HEADER_TO, &value);
if (rc == 0)
send_address_add (&rcpt_addr, value);
else if (rc != MU_ERR_NOENT)
{
mu_diag_funcall (MU_DIAG_ERROR, "mu_header_sget_value",
MU_HEADER_TO, rc);
exit (1);
}
rc = mu_header_sget_value (header, MU_HEADER_CC, &value);
if (rc == 0)
send_address_add (&rcpt_addr, value);
else if (rc != MU_ERR_NOENT)
{
mu_diag_funcall (MU_DIAG_ERROR, "mu_header_sget_value",
MU_HEADER_CC, rc);
exit (1);
}
rc = mu_header_sget_value (header, MU_HEADER_BCC, &value);
if (rc == 0)
send_address_add (&rcpt_addr, value);
else if (rc != MU_ERR_NOENT)
{
mu_diag_funcall (MU_DIAG_ERROR, "mu_header_sget_value",
MU_HEADER_BCC, rc);
exit (1);
}
}
mu_address_get_count (rcpt_addr, &count);
if (count == 0)
{
mu_error (_("no recipients"));
exit (1);
}
MU_ASSERT (mu_url_create (&urlhint, "smtp://"));
MU_ASSERT (mu_url_create_hint (&url, argv[0], MU_URL_PARSE_DEFAULT,
urlhint));
mu_url_invalidate (url);
MU_ASSERT (mu_mailer_create_from_url (&mailer, url));
MU_ASSERT (mu_mailer_open (mailer, MU_STREAM_RDWR));
MU_ASSERT (mu_mailer_send_message (mailer, msg, from_addr, rcpt_addr));
mu_mailer_close (mailer);
mu_mailer_destroy (&mailer);
return 0;
}
/*
MU Setup: send
mu-handler: mutool_send
mu-docstring: send_docstring
End MU Setup:
*/