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