/
user.go
95 lines (79 loc) · 1.82 KB
/
user.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
package main
import (
"fmt"
"log"
"net/smtp"
"github.com/BurntSushi/locker"
)
type lcmUser struct {
configUser
}
func newLcmUser(confUser configUser) *lcmUser {
return &lcmUser{confUser}
}
func findUserById(userid string) *lcmUser {
if len(userid) == 0 {
panic(ue("No user specified."))
}
if user, ok := conf.Users[userid]; ok {
return newLcmUser(user)
}
panic(ue("Could not find user with id **%s**.", userid))
}
// findUserByNo finds a user by a given user number. We don't panic here
// because we only look for users by number to match things in the DB. If
// we don't find a match, we want to be free to ignore it.
func findUserByNo(userid string) *lcmUser {
if user, ok := conf.usersById[userid]; ok {
return newLcmUser(user)
}
return nil
}
func (user *lcmUser) String() string {
return user.Name
}
func (user *lcmUser) valid() bool {
return user != nil && len(user.Id) > 0
}
func (user *lcmUser) lock() {
locker.Lock(user.Id)
}
func (user *lcmUser) unlock() {
locker.Unlock(user.Id)
}
func (user *lcmUser) email(subject, message string) error {
auth := smtp.PlainAuth(
"",
conf.Email.SMTP.Username,
conf.Email.SMTP.Password,
conf.Email.SMTP.Server,
)
err := smtp.SendMail(
fmt.Sprintf("%s:%d", conf.Email.SMTP.Server, conf.Email.SMTP.Port),
auth,
conf.Email.FromEmail,
[]string{user.Email},
[]byte(fmt.Sprintf(`From: "%s" <%s>
To: "%s" <%s>
Subject: %s
%s
`,
conf.Email.FromName, conf.Email.FromEmail,
user.Name, user.Email,
subject, message)))
if err != nil {
log.Printf("ERROR [email]: %s", err)
return err
}
return nil
}
type usersAlphabetical []*lcmUser
func (us usersAlphabetical) Less(i, j int) bool {
return us[i].Name < us[j].Name
}
func (us usersAlphabetical) Swap(i, j int) {
us[i], us[j] = us[j], us[i]
}
func (us usersAlphabetical) Len() int {
return len(us)
}