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