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