]> diplodocus.org Git - nmh/blob - uip/rcvstore.c
Quiet lock warning.
[nmh] / 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 extern int errno;
46
47 /*
48 * name of temporary file to store incoming message
49 */
50 static char *tmpfilenam = NULL;
51
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 #ifdef LOCALE
65 setlocale(LC_ALL, "");
66 #endif
67 invo_name = r1bindex (argv[0], '/');
68
69 /* read user profile/context */
70 context_read();
71
72 mts_init (invo_name);
73 arguments = getarguments (invo_name, argc, argv, 1);
74 argp = arguments;
75
76 /* parse arguments */
77 while ((cp = *argp++)) {
78 if (*cp == '-') {
79 switch (smatch (++cp, switches)) {
80 case AMBIGSW:
81 ambigsw (cp, switches);
82 done (1);
83 case UNKWNSW:
84 adios (NULL, "-%s unknown", cp);
85
86 case HELPSW:
87 snprintf (buf, sizeof(buf), "%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 = path (cp + 1, *cp == '+' ? TFOLDER : TSUBCWF);
139 } else {
140 adios (NULL, "usage: %s [+folder] [switches]", invo_name);
141 }
142 }
143
144 seqs[seqp] = NULL; /* NULL terminate list of sequences */
145
146 if (!context_find ("path"))
147 free (path ("./", TFOLDER));
148
149 /* if no folder is given, use default folder */
150 if (!folder)
151 folder = getfolder (0);
152 maildir = m_maildir (folder);
153
154 /* check if folder exists */
155 if (stat (maildir, &st) == NOTOK) {
156 if (errno != ENOENT)
157 adios (maildir, "error on folder");
158 if (!create)
159 adios (NULL, "folder %s doesn't exist", maildir);
160 if (!makedir (maildir))
161 adios (NULL, "unable to create folder %s", maildir);
162 }
163
164 if (chdir (maildir) == NOTOK)
165 adios (maildir, "unable to change directory to");
166
167 /* ignore a few signals */
168 SIGNAL (SIGHUP, SIG_IGN);
169 SIGNAL (SIGINT, SIG_IGN);
170 SIGNAL (SIGQUIT, SIG_IGN);
171 SIGNAL (SIGTERM, SIG_IGN);
172
173 /* create a temporary file */
174 tmpfilenam = m_scratch ("", invo_name);
175 if ((fd = creat (tmpfilenam, m_gmprot ())) == NOTOK)
176 adios (tmpfilenam, "unable to create");
177 chmod (tmpfilenam, m_gmprot());
178
179 /* copy the message from stdin into temp file */
180 cpydata (fileno (stdin), fd, "standard input", tmpfilenam);
181
182 if (fstat (fd, &st) == NOTOK) {
183 unlink (tmpfilenam);
184 adios (tmpfilenam, "unable to fstat");
185 }
186 if (close (fd) == NOTOK)
187 adios (tmpfilenam, "error closing");
188
189 /* don't add file if it is empty */
190 if (st.st_size == 0) {
191 unlink (tmpfilenam);
192 advise (NULL, "empty file");
193 done (0);
194 }
195
196 /*
197 * read folder and create message structure
198 */
199 if (!(mp = folder_read (folder)))
200 adios (NULL, "unable to read folder %s", folder);
201
202 /*
203 * Link message into folder, and possibly add
204 * to the Unseen-Sequence's.
205 */
206 if ((msgnum = folder_addmsg (&mp, tmpfilenam, 0, unseensw, 0, 0)) == -1)
207 done (1);
208
209 /*
210 * Add the message to any extra sequences
211 * that have been specified.
212 */
213 for (seqp = 0; seqs[seqp]; seqp++) {
214 if (!seq_addmsg (mp, seqs[seqp], msgnum, publicsw, zerosw))
215 done (1);
216 }
217
218 seq_setunseen (mp, 0); /* synchronize any Unseen-Sequence's */
219 seq_save (mp); /* synchronize and save message sequences */
220 folder_free (mp); /* free folder/message structure */
221
222 context_save (); /* save the global context file */
223 unlink (tmpfilenam); /* remove temporary file */
224 tmpfilenam = NULL;
225
226 return done (0);
227 }
228
229 /*
230 * Clean up and exit
231 */
232 int
233 done(int status)
234 {
235 if (tmpfilenam && *tmpfilenam)
236 unlink (tmpfilenam);
237 exit (status);
238 return 1; /* dead code to satisfy the compiler */
239 }