Skip to content

Commit

Permalink
first lua script, WIP, rss script incoming
Browse files Browse the repository at this point in the history
  • Loading branch information
terminaldweller committed Jun 3, 2024
1 parent 165f496 commit f8738b5
Show file tree
Hide file tree
Showing 4 changed files with 36 additions and 19 deletions.
2 changes: 2 additions & 0 deletions main.go
Original file line number Diff line number Diff line change
Expand Up @@ -1046,6 +1046,8 @@ func runIRC(appConfig TomlConfig) {
chatGPTHandler(irc, &appConfig, &GPTMemory)
}

go LoadAllPlugins(&appConfig, irc)

if appConfig.DatabaseAddress != "" {
context, cancel := context.WithTimeout(context.Background(), time.Duration(appConfig.RequestTimeout)*time.Second)
defer cancel()
Expand Down
38 changes: 20 additions & 18 deletions plugins.go
Original file line number Diff line number Diff line change
@@ -1,11 +1,10 @@
package main

import (
"fmt"
"log"
"os"
"reflect"

"github.com/lrstanley/girc"
lua "github.com/yuin/gopher-lua"
)

Expand Down Expand Up @@ -180,33 +179,36 @@ func RegisterCustomLuaTypes(luaState *lua.LState) {
registerStructAsLuaMetaTable[LogModel](luaState, checkStruct, LogModel{}, "log_model")
}

func returnAllPlugins(pluginPath string) ([]string, error) {
pluginList := make([]string, 0)
func sendMessageClosure(luaState *lua.LState, client *girc.Client) func(*lua.LState) int {
return func(luaState *lua.LState) int {
message := luaState.CheckString(1)
target := luaState.CheckString(2)

files, err := os.ReadDir(pluginPath)
if err != nil {
return pluginList, fmt.Errorf("Error reading plugins directory: %v", err)
}
client.Cmd.Message(target, message)

for _, file := range files {
pluginList = append(pluginList, file.Name())
return 0
}

return pluginList, nil
}

func LoadAllPlugins(appConfig *TomlConfig) {
func RunScript(scriptPath string, client *girc.Client) {
luaState := lua.NewState()
defer luaState.Close()

RegisterCustomLuaTypes(luaState)

allPlugins, err := returnAllPlugins(appConfig.PluginPath)
if err != nil {
luaState.Close()
luaState.SetGlobal("send_message", luaState.NewFunction(sendMessageClosure(luaState, client)))

log.Fatal(err) //nolint: gocritic
log.Print("Running script: ", scriptPath)
err := luaState.DoFile(scriptPath)

if err != nil {
log.Print(err)
}
}

log.Println(allPlugins)
func LoadAllPlugins(appConfig *TomlConfig, client *girc.Client) {
for _, scriptPath := range appConfig.Plugins {
log.Print("Loading plugin: ", scriptPath)
go RunScript(scriptPath, client)
}
}
13 changes: 13 additions & 0 deletions plugins/test.lua
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
local function sleep(n)
local t0 = os.clock()
while os.clock() - t0 <= n do end
end

local function printer()
while true do
send_message("Hello, World!", "#warroom")
sleep(5)
end
end

printer()
2 changes: 1 addition & 1 deletion types.go
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@ type TomlConfig struct {
WebIRCGateway string `toml:"webIRCGateway"`
WebIRCHostname string `toml:"webIRCHostname"`
WebIRCAddress string `toml:"webIRCAddress"`
PluginPath string `toml:"pluginPath"`
Plugins []string `toml:"plugins"`
CustomCommands map[string]CustomCommand `toml:"customCommands"`
Temp float64 `toml:"temp"`
RequestTimeout int `toml:"requestTimeout"`
Expand Down

0 comments on commit f8738b5

Please sign in to comment.