aboutsummaryrefslogtreecommitdiffstats
path: root/commands/compose/postpone.go
blob: 7aba6b188e5594d2b0e4c018e37394153671f903 (plain) (blame)
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
package compose

import (
	"bytes"
	"time"

	"github.com/pkg/errors"

	"git.sr.ht/~rjarry/aerc/app"
	"git.sr.ht/~rjarry/aerc/commands"
	"git.sr.ht/~rjarry/aerc/lib/log"
	"git.sr.ht/~rjarry/aerc/models"
	"git.sr.ht/~rjarry/aerc/worker/types"
)

type Postpone struct {
	Folder string `opt:"-t" complete:"CompleteFolder" desc:"Override the target folder."`
}

func init() {
	commands.Register(Postpone{})
}

func (Postpone) Description() string {
	return "Save the current state of the message to the postpone folder."
}

func (Postpone) Context() commands.CommandContext {
	return commands.COMPOSE
}

func (Postpone) Aliases() []string {
	return []string{"postpone"}
}

func (*Postpone) CompleteFolder(arg string) []string {
	return commands.GetFolders(arg)
}

func (p Postpone) 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("No message store selected")
	}
	tab := app.SelectedTab()
	if tab == nil {
		return errors.New("No tab selected")
	}
	composer, _ := tab.Content.(*app.Composer)
	config := composer.Config()
	tabName := tab.Name

	targetFolder := config.Postpone
	if composer.RecalledFrom() != "" {
		targetFolder = composer.RecalledFrom()
	}
	if p.Folder != "" {
		targetFolder = p.Folder
	}
	if targetFolder == "" {
		return errors.New("No Postpone location configured")
	}

	log.Tracef("Postponing mail")

	header, err := composer.PrepareHeader()
	if err != nil {
		return errors.Wrap(err, "PrepareHeader")
	}
	header.SetContentType("text/plain", map[string]string{"charset": "UTF-8"})
	header.Set("Content-Transfer-Encoding", "quoted-printable")
	worker := composer.Worker()
	dirs := acct.Directories().List()
	alreadyCreated := false
	for _, dir := range dirs {
		if dir == targetFolder {
			alreadyCreated = true
			break
		}
	}

	errChan := make(chan string)

	// run this as a goroutine so we can make other progress. The message
	// will be saved once the directory is created.
	go func() {
		defer log.PanicHandler()

		errStr := <-errChan
		if errStr != "" {
			app.PushError(errStr)
			return
		}

		handleErr := func(err error) {
			app.PushError(err.Error())
			log.Errorf("Postponing failed: %v", err)
			app.NewTab(composer, tabName)
		}

		app.RemoveTab(composer, false)
		buf := &bytes.Buffer{}

		err = composer.WriteMessage(header, buf)
		if err != nil {
			handleErr(errors.Wrap(err, "WriteMessage"))
			return
		}
		store.Append(
			targetFolder,
			models.SeenFlag|models.DraftFlag,
			time.Now(),
			buf,
			buf.Len(),
			func(msg types.WorkerMessage) {
				switch msg := msg.(type) {
				case *types.Done:
					app.PushStatus("Message postponed.", 10*time.Second)
					composer.SetPostponed()
					composer.Close()
				case *types.Error:
					handleErr(msg.Error)
				}
			},
		)
	}()

	if !alreadyCreated {
		// to synchronise the creating of the directory
		worker.PostAction(&types.CreateDirectory{
			Directory: targetFolder,
		}, func(msg types.WorkerMessage) {
			switch msg := msg.(type) {
			case *types.Done:
				errChan <- ""
			case *types.Error:
				errChan <- msg.Error.Error()
			}
		})
	} else {
		errChan <- ""
	}

	return nil
}