| 1 |
package msg |
| 2 |
|
| 3 |
import ( |
| 4 |
"context" |
| 5 |
"errors" |
| 6 |
"fmt" |
| 7 |
"io" |
| 8 |
"time" |
| 9 |
|
| 10 |
"git.sr.ht/~rjarry/aerc/app" |
| 11 |
"git.sr.ht/~rjarry/aerc/commands" |
| 12 |
"git.sr.ht/~rjarry/aerc/config" |
| 13 |
"git.sr.ht/~rjarry/aerc/lib" |
| 14 |
"git.sr.ht/~rjarry/aerc/lib/calendar" |
| 15 |
"git.sr.ht/~rjarry/aerc/lib/format" |
| 16 |
"git.sr.ht/~rjarry/aerc/lib/log" |
| 17 |
"git.sr.ht/~rjarry/aerc/models" |
| 18 |
"github.com/emersion/go-message/mail" |
| 19 |
) |
| 20 |
|
| 21 |
type invite struct { |
| 22 |
Edit bool `opt:"-e" desc:"Force [compose].edit-headers = true."` |
| 23 |
NoEdit bool `opt:"-E" desc:"Force [compose].edit-headers = false."` |
| 24 |
SkipEditor bool `opt:"-s" desc:"Skip the editor and go directly to the review screen."` |
| 25 |
} |
| 26 |
|
| 27 |
func init() { |
| 28 |
commands.Register(invite{}) |
| 29 |
} |
| 30 |
|
| 31 |
func (invite) Description() string { |
| 32 |
return "Accept or decline a meeting invitation." |
| 33 |
} |
| 34 |
|
| 35 |
func (invite) Context() commands.CommandContext { |
| 36 |
return commands.MESSAGE_LIST | commands.MESSAGE_VIEWER |
| 37 |
} |
| 38 |
|
| 39 |
func (invite) Aliases() []string { |
| 40 |
return []string{"accept", "accept-tentative", "decline"} |
| 41 |
} |
| 42 |
|
| 43 |
func (i invite) Execute(args []string) error { |
| 44 |
acct := app.SelectedAccount() |
| 45 |
if acct == nil { |
| 46 |
return errors.New("no account selected") |
| 47 |
} |
| 48 |
store := acct.Store() |
| 49 |
if store == nil { |
| 50 |
return errors.New("cannot perform action: messages still loading") |
| 51 |
} |
| 52 |
msg, err := acct.SelectedMessage() |
| 53 |
if err != nil { |
| 54 |
return err |
| 55 |
} |
| 56 |
|
| 57 |
part := lib.FindCalendartext(msg.BodyStructure, nil) |
| 58 |
if part == nil { |
| 59 |
return fmt.Errorf("no invitation found (missing text/calendar)") |
| 60 |
} |
| 61 |
|
| 62 |
editHeaders := (config.Compose().EditHeaders || i.Edit) && !i.NoEdit |
| 63 |
|
| 64 |
subject := trimLocalizedRe(msg.Envelope.Subject, acct.AccountConfig().LocalizedRe) |
| 65 |
switch args[0] { |
| 66 |
case "accept": |
| 67 |
subject = "Accepted: " + subject |
| 68 |
case "accept-tentative": |
| 69 |
subject = "Tentatively Accepted: " + subject |
| 70 |
case "decline": |
| 71 |
subject = "Declined: " + subject |
| 72 |
default: |
| 73 |
return fmt.Errorf("no participation status defined") |
| 74 |
} |
| 75 |
|
| 76 |
from, err := chooseFromAddr(acct.AccountConfig(), msg) |
| 77 |
if err != nil { |
| 78 |
return err |
| 79 |
} |
| 80 |
|
| 81 |
var to []*mail.Address |
| 82 |
|
| 83 |
if len(msg.Envelope.ReplyTo) != 0 { |
| 84 |
to = msg.Envelope.ReplyTo |
| 85 |
} else { |
| 86 |
to = msg.Envelope.From |
| 87 |
} |
| 88 |
|
| 89 |
if !config.Compose().ReplyToSelf { |
| 90 |
for i, v := range to { |
| 91 |
if v.Address == from.Address { |
| 92 |
to = append(to[:i], to[i+1:]...) |
| 93 |
break |
| 94 |
} |
| 95 |
} |
| 96 |
if len(to) == 0 { |
| 97 |
to = msg.Envelope.To |
| 98 |
} |
| 99 |
} |
| 100 |
|
| 101 |
recSet := newAddrSet() // used for de-duping |
| 102 |
recSet.AddList(to) |
| 103 |
|
| 104 |
h := &mail.Header{} |
| 105 |
h.SetAddressList("from", []*mail.Address{from}) |
| 106 |
h.SetSubject(subject) |
| 107 |
h.SetMsgIDList("in-reply-to", []string{msg.Envelope.MessageId}) |
| 108 |
err = setReferencesHeader(h, msg.RFC822Headers) |
| 109 |
if err != nil { |
| 110 |
app.PushError(fmt.Sprintf("could not set references: %v", err)) |
| 111 |
} |
| 112 |
original := models.OriginalMail{ |
| 113 |
From: format.FormatAddresses(msg.Envelope.From), |
| 114 |
Date: msg.Envelope.Date, |
| 115 |
RFC822Headers: msg.RFC822Headers, |
| 116 |
} |
| 117 |
|
| 118 |
handleInvite := func(reader io.Reader) (*calendar.Reply, error) { |
| 119 |
cr, err := calendar.CreateReply(reader, from, args[0]) |
| 120 |
if err != nil { |
| 121 |
return nil, err |
| 122 |
} |
| 123 |
for _, org := range cr.Organizers { |
| 124 |
organizer, err := mail.ParseAddress(org) |
| 125 |
if err != nil { |
| 126 |
continue |
| 127 |
} |
| 128 |
if !recSet.Contains(organizer) { |
| 129 |
to = append(to, organizer) |
| 130 |
} |
| 131 |
} |
| 132 |
h.SetAddressList("to", to) |
| 133 |
return cr, nil |
| 134 |
} |
| 135 |
|
| 136 |
addTab := func(cr *calendar.Reply) error { |
| 137 |
composer, err := app.NewComposer(acct, |
| 138 |
acct.AccountConfig(), acct.Worker(), editHeaders, |
| 139 |
"", h, &original, cr.PlainText) |
| 140 |
if err != nil { |
| 141 |
app.PushError("Error: " + err.Error()) |
| 142 |
return err |
| 143 |
} |
| 144 |
err = composer.AppendPart(cr.MimeType, cr.Params, cr.CalendarText) |
| 145 |
if err != nil { |
| 146 |
return fmt.Errorf("failed to write invitation: %w", err) |
| 147 |
} |
| 148 |
if i.SkipEditor { |
| 149 |
composer.Terminal().Close() |
| 150 |
} else { |
| 151 |
composer.FocusTerminal() |
| 152 |
} |
| 153 |
|
| 154 |
composer.Tab = app.NewTab(composer, subject) |
| 155 |
|
| 156 |
composer.OnClose(func(c *app.Composer) { |
| 157 |
switch { |
| 158 |
case c.Sent() && c.Archive() != "": |
| 159 |
store.Answered([]models.UID{msg.Uid}, true, nil) |
| 160 |
err := archive([]*models.MessageInfo{msg}, nil, c.Archive(), acct, store) |
| 161 |
if err != nil { |
| 162 |
app.PushStatus("Archive failed", 10*time.Second) |
| 163 |
} |
| 164 |
case c.Sent(): |
| 165 |
store.Answered([]models.UID{msg.Uid}, true, nil) |
| 166 |
} |
| 167 |
}) |
| 168 |
|
| 169 |
return nil |
| 170 |
} |
| 171 |
|
| 172 |
store.FetchBodyPart(context.TODO(), msg.Uid, part, func(reader io.Reader) { |
| 173 |
if cr, err := handleInvite(reader); err != nil { |
| 174 |
app.PushError(err.Error()) |
| 175 |
return |
| 176 |
} else { |
| 177 |
err := addTab(cr) |
| 178 |
if err != nil { |
| 179 |
log.Warnf("failed to add tab: %v", err) |
| 180 |
} |
| 181 |
} |
| 182 |
}) |
| 183 |
return nil |
| 184 |
} |