]>
diplodocus.org Git - nmh/blob - uip/rcvstore.c
65fe41d3403938032fe3f065e679e8188a2e30d8
2 * rcvstore.c -- asynchronously add mail to a folder
4 * This code is Copyright (c) 2002, by the authors of nmh. See the
5 * COPYRIGHT file in the root directory of the nmh distribution for
6 * complete copyright information.
11 #include <h/signals.h>
14 #define RCVSTORE_SWITCHES \
15 X("create", 0, CRETSW) \
16 X("nocreate", 0, NCRETSW) \
17 X("unseen", 0, UNSEENSW) \
18 X("nounseen", 0, NUNSEENSW) \
19 X("public", 0, PUBSW) \
20 X("nopublic", 0, NPUBSW) \
21 X("zero", 0, ZEROSW) \
22 X("nozero", 0, NZEROSW) \
23 X("sequence name", 0, SEQSW) \
24 X("version", 0, VERSIONSW) \
25 X("help", 0, HELPSW) \
27 #define X(sw, minchars, id) id,
28 DEFINE_SWITCH_ENUM(RCVSTORE
);
31 #define X(sw, minchars, id) { sw, minchars, id },
32 DEFINE_SWITCH_ARRAY(RCVSTORE
, switches
);
37 * name of temporary file to store incoming message
39 static char *tmpfilenam
= NULL
;
41 static void unlink_done(int) NORETURN
;
44 main (int argc
, char **argv
)
46 int publicsw
= -1, zerosw
= 0;
47 int create
= 1, unseensw
= 1;
50 char *cp
, *maildir
, *folder
= NULL
, buf
[BUFSIZ
];
51 char **argp
, **arguments
;
52 svector_t seqs
= svector_create (0);
56 if (nmh_init(argv
[0], 2)) { return 1; }
61 arguments
= getarguments (invo_name
, argc
, argv
, 1);
65 while ((cp
= *argp
++)) {
67 switch (smatch (++cp
, switches
)) {
69 ambigsw (cp
, switches
);
72 adios (NULL
, "-%s unknown", cp
);
75 snprintf (buf
, sizeof(buf
), "%s [+folder] [switches]",
77 print_help (buf
, switches
, 1);
80 print_version(invo_name
);
84 if (!(cp
= *argp
++) || *cp
== '-')
85 adios (NULL
, "missing argument name to %s", argp
[-2]);
87 svector_push_back (seqs
, cp
);
120 if (*cp
== '+' || *cp
== '@') {
122 adios (NULL
, "only one folder at a time!");
124 folder
= pluspath (cp
);
126 adios (NULL
, "usage: %s [+folder] [switches]", invo_name
);
130 if (!context_find ("path"))
131 free (path ("./", TFOLDER
));
133 /* if no folder is given, use default folder */
135 folder
= getfolder (0);
136 maildir
= m_maildir (folder
);
138 /* check if folder exists */
139 if (stat (maildir
, &st
) == NOTOK
) {
141 adios (maildir
, "error on folder");
143 adios (NULL
, "folder %s doesn't exist", maildir
);
144 if (!makedir (maildir
))
145 adios (NULL
, "unable to create folder %s", maildir
);
148 if (chdir (maildir
) == NOTOK
)
149 adios (maildir
, "unable to change directory to");
151 /* ignore a few signals */
152 SIGNAL (SIGHUP
, SIG_IGN
);
153 SIGNAL (SIGINT
, SIG_IGN
);
154 SIGNAL (SIGQUIT
, SIG_IGN
);
155 SIGNAL (SIGTERM
, SIG_IGN
);
157 /* create a temporary file */
158 tmpfilenam
= m_mktemp (invo_name
, &fd
, NULL
);
159 if (tmpfilenam
== NULL
) {
160 adios(NULL
, "unable to create temporary file in %s", get_temp_dir());
162 chmod (tmpfilenam
, m_gmprot());
164 /* copy the message from stdin into temp file */
165 cpydata (fileno (stdin
), fd
, "standard input", tmpfilenam
);
167 if (fstat (fd
, &st
) == NOTOK
) {
168 (void) m_unlink (tmpfilenam
);
169 adios (tmpfilenam
, "unable to fstat");
171 if (close (fd
) == NOTOK
)
172 adios (tmpfilenam
, "error closing");
174 /* don't add file if it is empty */
175 if (st
.st_size
== 0) {
176 (void) m_unlink (tmpfilenam
);
177 inform("empty file");
182 * read folder and create message structure
184 if (!(mp
= folder_read (folder
, 1)))
185 adios (NULL
, "unable to read folder %s", folder
);
188 * Link message into folder, and possibly add
189 * to the Unseen-Sequence's.
191 if ((msgnum
= folder_addmsg (&mp
, tmpfilenam
, 0, unseensw
, 0, 0, NULL
)) == -1)
195 * Add the message to any extra sequences
196 * that have been specified.
199 /* The only reason that seqp was checked to be non-zero is in
200 case a -nosequence switch is added. */
201 for (seqp
= 0; seqp
< svector_size (seqs
); seqp
++) {
202 if (!seq_addmsg (mp
, svector_at (seqs
, seqp
), msgnum
, publicsw
,
209 seq_setunseen (mp
, 0); /* synchronize any Unseen-Sequence's */
210 seq_save (mp
); /* synchronize and save message sequences */
211 folder_free (mp
); /* free folder/message structure */
213 context_save (); /* save the global context file */
214 (void) m_unlink (tmpfilenam
); /* remove temporary file */
225 unlink_done(int status
)
227 if (tmpfilenam
&& *tmpfilenam
)
228 (void) m_unlink (tmpfilenam
);