1
0
Fork 0
mirror of https://git.sr.ht/~rjarry/aerc synced 2026-01-02 00:31:13 +01:00
aerc/commands/msg/invite.go
Robin Jarry 51fd25c0f1 reload: fix crash when reloading via IPC
When reloading the configuration with :reload, global variables in the
config package are reset to their startup values and then, the config is
parsed from disk. While the parsing is done, these variables are
temporarily in an inconsistent and possibly invalid state.

When commands are executed interactively from aerc, they are handled by
the main goroutine which also deals with UI rendering. No UI render will
be done while :reload is in progress.

However, the IPC socket handler runs in an independent goroutine. This
has the unfortunate side effect to let the UI goroutine to run while
config parsing is in progress and causes crashes:

[signal SIGSEGV: segmentation violation code=0x1 addr=0x0 pc=0x6bb142]

goroutine 1 [running]:
git.sr.ht/~rjarry/aerc/lib/log.PanicHandler()
	lib/log/panic-logger.go:51 +0x6cf
panic({0xc1d960?, 0x134a6e0?})
	/usr/lib/go/src/runtime/panic.go:783 +0x132
git.sr.ht/~rjarry/aerc/config.(*StyleConf).getStyle(0xc00038b908?, 0x4206b7?)
	config/style.go:386 +0x42
git.sr.ht/~rjarry/aerc/config.StyleSet.Get({0x0, 0x0, 0x0, {0x0, 0x0, 0x0}}, 0x421a65?, 0x0)
	config/style.go:408 +0x8b
git.sr.ht/~rjarry/aerc/config.(*UIConfig).GetStyle(...)
	config/ui.go:379
git.sr.ht/~rjarry/aerc/lib/ui.(*TabStrip).Draw(0xc000314700, 0xc000192230)
	lib/ui/tab.go:378 +0x15b
git.sr.ht/~rjarry/aerc/lib/ui.(*Grid).Draw(0xc000186fc0, 0xc0002c25f0)
	lib/ui/grid.go:126 +0x28e
git.sr.ht/~rjarry/aerc/app.(*Aerc).Draw(0x14b9f00, 0xc0002c25f0)
	app/aerc.go:192 +0x1fe
git.sr.ht/~rjarry/aerc/lib/ui.Render()
	lib/ui/ui.go:155 +0x16b
main.main()
	main.go:310 +0x997

Make the reload operation safe by changing how config objects are
exposed and updated. Change all objects to be atomic pointers. Expose
public functions to access their value atomically. Only update their
value after a complete and successful config parse. This way the UI
thread will always have access to a valid configuration.

NB: The account configuration is not included in this change since it
cannot be reloaded.

Fixes: https://todo.sr.ht/~rjarry/aerc/319
Reported-by: Anachron <gith@cron.world>
Signed-off-by: Robin Jarry <robin@jarry.cc>
2025-09-23 14:02:37 +02:00

183 lines
4.3 KiB
Go

