-
Notifications
You must be signed in to change notification settings - Fork 0
/
script_parser.go
327 lines (280 loc) · 7.9 KB
/
script_parser.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
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
package main
import (
"log"
"net/http"
"os"
"os/exec"
"errors"
"reflect"
"github.com/1egoman/slick/frontend"
"github.com/1egoman/slick/gateway"
"github.com/atotto/clipboard"
"github.com/cjoudrey/gluahttp" // gopher-lua http library
"github.com/yuin/gopher-lua"
)
type SlickEvent int
const (
EVENT_KEYMAP SlickEvent = iota
EVENT_CONNECTION_CHANGE
EVENT_COMMAND_RUN
EVENT_MESSAGE_SENT
EVENT_MESSAGE_RECEIVED
EVENT_MODE_CHANGE
)
type EventAction struct {
Type SlickEvent
Key []rune
Handler func(*State, *map[string]string) error
}
// Send an event to all the stored listeners.
func EmitEvent(state *State, event SlickEvent, metadata map[string]string) error {
log.Printf("Event emitted: %+v %+v", event, metadata)
for _, i := range state.EventActions {
if i.Type == event {
if err := i.Handler(state, &metadata); err != nil {
return err
}
}
}
return nil
}
func AddSlickStandardLib(L *lua.LState, state *State, term *frontend.TerminalDisplay) {
// Add some logging utilities
L.SetGlobal("print", L.NewFunction(func(L *lua.LState) int {
state.Status.Printf(L.ToString(1))
render(state, term)
return 0
}))
L.SetGlobal("error", L.NewFunction(func(L *lua.LState) int {
state.Status.Errorf(L.ToString(1))
render(state, term)
return 0
}))
L.SetGlobal("clear", L.NewFunction(func(L *lua.LState) int {
state.Status.Clear()
render(state, term)
return 0
}))
// Allow lua to run things when a user presses a key.
L.SetGlobal("keymap", L.NewFunction(func(L *lua.LState) int {
key := L.ToString(1)
function := L.ToFunction(2)
state.EventActions = append(state.EventActions, EventAction{
Type: EVENT_KEYMAP,
Key: []rune(key),
Handler: func(state *State, metadata *map[string]string) error {
return L.CallByParam(lua.P{Fn: function, NRet: 0})
},
})
return 0
}))
L.SetGlobal("command", L.NewFunction(func(L *lua.LState) int {
name := L.ToString(1)
callback := L.ToFunction(4)
COMMANDS = append(COMMANDS, Command{
Name: name,
Type: NATIVE,
Description: L.ToString(2),
Arguments: L.ToString(3),
Permutations: []string{name},
Handler: func(args []string, state *State) error {
log.Println("Running lua command", name, args)
// Convert arguments slice into table
luaArgs := L.NewTable()
for _, arg := range args {
luaArgs.Append(lua.LString(arg))
}
return L.CallByParam(lua.P{Fn: callback, NRet: 0}, luaArgs)
},
})
return 0
}))
L.SetGlobal("getenv", L.NewFunction(func(L *lua.LState) int {
envName := L.ToString(1)
L.Push(lua.LString(os.Getenv(envName)))
return 1
}))
L.SetGlobal("shell", L.NewFunction(func(L *lua.LState) int {
commandName := L.ToString(1)
if len(commandName) == 0 {
L.Push(lua.LString("First argument (command name) is required."))
return 1
}
var args []string
argc := 2
for ; ; argc++ {
arg := L.ToString(argc)
if len(arg) > 0 {
args = append(args, arg)
} else {
break
}
}
log.Println("Running command", commandName, "with args", args)
command := exec.Command(commandName, args...)
output, err := command.Output()
if err != nil {
L.Push(lua.LString(err.Error()))
return 1
}
log.Println("Command output", output)
L.Push(lua.LNil)
L.Push(lua.LString(string(output)))
return 1
}))
L.SetGlobal("sendmessage", L.NewFunction(func(L *lua.LState) int {
messageText := L.ToString(1)
if len(messageText) == 0 {
L.Push(lua.LString("First argument (message text) is required."))
return 1
}
// Just send a normal message!
message := gateway.Message{
Sender: state.ActiveConnection().Self(),
Text: messageText,
}
// Sometimes, a message could have a response. This is for example true in the
// case of slash commands, sometimes.
_, err := state.ActiveConnection().SendMessage(
message,
state.ActiveConnection().SelectedChannel(),
)
if err != nil {
L.Push(lua.LString("Error sending message: " + err.Error()))
} else {
L.Push(lua.LNil)
}
return 1
}))
L.SetGlobal("getclip", L.NewFunction(func(L *lua.LState) int {
text, err := clipboard.ReadAll()
if err != nil {
L.Push(lua.LString(text))
L.Push(lua.LString(err.Error()))
return 2
} else {
L.Push(lua.LString(text))
L.Push(lua.LNil)
return 2
}
}))
L.SetGlobal("setclip", L.NewFunction(func(L *lua.LState) int {
clipboard.WriteAll(L.ToString(1))
return 0
}))
// Allow lua to run things when a user presses a key.
L.SetGlobal("onevent", L.NewFunction(func(L *lua.LState) int {
eventString := L.ToString(1)
function := L.ToFunction(2)
// Convert the string typed by the user into an kj
var event SlickEvent
switch eventString {
/* no EVENT_KEYMAP */
case "connectionchange":
event = EVENT_CONNECTION_CHANGE
case "commandrun":
event = EVENT_COMMAND_RUN
case "messagesent":
event = EVENT_MESSAGE_SENT
case "messagereceived":
event = EVENT_MESSAGE_RECEIVED
case "modechange":
event = EVENT_MODE_CHANGE
}
state.EventActions = append(state.EventActions, EventAction{
Type: event,
Handler: func(state *State, metadata *map[string]string) error {
if metadata == nil {
return errors.New("Metadata passed to event handler for " + eventString + " was nil.")
}
// Convert metadata into a table
metadataTable := L.NewTable()
for key, value := range *metadata {
metadataTable.RawSet(lua.LString(key), lua.LString(value))
}
// Call into lua with with metadata
return L.CallByParam(lua.P{Fn: function, NRet: 0}, metadataTable)
},
})
return 0
}))
// Load Gluahttp so the config can make http requests: https://github.com/cjoudrey/gluahttp
L.PreloadModule("http", gluahttp.NewHttpModule(&http.Client{}).Loader)
// Export all commands in the lua context
for _, command := range COMMANDS {
func(command Command) { // Close over command so it
L.SetGlobal(command.Name, L.NewFunction(func(L *lua.LState) int {
// Collect all arguments into an array
args := []string{"__COMMAND"}
argc := 1
for ; ; argc += 1 {
arg := L.ToString(argc)
if len(arg) > 0 {
args = append(args, arg)
} else {
break
}
}
log.Printf("* Running command %s with args %v", command.Name, args)
command := GetCommand(command.Name)
if command.Handler == nil {
L.Push(lua.LString("No handler defined for the command " + command.Name + "."))
return 1
}
err := command.Handler(args, state)
if err == nil {
L.Push(lua.LNil)
} else {
L.Push(lua.LString(err.Error()))
}
render(state, term)
return 1
}))
}(command)
}
}
// Given a struct, convert it to a lua table.
func StructToTable(L *lua.LState, s interface{}) *lua.LTable {
tbl := L.NewTable()
v := reflect.ValueOf(s)
// For each field, try to add to the lua table
var luaValue lua.LValue
for i := 0; i < v.NumField(); i++ {
// Start value at nil for each iteration
luaValue = lua.LNil
key := v.Type().Field(i).Name
value := v.Field(i).Interface()
typ := v.Field(i).Type().Kind()
// Dereference pointers into their native types.
var deref reflect.Value = v.Field(i)
for typ == reflect.Ptr {
deref = reflect.Indirect(deref)
// If the pointer was dereferenced into `nil`, then we're done dereerencing.
if deref.IsValid() == false {
break
}
typ = deref.Type().Kind()
value = deref.Interface()
}
switch typ {
case reflect.String:
luaValue = lua.LString(value.(string))
case reflect.Int:
luaValue = lua.LNumber(value.(int))
case reflect.Bool:
luaValue = lua.LBool(value.(bool))
case reflect.Struct:
luaValue = StructToTable(L, value)
}
// Add the value to the lua table, if it could be successfully converted. Otherwise, use
// nil.
tbl.RawSetString(key, luaValue)
}
return tbl
}
func ParseScript(script string, state *State, term *frontend.TerminalDisplay) error {
L := lua.NewState()
defer L.Close()
AddSlickStandardLib(L, state, term)
return L.DoString(script)
}