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