package msg
import (
"errors"
"fmt"
"io"
"time"
"git.sr.ht/~rjarry/aerc/app"
"git.sr.ht/~rjarry/aerc/commands"
"git.sr.ht/~rjarry/aerc/config"
"git.sr.ht/~rjarry/aerc/lib"
"git.sr.ht/~rjarry/aerc/lib/calendar"
"git.sr.ht/~rjarry/aerc/lib/format"
"git.sr.ht/~rjarry/aerc/lib/log"
"git.sr.ht/~rjarry/aerc/models"
"github.com/emersion/go-message/mail"
)
type invite struct {
Edit bool `opt:"-e" desc:"Force [compose].edit-headers = true."`
NoEdit bool `opt:"-E" desc:"Force [compose].edit-headers = false."`
SkipEditor bool `opt:"-s" desc:"Skip the editor and go directly to the review screen."`
}
func init() {
commands.Register(invite{})
}
func (invite) Description() string {
return "Accept or decline a meeting invitation."
}
func (invite) Context() commands.CommandContext {
return commands.MESSAGE_LIST | commands.MESSAGE_VIEWER
}
func (invite) Aliases() []string {
return []string{"accept", "accept-tentative", "decline"}
}
func (i invite) Execute(args []string) error {
acct := app.SelectedAccount()
if acct == nil {
return errors.New("no account selected")
}
store := acct.Store()
if store == nil {
return errors.New("cannot perform action: messages still loading")
}
msg, err := acct.SelectedMessage()
if err != nil {
return err
}
part := lib.FindCalendartext(msg.BodyStructure, nil)
if part == nil {
return fmt.Errorf("no invitation found (missing text/calendar)")
}
editHeaders := (config.Compose().EditHeaders || i.Edit) && !i.NoEdit
subject := trimLocalizedRe(msg.Envelope.Subject, acct.AccountConfig().LocalizedRe)
switch args[0] {
case "accept":
subject = "Accepted: " + subject
case "accept-tentative":
subject = "Tentatively Accepted: " + subject
case "decline":
subject = "Declined: " + subject
default:
return fmt.Errorf("no participation status defined")
}
from, err := chooseFromAddr(acct.AccountConfig(), msg)
if err != nil {
return err
}
var to []*mail.Address
if len(msg.Envelope.ReplyTo) != 0 {
to = msg.Envelope.ReplyTo
} else {
to = msg.Envelope.From
}
if !config.Compose().ReplyToSelf {
for i, v := range to {
if v.Address == from.Address {
to = append(to[:i], to[i+1:]...)
break
}
}
if len(to) == 0 {
to = msg.Envelope.To
}
}
recSet := newAddrSet() // used for de-duping
recSet.AddList(to)
h := &mail.Header{}
h.SetAddressList("from", []*mail.Address{from})
h.SetSubject(subject)
h.SetMsgIDList("in-reply-to", []string{msg.Envelope.MessageId})
err = setReferencesHeader(h, msg.RFC822Headers)
if err != nil {
app.PushError(fmt.Sprintf("could not set references: %v", err))
}
original := models.OriginalMail{
From: format.FormatAddresses(msg.Envelope.From),
Date: msg.Envelope.Date,
RFC822Headers: msg.RFC822Headers,
}
handleInvite := func(reader io.Reader) (*calendar.Reply, error) {
cr, err := calendar.CreateReply(reader, from, args[0])
if err != nil {
return nil, err
}
for _, org := range cr.Organizers {
organizer, err := mail.ParseAddress(org)
if err != nil {
continue
}
if !recSet.Contains(organizer) {
to = append(to, organizer)
}
}
h.SetAddressList("to", to)
return cr, nil
}
addTab := func(cr *calendar.Reply) error {
composer, err := app.NewComposer(acct,
acct.AccountConfig(), acct.Worker(), editHeaders,
"", h, &original, cr.PlainText)
if err != nil {
app.PushError("Error: " + err.Error())
return err
}
err = composer.AppendPart(cr.MimeType, cr.Params, cr.CalendarText)
if err != nil {
return fmt.Errorf("failed to write invitation: %w", err)
}
if i.SkipEditor {
composer.Terminal().Close()
} else {
composer.FocusTerminal()
}
composer.Tab = app.NewTab(composer, subject)
composer.OnClose(func(c *app.Composer) {
switch {
case c.Sent() && c.Archive() != "":
store.Answered([]models.UID{msg.Uid}, true, nil)
err := archive([]*models.MessageInfo{msg}, nil, c.Archive())
if err != nil {
app.PushStatus("Archive failed", 10*time.Second)
}
case c.Sent():
store.Answered([]models.UID{msg.Uid}, true, nil)
}
})
return nil
}
store.FetchBodyPart(msg.Uid, part, func(reader io.Reader) {
if cr, err := handleInvite(reader); err != nil {
app.PushError(err.Error())
return
} else {
err := addTab(cr)
if err != nil {
log.Warnf("failed to add tab: %v", err)
}
}
})
return nil
}