aboutsummaryrefslogtreecommitdiffstats
path: root/cmd/run.go
blob: 4ba7b165a4f15585e0a2fa0eb5986bbe93177701 (plain)
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
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
/* run.go: send pending messages
 *
 * Copyright (C) 2016 Clemens Fries <github-clockrotz@xenoworld.de>
 *
 * This program is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
 */
package cmd

import (
	"crypto/tls"
	"fmt"
	"github.com/docopt/docopt.go"
	. "github.com/githubert/clockrotz/common"
	"github.com/jordan-wright/email"
	"net/mail"
	"net/textproto"
	"os"
	"path/filepath"
	"strings"
	"time"
)

var usageRun =
// tag::run[]
`
Usage:
  clockrotz run [options]

Options:
  --help             Show this help.
  --to=ADDR          Destination address.
  --from=ADDR        Sender address.
  --subject=ADDR     Short subject.
  --cc=ADDR          Set "Cc".
  --bcc=ADDR         Set "Bcc".
  --reply-to=ADDR    Set "Reply-To".
  --not-before=TIME  Not before TIME. (default: 00:00)
  --not-after=TIME   Not after TIME. (default: 23:59)
  --server=HOST      SMTP hostname. (default: localhost)
  --port=PORT        SMTP port. (default: 587)
  --verbose          Report on successfully sent messages.
  --dry-run          Do not send the message.
  --insecure         Accept any TLS certificate.
` // end::run[]

func Run(argv []string, conf *Configuration) {
	args, _ := docopt.Parse(usageRun, argv, true, "", false)
	conf.MergeWithDocOptArgs(CMD_RUN, &args)

	verbose := args["--verbose"].(bool)
	dryRun := args["--dry-run"].(bool)
	insecure := args["--insecure"].(bool)

	now := time.Now()

	notBeforeTime, err := time.Parse(TIME_FORMAT, conf.Get(CONF_NOT_BEFORE))

	if err != nil {
		fmt.Printf("Failed parsing not-before time: %s\n", err.Error())
		return
	}

	notAfterTime, err := time.Parse(TIME_FORMAT, conf.Get(CONF_NOT_AFTER))

	if err != nil {
		fmt.Printf("Failed parsing not-after time: %s\n", err.Error())
		return
	}

	notBefore := buildTime(now, notBeforeTime.Hour(), notBeforeTime.Minute(), true)
	notAfter := buildTime(now, notAfterTime.Hour(), notAfterTime.Minute(), false)

	// Return if we are in some quiet period.
	if now.After(notAfter) || now.Before(notBefore) {
		return
	}

	messages := NewMessagesFromDirectory(filepath.Join(conf.Get(CONF_WORKDIR), DIR_TODO))

	verificationError := false

	for _, message := range messages {
		message.Conf.MergeWith(conf)
		err := processMessage(message, now, dryRun, insecure, verbose)

		if err != nil {
			verificationError = true
			continue
		}
	}

	if verificationError {
		// FIXME: This suggests that other messages were not sent, but they were....
		fmt.Println("There were errors when verifying one or more messages.")
		fmt.Println("Please run 'clockrotz check'")
		os.Exit(1)
	}
}

func processMessage(message Message, now time.Time, dryRun, insecure, verbose bool) error {
	if errs := message.Verify(); errs != nil {
		return fmt.Errorf("Message %s failed verification.", message.Name)
	}

	date, err := ParseTime(message.Get("date"))

	if err != nil {
		return err
	}

	if !now.After(date) {
		return nil
	}

	sendErr := sendMessage(message, dryRun, insecure)

	if sendErr != nil {
		if !dryRun {
			err := moveMessage(message, DIR_ERRORS)

			if err != nil {
				fmt.Printf("Error when moving message %s: %s\n", message.Name, err.Error())
			}

			logMessage(message, DIR_ERRORS, sendErr.Error())
		}

		fmt.Printf("Error when sending message %s: %s\n", message.Name, sendErr.Error())
	} else {
		if !dryRun {
			err := moveMessage(message, DIR_DONE)

			if err != nil {
				fmt.Printf("Error when moving message %s: %s\n", message.Name, err.Error())
			}

			logMessage(message, DIR_DONE, "Successfully delivered.")
		}

		if verbose {
			fmt.Printf("Message %s delivered.\n", message.Name)
		}
	}

	return nil // TODO: what about errors that are not verification errors?
}

