1 /* rcvstore.c -- asynchronously add mail to a folder
2  *
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.
6  */
7 
8 #include <h/mh.h>
9 #include <fcntl.h>
10 #include <h/signals.h>
11 #include <h/mts.h>
12 #include <h/utils.h>
13 #include "../sbr/m_maildir.h"
14 #include "../sbr/m_mktemp.h"
15 #include "../sbr/makedir.h"
16 
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) \
29 
30 #define X(sw, minchars, id) id,
31 DEFINE_SWITCH_ENUM(RCVSTORE);
32 #undef X
33 
34 #define X(sw, minchars, id) { sw, minchars, id },
35 DEFINE_SWITCH_ARRAY(RCVSTORE, switches);
36 #undef X
37 
38 
39 /*
40  * name of temporary file to store incoming message
41  */
42 static char *tmpfilenam = NULL;
43 
44 static void unlink_done(int) NORETURN;
45 
46 int
main(int argc,char ** argv)47 main (int argc, char **argv)
48 {
49     int publicsw = -1, zerosw = 0;
50     int create = 1, unseensw = 1;
51     int fd, msgnum;
52     size_t seqp = 0;
53     char *cp, *maildir, *folder = NULL, buf[BUFSIZ];
54     char **argp, **arguments;
55     svector_t seqs = svector_create (0);
56     struct msgs *mp;
57     struct stat st;
58 
59     if (nmh_init(argv[0], 2)) { return 1; }
60 
61     done=unlink_done;
62 
63     mts_init ();
64     arguments = getarguments (invo_name, argc, argv, 1);
65     argp = arguments;
66 
67     /* parse arguments */
68     while ((cp = *argp++)) {
69 	if (*cp == '-') {
70 	    switch (smatch (++cp, switches)) {
71 	    case AMBIGSW:
72 		ambigsw (cp, switches);
73 		done (1);
74 	    case UNKWNSW:
75 		adios (NULL, "-%s unknown", cp);
76 
77 	    case HELPSW:
78 		snprintf (buf, sizeof(buf), "%s [+folder] [switches]",
79 			  invo_name);
80 		print_help (buf, switches, 1);
81 		done (0);
82 	    case VERSIONSW:
83 		print_version(invo_name);
84 		done (0);
85 
86 	    case SEQSW:
87 		if (!(cp = *argp++) || *cp == '-')
88 		    adios (NULL, "missing argument name to %s", argp[-2]);
89 
90 		svector_push_back (seqs, cp);
91 		seqp++;
92 		continue;
93 
94 	    case UNSEENSW:
95 		unseensw = 1;
96 		continue;
97 	    case NUNSEENSW:
98 		unseensw = 0;
99 		continue;
100 
101 	    case PUBSW:
102 		publicsw = 1;
103 		continue;
104 	    case NPUBSW:
105 		publicsw = 0;
106 		continue;
107 
108 	    case ZEROSW:
109 		zerosw++;
110 		continue;
111 	    case NZEROSW:
112 		zerosw = 0;
113 		continue;
114 
115 	    case CRETSW:
116 		create++;
117 		continue;
118 	    case NCRETSW:
119 		create = 0;
120 		continue;
121 	    }
122 	}
123 	if (*cp == '+' || *cp == '@') {
124 	    if (folder)
125 		adios (NULL, "only one folder at a time!");
126 	    else
127 		folder = pluspath (cp);
128 	} else {
129 	    adios (NULL, "usage: %s [+folder] [switches]", invo_name);
130 	}
131     }
132 
133     if (!context_find ("path"))
134 	free (path ("./", TFOLDER));
135 
136     /* if no folder is given, use default folder */
137     if (!folder)
138 	folder = getfolder (0);
139     maildir = m_maildir (folder);
140 
141     /* check if folder exists */
142     if (stat (maildir, &st) == NOTOK) {
143 	if (errno != ENOENT)
144 	    adios (maildir, "error on folder");
145 	if (!create)
146 	    adios (NULL, "folder %s doesn't exist", maildir);
147 	if (!makedir (maildir))
148 	    adios (NULL, "unable to create folder %s", maildir);
149     }
150 
151     if (chdir (maildir) == NOTOK)
152 	adios (maildir, "unable to change directory to");
153 
154     /* ignore a few signals */
155     SIGNAL (SIGHUP, SIG_IGN);
156     SIGNAL (SIGINT, SIG_IGN);
157     SIGNAL (SIGQUIT, SIG_IGN);
158     SIGNAL (SIGTERM, SIG_IGN);
159 
160     /* create a temporary file */
161     tmpfilenam = m_mktemp (invo_name, &fd, NULL);
162     if (tmpfilenam == NULL) {
163 	adios(NULL, "unable to create temporary file in %s", get_temp_dir());
164     }
165     chmod (tmpfilenam, m_gmprot());
166 
167     /* copy the message from stdin into temp file */
168     cpydata (fileno (stdin), fd, "standard input", tmpfilenam);
169 
170     if (fstat (fd, &st) == NOTOK) {
171 	(void) m_unlink (tmpfilenam);
172 	adios (tmpfilenam, "unable to fstat");
173     }
174     if (close (fd) == NOTOK)
175 	adios (tmpfilenam, "error closing");
176 
177     /* don't add file if it is empty */
178     if (st.st_size == 0) {
179 	(void) m_unlink (tmpfilenam);
180 	inform("empty file");
181 	done (0);
182     }
183 
184     /*
185      * read folder and create message structure
186      */
187     if (!(mp = folder_read (folder, 1)))
188 	adios (NULL, "unable to read folder %s", folder);
189 
190     /*
191      * Link message into folder, and possibly add
192      * to the Unseen-Sequence's.
193      */
194     if ((msgnum = folder_addmsg (&mp, tmpfilenam, 0, unseensw, 0, 0, NULL)) == -1)
195 	done (1);
196 
197     /*
198      * Add the message to any extra sequences
199      * that have been specified.
200      */
201     if (seqp) {
202 	/* The only reason that seqp was checked to be non-zero is in
203 	   case a -nosequence switch is added. */
204 	for (seqp = 0; seqp < svector_size (seqs); seqp++) {
205 	    if (!seq_addmsg (mp, svector_at (seqs, seqp), msgnum, publicsw,
206 			     zerosw))
207 		done (1);
208 	}
209     }
210 
211     svector_free (seqs);
212     seq_setunseen (mp, 0);	/* synchronize any Unseen-Sequence's      */
213     seq_save (mp);		/* synchronize and save message sequences */
214     folder_free (mp);		/* free folder/message structure          */
215 
216     context_save ();		/* save the global context file           */
217     (void) m_unlink (tmpfilenam); /* remove temporary file                  */
218     tmpfilenam = NULL;
219 
220     done (0);
221     return 1;
222 }
223 
224 /*
225  * Clean up and exit
226  */
227 static void NORETURN
unlink_done(int status)228 unlink_done(int status)
229 {
230     if (tmpfilenam && *tmpfilenam)
231 	(void) m_unlink (tmpfilenam);
232     exit (status);
233 }
234