Updated #include's to point to new location of mts.h on mts/generic
[mmh] / uip / rcvstore.c
1
2 /*
3  * rcvstore.c -- asynchronously add mail to a folder
4  *
5  * $Id$
6  */
7
8 #include <h/mh.h>
9 #include <fcntl.h>
10 #include <h/signals.h>
11 #include <errno.h>
12 #include <signal.h>
13 #include <mts/generic/mts.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 extern int errno;
42
43 /*
44  * name of temporary file to store incoming message
45  */
46 static char *tmpfilenam = NULL;
47
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 #ifdef LOCALE
61     setlocale(LC_ALL, "");
62 #endif
63     invo_name = r1bindex (argv[0], '/');
64
65     /* read user profile/context */
66     context_read();
67
68     mts_init (invo_name);
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), "%s [+folder] [switches]",
84                           invo_name);
85                 print_help (buf, switches, 1);
86                 done (1);
87             case VERSIONSW:
88                 print_version(invo_name);
89                 done (1);
90
91             case SEQSW: 
92                 if (!(cp = *argp++) || *cp == '-')
93                     adios (NULL, "missing argument name to %s", argp[-2]);
94
95                 /* check if too many sequences specified */
96                 if (seqp >= NUMATTRS)
97                     adios (NULL, "too many sequences (more than %d) specified", NUMATTRS);
98                 seqs[seqp++] = cp;
99                 continue;
100
101             case UNSEENSW:
102                 unseensw = 1;
103                 continue;
104             case NUNSEENSW:
105                 unseensw = 0;
106                 continue;
107
108             case PUBSW: 
109                 publicsw = 1;
110                 continue;
111             case NPUBSW: 
112                 publicsw = 0;
113                 continue;
114
115             case ZEROSW: 
116                 zerosw++;
117                 continue;
118             case NZEROSW: 
119                 zerosw = 0;
120                 continue;
121
122             case CRETSW: 
123                 create++;
124                 continue;
125             case NCRETSW: 
126                 create = 0;
127                 continue;
128             }
129         }
130         if (*cp == '+' || *cp == '@') {
131             if (folder)
132                 adios (NULL, "only one folder at a time!");
133             else
134                 folder = path (cp + 1, *cp == '+' ? TFOLDER : TSUBCWF);
135         } else {
136             adios (NULL, "usage: %s [+folder] [switches]", invo_name);
137         }
138     }
139
140     seqs[seqp] = NULL;  /* NULL terminate list of sequences */
141
142     if (!context_find ("path"))
143         free (path ("./", TFOLDER));
144
145     /* if no folder is given, use default folder */
146     if (!folder)
147         folder = getfolder (0);
148     maildir = m_maildir (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_scratch ("", invo_name);
171     if ((fd = creat (tmpfilenam, m_gmprot ())) == NOTOK)
172         adios (tmpfilenam, "unable to create");
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)) == -1)
203         done (1);
204
205     /*
206      * Add the message to any extra sequences
207      * that have been specified.
208      */
209     for (seqp = 0; seqs[seqp]; seqp++) {
210         if (!seq_addmsg (mp, seqs[seqp], msgnum, publicsw, zerosw))
211             done (1);
212     }
213
214     seq_setunseen (mp, 0);      /* synchronize any Unseen-Sequence's      */
215     seq_save (mp);              /* synchronize and save message sequences */
216     folder_free (mp);           /* free folder/message structure          */
217
218     context_save ();            /* save the global context file           */
219     unlink (tmpfilenam);        /* remove temporary file                  */
220     tmpfilenam = NULL;
221
222     return done (0);
223 }
224
225 /*
226  * Clean up and exit
227  */
228 int
229 done(int status)
230 {
231     if (tmpfilenam && *tmpfilenam)
232         unlink (tmpfilenam);
233     exit (status);
234     return 1;  /* dead code to satisfy the compiler */
235 }