/
secretsanta.go
433 lines (365 loc) · 9.92 KB
/
secretsanta.go
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
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
package main
import (
"fmt"
"math/rand"
"bufio"
"strings"
"errors"
"os"
"net/smtp"
"sync"
"time"
"sort"
"strconv"
)
// Format of input file (named santa.txt):
// Designated by four sections. Each section is defined by a set of newline-delimited data.
// Each section ends with at least one empty line. In addition, the first section may optionally
// be preceeded by an arbitrary amount of blank lines.
//
// Section one:
// Contains four lines:
// 1. Comma separated values for authentification of the sender of the emails:
// identity,username,password,host
// Note: Because it is csv list, none of the values may contain a comma
// 2. Comma separated values for email information:
// server,from
// 3. The subject of the email
// 4. A generic message for the email, where the following macros are supported:
// %1: Name of gift sender
// %2: Email of gift sender
// %3: Name of gift receiver
// Section two:
// Contains one line: the seed of the random matching. Must fit into a int64.
// Section three:
// A list of people in the secret santa in the following format:
// name:emailaddress
// Note: Names must not contain commas
// Section four:
// A list of sets of people who may not receive each other as matchings. Each set is
// represented by a csv list. Note that names must be (almost) exactly the same as they
// appear in section three. The almost comes from the fact that in this section, names
// are run through strings.Title first.
//
// This section is optional.
// If set to true, the emailer will instead send all of the matching emails to itself. Use
// this to test that you set up section four correctly.
const DRY_RUN = false
type Input struct {
sc *bufio.Scanner
atLines bool
}
func (i*Input) Next() (string, bool) {
for i.sc.Scan() {
line := strings.Trim(i.sc.Text(), " \t")
if strings.HasPrefix(line, "//") {
continue
}
if len(line) > 0 {
i.atLines = true
return line, true
}
if i.atLines {
i.atLines = false
return "", false
}
// we weren't at lines already, so keep reading until we get some lines
}
return "", false
}
func (i*Input) Err() error {
return i.sc.Err()
}
type Person struct {
name string
email string
good []*Person
}
func (p*Person) String() string {
return p.name
}
func readInPeople(in *Input) (map[string]*Person, error) {
ret := make(map[string]*Person)
for line, ok := in.Next(); ok; line, ok = in.Next(){
colon := strings.Index(line, ":")
if colon == -1 {
return nil, errors.New(fmt.Sprint("Couldn't find email address in", line))
}
name, email := strings.Trim(line[:colon], " \t"), strings.Trim(line[colon+1:], " \t")
ret[name] = &Person{
name: name,
email: email,
good: nil,
}
}
if err := in.Err(); err != nil {
return nil, err
}
return ret, nil
}
type B struct{}
type BadSet struct {
pbad map[*Person]B
}
func (b *BadSet) AddPerson(p *Person) {
b.pbad[p] = B{}
}
func (b *BadSet) IsIn(p *Person) bool {
_, ok := b.pbad[p]
return ok
}
func (b *BadSet) AddAll(o *BadSet) {
for p,_ := range o.pbad {
b.pbad[p] = B{}
}
}
func NewBadSet() *BadSet {
return &BadSet {
pbad: make(map[*Person]B),
}
}
func (b *BadSet) String() string {
return fmt.Sprint(b.pbad)
}
func readInBadSets(in *Input, people map[string]*Person) ([]*BadSet, error) {
ret := make([]*BadSet, 0, 10)
for line, ok := in.Next(); ok; line, ok = in.Next() {
set := NewBadSet()
ret = append(ret, set)
for _, p := range strings.Split(line, ",") {
p = strings.Title(p)
set.AddPerson(people[p])
}
}
if err := in.Err(); err != nil {
return nil, err
}
return ret, nil
}
func compileBadSet(bads []*BadSet) map[*Person]*BadSet {
ret := make(map[*Person]*BadSet)
for _,v := range bads {
for p,_ := range v.pbad {
b, ok := ret[p]
if !ok {
b = NewBadSet()
ret[p] = b
}
b.AddAll(v)
}
}
return ret
}
func makeDomain(cur *Person, all map[string]*Person, bad *BadSet) []*Person {
ret := make([]*Person, 0, len(all)/2)
for _,v := range all {
if bad == nil {
// Didn't belong to any badsets, so just make sure they can't get themself
if v == cur {
continue
}
} else if bad.IsIn(v) {
// Only add them to the set if the person isn't in the badset
continue
}
ret = append(ret, v)
}
return ret
}
type People []*Person
func (p People) Len() int { return len(p) }
func (p People) Swap(i, j int) { p[i], p[j] = p[j], p[i] }
func (p People) Less(i, j int) bool { return p[i].name < p[j].name }
func shuffle(l []*Person, r *rand.Rand) {
// First sort them so that they are in a deterministic order so that the random assignment can be re-encountered if needed
sort.Sort(People(l))
for i := int32(len(l) - 1); i > 0; i-- {
j := r.Int31n(i + 1)
l[i], l[j] = l[j], l[i]
}
}
func buildDomains(people map[string]*Person, bad []*BadSet, r *rand.Rand) []*Person {
badMap := compileBadSet(bad)
ret := make([]*Person, len(people))
i := 0
for _,v := range people {
ret[i] = v
i++
}
// Sort so that we have deterministic output
sort.Sort(People(ret))
for _,v := range ret {
v.good = makeDomain(v, people, badMap[v])
shuffle(v.good, r)
fmt.Println("Domain for", v, ":", v.good)
}
return ret
}
func solve(all []*Person, cur int, assigned map[*Person]B) map[*Person]*Person {
if cur == len(all) {
// Got to the end without problems!
return make(map[*Person]*Person, len(all))
}
c := all[cur]
next := cur + 1
for _,try := range c.good {
if _,ok := assigned[try]; ok {
// already assigned
continue
}
// Try to assign this one
assigned[try] = B{}
if r := solve(all, next, assigned); r != nil {
// This assignment was correct! Record our match and return
r[c] = try
return r
} // else: guess caused problems. Try again.
delete(assigned, try)
}
// Domain is zero - no solution!
return nil
}
func makeRand(seed int64) *rand.Rand {
return rand.New(rand.NewSource(seed))
}
func main() {
f, err := os.Open("santa.txt")
if err != nil {
panic(err)
}
defer f.Close()
in := &Input{sc:bufio.NewScanner(f),}
emailer,err := readInEmailer(in)
if err != nil {
panic(err)
}
var seed int64
strSeed,_ := in.Next()
if seed, err = strconv.ParseInt(strSeed, 10, 64); err != nil {
panic(errors.New("Need a line designating the seed in the second section of the page" + err.Error()))
}
if _, b := in.Next(); b {
panic(errors.New("Extra line at the end of the seed section"))
}
peoples, err := readInPeople(in)
if err != nil {
panic(err)
}
fmt.Println("Found", len(peoples), "people")
rawbads, err := readInBadSets(in, peoples)
if err != nil {
panic(err)
}
people := buildDomains(peoples, rawbads, makeRand(seed))
if DRY_RUN {
fmt.Println("Doing a dry-run of matching!")
} else {
sc := bufio.NewScanner(os.Stdin)
fmt.Println("WARNING: This is about to send out emails to everyone involved in the secret santa event. Please make sure that you have permission to email all of the people in the santa.txt file and that you are meaning to do this.")
fmt.Println()
fmt.Println("To continue press enter. To cancel press ctrl-c.")
sc.Scan()
}
solution := solve(people, 0, make(map[*Person]B))
if solution == nil {
panic(errors.New("Could not find a solution with the given parameters!"))
}
// Shouldn't try to send too many emails at a time. Limit to four conncurrent sends.
out, wait := BatchSend(4, *emailer)
for k,v := range solution {
out <- Assignment{k, v}
}
close(out)
wait.Wait()
fmt.Println("Done!")
}
func BatchSend(workers int, e Emailer) (chan<-Assignment, *sync.WaitGroup) {
var done sync.WaitGroup
done.Add(workers)
ret := make(chan Assignment, 4)
ffunc := func() {
for ii := range ret {
k, v := ii.src, ii.dest
for {
err := e.sendAssignment(k, v)
if err != nil {
fmt.Println("Error sending assignment to", k, ":", err)
<-time.After(time.Second)
} else {
fmt.Println("Sent", k)//, ":", v)
break
}
}
}
done.Done()
}
for i := 0; i < workers; i++ {
go ffunc()
}
return ret, &done
}
type Assignment struct {
src, dest *Person
}
func (e Emailer) sendAssignment(src, dest *Person) error {
// %1: Name of gift sender
// %2: Email of gift sender
// %3: Name of gift receiver
msg := strings.Replace(
strings.Replace(
strings.Replace(e.rawMsg, "%1", src.name, -1),
"%2", src.email, -1),
"%3", dest.name, -1)
var to string
if DRY_RUN {
to = e.from
} else {
to = src.email
}
return smtp.SendMail(e.server, e.auth, e.from, []string{to}, ([]byte)(msg))
}
type Emailer struct {
auth smtp.Auth
server, from string
rawMsg string
}
func readLine(in*Input, exp int, errString string) ([]string, error) {
if l, b := in.Next(); !b {
if e := in.Err(); e != nil {
return nil, e
}
} else if exp == -1 {
return []string{l}, nil
} else if sp := strings.Split(l, ","); len(sp) == exp {
return sp, nil
}
return nil, errors.New(errString)
}
func readInEmailer(in*Input) (*Emailer, error) {
auths, err := readLine(in, 4, "Need four fields in the first line of the input file: identity,username,password,host")
if err != nil {
return nil, err
}
sp, err := readLine(in, 2, "Need two fields in the second line of the input file: server,from")
if err != nil {
return nil, err
}
subject, err := readLine(in, -1, "Need a line designating the subject in the third line of the file")
if err != nil {
return nil, err
}
msg, err := readLine(in, -1, "Need a line designating the generic message in the fourth line of the file")
if err != nil {
return nil, err
}
raw := "Subject: " + subject[0] + "\r\nContent-Type: text/html\r\n\r\n" + msg[0]
if line, b := in.Next(); b {
return nil, errors.New("Extra line at the end of the first section: " + line)
}
return &Emailer {
auth: smtp.PlainAuth(auths[0], auths[1], auths[2], auths[3]),
server: sp[0],
from: sp[1],
rawMsg: raw,
}, nil
}