]>
diplodocus.org Git - nmh/blob - uip/rcvstore.c
8f28aa0d77e569aefe3d22f972fc079f16339695
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/context_save.h"
10 #include "sbr/context_find.h"
11 #include "sbr/ambigsw.h"
13 #include "sbr/print_version.h"
14 #include "sbr/print_help.h"
15 #include "sbr/seq_add.h"
16 #include "sbr/error.h"
18 #include "h/signals.h"
22 #include "sbr/m_maildir.h"
23 #include "sbr/m_mktemp.h"
24 #include "sbr/makedir.h"
26 #define RCVSTORE_SWITCHES \
27 X("create", 0, CRETSW) \
28 X("nocreate", 0, NCRETSW) \
29 X("unseen", 0, UNSEENSW) \
30 X("nounseen", 0, NUNSEENSW) \
31 X("public", 0, PUBSW) \
32 X("nopublic", 0, NPUBSW) \
33 X("zero", 0, ZEROSW) \
34 X("nozero", 0, NZEROSW) \
35 X("sequence name", 0, SEQSW) \
36 X("version", 0, VERSIONSW) \
37 X("help", 0, HELPSW) \
39 #define X(sw, minchars, id) id,
40 DEFINE_SWITCH_ENUM(RCVSTORE
);
43 #define X(sw, minchars, id) { sw, minchars, id },
44 DEFINE_SWITCH_ARRAY(RCVSTORE
, switches
);
49 * name of temporary file to store incoming message
51 static char *tmpfilenam
= NULL
;
53 static void unlink_done(int) NORETURN
;
56 main (int argc
, char **argv
)
64 char *cp
, *maildir
, *folder
= NULL
, buf
[BUFSIZ
];
65 char **argp
, **arguments
;
66 svector_t seqs
= svector_create (0);
70 if (nmh_init(argv
[0], true, false)) { return 1; }
72 set_done(unlink_done
);
75 arguments
= getarguments (invo_name
, argc
, argv
, 1);
79 while ((cp
= *argp
++)) {
81 switch (smatch (++cp
, switches
)) {
83 ambigsw (cp
, switches
);
86 die("-%s unknown", cp
);
89 snprintf (buf
, sizeof(buf
), "%s [+folder] [switches]",
91 print_help (buf
, switches
, 1);
94 print_version(invo_name
);
98 if (!(cp
= *argp
++) || *cp
== '-')
99 die("missing argument name to %s", argp
[-2]);
101 svector_push_back (seqs
, cp
);
134 if (*cp
== '+' || *cp
== '@') {
136 die("only one folder at a time!");
137 folder
= pluspath (cp
);
139 die("usage: %s [+folder] [switches]", invo_name
);
143 if (!context_find ("path"))
144 free (path ("./", TFOLDER
));
146 /* if no folder is given, use default folder */
148 folder
= getfolder (0);
149 maildir
= m_maildir (folder
);
151 /* check if folder exists */
152 if (stat (maildir
, &st
) == NOTOK
) {
154 adios (maildir
, "error on folder");
156 die("folder %s doesn't exist", maildir
);
157 if (!makedir (maildir
))
158 die("unable to create folder %s", maildir
);
161 if (chdir (maildir
) == NOTOK
)
162 adios (maildir
, "unable to change directory to");
164 /* ignore a few signals */
165 SIGNAL (SIGHUP
, SIG_IGN
);
166 SIGNAL (SIGINT
, SIG_IGN
);
167 SIGNAL (SIGQUIT
, SIG_IGN
);
168 SIGNAL (SIGTERM
, SIG_IGN
);
170 /* create a temporary file */
171 tmpfilenam
= m_mktemp (invo_name
, &fd
, NULL
);
172 if (tmpfilenam
== NULL
) {
173 die("unable to create temporary file in %s", get_temp_dir());
175 chmod (tmpfilenam
, m_gmprot());
177 /* copy the message from stdin into temp file */
178 cpydata (fileno (stdin
), fd
, "standard input", tmpfilenam
);
180 if (fstat (fd
, &st
) == NOTOK
) {
181 (void) m_unlink (tmpfilenam
);
182 adios (tmpfilenam
, "unable to fstat");
184 if (close (fd
) == NOTOK
)
185 adios (tmpfilenam
, "error closing");
187 /* don't add file if it is empty */
188 if (st
.st_size
== 0) {
189 (void) m_unlink (tmpfilenam
);
190 inform("empty file");
195 * read folder and create message structure
197 if (!(mp
= folder_read (folder
, 1)))
198 die("unable to read folder %s", folder
);
201 * Link message into folder, and possibly add
202 * to the Unseen-Sequence's.
204 if ((msgnum
= folder_addmsg (&mp
, tmpfilenam
, 0, unseensw
, 0, 0, NULL
)) == -1)
208 * Add the message to any extra sequences
209 * that have been specified.
212 /* The only reason that seqp was checked to be non-zero is in
213 case a -nosequence switch is added. */
214 for (seqp
= 0; seqp
< svector_size (seqs
); seqp
++) {
215 if (!seq_addmsg (mp
, svector_at (seqs
, seqp
), msgnum
, publicsw
,
222 seq_setunseen (mp
, 0); /* synchronize any Unseen-Sequence's */
223 seq_save (mp
); /* synchronize and save message sequences */
224 folder_free (mp
); /* free folder/message structure */
226 context_save (); /* save the global context file */
227 (void) m_unlink (tmpfilenam
); /* remove temporary file */
238 unlink_done(int status
)
240 if (tmpfilenam
&& *tmpfilenam
)
241 (void) m_unlink (tmpfilenam
);