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