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