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