]>
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);
57 if (nmh_init(argv
[0], 2)) { return 1; }
62 arguments
= getarguments (invo_name
, argc
, argv
, 1);
66 while ((cp
= *argp
++)) {
68 switch (smatch (++cp
, switches
)) {
70 ambigsw (cp
, switches
);
73 adios (NULL
, "-%s unknown", cp
);
76 snprintf (buf
, sizeof(buf
), "%s [+folder] [switches]",
78 print_help (buf
, switches
, 1);
81 print_version(invo_name
);
85 if (!(cp
= *argp
++) || *cp
== '-')
86 adios (NULL
, "missing argument name to %s", argp
[-2]);
88 svector_push_back (seqs
, cp
);
121 if (*cp
== '+' || *cp
== '@') {
123 adios (NULL
, "only one folder at a time!");
125 folder
= pluspath (cp
);
127 adios (NULL
, "usage: %s [+folder] [switches]", invo_name
);
131 if (!context_find ("path"))
132 free (path ("./", TFOLDER
));
134 /* if no folder is given, use default folder */
136 folder
= getfolder (0);
137 maildir
= m_maildir (folder
);
139 /* check if folder exists */
140 if (stat (maildir
, &st
) == NOTOK
) {
142 adios (maildir
, "error on folder");
144 adios (NULL
, "folder %s doesn't exist", maildir
);
145 if (!makedir (maildir
))
146 adios (NULL
, "unable to create folder %s", maildir
);
149 if (chdir (maildir
) == NOTOK
)
150 adios (maildir
, "unable to change directory to");
152 /* ignore a few signals */
153 SIGNAL (SIGHUP
, SIG_IGN
);
154 SIGNAL (SIGINT
, SIG_IGN
);
155 SIGNAL (SIGQUIT
, SIG_IGN
);
156 SIGNAL (SIGTERM
, SIG_IGN
);
158 /* create a temporary file */
159 tmpfilenam
= m_mktemp (invo_name
, &fd
, NULL
);
160 if (tmpfilenam
== NULL
) {
161 adios(NULL
, "unable to create temporary file in %s", get_temp_dir());
163 chmod (tmpfilenam
, m_gmprot());
165 /* copy the message from stdin into temp file */
166 cpydata (fileno (stdin
), fd
, "standard input", tmpfilenam
);
168 if (fstat (fd
, &st
) == NOTOK
) {
169 (void) m_unlink (tmpfilenam
);
170 adios (tmpfilenam
, "unable to fstat");
172 if (close (fd
) == NOTOK
)
173 adios (tmpfilenam
, "error closing");
175 /* don't add file if it is empty */
176 if (st
.st_size
== 0) {
177 (void) m_unlink (tmpfilenam
);
178 advise (NULL
, "empty file");
183 * read folder and create message structure
185 if (!(mp
= folder_read (folder
, 1)))
186 adios (NULL
, "unable to read folder %s", folder
);
189 * Link message into folder, and possibly add
190 * to the Unseen-Sequence's.
192 if ((msgnum
= folder_addmsg (&mp
, tmpfilenam
, 0, unseensw
, 0, 0, (char *)0)) == -1)
196 * Add the message to any extra sequences
197 * that have been specified.
200 /* The only reason that seqp was checked to be non-zero is in
201 case a -nosequence switch is added. */
202 for (seqp
= 0; seqp
< svector_size (seqs
); seqp
++) {
203 if (!seq_addmsg (mp
, svector_at (seqs
, seqp
), msgnum
, publicsw
,
210 seq_setunseen (mp
, 0); /* synchronize any Unseen-Sequence's */
211 seq_save (mp
); /* synchronize and save message sequences */
212 folder_free (mp
); /* free folder/message structure */
214 context_save (); /* save the global context file */
215 (void) m_unlink (tmpfilenam
); /* remove temporary file */
226 unlink_done(int status
)
228 if (tmpfilenam
&& *tmpfilenam
)
229 (void) m_unlink (tmpfilenam
);