aboutsummaryrefslogtreecommitdiff
path: root/ui/command-processor.go
blob: 082d157726c496613b97486ff41d59b1611830ff (plain)
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
168
169
170
171
172
173
174
175
// gomuks - A terminal Matrix client written in Go.
// Copyright (C) 2019 Tulir Asokan
//
// This program is free software: you can redistribute it and/or modify
// it under the terms of the GNU Affero General Public License as published by
// 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
// GNU Affero General Public License for more details.
//
// 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/>.

package ui

import (
	"fmt"
	"strings"

	"maunium.net/go/gomuks/config"
	"maunium.net/go/gomuks/debug"
	"maunium.net/go/gomuks/interface"
)

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
}

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{
			"part":       {"leave"},
			"send":       {"sendevent"},
			"msend":      {"msendevent"},
			"state":      {"setstate"},
			"mstate":     {"msetstate"},
			"rb":         {"rainbow"},
			"rbme":       {"rainbowme"},
			"myroomnick": {"roomnick"},
			"createroom": {"create"},
			"dm":         {"pm"},
			"query":      {"pm"},
			"r":          {"reply"},
			"delete":     {"redact"},
			"remove":     {"redact"},
			"rm":         {"redact"},
			"del":        {"redact"},
			"dl":         {"download"},
			"o":          {"open"},
		},
		commands: map[string]CommandHandler{
			"unknown-command": cmdUnknownCommand,

			"id":         cmdID,
			"help":       cmdHelp,
			"me":         cmdMe,
			"quit":       cmdQuit,
			"clearcache": cmdClearCache,
			"leave":      cmdLeave,
			"create":     cmdCreateRoom,
			"pm":         cmdPrivateMessage,
			"join":       cmdJoin,
			"kick":       cmdKick,
			"ban":        cmdBan,
			"unban":      cmdUnban,
			"toggle":     cmdToggle,
			"logout":     cmdLogout,
			"accept":     cmdAccept,
			"reject":     cmdReject,
			"reply":      cmdReply,
			"redact":     cmdRedact,
			"react":      cmdReact,
			"download":   cmdDownload,
			"open":       cmdOpen,
			"sendevent":  cmdSendEvent,
			"msendevent": cmdMSendEvent,
			"setstate":   cmdSetState,
			"msetstate":  cmdMSetState,
			"roomnick":   cmdRoomNick,
			"rainbow":    cmdRainbow,
			"rainbowme":  cmdRainbowMe,
			"notice":     cmdNotice,
			"tags":       cmdTags,
			"tag":        cmdTag,
			"untag":      cmdUntag,
			"invite":     cmdInvite,
			"hprof":      cmdHeapProfile,
			"cprof":      cmdCPUProfile,
			"trace":      cmdTrace,
		},
	}
}

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)
	return &Command{
		gomuksPointerContainer: ch.gomuksPointerContainer,
		Handler:                ch,

		Room:        roomView,
		Command:     strings.ToLower(split[0]),
		OrigCommand: split[0],
		Args:        split[1:],
		OrigText:    text,
	}
}

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)
}