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