]>
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.
10 #include <h/signals.h>
14 #include "sbr/m_maildir.h"
15 #include "sbr/m_mktemp.h"
16 #include "sbr/makedir.h"
18 #define RCVSTORE_SWITCHES \
19 X("create", 0, CRETSW) \
20 X("nocreate", 0, NCRETSW) \
21 X("unseen", 0, UNSEENSW) \
22 X("nounseen", 0, NUNSEENSW) \
23 X("public", 0, PUBSW) \
24 X("nopublic", 0, NPUBSW) \
25 X("zero", 0, ZEROSW) \
26 X("nozero", 0, NZEROSW) \
27 X("sequence name", 0, SEQSW) \
28 X("version", 0, VERSIONSW) \
29 X("help", 0, HELPSW) \
31 #define X(sw, minchars, id) id,
32 DEFINE_SWITCH_ENUM(RCVSTORE
);
35 #define X(sw, minchars, id) { sw, minchars, id },
36 DEFINE_SWITCH_ARRAY(RCVSTORE
, switches
);
41 * name of temporary file to store incoming message
43 static char *tmpfilenam
= NULL
;
45 static void unlink_done(int) NORETURN
;
48 main (int argc
, char **argv
)
56 char *cp
, *maildir
, *folder
= NULL
, buf
[BUFSIZ
];
57 char **argp
, **arguments
;
58 svector_t seqs
= svector_create (0);
62 if (nmh_init(argv
[0], true, false)) { return 1; }
64 set_done(unlink_done
);
67 arguments
= getarguments (invo_name
, argc
, argv
, 1);
71 while ((cp
= *argp
++)) {
73 switch (smatch (++cp
, switches
)) {
75 ambigsw (cp
, switches
);
78 die("-%s unknown", cp
);
81 snprintf (buf
, sizeof(buf
), "%s [+folder] [switches]",
83 print_help (buf
, switches
, 1);
86 print_version(invo_name
);
90 if (!(cp
= *argp
++) || *cp
== '-')
91 die("missing argument name to %s", argp
[-2]);
93 svector_push_back (seqs
, cp
);
126 if (*cp
== '+' || *cp
== '@') {
128 die("only one folder at a time!");
129 folder
= pluspath (cp
);
131 die("usage: %s [+folder] [switches]", invo_name
);
135 if (!context_find ("path"))
136 free (path ("./", TFOLDER
));
138 /* if no folder is given, use default folder */
140 folder
= getfolder (0);
141 maildir
= m_maildir (folder
);
143 /* check if folder exists */
144 if (stat (maildir
, &st
) == NOTOK
) {
146 adios (maildir
, "error on folder");
148 die("folder %s doesn't exist", maildir
);
149 if (!makedir (maildir
))
150 die("unable to create folder %s", maildir
);
153 if (chdir (maildir
) == NOTOK
)
154 adios (maildir
, "unable to change directory to");
156 /* ignore a few signals */
157 SIGNAL (SIGHUP
, SIG_IGN
);
158 SIGNAL (SIGINT
, SIG_IGN
);
159 SIGNAL (SIGQUIT
, SIG_IGN
);
160 SIGNAL (SIGTERM
, SIG_IGN
);
162 /* create a temporary file */
163 tmpfilenam
= m_mktemp (invo_name
, &fd
, NULL
);
164 if (tmpfilenam
== NULL
) {
165 die("unable to create temporary file in %s", get_temp_dir());
167 chmod (tmpfilenam
, m_gmprot());
169 /* copy the message from stdin into temp file */
170 cpydata (fileno (stdin
), fd
, "standard input", tmpfilenam
);
172 if (fstat (fd
, &st
) == NOTOK
) {
173 (void) m_unlink (tmpfilenam
);
174 adios (tmpfilenam
, "unable to fstat");
176 if (close (fd
) == NOTOK
)
177 adios (tmpfilenam
, "error closing");
179 /* don't add file if it is empty */
180 if (st
.st_size
== 0) {
181 (void) m_unlink (tmpfilenam
);
182 inform("empty file");
187 * read folder and create message structure
189 if (!(mp
= folder_read (folder
, 1)))
190 die("unable to read folder %s", folder
);
193 * Link message into folder, and possibly add
194 * to the Unseen-Sequence's.
196 if ((msgnum
= folder_addmsg (&mp
, tmpfilenam
, 0, unseensw
, 0, 0, NULL
)) == -1)
200 * Add the message to any extra sequences
201 * that have been specified.
204 /* The only reason that seqp was checked to be non-zero is in
205 case a -nosequence switch is added. */
206 for (seqp
= 0; seqp
< svector_size (seqs
); seqp
++) {
207 if (!seq_addmsg (mp
, svector_at (seqs
, seqp
), msgnum
, publicsw
,
214 seq_setunseen (mp
, 0); /* synchronize any Unseen-Sequence's */
215 seq_save (mp
); /* synchronize and save message sequences */
216 folder_free (mp
); /* free folder/message structure */
218 context_save (); /* save the global context file */
219 (void) m_unlink (tmpfilenam
); /* remove temporary file */
230 unlink_done(int status
)
232 if (tmpfilenam
&& *tmpfilenam
)
233 (void) m_unlink (tmpfilenam
);