]>
diplodocus.org Git - nmh/blob - uip/rcvstore.c
073c35a541f561d608d3dc94a6e5d3fd0be67dbc
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/ambigsw.h"
11 #include "sbr/print_version.h"
12 #include "sbr/print_help.h"
13 #include "sbr/seq_add.h"
14 #include "sbr/error.h"
16 #include "h/signals.h"
20 #include "sbr/m_maildir.h"
21 #include "sbr/m_mktemp.h"
22 #include "sbr/makedir.h"
24 #define RCVSTORE_SWITCHES \
25 X("create", 0, CRETSW) \
26 X("nocreate", 0, NCRETSW) \
27 X("unseen", 0, UNSEENSW) \
28 X("nounseen", 0, NUNSEENSW) \
29 X("public", 0, PUBSW) \
30 X("nopublic", 0, NPUBSW) \
31 X("zero", 0, ZEROSW) \
32 X("nozero", 0, NZEROSW) \
33 X("sequence name", 0, SEQSW) \
34 X("version", 0, VERSIONSW) \
35 X("help", 0, HELPSW) \
37 #define X(sw, minchars, id) id,
38 DEFINE_SWITCH_ENUM(RCVSTORE
);
41 #define X(sw, minchars, id) { sw, minchars, id },
42 DEFINE_SWITCH_ARRAY(RCVSTORE
, switches
);
47 * name of temporary file to store incoming message
49 static char *tmpfilenam
= NULL
;
51 static void unlink_done(int) NORETURN
;
54 main (int argc
, char **argv
)
62 char *cp
, *maildir
, *folder
= NULL
, buf
[BUFSIZ
];
63 char **argp
, **arguments
;
64 svector_t seqs
= svector_create (0);
68 if (nmh_init(argv
[0], true, false)) { return 1; }
70 set_done(unlink_done
);
73 arguments
= getarguments (invo_name
, argc
, argv
, 1);
77 while ((cp
= *argp
++)) {
79 switch (smatch (++cp
, switches
)) {
81 ambigsw (cp
, switches
);
84 die("-%s unknown", cp
);
87 snprintf (buf
, sizeof(buf
), "%s [+folder] [switches]",
89 print_help (buf
, switches
, 1);
92 print_version(invo_name
);
96 if (!(cp
= *argp
++) || *cp
== '-')
97 die("missing argument name to %s", argp
[-2]);
99 svector_push_back (seqs
, cp
);
132 if (*cp
== '+' || *cp
== '@') {
134 die("only one folder at a time!");
135 folder
= pluspath (cp
);
137 die("usage: %s [+folder] [switches]", invo_name
);
141 if (!context_find ("path"))
142 free (path ("./", TFOLDER
));
144 /* if no folder is given, use default folder */
146 folder
= getfolder (0);
147 maildir
= m_maildir (folder
);
149 /* check if folder exists */
150 if (stat (maildir
, &st
) == NOTOK
) {
152 adios (maildir
, "error on folder");
154 die("folder %s doesn't exist", maildir
);
155 if (!makedir (maildir
))
156 die("unable to create folder %s", maildir
);
159 if (chdir (maildir
) == NOTOK
)
160 adios (maildir
, "unable to change directory to");
162 /* ignore a few signals */
163 SIGNAL (SIGHUP
, SIG_IGN
);
164 SIGNAL (SIGINT
, SIG_IGN
);
165 SIGNAL (SIGQUIT
, SIG_IGN
);
166 SIGNAL (SIGTERM
, SIG_IGN
);
168 /* create a temporary file */
169 tmpfilenam
= m_mktemp (invo_name
, &fd
, NULL
);
170 if (tmpfilenam
== NULL
) {
171 die("unable to create temporary file in %s", get_temp_dir());
173 chmod (tmpfilenam
, m_gmprot());
175 /* copy the message from stdin into temp file */
176 cpydata (fileno (stdin
), fd
, "standard input", tmpfilenam
);
178 if (fstat (fd
, &st
) == NOTOK
) {
179 (void) m_unlink (tmpfilenam
);
180 adios (tmpfilenam
, "unable to fstat");
182 if (close (fd
) == NOTOK
)
183 adios (tmpfilenam
, "error closing");
185 /* don't add file if it is empty */
186 if (st
.st_size
== 0) {
187 (void) m_unlink (tmpfilenam
);
188 inform("empty file");
193 * read folder and create message structure
195 if (!(mp
= folder_read (folder
, 1)))
196 die("unable to read folder %s", folder
);
199 * Link message into folder, and possibly add
200 * to the Unseen-Sequence's.
202 if ((msgnum
= folder_addmsg (&mp
, tmpfilenam
, 0, unseensw
, 0, 0, NULL
)) == -1)
206 * Add the message to any extra sequences
207 * that have been specified.
210 /* The only reason that seqp was checked to be non-zero is in
211 case a -nosequence switch is added. */
212 for (seqp
= 0; seqp
< svector_size (seqs
); seqp
++) {
213 if (!seq_addmsg (mp
, svector_at (seqs
, seqp
), msgnum
, publicsw
,
220 seq_setunseen (mp
, 0); /* synchronize any Unseen-Sequence's */
221 seq_save (mp
); /* synchronize and save message sequences */
222 folder_free (mp
); /* free folder/message structure */
224 context_save (); /* save the global context file */
225 (void) m_unlink (tmpfilenam
); /* remove temporary file */
236 unlink_done(int status
)
238 if (tmpfilenam
&& *tmpfilenam
)
239 (void) m_unlink (tmpfilenam
);