]>
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/m_gmprot.h"
10 #include "sbr/getarguments.h"
11 #include "sbr/seq_setunseen.h"
12 #include "sbr/seq_save.h"
13 #include "sbr/smatch.h"
14 #include "sbr/cpydata.h"
15 #include "sbr/getfolder.h"
16 #include "sbr/folder_read.h"
17 #include "sbr/folder_free.h"
18 #include "sbr/folder_addmsg.h"
19 #include "sbr/context_save.h"
20 #include "sbr/context_find.h"
21 #include "sbr/ambigsw.h"
23 #include "sbr/print_version.h"
24 #include "sbr/print_help.h"
25 #include "sbr/seq_add.h"
26 #include "sbr/error.h"
28 #include "h/signals.h"
32 #include "sbr/m_maildir.h"
33 #include "sbr/m_mktemp.h"
34 #include "sbr/makedir.h"
36 #define RCVSTORE_SWITCHES \
37 X("create", 0, CRETSW) \
38 X("nocreate", 0, NCRETSW) \
39 X("unseen", 0, UNSEENSW) \
40 X("nounseen", 0, NUNSEENSW) \
41 X("public", 0, PUBSW) \
42 X("nopublic", 0, NPUBSW) \
43 X("zero", 0, ZEROSW) \
44 X("nozero", 0, NZEROSW) \
45 X("sequence name", 0, SEQSW) \
46 X("version", 0, VERSIONSW) \
47 X("help", 0, HELPSW) \
49 #define X(sw, minchars, id) id,
50 DEFINE_SWITCH_ENUM(RCVSTORE
);
53 #define X(sw, minchars, id) { sw, minchars, id },
54 DEFINE_SWITCH_ARRAY(RCVSTORE
, switches
);
59 * name of temporary file to store incoming message
61 static char *tmpfilenam
= NULL
;
63 static void unlink_done(int) NORETURN
;
66 main (int argc
, char **argv
)
74 char *cp
, *maildir
, *folder
= NULL
, buf
[BUFSIZ
];
75 char **argp
, **arguments
;
76 svector_t seqs
= svector_create (0);
80 if (nmh_init(argv
[0], true, false)) { return 1; }
82 set_done(unlink_done
);
85 arguments
= getarguments (invo_name
, argc
, argv
, 1);
89 while ((cp
= *argp
++)) {
91 switch (smatch (++cp
, switches
)) {
93 ambigsw (cp
, switches
);
96 die("-%s unknown", cp
);
99 snprintf (buf
, sizeof(buf
), "%s [+folder] [switches]",
101 print_help (buf
, switches
, 1);
104 print_version(invo_name
);
108 if (!(cp
= *argp
++) || *cp
== '-')
109 die("missing argument name to %s", argp
[-2]);
111 svector_push_back (seqs
, cp
);
144 if (*cp
== '+' || *cp
== '@') {
146 die("only one folder at a time!");
147 folder
= pluspath (cp
);
149 die("usage: %s [+folder] [switches]", invo_name
);
153 if (!context_find ("path"))
154 free (path ("./", TFOLDER
));
156 /* if no folder is given, use default folder */
158 folder
= getfolder (0);
159 maildir
= m_maildir (folder
);
161 /* check if folder exists */
162 if (stat (maildir
, &st
) == NOTOK
) {
164 adios (maildir
, "error on folder");
166 die("folder %s doesn't exist", maildir
);
167 if (!makedir (maildir
))
168 die("unable to create folder %s", maildir
);
171 if (chdir (maildir
) == NOTOK
)
172 adios (maildir
, "unable to change directory to");
174 /* ignore a few signals */
175 SIGNAL (SIGHUP
, SIG_IGN
);
176 SIGNAL (SIGINT
, SIG_IGN
);
177 SIGNAL (SIGQUIT
, SIG_IGN
);
178 SIGNAL (SIGTERM
, SIG_IGN
);
180 /* create a temporary file */
181 tmpfilenam
= m_mktemp (invo_name
, &fd
, NULL
);
182 if (tmpfilenam
== NULL
) {
183 die("unable to create temporary file in %s", get_temp_dir());
185 chmod (tmpfilenam
, m_gmprot());
187 /* copy the message from stdin into temp file */
188 cpydata (fileno (stdin
), fd
, "standard input", tmpfilenam
);
190 if (fstat (fd
, &st
) == NOTOK
) {
191 (void) m_unlink (tmpfilenam
);
192 adios (tmpfilenam
, "unable to fstat");
194 if (close (fd
) == NOTOK
)
195 adios (tmpfilenam
, "error closing");
197 /* don't add file if it is empty */
198 if (st
.st_size
== 0) {
199 (void) m_unlink (tmpfilenam
);
200 inform("empty file");
205 * read folder and create message structure
207 if (!(mp
= folder_read (folder
, 1)))
208 die("unable to read folder %s", folder
);
211 * Link message into folder, and possibly add
212 * to the Unseen-Sequence's.
214 if ((msgnum
= folder_addmsg (&mp
, tmpfilenam
, 0, unseensw
, 0, 0, NULL
)) == -1)
218 * Add the message to any extra sequences
219 * that have been specified.
222 /* The only reason that seqp was checked to be non-zero is in
223 case a -nosequence switch is added. */
224 for (seqp
= 0; seqp
< svector_size (seqs
); seqp
++) {
225 if (!seq_addmsg (mp
, svector_at (seqs
, seqp
), msgnum
, publicsw
,
232 seq_setunseen (mp
, 0); /* synchronize any Unseen-Sequence's */
233 seq_save (mp
); /* synchronize and save message sequences */
234 folder_free (mp
); /* free folder/message structure */
236 context_save (); /* save the global context file */
237 (void) m_unlink (tmpfilenam
); /* remove temporary file */
248 unlink_done(int status
)
250 if (tmpfilenam
&& *tmpfilenam
)
251 (void) m_unlink (tmpfilenam
);