]>
diplodocus.org Git - nmh/blob - uip/rcvstore.c
e01a5a26ae3d4920ff8cd30b9384cf1809bec837
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_setunseen.h"
10 #include "sbr/seq_save.h"
11 #include "sbr/smatch.h"
12 #include "sbr/cpydata.h"
13 #include "sbr/getfolder.h"
14 #include "sbr/folder_read.h"
15 #include "sbr/folder_free.h"
16 #include "sbr/folder_addmsg.h"
17 #include "sbr/context_save.h"
18 #include "sbr/context_find.h"
19 #include "sbr/ambigsw.h"
21 #include "sbr/print_version.h"
22 #include "sbr/print_help.h"
23 #include "sbr/seq_add.h"
24 #include "sbr/error.h"
26 #include "h/signals.h"
30 #include "sbr/m_maildir.h"
31 #include "sbr/m_mktemp.h"
32 #include "sbr/makedir.h"
34 #define RCVSTORE_SWITCHES \
35 X("create", 0, CRETSW) \
36 X("nocreate", 0, NCRETSW) \
37 X("unseen", 0, UNSEENSW) \
38 X("nounseen", 0, NUNSEENSW) \
39 X("public", 0, PUBSW) \
40 X("nopublic", 0, NPUBSW) \
41 X("zero", 0, ZEROSW) \
42 X("nozero", 0, NZEROSW) \
43 X("sequence name", 0, SEQSW) \
44 X("version", 0, VERSIONSW) \
45 X("help", 0, HELPSW) \
47 #define X(sw, minchars, id) id,
48 DEFINE_SWITCH_ENUM(RCVSTORE
);
51 #define X(sw, minchars, id) { sw, minchars, id },
52 DEFINE_SWITCH_ARRAY(RCVSTORE
, switches
);
57 * name of temporary file to store incoming message
59 static char *tmpfilenam
= NULL
;
61 static void unlink_done(int) NORETURN
;
64 main (int argc
, char **argv
)
72 char *cp
, *maildir
, *folder
= NULL
, buf
[BUFSIZ
];
73 char **argp
, **arguments
;
74 svector_t seqs
= svector_create (0);
78 if (nmh_init(argv
[0], true, false)) { return 1; }
80 set_done(unlink_done
);
83 arguments
= getarguments (invo_name
, argc
, argv
, 1);
87 while ((cp
= *argp
++)) {
89 switch (smatch (++cp
, switches
)) {
91 ambigsw (cp
, switches
);
94 die("-%s unknown", cp
);
97 snprintf (buf
, sizeof(buf
), "%s [+folder] [switches]",
99 print_help (buf
, switches
, 1);
102 print_version(invo_name
);
106 if (!(cp
= *argp
++) || *cp
== '-')
107 die("missing argument name to %s", argp
[-2]);
109 svector_push_back (seqs
, cp
);
142 if (*cp
== '+' || *cp
== '@') {
144 die("only one folder at a time!");
145 folder
= pluspath (cp
);
147 die("usage: %s [+folder] [switches]", invo_name
);
151 if (!context_find ("path"))
152 free (path ("./", TFOLDER
));
154 /* if no folder is given, use default folder */
156 folder
= getfolder (0);
157 maildir
= m_maildir (folder
);
159 /* check if folder exists */
160 if (stat (maildir
, &st
) == NOTOK
) {
162 adios (maildir
, "error on folder");
164 die("folder %s doesn't exist", maildir
);
165 if (!makedir (maildir
))
166 die("unable to create folder %s", maildir
);
169 if (chdir (maildir
) == NOTOK
)
170 adios (maildir
, "unable to change directory to");
172 /* ignore a few signals */
173 SIGNAL (SIGHUP
, SIG_IGN
);
174 SIGNAL (SIGINT
, SIG_IGN
);
175 SIGNAL (SIGQUIT
, SIG_IGN
);
176 SIGNAL (SIGTERM
, SIG_IGN
);
178 /* create a temporary file */
179 tmpfilenam
= m_mktemp (invo_name
, &fd
, NULL
);
180 if (tmpfilenam
== NULL
) {
181 die("unable to create temporary file in %s", get_temp_dir());
183 chmod (tmpfilenam
, m_gmprot());
185 /* copy the message from stdin into temp file */
186 cpydata (fileno (stdin
), fd
, "standard input", tmpfilenam
);
188 if (fstat (fd
, &st
) == NOTOK
) {
189 (void) m_unlink (tmpfilenam
);
190 adios (tmpfilenam
, "unable to fstat");
192 if (close (fd
) == NOTOK
)
193 adios (tmpfilenam
, "error closing");
195 /* don't add file if it is empty */
196 if (st
.st_size
== 0) {
197 (void) m_unlink (tmpfilenam
);
198 inform("empty file");
203 * read folder and create message structure
205 if (!(mp
= folder_read (folder
, 1)))
206 die("unable to read folder %s", folder
);
209 * Link message into folder, and possibly add
210 * to the Unseen-Sequence's.
212 if ((msgnum
= folder_addmsg (&mp
, tmpfilenam
, 0, unseensw
, 0, 0, NULL
)) == -1)
216 * Add the message to any extra sequences
217 * that have been specified.
220 /* The only reason that seqp was checked to be non-zero is in
221 case a -nosequence switch is added. */
222 for (seqp
= 0; seqp
< svector_size (seqs
); seqp
++) {
223 if (!seq_addmsg (mp
, svector_at (seqs
, seqp
), msgnum
, publicsw
,
230 seq_setunseen (mp
, 0); /* synchronize any Unseen-Sequence's */
231 seq_save (mp
); /* synchronize and save message sequences */
232 folder_free (mp
); /* free folder/message structure */
234 context_save (); /* save the global context file */
235 (void) m_unlink (tmpfilenam
); /* remove temporary file */
246 unlink_done(int status
)
248 if (tmpfilenam
&& *tmpfilenam
)
249 (void) m_unlink (tmpfilenam
);