-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.go
203 lines (190 loc) · 4.48 KB
/
main.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
package main
import (
"bufio"
"bytes"
"crypto/sha256"
"encoding/json"
"flag"
"fmt"
"io"
"net/mail"
"os"
"os/exec"
"path/filepath"
"strings"
"time"
imapclient "github.com/emersion/go-imap/client"
)
const conf_file = ".go_sendmail.conf"
var targetdir = flag.String("t", "mail/target", "target directory")
var portable = flag.Bool("p", false, "portable (not relative $HOME)")
var noimap = flag.Bool("noimap", false, "do not upload to IMAP when plain")
var nosmtp = flag.Bool("nosmtp", false, "do not send on SMTP (put into local mail directory)")
func main() {
flag.Parse()
user_info := make(map[string]string)
if s, e := os.UserHomeDir(); e == nil {
if !*portable {
*targetdir = filepath.Join(s, *targetdir)
}
userconfs := filepath.Join(s, conf_file)
if f, e := os.Open(userconfs); e == nil {
dec := json.NewDecoder(f)
dec.Decode(&user_info)
} else {
panic(e)
}
} else {
panic(e)
}
var raw_msg *os.File
if f, e := os.CreateTemp("/tmp", "go_sendmail_tmp_"); e != nil {
panic(e)
} else if _, e := io.Copy(f, os.Stdin); e != nil {
panic(e)
} else if e := f.Close(); e != nil {
panic(e)
} else if g, e := os.Open(f.Name()); e != nil {
panic(e)
} else {
raw_msg = g
}
msg, e := mail.ReadMessage(raw_msg)
if e != nil {
panic(e)
}
mechan := make(chan string)
rp, wp := io.Pipe()
go func() {
var me string
if a, e := mail.ParseAddress(msg.Header.Get("From")); e == nil {
me = strings.ToLower(a.Address)
if filename, ok := user_info[me]; ok != true {
panic(ok)
} else {
if strings.HasSuffix(filename, ".gpg") {
cmd := exec.Command("/usr/bin/gpg", "-qd", filename)
cmd.Stdout = wp
if e := cmd.Run(); e != nil {
panic(fmt.Errorf("gpg decryption error!"))
} else {
wp.Close()
}
} else if f, e := os.Open(filename); e == nil {
if _, e := io.Copy(wp, f); e != nil {
panic(e)
}
wp.Close()
f.Close()
}
}
mechan <- me
} else {
panic(e)
}
}()
hostname, port, a, imap_addr, imap_a, e := LoadConfig(rp)
if e != nil {
panic(e)
}
rcpt_addrs := make([]*mail.Address, 0, 16)
if a, e := msg.Header.AddressList("To"); e == nil {
rcpt_addrs = append(rcpt_addrs, a...)
}
if a, e := msg.Header.AddressList("Cc"); e == nil {
rcpt_addrs = append(rcpt_addrs, a...)
}
if a, e := msg.Header.AddressList("Bcc"); e == nil {
rcpt_addrs = append(rcpt_addrs, a...)
}
if strings.SplitN(rcpt_addrs[0].Address, "@", 2)[0] == "nobody" {
*noimap = true
*nosmtp = true
}
// SMTP handler
func() {
if *nosmtp {
fmt.Fprintln(os.Stderr, "no smtp")
return
}
client, e := ClientConnect(a, hostname, port)
if e != nil {
panic(e)
}
if e := client.Mail(<-mechan); e != nil {
panic(e)
}
done_addrs := make([]string, 0, len(rcpt_addrs))
for _, addr := range rcpt_addrs {
for _, d := range done_addrs {
if d == addr.String() {
continue
}
}
if e := client.Rcpt(addr.Address); e != nil {
panic(e)
}
done_addrs = append(done_addrs, addr.String())
}
data, e := client.Data()
if e != nil {
panic(e)
}
var wb *bufio.Writer
if i, e := raw_msg.Stat(); e != nil {
panic(e)
} else {
wb = bufio.NewWriterSize(data, int(i.Size()))
}
// msg.Body gets consumed
if _, e := WriteMessage(msg.Header, msg.Body, wb); e != nil {
panic(e)
} else if e := wb.Flush(); e != nil {
panic(e)
} else {
data.Close()
client.Close()
raw_msg.Close()
}
}()
// imap submission
if imap_a != nil && !*noimap {
if f, e := os.Open(raw_msg.Name()); e != nil {
panic(e)
} else if c, e := imapclient.DialTLS(imap_addr, nil); e != nil {
panic(e)
} else if e := c.Authenticate(imap_a); e != nil {
panic(e)
} else if stat, e := c.Select("sent", false); e != nil {
panic(e)
} else {
defer c.Logout()
buf := bytes.NewBuffer(nil)
if _, e := io.Copy(buf, f); e != nil {
panic(e)
} else if e := c.Append(stat.Name, []string{"\\Seen"}, time.Now(), buf); e != nil {
panic(e)
} else if e := f.Close(); e != nil {
panic(e)
}
}
}
// archive
arch := &ArchiveTicket{
hasher: sha256.New(),
header: msg.Header,
filename: raw_msg.Name(),
}
if name, e := arch.Submit(); e != nil {
panic(e)
} else {
fmt.Println(name)
}
// notmuch
msgid_query := fmt.Sprintf("id:%s", strings.Trim(msg.Header.Get("Message-ID"), "<>"))
if e := exec.Command("notmuch", "new").Run(); e != nil {
panic(e)
} else if e := exec.Command("notmuch", "tag", "-unread", "+sent", msgid_query).Run(); e != nil {
panic(e)
}
}