From a11be80cf0c13263791f3e4f82fda461acd77130 Mon Sep 17 00:00:00 2001 From: Alex Auvolat Date: Sun, 16 Feb 2020 22:57:30 +0100 Subject: Work --- appservice/account.go | 65 ++++++++++++++++++++++++++++++++++++++++++++------- 1 file changed, 56 insertions(+), 9 deletions(-) (limited to 'appservice/account.go') diff --git a/appservice/account.go b/appservice/account.go index 4316362..230860a 100644 --- a/appservice/account.go +++ b/appservice/account.go @@ -84,23 +84,32 @@ func (a *Account) Event(event *Event) { if err != nil { log.Printf("Could not leave %s as %s", a.MatrixUser, mx_room_id) } - } else if event.Type == EVENT_MESSAGE { + } else { + log.Printf("%s msg %s %s", a.Protocol, event.Author, event.Room) + mx_room_id := "" + if len(event.Room) > 0 { - log.Printf("%s msg %s %s", a.Protocol, event.Author, event.Room) - mx_room_id, err := dbGetMxRoom(a.Protocol, event.Room) + mx_room_id, err = dbGetMxRoom(a.Protocol, event.Room) if err != nil { return } - - err = mxSendMessageAs(mx_room_id, event.Text, mx_user_id) + } else { + mx_room_id, err = dbGetMxPmRoom(a.Protocol, event.Author, mx_user_id, a.MatrixUser, a.AccountName) if err != nil { - log.Printf("Could not send %s as %s", event.Text, mx_user_id) + return } - } else { - // TODO + } + + 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) } } - // TODO } // ---- @@ -142,6 +151,44 @@ func dbGetMxRoom(protocol string, roomId RoomID) (string, error) { return room.MxRoomID, nil } +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 +} + func dbGetMxUser(protocol string, userId UserID) (string, error) { var user DbUserMap -- cgit v1.2.3