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