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