gomuks/ui/command-processor.go

151 lines
3.7 KiB
Go
Raw Normal View History

2018-05-27 13:54:07 +02:00
// gomuks - A terminal Matrix client written in Go.
2019-01-17 13:13:25 +01:00
// Copyright (C) 2019 Tulir Asokan
2018-05-27 13:54:07 +02:00
//
// This program is free software: you can redistribute it and/or modify
2019-01-17 13:13:25 +01:00
// it under the terms of the GNU Affero General Public License as published by
2018-05-27 13:54:07 +02:00
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// This program is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
2019-01-17 13:13:25 +01:00
// GNU Affero General Public License for more details.
2018-05-27 13:54:07 +02:00
//
2019-01-17 13:13:25 +01:00
// You should have received a copy of the GNU Affero General Public License
// along with this program. If not, see <https://www.gnu.org/licenses/>.
2018-05-27 13:54:07 +02:00
package ui
import (
2018-06-01 23:44:21 +02:00
"fmt"
2019-01-17 13:13:25 +01:00
"strings"
2018-05-27 13:54:07 +02:00
"maunium.net/go/gomuks/config"
"maunium.net/go/gomuks/debug"
2018-06-01 23:44:21 +02:00
"maunium.net/go/gomuks/interface"
2018-05-27 13:54:07 +02:00
)
type gomuksPointerContainer struct {
MainView *MainView
UI *GomuksUI
Matrix ifc.MatrixContainer
Config *config.Config
Gomuks ifc.Gomuks
}
type Command struct {
gomuksPointerContainer
Handler *CommandProcessor
Room *RoomView
Command string
OrigCommand string
Args []string
OrigText string
2018-05-27 13:54:07 +02:00
}
func (cmd *Command) Reply(message string, args ...interface{}) {
cmd.Room.AddServiceMessage(fmt.Sprintf(message, args...))
}
type Alias struct {
NewCommand string
}
func (alias *Alias) Process(cmd *Command) *Command {
cmd.Command = alias.NewCommand
return cmd
}
type CommandHandler func(cmd *Command)
type CommandProcessor struct {
gomuksPointerContainer
aliases map[string]*Alias
commands map[string]CommandHandler
}
func NewCommandProcessor(parent *MainView) *CommandProcessor {
return &CommandProcessor{
gomuksPointerContainer: gomuksPointerContainer{
MainView: parent,
UI: parent.parent,
Matrix: parent.matrix,
Config: parent.config,
Gomuks: parent.gmx,
},
aliases: map[string]*Alias{
2019-06-14 20:01:24 +02:00
"part": {"leave"},
"send": {"sendevent"},
"msend": {"msendevent"},
"state": {"setstate"},
"mstate": {"msetstate"},
"rb": {"rainbow"},
"createroom": {"create"},
"dm": {"pm"},
"query": {"pm"},
2018-05-27 13:54:07 +02:00
},
commands: map[string]CommandHandler{
"unknown-command": cmdUnknownCommand,
"help": cmdHelp,
"me": cmdMe,
"quit": cmdQuit,
"clearcache": cmdClearCache,
"leave": cmdLeave,
"create": cmdCreateRoom,
2019-06-14 20:01:24 +02:00
"pm": cmdPrivateMessage,
2018-05-27 13:54:07 +02:00
"join": cmdJoin,
"kick": cmdKick,
"ban": cmdBan,
"unban": cmdUnban,
"toggle": cmdToggle,
2018-05-27 13:54:07 +02:00
"logout": cmdLogout,
"sendevent": cmdSendEvent,
2019-03-19 15:57:50 +01:00
"msendevent": cmdMSendEvent,
"setstate": cmdSetState,
2019-03-19 15:58:57 +01:00
"msetstate": cmdMSetState,
"rainbow": cmdRainbow,
"invite": cmdInvite,
2019-04-07 17:21:38 +02:00
"hprof": cmdHeapProfile,
2019-06-15 16:04:08 +02:00
"cprof": cmdCPUProfile,
2018-05-27 13:54:07 +02:00
},
}
}
func (ch *CommandProcessor) ParseCommand(roomView *RoomView, text string) *Command {
if text[0] != '/' || len(text) < 2 {
return nil
}
text = text[1:]
split := strings.SplitN(text, " ", -1)
2018-05-27 13:54:07 +02:00
return &Command{
gomuksPointerContainer: ch.gomuksPointerContainer,
Handler: ch,
Room: roomView,
Command: strings.ToLower(split[0]),
OrigCommand: split[0],
Args: split[1:],
OrigText: text,
2018-05-27 13:54:07 +02:00
}
}
func (ch *CommandProcessor) HandleCommand(cmd *Command) {
defer debug.Recover()
if cmd == nil {
return
}
if alias, ok := ch.aliases[cmd.Command]; ok {
cmd = alias.Process(cmd)
}
if cmd == nil {
return
}
if handler, ok := ch.commands[cmd.Command]; ok {
handler(cmd)
return
}
cmdUnknownCommand(cmd)
}