aboutsummaryrefslogtreecommitdiff
path: root/appservice/account.go
diff options
context:
space:
mode:
Diffstat (limited to 'appservice/account.go')
-rw-r--r--appservice/account.go65
1 files changed, 56 insertions, 9 deletions
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