aboutsummaryrefslogtreecommitdiff
path: root/appservice/server.go
blob: ed206ee673efab432709d547e0591a1ff843320a (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
package appservice

import (
	"encoding/json"
	"fmt"
	"strings"
	"log"
	"net/http"

	"git.deuxfleurs.fr/Deuxfleurs/easybridge/mxlib"
	"git.deuxfleurs.fr/Deuxfleurs/easybridge/connector"
	"github.com/gorilla/mux"
)

type Config struct {
	HttpBindAddr string
	Server string
	DbType string
	DbPath string
	MatrixDomain string
}


var registration *mxlib.Registration
var config *Config

func Start(r *mxlib.Registration, c *Config) (chan error, error) {
	registration = r
	config = c

	err := InitDb()
	if err != nil {
		return nil, err
	}

	router := mux.NewRouter()
	router.HandleFunc("/_matrix/app/v1/transactions/{txnId}", handleTxn)
	router.HandleFunc("/transactions/{txnId}", handleTxn)

	errch := make(chan error)
	go func() {
		log.Printf("Starting HTTP server on %s", config.HttpBindAddr)
		err := http.ListenAndServe(config.HttpBindAddr, checkTokenAndLog(router))
		if err != nil {
			errch <- err
		}
	}()

	return errch, nil
}

func checkTokenAndLog(handler http.Handler) http.Handler {
	return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
		r.ParseForm()
		if strings.Join(r.Form["access_token"], "") != registration.HsToken {
			http.Error(w, "Wrong or no token provided", http.StatusUnauthorized)
			return
		}

		log.Printf("%s %s %s\n", r.RemoteAddr, r.Method, r.URL)
		handler.ServeHTTP(w, r)
	})
}

func handleTxn(w http.ResponseWriter, r *http.Request) {
	if r.Method == "PUT" {
		var txn mxlib.Transaction
		err := json.NewDecoder(r.Body).Decode(&txn)
		if err != nil {
			http.Error(w, err.Error(), http.StatusBadRequest)
			log.Printf("JSON decode error: %s\n", err)
			return
		}

		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)
			}
		}
	}
}