| 1 |
package compose |
| 2 |
|
| 3 |
import ( |
| 4 |
"bytes" |
| 5 |
"context" |
| 6 |
"slices" |
| 7 |
"time" |
| 8 |
|
| 9 |
"github.com/pkg/errors" |
| 10 |
|
| 11 |
"git.sr.ht/~rjarry/aerc/app" |
| 12 |
"git.sr.ht/~rjarry/aerc/commands" |
| 13 |
"git.sr.ht/~rjarry/aerc/lib/log" |
| 14 |
"git.sr.ht/~rjarry/aerc/models" |
| 15 |
"git.sr.ht/~rjarry/aerc/worker/types" |
| 16 |
) |
| 17 |
|
| 18 |
type Postpone struct { |
| 19 |
Folder string `opt:"-t" complete:"CompleteFolder" desc:"Override the target folder."` |
| 20 |
} |
| 21 |
|
| 22 |
func init() { |
| 23 |
commands.Register(Postpone{}) |
| 24 |
} |
| 25 |
|
| 26 |
func (Postpone) Description() string { |
| 27 |
return "Save the current state of the message to the postpone folder." |
| 28 |
} |
| 29 |
|
| 30 |
func (Postpone) Context() commands.CommandContext { |
| 31 |
return commands.COMPOSE_REVIEW |
| 32 |
} |
| 33 |
|
| 34 |
func (Postpone) Aliases() []string { |
| 35 |
return []string{"postpone"} |
| 36 |
} |
| 37 |
|
| 38 |
func (*Postpone) CompleteFolder(arg string) []string { |
| 39 |
return commands.GetFolders(arg) |
| 40 |
} |
| 41 |
|
| 42 |
func (p Postpone) Execute(args []string) error { |
| 43 |
acct := app.SelectedAccount() |
| 44 |
if acct == nil { |
| 45 |
return errors.New("No account selected") |
| 46 |
} |
| 47 |
store := acct.Store() |
| 48 |
if store == nil { |
| 49 |
return errors.New("No message store selected") |
| 50 |
} |
| 51 |
tab := app.SelectedTab() |
| 52 |
if tab == nil { |
| 53 |
return errors.New("No tab selected") |
| 54 |
} |
| 55 |
composer, _ := tab.Content.(*app.Composer) |
| 56 |
config := composer.Config() |
| 57 |
tabName := tab.Name |
| 58 |
|
| 59 |
targetFolder := config.Postpone |
| 60 |
if composer.RecalledFrom() != "" { |
| 61 |
targetFolder = composer.RecalledFrom() |
| 62 |
} |
| 63 |
if p.Folder != "" { |
| 64 |
targetFolder = p.Folder |
| 65 |
} |
| 66 |
if targetFolder == "" { |
| 67 |
return errors.New("No Postpone location configured") |
| 68 |
} |
| 69 |
|
| 70 |
log.Tracef("Postponing mail") |
| 71 |
|
| 72 |
header, err := composer.PrepareHeader() |
| 73 |
if err != nil { |
| 74 |
return errors.Wrap(err, "PrepareHeader") |
| 75 |
} |
| 76 |
header.SetContentType("text/plain", map[string]string{"charset": "UTF-8"}) |
| 77 |
header.Set("Content-Transfer-Encoding", "quoted-printable") |
| 78 |
worker := composer.Worker() |
| 79 |
dirs := acct.Directories().List() |
| 80 |
alreadyCreated := slices.Contains(dirs, targetFolder) |
| 81 |
|
| 82 |
errChan := make(chan string) |
| 83 |
|
| 84 |
// run this as a goroutine so we can make other progress. The message |
| 85 |
// will be saved once the directory is created. |
| 86 |
go func() { |
| 87 |
defer log.PanicHandler() |
| 88 |
|
| 89 |
errStr := <-errChan |
| 90 |
if errStr != "" { |
| 91 |
app.PushError(errStr) |
| 92 |
return |
| 93 |
} |
| 94 |
|
| 95 |
handleErr := func(err error) { |
| 96 |
app.PushError(err.Error()) |
| 97 |
log.Errorf("Postponing failed: %v", err) |
| 98 |
app.NewTab(composer, tabName) |
| 99 |
} |
| 100 |
|
| 101 |
app.RemoveTab(composer, false) |
| 102 |
buf := &bytes.Buffer{} |
| 103 |
|
| 104 |
err = composer.WriteMessage(header, buf) |
| 105 |
if err != nil { |
| 106 |
handleErr(errors.Wrap(err, "WriteMessage")) |
| 107 |
return |
| 108 |
} |
| 109 |
store.Append( |
| 110 |
targetFolder, |
| 111 |
models.SeenFlag|models.DraftFlag, |
| 112 |
time.Now(), |
| 113 |
buf, |
| 114 |
buf.Len(), |
| 115 |
func(msg types.WorkerMessage) { |
| 116 |
switch msg := msg.(type) { |
| 117 |
case *types.Done: |
| 118 |
app.PushStatus("Message postponed.", 10*time.Second) |
| 119 |
composer.SetPostponed() |
| 120 |
composer.Close() |
| 121 |
case *types.Error: |
| 122 |
handleErr(msg.Error) |
| 123 |
} |
| 124 |
}, |
| 125 |
) |
| 126 |
}() |
| 127 |
|
| 128 |
if !alreadyCreated { |
| 129 |
// to synchronise the creating of the directory |
| 130 |
worker.PostAction(context.TODO(), &types.CreateDirectory{ |
| 131 |
Directory: targetFolder, |
| 132 |
}, func(msg types.WorkerMessage) { |
| 133 |
switch msg := msg.(type) { |
| 134 |
case *types.Done: |
| 135 |
errChan <- "" |
| 136 |
case *types.Error: |
| 137 |
errChan <- msg.Error.Error() |
| 138 |
} |
| 139 |
}) |
| 140 |
} else { |
| 141 |
errChan <- "" |
| 142 |
} |
| 143 |
|
| 144 |
return nil |
| 145 |
} |