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