-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathemail_account.go
179 lines (150 loc) · 3.84 KB
/
email_account.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
package main
import (
"database/sql"
"fmt"
"io/ioutil"
"os"
"os/exec"
"strings"
"time"
log "github.com/Sirupsen/logrus"
emailLib "github.com/jordan-wright/email"
"github.com/thecloakproject/utils/crypt"
)
type EmailAccount struct {
Id string `json:"id,omitempty"`
Email string `json:"email"`
PubKey string `json:"pubkey,omitempty"`
Created time.Time `json:"created,omitempty"`
}
func GetEmailAccount(db *sql.DB, id string) (*EmailAccount, error) {
accounts, err := GetEmailAccounts(db, []string{id})
if err != nil {
return nil, err
}
if len(accounts) != 1 {
return nil, fmt.Errorf("%d accounts with id %v, not 1!", len(accounts), id)
}
return accounts[0], nil
}
func GetEmailAccounts(db *sql.DB, ids []string) ([]*EmailAccount, error) {
idsParam := "{" + strings.Join(ids, ",") + "}"
rows, err := db.Query(`
SELECT
id, email, created
FROM
email_account
WHERE
id = ANY($1::uuid[])
`, idsParam)
if err != nil {
if err != sql.ErrNoRows {
log.Errorf("Error getting email_accounts. Err: %s", err)
}
return nil, err
}
defer rows.Close()
emailAccounts := []*EmailAccount{}
for rows.Next() {
var ea EmailAccount
err := rows.Scan(&ea.Id, &ea.Email, &ea.Created)
if err != nil {
log.Errorf("Error with scan. Err: %v", err)
return nil, err
}
emailAccounts = append(emailAccounts, &ea)
}
log.Debugf("GetEmailAccounts returning %v accounts", len(emailAccounts))
return emailAccounts, nil
}
// Save Email and PubKey, attach Id that is returned.
func (e *EmailAccount) Save(db *sql.DB) error {
tx, err := db.Begin()
if err != nil {
log.Errorf("Error beginning transaction. Err: %s", err)
return err
}
if e.PubKey != "" {
err = importPublicKey(e.PubKey)
if err != nil {
log.Errorf("Error importing public key. Err: %s", err)
return err
}
}
err = tx.QueryRow(`
INSERT INTO email_account(email)
VALUES ($1)
RETURNING id, created
`, e.Email).Scan(&e.Id, &e.Created)
if err != nil {
log.Errorf("Error adding email_account. Err: %s", err)
rollbackErr := tx.Rollback()
if rollbackErr != nil {
log.Errorf("Got error rolling back transaction. Err: %s", rollbackErr)
}
return err
}
err = tx.Commit()
if err != nil {
log.Errorf("Error committing transaction. Err: %s", err)
return err
}
return nil
}
func (e *EmailAccount) Send(emailData EmailData, emailPool *emailLib.Pool) error {
sendableEmail := emailData.toSendableEmail()
if e.HasPubKey() {
encryptedMsg, err := encryptEmailBody(sendableEmail.From, e.Email,
string(sendableEmail.Text))
if err != nil {
log.Errorf("Error encrypting message: %v\n", err)
return err
}
sendableEmail.Text = encryptedMsg
}
sendableEmail.To = []string{e.Email}
// TODO - Make timeout configurable?
return emailPool.Send(sendableEmail, 15*time.Second)
}
func (e *EmailAccount) HasPubKey() bool {
_, err := crypt.GetEntityFrom(e.Email, crypt.PUBLIC_KEYRING_FILENAME)
if err != nil {
log.Debugf("Entity not found for %s. Err: %s", e.Email, err)
return false
}
return true
}
type EmailData struct {
// TODO: Have a default from email
From string `json:"from,omitempty"`
Subject string `json:"subject"`
Body string `json:"body"`
}
func (ed EmailData) toSendableEmail() *emailLib.Email {
em := emailLib.NewEmail()
em.From = ed.From
em.Subject = ed.Subject
em.Text = []byte(ed.Body)
return em
}
func importPublicKey(pubkey string) error {
// TODO - make tempfile directory configurable
tmpfile, err := ioutil.TempFile("", "pubkey-import")
if err != nil {
return err
}
defer os.Remove(tmpfile.Name())
// Save pubkey to temp file
if _, err := tmpfile.Write([]byte(pubkey)); err != nil {
return err
}
if err := tmpfile.Close(); err != nil {
return err
}
cmd := "gpg"
args := []string{"--import", tmpfile.Name()}
if err := exec.Command(cmd, args...).Run(); err != nil {
return err
}
return nil
}