Skip to content

Commit

Permalink
added a load and unload command for plugins
Browse files Browse the repository at this point in the history
  • Loading branch information
terminaldweller committed Jun 9, 2024
1 parent c16f2f2 commit 309de82
Show file tree
Hide file tree
Showing 6 changed files with 128 additions and 21 deletions.
8 changes: 8 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -362,6 +362,14 @@ Joins a channel: `/join #channel`

Leaves a channel: `/leave #channel`

#### load

Load a plugin: `/load /plugins/rss.lua`

#### unload

Unload a plugin: `/unload /plugins/rss.lua`

## Deploy

### Docker
Expand Down
24 changes: 24 additions & 0 deletions main.go
Original file line number Diff line number Diff line change
Expand Up @@ -474,6 +474,30 @@ func runCommand(
}

handleCustomCommand(args, client, event, appConfig)
case "load":
if !isFromAdmin(appConfig.Admins, event) {
break
}

if len(args) < 2 { //nolint: mnd,gomnd
client.Cmd.Reply(event, errNotEnoughArgs.Error())

break
}

RunScript(args[1], client, appConfig)
case "unload":
if !isFromAdmin(appConfig.Admins, event) {
break
}

if len(args) < 2 { //nolint: mnd,gomnd
client.Cmd.Reply(event, errNotEnoughArgs.Error())

break
}

appConfig.deleteLstate(args[1])
default:
client.Cmd.Reply(event, errUnknCmd.Error())
}
Expand Down
7 changes: 7 additions & 0 deletions plugins.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package main

