From 209833798bd09ecc2bc37aee501e57e0e09063db Mon Sep 17 00:00:00 2001 From: nate smith Date: Tue, 2 May 2023 20:23:12 -0700 Subject: [PATCH] WIP: a notable unfucking --- server/db/db.go | 3 +++ server/witch/header.go | 50 ++++++++++++++++++++++++------------------ server/witch/witch.go | 27 ++++++++++++++++------- 3 files changed, 51 insertions(+), 29 deletions(-) diff --git a/server/db/db.go b/server/db/db.go index b72c953..b5f9656 100644 --- a/server/db/db.go +++ b/server/db/db.go @@ -176,6 +176,9 @@ func (db *pgDB) Ensure() error { oakDoor = &Object{ Data: data, Script: ` + provides("get tetanus", function(args) + tellSender("you now have tetanus") + end) goes(north, "pub") `, } diff --git a/server/witch/header.go b/server/witch/header.go index c80048c..80b9ffa 100644 --- a/server/witch/header.go +++ b/server/witch/header.go @@ -9,6 +9,7 @@ TODO: consider making this (or witch.go) a different package entirely. the `witc */ import ( + "log" "strings" lua "github.com/yuin/gopher-lua" @@ -23,36 +24,47 @@ func witchProvides(l *lua.LState) int { // TODO test this manually verbAndPattern := l.ToString(1) - l.Pop(1) + cb := l.ToFunction(2) split := strings.SplitN(verbAndPattern, " ", 2) verb := split[0] pattern := split[1] - l.Push(lua.LString(pattern)) - - return addPatternHandler(l, verb) + return addPatternHandler(l, verb, pattern, cb) } func witchHears(l *lua.LState) int { - return addPatternHandler(l, "say") + pattern := l.ToString(1) + cb := l.ToFunction(2) + return addPatternHandler(l, "say", pattern, cb) } func witchSees(l *lua.LState) int { - return addPatternHandler(l, "emote") + pattern := l.ToString(1) + cb := l.ToFunction(2) + + return addPatternHandler(l, "emote", pattern, cb) } func witchGoes(l *lua.LState) int { - // arg 0: direction - // arg 1: from room - // arg 2: to room - // TODO call addPatternHandler with "go" verb and direction pattern; figure out how to call moveSender etc - // TODO call addPatternHandler again for the reverse direction - return -1 + // TODO validate direction + // TODO convert direction constant to english + + direction := l.ToString(1) + targetRoom := l.ToString(2) + + cb := func(l *lua.LState) int { + log.Printf("please move sender to target room '%s'", targetRoom) + return 0 + } + + // TODO call addPatternHandler again for the reverse direction (make a reverse helper) + return addPatternHandler(l, "go", direction, l.NewFunction(cb)) } func witchSeen(l *lua.LState) int { - return addHandler(l, "look") + cb := l.ToFunction(1) + return addHandler(l, "look", cb) } func witchMy(l *lua.LState) int { @@ -69,11 +81,10 @@ func witchDoes(ls *lua.LState) int { return 0 } -func addHandler(l *lua.LState, verb string) int { +func addHandler(l *lua.LState, verb string, cb *lua.LFunction) int { pattern := ".*" - cb := l.ToFunction(1) - //log.Printf("adding handler: %s %s %#v", verb, pattern, cb) + log.Printf("adding handler: %s %s %#v", verb, pattern, cb) handlers := l.GetGlobal("_handlers").(*lua.LTable) @@ -88,11 +99,8 @@ func addHandler(l *lua.LState, verb string) int { return 0 } -func addPatternHandler(l *lua.LState, verb string) int { - pattern := l.ToString(1) - cb := l.ToFunction(2) - - //log.Printf("adding handler: %s %s %#v", verb, string(pattern), cb) +func addPatternHandler(l *lua.LState, verb, pattern string, cb *lua.LFunction) int { + log.Printf("adding handler: %s %s %#v", verb, string(pattern), cb) handlers := l.GetGlobal("_handlers").(*lua.LTable) diff --git a/server/witch/witch.go b/server/witch/witch.go index 847a793..860517b 100644 --- a/server/witch/witch.go +++ b/server/witch/witch.go @@ -16,6 +16,15 @@ import ( lua "github.com/yuin/gopher-lua" ) +const ( + dirEast = "_DIR_EAST" + dirWest = "_DIR_WEST" + dirNorth = "_DIR_NORTH" + dirSouth = "_DIR_SOUTH" + dirAbove = "_DIR_ABOVE" + dirBelow = "_DIR_BELOW" +) + /* allows({ read = "world", @@ -135,14 +144,14 @@ func NewScriptContext(db db.DB, getSend func(string) func(*proto.ClientMessage) l = lua.NewState() // direction constants - l.SetGlobal("east", lua.LString("_DIR_EAST")) - l.SetGlobal("west", lua.LString("_DIR_WEST")) - l.SetGlobal("north", lua.LString("_DIR_NORTH")) - l.SetGlobal("south", lua.LString("_DIR_SOUTH")) - l.SetGlobal("above", lua.LString("_DIR_ABOVE")) - l.SetGlobal("below", lua.LString("_DIR_BELOW")) - l.SetGlobal("up", lua.LString("_DIR_ABOVE")) - l.SetGlobal("down", lua.LString("_DIR_BELOW")) + l.SetGlobal("east", lua.LString(dirEast)) + l.SetGlobal("west", lua.LString(dirWest)) + l.SetGlobal("north", lua.LString(dirNorth)) + l.SetGlobal("south", lua.LString(dirSouth)) + l.SetGlobal("above", lua.LString(dirAbove)) + l.SetGlobal("below", lua.LString(dirBelow)) + l.SetGlobal("up", lua.LString(dirAbove)) + l.SetGlobal("down", lua.LString(dirBelow)) // witch object behavior functions l.SetGlobal("has", l.NewFunction(witchHas)) @@ -227,11 +236,13 @@ func NewScriptContext(db db.DB, getSend func(string) func(*proto.ClientMessage) handlers := l.GetGlobal("_handlers").(*lua.LTable) handlers.ForEach(func(k, v lua.LValue) { + log.Println("checking handler verbs", k) if k.String() != vc.Verb { return } v.(*lua.LTable).ForEach(func(kk, vv lua.LValue) { pattern := regexp.MustCompile(kk.String()) + log.Println("checking handler", kk.String(), vv, pattern) if pattern.MatchString(vc.Rest) { // TODO TODO TODO TODO TODO // this could be a remote code execution vuln; but by being here, I