File: adium.go

package info (click to toggle)
coyim 0.3.7-3
  • links: PTS, VCS
  • area: main
  • in suites: stretch
  • size: 4,064 kB
  • ctags: 4,528
  • sloc: xml: 5,120; sh: 328; python: 286; makefile: 235; ruby: 51
file content (177 lines) | stat: -rw-r--r-- 4,617 bytes parent folder | download
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
package importer

import (
	"io/ioutil"
	"os"
	"path/filepath"
	"sort"
	"strings"

	plist "github.com/DHowett/go-plist"
	"github.com/twstrike/coyim/config"
)

// In $HOME
const adiumConfigDir = "Library/Application Support/Adium 2.0/Users/Default"

const adiumAccountMappingsFile = "Accounts.plist"

type adiumImporter struct{}

type adiumAccountMapping struct {
	objectID    string
	uid         string
	accountType string
}

func (p *adiumImporter) readAccountMappings(s string) (map[string]adiumAccountMapping, bool) {
	contents, _ := ioutil.ReadFile(s)
	var res map[string]interface{}
	plist.Unmarshal(contents, &res)

	result := make(map[string]adiumAccountMapping)

	acs := res["Accounts"].([]interface{})

	for _, v := range acs {
		vals := v.(map[string]interface{})

		var m adiumAccountMapping
		m.uid = vals["UID"].(string)
		m.objectID = vals["ObjectID"].(string)
		m.accountType = vals["Type"].(string)
		result[m.objectID] = m
	}

	return result, true
}

func (p *adiumImporter) protocolMatches(s string) bool {
	return strings.HasPrefix(strings.ToLower(s), "libpurple-jabber")
}

func (p *adiumImporter) importKeysFrom(f string) (map[string][]byte, bool) {
	return ImportKeysFromPidginStyle(f, p.protocolMatches)
}

func (p *adiumImporter) importFingerprintsFrom(f string) (map[string][]*config.KnownFingerprint, bool) {
	return ImportFingerprintsFromPidginStyle(f, p.protocolMatches)
}

func (p *adiumImporter) importAccounts(f string) (map[string]*config.Account, bool) {
	return importAccountsPidginStyle(f)
}

func (p *adiumImporter) importPeerPrefs(f string) (map[string]map[string]*pidginOTRSettings, bool) {
	return importPeerPrefsPidginStyle(f)
}

func (p *adiumImporter) importGlobalPrefs(f string) (*pidginOTRSettings, bool) {
	return importGlobalPrefsPidginStyle(f)
}

func (p *adiumImporter) importAllFrom(accountMappingsFile, accountsFile, prefsFile, blistFile, keyFile, fprFile string) (*config.ApplicationConfig, bool) {
	accountMappings, ok0 := p.readAccountMappings(accountMappingsFile)
	accounts, ok1 := p.importAccounts(accountsFile)
	globalPrefs, ok2 := p.importGlobalPrefs(prefsFile)
	peerPrefs, ok3 := p.importPeerPrefs(blistFile)
	keysRaw, ok4 := p.importKeysFrom(keyFile)
	fprsRaw, ok5 := p.importFingerprintsFrom(fprFile)

	if !ok0 || !ok1 {
		return nil, false
	}

	keys := make(map[string][]byte)
	for k, v := range keysRaw {
		keys[accountMappings[k].uid] = v
	}

	fprs := make(map[string][]*config.KnownFingerprint)
	for k, v := range fprsRaw {
		fprs[accountMappings[k].uid] = v
	}

	res := &config.ApplicationConfig{}
	for name, ac := range accounts {
		res.Add(ac)
		if ok2 {
			if globalPrefs.enabled {
				if globalPrefs.onlyPrivate {
					ac.AlwaysEncrypt = true
					ac.OTRAutoStartSession = true
				} else if globalPrefs.automatic {
					ac.OTRAutoStartSession = true
					ac.OTRAutoAppendTag = true
				}
			} else {
				ac.AlwaysEncrypt = false
			}
		}
		if ok3 {
			if ss, ok := peerPrefs[name]; ok {
				for p, sp := range ss {
					if sp.enabled {
						if sp.onlyPrivate {
							ac.AlwaysEncryptWith = append(ac.AlwaysEncryptWith, p)
						}
					} else {
						ac.DontEncryptWith = append(ac.DontEncryptWith, p)
					}
				}
			}
		}
		if ok4 {
			if kk, ok := keys[name]; ok {
				ac.PrivateKeys = [][]byte{kk}
			}
		}
		if ok5 {
			if fprs, ok := fprs[name]; ok {
				ac.Peers = nil
				sort.Sort(config.LegacyByNaturalOrder(fprs))
				for _, kfpr := range fprs {
					fpr, _ := ac.EnsurePeer(kfpr.UserID).EnsureHasFingerprint(kfpr.Fingerprint)
					if !kfpr.Untrusted {
						fpr.Trusted = true
					}
				}
			}
		}
	}

	sort.Sort(config.ByAccountNameAlphabetic(res.Accounts))

	return res, true
}

func (p *adiumImporter) findDir() (string, bool) {
	if fi, err := os.Stat(config.WithHome(filepath.Join(adiumConfigDir, adiumAccountMappingsFile))); err == nil && !fi.IsDir() {
		return config.WithHome(adiumConfigDir), true
	}

	return "", false
}

func (p *adiumImporter) composeFileNamesFrom(dir string) (accountMappingsFile, accountsFile, prefsFile, blistFile, keyFile, fprFile string) {
	return filepath.Join(dir, adiumAccountMappingsFile),
		filepath.Join(dir, "libpurple", pidginAccountsFile),
		filepath.Join(dir, "libpurple", pidginPrefsFile),
		filepath.Join(dir, "libpurple", pidginBuddyFile),
		filepath.Join(dir, pidginOtrDataKeyFile),
		filepath.Join(dir, pidginOtrDataFingerprintsFile)
}

func (p *adiumImporter) TryImport() []*config.ApplicationConfig {
	var res []*config.ApplicationConfig

	dd, ok := p.findDir()
	if ok {
		ac, ok := p.importAllFrom(p.composeFileNamesFrom(dd))
		if ok {
			res = append(res, ac)
		}
	}

	return res
}