2018-05-27 13:54:07 +02:00
|
|
|
// gomuks - A terminal Matrix client written in Go.
|
2020-04-19 17:10:14 +02:00
|
|
|
// Copyright (C) 2020 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"
|
|
|
|
|
2020-07-24 20:47:22 +02:00
|
|
|
"github.com/mattn/go-runewidth"
|
|
|
|
|
2018-05-27 13:54:07 +02:00
|
|
|
"maunium.net/go/gomuks/config"
|
|
|
|
"maunium.net/go/gomuks/debug"
|
2022-04-15 11:53:09 +02:00
|
|
|
ifc "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
|
|
|
|
|
2018-05-27 14:30:52 +02:00
|
|
|
Room *RoomView
|
|
|
|
Command string
|
|
|
|
OrigCommand string
|
|
|
|
Args []string
|
2020-06-17 13:15:22 +02:00
|
|
|
RawArgs string
|
2018-05-27 14:30:52 +02:00
|
|
|
OrigText string
|
2018-05-27 13:54:07 +02:00
|
|
|
}
|
|
|
|
|
2020-07-24 20:47:22 +02:00
|
|
|
type CommandAutocomplete Command
|
|
|
|
|
2018-05-27 13:54:07 +02:00
|
|
|
func (cmd *Command) Reply(message string, args ...interface{}) {
|
2023-04-04 20:43:23 +02:00
|
|
|
if len(args) > 0 {
|
|
|
|
message = fmt.Sprintf(message, args...)
|
|
|
|
}
|
|
|
|
cmd.Room.AddServiceMessage(message)
|
2020-06-17 13:15:22 +02:00
|
|
|
cmd.UI.Render()
|
2018-05-27 13:54:07 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
type Alias struct {
|
|
|
|
NewCommand string
|
|
|
|
}
|
|
|
|
|
|
|
|
func (alias *Alias) Process(cmd *Command) *Command {
|
|
|
|
cmd.Command = alias.NewCommand
|
|
|
|
return cmd
|
|
|
|
}
|
|
|
|
|
|
|
|
type CommandHandler func(cmd *Command)
|
2020-07-24 20:47:22 +02:00
|
|
|
type CommandAutocompleter func(cmd *CommandAutocomplete) (completions []string, newText string)
|
2018-05-27 13:54:07 +02:00
|
|
|
|
|
|
|
type CommandProcessor struct {
|
|
|
|
gomuksPointerContainer
|
|
|
|
|
|
|
|
aliases map[string]*Alias
|
|
|
|
commands map[string]CommandHandler
|
2020-07-24 20:47:22 +02:00
|
|
|
|
|
|
|
autocompleters map[string]CommandAutocompleter
|
2018-05-27 13:54:07 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
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"},
|
2019-06-16 15:32:56 +02:00
|
|
|
"rbme": {"rainbowme"},
|
2022-02-16 18:38:10 +01:00
|
|
|
"rbn": {"rainbownotice"},
|
2019-06-16 15:32:56 +02:00
|
|
|
"myroomnick": {"roomnick"},
|
2019-06-14 20:01:24 +02:00
|
|
|
"createroom": {"create"},
|
|
|
|
"dm": {"pm"},
|
|
|
|
"query": {"pm"},
|
2020-02-29 23:33:37 +01:00
|
|
|
"r": {"reply"},
|
|
|
|
"delete": {"redact"},
|
|
|
|
"remove": {"redact"},
|
|
|
|
"rm": {"redact"},
|
|
|
|
"del": {"redact"},
|
2020-08-31 00:23:14 +02:00
|
|
|
"e": {"edit"},
|
2020-04-08 14:30:29 +02:00
|
|
|
"dl": {"download"},
|
|
|
|
"o": {"open"},
|
2020-09-12 00:59:53 +02:00
|
|
|
"4s": {"ssss"},
|
|
|
|
"s4": {"ssss"},
|
|
|
|
"cs": {"cross-signing"},
|
2023-04-04 21:25:36 +02:00
|
|
|
"power": {"powerlevel"},
|
|
|
|
"pl": {"powerlevel"},
|
2018-05-27 13:54:07 +02:00
|
|
|
},
|
2020-07-24 20:47:22 +02:00
|
|
|
autocompleters: map[string]CommandAutocompleter{
|
2020-09-12 23:13:08 +02:00
|
|
|
"devices": autocompleteUser,
|
|
|
|
"device": autocompleteDevice,
|
|
|
|
"verify": autocompleteUser,
|
|
|
|
"verify-device": autocompleteDevice,
|
|
|
|
"unverify": autocompleteDevice,
|
|
|
|
"blacklist": autocompleteDevice,
|
|
|
|
"upload": autocompleteFile,
|
|
|
|
"download": autocompleteFile,
|
|
|
|
"open": autocompleteFile,
|
|
|
|
"import": autocompleteFile,
|
|
|
|
"export": autocompleteFile,
|
|
|
|
"export-room": autocompleteFile,
|
2022-04-11 00:05:37 +02:00
|
|
|
"toggle": autocompleteToggle,
|
2023-04-04 21:25:36 +02:00
|
|
|
"powerlevel": autocompletePowerLevel,
|
2020-07-24 20:47:22 +02:00
|
|
|
},
|
2018-05-27 13:54:07 +02:00
|
|
|
commands: map[string]CommandHandler{
|
|
|
|
"unknown-command": cmdUnknownCommand,
|
2020-02-22 00:17:52 +01:00
|
|
|
|
|
|
|
"id": cmdID,
|
|
|
|
"help": cmdHelp,
|
|
|
|
"me": cmdMe,
|
|
|
|
"quit": cmdQuit,
|
|
|
|
"clearcache": cmdClearCache,
|
|
|
|
"leave": cmdLeave,
|
|
|
|
"create": cmdCreateRoom,
|
|
|
|
"pm": cmdPrivateMessage,
|
|
|
|
"join": cmdJoin,
|
|
|
|
"kick": cmdKick,
|
|
|
|
"ban": cmdBan,
|
|
|
|
"unban": cmdUnban,
|
2023-04-04 21:25:36 +02:00
|
|
|
"powerlevel": cmdPowerLevel,
|
2020-02-22 00:17:52 +01:00
|
|
|
"toggle": cmdToggle,
|
|
|
|
"logout": cmdLogout,
|
|
|
|
"accept": cmdAccept,
|
|
|
|
"reject": cmdReject,
|
2020-02-29 23:33:37 +01:00
|
|
|
"reply": cmdReply,
|
|
|
|
"redact": cmdRedact,
|
|
|
|
"react": cmdReact,
|
2020-08-31 00:23:14 +02:00
|
|
|
"edit": cmdEdit,
|
2022-03-06 21:13:55 +01:00
|
|
|
"external": cmdExternalEditor,
|
2020-04-08 14:30:29 +02:00
|
|
|
"download": cmdDownload,
|
2020-09-02 01:16:46 +02:00
|
|
|
"upload": cmdUpload,
|
2020-04-08 14:30:29 +02:00
|
|
|
"open": cmdOpen,
|
2020-04-26 19:02:35 +02:00
|
|
|
"copy": cmdCopy,
|
2020-02-22 00:17:52 +01:00
|
|
|
"sendevent": cmdSendEvent,
|
|
|
|
"msendevent": cmdMSendEvent,
|
|
|
|
"setstate": cmdSetState,
|
|
|
|
"msetstate": cmdMSetState,
|
|
|
|
"roomnick": cmdRoomNick,
|
|
|
|
"rainbow": cmdRainbow,
|
|
|
|
"rainbowme": cmdRainbowMe,
|
|
|
|
"notice": cmdNotice,
|
2020-06-17 13:15:22 +02:00
|
|
|
"alias": cmdAlias,
|
2020-02-22 00:17:52 +01:00
|
|
|
"tags": cmdTags,
|
|
|
|
"tag": cmdTag,
|
|
|
|
"untag": cmdUntag,
|
|
|
|
"invite": cmdInvite,
|
|
|
|
"hprof": cmdHeapProfile,
|
|
|
|
"cprof": cmdCPUProfile,
|
|
|
|
"trace": cmdTrace,
|
2022-04-17 22:54:52 +02:00
|
|
|
"panic": func(cmd *Command) {
|
|
|
|
panic("hello world")
|
|
|
|
},
|
2020-05-07 10:56:21 +02:00
|
|
|
|
2022-02-16 18:38:10 +01:00
|
|
|
"rainbownotice": cmdRainbowNotice,
|
|
|
|
|
2020-07-25 19:54:32 +02:00
|
|
|
"fingerprint": cmdFingerprint,
|
|
|
|
"devices": cmdDevices,
|
2020-09-12 23:13:08 +02:00
|
|
|
"verify-device": cmdVerifyDevice,
|
2020-07-25 19:54:32 +02:00
|
|
|
"verify": cmdVerify,
|
|
|
|
"device": cmdDevice,
|
|
|
|
"unverify": cmdUnverify,
|
|
|
|
"blacklist": cmdBlacklist,
|
|
|
|
"reset-session": cmdResetSession,
|
2020-08-25 20:09:44 +02:00
|
|
|
"import": cmdImportKeys,
|
|
|
|
"export": cmdExportKeys,
|
|
|
|
"export-room": cmdExportRoomKeys,
|
2020-09-12 00:59:53 +02:00
|
|
|
"ssss": cmdSSSS,
|
|
|
|
"cross-signing": cmdCrossSigning,
|
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:]
|
2020-06-17 13:15:22 +02:00
|
|
|
split := strings.Fields(text)
|
|
|
|
command := split[0]
|
|
|
|
args := split[1:]
|
|
|
|
var rawArgs string
|
|
|
|
if len(text) > len(command)+1 {
|
|
|
|
rawArgs = text[len(command)+1:]
|
|
|
|
}
|
2018-05-27 13:54:07 +02:00
|
|
|
return &Command{
|
|
|
|
gomuksPointerContainer: ch.gomuksPointerContainer,
|
|
|
|
Handler: ch,
|
|
|
|
|
2018-05-27 14:30:52 +02:00
|
|
|
Room: roomView,
|
2020-06-17 13:15:22 +02:00
|
|
|
Command: strings.ToLower(command),
|
|
|
|
OrigCommand: command,
|
|
|
|
Args: args,
|
|
|
|
RawArgs: rawArgs,
|
2018-05-27 14:30:52 +02:00
|
|
|
OrigText: text,
|
2018-05-27 13:54:07 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-07-24 20:47:22 +02:00
|
|
|
func (ch *CommandProcessor) Autocomplete(roomView *RoomView, text string, cursorOffset int) ([]string, string, bool) {
|
|
|
|
var completions []string
|
2020-09-02 12:01:55 +02:00
|
|
|
if cursorOffset != runewidth.StringWidth(text) {
|
2020-07-24 20:47:22 +02:00
|
|
|
return completions, text, false
|
2020-09-02 12:01:55 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
var cmd *Command
|
|
|
|
if cmd = ch.ParseCommand(roomView, text); cmd == nil {
|
2020-07-24 20:47:22 +02:00
|
|
|
return completions, text, false
|
2020-09-02 12:01:55 +02:00
|
|
|
} else if alias, ok := ch.aliases[cmd.Command]; ok {
|
|
|
|
cmd = alias.Process(cmd)
|
|
|
|
}
|
|
|
|
|
|
|
|
handler, ok := ch.autocompleters[cmd.Command]
|
|
|
|
if ok {
|
|
|
|
var newText string
|
|
|
|
completions, newText = handler((*CommandAutocomplete)(cmd))
|
|
|
|
if newText != "" {
|
|
|
|
text = newText
|
2020-07-24 20:47:22 +02:00
|
|
|
}
|
|
|
|
}
|
2020-09-08 01:06:09 +02:00
|
|
|
return completions, text, ok
|
2020-07-24 20:47:22 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
func (ch *CommandProcessor) AutocompleteCommand(word string) (completions []string) {
|
|
|
|
if word[0] != '/' {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
word = word[1:]
|
|
|
|
for alias := range ch.aliases {
|
|
|
|
if alias == word {
|
|
|
|
return []string{"/" + alias}
|
|
|
|
}
|
|
|
|
if strings.HasPrefix(alias, word) {
|
|
|
|
completions = append(completions, "/"+alias)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
for command := range ch.commands {
|
|
|
|
if command == word {
|
|
|
|
return []string{"/" + command}
|
|
|
|
}
|
|
|
|
if strings.HasPrefix(command, word) {
|
|
|
|
completions = append(completions, "/"+command)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
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)
|
|
|
|
}
|