]>
diplodocus.org Git - nmh/blob - uip/rcvstore.c
12d1ab2deefd908f367dffb34862d4c8a168e285
3 * rcvstore.c -- asynchronously add mail to a folder
5 * This code is Copyright (c) 2002, by the authors of nmh. See the
6 * COPYRIGHT file in the root directory of the nmh distribution for
7 * complete copyright information.
12 #include <h/signals.h>
16 #define RCVSTORE_SWITCHES \
17 X("create", 0, CRETSW) \
18 X("nocreate", 0, NCRETSW) \
19 X("unseen", 0, UNSEENSW) \
20 X("nounseen", 0, NUNSEENSW) \
21 X("public", 0, PUBSW) \
22 X("nopublic", 0, NPUBSW) \
23 X("zero", 0, ZEROSW) \
24 X("nozero", 0, NZEROSW) \
25 X("sequence name", 0, SEQSW) \
26 X("version", 0, VERSIONSW) \
27 X("help", 0, HELPSW) \
29 #define X(sw, minchars, id) id,
30 DEFINE_SWITCH_ENUM(RCVSTORE
);
33 #define X(sw, minchars, id) { sw, minchars, id },
34 DEFINE_SWITCH_ARRAY(RCVSTORE
, switches
);
39 * name of temporary file to store incoming message
41 static char *tmpfilenam
= NULL
;
43 static void unlink_done(int) NORETURN
;
46 main (int argc
, char **argv
)
48 int publicsw
= -1, zerosw
= 0;
49 int create
= 1, unseensw
= 1;
52 char *cp
, *maildir
, *folder
= NULL
, buf
[BUFSIZ
];
53 char **argp
, **arguments
;
54 svector_t seqs
= svector_create (0);
61 setlocale(LC_ALL
, "");
63 invo_name
= r1bindex (argv
[0], '/');
65 /* read user profile/context */
69 arguments
= getarguments (invo_name
, argc
, argv
, 1);
73 while ((cp
= *argp
++)) {
75 switch (smatch (++cp
, switches
)) {
77 ambigsw (cp
, switches
);
80 adios (NULL
, "-%s unknown", cp
);
83 snprintf (buf
, sizeof(buf
), "%s [+folder] [switches]",
85 print_help (buf
, switches
, 1);
88 print_version(invo_name
);
92 if (!(cp
= *argp
++) || *cp
== '-')
93 adios (NULL
, "missing argument name to %s", argp
[-2]);
95 svector_push_back (seqs
, cp
);
128 if (*cp
== '+' || *cp
== '@') {
130 adios (NULL
, "only one folder at a time!");
132 folder
= pluspath (cp
);
134 adios (NULL
, "usage: %s [+folder] [switches]", invo_name
);
138 if (!context_find ("path"))
139 free (path ("./", TFOLDER
));
141 /* if no folder is given, use default folder */
143 folder
= getfolder (0);
144 maildir
= m_maildir (folder
);
146 /* check if folder exists */
147 if (stat (maildir
, &st
) == NOTOK
) {
149 adios (maildir
, "error on folder");
151 adios (NULL
, "folder %s doesn't exist", maildir
);
152 if (!makedir (maildir
))
153 adios (NULL
, "unable to create folder %s", maildir
);
156 if (chdir (maildir
) == NOTOK
)
157 adios (maildir
, "unable to change directory to");
159 /* ignore a few signals */
160 SIGNAL (SIGHUP
, SIG_IGN
);
161 SIGNAL (SIGINT
, SIG_IGN
);
162 SIGNAL (SIGQUIT
, SIG_IGN
);
163 SIGNAL (SIGTERM
, SIG_IGN
);
165 /* create a temporary file */
166 tmpfilenam
= m_mktemp (invo_name
, &fd
, NULL
);
167 if (tmpfilenam
== NULL
) {
168 adios ("rcvstore", "unable to create temporary file");
170 chmod (tmpfilenam
, m_gmprot());
172 /* copy the message from stdin into temp file */
173 cpydata (fileno (stdin
), fd
, "standard input", tmpfilenam
);
175 if (fstat (fd
, &st
) == NOTOK
) {
177 adios (tmpfilenam
, "unable to fstat");
179 if (close (fd
) == NOTOK
)
180 adios (tmpfilenam
, "error closing");
182 /* don't add file if it is empty */
183 if (st
.st_size
== 0) {
185 advise (NULL
, "empty file");
190 * read folder and create message structure
192 if (!(mp
= folder_read (folder
, 1)))
193 adios (NULL
, "unable to read folder %s", folder
);
196 * Link message into folder, and possibly add
197 * to the Unseen-Sequence's.
199 if ((msgnum
= folder_addmsg (&mp
, tmpfilenam
, 0, unseensw
, 0, 0, (char *)0)) == -1)
203 * Add the message to any extra sequences
204 * that have been specified.
207 /* The only reason that seqp was checked to be non-zero is in
208 case a -nosequence switch is added. */
209 for (seqp
= 0; seqp
< svector_size (seqs
); seqp
++) {
210 if (!seq_addmsg (mp
, svector_at (seqs
, seqp
), msgnum
, publicsw
,
217 seq_setunseen (mp
, 0); /* synchronize any Unseen-Sequence's */
218 seq_save (mp
); /* synchronize and save message sequences */
219 folder_free (mp
); /* free folder/message structure */
221 context_save (); /* save the global context file */
222 unlink (tmpfilenam
); /* remove temporary file */
233 unlink_done(int status
)
235 if (tmpfilenam
&& *tmpfilenam
)