-
Notifications
You must be signed in to change notification settings - Fork 80
/
account_manager.go
216 lines (169 loc) · 4.53 KB
/
account_manager.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
204
205
206
207
208
209
210
211
212
213
214
215
216
package gui
import (
"fmt"
"log"
"sync"
"github.com/coyim/coyim/config"
"github.com/coyim/coyim/otr_client"
rosters "github.com/coyim/coyim/roster"
"github.com/coyim/coyim/session/access"
"github.com/coyim/coyim/xmpp/interfaces"
"github.com/coyim/coyim/xmpp/jid"
)
type accountManager struct {
accounts []*account
events chan interface{}
contacts map[*account]*rosters.List
otr_client.CommandManager
sync.RWMutex
}
func newAccountManager(c otr_client.CommandManager) *accountManager {
return &accountManager{
events: make(chan interface{}, 10),
accounts: make([]*account, 0, 5),
contacts: make(map[*account]*rosters.List),
CommandManager: c,
}
}
func (m *accountManager) getAllAccounts() []*account {
m.RLock()
defer m.RUnlock()
return append([]*account(nil), m.accounts...)
}
func (m *accountManager) getAllConnectedAccounts() []*account {
m.RLock()
defer m.RUnlock()
accounts := make([]*account, 0, len(m.accounts))
for _, acc := range m.accounts {
if !acc.connected() {
continue
}
accounts = append(accounts, acc)
}
return accounts
}
func (m *accountManager) disconnectAll() {
m.RLock()
defer m.RUnlock()
for _, acc := range m.accounts {
acc.disconnect()
}
}
func (m *accountManager) getAccountByID(ID string) (*account, bool) {
m.RLock()
defer m.RUnlock()
for _, acc := range m.accounts {
if acc.ID() == ID {
return acc, true
}
}
return nil, false
}
func (m *accountManager) findAccountForSession(s access.Session) *account {
acc, _ := m.getAccountByID(s.GetConfig().ID())
return acc
}
func (m *accountManager) getAllContacts() map[*account]*rosters.List {
return m.contacts
}
func (m *accountManager) getContacts(acc *account) *rosters.List {
return m.contacts[acc]
}
func (m *accountManager) setContacts(account *account, contacts *rosters.List) {
if account == nil {
panic("Developer error: account should never be nil")
}
m.contacts[account] = contacts
}
func (m *accountManager) addAccount(appConfig *config.ApplicationConfig, account *config.Account, sf access.Factory, df interfaces.DialerFactory) {
m.Lock()
defer m.Unlock()
acc := newAccount(appConfig, account, sf, df)
acc.session.Subscribe(m.events)
acc.session.SetCommandManager(m)
acc.session.SetConnector(acc)
m.accounts = append(m.accounts, acc)
m.setContacts(acc, rosters.New())
}
func (m *accountManager) removeAccount(conf *config.Account, k func()) {
toRemove, exists := m.getAccountByID(conf.ID())
if !exists {
return
}
m.Lock()
defer m.Unlock()
accs := make([]*account, 0, len(m.accounts)-1)
for _, acc := range m.accounts {
if acc == toRemove {
delete(m.contacts, acc)
continue
}
accs = append(accs, acc)
}
m.accounts = accs
k()
}
func (m *accountManager) buildAccounts(appConfig *config.ApplicationConfig, sf access.Factory, df interfaces.DialerFactory) {
hasConfUpdates := false
for _, accountConf := range appConfig.Accounts {
if _, ok := m.getAccountByID(accountConf.ID()); ok {
continue
}
hasUpdate, err := accountConf.EnsurePrivateKey()
if err != nil {
continue
}
hasConfUpdates = hasConfUpdates || hasUpdate
m.addAccount(appConfig, accountConf, sf, df)
}
if hasConfUpdates {
m.ExecuteCmd(otr_client.SaveApplicationConfigCmd{})
}
}
func (m *accountManager) addNewAccountsFromConfig(appConfig *config.ApplicationConfig, sf access.Factory, df interfaces.DialerFactory) {
for _, configAccount := range appConfig.Accounts {
_, found := m.getAccountByID(configAccount.ID())
if found {
continue
}
m.addAccount(appConfig, configAccount, sf, df)
}
}
func (m *accountManager) removePeer(account *account, peer jid.WithoutResource) {
m.Lock()
defer m.Unlock()
l, ok := m.contacts[account]
if !ok {
return
}
l.Remove(peer)
}
func (m *accountManager) getPeer(account *account, peer jid.WithoutResource) (*rosters.Peer, bool) {
m.RLock()
defer m.RUnlock()
l, ok := m.contacts[account]
if !ok {
log.Printf("Failure to look up account %v from account manager", account.session.GetConfig().Account)
return nil, false
}
return l.Get(peer)
}
func (m *accountManager) displayNameFor(account *account, peer jid.WithoutResource) string {
p, ok := m.getPeer(account, peer)
if !ok {
return peer.String()
}
return p.NameForPresentation()
}
func (m *accountManager) debugPeersFor(account *account) {
m.RLock()
defer m.RUnlock()
rs, ok := m.getAllContacts()[account]
if ok {
rs.Iter(func(_ int, item *rosters.Peer) {
fmt.Printf("-> %s\n", item.Dump())
})
}
fmt.Printf(" ************************************** \n")
fmt.Println()
}