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