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