Move #include from h/mh.h to source files
[mmh] / uip / rcvstore.c
1 /*
2 ** rcvstore.c -- asynchronously add mail to a folder
3 **
4 ** This code is Copyright (c) 2002, by the authors of nmh.  See the
5 ** COPYRIGHT file in the root directory of the nmh distribution for
6 ** complete copyright information.
7 */
8
9 #include <h/mh.h>
10 #include <fcntl.h>
11 #include <h/signals.h>
12 #include <errno.h>
13 #include <signal.h>
14 #include <unistd.h>
15 #include <sys/stat.h>
16 #include <locale.h>
17
18 static struct swit switches[] = {
19 #define CRETSW  0
20         { "create",   0 },
21 #define NCRETSW  1
22         { "nocreate", 2 },
23 #define UNSEENSW  2
24         { "unseen",   0 },
25 #define NUNSEENSW  3
26         { "nounseen", 2 },
27 #define PUBSW  4
28         { "public",   0 },
29 #define NPUBSW  5
30         { "nopublic", 2 },
31 #define ZEROSW  6
32         { "zero",     0 },
33 #define NZEROSW  7
34         { "nozero",   2 },
35 #define SEQSW  8
36         { "sequence name", 0 },
37 #define VERSIONSW  9
38         { "Version", 0 },
39 #define HELPSW  10
40         { "help", 0 },
41         { NULL, 0 }
42 };
43
44
45 /*
46 ** name of temporary file to store incoming message
47 */
48 static char *tmpfilenam = NULL;
49
50 void unlink_done();
51
52 int
53 main(int argc, char **argv)
54 {
55         int publicsw = -1, zerosw = 0;
56         int create = 1, unseensw = 1;
57         int fd, msgnum;
58         size_t seqp = 0;
59         char *cp, *maildir, *folder = NULL, buf[BUFSIZ];
60         char **argp, **arguments, *seqs[NUMATTRS+1];
61         struct msgs *mp;
62         struct stat st;
63
64         if (atexit(unlink_done) != 0) {
65                 adios(NULL, "atexit failed");
66         }
67
68         setlocale(LC_ALL, "");
69         invo_name = mhbasename(argv[0]);
70
71         /* read user profile/context */
72         context_read();
73
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                                 exit(1);
84                         case UNKWNSW:
85                                 adios(NULL, "-%s unknown", cp);
86
87                         case HELPSW:
88                                 snprintf(buf, sizeof(buf),
89                                                 "%s [+folder] [switches]",
90                                                 invo_name);
91                                 print_help(buf, switches, 1);
92                                 exit(1);
93                         case VERSIONSW:
94                                 print_version(invo_name);
95                                 exit(1);
96
97                         case SEQSW:
98                                 if (!(cp = *argp++) || *cp == '-')
99                                         adios(NULL, "missing argument name to %s", argp[-2]);
100
101                                 /* check if too many sequences specified */
102                                 if (seqp >= NUMATTRS)
103                                         adios(NULL, "too many sequences (more than %d) specified", NUMATTRS);
104                                 seqs[seqp++] = cp;
105                                 continue;
106
107                         case UNSEENSW:
108                                 unseensw = 1;
109                                 continue;
110                         case NUNSEENSW:
111                                 unseensw = 0;
112                                 continue;
113
114                         case PUBSW:
115                                 publicsw = 1;
116                                 continue;
117                         case NPUBSW:
118                                 publicsw = 0;
119                                 continue;
120
121                         case ZEROSW:
122                                 zerosw++;
123                                 continue;
124                         case NZEROSW:
125                                 zerosw = 0;
126                                 continue;
127
128                         case CRETSW:
129                                 create++;
130                                 continue;
131                         case NCRETSW:
132                                 create = 0;
133                                 continue;
134                         }
135                 }
136                 if (*cp == '+' || *cp == '@') {
137                         if (folder)
138                                 adios(NULL, "only one folder at a time!");
139                         else
140                                 folder = getcpy(expandfol(cp));
141                 } else {
142                         adios(NULL, "usage: %s [+folder] [switches]",
143                                         invo_name);
144                 }
145         }
146
147         seqs[seqp] = NULL;  /* NULL terminate list of sequences */
148
149         /* if no folder is given, use default folder */
150         if (!folder)
151                 folder = getdeffol();
152         maildir = toabsdir(folder);
153
154         /* check if folder exists */
155         if (stat(maildir, &st) == NOTOK) {
156                 if (errno != ENOENT)
157                         adios(maildir, "error on folder");
158                 if (!create)
159                         adios(NULL, "folder %s doesn't exist", maildir);
160                 if (!makedir(maildir))
161                         adios(NULL, "unable to create folder %s", maildir);
162         }
163
164         if (chdir(maildir) == NOTOK)
165                 adios(maildir, "unable to change directory to");
166
167         /* ignore a few signals */
168         SIGNAL(SIGHUP, SIG_IGN);
169         SIGNAL(SIGINT, SIG_IGN);
170         SIGNAL(SIGQUIT, SIG_IGN);
171         SIGNAL(SIGTERM, SIG_IGN);
172
173         /* create a temporary file */
174         tmpfilenam = m_mktemp(invo_name, &fd, NULL);
175         if (tmpfilenam == NULL) {
176                 adios("rcvstore", "unable to create temporary file");
177         }
178         chmod(tmpfilenam, m_gmprot());
179
180         /* copy the message from stdin into temp file */
181         cpydata(fileno(stdin), fd, "standard input", tmpfilenam);
182
183         if (fstat(fd, &st) == NOTOK) {
184                 unlink(tmpfilenam);
185                 adios(tmpfilenam, "unable to fstat");
186         }
187         if (close(fd) == NOTOK)
188                 adios(tmpfilenam, "error closing");
189
190         /* don't add file if it is empty */
191         if (st.st_size == 0) {
192                 unlink(tmpfilenam);
193                 advise(NULL, "empty file");
194                 exit(0);
195         }
196
197         /*
198         ** read folder and create message structure
199         */
200         if (!(mp = folder_read(folder)))
201                 adios(NULL, "unable to read folder %s", folder);
202
203         /*
204         ** Link message into folder, and possibly add
205         ** to the Unseen-Sequence's.
206         */
207         if ((msgnum = folder_addmsg(&mp, tmpfilenam, 0, unseensw, 0, 0, NULL))
208                         == -1)
209                 exit(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                         exit(1);
218         }
219
220         seq_setunseen(mp, 1);  /* add new msgs to unseen sequences */
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         return 0;
229 }
230
231 /*
232 ** Clean up and exit
233 */
234 void
235 unlink_done()
236 {
237         if (tmpfilenam && *tmpfilenam) {
238                 unlink(tmpfilenam);
239         }
240 }