movemail.c
7.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
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
/* GNU Mailutils -- a suite of utilities for electronic mail
Copyright (C) 2003 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., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA */
#if defined(HAVE_CONFIG_H)
# include <config.h>
#endif
#include <stdlib.h>
#include <string.h>
#include <mailutils/mailutils.h>
#include <mailutils/tls.h>
#include <mu_asprintf.h>
const char *program_version = "movemail (" PACKAGE_STRING ")";
static char doc[] = N_("GNU movemail");
static char args_doc[] = N_("inbox-url destfile [POP-password]");
#define OPT_EMACS 256
static struct argp_option options[] = {
{ "preserve", 'p', NULL, 0, N_("Preserve the source mailbox"), 0 },
{ "keep-messages", 0, NULL, OPTION_ALIAS, NULL },
{ "reverse", 'r', NULL, 0, N_("Reverse the sorting order"), 0 },
{ "emacs", OPT_EMACS, NULL, 0, N_("Output information used by Emacs rmail interface"), 0 },
{ NULL, 0, NULL, 0, NULL, 0 }
};
static int reverse_order;
static int preserve_mail;
static int emacs_mode;
static error_t
parse_opt (int key, char *arg, struct argp_state *state)
{
switch (key)
{
case 'r':
reverse_order++;
break;
case 'p':
preserve_mail++;
break;
case OPT_EMACS:
emacs_mode++;
break;
default:
return ARGP_ERR_UNKNOWN;
}
return 0;
}
static struct argp argp = {
options,
parse_opt,
args_doc,
doc,
NULL,
NULL, NULL
};
static const char *mail_capa[] = {
"common",
"license",
"mailbox",
#ifdef WITH_TLS
"tls",
#endif
NULL
};
int
movemail_error_printer (const char *fmt, va_list ap)
{
int n;
n = fprintf (stderr, "%s: ", program_invocation_short_name);
n += vfprintf (stderr, fmt, ap);
fputc ('\n', stderr);
return n + 1;
}
void
die (mailbox_t mbox, char *msg, int status)
{
url_t url = NULL;
mailbox_get_url (mbox, &url);
if (emacs_mode)
mu_error (_("%s:mailbox '%s': %s: %s"),
mu_errname (status),
url_to_string (url),
msg,
mu_strerror (status));
else
mu_error (_("mailbox '%s': %s: %s"),
url_to_string (url), msg, mu_strerror (status));
exit (1);
}
void
lock_mailbox (mailbox_t mbox)
{
locker_t lock;
int status;
status = mailbox_get_locker (mbox, &lock);
if (status)
die (mbox, _("Cannot retrieve locker"), status);
if (!lock)
/* Remote mailboxes have no lockers */
return;
status = locker_lock (lock);
if (status)
die (mbox, _("Cannot lock"), status);
}
/* A password ticket: returns the cleantext password. */
void
password_destroy (ticket_t t)
{
char *p = ticket_get_owner (t);
free (p);
}
int
password_pop (ticket_t t, url_t u, const char *challenge, char **ppwd)
{
char *p = ticket_get_owner (t);
*ppwd = strdup (p);
return 0;
}
void
attach_passwd_ticket (mailbox_t mbx, char *passwd)
{
folder_t folder = NULL;
authority_t auth = NULL;
char *p = strdup (passwd);
ticket_t t;
int rc;
ticket_create (&t, p);
ticket_set_destroy (t, password_destroy, p);
ticket_set_pop (t, password_pop, p);
if ((rc = mailbox_get_folder (mbx, &folder)))
die (mbx, _("mailbox_get_folder failed"), rc);
if ((rc = folder_get_authority (folder, &auth)))
die (mbx, _("folder_get_authority failed"), rc);
if (auth && (rc = authority_set_ticket (auth, t)))
die (mbx, _("authority_set_ticket failed"), rc);
}
/* Create and open a mailbox associated with the given URL,
flags and (optionally) password */
void
open_mailbox (mailbox_t *mbx, char *name, int flags, char *passwd)
{
int status = mailbox_create_default (mbx, name);
if (status)
{
mu_error (_("Could not create mailbox <%s>: %s\n"),
name ? name : _("default"),
mu_strerror (status));
exit (1);
}
if (passwd)
attach_passwd_ticket (*mbx, passwd);
status = mailbox_open (*mbx, flags);
if (status)
die (*mbx, _("Cannot open"), status);
lock_mailbox (*mbx);
}
int
move_message (mailbox_t src, mailbox_t dst, size_t msgno)
{
int rc;
message_t msg;
if ((rc = mailbox_get_message (src, msgno, &msg)) != 0)
{
fprintf (stderr, _("Cannot read message %lu: %s"),
(unsigned long) msgno, mu_strerror (rc));
return rc;
}
if ((rc = mailbox_append_message (dst, msg)) != 0)
{
fprintf (stderr, _("Cannot append message %lu: %s"),
(unsigned long) msgno, mu_strerror (rc));
return rc;
}
if (!preserve_mail)
{
attribute_t attr;
message_get_attribute (msg, &attr);
attribute_set_deleted (attr);
}
return rc;
}
/* Open source mailbox using compatibility syntax. Source_name is
of the form:
po:USERNAME[:POP-SERVER]
if POP-SERVER part is omitted, the MAILHOST environment variable
will be consulted. */
void
compatibility_mode (mailbox_t *mbx, char *source_name, char *password,
int flags)
{
char *tmp;
char *user_name = strtok (source_name+3, ":");
char *host = strtok (NULL, ":");
if (!host)
host = getenv ("MAILHOST");
if (!host)
{
mu_error (_("Hostname of the POP3 server is unknown"));
exit (1);
}
asprintf (&tmp, "pop://%s@%s", user_name, host);
open_mailbox (mbx, tmp, flags, password);
free (tmp);
}
int
main (int argc, char **argv)
{
int index;
mailbox_t source, dest;
size_t i, total;
int rc = 0;
char *source_name, *dest_name;
int flags;
/* Native Language Support */
mu_init_nls ();
/* Register the desired mailbox formats. */
mu_register_all_mbox_formats ();
/* argument parsing */
mu_error_set_print (movemail_error_printer);
mu_argp_init (program_version, NULL);
#ifdef WITH_TLS
mu_tls_init_client_argp ();
#endif
mu_argp_parse (&argp, &argc, &argv, 0, mail_capa, &index, NULL);
argc -= index;
argv += index;
if (argc < 2 || argc > 3)
{
mu_error (_("Wrong number of arguments"));
return 1;
}
source_name = argv[0];
dest_name = argv[1];
flags = preserve_mail ? MU_STREAM_READ : MU_STREAM_RDWR;
if (strncmp (source_name, "po:", 3) == 0)
compatibility_mode (&source, source_name, argv[2], flags);
else
open_mailbox (&source, source_name, flags, argv[2]);
open_mailbox (&dest, dest_name, MU_STREAM_RDWR | MU_STREAM_CREAT, NULL);
mailbox_messages_count (source, &total);
if (reverse_order)
{
for (i = total; rc == 0 && i > 0; i--)
move_message (source, dest, i);
}
else
{
for (i = 1; rc == 0 && i <= total; i++)
move_message (source, dest, i);
}
if (rc)
return rc;
mailbox_flush (source, 1);
mailbox_close (source);
mailbox_destroy (&source);
mailbox_close (dest);
mailbox_destroy (&dest);
return 0;
}