send.c
4.78 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
/* GNU Mailutils -- a suite of utilities for electronic mail
Copyright (C) 2010-2012, 2014-2016 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/>. */
#ifdef HAVE_CONFIG_H
# include <config.h>
#endif
#include <unistd.h>
#include <stdio.h>
#include <stdlib.h>
#include <netdb.h>
#include <netinet/in.h>
#include <mailutils/cctype.h>
#include <mailutils/mailutils.h>
#include <argp.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);
}
static char send_doc[] = N_("mu send - send a message.");
char send_docstring[] = N_("send a message");
static char send_args_doc[] = "URL-or-HOST [FILE]";
static struct argp_option send_options[] = {
{ "from", 'F', N_("ADDRESS"), 0,
N_("send mail from this ADDRESS") },
{ "rcpt", 'T', N_("ADDRESS"), 0,
N_("send mail to this ADDRESS") },
{ "read-recipients", 't', NULL, 0,
N_("read recipients from the message") },
{ NULL }
};
static error_t
send_parse_opt (int key, char *arg, struct argp_state *state)
{
switch (key)
{
case 'F':
MU_ASSERT (mu_address_create_null (&from_addr));
send_address_add (&from_addr, arg);
break;
case 'T':
send_address_add (&rcpt_addr, arg);
break;
case 't':
read_recipients = 1;
break;
default:
return ARGP_ERR_UNKNOWN;
}
return 0;
}
static struct argp send_argp = {
send_options,
send_parse_opt,
send_args_doc,
send_doc,
NULL,
NULL,
NULL
};
int
mutool_send (int argc, char **argv)
{
int index;
char *infile;
mu_stream_t instr;
mu_message_t msg;
size_t count;
mu_url_t urlhint, url;
mu_mailer_t mailer;
MU_ASSERT (mu_address_create_null (&rcpt_addr));
mu_register_all_mailer_formats ();
if (argp_parse (&send_argp, argc, argv, 0, &index, NULL))
return 1;
argc -= index;
argv += index;
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:
*/