]>
diplodocus.org Git - nmh/blob - uip/rcvstore.c
1 /* rcvstore.c -- asynchronously add mail to a folder
3 * This code is Copyright (c) 2002, by the authors of nmh. See the
4 * COPYRIGHT file in the root directory of the nmh distribution for
5 * complete copyright information.
10 #include <h/signals.h>
13 #define RCVSTORE_SWITCHES \
14 X("create", 0, CRETSW) \
15 X("nocreate", 0, NCRETSW) \
16 X("unseen", 0, UNSEENSW) \
17 X("nounseen", 0, NUNSEENSW) \
18 X("public", 0, PUBSW) \
19 X("nopublic", 0, NPUBSW) \
20 X("zero", 0, ZEROSW) \
21 X("nozero", 0, NZEROSW) \
22 X("sequence name", 0, SEQSW) \
23 X("version", 0, VERSIONSW) \
24 X("help", 0, HELPSW) \
26 #define X(sw, minchars, id) id,
27 DEFINE_SWITCH_ENUM(RCVSTORE
);
30 #define X(sw, minchars, id) { sw, minchars, id },
31 DEFINE_SWITCH_ARRAY(RCVSTORE
, switches
);
36 * name of temporary file to store incoming message
38 static char *tmpfilenam
= NULL
;
40 static void unlink_done(int) NORETURN
;
43 main (int argc
, char **argv
)
45 int publicsw
= -1, zerosw
= 0;
46 int create
= 1, unseensw
= 1;
49 char *cp
, *maildir
, *folder
= NULL
, buf
[BUFSIZ
];
50 char **argp
, **arguments
;
51 svector_t seqs
= svector_create (0);
55 if (nmh_init(argv
[0], 2)) { return 1; }
60 arguments
= getarguments (invo_name
, argc
, argv
, 1);
64 while ((cp
= *argp
++)) {
66 switch (smatch (++cp
, switches
)) {
68 ambigsw (cp
, switches
);
71 adios (NULL
, "-%s unknown", cp
);
74 snprintf (buf
, sizeof(buf
), "%s [+folder] [switches]",
76 print_help (buf
, switches
, 1);
79 print_version(invo_name
);
83 if (!(cp
= *argp
++) || *cp
== '-')
84 adios (NULL
, "missing argument name to %s", argp
[-2]);
86 svector_push_back (seqs
, cp
);
119 if (*cp
== '+' || *cp
== '@') {
121 adios (NULL
, "only one folder at a time!");
123 folder
= pluspath (cp
);
125 adios (NULL
, "usage: %s [+folder] [switches]", invo_name
);
129 if (!context_find ("path"))
130 free (path ("./", TFOLDER
));
132 /* if no folder is given, use default folder */
134 folder
= getfolder (0);
135 maildir
= m_maildir (folder
);
137 /* check if folder exists */
138 if (stat (maildir
, &st
) == NOTOK
) {
140 adios (maildir
, "error on folder");
142 adios (NULL
, "folder %s doesn't exist", maildir
);
143 if (!makedir (maildir
))
144 adios (NULL
, "unable to create folder %s", maildir
);
147 if (chdir (maildir
) == NOTOK
)
148 adios (maildir
, "unable to change directory to");
150 /* ignore a few signals */
151 SIGNAL (SIGHUP
, SIG_IGN
);
152 SIGNAL (SIGINT
, SIG_IGN
);
153 SIGNAL (SIGQUIT
, SIG_IGN
);
154 SIGNAL (SIGTERM
, SIG_IGN
);
156 /* create a temporary file */
157 tmpfilenam
= m_mktemp (invo_name
, &fd
, NULL
);
158 if (tmpfilenam
== NULL
) {
159 adios(NULL
, "unable to create temporary file in %s", get_temp_dir());
161 chmod (tmpfilenam
, m_gmprot());
163 /* copy the message from stdin into temp file */
164 cpydata (fileno (stdin
), fd
, "standard input", tmpfilenam
);
166 if (fstat (fd
, &st
) == NOTOK
) {
167 (void) m_unlink (tmpfilenam
);
168 adios (tmpfilenam
, "unable to fstat");
170 if (close (fd
) == NOTOK
)
171 adios (tmpfilenam
, "error closing");
173 /* don't add file if it is empty */
174 if (st
.st_size
== 0) {
175 (void) m_unlink (tmpfilenam
);
176 inform("empty file");
181 * read folder and create message structure
183 if (!(mp
= folder_read (folder
, 1)))
184 adios (NULL
, "unable to read folder %s", folder
);
187 * Link message into folder, and possibly add
188 * to the Unseen-Sequence's.
190 if ((msgnum
= folder_addmsg (&mp
, tmpfilenam
, 0, unseensw
, 0, 0, NULL
)) == -1)
194 * Add the message to any extra sequences
195 * that have been specified.
198 /* The only reason that seqp was checked to be non-zero is in
199 case a -nosequence switch is added. */
200 for (seqp
= 0; seqp
< svector_size (seqs
); seqp
++) {
201 if (!seq_addmsg (mp
, svector_at (seqs
, seqp
), msgnum
, publicsw
,
208 seq_setunseen (mp
, 0); /* synchronize any Unseen-Sequence's */
209 seq_save (mp
); /* synchronize and save message sequences */
210 folder_free (mp
); /* free folder/message structure */
212 context_save (); /* save the global context file */
213 (void) m_unlink (tmpfilenam
); /* remove temporary file */
224 unlink_done(int status
)
226 if (tmpfilenam
&& *tmpfilenam
)
227 (void) m_unlink (tmpfilenam
);