shithub: mongrel

Download patch

ref: 66e9098dcd2eae1d65cd336164f2b6d7e5ea9b82
parent: 0d843ac836b5afb166eb6b9b8a6590f1548af4ee
author: phil9 <telephil9@gmail.com>
date: Wed Feb 9 12:10:11 EST 2022

add ability to save parts

	middle-clicking on individual message parts will brind a menu with
	a save option allowing to save the part to a file on disk

--- a/a.h
+++ b/a.h
@@ -88,7 +88,7 @@
 void indexswitch(Mailbox*);
 
 /* pager */
-void pagerinit(Mousectl*, Theme*);
+void pagerinit(Mousectl*, Kbdctl*, Theme*);
 void pagerresize(Rectangle);
 void pagerdraw(void);
 void pagermouse(Mouse);
@@ -97,3 +97,4 @@
 
 void mesgmenuhit(int, Mouse);
 
+int kbdenter(char *ask, char *buf, int len, Mousectl *mc, Kbdctl *kc, Screen *scr);
--- a/index.c
+++ b/index.c
@@ -6,6 +6,7 @@
 #include <thread.h>
 #include <plumb.h>
 #include "theme.h"
+#include "kbd.h"
 #include "a.h"
 
 enum
--- a/main.c
+++ b/main.c
@@ -7,8 +7,8 @@
 #include <thread.h>
 #include <plumb.h>
 #include "theme.h"
-#include "a.h"
 #include "kbd.h"
+#include "a.h"
 
 enum
 {
@@ -182,7 +182,7 @@
 		*/
 	}
 	indexinit(mctl, showc, selc, theme);
-	pagerinit(mctl, theme);
+	pagerinit(mctl, kctl, theme);
 }
 
 void
--- a/mbox.c
+++ b/mbox.c
@@ -6,6 +6,7 @@
 #include <draw.h>
 #include <mouse.h>
 #include "theme.h"
+#include "kbd.h"
 #include "a.h"
 
 char*
--- a/mkfile
+++ b/mkfile
@@ -2,7 +2,7 @@
 
 BIN=/$objtype/bin
 TARG=mongrel
-OFILES=main.$O index.$O pager.$O mbox.$O utils.$O kbd.$O text.$O theme.$O
+OFILES=main.$O index.$O pager.$O mbox.$O utils.$O kbd.$O text.$O theme.$O enter.$O
 HFILES=a.h w.h kbd.h theme.h
 
 </sys/src/cmd/mkone
--- a/pager.c
+++ b/pager.c
@@ -6,6 +6,7 @@
 #include <thread.h>
 #include <plumb.h>
 #include "theme.h"
+#include "kbd.h"
 #include "a.h"
 #include "w.h"
 
@@ -36,6 +37,7 @@
 };
 
 static Mousectl *mc;
+static Kbdctl *kc;
 static Text text;
 static Rectangle viewr;
 static Rectangle headr;
@@ -222,9 +224,40 @@
 }
 
 void
-partclick(Point p)
+savepart(Message *m)
 {
-	Message *m;
+	char name[255] = {0}, iname[255] = {0}, buf[1024] = {0};
+	int ifd, fd, n;
+
+	snprint(name, sizeof name, "%s", m->filename);
+	n = kbdenter("Save as:", name, sizeof name, mc, kc, nil);
+	if(n <= 0)
+		return;
+	fd = create(name, OWRITE, 0644);
+	if(fd < 0){
+		fprint(2, "unable to save part as '%s': %r", name); /* FIXME */
+		return;
+	}
+	snprint(iname, sizeof iname, "%s/body", m->path);
+	ifd = open(iname, OREAD);
+	if(ifd < 0){
+		close(fd);
+		fprint(2, "unable to open part file '%s': %r", iname); /* FIXME */
+		return;
+	}
+	for(;;){
+		n = read(ifd, buf, sizeof buf);
+		if(n <= 0)
+			break;
+		write(fd, buf, n);
+	}
+	close(ifd);
+	close(fd);
+}
+
+void
+plumbpart(Message *m)
+{
 	char buf[1024] = {0};
 	int fd, n;
 
@@ -231,12 +264,6 @@
 	fd = plumbopen("send", OWRITE);
 	if(fd < 0)
 		return;
-	n = (p.y - partsr.min.y) / (font->height+Padding);
-	if(n < 0 || n >= nparts){
-		close(fd);
-		return;
-	}
-	m = parts[n];
 	for(n = 0; n < nelem(handlers); n++){
 		if(strcmp(m->type, handlers[n].type) == 0){
 			snprint(buf, sizeof buf, handlers[n].fmt, m->path);
@@ -251,12 +278,37 @@
 }
 
 void
+partclick(Mouse m)
+{
+	enum { Mpsave, Mpplumb };
+	char *menustr[] = { "save", "plumb", nil };
+	Menu menu = { menustr };
+	int n, i;
+
+	n = (m.xy.y - partsr.min.y) / (font->height+Padding);
+	if(n < 0 || n >= nparts)
+		return;
+	if(m.buttons == 2){
+		i = menuhit(2, mc, &menu, nil);
+		switch(i){
+			case Mpsave:
+				savepart(parts[n]);
+				break;
+			case Mpplumb:
+				plumbpart(parts[n]);
+				break;
+		}
+	}else if(m.buttons == 4)
+		plumbpart(parts[n]);
+}
+
+void
 pagermouse(Mouse m)
 {
 	if(!ptinrect(m.xy, viewr))
 		return;
-	if(nparts > 0 && ptinrect(m.xy, partsr) && m.buttons == 4)
-		partclick(m.xy);
+	if(nparts > 0 && ptinrect(m.xy, partsr))
+		partclick(m);
 	else if(ptinrect(m.xy, textr)){
 		if(m.buttons == 4)
 			mesgmenuhit(3, m);
@@ -272,11 +324,12 @@
 }
 
 void
-pagerinit(Mousectl *mctl, Theme *theme)
+pagerinit(Mousectl *mctl, Kbdctl *kctl, Theme *theme)
 {
 	Rectangle r;
 
 	mc = mctl;
+	kc = kctl;
 	tz = tzload("local");
 	showcc = 0;
 	nparts = 0;
--- a/utils.c
+++ b/utils.c
@@ -4,6 +4,7 @@
 #include <thread.h>
 #include <mouse.h>
 #include "theme.h"
+#include "kbd.h"
 #include "a.h"
 
 void*