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