Change link-time overriding of done() to run-time reassigning of equivalent
[mmh] / uip / rcvstore.c
1
2 /*
3  * rcvstore.c -- asynchronously add mail to a folder
4  *
5  * $Id$
6  *
7  * This code is Copyright (c) 2002, by the authors of nmh.  See the
8  * COPYRIGHT file in the root directory of the nmh distribution for
9  * complete copyright information.
10  */
11
12 #include <h/mh.h>
13 #include <fcntl.h>
14 #include <h/signals.h>
15 #include <errno.h>
16 #include <signal.h>
17 #include <h/mts.h>
18
19 static struct swit switches[] = {
20 #define CRETSW         0
21     { "create", 0 },
22 #define NCRETSW        1
23     { "nocreate", 0 },
24 #define UNSEENSW       2
25     { "unseen", 0 },
26 #define NUNSEENSW      3
27     { "nounseen", 0 },
28 #define PUBSW          4
29     { "public", 0 },
30 #define NPUBSW         5
31     { "nopublic",  0 },
32 #define ZEROSW         6
33     { "zero",   0 },
34 #define NZEROSW        7
35     { "nozero", 0 },
36 #define SEQSW          8
37     { "sequence name", 0 },
38 #define VERSIONSW      9
39     { "version", 0 },
40 #define HELPSW        10
41     { "help", 0 },
42     { NULL, 0 }
43 };
44
45
46 /*
47  * name of temporary file to store incoming message
48  */
49 static char *tmpfilenam = NULL;
50
51 static int unlink_done(int);
52
53 int
54 main (int argc, char **argv)
55 {
56     int publicsw = -1, zerosw = 0;
57     int create = 1, unseensw = 1;
58     int fd, msgnum, 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     done=unlink_done;
65
66 #ifdef LOCALE
67     setlocale(LC_ALL, "");
68 #endif
69     invo_name = r1bindex (argv[0], '/');
70
71     /* read user profile/context */
72     context_read();
73
74     mts_init (invo_name);
75     arguments = getarguments (invo_name, argc, argv, 1);
76     argp = arguments;
77
78     /* parse arguments */
79     while ((cp = *argp++)) {
80         if (*cp == '-') {
81             switch (smatch (++cp, switches)) {
82             case AMBIGSW: 
83                 ambigsw (cp, switches);
84                 done (1);
85             case UNKWNSW: 
86                 adios (NULL, "-%s unknown", cp);
87
88             case HELPSW: 
89                 snprintf (buf, sizeof(buf), "%s [+folder] [switches]",
90                           invo_name);
91                 print_help (buf, switches, 1);
92                 done (1);
93             case VERSIONSW:
94                 print_version(invo_name);
95                 done (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 = pluspath (cp);
141         } else {
142             adios (NULL, "usage: %s [+folder] [switches]", invo_name);
143         }
144     }
145
146     seqs[seqp] = NULL;  /* NULL terminate list of sequences */
147
148     if (!context_find ("path"))
149         free (path ("./", TFOLDER));
150
151     /* if no folder is given, use default folder */
152     if (!folder)
153         folder = getfolder (0);
154     maildir = m_maildir (folder);
155
156     /* check if folder exists */
157     if (stat (maildir, &st) == NOTOK) {
158         if (errno != ENOENT)
159             adios (maildir, "error on folder");
160         if (!create)
161             adios (NULL, "folder %s doesn't exist", maildir);
162         if (!makedir (maildir))
163             adios (NULL, "unable to create folder %s", maildir);
164     }
165
166     if (chdir (maildir) == NOTOK)
167         adios (maildir, "unable to change directory to");
168
169     /* ignore a few signals */
170     SIGNAL (SIGHUP, SIG_IGN);
171     SIGNAL (SIGINT, SIG_IGN);
172     SIGNAL (SIGQUIT, SIG_IGN);
173     SIGNAL (SIGTERM, SIG_IGN);
174
175     /* create a temporary file */
176     tmpfilenam = m_scratch ("", invo_name);
177     if ((fd = creat (tmpfilenam, m_gmprot ())) == NOTOK)
178         adios (tmpfilenam, "unable to create");
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, (char *)0)) == -1)
209         done (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             done (1);
218     }
219
220     seq_setunseen (mp, 0);      /* synchronize any Unseen-Sequence's      */
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 done (0);
229 }
230
231 /*
232  * Clean up and exit
233  */
234 static int
235 unlink_done(int status)
236 {
237     if (tmpfilenam && *tmpfilenam)
238         unlink (tmpfilenam);
239     exit (status);
240     return 1;  /* dead code to satisfy the compiler */
241 }