]> diplodocus.org Git - nmh/blob - uip/rcvstore.c
12d1ab2deefd908f367dffb34862d4c8a168e285
[nmh] / uip / rcvstore.c
1
2 /*
3 * rcvstore.c -- asynchronously add mail to a folder
4 *
5 * This code is Copyright (c) 2002, by the authors of nmh. See the
6 * COPYRIGHT file in the root directory of the nmh distribution for
7 * complete copyright information.
8 */
9
10 #include <h/mh.h>
11 #include <fcntl.h>
12 #include <h/signals.h>
13 #include <signal.h>
14 #include <h/mts.h>
15
16 #define RCVSTORE_SWITCHES \
17 X("create", 0, CRETSW) \
18 X("nocreate", 0, NCRETSW) \
19 X("unseen", 0, UNSEENSW) \
20 X("nounseen", 0, NUNSEENSW) \
21 X("public", 0, PUBSW) \
22 X("nopublic", 0, NPUBSW) \
23 X("zero", 0, ZEROSW) \
24 X("nozero", 0, NZEROSW) \
25 X("sequence name", 0, SEQSW) \
26 X("version", 0, VERSIONSW) \
27 X("help", 0, HELPSW) \
28
29 #define X(sw, minchars, id) id,
30 DEFINE_SWITCH_ENUM(RCVSTORE);
31 #undef X
32
33 #define X(sw, minchars, id) { sw, minchars, id },
34 DEFINE_SWITCH_ARRAY(RCVSTORE, switches);
35 #undef X
36
37
38 /*
39 * name of temporary file to store incoming message
40 */
41 static char *tmpfilenam = NULL;
42
43 static void unlink_done(int) NORETURN;
44
45 int
46 main (int argc, char **argv)
47 {
48 int publicsw = -1, zerosw = 0;
49 int create = 1, unseensw = 1;
50 int fd, msgnum;
51 size_t seqp = 0;
52 char *cp, *maildir, *folder = NULL, buf[BUFSIZ];
53 char **argp, **arguments;
54 svector_t seqs = svector_create (0);
55 struct msgs *mp;
56 struct stat st;
57
58 done=unlink_done;
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 (0);
87 case VERSIONSW:
88 print_version(invo_name);
89 done (0);
90
91 case SEQSW:
92 if (!(cp = *argp++) || *cp == '-')
93 adios (NULL, "missing argument name to %s", argp[-2]);
94
95 svector_push_back (seqs, cp);
96 seqp++;
97 continue;
98
99 case UNSEENSW:
100 unseensw = 1;
101 continue;
102 case NUNSEENSW:
103 unseensw = 0;
104 continue;
105
106 case PUBSW:
107 publicsw = 1;
108 continue;
109 case NPUBSW:
110 publicsw = 0;
111 continue;
112
113 case ZEROSW:
114 zerosw++;
115 continue;
116 case NZEROSW:
117 zerosw = 0;
118 continue;
119
120 case CRETSW:
121 create++;
122 continue;
123 case NCRETSW:
124 create = 0;
125 continue;
126 }
127 }
128 if (*cp == '+' || *cp == '@') {
129 if (folder)
130 adios (NULL, "only one folder at a time!");
131 else
132 folder = pluspath (cp);
133 } else {
134 adios (NULL, "usage: %s [+folder] [switches]", invo_name);
135 }
136 }
137
138 if (!context_find ("path"))
139 free (path ("./", TFOLDER));
140
141 /* if no folder is given, use default folder */
142 if (!folder)
143 folder = getfolder (0);
144 maildir = m_maildir (folder);
145
146 /* check if folder exists */
147 if (stat (maildir, &st) == NOTOK) {
148 if (errno != ENOENT)
149 adios (maildir, "error on folder");
150 if (!create)
151 adios (NULL, "folder %s doesn't exist", maildir);
152 if (!makedir (maildir))
153 adios (NULL, "unable to create folder %s", maildir);
154 }
155
156 if (chdir (maildir) == NOTOK)
157 adios (maildir, "unable to change directory to");
158
159 /* ignore a few signals */
160 SIGNAL (SIGHUP, SIG_IGN);
161 SIGNAL (SIGINT, SIG_IGN);
162 SIGNAL (SIGQUIT, SIG_IGN);
163 SIGNAL (SIGTERM, SIG_IGN);
164
165 /* create a temporary file */
166 tmpfilenam = m_mktemp (invo_name, &fd, NULL);
167 if (tmpfilenam == NULL) {
168 adios ("rcvstore", "unable to create temporary file");
169 }
170 chmod (tmpfilenam, m_gmprot());
171
172 /* copy the message from stdin into temp file */
173 cpydata (fileno (stdin), fd, "standard input", tmpfilenam);
174
175 if (fstat (fd, &st) == NOTOK) {
176 unlink (tmpfilenam);
177 adios (tmpfilenam, "unable to fstat");
178 }
179 if (close (fd) == NOTOK)
180 adios (tmpfilenam, "error closing");
181
182 /* don't add file if it is empty */
183 if (st.st_size == 0) {
184 unlink (tmpfilenam);
185 advise (NULL, "empty file");
186 done (0);
187 }
188
189 /*
190 * read folder and create message structure
191 */
192 if (!(mp = folder_read (folder, 1)))
193 adios (NULL, "unable to read folder %s", folder);
194
195 /*
196 * Link message into folder, and possibly add
197 * to the Unseen-Sequence's.
198 */
199 if ((msgnum = folder_addmsg (&mp, tmpfilenam, 0, unseensw, 0, 0, (char *)0)) == -1)
200 done (1);
201
202 /*
203 * Add the message to any extra sequences
204 * that have been specified.
205 */
206 if (seqp) {
207 /* The only reason that seqp was checked to be non-zero is in
208 case a -nosequence switch is added. */
209 for (seqp = 0; seqp < svector_size (seqs); seqp++) {
210 if (!seq_addmsg (mp, svector_at (seqs, seqp), msgnum, publicsw,
211 zerosw))
212 done (1);
213 }
214 }
215
216 svector_free (seqs);
217 seq_setunseen (mp, 0); /* synchronize any Unseen-Sequence's */
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 }