Reformated comments and long lines
[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 <h/mts.h>
15
16 static struct swit switches[] = {
17 #define CRETSW  0
18         { "create",   0 },
19 #define NCRETSW  1
20         { "nocreate", 0 },
21 #define UNSEENSW  2
22         { "unseen",   0 },
23 #define NUNSEENSW  3
24         { "nounseen", 0 },
25 #define PUBSW  4
26         { "public",   0 },
27 #define NPUBSW  5
28         { "nopublic", 0 },
29 #define ZEROSW  6
30         { "zero",     0 },
31 #define NZEROSW  7
32         { "nozero",   0 },
33 #define SEQSW  8
34         { "sequence name", 0 },
35 #define VERSIONSW  9
36         { "version", 0 },
37 #define HELPSW  10
38         { "help", 0 },
39         { NULL, 0 }
40 };
41
42
43 /*
44 ** name of temporary file to store incoming message
45 */
46 static char *tmpfilenam = NULL;
47
48 static void unlink_done(int) NORETURN;
49
50 int
51 main (int argc, char **argv)
52 {
53         int publicsw = -1, zerosw = 0;
54         int create = 1, unseensw = 1;
55         int fd, msgnum, 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 = r1bindex (argv[0], '/');
67
68         /* read user profile/context */
69         context_read();
70
71         mts_init (invo_name);
72         arguments = getarguments (invo_name, argc, argv, 1);
73         argp = arguments;
74
75         /* parse arguments */
76         while ((cp = *argp++)) {
77                 if (*cp == '-') {
78                         switch (smatch (++cp, switches)) {
79                         case AMBIGSW:
80                                 ambigsw (cp, switches);
81                                 done (1);
82                         case UNKWNSW:
83                                 adios (NULL, "-%s unknown", cp);
84
85                         case HELPSW:
86                                 snprintf (buf, sizeof(buf),
87                                         "%s [+folder] [switches]",
88                                         invo_name);
89                                 print_help (buf, switches, 1);
90                                 done (1);
91                         case VERSIONSW:
92                                 print_version(invo_name);
93                                 done (1);
94
95                         case SEQSW:
96                                 if (!(cp = *argp++) || *cp == '-')
97                                         adios (NULL, "missing argument name to %s", argp[-2]);
98
99                                 /* check if too many sequences specified */
100                                 if (seqp >= NUMATTRS)
101                                         adios (NULL, "too many sequences (more than %d) specified", NUMATTRS);
102                                 seqs[seqp++] = cp;
103                                 continue;
104
105                         case UNSEENSW:
106                                 unseensw = 1;
107                                 continue;
108                         case NUNSEENSW:
109                                 unseensw = 0;
110                                 continue;
111
112                         case PUBSW:
113                                 publicsw = 1;
114                                 continue;
115                         case NPUBSW:
116                                 publicsw = 0;
117                                 continue;
118
119                         case ZEROSW:
120                                 zerosw++;
121                                 continue;
122                         case NZEROSW:
123                                 zerosw = 0;
124                                 continue;
125
126                         case CRETSW:
127                                 create++;
128                                 continue;
129                         case NCRETSW:
130                                 create = 0;
131                                 continue;
132                         }
133                 }
134                 if (*cp == '+' || *cp == '@') {
135                         if (folder)
136                                 adios (NULL, "only one folder at a time!");
137                         else
138                                 folder = pluspath (cp);
139                 } else {
140                         adios (NULL, "usage: %s [+folder] [switches]",
141                                         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,
209                         (char *)0)) == -1)
210                 done (1);
211
212         /*
213         ** Add the message to any extra sequences
214         ** that have been specified.
215         */
216         for (seqp = 0; seqs[seqp]; seqp++) {
217                 if (!seq_addmsg (mp, seqs[seqp], msgnum, publicsw, zerosw))
218                         done (1);
219         }
220
221         seq_setunseen (mp, 0);  /* synchronize any Unseen-Sequence's */
222         seq_save (mp);  /* synchronize and save message sequences */
223         folder_free (mp);  /* free folder/message structure */
224
225         context_save ();  /* save the global context file */
226         unlink (tmpfilenam);  /* remove temporary file */
227         tmpfilenam = NULL;
228
229         done (0);
230         return 1;
231 }
232
233 /*
234 ** Clean up and exit
235 */
236 static void
237 unlink_done(int status)
238 {
239         if (tmpfilenam && *tmpfilenam)
240                 unlink (tmpfilenam);
241         exit (status);
242 }