aboutsummaryrefslogtreecommitdiff
path: root/appservice/server.go
diff options
context:
space:
mode:
Diffstat (limited to 'appservice/server.go')
-rw-r--r--appservice/server.go47
1 files changed, 47 insertions, 0 deletions
diff --git a/appservice/server.go b/appservice/server.go
index 3f52119..ed206ee 100644
--- a/appservice/server.go
+++ b/appservice/server.go
@@ -8,6 +8,7 @@ import (
"net/http"
"git.deuxfleurs.fr/Deuxfleurs/easybridge/mxlib"
+ "git.deuxfleurs.fr/Deuxfleurs/easybridge/connector"
"github.com/gorilla/mux"
)
@@ -72,8 +73,54 @@ func handleTxn(w http.ResponseWriter, r *http.Request) {
}
log.Printf("Got transaction %#v\n", txn)
+
+ for i := range txn.Events {
+ handleTxnEvent(&txn.Events[i])
+ }
+
fmt.Fprintf(w, "{}\n")
} else {
http.Error(w, "Expected PUT request", http.StatusBadRequest)
}
}
+
+func handleTxnEvent(e *mxlib.Event) {
+ if e.Type == "m.room.message" {
+ ev := &connector.Event{
+ Type: connector.EVENT_MESSAGE,
+ Text: e.Content["body"].(string),
+ }
+ typ := e.Content["msgtype"].(string)
+ if typ == "m.emote" {
+ ev.Type = connector.EVENT_MESSAGE
+ }
+
+ // Look up if this is a private message room
+ var pm_room DbPmRoomMap
+ is_pm_room := !db.First(&pm_room, DbPmRoomMap{
+ MxRoomID: e.RoomId,
+ }).RecordNotFound()
+ if is_pm_room {
+ acct := FindAccount(pm_room.MxUserID, pm_room.AccountName)
+ if acct != nil && e.Sender == pm_room.MxUserID {
+ ev.Author = acct.Conn.User()
+ ev.Recipient = pm_room.UserID
+ acct.Conn.Send(ev)
+ }
+ }
+
+ // Look up if this is a regular room
+ var room DbRoomMap
+ is_room := !db.First(&room, DbRoomMap{
+ MxRoomID: e.RoomId,
+ }).RecordNotFound()
+ if is_room {
+ acct := FindJoinedAccount(e.Sender, room.Protocol, room.RoomID)
+ if acct != nil {
+ ev.Author = acct.Conn.User()
+ ev.Room = room.RoomID
+ acct.Conn.Send(ev)
+ }
+ }
+ }
+}