]>
diplodocus.org Git - nmh/blob - uip/rcvstore.c
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/seq_save.h"
10 #include "sbr/smatch.h"
11 #include "sbr/cpydata.h"
12 #include "sbr/getfolder.h"
13 #include "sbr/folder_read.h"
14 #include "sbr/folder_free.h"
15 #include "sbr/folder_addmsg.h"
16 #include "sbr/context_save.h"
17 #include "sbr/context_find.h"
18 #include "sbr/ambigsw.h"
20 #include "sbr/print_version.h"
21 #include "sbr/print_help.h"
22 #include "sbr/seq_add.h"
23 #include "sbr/error.h"
25 #include "h/signals.h"
29 #include "sbr/m_maildir.h"
30 #include "sbr/m_mktemp.h"
31 #include "sbr/makedir.h"
33 #define RCVSTORE_SWITCHES \
34 X("create", 0, CRETSW) \
35 X("nocreate", 0, NCRETSW) \
36 X("unseen", 0, UNSEENSW) \
37 X("nounseen", 0, NUNSEENSW) \
38 X("public", 0, PUBSW) \
39 X("nopublic", 0, NPUBSW) \
40 X("zero", 0, ZEROSW) \
41 X("nozero", 0, NZEROSW) \
42 X("sequence name", 0, SEQSW) \
43 X("version", 0, VERSIONSW) \
44 X("help", 0, HELPSW) \
46 #define X(sw, minchars, id) id,
47 DEFINE_SWITCH_ENUM(RCVSTORE
);
50 #define X(sw, minchars, id) { sw, minchars, id },
51 DEFINE_SWITCH_ARRAY(RCVSTORE
, switches
);
56 * name of temporary file to store incoming message
58 static char *tmpfilenam
= NULL
;
60 static void unlink_done(int) NORETURN
;
63 main (int argc
, char **argv
)
71 char *cp
, *maildir
, *folder
= NULL
, buf
[BUFSIZ
];
72 char **argp
, **arguments
;
73 svector_t seqs
= svector_create (0);
77 if (nmh_init(argv
[0], true, false)) { return 1; }
79 set_done(unlink_done
);
82 arguments
= getarguments (invo_name
, argc
, argv
, 1);
86 while ((cp
= *argp
++)) {
88 switch (smatch (++cp
, switches
)) {
90 ambigsw (cp
, switches
);
93 die("-%s unknown", cp
);
96 snprintf (buf
, sizeof(buf
), "%s [+folder] [switches]",
98 print_help (buf
, switches
, 1);
101 print_version(invo_name
);
105 if (!(cp
= *argp
++) || *cp
== '-')
106 die("missing argument name to %s", argp
[-2]);
108 svector_push_back (seqs
, cp
);
141 if (*cp
== '+' || *cp
== '@') {
143 die("only one folder at a time!");
144 folder
= pluspath (cp
);
146 die("usage: %s [+folder] [switches]", invo_name
);
150 if (!context_find ("path"))
151 free (path ("./", TFOLDER
));
153 /* if no folder is given, use default folder */
155 folder
= getfolder (0);
156 maildir
= m_maildir (folder
);
158 /* check if folder exists */
159 if (stat (maildir
, &st
) == NOTOK
) {
161 adios (maildir
, "error on folder");
163 die("folder %s doesn't exist", maildir
);
164 if (!makedir (maildir
))
165 die("unable to create folder %s", maildir
);
168 if (chdir (maildir
) == NOTOK
)
169 adios (maildir
, "unable to change directory to");
171 /* ignore a few signals */
172 SIGNAL (SIGHUP
, SIG_IGN
);
173 SIGNAL (SIGINT
, SIG_IGN
);
174 SIGNAL (SIGQUIT
, SIG_IGN
);
175 SIGNAL (SIGTERM
, SIG_IGN
);
177 /* create a temporary file */
178 tmpfilenam
= m_mktemp (invo_name
, &fd
, NULL
);
179 if (tmpfilenam
== NULL
) {
180 die("unable to create temporary file in %s", get_temp_dir());
182 chmod (tmpfilenam
, m_gmprot());
184 /* copy the message from stdin into temp file */
185 cpydata (fileno (stdin
), fd
, "standard input", tmpfilenam
);
187 if (fstat (fd
, &st
) == NOTOK
) {
188 (void) m_unlink (tmpfilenam
);
189 adios (tmpfilenam
, "unable to fstat");
191 if (close (fd
) == NOTOK
)
192 adios (tmpfilenam
, "error closing");
194 /* don't add file if it is empty */
195 if (st
.st_size
== 0) {
196 (void) m_unlink (tmpfilenam
);
197 inform("empty file");
202 * read folder and create message structure
204 if (!(mp
= folder_read (folder
, 1)))
205 die("unable to read folder %s", folder
);
208 * Link message into folder, and possibly add
209 * to the Unseen-Sequence's.
211 if ((msgnum
= folder_addmsg (&mp
, tmpfilenam
, 0, unseensw
, 0, 0, NULL
)) == -1)
215 * Add the message to any extra sequences
216 * that have been specified.
219 /* The only reason that seqp was checked to be non-zero is in
220 case a -nosequence switch is added. */
221 for (seqp
= 0; seqp
< svector_size (seqs
); seqp
++) {
222 if (!seq_addmsg (mp
, svector_at (seqs
, seqp
), msgnum
, publicsw
,
229 seq_setunseen (mp
, 0); /* synchronize any Unseen-Sequence's */
230 seq_save (mp
); /* synchronize and save message sequences */
231 folder_free (mp
); /* free folder/message structure */
233 context_save (); /* save the global context file */
234 (void) m_unlink (tmpfilenam
); /* remove temporary file */
245 unlink_done(int status
)
247 if (tmpfilenam
&& *tmpfilenam
)
248 (void) m_unlink (tmpfilenam
);