| 1 |
package msg |
| 2 |
|
| 3 |
import ( |
| 4 |
"bufio" |
| 5 |
"bytes" |
| 6 |
"errors" |
| 7 |
"fmt" |
| 8 |
"io" |
| 9 |
"mime/multipart" |
| 10 |
"net/http" |
| 11 |
"net/url" |
| 12 |
"slices" |
| 13 |
"strings" |
| 14 |
"time" |
| 15 |
|
| 16 |
"git.sr.ht/~rjarry/aerc/app" |
| 17 |
"git.sr.ht/~rjarry/aerc/commands" |
| 18 |
"git.sr.ht/~rjarry/aerc/config" |
| 19 |
"git.sr.ht/~rjarry/aerc/lib" |
| 20 |
"git.sr.ht/~rjarry/aerc/lib/authres" |
| 21 |
"git.sr.ht/~rjarry/aerc/lib/log" |
| 22 |
"github.com/emersion/go-message/mail" |
| 23 |
) |
| 24 |
|
| 25 |
// Unsubscribe helps people unsubscribe from mailing lists by way of the |
| 26 |
// List-Unsubscribe header. |
| 27 |
type Unsubscribe struct { |
| 28 |
Edit bool `opt:"-e" desc:"Force [compose].edit-headers = true."` |
| 29 |
NoEdit bool `opt:"-E" desc:"Force [compose].edit-headers = false."` |
| 30 |
SkipEditor bool `opt:"-s" desc:"Skip the editor and go directly to the review screen."` |
| 31 |
} |
| 32 |
|
| 33 |
func init() { |
| 34 |
commands.Register(Unsubscribe{}) |
| 35 |
} |
| 36 |
|
| 37 |
func (Unsubscribe) Description() string { |
| 38 |
return "Attempt to automatically unsubscribe from mailing lists." |
| 39 |
} |
| 40 |
|
| 41 |
func (Unsubscribe) Context() commands.CommandContext { |
| 42 |
return commands.MESSAGE_LIST | commands.MESSAGE_VIEWER |
| 43 |
} |
| 44 |
|
| 45 |
// Aliases returns a list of aliases for the :unsubscribe command |
| 46 |
func (Unsubscribe) Aliases() []string { |
| 47 |
return []string{"unsubscribe"} |
| 48 |
} |
| 49 |
|
| 50 |
// Execute runs the Unsubscribe command |
| 51 |
func (u Unsubscribe) Execute(args []string) error { |
| 52 |
editHeaders := (config.Compose().EditHeaders || u.Edit) && !u.NoEdit |
| 53 |
|
| 54 |
widget := app.SelectedTabContent().(app.ProvidesMessage) |
| 55 |
msg, err := widget.SelectedMessage() |
| 56 |
if err != nil { |
| 57 |
return err |
| 58 |
} |
| 59 |
|
| 60 |
headers := msg.RFC822Headers |
| 61 |
|
| 62 |
details, err := authres.CreateParser(authres.DKIM)(headers, widget.SelectedAccount().AccountConfig().TrustedAuthRes) |
| 63 |
switch { |
| 64 |
case err != nil: |
| 65 |
return errors.New("Failed to validate DKIM signature") |
| 66 |
case slices.Contains(details.Results, authres.ResultFail): |
| 67 |
return errors.New("DKIM validation failed") |
| 68 |
case !slices.Contains(details.Results, authres.ResultPass): |
| 69 |
return errors.New("No passing DKIM signature found") |
| 70 |
} |
| 71 |
|
| 72 |
if !headers.Has("list-unsubscribe") { |
| 73 |
return errors.New("No List-Unsubscribe header found") |
| 74 |
} |
| 75 |
text, err := headers.Text("List-Unsubscribe") |
| 76 |
if err != nil { |
| 77 |
return err |
| 78 |
} |
| 79 |
methods := parseUnsubscribeMethods(text) |
| 80 |
if len(methods) == 0 { |
| 81 |
return fmt.Errorf("no methods found to unsubscribe") |
| 82 |
} |
| 83 |
log.Debugf("unsubscribe: found %d methods", len(methods)) |
| 84 |
|
| 85 |
unsubscribe := func(method *url.URL) { |
| 86 |
log.Debugf("unsubscribe: trying to unsubscribe using %s", method.Scheme) |
| 87 |
var err error |
| 88 |
switch strings.ToLower(method.Scheme) { |
| 89 |
case "mailto": |
| 90 |
err = unsubscribeMailto(method, editHeaders, u.SkipEditor) |
| 91 |
case "http", "https": |
| 92 |
err = unsubscribeHTTP(method, headers.Values("List-Unsubscribe-Post")) |
| 93 |
default: |
| 94 |
err = fmt.Errorf("unsubscribe: skipping unrecognized scheme: %s", method.Scheme) |
| 95 |
} |
| 96 |
if err != nil { |
| 97 |
app.PushError(err.Error()) |
| 98 |
} |
| 99 |
} |
| 100 |
|
| 101 |
var title string = "Select method to unsubscribe" |
| 102 |
if msg != nil && msg.Envelope != nil && len(msg.Envelope.From) > 0 { |
| 103 |
title = fmt.Sprintf("%s from %s", title, msg.Envelope.From[0]) |
| 104 |
} |
| 105 |
|
| 106 |
options := make([]string, len(methods)) |
| 107 |
for i, method := range methods { |
| 108 |
options[i] = method.Scheme |
| 109 |
} |
| 110 |
|
| 111 |
if len(methods) == 1 { |
| 112 |
unsubscribe(methods[0]) |
| 113 |
return nil |
| 114 |
} |
| 115 |
|
| 116 |
dialog := app.NewSelectorDialog( |
| 117 |
title, |
| 118 |
"Press <Enter> to confirm or <ESC> to cancel", |
| 119 |
options, 0, app.SelectedAccountUiConfig(), |
| 120 |
func(option string, err error) { |
| 121 |
app.CloseDialog() |
| 122 |
if err != nil { |
| 123 |
if errors.Is(err, app.ErrNoOptionSelected) { |
| 124 |
app.PushStatus("Unsubscribe: "+err.Error(), |
| 125 |
5*time.Second) |
| 126 |
} else { |
| 127 |
app.PushError("Unsubscribe: " + err.Error()) |
| 128 |
} |
| 129 |
return |
| 130 |
} |
| 131 |
for _, m := range methods { |
| 132 |
if m.Scheme == option { |
| 133 |
unsubscribe(m) |
| 134 |
return |
| 135 |
} |
| 136 |
} |
| 137 |
app.PushError("Unsubscribe: selected method not found") |
| 138 |
}, |
| 139 |
) |
| 140 |
app.AddDialog(dialog) |
| 141 |
|
| 142 |
return nil |
| 143 |
} |
| 144 |
|
| 145 |
// parseUnsubscribeMethods reads the list-unsubscribe header and parses it as a |
| 146 |
// list of angle-bracket <> deliminated URLs. See RFC 2369. |
| 147 |
func parseUnsubscribeMethods(header string) (methods []*url.URL) { |
| 148 |
r := bufio.NewReader(strings.NewReader(header)) |
| 149 |
for { |
| 150 |
// discard until < |
| 151 |
_, err := r.ReadSlice('<') |
| 152 |
if err != nil { |
| 153 |
return |
| 154 |
} |
| 155 |
// read until < |
| 156 |
m, err := r.ReadSlice('>') |
| 157 |
if err != nil { |
| 158 |
return |
| 159 |
} |
| 160 |
m = m[:len(m)-1] |
| 161 |
if u, err := url.Parse(string(m)); err == nil { |
| 162 |
methods = append(methods, u) |
| 163 |
} |
| 164 |
} |
| 165 |
} |
| 166 |
|
| 167 |
func unsubscribeMailto(u *url.URL, editHeaders, skipEditor bool) error { |
| 168 |
widget := app.SelectedTabContent().(app.ProvidesMessage) |
| 169 |
acct := widget.SelectedAccount() |
| 170 |
if acct == nil { |
| 171 |
return errors.New("No account selected") |
| 172 |
} |
| 173 |
|
| 174 |
h := &mail.Header{} |
| 175 |
h.SetSubject(u.Query().Get("subject")) |
| 176 |
if to, err := mail.ParseAddressList(u.Opaque); err == nil { |
| 177 |
h.SetAddressList("to", to) |
| 178 |
} |
| 179 |
|
| 180 |
composer, err := app.NewComposer( |
| 181 |
acct, |
| 182 |
acct.AccountConfig(), |
| 183 |
acct.Worker(), |
| 184 |
editHeaders, |
| 185 |
"", |
| 186 |
h, |
| 187 |
nil, |
| 188 |
strings.NewReader(u.Query().Get("body")), |
| 189 |
) |
| 190 |
if err != nil { |
| 191 |
return err |
| 192 |
} |
| 193 |
composer.Tab = app.NewTab(composer, "unsubscribe") |
| 194 |
if skipEditor { |
| 195 |
composer.Terminal().Close() |
| 196 |
} else { |
| 197 |
composer.FocusTerminal() |
| 198 |
} |
| 199 |
return nil |
| 200 |
} |
| 201 |
|
| 202 |
func unsubscribeHTTP(u *url.URL, postData []string) error { |
| 203 |
confirm := app.NewSelectorDialog( |
| 204 |
"Do you want to unsubscribe?", |
| 205 |
u.String(), |
| 206 |
[]string{"No", "Yes", "Open in Browser"}, 0, app.SelectedAccountUiConfig(), |
| 207 |
func(option string, _ error) { |
| 208 |
app.CloseDialog() |
| 209 |
switch option { |
| 210 |
case "Yes": |
| 211 |
go func() { |
| 212 |
defer log.PanicHandler() |
| 213 |
|
| 214 |
buf := bytes.NewBuffer([]byte{}) |
| 215 |
wr := multipart.NewWriter(buf) |
| 216 |
|
| 217 |
for dat := range slices.Values(postData) { |
| 218 |
header := strings.SplitN(dat, "=", 2) |
| 219 |
if len(header) < 2 { |
| 220 |
header = append(header, "") |
| 221 |
} |
| 222 |
_ = wr.WriteField(header[0], header[1]) // can't reasonably fail |
| 223 |
} |
| 224 |
|
| 225 |
data, err := http.Post(u.String(), "multipart/form-data", buf) |
| 226 |
if err != nil { |
| 227 |
app.PushError(fmt.Sprintf("Unsubscribe: failed to POST data: %v", err)) |
| 228 |
return |
| 229 |
} |
| 230 |
|
| 231 |
responseData, err := io.ReadAll(data.Body) |
| 232 |
response := string(responseData) |
| 233 |
if err != nil { |
| 234 |
response = fmt.Sprintf("failed to read response-data: %v", err) |
| 235 |
} |
| 236 |
|
| 237 |
body := fmt.Sprintf( |
| 238 |
"Success: %s\nReceived data:\n%s", |
| 239 |
data.Status, |
| 240 |
response, |
| 241 |
) |
| 242 |
|
| 243 |
confirmation := app.NewSelectorDialog( |
| 244 |
fmt.Sprintf("Sent request. Status %d", data.StatusCode), |
| 245 |
body, |
| 246 |
[]string{"OK"}, 0, app.SelectedAccountUiConfig(), |
| 247 |
func(_ string, _ error) { app.CloseDialog() }, |
| 248 |
) |
| 249 |
app.AddDialog(confirmation) |
| 250 |
}() |
| 251 |
case "Open in Browser": |
| 252 |
go func() { |
| 253 |
defer log.PanicHandler() |
| 254 |
mime := fmt.Sprintf("x-scheme-handler/%s", u.Scheme) |
| 255 |
if err := lib.XDGOpenMime(u.String(), mime, ""); err != nil { |
| 256 |
app.PushError("Unsubscribe:" + err.Error()) |
| 257 |
} |
| 258 |
}() |
| 259 |
default: |
| 260 |
app.PushError("Unsubscribe: link will not be opened") |
| 261 |
} |
| 262 |
}, |
| 263 |
) |
| 264 |
app.AddDialog(confirm) |
| 265 |
return nil |
| 266 |
} |