]>
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 <h/signals.h>
13 #include "sbr/m_maildir.h"
14 #include "sbr/m_mktemp.h"
15 #include "sbr/makedir.h"
17 #define RCVSTORE_SWITCHES \
18 X("create", 0, CRETSW) \
19 X("nocreate", 0, NCRETSW) \
20 X("unseen", 0, UNSEENSW) \
21 X("nounseen", 0, NUNSEENSW) \
22 X("public", 0, PUBSW) \
23 X("nopublic", 0, NPUBSW) \
24 X("zero", 0, ZEROSW) \
25 X("nozero", 0, NZEROSW) \
26 X("sequence name", 0, SEQSW) \
27 X("version", 0, VERSIONSW) \
28 X("help", 0, HELPSW) \
30 #define X(sw, minchars, id) id,
31 DEFINE_SWITCH_ENUM(RCVSTORE
);
34 #define X(sw, minchars, id) { sw, minchars, id },
35 DEFINE_SWITCH_ARRAY(RCVSTORE
, switches
);
40 * name of temporary file to store incoming message
42 static char *tmpfilenam
= NULL
;
44 static void unlink_done(int) NORETURN
;
47 main (int argc
, char **argv
)
49 int publicsw
= -1, zerosw
= 0;
50 int create
= 1, unseensw
= 1;
53 char *cp
, *maildir
, *folder
= NULL
, buf
[BUFSIZ
];
54 char **argp
, **arguments
;
55 svector_t seqs
= svector_create (0);
59 if (nmh_init(argv
[0], 2)) { return 1; }
64 arguments
= getarguments (invo_name
, argc
, argv
, 1);
68 while ((cp
= *argp
++)) {
70 switch (smatch (++cp
, switches
)) {
72 ambigsw (cp
, switches
);
75 adios (NULL
, "-%s unknown", cp
);
78 snprintf (buf
, sizeof(buf
), "%s [+folder] [switches]",
80 print_help (buf
, switches
, 1);
83 print_version(invo_name
);
87 if (!(cp
= *argp
++) || *cp
== '-')
88 adios (NULL
, "missing argument name to %s", argp
[-2]);
90 svector_push_back (seqs
, cp
);
123 if (*cp
== '+' || *cp
== '@') {
125 adios (NULL
, "only one folder at a time!");
126 folder
= pluspath (cp
);
128 adios (NULL
, "usage: %s [+folder] [switches]", invo_name
);
132 if (!context_find ("path"))
133 free (path ("./", TFOLDER
));
135 /* if no folder is given, use default folder */
137 folder
= getfolder (0);
138 maildir
= m_maildir (folder
);
140 /* check if folder exists */
141 if (stat (maildir
, &st
) == NOTOK
) {
143 adios (maildir
, "error on folder");
145 adios (NULL
, "folder %s doesn't exist", maildir
);
146 if (!makedir (maildir
))
147 adios (NULL
, "unable to create folder %s", maildir
);
150 if (chdir (maildir
) == NOTOK
)
151 adios (maildir
, "unable to change directory to");
153 /* ignore a few signals */
154 SIGNAL (SIGHUP
, SIG_IGN
);
155 SIGNAL (SIGINT
, SIG_IGN
);
156 SIGNAL (SIGQUIT
, SIG_IGN
);
157 SIGNAL (SIGTERM
, SIG_IGN
);
159 /* create a temporary file */
160 tmpfilenam
= m_mktemp (invo_name
, &fd
, NULL
);
161 if (tmpfilenam
== NULL
) {
162 adios(NULL
, "unable to create temporary file in %s", get_temp_dir());
164 chmod (tmpfilenam
, m_gmprot());
166 /* copy the message from stdin into temp file */
167 cpydata (fileno (stdin
), fd
, "standard input", tmpfilenam
);
169 if (fstat (fd
, &st
) == NOTOK
) {
170 (void) m_unlink (tmpfilenam
);
171 adios (tmpfilenam
, "unable to fstat");
173 if (close (fd
) == NOTOK
)
174 adios (tmpfilenam
, "error closing");
176 /* don't add file if it is empty */
177 if (st
.st_size
== 0) {
178 (void) m_unlink (tmpfilenam
);
179 inform("empty file");
184 * read folder and create message structure
186 if (!(mp
= folder_read (folder
, 1)))
187 adios (NULL
, "unable to read folder %s", folder
);
190 * Link message into folder, and possibly add
191 * to the Unseen-Sequence's.
193 if ((msgnum
= folder_addmsg (&mp
, tmpfilenam
, 0, unseensw
, 0, 0, NULL
)) == -1)
197 * Add the message to any extra sequences
198 * that have been specified.
201 /* The only reason that seqp was checked to be non-zero is in
202 case a -nosequence switch is added. */
203 for (seqp
= 0; seqp
< svector_size (seqs
); seqp
++) {
204 if (!seq_addmsg (mp
, svector_at (seqs
, seqp
), msgnum
, publicsw
,
211 seq_setunseen (mp
, 0); /* synchronize any Unseen-Sequence's */
212 seq_save (mp
); /* synchronize and save message sequences */
213 folder_free (mp
); /* free folder/message structure */
215 context_save (); /* save the global context file */
216 (void) m_unlink (tmpfilenam
); /* remove temporary file */
227 unlink_done(int status
)
229 if (tmpfilenam
&& *tmpfilenam
)
230 (void) m_unlink (tmpfilenam
);