]>
diplodocus.org Git - nmh/blob - uip/rcvstore.c
0cae6014a00e09a325b2b0d83321cc9687943a20
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_version.h"
10 #include "sbr/print_help.h"
11 #include "sbr/seq_add.h"
12 #include "sbr/error.h"
14 #include "h/signals.h"
18 #include "sbr/m_maildir.h"
19 #include "sbr/m_mktemp.h"
20 #include "sbr/makedir.h"
22 #define RCVSTORE_SWITCHES \
23 X("create", 0, CRETSW) \
24 X("nocreate", 0, NCRETSW) \
25 X("unseen", 0, UNSEENSW) \
26 X("nounseen", 0, NUNSEENSW) \
27 X("public", 0, PUBSW) \
28 X("nopublic", 0, NPUBSW) \
29 X("zero", 0, ZEROSW) \
30 X("nozero", 0, NZEROSW) \
31 X("sequence name", 0, SEQSW) \
32 X("version", 0, VERSIONSW) \
33 X("help", 0, HELPSW) \
35 #define X(sw, minchars, id) id,
36 DEFINE_SWITCH_ENUM(RCVSTORE
);
39 #define X(sw, minchars, id) { sw, minchars, id },
40 DEFINE_SWITCH_ARRAY(RCVSTORE
, switches
);
45 * name of temporary file to store incoming message
47 static char *tmpfilenam
= NULL
;
49 static void unlink_done(int) NORETURN
;
52 main (int argc
, char **argv
)
60 char *cp
, *maildir
, *folder
= NULL
, buf
[BUFSIZ
];
61 char **argp
, **arguments
;
62 svector_t seqs
= svector_create (0);
66 if (nmh_init(argv
[0], true, false)) { return 1; }
68 set_done(unlink_done
);
71 arguments
= getarguments (invo_name
, argc
, argv
, 1);
75 while ((cp
= *argp
++)) {
77 switch (smatch (++cp
, switches
)) {
79 ambigsw (cp
, switches
);
82 die("-%s unknown", cp
);
85 snprintf (buf
, sizeof(buf
), "%s [+folder] [switches]",
87 print_help (buf
, switches
, 1);
90 print_version(invo_name
);
94 if (!(cp
= *argp
++) || *cp
== '-')
95 die("missing argument name to %s", argp
[-2]);
97 svector_push_back (seqs
, cp
);
130 if (*cp
== '+' || *cp
== '@') {
132 die("only one folder at a time!");
133 folder
= pluspath (cp
);
135 die("usage: %s [+folder] [switches]", invo_name
);
139 if (!context_find ("path"))
140 free (path ("./", TFOLDER
));
142 /* if no folder is given, use default folder */
144 folder
= getfolder (0);
145 maildir
= m_maildir (folder
);
147 /* check if folder exists */
148 if (stat (maildir
, &st
) == NOTOK
) {
150 adios (maildir
, "error on folder");
152 die("folder %s doesn't exist", maildir
);
153 if (!makedir (maildir
))
154 die("unable to create folder %s", maildir
);
157 if (chdir (maildir
) == NOTOK
)
158 adios (maildir
, "unable to change directory to");
160 /* ignore a few signals */
161 SIGNAL (SIGHUP
, SIG_IGN
);
162 SIGNAL (SIGINT
, SIG_IGN
);
163 SIGNAL (SIGQUIT
, SIG_IGN
);
164 SIGNAL (SIGTERM
, SIG_IGN
);
166 /* create a temporary file */
167 tmpfilenam
= m_mktemp (invo_name
, &fd
, NULL
);
168 if (tmpfilenam
== NULL
) {
169 die("unable to create temporary file in %s", get_temp_dir());
171 chmod (tmpfilenam
, m_gmprot());
173 /* copy the message from stdin into temp file */
174 cpydata (fileno (stdin
), fd
, "standard input", tmpfilenam
);
176 if (fstat (fd
, &st
) == NOTOK
) {
177 (void) m_unlink (tmpfilenam
);
178 adios (tmpfilenam
, "unable to fstat");
180 if (close (fd
) == NOTOK
)
181 adios (tmpfilenam
, "error closing");
183 /* don't add file if it is empty */
184 if (st
.st_size
== 0) {
185 (void) m_unlink (tmpfilenam
);
186 inform("empty file");
191 * read folder and create message structure
193 if (!(mp
= folder_read (folder
, 1)))
194 die("unable to read folder %s", folder
);
197 * Link message into folder, and possibly add
198 * to the Unseen-Sequence's.
200 if ((msgnum
= folder_addmsg (&mp
, tmpfilenam
, 0, unseensw
, 0, 0, NULL
)) == -1)
204 * Add the message to any extra sequences
205 * that have been specified.
208 /* The only reason that seqp was checked to be non-zero is in
209 case a -nosequence switch is added. */
210 for (seqp
= 0; seqp
< svector_size (seqs
); seqp
++) {
211 if (!seq_addmsg (mp
, svector_at (seqs
, seqp
), msgnum
, publicsw
,
218 seq_setunseen (mp
, 0); /* synchronize any Unseen-Sequence's */
219 seq_save (mp
); /* synchronize and save message sequences */
220 folder_free (mp
); /* free folder/message structure */
222 context_save (); /* save the global context file */
223 (void) m_unlink (tmpfilenam
); /* remove temporary file */
234 unlink_done(int status
)
236 if (tmpfilenam
&& *tmpfilenam
)
237 (void) m_unlink (tmpfilenam
);