WIP: a notable unfucking

trunk
nate smith 2023-05-02 20:23:12 -07:00
parent e86ac5875a
commit 209833798b
3 changed files with 51 additions and 29 deletions

View File

@ -176,6 +176,9 @@ func (db *pgDB) Ensure() error {
oakDoor = &Object{ oakDoor = &Object{
Data: data, Data: data,
Script: ` Script: `
provides("get tetanus", function(args)
tellSender("you now have tetanus")
end)
goes(north, "pub") goes(north, "pub")
`, `,
} }

View File

@ -9,6 +9,7 @@ TODO: consider making this (or witch.go) a different package entirely. the `witc
*/ */
import ( import (
"log"
"strings" "strings"
lua "github.com/yuin/gopher-lua" lua "github.com/yuin/gopher-lua"
@ -23,36 +24,47 @@ func witchProvides(l *lua.LState) int {
// TODO test this manually // TODO test this manually
verbAndPattern := l.ToString(1) verbAndPattern := l.ToString(1)
l.Pop(1) cb := l.ToFunction(2)
split := strings.SplitN(verbAndPattern, " ", 2) split := strings.SplitN(verbAndPattern, " ", 2)
verb := split[0] verb := split[0]
pattern := split[1] pattern := split[1]
l.Push(lua.LString(pattern)) return addPatternHandler(l, verb, pattern, cb)
return addPatternHandler(l, verb)
} }
func witchHears(l *lua.LState) int { 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 { 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 { func witchGoes(l *lua.LState) int {
// arg 0: direction // TODO validate direction
// arg 1: from room // TODO convert direction constant to english
// arg 2: to room
// TODO call addPatternHandler with "go" verb and direction pattern; figure out how to call moveSender etc direction := l.ToString(1)
// TODO call addPatternHandler again for the reverse direction targetRoom := l.ToString(2)
return -1
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 { 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 { func witchMy(l *lua.LState) int {
@ -69,11 +81,10 @@ func witchDoes(ls *lua.LState) int {
return 0 return 0
} }
func addHandler(l *lua.LState, verb string) int { func addHandler(l *lua.LState, verb string, cb *lua.LFunction) int {
pattern := ".*" 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) handlers := l.GetGlobal("_handlers").(*lua.LTable)
@ -88,11 +99,8 @@ func addHandler(l *lua.LState, verb string) int {
return 0 return 0
} }
func addPatternHandler(l *lua.LState, verb string) int { func addPatternHandler(l *lua.LState, verb, pattern string, cb *lua.LFunction) int {
pattern := l.ToString(1) log.Printf("adding handler: %s %s %#v", verb, string(pattern), cb)
cb := l.ToFunction(2)
//log.Printf("adding handler: %s %s %#v", verb, string(pattern), cb)
handlers := l.GetGlobal("_handlers").(*lua.LTable) handlers := l.GetGlobal("_handlers").(*lua.LTable)

View File

@ -16,6 +16,15 @@ import (
lua "github.com/yuin/gopher-lua" 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({ allows({
read = "world", read = "world",
@ -135,14 +144,14 @@ func NewScriptContext(db db.DB, getSend func(string) func(*proto.ClientMessage)
l = lua.NewState() l = lua.NewState()
// direction constants // direction constants
l.SetGlobal("east", lua.LString("_DIR_EAST")) l.SetGlobal("east", lua.LString(dirEast))
l.SetGlobal("west", lua.LString("_DIR_WEST")) l.SetGlobal("west", lua.LString(dirWest))
l.SetGlobal("north", lua.LString("_DIR_NORTH")) l.SetGlobal("north", lua.LString(dirNorth))
l.SetGlobal("south", lua.LString("_DIR_SOUTH")) l.SetGlobal("south", lua.LString(dirSouth))
l.SetGlobal("above", lua.LString("_DIR_ABOVE")) l.SetGlobal("above", lua.LString(dirAbove))
l.SetGlobal("below", lua.LString("_DIR_BELOW")) l.SetGlobal("below", lua.LString(dirBelow))
l.SetGlobal("up", lua.LString("_DIR_ABOVE")) l.SetGlobal("up", lua.LString(dirAbove))
l.SetGlobal("down", lua.LString("_DIR_BELOW")) l.SetGlobal("down", lua.LString(dirBelow))
// witch object behavior functions // witch object behavior functions
l.SetGlobal("has", l.NewFunction(witchHas)) 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 := l.GetGlobal("_handlers").(*lua.LTable)
handlers.ForEach(func(k, v lua.LValue) { handlers.ForEach(func(k, v lua.LValue) {
log.Println("checking handler verbs", k)
if k.String() != vc.Verb { if k.String() != vc.Verb {
return return
} }
v.(*lua.LTable).ForEach(func(kk, vv lua.LValue) { v.(*lua.LTable).ForEach(func(kk, vv lua.LValue) {
pattern := regexp.MustCompile(kk.String()) pattern := regexp.MustCompile(kk.String())
log.Println("checking handler", kk.String(), vv, pattern)
if pattern.MatchString(vc.Rest) { if pattern.MatchString(vc.Rest) {
// TODO TODO TODO TODO TODO // TODO TODO TODO TODO TODO
// this could be a remote code execution vuln; but by being here, I // this could be a remote code execution vuln; but by being here, I