]>
diplodocus.org Git - nmh/blob - uip/rcvstore.c
df1c1e89abeca54351206fcb8d7101071c7f1abb
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/print_help.h"
10 #include "sbr/seq_add.h"
11 #include "sbr/error.h"
13 #include "h/signals.h"
17 #include "sbr/m_maildir.h"
18 #include "sbr/m_mktemp.h"
19 #include "sbr/makedir.h"
21 #define RCVSTORE_SWITCHES \
22 X("create", 0, CRETSW) \
23 X("nocreate", 0, NCRETSW) \
24 X("unseen", 0, UNSEENSW) \
25 X("nounseen", 0, NUNSEENSW) \
26 X("public", 0, PUBSW) \
27 X("nopublic", 0, NPUBSW) \
28 X("zero", 0, ZEROSW) \
29 X("nozero", 0, NZEROSW) \
30 X("sequence name", 0, SEQSW) \
31 X("version", 0, VERSIONSW) \
32 X("help", 0, HELPSW) \
34 #define X(sw, minchars, id) id,
35 DEFINE_SWITCH_ENUM(RCVSTORE
);
38 #define X(sw, minchars, id) { sw, minchars, id },
39 DEFINE_SWITCH_ARRAY(RCVSTORE
, switches
);
44 * name of temporary file to store incoming message
46 static char *tmpfilenam
= NULL
;
48 static void unlink_done(int) NORETURN
;
51 main (int argc
, char **argv
)
59 char *cp
, *maildir
, *folder
= NULL
, buf
[BUFSIZ
];
60 char **argp
, **arguments
;
61 svector_t seqs
= svector_create (0);
65 if (nmh_init(argv
[0], true, false)) { return 1; }
67 set_done(unlink_done
);
70 arguments
= getarguments (invo_name
, argc
, argv
, 1);
74 while ((cp
= *argp
++)) {
76 switch (smatch (++cp
, switches
)) {
78 ambigsw (cp
, switches
);
81 die("-%s unknown", cp
);
84 snprintf (buf
, sizeof(buf
), "%s [+folder] [switches]",
86 print_help (buf
, switches
, 1);
89 print_version(invo_name
);
93 if (!(cp
= *argp
++) || *cp
== '-')
94 die("missing argument name to %s", argp
[-2]);
96 svector_push_back (seqs
, cp
);
129 if (*cp
== '+' || *cp
== '@') {
131 die("only one folder at a time!");
132 folder
= pluspath (cp
);
134 die("usage: %s [+folder] [switches]", invo_name
);
138 if (!context_find ("path"))
139 free (path ("./", TFOLDER
));
141 /* if no folder is given, use default folder */
143 folder
= getfolder (0);
144 maildir
= m_maildir (folder
);
146 /* check if folder exists */
147 if (stat (maildir
, &st
) == NOTOK
) {
149 adios (maildir
, "error on folder");
151 die("folder %s doesn't exist", maildir
);
152 if (!makedir (maildir
))
153 die("unable to create folder %s", maildir
);
156 if (chdir (maildir
) == NOTOK
)
157 adios (maildir
, "unable to change directory to");
159 /* ignore a few signals */
160 SIGNAL (SIGHUP
, SIG_IGN
);
161 SIGNAL (SIGINT
, SIG_IGN
);
162 SIGNAL (SIGQUIT
, SIG_IGN
);
163 SIGNAL (SIGTERM
, SIG_IGN
);
165 /* create a temporary file */
166 tmpfilenam
= m_mktemp (invo_name
, &fd
, NULL
);
167 if (tmpfilenam
== NULL
) {
168 die("unable to create temporary file in %s", get_temp_dir());
170 chmod (tmpfilenam
, m_gmprot());
172 /* copy the message from stdin into temp file */
173 cpydata (fileno (stdin
), fd
, "standard input", tmpfilenam
);
175 if (fstat (fd
, &st
) == NOTOK
) {
176 (void) m_unlink (tmpfilenam
);
177 adios (tmpfilenam
, "unable to fstat");
179 if (close (fd
) == NOTOK
)
180 adios (tmpfilenam
, "error closing");
182 /* don't add file if it is empty */
183 if (st
.st_size
== 0) {
184 (void) m_unlink (tmpfilenam
);
185 inform("empty file");
190 * read folder and create message structure
192 if (!(mp
= folder_read (folder
, 1)))
193 die("unable to read folder %s", folder
);
196 * Link message into folder, and possibly add
197 * to the Unseen-Sequence's.
199 if ((msgnum
= folder_addmsg (&mp
, tmpfilenam
, 0, unseensw
, 0, 0, NULL
)) == -1)
203 * Add the message to any extra sequences
204 * that have been specified.
207 /* The only reason that seqp was checked to be non-zero is in
208 case a -nosequence switch is added. */
209 for (seqp
= 0; seqp
< svector_size (seqs
); seqp
++) {
210 if (!seq_addmsg (mp
, svector_at (seqs
, seqp
), msgnum
, publicsw
,
217 seq_setunseen (mp
, 0); /* synchronize any Unseen-Sequence's */
218 seq_save (mp
); /* synchronize and save message sequences */
219 folder_free (mp
); /* free folder/message structure */
221 context_save (); /* save the global context file */
222 (void) m_unlink (tmpfilenam
); /* remove temporary file */
233 unlink_done(int status
)
235 if (tmpfilenam
&& *tmpfilenam
)
236 (void) m_unlink (tmpfilenam
);