]> diplodocus.org Git - nmh/blob - uip/rcvstore.c
98f17503a869fd99813cc710ebc0edb3df4d880f
[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 <h/mts.h>
14
15 #define RCVSTORE_SWITCHES \
16 X("create", 0, CRETSW) \
17 X("nocreate", 0, NCRETSW) \
18 X("unseen", 0, UNSEENSW) \
19 X("nounseen", 0, NUNSEENSW) \
20 X("public", 0, PUBSW) \
21 X("nopublic", 0, NPUBSW) \
22 X("zero", 0, ZEROSW) \
23 X("nozero", 0, NZEROSW) \
24 X("sequence name", 0, SEQSW) \
25 X("version", 0, VERSIONSW) \
26 X("help", 0, HELPSW) \
27
28 #define X(sw, minchars, id) id,
29 DEFINE_SWITCH_ENUM(RCVSTORE);
30 #undef X
31
32 #define X(sw, minchars, id) { sw, minchars, id },
33 DEFINE_SWITCH_ARRAY(RCVSTORE, switches);
34 #undef X
35
36
37 /*
38 * name of temporary file to store incoming message
39 */
40 static char *tmpfilenam = NULL;
41
42 static void unlink_done(int) NORETURN;
43
44 int
45 main (int argc, char **argv)
46 {
47 int publicsw = -1, zerosw = 0;
48 int create = 1, unseensw = 1;
49 int fd, msgnum;
50 size_t seqp = 0;
51 char *cp, *maildir, *folder = NULL, buf[BUFSIZ];
52 char **argp, **arguments;
53 svector_t seqs = svector_create (0);
54 struct msgs *mp;
55 struct stat st;
56
57 done=unlink_done;
58
59 setlocale(LC_ALL, "");
60 invo_name = r1bindex (argv[0], '/');
61
62 /* read user profile/context */
63 context_read();
64
65 mts_init (invo_name);
66 arguments = getarguments (invo_name, argc, argv, 1);
67 argp = arguments;
68
69 /* parse arguments */
70 while ((cp = *argp++)) {
71 if (*cp == '-') {
72 switch (smatch (++cp, switches)) {
73 case AMBIGSW:
74 ambigsw (cp, switches);
75 done (1);
76 case UNKWNSW:
77 adios (NULL, "-%s unknown", cp);
78
79 case HELPSW:
80 snprintf (buf, sizeof(buf), "%s [+folder] [switches]",
81 invo_name);
82 print_help (buf, switches, 1);
83 done (0);
84 case VERSIONSW:
85 print_version(invo_name);
86 done (0);
87
88 case SEQSW:
89 if (!(cp = *argp++) || *cp == '-')
90 adios (NULL, "missing argument name to %s", argp[-2]);
91
92 svector_push_back (seqs, cp);
93 seqp++;
94 continue;
95
96 case UNSEENSW:
97 unseensw = 1;
98 continue;
99 case NUNSEENSW:
100 unseensw = 0;
101 continue;
102
103 case PUBSW:
104 publicsw = 1;
105 continue;
106 case NPUBSW:
107 publicsw = 0;
108 continue;
109
110 case ZEROSW:
111 zerosw++;
112 continue;
113 case NZEROSW:
114 zerosw = 0;
115 continue;
116
117 case CRETSW:
118 create++;
119 continue;
120 case NCRETSW:
121 create = 0;
122 continue;
123 }
124 }
125 if (*cp == '+' || *cp == '@') {
126 if (folder)
127 adios (NULL, "only one folder at a time!");
128 else
129 folder = pluspath (cp);
130 } else {
131 adios (NULL, "usage: %s [+folder] [switches]", invo_name);
132 }
133 }
134
135 if (!context_find ("path"))
136 free (path ("./", TFOLDER));
137
138 /* if no folder is given, use default folder */
139 if (!folder)
140 folder = getfolder (0);
141 maildir = m_maildir (folder);
142
143 /* check if folder exists */
144 if (stat (maildir, &st) == NOTOK) {
145 if (errno != ENOENT)
146 adios (maildir, "error on folder");
147 if (!create)
148 adios (NULL, "folder %s doesn't exist", maildir);
149 if (!makedir (maildir))
150 adios (NULL, "unable to create folder %s", maildir);
151 }
152
153 if (chdir (maildir) == NOTOK)
154 adios (maildir, "unable to change directory to");
155
156 /* ignore a few signals */
157 SIGNAL (SIGHUP, SIG_IGN);
158 SIGNAL (SIGINT, SIG_IGN);
159 SIGNAL (SIGQUIT, SIG_IGN);
160 SIGNAL (SIGTERM, SIG_IGN);
161
162 /* create a temporary file */
163 tmpfilenam = m_mktemp (invo_name, &fd, NULL);
164 if (tmpfilenam == NULL) {
165 adios ("rcvstore", "unable to create temporary file");
166 }
167 chmod (tmpfilenam, m_gmprot());
168
169 /* copy the message from stdin into temp file */
170 cpydata (fileno (stdin), fd, "standard input", tmpfilenam);
171
172 if (fstat (fd, &st) == NOTOK) {
173 unlink (tmpfilenam);
174 adios (tmpfilenam, "unable to fstat");
175 }
176 if (close (fd) == NOTOK)
177 adios (tmpfilenam, "error closing");
178
179 /* don't add file if it is empty */
180 if (st.st_size == 0) {
181 unlink (tmpfilenam);
182 advise (NULL, "empty file");
183 done (0);
184 }
185
186 /*
187 * read folder and create message structure
188 */
189 if (!(mp = folder_read (folder, 1)))
190 adios (NULL, "unable to read folder %s", folder);
191
192 /*
193 * Link message into folder, and possibly add
194 * to the Unseen-Sequence's.
195 */
196 if ((msgnum = folder_addmsg (&mp, tmpfilenam, 0, unseensw, 0, 0, (char *)0)) == -1)
197 done (1);
198
199 /*
200 * Add the message to any extra sequences
201 * that have been specified.
202 */
203 if (seqp) {
204 /* The only reason that seqp was checked to be non-zero is in
205 case a -nosequence switch is added. */
206 for (seqp = 0; seqp < svector_size (seqs); seqp++) {
207 if (!seq_addmsg (mp, svector_at (seqs, seqp), msgnum, publicsw,
208 zerosw))
209 done (1);
210 }
211 }
212
213 svector_free (seqs);
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 done (0);
223 return 1;
224 }
225
226 /*
227 * Clean up and exit
228 */
229 static void
230 unlink_done(int status)
231 {
232 if (tmpfilenam && *tmpfilenam)
233 unlink (tmpfilenam);
234 exit (status);
235 }