9front - general discussion about 9front
 help / color / mirror / Atom feed
From: theinicke@bss-wf.de
To: 9front@9front.org
Subject: Re: [9front] Mail rewrite; open path with stored mails
Date: Sun, 14 Feb 2021 11:42:17 +0100	[thread overview]
Message-ID: <CB328208790410CFDF274B4E135DBB31@bss-wf.de> (raw)
In-Reply-To: <C3D560149971AA5B102B91F99227CF69@musolino.id.au>

Quoth Alex Musolino <alex@musolino.id.au>:
> I have a couple of general comments.
> 
> Firstly, the -d option is typically used as to enable debugging output
> of some kind.  Can you use a different character?  Perhaps -p for
> path?  Also, it seems that the options we support and the usage
> string(s) do not agree.
> 
> Secondly, you can register the removembox function to run on exit with
> atexit(2).  That'll save you having to call it yourself from any and
> every place that might result in an exit.  At the moment, removembox
> won't be called if we hit one of the sysfatals.
> 
> --
> Cheers,
> Alex Musolino
> 

Thank you for your comments, based on your suggestions I have provided the following patch
(assuming that you meant the usage string discrepancy that was there before my patch -
while at it I have also changed NAIL(1) to ACMEMAIL(1); if this was not what you meant please elaborate).

--
Cheers,
Tobias Heinicke


diff -r 02e3059af5bc sys/man/1/acmemail
--- a/sys/man/1/acmemail	Thu Feb 11 09:37:36 2021 +0100
+++ b/sys/man/1/acmemail	Sun Feb 14 11:36:18 2021 +0100
@@ -1,4 +1,4 @@
-.TH NAIL 1
+.TH ACMEMAIL 1
 .SH NAME
 Mail \- view mail in acme
 
@@ -6,7 +6,7 @@
 
 Mail
 [
-.B -OsT
+.B -TOs
 ]
 [
 .B -m
@@ -20,6 +20,13 @@
 .B -o
 .I outbox
 ]
+[
+.B -p
+.I path
+]
+[
+.I mbox
+]
 
 .SH DESCRIPTION
 .PP
@@ -64,6 +71,10 @@
 Save a copy of outgoing messages to the mailbox
 .IR outbox ,
 instead of discarding them after they're enqueued.
+.TP
+.BI -p " path
+Opens new mailbox for the given path,
+closes it on exit.
 
 .PP
 Mail presents and acme interface for a upas/fs mailbox.
diff -r 02e3059af5bc sys/src/cmd/upas/Mail/mbox.c
--- a/sys/src/cmd/upas/Mail/mbox.c	Thu Feb 11 09:37:36 2021 +0100
+++ b/sys/src/cmd/upas/Mail/mbox.c	Sun Feb 14 11:36:18 2021 +0100
@@ -32,6 +32,8 @@
 
 Reprog	*mesgpat;
 
+int	openedmbox = 0;
+
 int	threadsort = 1;
 int	sender;
 
@@ -45,6 +47,34 @@
 
 static void	showmesg(Biobuf*, Mesg*, int, int);
 
