]>
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!");
127 folder
= pluspath (cp
);
129 adios (NULL
, "usage: %s [+folder] [switches]", invo_name
);
133 if (!context_find ("path"))
134 free (path ("./", TFOLDER
));
136 /* if no folder is given, use default folder */
138 folder
= getfolder (0);
139 maildir
= m_maildir (folder
);
141 /* check if folder exists */
142 if (stat (maildir
, &st
) == NOTOK
) {
144 adios (maildir
, "error on folder");
146 adios (NULL
, "folder %s doesn't exist", maildir
);
147 if (!makedir (maildir
))
148 adios (NULL
, "unable to create folder %s", maildir
);
151 if (chdir (maildir
) == NOTOK
)
152 adios (maildir
, "unable to change directory to");
154 /* ignore a few signals */
155 SIGNAL (SIGHUP
, SIG_IGN
);
156 SIGNAL (SIGINT
, SIG_IGN
);
157 SIGNAL (SIGQUIT
, SIG_IGN
);
158 SIGNAL (SIGTERM
, SIG_IGN
);
160 /* create a temporary file */
161 tmpfilenam
= m_mktemp (invo_name
, &fd
, NULL
);
162 if (tmpfilenam
== NULL
) {
163 adios(NULL
, "unable to create temporary file in %s", get_temp_dir());
165 chmod (tmpfilenam
, m_gmprot());
167 /* copy the message from stdin into temp file */
168 cpydata (fileno (stdin
), fd
, "standard input", tmpfilenam
);
170 if (fstat (fd
, &st
) == NOTOK
) {
171 (void) m_unlink (tmpfilenam
);
172 adios (tmpfilenam
, "unable to fstat");
174 if (close (fd
) == NOTOK
)
175 adios (tmpfilenam
, "error closing");
177 /* don't add file if it is empty */
178 if (st
.st_size
== 0) {
179 (void) m_unlink (tmpfilenam
);
180 inform("empty file");
185 * read folder and create message structure
187 if (!(mp
= folder_read (folder
, 1)))
188 adios (NULL
, "unable to read folder %s", folder
);
191 * Link message into folder, and possibly add
192 * to the Unseen-Sequence's.
194 if ((msgnum
= folder_addmsg (&mp
, tmpfilenam
, 0, unseensw
, 0, 0, NULL
)) == -1)
198 * Add the message to any extra sequences
199 * that have been specified.
202 /* The only reason that seqp was checked to be non-zero is in
203 case a -nosequence switch is added. */
204 for (seqp
= 0; seqp
< svector_size (seqs
); seqp
++) {
205 if (!seq_addmsg (mp
, svector_at (seqs
, seqp
), msgnum
, publicsw
,
212 seq_setunseen (mp
, 0); /* synchronize any Unseen-Sequence's */
213 seq_save (mp
); /* synchronize and save message sequences */
214 folder_free (mp
); /* free folder/message structure */
216 context_save (); /* save the global context file */
217 (void) m_unlink (tmpfilenam
); /* remove temporary file */
228 unlink_done(int status
)
230 if (tmpfilenam
&& *tmpfilenam
)
231 (void) m_unlink (tmpfilenam
);