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