import (
"context"
"log"
"net/http"
"reflect"
Expand Down Expand Up @@ -285,6 +286,12 @@ func RunScript(scriptPath string, client *girc.Client, appConfig *TomlConfig) {
luaState := lua.NewState()
defer luaState.Close()

ctx, cancel := context.WithCancel(context.Background())

luaState.SetContext(ctx)

appConfig.insertLState(scriptPath, luaState, cancel)

luaState.PreloadModule("milla", millaModuleLoaderClosure(luaState, client, appConfig))
gluasocket.Preload(luaState)
gluaxmlpath.Preload(luaState)
Expand Down
42 changes: 39 additions & 3 deletions plugins/rss.lua
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
#!/usr/bin/env lua5.1

local milla = require("milla")
local yaml = require("yaml")
local http = require("http")
Expand All @@ -10,35 +12,64 @@ local function read_file(file)
return content
end

local function get_rss_feed()
local function sleep(n) os.execute("sleep " .. tonumber(n)) end

local function get_config()
local yaml_config = read_file("./plugins/rss.yaml")
local config = yaml.parse(yaml_config)
return config
end

local function get_rss_feed(config)
local titles = {}
local author_names = {}
local uris = {}
local rss_feed_list = {}

for _, v in pairs(config.rssfeeds) do
local response, err = http.request("GET", v.url)
if err ~= nil then
milla.send_message(err, "")
goto continue
end
local node, err = xmlpath.loadxml(response.body)
if err ~= nil then
milla.send_message(err, "")
goto continue
end

local path, err = xmlpath.compile("//entry/title")
if err ~= nil then
milla.send_message(err, "")
goto continue
end
local iterator = path:iter(node)
for _, match in ipairs(iterator) do
table.insert(titles, match:string())
end

path, err = xmlpath.compile("//entry/author/name")
-- local path, err = xmlpath.compile("//entry/title")
if err ~= nil then
milla.send_message(err, "")
goto continue
end
iterator = path:iter(node)
for _, match in ipairs(iterator) do
table.insert(author_names, match:string())
end

path, err = xmlpath.compile("//entry/author/uri")
-- local path, err = xmlpath.compile("//entry/title")
if err ~= nil then
milla.send_message(err, "")
goto continue
end
iterator = path:iter(node)
for _, match in ipairs(iterator) do
table.insert(uris, match:string())
end
::continue::
end

for i = 1, #titles do
Expand All @@ -50,8 +81,13 @@ local function get_rss_feed()
end

local function rss_feed()
local rss_feeds = get_rss_feed()
for _, v in pairs(rss_feeds) do milla.send_message(v, "#rssfeed") end
local config = get_config()
while true do
for _, v in pairs(get_rss_feed(config)) do
milla.send_message(v, config.channel)
sleep(config.period)
end
end
end

rss_feed()
2 changes: 2 additions & 0 deletions plugins/rss.yaml
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
period: 3600
channel: "#rssfeed"
rssfeeds:
- name: "one"
url: "https://www.youtube.com/feeds/videos.xml?channel_id=UCaiL2GDNpLYH6Wokkk1VNcg"
Expand Down
66 changes: 48 additions & 18 deletions types.go
Original file line number Diff line number Diff line change
@@ -1,9 +1,11 @@
package main

import (
"context"
"time"

"github.com/jackc/pgx/v5/pgxpool"
lua "github.com/yuin/gopher-lua"
)

type LogModel struct {
Expand All @@ -20,6 +22,11 @@ type CustomCommand struct {
Prompt string `toml:"prompt"`
}

type LuaLstates struct {
LuaState *lua.LState
Cancel context.CancelFunc
}

type TomlConfig struct {
IrcServer string `toml:"ircServer"`
IrcNick string `toml:"ircNick"`
Expand Down Expand Up @@ -49,30 +56,53 @@ type TomlConfig struct {
WebIRCAddress string `toml:"webIRCAddress"`
Plugins []string `toml:"plugins"`
CustomCommands map[string]CustomCommand `toml:"customCommands"`
Temp float64 `toml:"temp"`
RequestTimeout int `toml:"requestTimeout"`
MillaReconnectDelay int `toml:"millaReconnectDelay"`
IrcPort int `toml:"ircPort"`
KeepAlive int `toml:"keepAlive"`
MemoryLimit int `toml:"memoryLimit"`
PingDelay int `toml:"pingDelay"`
PingTimeout int `toml:"pingTimeout"`
TopP float32 `toml:"topP"`
TopK int32 `toml:"topK"`
EnableSasl bool `toml:"enableSasl"`
SkipTLSVerify bool `toml:"skipTLSVerify"`
UseTLS bool `toml:"useTLS"`
DisableSTSFallback bool `toml:"disableSTSFallback"`
AllowFlood bool `toml:"allowFlood"`
Debug bool `toml:"debug"`
Out bool `toml:"out"`
AdminOnly bool `toml:"adminOnly"`
LuaStates map[string]LuaLstates
Temp float64 `toml:"temp"`
RequestTimeout int `toml:"requestTimeout"`
MillaReconnectDelay int `toml:"millaReconnectDelay"`
IrcPort int `toml:"ircPort"`
KeepAlive int `toml:"keepAlive"`
MemoryLimit int `toml:"memoryLimit"`
PingDelay int `toml:"pingDelay"`
PingTimeout int `toml:"pingTimeout"`
TopP float32 `toml:"topP"`
TopK int32 `toml:"topK"`
EnableSasl bool `toml:"enableSasl"`
SkipTLSVerify bool `toml:"skipTLSVerify"`
UseTLS bool `toml:"useTLS"`
DisableSTSFallback bool `toml:"disableSTSFallback"`
AllowFlood bool `toml:"allowFlood"`
Debug bool `toml:"debug"`
Out bool `toml:"out"`
AdminOnly bool `toml:"adminOnly"`
pool *pgxpool.Pool
Admins []string `toml:"admins"`
IrcChannels []string `toml:"ircChannels"`
ScrapeChannels []string `toml:"scrapeChannels"`
}

func (config *TomlConfig) insertLState(
name string,
luaState *lua.LState,
cancel context.CancelFunc,
) {
if config.LuaStates == nil {
config.LuaStates = make(map[string]LuaLstates)
}
config.LuaStates[name] = LuaLstates{
LuaState: luaState,
Cancel: cancel,
}
}

func (config *TomlConfig) deleteLstate(name string) {
if config.LuaStates == nil {
return
}
config.LuaStates[name].Cancel()
delete(config.LuaStates, name)
}

type AppConfig struct {
Ircd map[string]TomlConfig `toml:"ircd"`
}
Expand Down

0 comments on commit 309de82

Please sign in to comment.