]> diplodocus.org Git - nmh/blob - uip/rcvstore.c
Removed temporary probes added in commit
[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 #ifdef LOCALE
60 setlocale(LC_ALL, "");
61 #endif
62 invo_name = r1bindex (argv[0], '/');
63
64 /* read user profile/context */
65 context_read();
66
67 mts_init (invo_name);
68 arguments = getarguments (invo_name, argc, argv, 1);
69 argp = arguments;
70
71 /* parse arguments */
72 while ((cp = *argp++)) {
73 if (*cp == '-') {
74 switch (smatch (++cp, switches)) {
75 case AMBIGSW:
76 ambigsw (cp, switches);
77 done (1);
78 case UNKWNSW:
79 adios (NULL, "-%s unknown", cp);
80
81 case HELPSW:
82 snprintf (buf, sizeof(buf), "%s [+folder] [switches]",
83 invo_name);
84 print_help (buf, switches, 1);
85 done (0);
86 case VERSIONSW:
87 print_version(invo_name);
88 done (0);
89
90 case SEQSW:
91 if (!(cp = *argp++) || *cp == '-')
92 adios (NULL, "missing argument name to %s", argp[-2]);
93
94 svector_push_back (seqs, cp);
95 seqp++;
96 continue;
97
98 case UNSEENSW:
99 unseensw = 1;
100 continue;
101 case NUNSEENSW:
102 unseensw = 0;
103 continue;
104
105 case PUBSW:
106 publicsw = 1;
107 continue;
108 case NPUBSW:
109 publicsw = 0;
110 continue;
111
112 case ZEROSW:
113 zerosw++;
114 continue;
115 case NZEROSW:
116 zerosw = 0;
117 continue;
118
119 case CRETSW:
120 create++;
121 continue;
122 case NCRETSW:
123 create = 0;
124 continue;
125 }
126 }
127 if (*cp == '+' || *cp == '@') {
128 if (folder)
129 adios (NULL, "only one folder at a time!");
130 else
131 folder = pluspath (cp);
132 } else {
133 adios (NULL, "usage: %s [+folder] [switches]", invo_name);
134 }
135 }
136
137 if (!context_find ("path"))
138 free (path ("./", TFOLDER));
139
140 /* if no folder is given, use default folder */
141 if (!folder)
142 folder = getfolder (0);
143 maildir = m_maildir (folder);
144
145 /* check if folder exists */
146 if (stat (maildir, &st) == NOTOK) {
147 if (errno != ENOENT)
148 adios (maildir, "error on folder");
149 if (!create)
150 adios (NULL, "folder %s doesn't exist", maildir);
151 if (!makedir (maildir))
152 adios (NULL, "unable to create folder %s", maildir);
153 }
154
155 if (chdir (maildir) == NOTOK)
156 adios (maildir, "unable to change directory to");
157
158 /* ignore a few signals */
159 SIGNAL (SIGHUP, SIG_IGN);
160 SIGNAL (SIGINT, SIG_IGN);
161 SIGNAL (SIGQUIT, SIG_IGN);
162 SIGNAL (SIGTERM, SIG_IGN);
163
164 /* create a temporary file */
165 tmpfilenam = m_mktemp (invo_name, &fd, NULL);
166 if (tmpfilenam == NULL) {
167 adios ("rcvstore", "unable to create temporary file");
168 }
169 chmod (tmpfilenam, m_gmprot());
170
171 /* copy the message from stdin into temp file */
172 cpydata (fileno (stdin), fd, "standard input", tmpfilenam);
173
174 if (fstat (fd, &st) == NOTOK) {
175 unlink (tmpfilenam);
176 adios (tmpfilenam, "unable to fstat");
177 }
178 if (close (fd) == NOTOK)
179 adios (tmpfilenam, "error closing");
180
181 /* don't add file if it is empty */
182 if (st.st_size == 0) {
183 unlink (tmpfilenam);
184 advise (NULL, "empty file");
185 done (0);
186 }
187
188 /*
189 * read folder and create message structure
190 */
191 if (!(mp = folder_read (folder, 1)))
192 adios (NULL, "unable to read folder %s", folder);
193
194 /*
195 * Link message into folder, and possibly add
196 * to the Unseen-Sequence's.
197 */
198 if ((msgnum = folder_addmsg (&mp, tmpfilenam, 0, unseensw, 0, 0, (char *)0)) == -1)
199 done (1);
200
201 /*
202 * Add the message to any extra sequences
203 * that have been specified.
204 */
205 if (seqp) {
206 /* The only reason that seqp was checked to be non-zero is in
207 case a -nosequence switch is added. */
208 for (seqp = 0; seqp < svector_size (seqs); seqp++) {
209 if (!seq_addmsg (mp, svector_at (seqs, seqp), msgnum, publicsw,
210 zerosw))
211 done (1);
212 }
213 }
214
215 svector_free (seqs);
216 seq_setunseen (mp, 0); /* synchronize any Unseen-Sequence's */
217 seq_save (mp); /* synchronize and save message sequences */
218 folder_free (mp); /* free folder/message structure */
219
220 context_save (); /* save the global context file */
221 unlink (tmpfilenam); /* remove temporary file */
222 tmpfilenam = NULL;
223
224 done (0);
225 return 1;
226 }
227
228 /*
229 * Clean up and exit
230 */
231 static void
232 unlink_done(int status)
233 {
234 if (tmpfilenam && *tmpfilenam)
235 unlink (tmpfilenam);
236 exit (status);
237 }