| 1 | // gomuks - A terminal Matrix client written in Go. |
| 2 | // Copyright (C) 2020 Tulir Asokan |
| 3 | // |
| 4 | // This program is free software: you can redistribute it and/or modify |
| 5 | // it under the terms of the GNU Affero General Public License as published by |
| 6 | // the Free Software Foundation, either version 3 of the License, or |
| 7 | // (at your option) any later version. |
| 8 | // |
| 9 | // This program is distributed in the hope that it will be useful, |
| 10 | // but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 11 | // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 12 | // GNU Affero General Public License for more details. |
| 13 | // |
| 14 | // You should have received a copy of the GNU Affero General Public License |
| 15 | // along with this program. If not, see <https://www.gnu.org/licenses/>. |
| 16 | |
| 17 | package ui |
| 18 | |
| 19 | import ( |
| 20 | "fmt" |
| 21 | "strings" |
| 22 | |
| 23 | "github.com/mattn/go-runewidth" |
| 24 | |
| 25 | "maunium.net/go/gomuks/config" |
| 26 | "maunium.net/go/gomuks/debug" |
| 27 | ifc "maunium.net/go/gomuks/interface" |
| 28 | ) |
| 29 | |
| 30 | type gomuksPointerContainer struct { |
| 31 | MainView *MainView |
| 32 | UI *GomuksUI |
| 33 | Matrix ifc.MatrixContainer |
| 34 | Config *config.Config |
| 35 | Gomuks ifc.Gomuks |
| 36 | } |
| 37 | |
| 38 | type Command struct { |
| 39 | gomuksPointerContainer |
| 40 | Handler *CommandProcessor |
| 41 | |
| 42 | Room *RoomView |
| 43 | Command string |
| 44 | OrigCommand string |
| 45 | Args []string |
| 46 | RawArgs string |
| 47 | OrigText string |
| 48 | } |
| 49 | |
| 50 | type CommandAutocomplete Command |
| 51 | |
| 52 | func (cmd *Command) Reply(message string, args ...interface{}) { |
| 53 | if len(args) > 0 { |
| 54 | message = fmt.Sprintf(message, args...) |
| 55 | } |
| 56 | cmd.Room.AddServiceMessage(message) |
| 57 | cmd.UI.Render() |
| 58 | } |
| 59 | |
| 60 | type Alias struct { |
| 61 | NewCommand string |
| 62 | } |
| 63 | |
| 64 | func (alias *Alias) Process(cmd *Command) *Command { |
| 65 | cmd.Command = alias.NewCommand |
| 66 | return cmd |
| 67 | } |
| 68 | |
| 69 | type CommandHandler func(cmd *Command) |
| 70 | type CommandAutocompleter func(cmd *CommandAutocomplete) (completions []string, newText string) |
| 71 | |
| 72 | type CommandProcessor struct { |
| 73 | gomuksPointerContainer |
| 74 | |
| 75 | aliases map[string]*Alias |
| 76 | commands map[string]CommandHandler |
| 77 | |
| 78 | autocompleters map[string]CommandAutocompleter |
| 79 | } |
| 80 | |
| 81 | func NewCommandProcessor(parent *MainView) *CommandProcessor { |
| 82 | return &CommandProcessor{ |
| 83 | gomuksPointerContainer: gomuksPointerContainer{ |
| 84 | MainView: parent, |
| 85 | UI: parent.parent, |
| 86 | Matrix: parent.matrix, |
| 87 | Config: parent.config, |
| 88 | Gomuks: parent.gmx, |
| 89 | }, |
| 90 | aliases: map[string]*Alias{ |
| 91 | "part": {"leave"}, |
| 92 | "send": {"sendevent"}, |
| 93 | "msend": {"msendevent"}, |
| 94 | "state": {"setstate"}, |
| 95 | "mstate": {"msetstate"}, |
| 96 | "rb": {"rainbow"}, |
| 97 | "rbme": {"rainbowme"}, |
| 98 | "rbn": {"rainbownotice"}, |
| 99 | "myroomnick": {"roomnick"}, |
| 100 | "createroom": {"create"}, |
| 101 | "dm": {"pm"}, |
| 102 | "query": {"pm"}, |
| 103 | "r": {"reply"}, |
| 104 | "delete": {"redact"}, |
| 105 | "remove": {"redact"}, |
| 106 | "rm": {"redact"}, |
| 107 | "del": {"redact"}, |
| 108 | "e": {"edit"}, |
| 109 | "dl": {"download"}, |
| 110 | "o": {"open"}, |
| 111 | "4s": {"ssss"}, |
| 112 | "s4": {"ssss"}, |
| 113 | "cs": {"cross-signing"}, |
| 114 | "power": {"powerlevel"}, |
| 115 | "pl": {"powerlevel"}, |
| 116 | }, |
| 117 | autocompleters: map[string]CommandAutocompleter{ |
| 118 | "devices": autocompleteUser, |
| 119 | "device": autocompleteDevice, |
| 120 | "verify": autocompleteUser, |
| 121 | "verify-device": autocompleteDevice, |
| 122 | "unverify": autocompleteDevice, |
| 123 | "blacklist": autocompleteDevice, |
| 124 | "upload": autocompleteFile, |
| 125 | "download": autocompleteFile, |
| 126 | "open": autocompleteFile, |
| 127 | "import": autocompleteFile, |
| 128 | "export": autocompleteFile, |
| 129 | "export-room": autocompleteFile, |
| 130 | "toggle": autocompleteToggle, |
| 131 | "powerlevel": autocompletePowerLevel, |
| 132 | }, |
| 133 | commands: map[string]CommandHandler{ |
| 134 | "unknown-command": cmdUnknownCommand, |
| 135 | |
| 136 | "id": cmdID, |
| 137 | "help": cmdHelp, |
| 138 | "me": cmdMe, |
| 139 | "quit": cmdQuit, |
| 140 | "clearcache": cmdClearCache, |
| 141 | "leave": cmdLeave, |
| 142 | "create": cmdCreateRoom, |
| 143 | "pm": cmdPrivateMessage, |
| 144 | "join": cmdJoin, |
| 145 | "kick": cmdKick, |
| 146 | "ban": cmdBan, |
| 147 | "unban": cmdUnban, |
| 148 | "powerlevel": cmdPowerLevel, |
| 149 | "toggle": cmdToggle, |
| 150 | "logout": cmdLogout, |
| 151 | "accept": cmdAccept, |
| 152 | "reject": cmdReject, |
| 153 | "reply": cmdReply, |
| 154 | "redact": cmdRedact, |
| 155 | "react": cmdReact, |
| 156 | "edit": cmdEdit, |
| 157 | "external": cmdExternalEditor, |
| 158 | "download": cmdDownload, |
| 159 | "upload": cmdUpload, |
| 160 | "open": cmdOpen, |
| 161 | "copy": cmdCopy, |
| 162 | "sendevent": cmdSendEvent, |
| 163 | "msendevent": cmdMSendEvent, |
| 164 | "setstate": cmdSetState, |
| 165 | "msetstate": cmdMSetState, |
| 166 | "roomnick": cmdRoomNick, |
| 167 | "rainbow": cmdRainbow, |
| 168 | "rainbowme": cmdRainbowMe, |
| 169 | "notice": cmdNotice, |
| 170 | "alias": cmdAlias, |
| 171 | "tags": cmdTags, |
| 172 | "tag": cmdTag, |
| 173 | "untag": cmdUntag, |
| 174 | "invite": cmdInvite, |
| 175 | "hprof": cmdHeapProfile, |
| 176 | "cprof": cmdCPUProfile, |
| 177 | "trace": cmdTrace, |
| 178 | "panic": func(cmd *Command) { |
| 179 | panic("hello world") |
| 180 | }, |
| 181 | |
| 182 | "rainbownotice": cmdRainbowNotice, |
| 183 | |
| 184 | "fingerprint": cmdFingerprint, |
| 185 | "devices": cmdDevices, |
| 186 | "verify-device": cmdVerifyDevice, |
| 187 | "verify": cmdVerify, |
| 188 | "device": cmdDevice, |
| 189 | "unverify": cmdUnverify, |
| 190 | "blacklist": cmdBlacklist, |
| 191 | "reset-session": cmdResetSession, |
| 192 | "import": cmdImportKeys, |
| 193 | "export": cmdExportKeys, |
| 194 | "export-room": cmdExportRoomKeys, |
| 195 | "ssss": cmdSSSS, |
| 196 | "cross-signing": cmdCrossSigning, |
| 197 | }, |
| 198 | } |
| 199 | } |
| 200 | |
| 201 | func (ch *CommandProcessor) ParseCommand(roomView *RoomView, text string) *Command { |
| 202 | if text[0] != '/' || len(text) < 2 { |
| 203 | return nil |
| 204 | } |
| 205 | text = text[1:] |
| 206 | split := strings.Fields(text) |
| 207 | command := split[0] |
| 208 | args := split[1:] |
| 209 | var rawArgs string |
| 210 | if len(text) > len(command)+1 { |
| 211 | rawArgs = text[len(command)+1:] |
| 212 | } |
| 213 | return &Command{ |
| 214 | gomuksPointerContainer: ch.gomuksPointerContainer, |
| 215 | Handler: ch, |
| 216 | |
| 217 | Room: roomView, |
| 218 | Command: strings.ToLower(command), |
| 219 | OrigCommand: command, |
| 220 | Args: args, |
| 221 | RawArgs: rawArgs, |
| 222 | OrigText: text, |
| 223 | } |
| 224 | } |
| 225 | |
| 226 | func (ch *CommandProcessor) Autocomplete(roomView *RoomView, text string, cursorOffset int) ([]string, string, bool) { |
| 227 | var completions []string |
| 228 | if cursorOffset != runewidth.StringWidth(text) { |
| 229 | return completions, text, false |
| 230 | } |
| 231 | |
| 232 | var cmd *Command |
| 233 | if cmd = ch.ParseCommand(roomView, text); cmd == nil { |
| 234 | return completions, text, false |
| 235 | } else if alias, ok := ch.aliases[cmd.Command]; ok { |
| 236 | cmd = alias.Process(cmd) |
| 237 | } |
| 238 | |
| 239 | handler, ok := ch.autocompleters[cmd.Command] |
| 240 | if ok { |
| 241 | var newText string |
| 242 | completions, newText = handler((*CommandAutocomplete)(cmd)) |
| 243 | if newText != "" { |
| 244 | text = newText |
| 245 | } |
| 246 | } |
| 247 | return completions, text, ok |
| 248 | } |
| 249 | |
| 250 | func (ch *CommandProcessor) AutocompleteCommand(word string) (completions []string) { |
| 251 | if word[0] != '/' { |
| 252 | return |
| 253 | } |
| 254 | word = word[1:] |
| 255 | for alias := range ch.aliases { |
| 256 | if alias == word { |
| 257 | return []string{"/" + alias} |
| 258 | } |
| 259 | if strings.HasPrefix(alias, word) { |
| 260 | completions = append(completions, "/"+alias) |
| 261 | } |
| 262 | } |
| 263 | for command := range ch.commands { |
| 264 | if command == word { |
| 265 | return []string{"/" + command} |
| 266 | } |
| 267 | if strings.HasPrefix(command, word) { |
| 268 | completions = append(completions, "/"+command) |
| 269 | } |
| 270 | } |
| 271 | return |
| 272 | } |
| 273 | |
| 274 | func (ch *CommandProcessor) HandleCommand(cmd *Command) { |
| 275 | defer debug.Recover() |
| 276 | if cmd == nil { |
| 277 | return |
| 278 | } |
| 279 | if alias, ok := ch.aliases[cmd.Command]; ok { |
| 280 | cmd = alias.Process(cmd) |
| 281 | } |
| 282 | if cmd == nil { |
| 283 | return |
| 284 | } |
| 285 | if handler, ok := ch.commands[cmd.Command]; ok { |
| 286 | handler(cmd) |
| 287 | return |
| 288 | } |
| 289 | cmdUnknownCommand(cmd) |
| 290 | } |