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