forked from coyim/coyim
-
Notifications
You must be signed in to change notification settings - Fork 0
/
xmpp_client.go
106 lines (90 loc) · 2.89 KB
/
xmpp_client.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
package importer
import (
"encoding/hex"
"encoding/json"
"io/ioutil"
"github.com/twstrike/coyim/config"
)
type xmppClientConfig struct {
Account string
Server string `json:",omitempty"`
Proxies []string `json:",omitempty"`
Password string `json:",omitempty"`
Port int `json:",omitempty"`
PrivateKey []byte
KnownFingerprints []xmppClientKnownFingerprint
RawLogFile string `json:",omitempty"`
NotifyCommand []string `json:",omitempty"`
IdleSecondsBeforeNotification int `json:",omitempty"`
Bell bool
HideStatusUpdates bool
UseTor bool
OTRAutoTearDown bool
OTRAutoAppendTag bool
OTRAutoStartSession bool
ServerCertificateSHA256 string `json:",omitempty"`
AlwaysEncrypt bool `json:",omitempty"`
AlwaysEncryptWith []string `json:",omitempty"`
}
type xmppClientKnownFingerprint struct {
UserID string `json:"UserId"`
FingerprintHex string
}
type xmppClientImporter struct{}
func (x *xmppClientImporter) importFrom(f string) (*config.ApplicationConfig, bool) {
contents, err := ioutil.ReadFile(f)
if err != nil {
return nil, false
}
c := new(xmppClientConfig)
err = json.Unmarshal(contents, c)
if err != nil {
return nil, false
}
a := new(config.ApplicationConfig)
ac, err := a.AddNewAccount()
if err != nil {
return nil, false
}
ac.Account = c.Account
ac.Server = c.Server
ac.Proxies = c.Proxies
ac.Password = c.Password
ac.Port = c.Port
ac.HideStatusUpdates = c.HideStatusUpdates
ac.OTRAutoStartSession = c.OTRAutoStartSession
ac.OTRAutoTearDown = c.OTRAutoTearDown
ac.OTRAutoAppendTag = c.OTRAutoAppendTag
ac.LegacyServerCertificateSHA256 = c.ServerCertificateSHA256
ac.PrivateKeys = [][]byte{c.PrivateKey}
ac.AlwaysEncryptWith = c.AlwaysEncryptWith
ac.Peers = nil
for _, kfpr := range c.KnownFingerprints {
fp, _ := hex.DecodeString(kfpr.FingerprintHex)
fpr, _ := ac.EnsurePeer(kfpr.UserID).EnsureHasFingerprint(fp)
fpr.Trusted = true
}
a.NotifyCommand = c.NotifyCommand
a.Bell = c.Bell
a.RawLogFile = c.RawLogFile
a.IdleSecondsBeforeNotification = c.IdleSecondsBeforeNotification
return a, true
}
func (x *xmppClientImporter) findFiles() []string {
var res []string
res = ifExists(res, config.WithHome(".xmpp-client"))
res = ifExists(res, config.WithHome("Persistent/.xmpp-client"))
res = ifExistsDir(res, config.WithHome(".xmpp-client"))
res = ifExistsDir(res, config.WithHome(".xmpp-clients"))
return res
}
func (x *xmppClientImporter) TryImport() []*config.ApplicationConfig {
var res []*config.ApplicationConfig
for _, f := range x.findFiles() {
ac, ok := x.importFrom(f)
if ok {
res = append(res, ac)
}
}
return res
}