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