]>
diplodocus.org Git - nmh/blob - uip/rcvstore.c
bd03196bf64ee4e17fe05b386d84d8e4a5d3198c
1 /* rcvstore.c -- asynchronously add mail to a folder
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.
9 #include "sbr/cpydata.h"
10 #include "sbr/getfolder.h"
11 #include "sbr/folder_read.h"
12 #include "sbr/folder_free.h"
13 #include "sbr/folder_addmsg.h"
14 #include "sbr/context_save.h"
15 #include "sbr/context_find.h"
16 #include "sbr/ambigsw.h"
18 #include "sbr/print_version.h"
19 #include "sbr/print_help.h"
20 #include "sbr/seq_add.h"
21 #include "sbr/error.h"
23 #include "h/signals.h"
27 #include "sbr/m_maildir.h"
28 #include "sbr/m_mktemp.h"
29 #include "sbr/makedir.h"
31 #define RCVSTORE_SWITCHES \
32 X("create", 0, CRETSW) \
33 X("nocreate", 0, NCRETSW) \
34 X("unseen", 0, UNSEENSW) \
35 X("nounseen", 0, NUNSEENSW) \
36 X("public", 0, PUBSW) \
37 X("nopublic", 0, NPUBSW) \
38 X("zero", 0, ZEROSW) \
39 X("nozero", 0, NZEROSW) \
40 X("sequence name", 0, SEQSW) \
41 X("version", 0, VERSIONSW) \
42 X("help", 0, HELPSW) \
44 #define X(sw, minchars, id) id,
45 DEFINE_SWITCH_ENUM(RCVSTORE
);
48 #define X(sw, minchars, id) { sw, minchars, id },
49 DEFINE_SWITCH_ARRAY(RCVSTORE
, switches
);
54 * name of temporary file to store incoming message
56 static char *tmpfilenam
= NULL
;
58 static void unlink_done(int) NORETURN
;
61 main (int argc
, char **argv
)
69 char *cp
, *maildir
, *folder
= NULL
, buf
[BUFSIZ
];
70 char **argp
, **arguments
;
71 svector_t seqs
= svector_create (0);
75 if (nmh_init(argv
[0], true, false)) { return 1; }
77 set_done(unlink_done
);
80 arguments
= getarguments (invo_name
, argc
, argv
, 1);
84 while ((cp
= *argp
++)) {
86 switch (smatch (++cp
, switches
)) {
88 ambigsw (cp
, switches
);
91 die("-%s unknown", cp
);
94 snprintf (buf
, sizeof(buf
), "%s [+folder] [switches]",
96 print_help (buf
, switches
, 1);
99 print_version(invo_name
);
103 if (!(cp
= *argp
++) || *cp
== '-')
104 die("missing argument name to %s", argp
[-2]);
106 svector_push_back (seqs
, cp
);
139 if (*cp
== '+' || *cp
== '@') {
141 die("only one folder at a time!");
142 folder
= pluspath (cp
);
144 die("usage: %s [+folder] [switches]", invo_name
);
148 if (!context_find ("path"))
149 free (path ("./", TFOLDER
));
151 /* if no folder is given, use default folder */
153 folder
= getfolder (0);
154 maildir
= m_maildir (folder
);
156 /* check if folder exists */
157 if (stat (maildir
, &st
) == NOTOK
) {
159 adios (maildir
, "error on folder");
161 die("folder %s doesn't exist", maildir
);
162 if (!makedir (maildir
))
163 die("unable to create folder %s", maildir
);
166 if (chdir (maildir
) == NOTOK
)
167 adios (maildir
, "unable to change directory to");
169 /* ignore a few signals */
170 SIGNAL (SIGHUP
, SIG_IGN
);
171 SIGNAL (SIGINT
, SIG_IGN
);
172 SIGNAL (SIGQUIT
, SIG_IGN
);
173 SIGNAL (SIGTERM
, SIG_IGN
);
175 /* create a temporary file */
176 tmpfilenam
= m_mktemp (invo_name
, &fd
, NULL
);
177 if (tmpfilenam
== NULL
) {
178 die("unable to create temporary file in %s", get_temp_dir());
180 chmod (tmpfilenam
, m_gmprot());
182 /* copy the message from stdin into temp file */
183 cpydata (fileno (stdin
), fd
, "standard input", tmpfilenam
);
185 if (fstat (fd
, &st
) == NOTOK
) {
186 (void) m_unlink (tmpfilenam
);
187 adios (tmpfilenam
, "unable to fstat");
189 if (close (fd
) == NOTOK
)
190 adios (tmpfilenam
, "error closing");
192 /* don't add file if it is empty */
193 if (st
.st_size
== 0) {
194 (void) m_unlink (tmpfilenam
);
195 inform("empty file");
200 * read folder and create message structure
202 if (!(mp
= folder_read (folder
, 1)))
203 die("unable to read folder %s", folder
);
206 * Link message into folder, and possibly add
207 * to the Unseen-Sequence's.
209 if ((msgnum
= folder_addmsg (&mp
, tmpfilenam
, 0, unseensw
, 0, 0, NULL
)) == -1)
213 * Add the message to any extra sequences
214 * that have been specified.
217 /* The only reason that seqp was checked to be non-zero is in
218 case a -nosequence switch is added. */
219 for (seqp
= 0; seqp
< svector_size (seqs
); seqp
++) {
220 if (!seq_addmsg (mp
, svector_at (seqs
, seqp
), msgnum
, publicsw
,
227 seq_setunseen (mp
, 0); /* synchronize any Unseen-Sequence's */
228 seq_save (mp
); /* synchronize and save message sequences */
229 folder_free (mp
); /* free folder/message structure */
231 context_save (); /* save the global context file */
232 (void) m_unlink (tmpfilenam
); /* remove temporary file */
243 unlink_done(int status
)
245 if (tmpfilenam
&& *tmpfilenam
)
246 (void) m_unlink (tmpfilenam
);