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