1 /*
2 ** mark.c -- add message(s) to sequences in given folder
3 **        -- delete messages (s) from sequences in given folder
4 **        -- list sequences in given folder
5 **
6 ** This code is Copyright (c) 2002, by the authors of nmh.  See the
7 ** COPYRIGHT file in the root directory of the nmh distribution for
8 ** complete copyright information.
9 */
10 
11 #include <h/mh.h>
12 #include <h/utils.h>
13 #include <unistd.h>
14 #include <locale.h>
15 #include <sysexits.h>
16 
17 static struct swit switches[] = {
18 #define ADDSW  0
19 	{ "add", 0 },
20 #define DELSW  1
21 	{ "delete", 0 },
22 #define LSTSW  2
23 	{ "list", 0 },
24 #define SEQSW  3
25 	{ "sequence name", 0 },
26 #define PUBLSW  4
27 	{ "public", 0 },
28 #define NPUBLSW  5
29 	{ "nopublic", 2 },
30 #define ZEROSW  6
31 	{ "zero", 0 },
32 #define NZEROSW  7
33 	{ "nozero", 2 },
34 #define VERSIONSW  8
35 	{ "Version", 0 },
36 #define HELPSW  9
37 	{ "help", 0 },
38 #define DEBUGSW  10
39 	{ "debug", -5 },
40 	{ NULL, 0 }
41 };
42 
43 char *version=VERSION;
44 
45 /*
46 ** static prototypes
47 */
48 static void print_debug(struct msgs *);
49 static void seq_printdebug(struct msgs *);
50 
51 
52 int
main(int argc,char ** argv)53 main(int argc, char **argv)
54 {
55 	int addsw = 0, deletesw = 0, debugsw = 0;
56 	int listsw = 0, publicsw = -1, zerosw = 0, msgnum;
57 	unsigned int seqp = 0;
58 	char *cp, *maildir, *folder = NULL, buf[BUFSIZ];
59 	char **argp, **arguments;
60 	char *seqs[NUMATTRS + 1];
61 	struct msgs_array msgs = { 0, 0, NULL };
62 	struct msgs *mp;
63 
64 	setlocale(LC_ALL, "");
65 	invo_name = mhbasename(argv[0]);
66 
67 	/* read user profile/context */
68 	context_read();
69 
70 	arguments = getarguments(invo_name, argc, argv, 1);
71 	argp = arguments;
72 
73 	/*
74 	** Parse arguments
75 	*/
76 	while ((cp = *argp++)) {
77 		if (*cp == '-') {
78 			switch (smatch(++cp, switches)) {
79 			case AMBIGSW:
80 				ambigsw(cp, switches);
81 				exit(EX_USAGE);
82 			case UNKWNSW:
83 				adios(EX_USAGE, NULL, "-%s unknown\n", cp);
84 
85 			case HELPSW:
86 				snprintf(buf, sizeof(buf), "%s [+folder] [msgs] [switches]", invo_name);
87 				print_help(buf, switches, 1);
88 				exit(argc == 2 ? EX_OK : EX_USAGE);
89 			case VERSIONSW:
90 				print_version(invo_name);
91 				exit(argc == 2 ? EX_OK : EX_USAGE);
92 
93 			case ADDSW:
94 				addsw++;
95 				deletesw = listsw = 0;
96 				continue;
97 			case DELSW:
98 				deletesw++;
99 				addsw = listsw = 0;
100 				continue;
101 			case LSTSW:
102 				listsw++;
103 				addsw = deletesw = 0;
104 				continue;
105 
106 			case SEQSW:
107 				if (!(cp = *argp++) || *cp == '-') {
108 					adios(EX_USAGE, NULL, "missing argument to %s",
109 							argp[-2]);
110 				}
111 
112 				/* check if too many sequences specified */
113 				if (seqp >= NUMATTRS) {
114 					adios(EX_USAGE, NULL, "too many sequences (more than %d) specified", NUMATTRS);
115 				}
116 				seqs[seqp++] = cp;
117 				continue;
118 
119 			case PUBLSW:
120 				publicsw = 1;
121 				continue;
122 			case NPUBLSW:
123 				publicsw = 0;
124 				continue;
125 
126 			case DEBUGSW:
127 				debugsw++;
128 				continue;
129 
130 			case ZEROSW:
131 				zerosw++;
132 				continue;
133 			case NZEROSW:
134 				zerosw = 0;
135 				continue;
136 			}
137 		}
138 		if (*cp == '+' || *cp == '@') {
139 			if (folder) {
140 				adios(EX_USAGE, NULL, "only one folder at a time!");
141 			} else {
142 				folder = mh_xstrdup(expandfol(cp));
143 			}
144 		} else {
145 			app_msgarg(&msgs, cp);
146 		}
147 	}
148 
149 	/*
150 	** If we haven't specified -add, -delete, or -list,
151 	** then use -add if a sequence was specified, else
152 	** use -list.
153 	*/
154 	if (!addsw && !deletesw && !listsw) {
155 		if (seqp)
156 			addsw++;
157 		else
158 			listsw++;
159 	}
160 
161 	if (!msgs.size)
162 		app_msgarg(&msgs, listsw ? seq_all : seq_cur);
163 	if (!folder)
164 		folder = getcurfol();
165 	maildir = toabsdir(folder);
166 
167 	if (chdir(maildir) == NOTOK) {
168 		adios(EX_OSERR, maildir, "unable to change directory to");
169 	}
170 
171 	/* read folder and create message structure */
172 	if (!(mp = folder_read(folder))) {
173 		adios(EX_IOERR, NULL, "unable to read folder %s", folder);
174 	}
175 
176 	/* print some general debugging info */
177 	if (debugsw)
178 		print_debug(mp);
179 
180 	/* check for empty folder */
181 	if (mp->nummsg == 0) {
182 		adios(EX_DATAERR, NULL, "no messages in %s", folder);
183 	}
184 
185 	/* parse all the message ranges/sequences and set SELECTED */
186 	for (msgnum = 0; msgnum < msgs.size; msgnum++) {
187 		if (!m_convert(mp, msgs.msgs[msgnum])) {
188 			exit(EX_USAGE);
189 		}
190 	}
191 
192 	if (publicsw == 1 && is_readonly(mp)) {
193 		adios(EX_NOPERM, NULL, "folder %s is read-only, so -public not allowed",
194 				folder);
195 	}
196 
197 	/*
198 	** Make sure at least one sequence has been
199 	** specified if we are adding or deleting.
200 	*/
201 	if (seqp == 0 && (addsw || deletesw)) {
202 		adios(EX_USAGE, NULL, "-%s requires at least one -sequence argument",
203 				addsw ? "add" : "delete");
204 	}
205 	seqs[seqp] = NULL;
206 
207 	/* Adding messages to sequences */
208 	if (addsw) {
209 		for (seqp = 0; seqs[seqp]; seqp++) {
210 			if (!seq_addsel(mp, seqs[seqp], publicsw, zerosw)) {
211 				exit(EX_SOFTWARE);
212 			}
213 		}
214 	}
215 
216 	/* Deleting messages from sequences */
217 	if (deletesw) {
218 		for (seqp = 0; seqs[seqp]; seqp++) {
219 			if (!seq_delsel(mp, seqs[seqp], publicsw, zerosw)) {
220 				exit(EX_SOFTWARE);
221 			}
222 		}
223 	}
224 
225 	/* Listing messages in sequences */
226 	if (listsw) {
227 		if (seqp) {
228 			/* print the sequences given */
229 			for (seqp = 0; seqs[seqp]; seqp++)
230 				seq_print(mp, seqs[seqp]);
231 		} else {
232 			/* else print them all */
233 			seq_printall(mp);
234 		}
235 
236 		/* print debugging info about SELECTED messages */
237 		if (debugsw)
238 			seq_printdebug(mp);
239 	}
240 
241 	seq_save(mp);  /* synchronize message sequences */
242 	context_replace(curfolder, folder);  /* update current folder */
243 	context_save();  /* save the context file */
244 	folder_free(mp);  /* free folder/message structure */
245 	return EX_OK;
246 }
247 
248 
249 /*
250 ** Print general debugging info
251 */
252 static void
print_debug(struct msgs * mp)253 print_debug(struct msgs *mp)
254 {
255 	char buf[100];
256 
257 	printf("invo_name = %s\n", invo_name);
258 	printf("mypath = %s\n", mypath);
259 	printf("defpath = %s\n", defpath);
260 	printf("ctxpath = %s\n", ctxpath);
261 	printf("context flags = %s\n", snprintb(buf, sizeof(buf),
262 			(unsigned) ctxflags, DBITS));
263 	printf("foldpath = %s\n", mp->foldpath);
264 	printf("folder flags  = %s\n\n", snprintb(buf, sizeof(buf),
265 			(unsigned) mp->msgflags, FBITS));
266 	printf("lowmsg=%d hghmsg=%d nummsg=%d curmsg=%d\n",
267 			mp->lowmsg, mp->hghmsg, mp->nummsg, mp->curmsg);
268 	printf("lowsel=%d hghsel=%d numsel=%d\n",
269 			mp->lowsel, mp->hghsel, mp->numsel);
270 	printf("lowoff=%d hghoff=%d\n\n", mp->lowoff, mp->hghoff);
271 }
272 
273 
274 /*
275 ** Print debugging info about all the SELECTED
276 ** messages and the sequences they are in.
277 */
278 static void
seq_printdebug(struct msgs * mp)279 seq_printdebug(struct msgs *mp)
280 {
281 	int msgnum;
282 	char buf[100];
283 
284 	printf("\n");
285 	for (msgnum = mp->lowsel; msgnum <= mp->hghsel; msgnum++) {
286 		if (is_selected(mp, msgnum))
287 			printf("%*d: %s\n", DMAXFOLDER, msgnum, snprintb(buf, sizeof(buf), (unsigned) mp->msgstats[msgnum - mp->lowoff], seq_bits(mp)));
288 	}
289 }
290