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