easybridge/appservice/account.go

224 lines
5.1 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"
"log"
"git.deuxfleurs.fr/Deuxfleurs/easybridge/mxlib"
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
}
func (a *Account) Joined(roomId 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 = 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) {
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
// ----
func dbGetMxRoom(protocol string, roomId RoomID) (string, error) {
var room DbRoomMap
// Check if room exists in our mapping,
// If not create it
must_create := db.First(&room, DbRoomMap{
Protocol: protocol,
RoomID: roomId,
}).RecordNotFound()
if must_create {
alias := roomAlias(protocol, roomId)
// Lookup alias
mx_room_id, err := mxDirectoryRoom(fmt.Sprintf("#%s:%s", alias, config.MatrixDomain))
// If no alias found, create room
if err != nil {
name := fmt.Sprintf("%s (%s)", roomId, protocol)
mx_room_id, err = mxCreateRoom(name, alias, []string{})
if err != nil {
log.Printf("Could not create room for %s: %s", name, err)
return "", err
}
}
room = DbRoomMap{
Protocol: protocol,
RoomID: roomId,
MxRoomID: mx_room_id,
}
db.Create(&room)
}
log.Printf("Got room id: %s", room.MxRoomID)
return room.MxRoomID, nil
}
2020-02-16 21:57:30 +00:00
func dbGetMxPmRoom(protocol string, them UserID, themMxId string, usMxId string, usAccount string) (string, error) {
var room DbPmRoomMap
must_create := db.First(&room, DbPmRoomMap{
MxUserID: usMxId,
Protocol: protocol,
AccountName: usAccount,
UserID: them,
}).RecordNotFound()
if must_create {
name := fmt.Sprintf("%s (%s)", them, protocol)
mx_room_id, err := mxCreateDirectRoomAs(name, []string{usMxId}, themMxId)
if err != nil {
log.Printf("Could not create room for %s: %s", name, err)
return "", err
}
err = mxRoomJoinAs(mx_room_id, themMxId)
if err != nil {
log.Printf("Could not join %s as %s", mx_room_id, themMxId)
return "", err
}
room = DbPmRoomMap{
MxUserID: usMxId,
Protocol: protocol,
AccountName: usAccount,
UserID: them,
MxRoomID: mx_room_id,
}
db.Create(&room)
}
log.Printf("Got PM room id: %s", room.MxRoomID)
return room.MxRoomID, nil
}
2020-02-16 21:07:41 +00:00
func dbGetMxUser(protocol string, userId UserID) (string, error) {
var user DbUserMap
must_create := db.First(&user, DbUserMap{
Protocol: protocol,
UserID: userId,
}).RecordNotFound()
if must_create {
username := userMxId(protocol, userId)
err := mxRegisterUser(username)
if err != nil {
if mxE, ok := err.(*mxlib.MxError); !ok || mxE.ErrCode != "M_USER_IN_USE" {
log.Printf("Could not register %s: %s", username, err)
return "", err
}
}
mxid := fmt.Sprintf("@%s:%s", username, config.MatrixDomain)
mxProfileDisplayname(mxid, fmt.Sprintf("%s (%s)", userId, protocol))
user = DbUserMap{
Protocol: protocol,
UserID: userId,
MxUserID: mxid,
}
db.Create(&user)
}
return user.MxUserID, nil
}