Skip to content
This repository has been archived by the owner on Jun 28, 2023. It is now read-only.

Add MinimumPowerLevel option to clients #357

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions api/api.go
Original file line number Diff line number Diff line change
Expand Up @@ -82,6 +82,8 @@ type ClientConfig struct {
// When a user starts a new SAS verification with us, their user ID has to match one of these regexes
// for the verification process to start.
AcceptVerificationFromUsers []string
// The minimum required powerlevel to honour an invite request
MinimumPowerLevel int
}

// A IncomingDecimalSAS contains the decimal SAS as displayed on another device. The SAS consists of three numbers.
Expand Down
33 changes: 27 additions & 6 deletions clients/clients.go
Original file line number Diff line number Diff line change
Expand Up @@ -303,7 +303,7 @@ func (c *Clients) onBotOptionsEvent(client *mautrix.Client, event *mevt.Event) {
}
}

func (c *Clients) onRoomMemberEvent(client *mautrix.Client, event *mevt.Event) {
func (c *Clients) onRoomMemberEvent(client *BotClient, event *mevt.Event) {
if event.StateKey == nil || *event.StateKey != client.UserID.String() {
return // not our member event
}
Expand All @@ -316,14 +316,35 @@ func (c *Clients) onRoomMemberEvent(client *mautrix.Client, event *mevt.Event) {
})
logger.Print("Accepting invite from user")

content := struct {
inviteContent := struct {
Inviter id.UserID `json:"inviter"`
}{event.Sender}

if _, err := client.JoinRoom(event.RoomID.String(), "", content); err != nil {
if _, err := client.JoinRoom(event.RoomID.String(), "", inviteContent); err != nil {
logger.WithError(err).Print("Failed to join room")
} else {
logger.Print("Joined room")
return
}
logger.Print("Joined room")
var rID = id.RoomID(event.RoomID.String())
var plContent struct {
Users map[string]int `json:"users"`
UsersDefault int
}
if client.config.MinimumPowerLevel == 0 {
jaywink marked this conversation as resolved.
Show resolved Hide resolved
return
}
if err := client.StateEvent(rID, mevt.StatePowerLevels, "", &plContent); err != nil {
logger.WithError(err).Print("Failed to get powerlevels from room")
client.LeaveRoom(rID)
return
}
var pl = plContent.Users[event.Sender.String()]
if pl == 0 {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Technically this is wrong - the user could have a PL of 0 set explicitly, and afaict in that case the PL should not revert to UsersDefault. It should revert to UsersDefault if the user has not explicitly been set any power level.

However I'm not sure in this context does plContent.Users have all users or just those who have a PL set? If the latter, wont this whole thing crash since there will be nothing in the map for that user?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I did some learning of go, and doing aMap["foo"] will return two values. The first is the value, and the second is whether the value existed in the map.

So in this case, I can do var pl, userExists and if userExists is false, we fall back to the default PL.

pl = plContent.UsersDefault
}
if pl < client.config.MinimumPowerLevel {
logger.Warningln("User tried to invite the bot without the required PLs, ignoring")
client.LeaveRoom(rID)
}
}
}
Expand Down Expand Up @@ -372,7 +393,7 @@ func (c *Clients) initClient(botClient *BotClient) error {

if config.AutoJoinRooms {
syncer.OnEventType(mevt.StateMember, func(_ mautrix.EventSource, event *mevt.Event) {
c.onRoomMemberEvent(client, event)
c.onRoomMemberEvent(botClient, event)
jaywink marked this conversation as resolved.
Show resolved Hide resolved
})
}

Expand Down
2 changes: 2 additions & 0 deletions config.sample.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ clients:
AutoJoinRooms: true
DisplayName: "Go-NEB!"
AcceptVerificationFromUsers: [":localhost:8008"]
MinimumPowerLevel: 50

- UserID: "@another_goneb:localhost"
AccessToken: "MDASDASJDIASDJASDAFGFRGER"
Expand All @@ -36,6 +37,7 @@ clients:
AutoJoinRooms: false
DisplayName: "Go-NEB!"
AcceptVerificationFromUsers: ["^@admin:localhost:8008$"]
MinimumPowerLevel: 100

# The list of realms which Go-NEB is aware of.
# Delete or modify this list as appropriate.
Expand Down