]>
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.
9 #include "sbr/folder_read.h"
10 #include "sbr/folder_free.h"
11 #include "sbr/folder_addmsg.h"
12 #include "sbr/context_save.h"
13 #include "sbr/context_find.h"
14 #include "sbr/ambigsw.h"
16 #include "sbr/print_version.h"
17 #include "sbr/print_help.h"
18 #include "sbr/seq_add.h"
19 #include "sbr/error.h"
21 #include "h/signals.h"
25 #include "sbr/m_maildir.h"
26 #include "sbr/m_mktemp.h"
27 #include "sbr/makedir.h"
29 #define RCVSTORE_SWITCHES \
30 X("create", 0, CRETSW) \
31 X("nocreate", 0, NCRETSW) \
32 X("unseen", 0, UNSEENSW) \
33 X("nounseen", 0, NUNSEENSW) \
34 X("public", 0, PUBSW) \
35 X("nopublic", 0, NPUBSW) \
36 X("zero", 0, ZEROSW) \
37 X("nozero", 0, NZEROSW) \
38 X("sequence name", 0, SEQSW) \
39 X("version", 0, VERSIONSW) \
40 X("help", 0, HELPSW) \
42 #define X(sw, minchars, id) id,
43 DEFINE_SWITCH_ENUM(RCVSTORE
);
46 #define X(sw, minchars, id) { sw, minchars, id },
47 DEFINE_SWITCH_ARRAY(RCVSTORE
, switches
);
52 * name of temporary file to store incoming message
54 static char *tmpfilenam
= NULL
;
56 static void unlink_done(int) NORETURN
;
59 main (int argc
, char **argv
)
67 char *cp
, *maildir
, *folder
= NULL
, buf
[BUFSIZ
];
68 char **argp
, **arguments
;
69 svector_t seqs
= svector_create (0);
73 if (nmh_init(argv
[0], true, false)) { return 1; }
75 set_done(unlink_done
);
78 arguments
= getarguments (invo_name
, argc
, argv
, 1);
82 while ((cp
= *argp
++)) {
84 switch (smatch (++cp
, switches
)) {
86 ambigsw (cp
, switches
);
89 die("-%s unknown", cp
);
92 snprintf (buf
, sizeof(buf
), "%s [+folder] [switches]",
94 print_help (buf
, switches
, 1);
97 print_version(invo_name
);
101 if (!(cp
= *argp
++) || *cp
== '-')
102 die("missing argument name to %s", argp
[-2]);
104 svector_push_back (seqs
, cp
);
137 if (*cp
== '+' || *cp
== '@') {
139 die("only one folder at a time!");
140 folder
= pluspath (cp
);
142 die("usage: %s [+folder] [switches]", invo_name
);
146 if (!context_find ("path"))
147 free (path ("./", TFOLDER
));
149 /* if no folder is given, use default folder */
151 folder
= getfolder (0);
152 maildir
= m_maildir (folder
);
154 /* check if folder exists */
155 if (stat (maildir
, &st
) == NOTOK
) {
157 adios (maildir
, "error on folder");
159 die("folder %s doesn't exist", maildir
);
160 if (!makedir (maildir
))
161 die("unable to create folder %s", maildir
);
164 if (chdir (maildir
) == NOTOK
)
165 adios (maildir
, "unable to change directory to");
167 /* ignore a few signals */
168 SIGNAL (SIGHUP
, SIG_IGN
);
169 SIGNAL (SIGINT
, SIG_IGN
);
170 SIGNAL (SIGQUIT
, SIG_IGN
);
171 SIGNAL (SIGTERM
, SIG_IGN
);
173 /* create a temporary file */
174 tmpfilenam
= m_mktemp (invo_name
, &fd
, NULL
);
175 if (tmpfilenam
== NULL
) {
176 die("unable to create temporary file in %s", get_temp_dir());
178 chmod (tmpfilenam
, m_gmprot());
180 /* copy the message from stdin into temp file */
181 cpydata (fileno (stdin
), fd
, "standard input", tmpfilenam
);
183 if (fstat (fd
, &st
) == NOTOK
) {
184 (void) m_unlink (tmpfilenam
);
185 adios (tmpfilenam
, "unable to fstat");
187 if (close (fd
) == NOTOK
)
188 adios (tmpfilenam
, "error closing");
190 /* don't add file if it is empty */
191 if (st
.st_size
== 0) {
192 (void) m_unlink (tmpfilenam
);
193 inform("empty file");
198 * read folder and create message structure
200 if (!(mp
= folder_read (folder
, 1)))
201 die("unable to read folder %s", folder
);
204 * Link message into folder, and possibly add
205 * to the Unseen-Sequence's.
207 if ((msgnum
= folder_addmsg (&mp
, tmpfilenam
, 0, unseensw
, 0, 0, NULL
)) == -1)
211 * Add the message to any extra sequences
212 * that have been specified.
215 /* The only reason that seqp was checked to be non-zero is in
216 case a -nosequence switch is added. */
217 for (seqp
= 0; seqp
< svector_size (seqs
); seqp
++) {
218 if (!seq_addmsg (mp
, svector_at (seqs
, seqp
), msgnum
, publicsw
,
225 seq_setunseen (mp
, 0); /* synchronize any Unseen-Sequence's */
226 seq_save (mp
); /* synchronize and save message sequences */
227 folder_free (mp
); /* free folder/message structure */
229 context_save (); /* save the global context file */
230 (void) m_unlink (tmpfilenam
); /* remove temporary file */
241 unlink_done(int status
)
243 if (tmpfilenam
&& *tmpfilenam
)
244 (void) m_unlink (tmpfilenam
);