2019-07-04 17:01:07 +02:00
|
|
|
package msg
|
|
|
|
|
|
|
|
import (
|
|
|
|
"bufio"
|
|
|
|
"errors"
|
|
|
|
"net/url"
|
|
|
|
"strings"
|
|
|
|
|
|
|
|
"git.sr.ht/~sircmpwn/aerc/lib"
|
2020-01-08 21:44:14 +01:00
|
|
|
"git.sr.ht/~sircmpwn/aerc/models"
|
2019-07-04 17:01:07 +02:00
|
|
|
"git.sr.ht/~sircmpwn/aerc/widgets"
|
2020-11-10 20:27:30 +01:00
|
|
|
"github.com/emersion/go-message/mail"
|
2019-07-04 17:01:07 +02:00
|
|
|
)
|
|
|
|
|
|
|
|
// Unsubscribe helps people unsubscribe from mailing lists by way of the
|
|
|
|
// List-Unsubscribe header.
|
|
|
|
type Unsubscribe struct{}
|
|
|
|
|
|
|
|
func init() {
|
|
|
|
register(Unsubscribe{})
|
|
|
|
}
|
|
|
|
|
|
|
|
// Aliases returns a list of aliases for the :unsubscribe command
|
|
|
|
func (Unsubscribe) Aliases() []string {
|
|
|
|
return []string{"unsubscribe"}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Complete returns a list of completions
|
|
|
|
func (Unsubscribe) Complete(aerc *widgets.Aerc, args []string) []string {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// Execute runs the Unsubscribe command
|
|
|
|
func (Unsubscribe) Execute(aerc *widgets.Aerc, args []string) error {
|
|
|
|
if len(args) != 1 {
|
|
|
|
return errors.New("Usage: unsubscribe")
|
|
|
|
}
|
|
|
|
widget := aerc.SelectedTab().(widgets.ProvidesMessage)
|
2019-07-10 02:04:21 +02:00
|
|
|
msg, err := widget.SelectedMessage()
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
headers := msg.RFC822Headers
|
2019-07-04 17:01:07 +02:00
|
|
|
if !headers.Has("list-unsubscribe") {
|
|
|
|
return errors.New("No List-Unsubscribe header found")
|
|
|
|
}
|
|
|
|
methods := parseUnsubscribeMethods(headers.Get("list-unsubscribe"))
|
|
|
|
aerc.Logger().Printf("found %d unsubscribe methods", len(methods))
|
|
|
|
for _, method := range methods {
|
|
|
|
aerc.Logger().Printf("trying to unsubscribe using %v", method)
|
|
|
|
switch method.Scheme {
|
|
|
|
case "mailto":
|
|
|
|
return unsubscribeMailto(aerc, method)
|
|
|
|
case "http", "https":
|
|
|
|
return unsubscribeHTTP(method)
|
|
|
|
default:
|
|
|
|
aerc.Logger().Printf("skipping unrecognized scheme: %s", method.Scheme)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return errors.New("no supported unsubscribe methods found")
|
|
|
|
}
|
|
|
|
|
|
|
|
// parseUnsubscribeMethods reads the list-unsubscribe header and parses it as a
|
|
|
|
// list of angle-bracket <> deliminated URLs. See RFC 2369.
|
|
|
|
func parseUnsubscribeMethods(header string) (methods []*url.URL) {
|
|
|
|
r := bufio.NewReader(strings.NewReader(header))
|
|
|
|
for {
|
|
|
|
// discard until <
|
|
|
|
_, err := r.ReadSlice('<')
|
|
|
|
if err != nil {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
// read until <
|
|
|
|
m, err := r.ReadSlice('>')
|
|
|
|
if err != nil {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
m = m[:len(m)-1]
|
|
|
|
if u, err := url.Parse(string(m)); err == nil {
|
|
|
|
methods = append(methods, u)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func unsubscribeMailto(aerc *widgets.Aerc, u *url.URL) error {
|
|
|
|
widget := aerc.SelectedTab().(widgets.ProvidesMessage)
|
|
|
|
acct := widget.SelectedAccount()
|
2020-11-10 20:27:30 +01:00
|
|
|
|
|
|
|
h := &mail.Header{}
|
|
|
|
h.SetSubject(u.Query().Get("subject"))
|
|
|
|
if to, err := mail.ParseAddressList(u.Opaque); err == nil {
|
|
|
|
h.SetAddressList("to", to)
|
2019-07-23 01:29:07 +02:00
|
|
|
}
|
2020-11-10 20:27:30 +01:00
|
|
|
|
2019-11-03 13:51:14 +01:00
|
|
|
composer, err := widgets.NewComposer(
|
2019-09-11 21:28:14 +02:00
|
|
|
aerc,
|
2020-04-24 11:42:21 +02:00
|
|
|
acct,
|
2019-07-23 01:29:07 +02:00
|
|
|
aerc.Config(),
|
|
|
|
acct.AccountConfig(),
|
|
|
|
acct.Worker(),
|
2019-11-03 13:51:14 +01:00
|
|
|
"",
|
2020-11-10 20:27:30 +01:00
|
|
|
h,
|
2020-01-08 21:44:14 +01:00
|
|
|
models.OriginalMail{},
|
2019-07-23 01:29:07 +02:00
|
|
|
)
|
2019-11-03 13:51:14 +01:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2019-07-04 17:01:07 +02:00
|
|
|
composer.SetContents(strings.NewReader(u.Query().Get("body")))
|
|
|
|
tab := aerc.NewTab(composer, "unsubscribe")
|
2019-07-23 01:29:07 +02:00
|
|
|
composer.OnHeaderChange("Subject", func(subject string) {
|
2019-07-04 17:01:07 +02:00
|
|
|
if subject == "" {
|
|
|
|
tab.Name = "unsubscribe"
|
|
|
|
} else {
|
|
|
|
tab.Name = subject
|
|
|
|
}
|
|
|
|
tab.Content.Invalidate()
|
|
|
|
})
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func unsubscribeHTTP(u *url.URL) error {
|
2020-07-06 21:14:15 +02:00
|
|
|
lib.OpenFile(u.String(), nil)
|
2020-04-13 23:05:52 +02:00
|
|
|
return nil
|
2019-07-04 17:01:07 +02:00
|
|
|
}
|