| 1 |
package mautrix |
| 2 |
|
| 3 |
import ( |
| 4 |
"encoding/json" |
| 5 |
|
| 6 |
"maunium.net/go/mautrix/event" |
| 7 |
"maunium.net/go/mautrix/id" |
| 8 |
"maunium.net/go/mautrix/pushrules" |
| 9 |
) |
| 10 |
|
| 11 |
type AuthType string |
| 12 |
|
| 13 |
const ( |
| 14 |
AuthTypePassword AuthType = "m.login.password" |
| 15 |
AuthTypeReCAPTCHA AuthType = "m.login.recaptcha" |
| 16 |
AuthTypeOAuth2 AuthType = "m.login.oauth2" |
| 17 |
AuthTypeSSO AuthType = "m.login.sso" |
| 18 |
AuthTypeEmail AuthType = "m.login.email.identity" |
| 19 |
AuthTypeMSISDN AuthType = "m.login.msisdn" |
| 20 |
AuthTypeToken AuthType = "m.login.token" |
| 21 |
AuthTypeDummy AuthType = "m.login.dummy" |
| 22 |
AuthTypeAppservice AuthType = "m.login.application_service" |
| 23 |
) |
| 24 |
|
| 25 |
type IdentifierType string |
| 26 |
|
| 27 |
const ( |
| 28 |
IdentifierTypeUser = "m.id.user" |
| 29 |
IdentifierTypeThirdParty = "m.id.thirdparty" |
| 30 |
IdentifierTypePhone = "m.id.phone" |
| 31 |
) |
| 32 |
|
| 33 |
// ReqRegister is the JSON request for https://spec.matrix.org/v1.2/client-server-api/#post_matrixclientv3register |
| 34 |
type ReqRegister struct { |
| 35 |
Username string `json:"username,omitempty"` |
| 36 |
Password string `json:"password,omitempty"` |
| 37 |
DeviceID id.DeviceID `json:"device_id,omitempty"` |
| 38 |
InitialDeviceDisplayName string `json:"initial_device_display_name,omitempty"` |
| 39 |
InhibitLogin bool `json:"inhibit_login,omitempty"` |
| 40 |
Auth interface{} `json:"auth,omitempty"` |
| 41 |
|
| 42 |
// Type for registration, only used for appservice user registrations |
| 43 |
// https://spec.matrix.org/v1.2/application-service-api/#server-admin-style-permissions |
| 44 |
Type AuthType `json:"type,omitempty"` |
| 45 |
} |
| 46 |
|
| 47 |
type BaseAuthData struct { |
| 48 |
Type AuthType `json:"type"` |
| 49 |
Session string `json:"session,omitempty"` |
| 50 |
} |
| 51 |
|
| 52 |
type UserIdentifier struct { |
| 53 |
Type IdentifierType `json:"type"` |
| 54 |
|
| 55 |
User string `json:"user,omitempty"` |
| 56 |
|
| 57 |
Medium string `json:"medium,omitempty"` |
| 58 |
Address string `json:"address,omitempty"` |
| 59 |
|
| 60 |
Country string `json:"country,omitempty"` |
| 61 |
Phone string `json:"phone,omitempty"` |
| 62 |
} |
| 63 |
|
| 64 |
// ReqLogin is the JSON request for https://spec.matrix.org/v1.2/client-server-api/#post_matrixclientv3login |
| 65 |
type ReqLogin struct { |
| 66 |
Type AuthType `json:"type"` |
| 67 |
Identifier UserIdentifier `json:"identifier"` |
| 68 |
Password string `json:"password,omitempty"` |
| 69 |
Token string `json:"token,omitempty"` |
| 70 |
DeviceID id.DeviceID `json:"device_id,omitempty"` |
| 71 |
InitialDeviceDisplayName string `json:"initial_device_display_name,omitempty"` |
| 72 |
|
| 73 |
// Whether or not the returned credentials should be stored in the Client |
| 74 |
StoreCredentials bool `json:"-"` |
| 75 |
// Whether or not the returned .well-known data should update the homeserver URL in the Client |
| 76 |
StoreHomeserverURL bool `json:"-"` |
| 77 |
} |
| 78 |
|
| 79 |
type ReqUIAuthFallback struct { |
| 80 |
Session string `json:"session"` |
| 81 |
User string `json:"user"` |
| 82 |
} |
| 83 |
|
| 84 |
type ReqUIAuthLogin struct { |
| 85 |
BaseAuthData |
| 86 |
User string `json:"user"` |
| 87 |
Password string `json:"password"` |
| 88 |
} |
| 89 |
|
| 90 |
// ReqCreateRoom is the JSON request for https://spec.matrix.org/v1.2/client-server-api/#post_matrixclientv3createroom |
| 91 |
type ReqCreateRoom struct { |
| 92 |
Visibility string `json:"visibility,omitempty"` |
| 93 |
RoomAliasName string `json:"room_alias_name,omitempty"` |
| 94 |
Name string `json:"name,omitempty"` |
| 95 |
Topic string `json:"topic,omitempty"` |
| 96 |
Invite []id.UserID `json:"invite,omitempty"` |
| 97 |
Invite3PID []ReqInvite3PID `json:"invite_3pid,omitempty"` |
| 98 |
CreationContent map[string]interface{} `json:"creation_content,omitempty"` |
| 99 |
InitialState []*event.Event `json:"initial_state,omitempty"` |
| 100 |
Preset string `json:"preset,omitempty"` |
| 101 |
IsDirect bool `json:"is_direct,omitempty"` |
| 102 |
|
| 103 |
PowerLevelOverride *event.PowerLevelsEventContent `json:"power_level_content_override,omitempty"` |
| 104 |
} |
| 105 |
|
| 106 |
// ReqRedact is the JSON request for https://spec.matrix.org/v1.2/client-server-api/#put_matrixclientv3roomsroomidredacteventidtxnid |
| 107 |
type ReqRedact struct { |
| 108 |
Reason string |
| 109 |
TxnID string |
| 110 |
Extra map[string]interface{} |
| 111 |
} |
| 112 |
|
| 113 |
type ReqMembers struct { |
| 114 |
At string `json:"at"` |
| 115 |
Membership event.Membership `json:"membership,omitempty"` |
| 116 |
NotMembership event.Membership `json:"not_membership,omitempty"` |
| 117 |
} |
| 118 |
|
| 119 |
// ReqInvite3PID is the JSON request for https://spec.matrix.org/v1.2/client-server-api/#post_matrixclientv3roomsroomidinvite-1 |
| 120 |
// It is also a JSON object used in https://spec.matrix.org/v1.2/client-server-api/#post_matrixclientv3createroom |
| 121 |
type ReqInvite3PID struct { |
| 122 |
IDServer string `json:"id_server"` |
| 123 |
Medium string `json:"medium"` |
| 124 |
Address string `json:"address"` |
| 125 |
} |
| 126 |
|
| 127 |
type ReqLeave struct { |
| 128 |
Reason string `json:"reason,omitempty"` |
| 129 |
} |
| 130 |
|
| 131 |
// ReqInviteUser is the JSON request for https://spec.matrix.org/v1.2/client-server-api/#post_matrixclientv3roomsroomidinvite |
| 132 |
type ReqInviteUser struct { |
| 133 |
Reason string `json:"reason,omitempty"` |
| 134 |
UserID id.UserID `json:"user_id"` |
| 135 |
} |
| 136 |
|
| 137 |
// ReqKickUser is the JSON request for https://spec.matrix.org/v1.2/client-server-api/#post_matrixclientv3roomsroomidkick |
| 138 |
type ReqKickUser struct { |
| 139 |
Reason string `json:"reason,omitempty"` |
| 140 |
UserID id.UserID `json:"user_id"` |
| 141 |
} |
| 142 |
|
| 143 |
// ReqBanUser is the JSON request for https://spec.matrix.org/v1.2/client-server-api/#post_matrixclientv3roomsroomidban |
| 144 |
type ReqBanUser struct { |
| 145 |
Reason string `json:"reason,omitempty"` |
| 146 |
UserID id.UserID `json:"user_id"` |
| 147 |
} |
| 148 |
|
| 149 |
// ReqUnbanUser is the JSON request for https://spec.matrix.org/v1.2/client-server-api/#post_matrixclientv3roomsroomidunban |
| 150 |
type ReqUnbanUser struct { |
| 151 |
Reason string `json:"reason,omitempty"` |
| 152 |
UserID id.UserID `json:"user_id"` |
| 153 |
} |
| 154 |
|
| 155 |
// ReqTyping is the JSON request for https://spec.matrix.org/v1.2/client-server-api/#put_matrixclientv3roomsroomidtypinguserid |
| 156 |
type ReqTyping struct { |
| 157 |
Typing bool `json:"typing"` |
| 158 |
Timeout int64 `json:"timeout,omitempty"` |
| 159 |
} |
| 160 |
|
| 161 |
type ReqPresence struct { |
| 162 |
Presence event.Presence `json:"presence"` |
| 163 |
} |
| 164 |
|
| 165 |
type ReqAliasCreate struct { |
| 166 |
RoomID id.RoomID `json:"room_id"` |
| 167 |
} |
| 168 |
|
| 169 |
type OneTimeKey struct { |
| 170 |
Key id.Curve25519 `json:"key"` |
| 171 |
IsSigned bool `json:"-"` |
| 172 |
Signatures Signatures `json:"signatures,omitempty"` |
| 173 |
Unsigned map[string]interface{} `json:"unsigned,omitempty"` |
| 174 |
} |
| 175 |
|
| 176 |
type serializableOTK OneTimeKey |
| 177 |
|
| 178 |
func (otk *OneTimeKey) UnmarshalJSON(data []byte) (err error) { |
| 179 |
if len(data) > 0 && data[0] == '"' && data[len(data)-1] == '"' { |
| 180 |
err = json.Unmarshal(data, &otk.Key) |
| 181 |
otk.Signatures = nil |
| 182 |
otk.Unsigned = nil |
| 183 |
otk.IsSigned = false |
| 184 |
} else { |
| 185 |
err = json.Unmarshal(data, (*serializableOTK)(otk)) |
| 186 |
otk.IsSigned = true |
| 187 |
} |
| 188 |
return err |
| 189 |
} |
| 190 |
|
| 191 |
func (otk *OneTimeKey) MarshalJSON() ([]byte, error) { |
| 192 |
if !otk.IsSigned { |
| 193 |
return json.Marshal(otk.Key) |
| 194 |
} else { |
| 195 |
return json.Marshal((*serializableOTK)(otk)) |
| 196 |
} |
| 197 |
} |
| 198 |
|
| 199 |
type ReqUploadKeys struct { |
| 200 |
DeviceKeys *DeviceKeys `json:"device_keys,omitempty"` |
| 201 |
OneTimeKeys map[id.KeyID]OneTimeKey `json:"one_time_keys"` |
| 202 |
} |
| 203 |
|
| 204 |
type ReqKeysSignatures struct { |
| 205 |
UserID id.UserID `json:"user_id"` |
| 206 |
DeviceID id.DeviceID `json:"device_id,omitempty"` |
| 207 |
Algorithms []id.Algorithm `json:"algorithms,omitempty"` |
| 208 |
Usage []id.CrossSigningUsage `json:"usage,omitempty"` |
| 209 |
Keys map[id.KeyID]string `json:"keys"` |
| 210 |
Signatures Signatures `json:"signatures"` |
| 211 |
} |
| 212 |
|
| 213 |
type ReqUploadSignatures map[id.UserID]map[string]ReqKeysSignatures |
| 214 |
|
| 215 |
type DeviceKeys struct { |
| 216 |
UserID id.UserID `json:"user_id"` |
| 217 |
DeviceID id.DeviceID `json:"device_id"` |
| 218 |
Algorithms []id.Algorithm `json:"algorithms"` |
| 219 |
Keys KeyMap `json:"keys"` |
| 220 |
Signatures Signatures `json:"signatures"` |
| 221 |
Unsigned map[string]interface{} `json:"unsigned,omitempty"` |
| 222 |
} |
| 223 |
|
| 224 |
type CrossSigningKeys struct { |
| 225 |
UserID id.UserID `json:"user_id"` |
| 226 |
Usage []id.CrossSigningUsage `json:"usage"` |
| 227 |
Keys map[id.KeyID]id.Ed25519 `json:"keys"` |
| 228 |
Signatures map[id.UserID]map[id.KeyID]string `json:"signatures,omitempty"` |
| 229 |
} |
| 230 |
|
| 231 |
func (csk *CrossSigningKeys) FirstKey() id.Ed25519 { |
| 232 |
for _, key := range csk.Keys { |
| 233 |
return key |
| 234 |
} |
| 235 |
return "" |
| 236 |
} |
| 237 |
|
| 238 |
type UploadCrossSigningKeysReq struct { |
| 239 |
Master CrossSigningKeys `json:"master_key"` |
| 240 |
SelfSigning CrossSigningKeys `json:"self_signing_key"` |
| 241 |
UserSigning CrossSigningKeys `json:"user_signing_key"` |
| 242 |
Auth interface{} `json:"auth,omitempty"` |
| 243 |
} |
| 244 |
|
| 245 |
type KeyMap map[id.DeviceKeyID]string |
| 246 |
|
| 247 |
func (km KeyMap) GetEd25519(deviceID id.DeviceID) id.Ed25519 { |
| 248 |
val, ok := km[id.NewDeviceKeyID(id.KeyAlgorithmEd25519, deviceID)] |
| 249 |
if !ok { |
| 250 |
return "" |
| 251 |
} |
| 252 |
return id.Ed25519(val) |
| 253 |
} |
| 254 |
|
| 255 |
func (km KeyMap) GetCurve25519(deviceID id.DeviceID) id.Curve25519 { |
| 256 |
val, ok := km[id.NewDeviceKeyID(id.KeyAlgorithmCurve25519, deviceID)] |
| 257 |
if !ok { |
| 258 |
return "" |
| 259 |
} |
| 260 |
return id.Curve25519(val) |
| 261 |
} |
| 262 |
|
| 263 |
type Signatures map[id.UserID]map[id.KeyID]string |
| 264 |
|
| 265 |
type ReqQueryKeys struct { |
| 266 |
DeviceKeys DeviceKeysRequest `json:"device_keys"` |
| 267 |
|
| 268 |
Timeout int64 `json:"timeout,omitempty"` |
| 269 |
Token string `json:"token,omitempty"` |
| 270 |
} |
| 271 |
|
| 272 |
type DeviceKeysRequest map[id.UserID]DeviceIDList |
| 273 |
|
| 274 |
type DeviceIDList []id.DeviceID |
| 275 |
|
| 276 |
type ReqClaimKeys struct { |
| 277 |
OneTimeKeys OneTimeKeysRequest `json:"one_time_keys"` |
| 278 |
|
| 279 |
Timeout int64 `json:"timeout,omitempty"` |
| 280 |
} |
| 281 |
|
| 282 |
type OneTimeKeysRequest map[id.UserID]map[id.DeviceID]id.KeyAlgorithm |
| 283 |
|
| 284 |
type ReqSendToDevice struct { |
| 285 |
Messages map[id.UserID]map[id.DeviceID]*event.Content `json:"messages"` |
| 286 |
} |
| 287 |
|
| 288 |
// ReqDeviceInfo is the JSON request for https://spec.matrix.org/v1.2/client-server-api/#put_matrixclientv3devicesdeviceid |
| 289 |
type ReqDeviceInfo struct { |
| 290 |
DisplayName string `json:"display_name,omitempty"` |
| 291 |
} |
| 292 |
|
| 293 |
// ReqDeleteDevice is the JSON request for https://spec.matrix.org/v1.2/client-server-api/#delete_matrixclientv3devicesdeviceid |
| 294 |
type ReqDeleteDevice struct { |
| 295 |
Auth interface{} `json:"auth,omitempty"` |
| 296 |
} |
| 297 |
|
| 298 |
// ReqDeleteDevices is the JSON request for https://spec.matrix.org/v1.2/client-server-api/#post_matrixclientv3delete_devices |
| 299 |
type ReqDeleteDevices struct { |
| 300 |
Devices []id.DeviceID `json:"devices"` |
| 301 |
Auth interface{} `json:"auth,omitempty"` |
| 302 |
} |
| 303 |
|
| 304 |
type ReqPutPushRule struct { |
| 305 |
Before string `json:"-"` |
| 306 |
After string `json:"-"` |
| 307 |
|
| 308 |
Actions []pushrules.PushActionType `json:"actions"` |
| 309 |
Conditions []pushrules.PushCondition `json:"conditions"` |
| 310 |
Pattern string `json:"pattern"` |
| 311 |
} |
| 312 |
|
| 313 |
type ReqBatchSend struct { |
| 314 |
PrevEventID id.EventID `json:"-"` |
| 315 |
BatchID id.BatchID `json:"-"` |
| 316 |
|
| 317 |
StateEventsAtStart []*event.Event `json:"state_events_at_start"` |
| 318 |
Events []*event.Event `json:"events"` |
| 319 |
} |
| 320 |
|
| 321 |
type ReqSetReadMarkers struct { |
| 322 |
Read id.EventID `json:"m.read"` |
| 323 |
FullyRead id.EventID `json:"m.fully_read"` |
| 324 |
} |