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
|
package importer
import (
"bufio"
"encoding/hex"
"encoding/xml"
"io/ioutil"
"os"
"strconv"
"strings"
"github.com/twstrike/otr3"
"github.com/twstrike/coyim/config"
"github.com/twstrike/coyim/xmpp/utils"
)
// ImportKeysFromPidginStyle will try to read keys in Pidgin style from the given file
func ImportKeysFromPidginStyle(f string, protocolMatcher func(string) bool) (map[string][]byte, bool) {
file, err := os.Open(f)
if err != nil {
return nil, false
}
defer file.Close()
acs, err := otr3.ImportKeys(file)
if err != nil {
return nil, false
}
res := make(map[string][]byte)
for _, ac := range acs {
if protocolMatcher(ac.Protocol) {
res[strings.TrimSuffix(ac.Name, "/")] = ac.Key.Serialize()
}
}
return res, true
}
// ImportFingerprintsFromPidginStyle will try to read fingerprints in Pidgin style from the given file
func ImportFingerprintsFromPidginStyle(f string, protocolMatcher func(string) bool) (map[string][]*config.KnownFingerprint, bool) {
file, err := os.Open(f)
if err != nil {
return nil, false
}
defer file.Close()
sc := bufio.NewScanner(file)
result := make(map[string][]*config.KnownFingerprint)
for sc.Scan() {
ln := strings.Split(sc.Text(), "\t")
if len(ln) < 4 {
return nil, false
}
name := strings.TrimSuffix(ln[1], "/")
if protocolMatcher(ln[2]) {
vv, ok := result[name]
if !ok {
vv = make([]*config.KnownFingerprint, 0, 1)
}
fp, err := hex.DecodeString(ln[3])
if err != nil {
continue
}
result[name] = append(vv, &config.KnownFingerprint{
UserID: ln[0],
Fingerprint: fp,
Untrusted: len(ln) < 5 || ln[4] != "verified",
})
}
}
return result, true
}
func importAccountsPidginStyle(f string) (map[string]*config.Account, bool) {
content, err := ioutil.ReadFile(f)
if err != nil {
return nil, false
}
var a pidginAccountsXML
err = xml.Unmarshal(content, &a)
if err != nil {
return nil, false
}
res := make(map[string]*config.Account)
for _, ac := range a.Accounts {
if ac.Protocol == "prpl-jabber" {
nm := utils.RemoveResourceFromJid(ac.Name)
a := &config.Account{}
a.Account = nm
a.Password = ac.Password
settings := ac.settingsAsMap()
a.Port = parseIntOr(settings["port"], 5222)
a.Proxies = make([]string, 0)
for _, px := range ac.Proxy {
if px.Type == "tor" {
a.Proxies = append(a.Proxies, "tor-auto://")
}
a.Proxies = append(a.Proxies,
composeProxyString(px.Type, px.Username, px.Password, px.Host, strconv.Itoa(px.Port)),
)
}
if settings["connect_server"] != "" {
a.Server = settings["connect_server"]
}
res[nm] = a
}
}
return res, true
}
func importPeerPrefsPidginStyle(f string) (map[string]map[string]*pidginOTRSettings, bool) {
content, err := ioutil.ReadFile(f)
if err != nil {
return nil, false
}
var a pidginBlistXML
err = xml.Unmarshal(content, &a)
if err != nil {
return nil, false
}
res := make(map[string]map[string]*pidginOTRSettings)
for _, p := range a.Peers {
if p.Protocol == "prpl-jabber" {
haveOTR := false
settings := &pidginOTRSettings{}
for _, s := range p.Settings {
switch s.Name {
case "OTR/enabled":
haveOTR = true
settings.enabled = s.Value == "1"
case "OTR/automatic":
haveOTR = true
settings.automatic = s.Value == "1"
case "OTR/avoidloggingotr":
haveOTR = true
settings.avoidLoggingOTR = s.Value == "1"
case "OTR/onlyprivate":
haveOTR = true
settings.onlyPrivate = s.Value == "1"
}
}
if haveOTR {
getOrMake(res, utils.RemoveResourceFromJid(p.Account))[utils.RemoveResourceFromJid(p.Name)] = settings
}
}
}
return res, true
}
func importGlobalPrefsPidginStyle(f string) (*pidginOTRSettings, bool) {
content, err := ioutil.ReadFile(f)
if err != nil {
return nil, false
}
var a pidginPrefsXML
err = xml.Unmarshal(content, &a)
if err != nil {
return nil, false
}
settings := pidginOTRSettings{}
have := false
if res, ok := a.lookup("OTR", "enabled"); ok {
have = true
settings.enabled = res.Value == "1"
}
if res, ok := a.lookup("OTR", "automatic"); ok {
have = true
settings.automatic = res.Value == "1"
}
if res, ok := a.lookup("OTR", "onlyprivate"); ok {
have = true
settings.onlyPrivate = res.Value == "1"
}
if res, ok := a.lookup("OTR", "avoidloggingotr"); ok {
have = true
settings.avoidLoggingOTR = res.Value == "1"
}
return &settings, have
}
|