]>
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 "sbr/print_version.h"
11 #include "sbr/print_help.h"
12 #include "sbr/seq_add.h"
13 #include "sbr/error.h"
15 #include "h/signals.h"
19 #include "sbr/m_maildir.h"
20 #include "sbr/m_mktemp.h"
21 #include "sbr/makedir.h"
23 #define RCVSTORE_SWITCHES \
24 X("create", 0, CRETSW) \
25 X("nocreate", 0, NCRETSW) \
26 X("unseen", 0, UNSEENSW) \
27 X("nounseen", 0, NUNSEENSW) \
28 X("public", 0, PUBSW) \
29 X("nopublic", 0, NPUBSW) \
30 X("zero", 0, ZEROSW) \
31 X("nozero", 0, NZEROSW) \
32 X("sequence name", 0, SEQSW) \
33 X("version", 0, VERSIONSW) \
34 X("help", 0, HELPSW) \
36 #define X(sw, minchars, id) id,
37 DEFINE_SWITCH_ENUM(RCVSTORE
);
40 #define X(sw, minchars, id) { sw, minchars, id },
41 DEFINE_SWITCH_ARRAY(RCVSTORE
, switches
);
46 * name of temporary file to store incoming message
48 static char *tmpfilenam
= NULL
;
50 static void unlink_done(int) NORETURN
;
53 main (int argc
, char **argv
)
61 char *cp
, *maildir
, *folder
= NULL
, buf
[BUFSIZ
];
62 char **argp
, **arguments
;
63 svector_t seqs
= svector_create (0);
67 if (nmh_init(argv
[0], true, false)) { return 1; }
69 set_done(unlink_done
);
72 arguments
= getarguments (invo_name
, argc
, argv
, 1);
76 while ((cp
= *argp
++)) {
78 switch (smatch (++cp
, switches
)) {
80 ambigsw (cp
, switches
);
83 die("-%s unknown", cp
);
86 snprintf (buf
, sizeof(buf
), "%s [+folder] [switches]",
88 print_help (buf
, switches
, 1);
91 print_version(invo_name
);
95 if (!(cp
= *argp
++) || *cp
== '-')
96 die("missing argument name to %s", argp
[-2]);
98 svector_push_back (seqs
, cp
);
131 if (*cp
== '+' || *cp
== '@') {
133 die("only one folder at a time!");
134 folder
= pluspath (cp
);
136 die("usage: %s [+folder] [switches]", invo_name
);
140 if (!context_find ("path"))
141 free (path ("./", TFOLDER
));
143 /* if no folder is given, use default folder */
145 folder
= getfolder (0);
146 maildir
= m_maildir (folder
);
148 /* check if folder exists */
149 if (stat (maildir
, &st
) == NOTOK
) {
151 adios (maildir
, "error on folder");
153 die("folder %s doesn't exist", maildir
);
154 if (!makedir (maildir
))
155 die("unable to create folder %s", maildir
);
158 if (chdir (maildir
) == NOTOK
)
159 adios (maildir
, "unable to change directory to");
161 /* ignore a few signals */
162 SIGNAL (SIGHUP
, SIG_IGN
);
163 SIGNAL (SIGINT
, SIG_IGN
);
164 SIGNAL (SIGQUIT
, SIG_IGN
);
165 SIGNAL (SIGTERM
, SIG_IGN
);
167 /* create a temporary file */
168 tmpfilenam
= m_mktemp (invo_name
, &fd
, NULL
);
169 if (tmpfilenam
== NULL
) {
170 die("unable to create temporary file in %s", get_temp_dir());
172 chmod (tmpfilenam
, m_gmprot());
174 /* copy the message from stdin into temp file */
175 cpydata (fileno (stdin
), fd
, "standard input", tmpfilenam
);
177 if (fstat (fd
, &st
) == NOTOK
) {
178 (void) m_unlink (tmpfilenam
);
179 adios (tmpfilenam
, "unable to fstat");
181 if (close (fd
) == NOTOK
)
182 adios (tmpfilenam
, "error closing");
184 /* don't add file if it is empty */
185 if (st
.st_size
== 0) {
186 (void) m_unlink (tmpfilenam
);
187 inform("empty file");
192 * read folder and create message structure
194 if (!(mp
= folder_read (folder
, 1)))
195 die("unable to read folder %s", folder
);
198 * Link message into folder, and possibly add
199 * to the Unseen-Sequence's.
201 if ((msgnum
= folder_addmsg (&mp
, tmpfilenam
, 0, unseensw
, 0, 0, NULL
)) == -1)
205 * Add the message to any extra sequences
206 * that have been specified.
209 /* The only reason that seqp was checked to be non-zero is in
210 case a -nosequence switch is added. */
211 for (seqp
= 0; seqp
< svector_size (seqs
); seqp
++) {
212 if (!seq_addmsg (mp
, svector_at (seqs
, seqp
), msgnum
, publicsw
,
219 seq_setunseen (mp
, 0); /* synchronize any Unseen-Sequence's */
220 seq_save (mp
); /* synchronize and save message sequences */
221 folder_free (mp
); /* free folder/message structure */
223 context_save (); /* save the global context file */
224 (void) m_unlink (tmpfilenam
); /* remove temporary file */
235 unlink_done(int status
)
237 if (tmpfilenam
&& *tmpfilenam
)
238 (void) m_unlink (tmpfilenam
);