+int
+openctlfile(void)
+{
+	int fd;
+	char *path;
+
+	path = estrjoin(maildir, "/ctl", nil);
+	fd = open(path, OWRITE);
+	free(path);
+	return fd;
+}
+
+void
+removeopened(void)
+{
+	int fd;
+	char buf[256];
+
+	if(!openedmbox)
+		return;
+	fd = openctlfile();
+	if(fd == -1)
+		return;
+
+	snprint(buf, sizeof buf, "close %s", mailbox);
+	write(fd, buf, strlen(buf));
+}
+
 static void
 plumbloop(Channel *ch, int fd)
 {
@@ -676,13 +706,10 @@
 mbflush(char **, int)
 {
 	int i, j, ln, fd;
-	char *path;
 	Mesg *m, *p;
 
 	i = 0;
-	path = estrjoin(maildir, "/ctl", nil);
-	fd = open(path, OWRITE);
-	free(path);
+	fd = openctlfile();
 	if(fd == -1)
 		sysfatal("open mbox: %r");
 	while(i < mbox.nmesg){
@@ -975,15 +1002,84 @@
 static void
 usage(void)
 {
-	fprint(2, "usage: %s [-T] [-m mailfs] [-s] [-f format] [mbox]\n", argv0);
+	fprint(2, "usage: %s [-TOs] [-m maildir] [-f format] [-o outbox] [-p path] [mbox]\n", argv0);
 	exits("usage");
 }
 
+static void
+openmbox(char *path, char *mboxname)
+{
+	int fd, i;
+	char buf[512], err[ERRMAX];
+	char *name, *fsname, *s1, *s2, *abspath;
+
+	/* if path is already absolute, leave it as it is - to allow /imap...
+	otherwise get absolute path from relative one */
+	if(*path == '/'){
+		i = strlen(path);
+		if(path[i-1] == '/')
+			path[i-1] = '\0';
+		abspath = estrdup(path);
+	}else{
+		fd = open(path, OREAD);
+		if(fd < 0)
+			sysfatal("can't open %s: %r", path);
+	
+		if(fd2path(fd, buf, sizeof buf) != 0)
+			sysfatal("fd2path %s: %r", path);
+		close(fd);
+		abspath = estrdup(buf);
+	}
+
+	if(mboxname != nil)
+		name = mboxname;
+	else{
+		s1 = strrchr(abspath, '/');
+		if(s1 == nil)
+			name = abspath;
+		else{
+			*s1++ = '\0';
+			name = s1;
+			*--s1 = '/';
+		}
+	}
+
+	fd = openctlfile();
+	if(fd == -1)
+		sysfatal("open mbox: %r");
+	fsname = estrdup(name);
+	s2 = emalloc(5+strlen(abspath)+1+strlen(fsname)+2+1);
+	for(i=0; i<10; i++){
+		sprint(s2, "open %s %s", abspath, fsname);
+		if(write(fd, s2, strlen(s2)) >= 0)
+			break;
+		err[0] = '\0';
+		errstr(err, sizeof err);
+		if(strstr(err, "mbox name in use") == nil)
+			sysfatal("can't create directory %s for mail: %s", name, err);
+		free(fsname);
+		fsname = emalloc(strlen(name)+3);
+		sprint(fsname, "%s-%d", name, i);
+	}
+	if(i == 10)
+		sysfatal("can't open %s: %r", abspath);
+	if(atexit(removeopened) == 0) {
+		removeopened();
+		sysfatal("atexit: %r");
+	}
+	mailbox = fsname;
+	openedmbox = 1;
+	free(s2);
+	free(abspath);
+	close(fd);
+}
+
 void
 threadmain(int argc, char **argv)
 {
 	Fmt fmt;
 	char *cmd;
+	char *path = nil, *mboxname = nil;
 	int i;
 
 	mbox.view = Vgroup;
@@ -1017,6 +1113,9 @@
 	case 'o':
 		savebox = EARGF(usage());
 		break;
+	case 'p':
+		path = EARGF(usage());
+		break;
 	default:
 		usage();
 		break;
@@ -1024,8 +1123,13 @@
 
 	if(argc > 1)
 		usage();
-	if(argc == 1)
+	if(argc == 1) {
 		mailbox = argv[0];
+		mboxname = mailbox;
+	}
+
+	if(path != nil)
+		openmbox(path, mboxname);
 
 	mesgpat = regcomp("([0-9]+)(/.*)?");
 	cwait = threadwaitchan();


  reply	other threads:[~2021-02-14 10:50 UTC|newest]

Thread overview: 12+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2021-02-12 20:54 theinicke
2021-02-12 21:10 ` ori
2021-02-13 20:47   ` theinicke
2021-02-13 21:25     ` theinicke
2021-02-13 23:59       ` Alex Musolino
2021-02-14 10:42         ` theinicke [this message]
2021-02-14 20:57           ` ori
2021-02-14 22:05             ` theinicke
2021-02-14 22:55           ` Alex Musolino
2021-02-15  8:15             ` theinicke
2021-02-15  9:56               ` theinicke
  -- strict thread matches above, loose matches on Subject: below --
2021-02-12 20:50 [9front] Mail rewrite " theinicke

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=CB328208790410CFDF274B4E135DBB31@bss-wf.de \
    --to=theinicke@bss-wf.de \
    --cc=9front@9front.org \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).