]>
diplodocus.org Git - nmh/blob - uip/rcvstore.c
a01c57ff0e80026c8ed7977f70f4e5f17cd8b5ce
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.
11 #include <h/signals.h>
15 #include "sbr/m_maildir.h"
16 #include "sbr/m_mktemp.h"
17 #include "sbr/makedir.h"
19 #define RCVSTORE_SWITCHES \
20 X("create", 0, CRETSW) \
21 X("nocreate", 0, NCRETSW) \
22 X("unseen", 0, UNSEENSW) \
23 X("nounseen", 0, NUNSEENSW) \
24 X("public", 0, PUBSW) \
25 X("nopublic", 0, NPUBSW) \
26 X("zero", 0, ZEROSW) \
27 X("nozero", 0, NZEROSW) \
28 X("sequence name", 0, SEQSW) \
29 X("version", 0, VERSIONSW) \
30 X("help", 0, HELPSW) \
32 #define X(sw, minchars, id) id,
33 DEFINE_SWITCH_ENUM(RCVSTORE
);
36 #define X(sw, minchars, id) { sw, minchars, id },
37 DEFINE_SWITCH_ARRAY(RCVSTORE
, switches
);
42 * name of temporary file to store incoming message
44 static char *tmpfilenam
= NULL
;
46 static void unlink_done(int) NORETURN
;
49 main (int argc
, char **argv
)
57 char *cp
, *maildir
, *folder
= NULL
, buf
[BUFSIZ
];
58 char **argp
, **arguments
;
59 svector_t seqs
= svector_create (0);
63 if (nmh_init(argv
[0], true, false)) { return 1; }
65 set_done(unlink_done
);
68 arguments
= getarguments (invo_name
, argc
, argv
, 1);
72 while ((cp
= *argp
++)) {
74 switch (smatch (++cp
, switches
)) {
76 ambigsw (cp
, switches
);
79 die("-%s unknown", cp
);
82 snprintf (buf
, sizeof(buf
), "%s [+folder] [switches]",
84 print_help (buf
, switches
, 1);
87 print_version(invo_name
);
91 if (!(cp
= *argp
++) || *cp
== '-')
92 die("missing argument name to %s", argp
[-2]);
94 svector_push_back (seqs
, cp
);
127 if (*cp
== '+' || *cp
== '@') {
129 die("only one folder at a time!");
130 folder
= pluspath (cp
);
132 die("usage: %s [+folder] [switches]", invo_name
);
136 if (!context_find ("path"))
137 free (path ("./", TFOLDER
));
139 /* if no folder is given, use default folder */
141 folder
= getfolder (0);
142 maildir
= m_maildir (folder
);
144 /* check if folder exists */
145 if (stat (maildir
, &st
) == NOTOK
) {
147 adios (maildir
, "error on folder");
149 die("folder %s doesn't exist", maildir
);
150 if (!makedir (maildir
))
151 die("unable to create folder %s", maildir
);
154 if (chdir (maildir
) == NOTOK
)
155 adios (maildir
, "unable to change directory to");
157 /* ignore a few signals */
158 SIGNAL (SIGHUP
, SIG_IGN
);
159 SIGNAL (SIGINT
, SIG_IGN
);
160 SIGNAL (SIGQUIT
, SIG_IGN
);
161 SIGNAL (SIGTERM
, SIG_IGN
);
163 /* create a temporary file */
164 tmpfilenam
= m_mktemp (invo_name
, &fd
, NULL
);
165 if (tmpfilenam
== NULL
) {
166 die("unable to create temporary file in %s", get_temp_dir());
168 chmod (tmpfilenam
, m_gmprot());
170 /* copy the message from stdin into temp file */
171 cpydata (fileno (stdin
), fd
, "standard input", tmpfilenam
);
173 if (fstat (fd
, &st
) == NOTOK
) {
174 (void) m_unlink (tmpfilenam
);
175 adios (tmpfilenam
, "unable to fstat");
177 if (close (fd
) == NOTOK
)
178 adios (tmpfilenam
, "error closing");
180 /* don't add file if it is empty */
181 if (st
.st_size
== 0) {
182 (void) m_unlink (tmpfilenam
);
183 inform("empty file");
188 * read folder and create message structure
190 if (!(mp
= folder_read (folder
, 1)))
191 die("unable to read folder %s", folder
);
194 * Link message into folder, and possibly add
195 * to the Unseen-Sequence's.
197 if ((msgnum
= folder_addmsg (&mp
, tmpfilenam
, 0, unseensw
, 0, 0, NULL
)) == -1)
201 * Add the message to any extra sequences
202 * that have been specified.
205 /* The only reason that seqp was checked to be non-zero is in
206 case a -nosequence switch is added. */
207 for (seqp
= 0; seqp
< svector_size (seqs
); seqp
++) {
208 if (!seq_addmsg (mp
, svector_at (seqs
, seqp
), msgnum
, publicsw
,
215 seq_setunseen (mp
, 0); /* synchronize any Unseen-Sequence's */
216 seq_save (mp
); /* synchronize and save message sequences */
217 folder_free (mp
); /* free folder/message structure */
219 context_save (); /* save the global context file */
220 (void) m_unlink (tmpfilenam
); /* remove temporary file */
231 unlink_done(int status
)
233 if (tmpfilenam
&& *tmpfilenam
)
234 (void) m_unlink (tmpfilenam
);