func logMessage(message Message, dir string, logMessage string) {
	dstDir := filepath.Join(message.Get(CONF_WORKDIR), dir)
	filename := filepath.Join(dstDir, message.Name[:len(message.Name)-len(".msg")]+".log")

	f, err := os.Create(filename)

	if err != nil {
		fmt.Printf("Error when creating log file: %s\n", err.Error())
		return
	}

	defer f.Close()

	f.WriteString("Log message:\n")
	f.WriteString(fmt.Sprintf("  %s\n", logMessage))
	f.WriteString("\n")

	f.WriteString("\nConfiguration:\n")
	for _, s := range message.Conf.DumpConfig() {
		f.WriteString(fmt.Sprintf("  %s\n", s))
	}

	f.WriteString("\nBody:\n")
	for _, s := range message.Body {
		f.WriteString(fmt.Sprintf("  %s\n", s))
	}
}

// Turn the given plain address list string into an array.
func getAddresses(addressList string) ([]string, error) {
	addresses, err := mail.ParseAddressList(addressList)

	if err != nil {
		return nil, err
	}

	result := []string{}

	for _, address := range addresses {
		result = append(result, address.String())
	}

	return result, nil
}

// Prepare a ready-to-send Email message.
func prepareEmail(message *Message) (*email.Email, error) {
	e := email.NewEmail()
	e.From = message.Get(CONF_FROM)
	e.Subject = message.Get(CONF_SUBJECT)

	// Build list of To addresses.
	to, err := getAddresses(message.Get(CONF_TO))

	if err != nil {
		return nil, err
	}

	e.To = to

	// Set optional Reply-To header.
	if r := message.Get(CONF_REPLY_TO); r != "" {
		replyTo, err := getAddresses(r)

		if err != nil {
			return nil, err
		}

		e.Headers = textproto.MIMEHeader{"Reply-To": replyTo}
	}

	// Build list of Cc addresses.
	if r := message.Get(CONF_CC); r != "" {
		cc, err := getAddresses(r)

		if err != nil {
			return nil, err
		}

		e.Cc = cc
	}

	// Build list of Bcc addresses.
	if r := message.Get(CONF_BCC); r != "" {
		bcc, err := getAddresses(r)

		if err != nil {
			return nil, err
		}

		e.Bcc = bcc
	}

	e.Text = []byte(strings.Join(message.Body, "\n"))

	return e, nil
}

// Send the given message, unless `dryRun` is true. Use `insecure` to work
// around things like self-signed certificates.
func sendMessage(message Message, dryRun bool, insecure bool) error {
	e, err := prepareEmail(&message)

	if err != nil {
		return err
	}

	smtpServer := message.Get(CONF_SMTP_SERVER) + ":" + message.Get(CONF_SMTP_PORT)

	if dryRun {
		fmt.Printf("Skip sending message %s through %s.\n", message.Name, smtpServer)
		return nil
	}

	if insecure {
		return e.SendWithTLS(smtpServer, nil, &tls.Config{InsecureSkipVerify: true})
	} else {
		return e.Send(smtpServer, nil)
	}
}

// Move the given message to a folder relative to the working directory.
func moveMessage(message Message, relative string) error {
	todo := filepath.Join(message.Get(CONF_WORKDIR), DIR_TODO)
	to := filepath.Join(message.Get(CONF_WORKDIR), relative)

	// FIXME: BUG: This will overwrite existing messages. Look at create.go:nextFreeFilename()
	//             for ideas on how to resolve this. We could try to use a similar approach.
	//             `foo.msg` to `foo.1.msg` and `foo.1.log`.

	return os.Rename(filepath.Join(todo, message.Name), filepath.Join(to, message.Name))
}

// Build a time.Time from some given base time. If floor is true, seconds will
// be set to 0, if false, 59.
// TODO: Maybe there is a better way?…
func buildTime(base time.Time, hour int, minute int, floor bool) time.Time {
	seconds := 0

	if !floor {
		// We ignore the possibility of leap seconds here, this is
		// just done so that we can get 23:59:59 instead of 23:59:00.
		// 23:59:00 would make us miss a whole minute. It would be
		// nice if there were a way to indicate to time.Date() to
		// build a date with start of the day / end of the day...
		seconds = 59
	}

	return time.Date(
		base.Year(),
		base.Month(),
		base.Day(),
		hour,
		minute,
		seconds,
		0,
		base.Location())
}