-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathconfig.go
105 lines (93 loc) · 2.6 KB
/
config.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
package main
import (
"encoding/base64"
"encoding/json"
"io"
"bufio"
"fmt"
"os"
"os/exec"
"strings"
"github.com/emersion/go-sasl"
)
type Config struct {
filename string
folders []string
r io.ReadCloser // decrypted
salt []byte
addr string
a sasl.Client
}
type UserInfo struct {
SMTPServer string `json:"smtp_server"`
IMAPServer string `json:"imap_server"`
Type string `json:"type"`
User string `json:"user"`
Password string `json:"password"`
ClientID string `json:"clientid"`
ClientSecret string `json:"clientsecret"`
RefreshToken string `json:"refreshtoken"`
}
func (c *Config) PrintAuth(m string, ir []byte) string {
return fmt.Sprintf("%s %s %s %s", GenerateMailboxID(c.folders[0], c.addr, c.salt), c.addr, m, base64.URLEncoding.EncodeToString(ir))
}
// LoadConfig loads a configuration file (json encoded) and returns the relevant information.
func LoadConfig(r io.Reader) (salt, addr string, a sasl.Client, e error) {
userinfo := make(map[string]string)
// load config from os.Stdin
dec := json.NewDecoder(r)
if e = dec.Decode(&userinfo); e != nil {
return
}
// directory = userinfo["directory"]
// os.MkdirAll(directory, os.ModePerm)
salt = userinfo["salt"]
addr = userinfo["imap_server"]
switch userinfo["type"] {
case "plain":
a = sasl.NewPlainClient("", userinfo["user"], userinfo["password"])
case "gmail":
config, token := Gmail_Generate_Token(userinfo["clientid"], userinfo["clientsecret"], userinfo["refreshtoken"])
a = XOAuth2(userinfo["user"], config, token)
case "outlook":
config, token := Outlook_Generate_Token(userinfo["clientid"], userinfo["refreshtoken"])
a = XOAuth2(userinfo["user"], config, token)
}
return
}
func HandleConfInit(cp []string) (c *Config, e error) {
if f, e := os.Open(cp[0]); e != nil {
return nil, fmt.Errorf("ignoring: %s\n", cp[0])
} else if !strings.HasSuffix(f.Name(), ".gpg") {
return &Config{cp[0], cp[1:], f, nil, "", nil}, nil
} else {
// has gpg suffix
cmd := exec.Command("/usr/bin/gpg", "-qd", "-")
cmd.Stdin = f
rp, wp := io.Pipe()
cmd.Stdout = wp
go func() {
defer f.Close()
if e := cmd.Run(); e != nil {
panic(e)
}
}()
return &Config{cp[0], cp[1:], rp, nil, "", nil}, nil
}
}
func ParseConfInit(r io.Reader) ([][]string, int) {
var size int
cp := make([][]string, 0, 4)
stdin := bufio.NewReader(r)
for {
if l, p, e := stdin.ReadLine(); e != nil {
break
} else if p {
panic("isPrefix")
} else {
size += len(strings.Split(fmt.Sprintf("%s", l), ",")) - 1
cp = append(cp, strings.Split(fmt.Sprintf("%s", l), ","))
}
}
return cp, size
}