| 1 |
package msg |
| 2 |
|
| 3 |
import ( |
| 4 |
"context" |
| 5 |
"fmt" |
| 6 |
"io" |
| 7 |
"net/url" |
| 8 |
"strings" |
| 9 |
"time" |
| 10 |
|
| 11 |
"github.com/emersion/go-message/mail" |
| 12 |
"github.com/pkg/errors" |
| 13 |
|
| 14 |
"git.sr.ht/~rjarry/aerc/app" |
| 15 |
"git.sr.ht/~rjarry/aerc/commands" |
| 16 |
"git.sr.ht/~rjarry/aerc/commands/mode" |
| 17 |
"git.sr.ht/~rjarry/aerc/lib/log" |
| 18 |
"git.sr.ht/~rjarry/aerc/lib/send" |
| 19 |
"git.sr.ht/~rjarry/aerc/worker/types" |
| 20 |
) |
| 21 |
|
| 22 |
type Bounce struct { |
| 23 |
Account string `opt:"-A" complete:"CompleteAccount" desc:"Account from which to re-send the message."` |
| 24 |
To []string `opt:"..." required:"true" complete:"CompleteTo" desc:"Recipient from address book."` |
| 25 |
} |
| 26 |
|
| 27 |
func init() { |
| 28 |
commands.Register(Bounce{}) |
| 29 |
} |
| 30 |
|
| 31 |
func (Bounce) Description() string { |
| 32 |
return "Re-send the selected message(s) to the specified addresses." |
| 33 |
} |
| 34 |
|
| 35 |
func (Bounce) Aliases() []string { |
| 36 |
return []string{"bounce", "resend"} |
| 37 |
} |
| 38 |
|
| 39 |
func (*Bounce) CompleteAccount(arg string) []string { |
| 40 |
return commands.FilterList(app.AccountNames(), arg, nil) |
| 41 |
} |
| 42 |
|
| 43 |
func (*Bounce) CompleteTo(arg string) []string { |
| 44 |
return commands.FilterList(commands.GetAddress(arg), arg, nil) |
| 45 |
} |
| 46 |
|
| 47 |
func (Bounce) Context() commands.CommandContext { |
| 48 |
return commands.MESSAGE_LIST | commands.MESSAGE_VIEWER |
| 49 |
} |
| 50 |
|
| 51 |
func (b Bounce) Execute(args []string) error { |
| 52 |
if len(b.To) == 0 { |
| 53 |
return errors.New("No recipients specified") |
| 54 |
} |
| 55 |
addresses := strings.Join(b.To, ", ") |
| 56 |
|
| 57 |
app.PushStatus("Bouncing to "+addresses, 10*time.Second) |
| 58 |
|
| 59 |
widget := app.SelectedTabContent().(app.ProvidesMessage) |
| 60 |
|
| 61 |
var err error |
| 62 |
acct := widget.SelectedAccount() |
| 63 |
if b.Account != "" { |
| 64 |
acct, err = app.Account(b.Account) |
| 65 |
} |
| 66 |
switch { |
| 67 |
case err != nil: |
| 68 |
return fmt.Errorf("Failed to select account %q: %w", b.Account, err) |
| 69 |
case acct == nil: |
| 70 |
return errors.New("No account selected") |
| 71 |
} |
| 72 |
|
| 73 |
store := widget.Store() |
| 74 |
if store == nil { |
| 75 |
return errors.New("Cannot perform action. Messages still loading") |
| 76 |
} |
| 77 |
|
| 78 |
config := acct.AccountConfig() |
| 79 |
|
| 80 |
outgoing, err := config.Outgoing.ConnectionString() |
| 81 |
if err != nil { |
| 82 |
return errors.Wrap(err, "ReadCredentials()") |
| 83 |
} |
| 84 |
if outgoing == "" { |
| 85 |
return errors.New("No outgoing mail transport configured for this account") |
| 86 |
} |
| 87 |
uri, err := url.Parse(outgoing) |
| 88 |
if err != nil { |
| 89 |
return errors.Wrap(err, "url.Parse()") |
| 90 |
} |
| 91 |
|
| 92 |
rcpts, err := mail.ParseAddressList(addresses) |
| 93 |
if err != nil { |
| 94 |
return errors.Wrap(err, "ParseAddressList()") |
| 95 |
} |
| 96 |
|
| 97 |
var domain string |
| 98 |
if domain_, ok := config.Params["smtp-domain"]; ok { |
| 99 |
domain = domain_ |
| 100 |
} |
| 101 |
|
| 102 |
hostname, err := send.GetMessageIdHostname(config.SendWithHostname, config.From) |
| 103 |
if err != nil { |
| 104 |
return errors.Wrap(err, "GetMessageIdHostname()") |
| 105 |
} |
| 106 |
|
| 107 |
// According to RFC2822, all of the resent fields corresponding |
| 108 |
// to a particular resending of the message SHOULD be together. |
| 109 |
// Each new set of resent fields is prepended to the message; |
| 110 |
// that is, the most recent set of resent fields appear earlier in the |
| 111 |
// message. |
| 112 |
headers := fmt.Sprintf("Resent-From: %s\r\n", config.From) |
| 113 |
headers += "Resent-Date: %s\r\n" |
| 114 |
headers += "Resent-Message-ID: <%s>\r\n" |
| 115 |
headers += fmt.Sprintf("Resent-To: %s\r\n", addresses) |
| 116 |
|
| 117 |
helper := newHelper() |
| 118 |
uids, err := helper.markedOrSelectedUids() |
| 119 |
if err != nil { |
| 120 |
return err |
| 121 |
} |
| 122 |
|
| 123 |
mode.NoQuit() |
| 124 |
|
| 125 |
marker := store.Marker() |
| 126 |
marker.ClearVisualMark() |
| 127 |
|
| 128 |
errCh := make(chan error) |
| 129 |
store.FetchFull(context.TODO(), uids, func(fm *types.FullMessage) { |
| 130 |
defer log.PanicHandler() |
| 131 |
|
| 132 |
var header mail.Header |
| 133 |
var msgId string |
| 134 |
var err, errClose error |
| 135 |
|
| 136 |
uid := fm.Content.Uid |
| 137 |
msg := store.Messages[uid] |
| 138 |
if msg == nil { |
| 139 |
errCh <- fmt.Errorf("no message info: %v", uid) |
| 140 |
return |
| 141 |
} |
| 142 |
if err = header.GenerateMessageIDWithHostname(hostname); err != nil { |
| 143 |
errCh <- errors.Wrap(err, "GenerateMessageIDWithHostname()") |
| 144 |
return |
| 145 |
} |
| 146 |
if msgId, err = header.MessageID(); err != nil { |
| 147 |
errCh <- errors.Wrap(err, "MessageID()") |
| 148 |
return |
| 149 |
} |
| 150 |
reader := strings.NewReader(fmt.Sprintf(headers, |
| 151 |
time.Now().Format(time.RFC1123Z), msgId)) |
| 152 |
|
| 153 |
go func() { |
| 154 |
defer log.PanicHandler() |
| 155 |
defer func() { errCh <- err }() |
| 156 |
|
| 157 |
var sender io.WriteCloser |
| 158 |
|
| 159 |
log.Debugf("Bouncing email <%s> to %s", |
| 160 |
msg.Envelope.MessageId, addresses) |
| 161 |
|
| 162 |
if sender, err = send.NewSender(acct.Worker(), uri, |
| 163 |
domain, config.From, rcpts, acct.Name(), nil, false); err != nil { |
| 164 |
return |
| 165 |
} |
| 166 |
defer func() { |
| 167 |
errClose = sender.Close() |
| 168 |
// If there has already been an error, |
| 169 |
// we don't want to clobber it. |
| 170 |
if err == nil { |
| 171 |
err = errClose |
| 172 |
} else if errClose != nil { |
| 173 |
app.PushError(errClose.Error()) |
| 174 |
} |
| 175 |
}() |
| 176 |
if _, err = io.Copy(sender, reader); err != nil { |
| 177 |
return |
| 178 |
} |
| 179 |
_, err = io.Copy(sender, fm.Content.Reader) |
| 180 |
}() |
| 181 |
}) |
| 182 |
|
| 183 |
go func() { |
| 184 |
defer log.PanicHandler() |
| 185 |
defer mode.NoQuitDone() |
| 186 |
|
| 187 |
var total, success int |
| 188 |
|
| 189 |
for err = range errCh { |
| 190 |
if err != nil { |
| 191 |
app.PushError(err.Error()) |
| 192 |
} else { |
| 193 |
success++ |
| 194 |
} |
| 195 |
total++ |
| 196 |
if total == len(uids) { |
| 197 |
break |
| 198 |
} |
| 199 |
} |
| 200 |
if success != total { |
| 201 |
marker.Remark() |
| 202 |
app.PushError(fmt.Sprintf("Failed to bounce %d of the messages", |
| 203 |
total-success)) |
| 204 |
} else { |
| 205 |
plural := "" |
| 206 |
if success > 1 { |
| 207 |
plural = "s" |
| 208 |
} |
| 209 |
app.PushStatus(fmt.Sprintf("Bounced %d message%s", |
| 210 |
success, plural), 10*time.Second) |
| 211 |
} |
| 212 |
}() |
| 213 |
|
| 214 |
return nil |
| 215 |
} |