]>
diplodocus.org Git - nmh/blob - uip/rcvstore.c
5875c21a1ccb58aff1421ceb607420227601841b
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
;
55 svector_t seqs
= svector_create (0);
62 setlocale(LC_ALL
, "");
64 invo_name
= r1bindex (argv
[0], '/');
66 /* read user profile/context */
70 arguments
= getarguments (invo_name
, argc
, argv
, 1);
74 while ((cp
= *argp
++)) {
76 switch (smatch (++cp
, switches
)) {
78 ambigsw (cp
, switches
);
81 adios (NULL
, "-%s unknown", cp
);
84 snprintf (buf
, sizeof(buf
), "%s [+folder] [switches]",
86 print_help (buf
, switches
, 1);
89 print_version(invo_name
);
93 if (!(cp
= *argp
++) || *cp
== '-')
94 adios (NULL
, "missing argument name to %s", argp
[-2]);
96 svector_push_back (seqs
, cp
);
129 if (*cp
== '+' || *cp
== '@') {
131 adios (NULL
, "only one folder at a time!");
133 folder
= pluspath (cp
);
135 adios (NULL
, "usage: %s [+folder] [switches]", invo_name
);
139 if (!context_find ("path"))
140 free (path ("./", TFOLDER
));
142 /* if no folder is given, use default folder */
144 folder
= getfolder (0);
145 maildir
= m_maildir (folder
);
147 /* check if folder exists */
148 if (stat (maildir
, &st
) == NOTOK
) {
150 adios (maildir
, "error on folder");
152 adios (NULL
, "folder %s doesn't exist", maildir
);
153 if (!makedir (maildir
))
154 adios (NULL
, "unable to create folder %s", maildir
);
157 if (chdir (maildir
) == NOTOK
)
158 adios (maildir
, "unable to change directory to");
160 /* ignore a few signals */
161 SIGNAL (SIGHUP
, SIG_IGN
);
162 SIGNAL (SIGINT
, SIG_IGN
);
163 SIGNAL (SIGQUIT
, SIG_IGN
);
164 SIGNAL (SIGTERM
, SIG_IGN
);
166 /* create a temporary file */
167 tmpfilenam
= m_mktemp (invo_name
, &fd
, NULL
);
168 if (tmpfilenam
== NULL
) {
169 adios ("rcvstore", "unable to create temporary file");
171 chmod (tmpfilenam
, m_gmprot());
173 /* copy the message from stdin into temp file */
174 cpydata (fileno (stdin
), fd
, "standard input", tmpfilenam
);
176 if (fstat (fd
, &st
) == NOTOK
) {
178 adios (tmpfilenam
, "unable to fstat");
180 if (close (fd
) == NOTOK
)
181 adios (tmpfilenam
, "error closing");
183 /* don't add file if it is empty */
184 if (st
.st_size
== 0) {
186 advise (NULL
, "empty file");
191 * read folder and create message structure
193 if (!(mp
= folder_read (folder
, 1)))
194 adios (NULL
, "unable to read folder %s", folder
);
197 * Link message into folder, and possibly add
198 * to the Unseen-Sequence's.
200 if ((msgnum
= folder_addmsg (&mp
, tmpfilenam
, 0, unseensw
, 0, 0, (char *)0)) == -1)
204 * Add the message to any extra sequences
205 * that have been specified.
208 /* The only reason that seqp was checked to be non-zero is in
209 case a -nosequence switch is added. */
210 for (seqp
= 0; seqp
< svector_size (seqs
); seqp
++) {
211 if (!seq_addmsg (mp
, svector_at (seqs
, seqp
), msgnum
, publicsw
,
218 seq_setunseen (mp
, 0); /* synchronize any Unseen-Sequence's */
219 seq_save (mp
); /* synchronize and save message sequences */
220 folder_free (mp
); /* free folder/message structure */
222 context_save (); /* save the global context file */
223 unlink (tmpfilenam
); /* remove temporary file */
234 unlink_done(int status
)
236 if (tmpfilenam
&& *tmpfilenam
)