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