コード例 #1
0
ファイル: handler.go プロジェクト: tyler-sommer/squircy
func (h *LispScript) Handle(e *irc.Event) {
	defer scriptRecoveryHandler(h.man, e)

	lisp.SetHandler("print", func(vars ...lisp.Value) (lisp.Value, error) {
		if len(vars) == 1 {
			h.man.conn.Privmsgf(replyTarget(e), vars[0].String())
		}
		return lisp.Nil, nil
	})
	_, err := runUnsafeLisp(fmt.Sprintf("(%s \"%s\" \"%s\" \"%s\")", h.fn, e.Arguments[0], e.Nick, e.Message()))

	if err == halt {
		panic(err)

	} else if err != nil {
		h.man.conn.Privmsgf(replyTarget(e), err.Error())

		return
	}
}
コード例 #2
0
ファイル: handler.go プロジェクト: tyler-sommer/squircy
func (h *ScriptHandler) Handle(e *irc.Event) {
	defer scriptRecoveryHandler(h.man, e)

	if h.repl == true {
		msg := e.Message()
		if strings.HasPrefix(msg, "!repl end") {
			h.man.conn.Privmsgf(replyTarget(e), "%s REPL session ended.", replTypePretty(h.replType))
			h.repl = false
			h.replType = ""
			return
		}

		switch {
		case h.replType == "lua":
			h.luaVm.Register("typename", func(vm *lua.State) int {
				o := vm.Typename(int(vm.Type(1)))
				h.luaVm.PushString(o)
				return 1
			})
			h.luaVm.Register("print", func(vm *lua.State) int {
				o := vm.ToString(1)
				h.man.conn.Privmsgf(replyTarget(e), o)
				return 0
			})
			h.luaVm.Register("setExternalProperty", func(vm *lua.State) int {
				key := vm.ToString(1)
				value := vm.ToString(2)
				h.data[key] = value
				return 0
			})
			h.luaVm.Register("getExternalProperty", func(vm *lua.State) int {
				key := vm.ToString(1)
				if val, ok := h.data[key]; ok {
					vm.PushString(val)
					return 1
				}
				return 0
			})
			err := runUnsafeLua(h.luaVm, msg)
			if err != nil {
				h.man.conn.Privmsgf(replyTarget(e), err.Error())
			}

		case h.replType == "js":
			h.jsVm.Set("print", func(call otto.FunctionCall) otto.Value {
				message, _ := call.Argument(0).ToString()
				h.man.conn.Privmsgf(replyTarget(e), message)
				return otto.Value{}
			})
			_, err := runUnsafeJavascript(h.jsVm, msg)
			if err != nil {
				h.man.conn.Privmsgf(replyTarget(e), err.Error())

				return
			}

		case h.replType == "lisp":
			lisp.SetHandler("print", func(vars ...lisp.Value) (lisp.Value, error) {
				if len(vars) == 1 {
					h.man.conn.Privmsgf(replyTarget(e), vars[0].String())
				}
				return lisp.Nil, nil
			})
			lisp.SetHandler("setex", func(vars ...lisp.Value) (lisp.Value, error) {
				if len(vars) != 2 {
					return lisp.Nil, nil
				}
				key := vars[0].String()
				value := vars[1].String()
				h.data[key] = value
				return lisp.Nil, nil
			})
			lisp.SetHandler("getex", func(vars ...lisp.Value) (lisp.Value, error) {
				if len(vars) != 1 {
					return lisp.Nil, nil
				}
				key := vars[0].String()
				if val, ok := h.data[key]; ok {
					return lisp.StringValue(val), nil
				}
				return lisp.Nil, nil
			})
			_, err := runUnsafeLisp(msg)
			if err != nil {
				h.man.conn.Privmsgf(replyTarget(e), err.Error())

				return
			}
		}

		return
	}

	switch command, args := parseCommand(e.Message()); {
	case command == "":
		break

	case command == "register":
		if len(args) != 2 && args[0] != "js" && args[0] != "lua" && args[0] != "lisp" {
			h.man.conn.Privmsgf(replyTarget(e), "Invalid syntax. Usage: !register <js|lua|lisp> <fn name>")

			return
		}

		switch {
		case args[0] == "js":
			handler := newJavascriptScript(h.man, h.jsVm, args[1])
			h.man.Remove(handler)
			h.man.Add(handler)

		case args[0] == "lua":
			handler := newLuaScript(h.man, h.luaVm, args[1])
			h.man.Remove(handler)
			h.man.Add(handler)

		case args[0] == "lisp":
			handler := newLispScript(h.man, args[1])
			h.man.Remove(handler)
			h.man.Add(handler)
		}

	case command == "unregister":
		if len(args) != 2 && args[0] != "js" && args[0] != "lua" && args[0] != "lisp" {
			h.man.conn.Privmsgf(replyTarget(e), "Invalid syntax. Usage: !unregister <js|lua|lisp> <fn name>")

			return
		}

		switch {
		case args[0] == "js":
			h.man.conn.Privmsgf(replyTarget(e), "Unregistered Javsacript handler "+args[1])
			h.man.RemoveId("js-" + args[1])

		case args[0] == "lua":
			h.man.conn.Privmsgf(replyTarget(e), "Unregistered Lua handler "+args[1])
			h.man.RemoveId("lua-" + args[1])

		case args[0] == "lisp":
			h.man.conn.Privmsgf(replyTarget(e), "Unregistered Lisp handler "+args[1])
			h.man.RemoveId("lisp-" + args[1])
		}

	case command == "repl":
		if len(args) != 1 && args[0] != "js" && args[0] != "lua" && args[0] != "lisp" {
			h.man.conn.Privmsgf(replyTarget(e), "Invalid syntax. Usage: !repl <js|lua|lisp>")
			return
		}

		h.repl = true
		h.replType = args[0]
		h.man.conn.Privmsgf(replyTarget(e), "%s REPL session started.", replTypePretty(h.replType))
	}
}