]> diplodocus.org Git - nmh/blob - uip/rcvstore.c
bd03196bf64ee4e17fe05b386d84d8e4a5d3198c
[nmh] / uip / rcvstore.c
1 /* rcvstore.c -- asynchronously add mail to a folder
2 *
3 * This code is Copyright (c) 2002, by the authors of nmh. See the
4 * COPYRIGHT file in the root directory of the nmh distribution for
5 * complete copyright information.
6 */
7
8 #include "h/mh.h"
9 #include "sbr/cpydata.h"
10 #include "sbr/getfolder.h"
11 #include "sbr/folder_read.h"
12 #include "sbr/folder_free.h"
13 #include "sbr/folder_addmsg.h"
14 #include "sbr/context_save.h"
15 #include "sbr/context_find.h"
16 #include "sbr/ambigsw.h"
17 #include "sbr/path.h"
18 #include "sbr/print_version.h"
19 #include "sbr/print_help.h"
20 #include "sbr/seq_add.h"
21 #include "sbr/error.h"
22 #include <fcntl.h>
23 #include "h/signals.h"
24 #include "h/mts.h"
25 #include "h/done.h"
26 #include "h/utils.h"
27 #include "sbr/m_maildir.h"
28 #include "sbr/m_mktemp.h"
29 #include "sbr/makedir.h"
30
31 #define RCVSTORE_SWITCHES \
32 X("create", 0, CRETSW) \
33 X("nocreate", 0, NCRETSW) \
34 X("unseen", 0, UNSEENSW) \
35 X("nounseen", 0, NUNSEENSW) \
36 X("public", 0, PUBSW) \
37 X("nopublic", 0, NPUBSW) \
38 X("zero", 0, ZEROSW) \
39 X("nozero", 0, NZEROSW) \
40 X("sequence name", 0, SEQSW) \
41 X("version", 0, VERSIONSW) \
42 X("help", 0, HELPSW) \
43
44 #define X(sw, minchars, id) id,
45 DEFINE_SWITCH_ENUM(RCVSTORE);
46 #undef X
47
48 #define X(sw, minchars, id) { sw, minchars, id },
49 DEFINE_SWITCH_ARRAY(RCVSTORE, switches);
50 #undef X
51
52
53 /*
54 * name of temporary file to store incoming message
55 */
56 static char *tmpfilenam = NULL;
57
58 static void unlink_done(int) NORETURN;
59
60 int
61 main (int argc, char **argv)
62 {
63 int publicsw = -1;
64 bool zerosw = false;
65 bool create = true;
66 bool unseensw = true;
67 int fd, msgnum;
68 size_t seqp = 0;
69 char *cp, *maildir, *folder = NULL, buf[BUFSIZ];
70 char **argp, **arguments;
71 svector_t seqs = svector_create (0);
72 struct msgs *mp;
73 struct stat st;
74
75 if (nmh_init(argv[0], true, false)) { return 1; }
76
77 set_done(unlink_done);
78
79 mts_init ();
80 arguments = getarguments (invo_name, argc, argv, 1);
81 argp = arguments;
82
83 /* parse arguments */
84 while ((cp = *argp++)) {
85 if (*cp == '-') {
86 switch (smatch (++cp, switches)) {
87 case AMBIGSW:
88 ambigsw (cp, switches);
89 done (1);
90 case UNKWNSW:
91 die("-%s unknown", cp);
92
93 case HELPSW:
94 snprintf (buf, sizeof(buf), "%s [+folder] [switches]",
95 invo_name);
96 print_help (buf, switches, 1);
97 done (0);
98 case VERSIONSW:
99 print_version(invo_name);
100 done (0);
101
102 case SEQSW:
103 if (!(cp = *argp++) || *cp == '-')
104 die("missing argument name to %s", argp[-2]);
105
106 svector_push_back (seqs, cp);
107 seqp++;
108 continue;
109
110 case UNSEENSW:
111 unseensw = true;
112 continue;
113 case NUNSEENSW:
114 unseensw = false;
115 continue;
116
117 case PUBSW:
118 publicsw = 1;
119 continue;
120 case NPUBSW:
121 publicsw = 0;
122 continue;
123
124 case ZEROSW:
125 zerosw = true;
126 continue;
127 case NZEROSW:
128 zerosw = false;
129 continue;
130
131 case CRETSW:
132 create = true;
133 continue;
134 case NCRETSW:
135 create = false;
136 continue;
137 }
138 }
139 if (*cp == '+' || *cp == '@') {
140 if (folder)
141 die("only one folder at a time!");
142 folder = pluspath (cp);
143 } else {
144 die("usage: %s [+folder] [switches]", invo_name);
145 }
146 }
147
148 if (!context_find ("path"))
149 free (path ("./", TFOLDER));
150
151 /* if no folder is given, use default folder */
152 if (!folder)
153 folder = getfolder (0);
154 maildir = m_maildir (folder);
155
156 /* check if folder exists */
157 if (stat (maildir, &st) == NOTOK) {
158 if (errno != ENOENT)
159 adios (maildir, "error on folder");
160 if (!create)
161 die("folder %s doesn't exist", maildir);
162 if (!makedir (maildir))
163 die("unable to create folder %s", maildir);
164 }
165
166 if (chdir (maildir) == NOTOK)
167 adios (maildir, "unable to change directory to");
168
169 /* ignore a few signals */
170 SIGNAL (SIGHUP, SIG_IGN);
171 SIGNAL (SIGINT, SIG_IGN);
172 SIGNAL (SIGQUIT, SIG_IGN);
173 SIGNAL (SIGTERM, SIG_IGN);
174
175 /* create a temporary file */
176 tmpfilenam = m_mktemp (invo_name, &fd, NULL);
177 if (tmpfilenam == NULL) {
178 die("unable to create temporary file in %s", get_temp_dir());
179 }
180 chmod (tmpfilenam, m_gmprot());
181
182 /* copy the message from stdin into temp file */
183 cpydata (fileno (stdin), fd, "standard input", tmpfilenam);
184
185 if (fstat (fd, &st) == NOTOK) {
186 (void) m_unlink (tmpfilenam);
187 adios (tmpfilenam, "unable to fstat");
188 }
189 if (close (fd) == NOTOK)
190 adios (tmpfilenam, "error closing");
191
192 /* don't add file if it is empty */
193 if (st.st_size == 0) {
194 (void) m_unlink (tmpfilenam);
195 inform("empty file");
196 done (0);
197 }
198
199 /*
200 * read folder and create message structure
201 */
202 if (!(mp = folder_read (folder, 1)))
203 die("unable to read folder %s", folder);
204
205 /*
206 * Link message into folder, and possibly add
207 * to the Unseen-Sequence's.
208 */
209 if ((msgnum = folder_addmsg (&mp, tmpfilenam, 0, unseensw, 0, 0, NULL)) == -1)
210 done (1);
211
212 /*
213 * Add the message to any extra sequences
214 * that have been specified.
215 */
216 if (seqp) {
217 /* The only reason that seqp was checked to be non-zero is in
218 case a -nosequence switch is added. */
219 for (seqp = 0; seqp < svector_size (seqs); seqp++) {
220 if (!seq_addmsg (mp, svector_at (seqs, seqp), msgnum, publicsw,
221 zerosw))
222 done (1);
223 }
224 }
225
226 svector_free (seqs);
227 seq_setunseen (mp, 0); /* synchronize any Unseen-Sequence's */
228 seq_save (mp); /* synchronize and save message sequences */
229 folder_free (mp); /* free folder/message structure */
230
231 context_save (); /* save the global context file */
232 (void) m_unlink (tmpfilenam); /* remove temporary file */
233 tmpfilenam = NULL;
234
235 done (0);
236 return 1;
237 }
238
239 /*
240 * Clean up and exit
241 */
242 static void NORETURN
243 unlink_done(int status)
244 {
245 if (tmpfilenam && *tmpfilenam)
246 (void) m_unlink (tmpfilenam);
247 exit (status);
248 }