1 /* GNU Mailutils -- a suite of utilities for electronic mail
2 Copyright (C) 1999-2021 Free Software Foundation, Inc.
3
4 GNU Mailutils 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 3, or (at your option)
7 any later version.
8
9 GNU Mailutils 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 GNU Mailutils. If not, see <http://www.gnu.org/licenses/>. */
16
17 #include "mail.h"
18
19 /*
20 * w[rite] [file] -- GNU extension
21 * w[rite] [msglist] file
22 * W[rite] [msglist] -- GNU extension
23 */
24
25 int
mail_write(int argc,char ** argv)26 mail_write (int argc, char **argv)
27 {
28 int rc;
29 mu_stream_t output;
30 char *filename = NULL;
31 char *namebuf = NULL;
32 msgset_t *msglist = NULL, *mp;
33 size_t total_size = 0, total_lines = 0;
34
35 if (mu_isupper (argv[0][0]))
36 {
37 if (msgset_parse (argc, argv, MSG_NODELETED|MSG_SILENT, &msglist))
38 return 1;
39 filename = namebuf = util_get_sender (msgset_msgno (msglist), 1);
40 }
41 else
42 {
43 if (argc >= 2)
44 {
45 filename = argv[--argc];
46 }
47 else
48 {
49 size_t n = get_cursor ();
50 if (n == 0)
51 {
52 mu_error (_("No applicable message"));
53 return 1;
54 }
55 mu_asprintf (&namebuf, "%lu", (unsigned long) n);
56 filename = namebuf;
57 }
58 if (msgset_parse (argc, argv, MSG_NODELETED|MSG_SILENT, &msglist))
59 return 1;
60 }
61
62 rc = mu_mailbox_expand_name (filename, &filename);
63 free (namebuf);
64 if (rc)
65 {
66 mu_diag_funcall (MU_DIAG_ERROR, "mu_mailbox_expand_name", NULL, rc);
67 msgset_free (msglist);
68 return 1;
69 }
70
71 rc = mu_file_stream_create (&output, filename,
72 MU_STREAM_APPEND|MU_STREAM_CREAT);
73 if (rc)
74 {
75 mu_error (_("can't open %s: %s"), filename, mu_strerror (rc));
76 free (filename);
77 msgset_free (msglist);
78 return 1;
79 }
80
81 for (mp = msglist; mp; mp = mp->next)
82 {
83 mu_message_t msg;
84 mu_attribute_t attr;
85 size_t stat[2];
86
87 if (util_get_message_part (mbox, mp, &msg))
88 continue;
89
90 rc = print_message_body (msg, output, stat);
91 if (rc == 0)
92 {
93 total_size += stat[0];
94 total_lines += stat[1];
95
96 /* mark as saved. */
97 if (msgset_length (mp) > 1)
98 util_get_message (mbox, msgset_msgno (mp), &msg);
99 mu_message_get_attribute (msg, &attr);
100 mu_attribute_set_userflag (attr, MAIL_ATTRIBUTE_SAVED);
101 }
102 else
103 mu_error (_("cannot save %lu: %s"),
104 (unsigned long) msgset_msgno (mp), mu_strerror (rc));
105 }
106
107 mu_stream_close (output);
108 mu_stream_destroy (&output);
109
110 mu_printf ("\"%s\" %3lu/%-5lu\n", filename,
111 (unsigned long) total_lines, (unsigned long) total_size);
112
113 free (filename);
114 msgset_free (msglist);
115 return 0;
116 }
117