-
Notifications
You must be signed in to change notification settings - Fork 1
/
cmdreset.go
65 lines (58 loc) · 1.71 KB
/
cmdreset.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
package main
import (
"context"
"fmt"
"strings"
"maunium.net/go/mautrix/id"
"maunium.net/go/mautrix/synapseadmin"
"maunium.net/go/mautrix/util"
)
const resetConfirm = `Are you sure you want to reset the access token of ´%s´?
This will invalidate the existing token and e2ee device keys.
To keep encryption working on the bot, make sure to clear the bot database too.
Type ´really reset´ to confirm deletion.`
func cmdReset(ctx context.Context, args []string) {
if len(args) < 1 {
reply(ctx, "**Usage:** `reset <username>`")
return
}
bot := getBotMeta(ctx, args[0])
if bot == nil {
return
}
cmdCtx := getUserCommandContext(ctx)
cmdCtx.Next = cmdReallyReset
cmdCtx.Data["reset_bot_mxid"] = bot.MXID
cmdCtx.Action = fmt.Sprintf("resetting `%s`", bot.MXID)
reply(ctx, resetConfirm, bot.MXID)
}
func cmdReallyReset(ctx context.Context, _ []string) {
cmdCtx := getUserCommandContext(ctx)
userID := cmdCtx.Data["reset_bot_mxid"].(id.UserID)
cmdCtx.Clear()
if strings.TrimSpace(getEvent(ctx).Content.AsMessage().Body) != "really reset" {
reply(ctx, "Cancelled resetting `%s`", userID)
return
}
bot := getBotMeta(ctx, userID.Localpart())
if bot == nil {
return
}
password := util.RandomString(72)
err := synadm.ResetPassword(ctx, synapseadmin.ReqResetPassword{
UserID: bot.MXID,
NewPassword: password,
LogoutDevices: true,
})
if err != nil {
replyErr(ctx, err, "Failed to reset bot")
return
}
resp, err := Login(ctx, bot.MXID, password)
if err != nil {
replyErr(ctx, err, "Failed to create device after resetting bot")
return
}
evtID := reply(ctx, "Bot reset successfully."+botDetails, resp.UserID, resp.DeviceID, resp.AccessToken)
selfDestruct(ctx, evtID, botDetailsSelfDestruct)
}