-
Notifications
You must be signed in to change notification settings - Fork 1
/
commands.go
167 lines (144 loc) · 3.86 KB
/
commands.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
package main
import (
"context"
"runtime/debug"
"strings"
"sync"
"github.com/rs/zerolog"
"golang.org/x/exp/maps"
"maunium.net/go/mautrix/event"
"maunium.net/go/mautrix/id"
)
const helpMessage = `Botbot %s
Commands:
* ´ping´: Pings the bot
* ´help´: Shows this message
* ´list´: Show a list of your bots
* ´show <username>´: Show info about a specific bot
* ´create <username>´: Register a new bot
* ´reset <username>´: Reset the access token of a bot
`
type CommandHandler func(ctx context.Context, args []string)
var commands = map[string]CommandHandler{
"ping": cmdPing,
"help": cmdHelp,
"list": cmdList,
"show": cmdShow,
"create": cmdCreate,
"reset": cmdReset,
"delete": cmdDelete,
"cancel": cmdCancel,
// Aliases
"register": cmdCreate,
"info": cmdShow,
"get": cmdShow,
"remove": cmdDelete,
"unregister": cmdDelete,
}
type CommandContext struct {
sync.Mutex
Next CommandHandler
Action string
Data map[string]any
}
func (cmdCtx *CommandContext) Clear() {
cmdCtx.Next = nil
cmdCtx.Action = ""
maps.Clear(cmdCtx.Data)
}
var commandContext = make(map[id.UserID]*CommandContext)
var commandContextLock sync.Mutex
func getCommandContextFromMap(userID id.UserID) *CommandContext {
commandContextLock.Lock()
ctx, ok := commandContext[userID]
if !ok {
ctx = &CommandContext{
Data: make(map[string]any),
}
commandContext[userID] = ctx
}
commandContextLock.Unlock()
return ctx
}
func getUserCommandContext(ctx context.Context) *CommandContext {
return ctx.Value(contextKeyCmdContext).(*CommandContext)
}
func backgroundMarkRead(ctx context.Context, evt *event.Event) {
go func() {
err := cli.MarkRead(evt.RoomID, evt.ID)
if err != nil {
zerolog.Ctx(ctx).Warn().Err(err).Msg("Failed to mark command as read")
}
}()
}
func handleCommand(ctx context.Context, evt *event.Event) {
log := *zerolog.Ctx(ctx)
defer func() {
if r := recover(); r != nil {
logEvt := log.Error()
if err, ok := r.(error); ok {
logEvt = logEvt.Err(err)
} else {
logEvt = logEvt.Interface("error", r)
}
logEvt.Bytes("stack", debug.Stack()).Msg("Panic while processing command")
reply(ctx, "Internal error processing command.")
}
}()
content, ok := evt.Content.Parsed.(*event.MessageEventContent)
if !ok {
log.Debug().Type("content_type", evt.Content).Msg("Ignoring message with unknown parsed content type")
return
} else if content.RelatesTo.GetReplaceID() != "" {
log.Debug().Msg("Ignoring edit event")
return
} else if content.MsgType == event.MsgNotice {
log.Debug().Msg("Ignoring m.notice message")
return
}
content.RemoveReplyFallback()
args := strings.Fields(content.Body)
if len(args) == 0 {
log.Debug().Msg("Ignoring empty message")
return
}
command := strings.TrimPrefix(strings.ToLower(args[0]), "!")
log = log.With().Str("command", command).Logger()
cmdCtx := getCommandContextFromMap(evt.Sender)
ctx = context.WithValue(ctx, contextKeyCmdContext, cmdCtx)
ctx = log.WithContext(ctx)
cmdCtx.Lock()
defer cmdCtx.Unlock()
if cmdCtx.Next != nil && command != "cancel" {
backgroundMarkRead(ctx, evt)
cmdCtx.Next(ctx, args)
} else if content.MsgType != event.MsgText {
log.Debug().Msg("Ignoring non-text non-context command")
} else {
backgroundMarkRead(ctx, evt)
args = args[1:]
cmd, ok := commands[command]
if !ok {
cmd = cmdUnknownCommand
}
cmd(ctx, args)
}
}
func cmdUnknownCommand(ctx context.Context, _ []string) {
reply(ctx, "Unknown command. Use `help` for help.")
}
func cmdPing(ctx context.Context, _ []string) {
reply(ctx, "Pong!")
}
func cmdHelp(ctx context.Context, _ []string) {
reply(ctx, helpMessage, Version)
}
func cmdCancel(ctx context.Context, _ []string) {
cmdCtx := getUserCommandContext(ctx)
if cmdCtx.Action == "" && cmdCtx.Next == nil {
reply(ctx, "Nothing to cancel")
} else {
reply(ctx, "Cancelled %s", cmdCtx.Action)
cmdCtx.Clear()
}
}