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