From 87d856c10cd500fb588d2af4cb2f590e21c6cee4 Mon Sep 17 00:00:00 2001
From: Moritz Poldrack <git@moritz.sh>
Date: Sun, 9 Oct 2022 00:52:43 +0200
Subject: [PATCH] mailto: allow attaching of files

Some programs like Skanpage allow sharing files via email and attaching
them automatically from the mailto: link.
This patch introduces parsing of the attach query argument in mailto
links and attaches the listed files.
A potential file:// URL has it's prefix removed.

Signed-off-by: Moritz Poldrack <git@moritz.sh>
Acked-by: Robin Jarry <robin@jarry.cc>
---
 CHANGELOG.md    |  1 +
 widgets/aerc.go | 10 ++++++++++
 2 files changed, 11 insertions(+)

diff --git a/CHANGELOG.md b/CHANGELOG.md
index f3ab262..e897e4b 100644
--- a/CHANGELOG.md
+++ b/CHANGELOG.md
@@ -19,6 +19,7 @@ The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/).
 - Specify opener as the first `:open` param instead of always using default
   handler (i.e. `:open gimp` to open attachment in GIMP).
 - Restored XOAUTH2 support for IMAP and SMTP.
+- Support for attaching files with `mailto:`-links
 
 ### Changed
 
diff --git a/widgets/aerc.go b/widgets/aerc.go
index 5697277..35c32fe 100644
--- a/widgets/aerc.go
+++ b/widgets/aerc.go
@@ -644,6 +644,7 @@ func (aerc *Aerc) Mailto(addr *url.URL) error {
 	var subject string
 	var body string
 	var acctName string
+	var attachments []string
 	h := &mail.Header{}
 	to, err := mail.ParseAddressList(addr.Opaque)
 	if err != nil && addr.Opaque != "" {
@@ -679,6 +680,11 @@ func (aerc *Aerc) Mailto(addr *url.URL) error {
 		case "subject":
 			subject = strings.Join(vals, ",")
 			h.SetText("Subject", subject)
+		case "attach":
+			for _, path := range vals {
+				// remove a potential file:// prefix.
+				attachments = append(attachments, strings.TrimPrefix(path, "file://"))
+			}
 		default:
 			// any other header gets ignored on purpose to avoid control headers
 			// being injected
@@ -720,6 +726,10 @@ func (aerc *Aerc) Mailto(addr *url.URL) error {
 		}
 		ui.Invalidate()
 	})
+
+	for _, file := range attachments {
+		composer.AddAttachment(file)
+	}
 	return nil
 }