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