-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathmemberhandler.go
185 lines (176 loc) · 6.37 KB
/
memberhandler.go
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
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
package main
import (
"context"
"errors"
"fmt"
"sync"
"github.com/rs/zerolog"
"maunium.net/go/mautrix"
"maunium.net/go/mautrix/event"
"maunium.net/go/mautrix/id"
)
func handleMember(source mautrix.EventSource, evt *event.Event) {
content := evt.Content.AsMember()
prevMembership := event.MembershipLeave
if evt.Unsigned.PrevContent != nil {
prevMembership = evt.Unsigned.PrevContent.AsMember().Membership
}
log := globalLog.With().
Str("event_id", evt.ID.String()).
Str("action", "membership event").
Logger()
ctx := log.WithContext(context.WithValue(context.Background(), contextKeyEvent, evt))
log.Debug().
Str("room_id", evt.RoomID.String()).
Str("sender", evt.Sender.String()).
Str("state_key", evt.GetStateKey()).
Str("membership", string(content.Membership)).
Str("prev_membership", string(prevMembership)).
Str("source", source.String()).
Msg("Received member event")
if evt.GetStateKey() == cli.UserID.String() {
if content.Membership != event.MembershipInvite || source&mautrix.EventSourceInvite == 0 {
log.Debug().Msg("Ignoring non-invite member event for self")
} else if evt.Sender.Homeserver() != cli.UserID.Homeserver() {
log.Debug().Msg("Rejecting invite from user on different homeserver")
leaveRoom(ctx, evt.RoomID, fmt.Sprintf("This bot only serves users on %s", cli.UserID.Homeserver()))
} else if bot, _ := db.GetBot(ctx, id.UserID(evt.GetStateKey())); bot != nil {
log.Debug().Msg("Rejecting invite from a bot managed by this bot")
leaveRoom(ctx, evt.RoomID, "Bots can't have their own bots")
} else if stateProblem := inviteLooksPrivate(evt); stateProblem != "" {
log.Debug().
Str("problem", stateProblem).
Msg("Rejecting invite to room that doesn't look like a private chat")
leaveRoom(ctx, evt.RoomID, "This bot only accepts invites to encrypted direct chats")
} else {
log.Debug().Msg("Accepting direct chat invite")
acceptInvite(ctx, evt)
}
} else if source&mautrix.EventSourceJoin > 0 {
_, err := getOtherUserID(ctx, evt.RoomID, false, true)
if errors.Is(err, errWrongMemberCount) {
log.Debug().Msg("Room has more than 2 members now, leaving")
leaveRoom(ctx, evt.RoomID, "Another user joined the room")
} else if errors.Is(err, errOtherMemberIsBot) {
log.Debug().Msg("Other member in room is a bot, leaving")
leaveRoom(ctx, evt.RoomID, "Bots can't have their own bots")
} else if err != nil {
log.Err(err).Msg("Failed to get members in room after member event from someone else")
}
} else {
log.Debug().Msg("Ignoring state event in invite stream")
}
}
func leaveRoom(ctx context.Context, roomID id.RoomID, reason string) {
_, err := cli.LeaveRoom(roomID, &mautrix.ReqLeave{Reason: reason})
if err != nil {
zerolog.Ctx(ctx).Err(err).Str("reason", reason).Msg("Failed to leave room")
}
}
func acceptInvite(ctx context.Context, evt *event.Event) {
log := zerolog.Ctx(ctx)
_, err := cli.JoinRoomByID(evt.RoomID)
if err != nil {
log.Err(err).Msg("Failed to accept invite")
return
}
_, err = getOtherUserID(ctx, evt.RoomID, false, false)
if errors.Is(err, errWrongMemberCount) {
log.Debug().Msg("Room has more than 2 members after accepting invite, leaving")
leaveRoom(ctx, evt.RoomID, "This bot only accepts direct chat invites")
} else if errors.Is(err, errOtherMemberIsBot) {
log.Debug().Msg("Other member in room is a bot, leaving")
leaveRoom(ctx, evt.RoomID, "Bots can't have their own bots")
} else if err != nil {
log.Err(err).Msg("Failed to check members in room after accepting invite")
leaveRoom(ctx, evt.RoomID, "Failed to check members in room")
} else {
log.Debug().Msg("Room only has 2 members, staying in room")
}
}
func inviteLooksPrivate(evt *event.Event) string {
if !evt.Content.AsMember().IsDirect {
return "invite isn't flagged as direct"
}
isEncrypted := false
for _, stateEvt := range evt.Unsigned.InviteRoomState {
switch stateEvt.Type {
case event.StateJoinRules:
if stateEvt.Content.AsJoinRules().JoinRule != event.JoinRuleInvite {
return "join rule is not invite"
}
case event.StateCreate:
if stateEvt.Sender != evt.Sender {
return "room was created by different user"
}
case event.StateRoomName:
return "room has name"
case event.StateRoomAvatar:
return "room has avatar"
case event.StateTopic:
return "room has topic"
case event.StateCanonicalAlias:
return "room has canonical alias"
case event.StateEncryption:
if stateEvt.Content.AsEncryption().Algorithm != id.AlgorithmMegolmV1 {
return "room has unknown encryption algorithm"
}
isEncrypted = true
}
}
if !isEncrypted {
return "room is not encrypted"
}
return ""
}
var errWrongMemberCount = errors.New("room has more than 2 members")
var errOtherMemberIsBot = errors.New("other member is a bot")
var memberValidatedRooms = make(map[id.RoomID]id.UserID)
var memberValidatedRoomsLock sync.Mutex
func getOtherUserID(ctx context.Context, roomID id.RoomID, allowMemoryCache, allowDBCache bool) (id.UserID, error) {
memberValidatedRoomsLock.Lock()
defer memberValidatedRoomsLock.Unlock()
if allowMemoryCache {
if userID, ok := memberValidatedRooms[roomID]; ok {
return userID, nil
}
}
log := zerolog.Ctx(ctx)
members, err := cli.StateStore.GetRoomJoinedOrInvitedMembers(roomID)
if err != nil {
return "", fmt.Errorf("failed to fetch members from cache: %w", err)
}
cached := true
if len(members) == 0 || !allowDBCache {
cached = false
log.Debug().Msg("No members in cache, fetching from server")
joinedMembers, err := cli.JoinedMembers(roomID)
if err != nil {
return "", fmt.Errorf("failed to fetch members from server: %w", err)
}
members = make([]id.UserID, 0, len(joinedMembers.Joined))
for userID := range joinedMembers.Joined {
members = append(members, userID)
}
}
log.Debug().Interface("members", members).Bool("from_cache", cached).Msg("Got members in room")
if len(members) != 2 {
memberValidatedRooms[roomID] = ""
return "", errWrongMemberCount
}
var otherUserID id.UserID
if members[0] == cli.UserID {
otherUserID = members[1]
} else if members[1] == cli.UserID {
otherUserID = members[0]
} else {
return "", fmt.Errorf("neither member in room is the bot")
}
if bot, err := db.GetBot(ctx, otherUserID); err != nil {
return "", fmt.Errorf("failed to check if other member is a bot: %w", err)
} else if bot != nil {
return "", errOtherMemberIsBot
}
memberValidatedRooms[roomID] = otherUserID
return otherUserID, nil
}