easybridge/appservice/account.go

160 lines
3.4 KiB
Go
Raw Normal View History

2020-02-16 18:30:49 +00:00
package appservice
import (
2020-02-16 21:07:41 +00:00
"fmt"
2020-02-17 08:41:08 +00:00
log "github.com/sirupsen/logrus"
2020-02-16 21:07:41 +00:00
2020-02-16 18:30:49 +00:00
. "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)
}
2020-02-16 18:30:49 +00:00
}
// ----
2020-02-16 18:30:49 +00:00
func (a *Account) Joined(roomId RoomID) {
a.JoinedRooms[roomId] = true
2020-02-16 21:07:41 +00:00
mx_room_id, err := dbGetMxRoom(a.Protocol, roomId)
if err != nil {
return
}
log.Printf("Joined %s (%s)\n", roomId, a.MatrixUser)
err = mxRoomInvite(mx_room_id, a.MatrixUser)
if err != nil {
log.Printf("Could not invite %s to %s", a.MatrixUser, mx_room_id)
}
2020-02-16 18:30:49 +00:00
}
func (a *Account) Left(roomId RoomID) {
delete(a.JoinedRooms, roomId)
2020-02-16 21:07:41 +00:00
mx_room_id, err := dbGetMxRoom(a.Protocol, roomId)
if err != nil {
return
}
log.Printf("Joined %s (%s)\n", roomId, a.MatrixUser)
err = mxRoomKick(mx_room_id, a.MatrixUser, fmt.Sprintf("got leave room event on %s", a.Protocol))
if err != nil {
log.Printf("Could not invite %s to %s", a.MatrixUser, mx_room_id)
}
2020-02-16 18:30:49 +00:00
}
func (a *Account) UserInfoUpdated(user UserID, info *UserInfo) {
// TODO
}
func (a *Account) RoomInfoUpdated(roomId RoomID, info *RoomInfo) {
// TODO
}
func (a *Account) Event(event *Event) {
2020-02-16 21:07:41 +00:00
mx_user_id, err := dbGetMxUser(a.Protocol, event.Author)
if err != nil {
return
}
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 = mxRoomInvite(mx_room_id, mx_user_id)
if err != nil {
log.Printf("Could not invite %s to %s", a.MatrixUser, mx_room_id)
}
err = mxRoomJoinAs(mx_room_id, mx_user_id)
if err != nil {
log.Printf("Could not join %s as %s", a.MatrixUser, mx_room_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 = mxRoomLeaveAs(mx_room_id, mx_user_id)
if err != nil {
log.Printf("Could not leave %s as %s", a.MatrixUser, mx_room_id)
}
2020-02-16 21:57:30 +00:00
} else {
log.Printf("%s msg %s %s", a.Protocol, event.Author, event.Room)
mx_room_id := ""
2020-02-16 21:07:41 +00:00
if len(event.Room) > 0 {
2020-02-16 21:57:30 +00:00
mx_room_id, err = dbGetMxRoom(a.Protocol, event.Room)
2020-02-16 21:07:41 +00:00
if err != nil {
return
}
2020-02-16 21:57:30 +00:00
} else {
mx_room_id, err = dbGetMxPmRoom(a.Protocol, event.Author, mx_user_id, a.MatrixUser, a.AccountName)
2020-02-16 21:07:41 +00:00
if err != nil {
2020-02-16 21:57:30 +00:00
return
2020-02-16 21:07:41 +00:00
}
2020-02-16 21:57:30 +00:00
}
typ := "m.text"
if event.Type == EVENT_ACTION {
typ = "m.emote"
}
err = mxSendMessageAs(mx_room_id, typ, event.Text, mx_user_id)
if err != nil {
log.Printf("Could not send %s as %s", event.Text, mx_user_id)
2020-02-16 21:07:41 +00:00
}
}
2020-02-16 18:30:49 +00:00
}
2020-02-16 21:07:41 +00:00