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