about summary refs log tree commit diff
path: root/mseq.c
blob: 733b141c17043096aa71963251d99a831810bbee (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
#include <dirent.h>
#include <errno.h>
#include <limits.h>
#include <search.h>
#include <string.h>
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>

#include "blaze822.h"

static int fflag;
static int rflag;
static int Aflag;
static char *cflag;
static char *Cflag;
static int Sflag;

struct name {
	char *id;
	char *file;
};

static void *names;

int
nameorder(const void *a, const void *b)
{
	struct name *ia = (struct name *)a;
	struct name *ib = (struct name *)b;

	return strcmp(ia->id, ib->id);
}

char *
namefind(char *id)
{
	struct name key, **result;
	key.id = id;

	if (!(result = tfind(&key, &names, nameorder)))
		return 0;

	return (*result)->file;
}

void
namescan(char *dir)
{
	DIR *fd;
	struct dirent *d;

	fd = opendir(dir);
	if (!fd)
		return;
	while ((d = readdir(fd))) {
#if defined(DT_REG) && defined(DT_UNKNOWN)
		if (d->d_type != DT_REG && d->d_type != DT_UNKNOWN)
			continue;
#endif
		if (d->d_name[0] == '.')
			continue;

		char file[PATH_MAX];
		snprintf(file, sizeof file, "%s/%s", dir, d->d_name);

		char *e;
		if ((e = strstr(d->d_name, ":2,")))
			*e = 0;

		struct name *c = malloc(sizeof (struct name));
		c->id = strdup(d->d_name);
		c->file = strdup(file);
		tsearch(c, &names, nameorder);
	}
	closedir(fd);

	// add dir so know we scanned it already
	struct name *c = malloc(sizeof (struct name));
	c->id = c->file = strdup(dir);
	tsearch(c, &names, nameorder);
}

char *
search(char *file)
{
	char dir[PATH_MAX];
	char *e;
	if ((e = strrchr(file, '/'))) {
		snprintf(dir, sizeof dir, "%.*s", (int)(e-file), file);
		file = e+1;
	} else {
		snprintf(dir, sizeof dir, ".");
	}

	if (!namefind(dir))
		namescan(dir);

	return namefind(file);
}

/* strategy to find a Maildir file name:
   - if file exists, all good
   - try a few common different flags
   - index the containing dir, try to search for the id
   - if its in new/, try the same in cur/
 */
int
fix(FILE *out, char *file)
{
	int i;
	for (i = 0; *file == ' '; i++, file++)
		;

	char buf[PATH_MAX];
	char *bufptr = buf;

	if (*file == '<' || access(file, F_OK) == 0) {
		bufptr = file;
		goto ok;
	}

	char *e;
	char *sep;

	if ((e = strstr(file, ":2,"))) {
		sep = "";
		e[3] = 0;
	} else {
		sep = ":2,";
	}
	snprintf(buf, sizeof buf, "%s%s", file, sep);
	if (access(buf, F_OK) == 0) goto ok;
	snprintf(buf, sizeof buf, "%s%sS", file, sep);
	if (access(buf, F_OK) == 0) goto ok;
	snprintf(buf, sizeof buf, "%s%sRS", file, sep);
	if (access(buf, F_OK) == 0) goto ok;
	snprintf(buf, sizeof buf, "%s%sFS", file, sep);
	if (access(buf, F_OK) == 0) goto ok;

	if ((bufptr = search(file))) goto ok;

	char *ee = strrchr(file, '/');
	if (ee >= file + 3 && ee[-3] == 'n' && ee[-2] == 'e' && ee[-1] == 'w') {
		ee[-3] = 'c'; ee[-2] = 'u'; ee[-1] = 'r';
		return fix(out, file-i);
	}

	return 0;
ok:
	while(i--)
		putc(' ', out);
	fprintf(out, "%s\n", bufptr);
	return 1;
}

void
cat(FILE *src, FILE *dst)
{
	char buf[4096];
	size_t rd;

	rewind(src);
	while ((rd = fread(buf, 1, sizeof buf, src)) > 0)
		fwrite(buf, 1, rd, dst);
	if (!feof(src)) {
		perror("mseq: fread");
		exit(2);
	}
}

int
stdinmode()
{
	char *line = 0;
	char *l;
	size_t linelen = 0;
	ssize_t rd;
	FILE *outfile;

	char tmpfile[PATH_MAX];
	char oldfile[PATH_MAX];
	char *seqfile = 0;

	if (Sflag) {
		// XXX locking?
		seqfile = getenv("MAILSEQ");
		if (!seqfile)
			seqfile = blaze822_home_file("seq");
		snprintf(tmpfile, sizeof tmpfile, "%s-", seqfile);
		snprintf(oldfile, sizeof oldfile, "%s.old", seqfile);
		outfile = fopen(tmpfile, "w+");
		if (!outfile) {
			fprintf(stderr,
			    "mseq: Could not create sequence file '%s': %s.\n",
			    seqfile, strerror(errno));
			fprintf(stderr,
			    "mseq: Ensure %s exists and is writable.\n",
			    blaze822_home_file(""));
			exit(2);
		}
		if (Aflag) {
			FILE *seq = fopen(seqfile, "r");
			if (seq) {
				cat(seq, outfile);
				fclose(seq);
			}
		}
	} else {
		outfile = stdout;
	}

	while ((rd = getdelim(&line, &linelen, '\n', stdin)) != -1) {
		if (line[rd-1] == '\n')
			line[rd-1] = 0;

		l = line;
		if (rflag)
			while (*l == ' ' || *l == '\t')
				l++;
		if (fflag)
			fix(outfile, l);
		else
			fprintf(outfile, "%s\n", l);
	}

	if (Sflag) {
		fflush(outfile);
		if (rename(seqfile, oldfile) < 0 && errno != ENOENT) {
			perror("mseq: rename");
			exit(2);
		}
		if (rename(tmpfile, seqfile) < 0) {
			perror("mseq: rename");
			exit(2);
		}

		if (!isatty(1))
			cat(outfile, stdout);

		fclose(outfile);
	}

	free(line);
	return 0;
}

void
overridecur(char *file)
{
	while (*file == ' ')
		file++;
	setenv("MAILDOT", file, 1);
}

void
setcur(char *file)
{
	while (*file == ' ')
		file++;
	unsetenv("MAILDOT");
	blaze822_seq_setcur(file);
}

int
main(int argc, char *argv[])
{
	int c;
	while ((c = getopt(argc, argv, "c:frAC:S")) != -1)
		switch(c) {
		case 'c': cflag = optarg; break;
		case 'f': fflag = 1; break;
		case 'r': rflag = 1; break;
		case 'A': Sflag = Aflag = 1; break;
		case 'C': Cflag = optarg; break;
		case 'S': Sflag = 1; break;
		default:
		usage:
			fprintf(stderr,
			    "Usage: mseq [-fr] [-c msg] [msgs...]\n"
			    "	    mseq -S [-fr] < sequence\n"
			    "	    mseq -A [-fr] < sequence\n"
			    "	    mseq -C msg\n"
			);
			exit(1);
		}

	if (cflag)
		blaze822_loop1(cflag, overridecur);

	if (Cflag) {
		blaze822_loop1(Cflag, setcur);
		return 0;
	}

	if (Sflag && optind != argc) {
		fprintf(stderr, "error: -S/-A doesn't take arguments.\n");
		goto usage;
	}

	if (optind == argc && !isatty(0))
		return stdinmode();

	char *seq = blaze822_seq_open(0);
	if (!seq)
		return 1;

	int i;
	char *f;
	char *a;
	struct blaze822_seq_iter iter = { 0 };

	if (optind == argc) {
		a = ":";
		i = argc;
		goto hack;
	}
	for (i = optind; i < argc; i++) {
		a = argv[i];
hack:
		if (strchr(a, '/')) {
			printf("%s\n", a);
			continue;
		}
		while ((f = blaze822_seq_next(seq, a, &iter))) {
			char *s = f;
			if (rflag)
				while (*s == ' ' || *s == '\t')
					s++;
			if (fflag)
				fix(stdout, s);
			else
				printf("%s\n", s);
			free(f);
		}
	}

	return 0;
}