]>
diplodocus.org Git - nmh/blob - uip/rcvstore.c
3159d1d24eaeb8ad7e326cc434e304b6357fceb1
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_addmsg.h"
10 #include "sbr/context_save.h"
11 #include "sbr/context_find.h"
12 #include "sbr/ambigsw.h"
14 #include "sbr/print_version.h"
15 #include "sbr/print_help.h"
16 #include "sbr/seq_add.h"
17 #include "sbr/error.h"
19 #include "h/signals.h"
23 #include "sbr/m_maildir.h"
24 #include "sbr/m_mktemp.h"
25 #include "sbr/makedir.h"
27 #define RCVSTORE_SWITCHES \
28 X("create", 0, CRETSW) \
29 X("nocreate", 0, NCRETSW) \
30 X("unseen", 0, UNSEENSW) \
31 X("nounseen", 0, NUNSEENSW) \
32 X("public", 0, PUBSW) \
33 X("nopublic", 0, NPUBSW) \
34 X("zero", 0, ZEROSW) \
35 X("nozero", 0, NZEROSW) \
36 X("sequence name", 0, SEQSW) \
37 X("version", 0, VERSIONSW) \
38 X("help", 0, HELPSW) \
40 #define X(sw, minchars, id) id,
41 DEFINE_SWITCH_ENUM(RCVSTORE
);
44 #define X(sw, minchars, id) { sw, minchars, id },
45 DEFINE_SWITCH_ARRAY(RCVSTORE
, switches
);
50 * name of temporary file to store incoming message
52 static char *tmpfilenam
= NULL
;
54 static void unlink_done(int) NORETURN
;
57 main (int argc
, char **argv
)
65 char *cp
, *maildir
, *folder
= NULL
, buf
[BUFSIZ
];
66 char **argp
, **arguments
;
67 svector_t seqs
= svector_create (0);
71 if (nmh_init(argv
[0], true, false)) { return 1; }
73 set_done(unlink_done
);
76 arguments
= getarguments (invo_name
, argc
, argv
, 1);
80 while ((cp
= *argp
++)) {
82 switch (smatch (++cp
, switches
)) {
84 ambigsw (cp
, switches
);
87 die("-%s unknown", cp
);
90 snprintf (buf
, sizeof(buf
), "%s [+folder] [switches]",
92 print_help (buf
, switches
, 1);
95 print_version(invo_name
);
99 if (!(cp
= *argp
++) || *cp
== '-')
100 die("missing argument name to %s", argp
[-2]);
102 svector_push_back (seqs
, cp
);
135 if (*cp
== '+' || *cp
== '@') {
137 die("only one folder at a time!");
138 folder
= pluspath (cp
);
140 die("usage: %s [+folder] [switches]", invo_name
);
144 if (!context_find ("path"))
145 free (path ("./", TFOLDER
));
147 /* if no folder is given, use default folder */
149 folder
= getfolder (0);
150 maildir
= m_maildir (folder
);
152 /* check if folder exists */
153 if (stat (maildir
, &st
) == NOTOK
) {
155 adios (maildir
, "error on folder");
157 die("folder %s doesn't exist", maildir
);
158 if (!makedir (maildir
))
159 die("unable to create folder %s", maildir
);
162 if (chdir (maildir
) == NOTOK
)
163 adios (maildir
, "unable to change directory to");
165 /* ignore a few signals */
166 SIGNAL (SIGHUP
, SIG_IGN
);
167 SIGNAL (SIGINT
, SIG_IGN
);
168 SIGNAL (SIGQUIT
, SIG_IGN
);
169 SIGNAL (SIGTERM
, SIG_IGN
);
171 /* create a temporary file */
172 tmpfilenam
= m_mktemp (invo_name
, &fd
, NULL
);
173 if (tmpfilenam
== NULL
) {
174 die("unable to create temporary file in %s", get_temp_dir());
176 chmod (tmpfilenam
, m_gmprot());
178 /* copy the message from stdin into temp file */
179 cpydata (fileno (stdin
), fd
, "standard input", tmpfilenam
);
181 if (fstat (fd
, &st
) == NOTOK
) {
182 (void) m_unlink (tmpfilenam
);
183 adios (tmpfilenam
, "unable to fstat");
185 if (close (fd
) == NOTOK
)
186 adios (tmpfilenam
, "error closing");
188 /* don't add file if it is empty */
189 if (st
.st_size
== 0) {
190 (void) m_unlink (tmpfilenam
);
191 inform("empty file");
196 * read folder and create message structure
198 if (!(mp
= folder_read (folder
, 1)))
199 die("unable to read folder %s", folder
);
202 * Link message into folder, and possibly add
203 * to the Unseen-Sequence's.
205 if ((msgnum
= folder_addmsg (&mp
, tmpfilenam
, 0, unseensw
, 0, 0, NULL
)) == -1)
209 * Add the message to any extra sequences
210 * that have been specified.
213 /* The only reason that seqp was checked to be non-zero is in
214 case a -nosequence switch is added. */
215 for (seqp
= 0; seqp
< svector_size (seqs
); seqp
++) {
216 if (!seq_addmsg (mp
, svector_at (seqs
, seqp
), msgnum
, publicsw
,
223 seq_setunseen (mp
, 0); /* synchronize any Unseen-Sequence's */
224 seq_save (mp
); /* synchronize and save message sequences */
225 folder_free (mp
); /* free folder/message structure */
227 context_save (); /* save the global context file */
228 (void) m_unlink (tmpfilenam
); /* remove temporary file */
239 unlink_done(int status
)
241 if (tmpfilenam
&& *tmpfilenam
)
242 (void) m_unlink (tmpfilenam
);