| 1 |
// Copyright (c) 2021 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 event |
| 8 |
|
| 9 |
import ( |
| 10 |
"encoding/json" |
| 11 |
"fmt" |
| 12 |
"strings" |
| 13 |
) |
| 14 |
|
| 15 |
type RoomType string |
| 16 |
|
| 17 |
const ( |
| 18 |
RoomTypeDefault RoomType = "" |
| 19 |
RoomTypeSpace RoomType = "m.space" |
| 20 |
) |
| 21 |
|
| 22 |
type TypeClass int |
| 23 |
|
| 24 |
func (tc TypeClass) Name() string { |
| 25 |
switch tc { |
| 26 |
case MessageEventType: |
| 27 |
return "message" |
| 28 |
case StateEventType: |
| 29 |
return "state" |
| 30 |
case EphemeralEventType: |
| 31 |
return "ephemeral" |
| 32 |
case AccountDataEventType: |
| 33 |
return "account data" |
| 34 |
case ToDeviceEventType: |
| 35 |
return "to-device" |
| 36 |
default: |
| 37 |
return "unknown" |
| 38 |
} |
| 39 |
} |
| 40 |
|
| 41 |
const ( |
| 42 |
// Unknown events |
| 43 |
UnknownEventType TypeClass = iota |
| 44 |
// Normal message events |
| 45 |
MessageEventType |
| 46 |
// State events |
| 47 |
StateEventType |
| 48 |
// Ephemeral events |
| 49 |
EphemeralEventType |
| 50 |
// Account data events |
| 51 |
AccountDataEventType |
| 52 |
// Device-to-device events |
| 53 |
ToDeviceEventType |
| 54 |
) |
| 55 |
|
| 56 |
type Type struct { |
| 57 |
Type string |
| 58 |
Class TypeClass |
| 59 |
} |
| 60 |
|
| 61 |
func NewEventType(name string) Type { |
| 62 |
evtType := Type{Type: name} |
| 63 |
evtType.Class = evtType.GuessClass() |
| 64 |
return evtType |
| 65 |
} |
| 66 |
|
| 67 |
func (et *Type) IsState() bool { |
| 68 |
return et.Class == StateEventType |
| 69 |
} |
| 70 |
|
| 71 |
func (et *Type) IsEphemeral() bool { |
| 72 |
return et.Class == EphemeralEventType |
| 73 |
} |
| 74 |
|
| 75 |
func (et *Type) IsAccountData() bool { |
| 76 |
return et.Class == AccountDataEventType |
| 77 |
} |
| 78 |
|
| 79 |
func (et *Type) IsToDevice() bool { |
| 80 |
return et.Class == ToDeviceEventType |
| 81 |
} |
| 82 |
|
| 83 |
func (et *Type) IsInRoomVerification() bool { |
| 84 |
switch et.Type { |
| 85 |
case InRoomVerificationStart.Type, InRoomVerificationReady.Type, InRoomVerificationAccept.Type, |
| 86 |
InRoomVerificationKey.Type, InRoomVerificationMAC.Type, InRoomVerificationCancel.Type: |
| 87 |
return true |
| 88 |
default: |
| 89 |
return false |
| 90 |
} |
| 91 |
} |
| 92 |
|
| 93 |
func (et *Type) IsCall() bool { |
| 94 |
switch et.Type { |
| 95 |
case CallInvite.Type, CallCandidates.Type, CallAnswer.Type, CallReject.Type, CallSelectAnswer.Type, |
| 96 |
CallNegotiate.Type, CallHangup.Type: |
| 97 |
return true |
| 98 |
default: |
| 99 |
return false |
| 100 |
} |
| 101 |
} |
| 102 |
|
| 103 |
func (et *Type) IsCustom() bool { |
| 104 |
return !strings.HasPrefix(et.Type, "m.") |
| 105 |
} |
| 106 |
|
| 107 |
func (et *Type) GuessClass() TypeClass { |
| 108 |
switch et.Type { |
| 109 |
case StateAliases.Type, StateCanonicalAlias.Type, StateCreate.Type, StateJoinRules.Type, StateMember.Type, |
| 110 |
StatePowerLevels.Type, StateRoomName.Type, StateRoomAvatar.Type, StateServerACL.Type, StateTopic.Type, |
| 111 |
StatePinnedEvents.Type, StateTombstone.Type, StateEncryption.Type, StateBridge.Type, StateHalfShotBridge.Type, |
| 112 |
StateSpaceParent.Type, StateSpaceChild.Type, StatePolicyRoom.Type, StatePolicyServer.Type, StatePolicyUser.Type: |
| 113 |
return StateEventType |
| 114 |
case EphemeralEventReceipt.Type, EphemeralEventTyping.Type, EphemeralEventPresence.Type: |
| 115 |
return EphemeralEventType |
| 116 |
case AccountDataDirectChats.Type, AccountDataPushRules.Type, AccountDataRoomTags.Type, |
| 117 |
AccountDataSecretStorageKey.Type, AccountDataSecretStorageDefaultKey.Type, |
| 118 |
AccountDataCrossSigningMaster.Type, AccountDataCrossSigningSelf.Type, AccountDataCrossSigningUser.Type: |
| 119 |
return AccountDataEventType |
| 120 |
case EventRedaction.Type, EventMessage.Type, EventEncrypted.Type, EventReaction.Type, EventSticker.Type, |
| 121 |
InRoomVerificationStart.Type, InRoomVerificationReady.Type, InRoomVerificationAccept.Type, |
| 122 |
InRoomVerificationKey.Type, InRoomVerificationMAC.Type, InRoomVerificationCancel.Type, |
| 123 |
CallInvite.Type, CallCandidates.Type, CallAnswer.Type, CallReject.Type, CallSelectAnswer.Type, |
| 124 |
CallNegotiate.Type, CallHangup.Type: |
| 125 |
return MessageEventType |
| 126 |
case ToDeviceRoomKey.Type, ToDeviceRoomKeyRequest.Type, ToDeviceForwardedRoomKey.Type, ToDeviceRoomKeyWithheld.Type: |
| 127 |
return ToDeviceEventType |
| 128 |
default: |
| 129 |
return UnknownEventType |
| 130 |
} |
| 131 |
} |
| 132 |
|
| 133 |
func (et *Type) UnmarshalJSON(data []byte) error { |
| 134 |
err := json.Unmarshal(data, &et.Type) |
| 135 |
if err != nil { |
| 136 |
return err |
| 137 |
} |
| 138 |
et.Class = et.GuessClass() |
| 139 |
return nil |
| 140 |
} |
| 141 |
|
| 142 |
func (et *Type) MarshalJSON() ([]byte, error) { |
| 143 |
return json.Marshal(&et.Type) |
| 144 |
} |
| 145 |
|
| 146 |
func (et Type) UnmarshalText(data []byte) error { |
| 147 |
et.Type = string(data) |
| 148 |
et.Class = et.GuessClass() |
| 149 |
return nil |
| 150 |
} |
| 151 |
|
| 152 |
func (et Type) MarshalText() ([]byte, error) { |
| 153 |
return []byte(et.Type), nil |
| 154 |
} |
| 155 |
|
| 156 |
func (et *Type) String() string { |
| 157 |
return et.Type |
| 158 |
} |
| 159 |
|
| 160 |
func (et *Type) Repr() string { |
| 161 |
return fmt.Sprintf("%s (%s)", et.Type, et.Class.Name()) |
| 162 |
} |
| 163 |
|
| 164 |
// State events |
| 165 |
var ( |
| 166 |
StateAliases = Type{"m.room.aliases", StateEventType} |
| 167 |
StateCanonicalAlias = Type{"m.room.canonical_alias", StateEventType} |
| 168 |
StateCreate = Type{"m.room.create", StateEventType} |
| 169 |
StateJoinRules = Type{"m.room.join_rules", StateEventType} |
| 170 |
StateHistoryVisibility = Type{"m.room.history_visibility", StateEventType} |
| 171 |
StateGuestAccess = Type{"m.room.guest_access", StateEventType} |
| 172 |
StateMember = Type{"m.room.member", StateEventType} |
| 173 |
StatePowerLevels = Type{"m.room.power_levels", StateEventType} |
| 174 |
StateRoomName = Type{"m.room.name", StateEventType} |
| 175 |
StateTopic = Type{"m.room.topic", StateEventType} |
| 176 |
StateRoomAvatar = Type{"m.room.avatar", StateEventType} |
| 177 |
StatePinnedEvents = Type{"m.room.pinned_events", StateEventType} |
| 178 |
StateServerACL = Type{"m.room.server_acl", StateEventType} |
| 179 |
StateTombstone = Type{"m.room.tombstone", StateEventType} |
| 180 |
StatePolicyRoom = Type{"m.policy.rule.room", StateEventType} |
| 181 |
StatePolicyServer = Type{"m.policy.rule.server", StateEventType} |
| 182 |
StatePolicyUser = Type{"m.policy.rule.user", StateEventType} |
| 183 |
StateEncryption = Type{"m.room.encryption", StateEventType} |
| 184 |
StateBridge = Type{"m.bridge", StateEventType} |
| 185 |
StateHalfShotBridge = Type{"uk.half-shot.bridge", StateEventType} |
| 186 |
StateSpaceChild = Type{"m.space.child", StateEventType} |
| 187 |
StateSpaceParent = Type{"m.space.parent", StateEventType} |
| 188 |
) |
| 189 |
|
| 190 |
// Message events |
| 191 |
var ( |
| 192 |
EventRedaction = Type{"m.room.redaction", MessageEventType} |
| 193 |
EventMessage = Type{"m.room.message", MessageEventType} |
| 194 |
EventEncrypted = Type{"m.room.encrypted", MessageEventType} |
| 195 |
EventReaction = Type{"m.reaction", MessageEventType} |
| 196 |
EventSticker = Type{"m.sticker", MessageEventType} |
| 197 |
|
| 198 |
InRoomVerificationStart = Type{"m.key.verification.start", MessageEventType} |
| 199 |
InRoomVerificationReady = Type{"m.key.verification.ready", MessageEventType} |
| 200 |
InRoomVerificationAccept = Type{"m.key.verification.accept", MessageEventType} |
| 201 |
InRoomVerificationKey = Type{"m.key.verification.key", MessageEventType} |
| 202 |
InRoomVerificationMAC = Type{"m.key.verification.mac", MessageEventType} |
| 203 |
InRoomVerificationCancel = Type{"m.key.verification.cancel", MessageEventType} |
| 204 |
|
| 205 |
CallInvite = Type{"m.call.invite", MessageEventType} |
| 206 |
CallCandidates = Type{"m.call.candidates", MessageEventType} |
| 207 |
CallAnswer = Type{"m.call.answer", MessageEventType} |
| 208 |
CallReject = Type{"m.call.reject", MessageEventType} |
| 209 |
CallSelectAnswer = Type{"m.call.select_answer", MessageEventType} |
| 210 |
CallNegotiate = Type{"m.call.negotiate", MessageEventType} |
| 211 |
CallHangup = Type{"m.call.hangup", MessageEventType} |
| 212 |
) |
| 213 |
|
| 214 |
// Ephemeral events |
| 215 |
var ( |
| 216 |
EphemeralEventReceipt = Type{"m.receipt", EphemeralEventType} |
| 217 |
EphemeralEventTyping = Type{"m.typing", EphemeralEventType} |
| 218 |
EphemeralEventPresence = Type{"m.presence", EphemeralEventType} |
| 219 |
) |
| 220 |
|
| 221 |
// Account data events |
| 222 |
var ( |
| 223 |
AccountDataDirectChats = Type{"m.direct", AccountDataEventType} |
| 224 |
AccountDataPushRules = Type{"m.push_rules", AccountDataEventType} |
| 225 |
AccountDataRoomTags = Type{"m.tag", AccountDataEventType} |
| 226 |
AccountDataFullyRead = Type{"m.fully_read", AccountDataEventType} |
| 227 |
AccountDataIgnoredUserList = Type{"m.ignored_user_list", AccountDataEventType} |
| 228 |
|
| 229 |
AccountDataSecretStorageDefaultKey = Type{"m.secret_storage.default_key", AccountDataEventType} |
| 230 |
AccountDataSecretStorageKey = Type{"m.secret_storage.key", AccountDataEventType} |
| 231 |
AccountDataCrossSigningMaster = Type{"m.cross_signing.master", AccountDataEventType} |
| 232 |
AccountDataCrossSigningUser = Type{"m.cross_signing.user_signing", AccountDataEventType} |
| 233 |
AccountDataCrossSigningSelf = Type{"m.cross_signing.self_signing", AccountDataEventType} |
| 234 |
) |
| 235 |
|
| 236 |
// Device-to-device events |
| 237 |
var ( |
| 238 |
ToDeviceRoomKey = Type{"m.room_key", ToDeviceEventType} |
| 239 |
ToDeviceRoomKeyRequest = Type{"m.room_key_request", ToDeviceEventType} |
| 240 |
ToDeviceForwardedRoomKey = Type{"m.forwarded_room_key", ToDeviceEventType} |
| 241 |
ToDeviceEncrypted = Type{"m.room.encrypted", ToDeviceEventType} |
| 242 |
ToDeviceRoomKeyWithheld = Type{"m.room_key.withheld", ToDeviceEventType} |
| 243 |
ToDeviceDummy = Type{"m.dummy", ToDeviceEventType} |
| 244 |
ToDeviceVerificationRequest = Type{"m.key.verification.request", ToDeviceEventType} |
| 245 |
ToDeviceVerificationStart = Type{"m.key.verification.start", ToDeviceEventType} |
| 246 |
ToDeviceVerificationAccept = Type{"m.key.verification.accept", ToDeviceEventType} |
| 247 |
ToDeviceVerificationKey = Type{"m.key.verification.key", ToDeviceEventType} |
| 248 |
ToDeviceVerificationMAC = Type{"m.key.verification.mac", ToDeviceEventType} |
| 249 |
ToDeviceVerificationCancel = Type{"m.key.verification.cancel", ToDeviceEventType} |
| 250 |
|
| 251 |
ToDeviceOrgMatrixRoomKeyWithheld = Type{"org.matrix.room_key.withheld", ToDeviceEventType} |
| 252 |
) |