summary refs log tree commit diff
diff options
context:
space:
mode:
authorLeah Neukirchen <leah@vuxu.org>2018-04-17 15:15:31 +0200
committerLeah Neukirchen <leah@vuxu.org>2018-04-17 15:15:31 +0200
commit5d43ad24048698ba89858648389e7e8479801746 (patch)
tree49395b69809b567e2ba51b3074809a746a4fb661
parent5f250330cb0269191a25ba76b52ac50d5dcf8cc4 (diff)
downloadmblaze-5d43ad24048698ba89858648389e7e8479801746.tar.gz
mblaze-5d43ad24048698ba89858648389e7e8479801746.tar.xz
mblaze-5d43ad24048698ba89858648389e7e8479801746.zip
mdeliver: add refiling capability
-rw-r--r--GNUmakefile5
-rw-r--r--mdeliver.c61
2 files changed, 64 insertions, 2 deletions
diff --git a/GNUmakefile b/GNUmakefile
index 63209fd..e736104 100644
--- a/GNUmakefile
+++ b/GNUmakefile
@@ -27,8 +27,8 @@ all: $(ALL) museragent
 $(ALL) : % : %.o
 maddr magrep mdeliver mexport mflag mflow mgenmid mhdr mpick mscan msed mshow \
   msort mthread : blaze822.o mymemmem.o mytimegm.o
-maddr magrep mexport mflag mgenmid mhdr mlist mpick mscan msed mseq mshow msort \
-  mthread : seq.o slurp.o
+maddr magrep mdeliver mexport mflag mgenmid mhdr mlist mpick mscan msed mseq \
+  mshow msort mthread : seq.o slurp.o
 maddr magrep mflow mhdr mpick mscan mshow : rfc2047.o
 magrep mflow mhdr mshow : rfc2045.o
 mshow : filter.o safe_u8putstr.o rfc2231.o pipeto.o
@@ -64,6 +64,7 @@ install: FRC all
 	ln -sf mcom $(DESTDIR)$(BINDIR)/mbnc
 	ln -sf mcom $(DESTDIR)$(BINDIR)/mfwd
 	ln -sf mcom $(DESTDIR)$(BINDIR)/mrep
+	ln -sf mdeliver $(DESTDIR)$(BINDIR)/mrefile
 	install -m0644 man/*.1 $(DESTDIR)$(MANDIR)/man1
 	install -m0644 man/*.5 $(DESTDIR)$(MANDIR)/man5
 	install -m0644 man/*.7 $(DESTDIR)$(MANDIR)/man7
diff --git a/mdeliver.c b/mdeliver.c
index 1a32af4..1714760 100644
--- a/mdeliver.c
+++ b/mdeliver.c
@@ -23,6 +23,7 @@ design rationale:
 */
 
 static int cflag;
+static int kflag;
 static int Mflag;
 static int vflag;
 static char *Xflag;
@@ -182,9 +183,69 @@ tryagain:
 	return 0;
 }
 
+void
+refile(char *file)
+{
+	while (*file == ' ' || *file == '\t')
+		file++;
+
+	FILE *f = fopen(file, "r");
+	if (!f) {
+		fprintf(stderr, "mrefile: %s: %s\n", file, strerror(errno)); 
+		return;
+	}
+
+	// keep flags
+	char *flags = strstr(file, ":2,");
+	if (flags)
+		Xflag = flags + 3;
+	else
+		Xflag = "";
+
+	if (deliver(f) < 0) {
+		perror("mrefile");
+		return;
+	}
+
+	fclose(f);
+	if (!kflag)
+		unlink(file);
+}
+
 int
 main(int argc, char *argv[])
 {
+	if (strchr(argv[0], 'f')) {
+		// mrefile(1)
+
+		cflag = 1;  // use cur/
+
+		int c;
+		while ((c = getopt(argc, argv, "kv")) != -1)
+			switch (c) {
+			case 'k': kflag = 1; break;
+			case 'v': vflag = 1; break;
+			default:
+usage:
+				fprintf(stderr,
+				    "Usage: mrefile [-kv] [msgs...] maildir\n");
+			exit(1);
+		}
+
+		if (argc == optind)
+			goto usage;
+
+		targetdir = argv[argc - 1];
+		gethost();
+
+		if (argc == optind + 1 && isatty(0))
+			blaze822_loop1(".", refile);
+		else
+			blaze822_loop(argc - 1 - optind, argv + optind, refile);
+
+		return 0;
+	}
+
 	int c;
 	while ((c = getopt(argc, argv, "cMvX:")) != -1)
 		switch (c) {