Debian Codegraph @

appservice/eventprocessor.go

raw
1 // Copyright (c) 2020 Tulir Asokan
2 //
3 // This Source Code Form is subject to the terms of the Mozilla Public
4 // License, v. 2.0. If a copy of the MPL was not distributed with this
5 // file, You can obtain one at http://mozilla.org/MPL/2.0/.
6
7 package appservice
8
9 import (
10 "encoding/json"
11 "runtime/debug"
12
13 log "maunium.net/go/maulogger/v2"
14
15 "maunium.net/go/mautrix"
16 "maunium.net/go/mautrix/event"
17 )
18
19 type ExecMode uint8
20
21 const (
22 AsyncHandlers ExecMode = iota
23 AsyncLoop
24 Sync
25 )
26
27 type EventHandler func(evt *event.Event)
28 type OTKHandler func(otk *mautrix.OTKCount)
29 type DeviceListHandler func(otk *mautrix.DeviceLists, since string)
30
31 type EventProcessor struct {
32 ExecMode ExecMode
33
34 as *AppService
35 log log.Logger
36 stop chan struct{}
37 handlers map[event.Type][]EventHandler
38
39 otkHandlers []OTKHandler
40 deviceListHandlers []DeviceListHandler
41 }
42
43 func NewEventProcessor(as *AppService) *EventProcessor {
44 return &EventProcessor{
45 ExecMode: AsyncHandlers,
46 as: as,
47 log: as.Log.Sub("Events"),
48 stop: make(chan struct{}, 1),
49 handlers: make(map[event.Type][]EventHandler),
50
51 otkHandlers: make([]OTKHandler, 0),
52 deviceListHandlers: make([]DeviceListHandler, 0),
53 }
54 }
55
56 func (ep *EventProcessor) On(evtType event.Type, handler EventHandler) {
57 handlers, ok := ep.handlers[evtType]
58 if !ok {
59 handlers = []EventHandler{handler}
60 } else {
61 handlers = append(handlers, handler)
62 }
63 ep.handlers[evtType] = handlers
64 }
65
66 func (ep *EventProcessor) OnOTK(handler OTKHandler) {
67 ep.otkHandlers = append(ep.otkHandlers, handler)
68 }
69
70 func (ep *EventProcessor) OnDeviceList(handler DeviceListHandler) {
71 ep.deviceListHandlers = append(ep.deviceListHandlers, handler)
72 }
73
74 func (ep *EventProcessor) recoverFunc(data interface{}) {
75 if err := recover(); err != nil {
76 d, _ := json.Marshal(data)
77 ep.log.Errorfln("Panic in Matrix event handler: %v (event content: %s):\n%s", err, string(d), string(debug.Stack()))
78 }
79 }
80
81 func (ep *EventProcessor) callHandler(handler EventHandler, evt *event.Event) {
82 defer ep.recoverFunc(evt)
83 handler(evt)
84 }
85
86 func (ep *EventProcessor) callOTKHandler(handler OTKHandler, otk *mautrix.OTKCount) {
87 defer ep.recoverFunc(otk)
88 handler(otk)
89 }
90
91 func (ep *EventProcessor) callDeviceListHandler(handler DeviceListHandler, dl *mautrix.DeviceLists) {
92 defer ep.recoverFunc(dl)
93 handler(dl, "")
94 }
95
96 func (ep *EventProcessor) DispatchOTK(otk *mautrix.OTKCount) {
97 for _, handler := range ep.otkHandlers {
98 go ep.callOTKHandler(handler, otk)
99 }
100 }
101
102 func (ep *EventProcessor) DispatchDeviceList(dl *mautrix.DeviceLists) {
103 for _, handler := range ep.deviceListHandlers {
104 go ep.callDeviceListHandler(handler, dl)
105 }
106 }
107
108 func (ep *EventProcessor) Dispatch(evt *event.Event) {
109 handlers, ok := ep.handlers[evt.Type]
110 if !ok {
111 return
112 }
113 switch ep.ExecMode {
114 case AsyncHandlers:
115 for _, handler := range handlers {
116 go ep.callHandler(handler, evt)
117 }
118 case AsyncLoop:
119 go func() {
120 for _, handler := range handlers {
121 ep.callHandler(handler, evt)
122 }
123 }()
124 case Sync:
125 for _, handler := range handlers {
126 ep.callHandler(handler, evt)
127 }
128 }
129 }
130
131 func (ep *EventProcessor) Start() {
132 for {
133 select {
134 case evt := <-ep.as.Events:
135 ep.Dispatch(evt)
136 case otk := <-ep.as.OTKCounts:
137 ep.DispatchOTK(otk)
138 case dl := <-ep.as.DeviceLists:
139 ep.DispatchDeviceList(dl)
140 case <-ep.stop:
141 return
142 }
143 }
144 }
145
146 func (ep *EventProcessor) Stop() {
147 ep.stop <- struct{}{}
148 }