]>
diplodocus.org Git - nmh/blob - uip/rcvstore.c
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>
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
, *seqs
[NUMATTRS
+1];
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 /* check if too many sequences specified */
97 adios (NULL
, "too many sequences (more than %d) specified", NUMATTRS
);
130 if (*cp
== '+' || *cp
== '@') {
132 adios (NULL
, "only one folder at a time!");
134 folder
= pluspath (cp
);
136 adios (NULL
, "usage: %s [+folder] [switches]", invo_name
);
140 seqs
[seqp
] = NULL
; /* NULL terminate list of sequences */
142 if (!context_find ("path"))
143 free (path ("./", TFOLDER
));
145 /* if no folder is given, use default folder */
147 folder
= getfolder (0);
148 maildir
= m_maildir (folder
);
150 /* check if folder exists */
151 if (stat (maildir
, &st
) == NOTOK
) {
153 adios (maildir
, "error on folder");
155 adios (NULL
, "folder %s doesn't exist", maildir
);
156 if (!makedir (maildir
))
157 adios (NULL
, "unable to create folder %s", maildir
);
160 if (chdir (maildir
) == NOTOK
)
161 adios (maildir
, "unable to change directory to");
163 /* ignore a few signals */
164 SIGNAL (SIGHUP
, SIG_IGN
);
165 SIGNAL (SIGINT
, SIG_IGN
);
166 SIGNAL (SIGQUIT
, SIG_IGN
);
167 SIGNAL (SIGTERM
, SIG_IGN
);
169 /* create a temporary file */
170 tmpfilenam
= m_mktemp (invo_name
, &fd
, NULL
);
171 if (tmpfilenam
== NULL
) {
172 adios ("rcvstore", "unable to create temporary file");
174 chmod (tmpfilenam
, m_gmprot());
176 /* copy the message from stdin into temp file */
177 cpydata (fileno (stdin
), fd
, "standard input", tmpfilenam
);
179 if (fstat (fd
, &st
) == NOTOK
) {
181 adios (tmpfilenam
, "unable to fstat");
183 if (close (fd
) == NOTOK
)
184 adios (tmpfilenam
, "error closing");
186 /* don't add file if it is empty */
187 if (st
.st_size
== 0) {
189 advise (NULL
, "empty file");
194 * read folder and create message structure
196 if (!(mp
= folder_read (folder
)))
197 adios (NULL
, "unable to read folder %s", folder
);
200 * Link message into folder, and possibly add
201 * to the Unseen-Sequence's.
203 if ((msgnum
= folder_addmsg (&mp
, tmpfilenam
, 0, unseensw
, 0, 0, (char *)0)) == -1)
207 * Add the message to any extra sequences
208 * that have been specified.
210 for (seqp
= 0; seqs
[seqp
]; seqp
++) {
211 if (!seq_addmsg (mp
, seqs
[seqp
], msgnum
, publicsw
, zerosw
))
215 seq_setunseen (mp
, 0); /* synchronize any Unseen-Sequence's */
216 seq_save (mp
); /* synchronize and save message sequences */
217 folder_free (mp
); /* free folder/message structure */
219 context_save (); /* save the global context file */
220 unlink (tmpfilenam
); /* remove temporary file */
231 unlink_done(int status
)
233 if (tmpfilenam
&& *tmpfilenam
)