]>
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>
15 #define RCVSTORE_SWITCHES \
16 X("create", 0, CRETSW) \
17 X("nocreate", 0, NCRETSW) \
18 X("unseen", 0, UNSEENSW) \
19 X("nounseen", 0, NUNSEENSW) \
20 X("public", 0, PUBSW) \
21 X("nopublic", 0, NPUBSW) \
22 X("zero", 0, ZEROSW) \
23 X("nozero", 0, NZEROSW) \
24 X("sequence name", 0, SEQSW) \
25 X("version", 0, VERSIONSW) \
26 X("help", 0, HELPSW) \
28 #define X(sw, minchars, id) id,
29 DEFINE_SWITCH_ENUM(RCVSTORE
);
32 #define X(sw, minchars, id) { sw, minchars, id },
33 DEFINE_SWITCH_ARRAY(RCVSTORE
, switches
);
38 * name of temporary file to store incoming message
40 static char *tmpfilenam
= NULL
;
42 static void unlink_done(int) NORETURN
;
45 main (int argc
, char **argv
)
47 int publicsw
= -1, zerosw
= 0;
48 int create
= 1, unseensw
= 1;
51 char *cp
, *maildir
, *folder
= NULL
, buf
[BUFSIZ
];
52 char **argp
, **arguments
;
53 svector_t seqs
= svector_create (0);
60 setlocale(LC_ALL
, "");
62 invo_name
= r1bindex (argv
[0], '/');
64 /* read user profile/context */
68 arguments
= getarguments (invo_name
, argc
, argv
, 1);
72 while ((cp
= *argp
++)) {
74 switch (smatch (++cp
, switches
)) {
76 ambigsw (cp
, switches
);
79 adios (NULL
, "-%s unknown", cp
);
82 snprintf (buf
, sizeof(buf
), "%s [+folder] [switches]",
84 print_help (buf
, switches
, 1);
87 print_version(invo_name
);
91 if (!(cp
= *argp
++) || *cp
== '-')
92 adios (NULL
, "missing argument name to %s", argp
[-2]);
94 svector_push_back (seqs
, cp
);
127 if (*cp
== '+' || *cp
== '@') {
129 adios (NULL
, "only one folder at a time!");
131 folder
= pluspath (cp
);
133 adios (NULL
, "usage: %s [+folder] [switches]", invo_name
);
137 if (!context_find ("path"))
138 free (path ("./", TFOLDER
));
140 /* if no folder is given, use default folder */
142 folder
= getfolder (0);
143 maildir
= m_maildir (folder
);
145 /* check if folder exists */
146 if (stat (maildir
, &st
) == NOTOK
) {
148 adios (maildir
, "error on folder");
150 adios (NULL
, "folder %s doesn't exist", maildir
);
151 if (!makedir (maildir
))
152 adios (NULL
, "unable to create folder %s", maildir
);
155 if (chdir (maildir
) == NOTOK
)
156 adios (maildir
, "unable to change directory to");
158 /* ignore a few signals */
159 SIGNAL (SIGHUP
, SIG_IGN
);
160 SIGNAL (SIGINT
, SIG_IGN
);
161 SIGNAL (SIGQUIT
, SIG_IGN
);
162 SIGNAL (SIGTERM
, SIG_IGN
);
164 /* create a temporary file */
165 tmpfilenam
= m_mktemp (invo_name
, &fd
, NULL
);
166 if (tmpfilenam
== NULL
) {
167 adios ("rcvstore", "unable to create temporary file");
169 chmod (tmpfilenam
, m_gmprot());
171 /* copy the message from stdin into temp file */
172 cpydata (fileno (stdin
), fd
, "standard input", tmpfilenam
);
174 if (fstat (fd
, &st
) == NOTOK
) {
176 adios (tmpfilenam
, "unable to fstat");
178 if (close (fd
) == NOTOK
)
179 adios (tmpfilenam
, "error closing");
181 /* don't add file if it is empty */
182 if (st
.st_size
== 0) {
184 advise (NULL
, "empty file");
189 * read folder and create message structure
191 if (!(mp
= folder_read (folder
, 1)))
192 adios (NULL
, "unable to read folder %s", folder
);
195 * Link message into folder, and possibly add
196 * to the Unseen-Sequence's.
198 if ((msgnum
= folder_addmsg (&mp
, tmpfilenam
, 0, unseensw
, 0, 0, (char *)0)) == -1)
202 * Add the message to any extra sequences
203 * that have been specified.
206 /* The only reason that seqp was checked to be non-zero is in
207 case a -nosequence switch is added. */
208 for (seqp
= 0; seqp
< svector_size (seqs
); seqp
++) {
209 if (!seq_addmsg (mp
, svector_at (seqs
, seqp
), msgnum
, publicsw
,
216 seq_setunseen (mp
, 0); /* synchronize any Unseen-Sequence's */
217 seq_save (mp
); /* synchronize and save message sequences */
218 folder_free (mp
); /* free folder/message structure */
220 context_save (); /* save the global context file */
221 unlink (tmpfilenam
); /* remove temporary file */
232 unlink_done(int status
)
234 if (tmpfilenam
&& *tmpfilenam
)