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
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
|
package appservice
import (
"fmt"
"strings"
log "github.com/sirupsen/logrus"
. "git.deuxfleurs.fr/Deuxfleurs/easybridge/connector"
)
type Account struct {
MatrixUser string
AccountName string
Protocol string
Conn Connector
JoinedRooms map[RoomID]bool
}
var registeredAccounts = map[string]map[string]*Account{}
func AddAccount(a *Account) {
if _, ok := registeredAccounts[a.MatrixUser]; !ok {
registeredAccounts[a.MatrixUser] = make(map[string]*Account)
}
registeredAccounts[a.MatrixUser][a.AccountName] = a
}
func FindAccount(mxUser string, name string) *Account {
if u, ok := registeredAccounts[mxUser]; ok {
if a, ok := u[name]; ok {
return a
}
}
return nil
}
func FindJoinedAccount(mxUser string, protocol string, room RoomID) *Account {
if u, ok := registeredAccounts[mxUser]; ok {
for _, acct := range u {
if acct.Protocol == protocol {
if j, ok := acct.JoinedRooms[room]; ok && j {
return acct
}
}
}
}
return nil
}
func RemoveAccount(mxUser string, name string) {
if u, ok := registeredAccounts[mxUser]; ok {
delete(u, name)
}
}
// ---- Begin event handlers ----
func (a *Account) Joined(roomId RoomID) {
err := a.joinedInternal(roomId)
if err != nil {
log.Warnf("Dropping Account.Joined %s: %s", roomId, err)
}
}
func (a *Account) joinedInternal(roomId RoomID) error {
a.JoinedRooms[roomId] = true
mx_room_id, err := dbGetMxRoom(a.Protocol, roomId)
if err != nil {
return err
}
log.Debugf("Joined %s (%s)\n", roomId, a.MatrixUser)
err = mxRoomInvite(mx_room_id, a.MatrixUser)
if err != nil && strings.Contains(err.Error(), "already in the room") {
err = nil
}
return err
}
// ----
func (a *Account) Left(roomId RoomID) {
err := a.leftInternal(roomId)
if err != nil {
log.Warnf("Dropping Account.Left %s: %s", roomId, err)
}
}
func (a *Account) leftInternal(roomId RoomID) error {
delete(a.JoinedRooms, roomId)
mx_room_id, err := dbGetMxRoom(a.Protocol, roomId)
if err != nil {
return err
}
log.Printf("Joined %s (%s)\n", roomId, a.MatrixUser)
return mxRoomKick(mx_room_id, a.MatrixUser, fmt.Sprintf("got leave room event on %s", a.Protocol))
}
// ----
func (a *Account) UserInfoUpdated(user UserID, info *UserInfo) {
err := a.userInfoUpdatedInternal(user, info)
if err != nil {
log.Warnf("Dropping Account.UserInfoUpdated %s: %s", user, err)
}
}
func (a *Account) userInfoUpdatedInternal(user UserID, info *UserInfo) error {
mx_user_id, err := dbGetMxUser(a.Protocol, user)
if err != nil {
return err
}
if info.DisplayName != "" {
err2 := mxProfileDisplayname(mx_user_id, fmt.Sprintf("%s (%s)", info.DisplayName, a.Protocol))
if err2 != nil {
err = err2
}
}
if info.Avatar != nil {
err = fmt.Errorf("Avatar: not implemented")
}
return err
}
// ----
func (a *Account) RoomInfoUpdated(roomId RoomID, author UserID, info *RoomInfo) {
err := a.roomInfoUpdatedInternal(roomId, author, info)
if err != nil {
log.Warnf("Dropping Account.RoomInfoUpdated %s: %s", roomId, err)
}
}
func (a *Account) roomInfoUpdatedInternal(roomId RoomID, author UserID, info *RoomInfo) error {
mx_room_id, err := dbGetMxRoom(a.Protocol, roomId)
if err != nil {
return err
}
as_mxid := ezbrMxId()
if len(author) > 0 {
mx_user_id, err2 := dbGetMxUser(a.Protocol, author)
if err2 == nil {
as_mxid = mx_user_id
}
}
if info.Topic != "" {
err2 := mxRoomTopicAs(mx_room_id, info.Topic, as_mxid)
if err2 != nil {
err = err2
}
}
if info.Name != "" {
err2 := mxRoomNameAs(mx_room_id, info.Name, as_mxid)
if err2 != nil {
err = err2
}
}
if info.Picture != nil {
// TODO
err = fmt.Errorf("Picture: not implemented")
}
return err
}
// ----
func (a *Account) Event(event *Event) {
err := a.eventInternal(event)
if err != nil {
log.Warnf("Dropping Account.Event %s %s %s: %s", event.Author, event.Recipient, event.Room, err)
}
}
func (a *Account) eventInternal(event *Event) error {
mx_user_id, err := dbGetMxUser(a.Protocol, event.Author)
if err != nil {
return err
}
if event.Type == EVENT_JOIN {
log.Printf("%s join %s %s", a.Protocol, event.Author, event.Room)
mx_room_id, err := dbGetMxRoom(a.Protocol, event.Room)
if err != nil {
return err
}
err = mxRoomInvite(mx_room_id, mx_user_id)
if err != nil {
if strings.Contains(err.Error(), "already in the room") {
err = nil
}
return err
}
return mxRoomJoinAs(mx_room_id, mx_user_id)
} else if event.Type == EVENT_LEAVE {
log.Printf("%s join %s %s", a.Protocol, event.Author, event.Room)
mx_room_id, err := dbGetMxRoom(a.Protocol, event.Room)
if err != nil {
return err
}
return mxRoomLeaveAs(mx_room_id, mx_user_id)
} else {
log.Printf("%s msg %s %s", a.Protocol, event.Author, event.Room)
mx_room_id := ""
if len(event.Room) > 0 {
mx_room_id, err = dbGetMxRoom(a.Protocol, event.Room)
if err != nil {
return err
}
} else {
mx_room_id, err = dbGetMxPmRoom(a.Protocol, event.Author, mx_user_id, a.MatrixUser, a.AccountName)
if err != nil {
return err
}
}
typ := "m.text"
if event.Type == EVENT_ACTION {
typ = "m.emote"
}
return mxSendMessageAs(mx_room_id, typ, event.Text, mx_user_id)
}
}
|