Skip to content

Commit 309de82

Browse files
added a load and unload command for plugins
1 parent c16f2f2 commit 309de82

File tree

6 files changed

+128
-21
lines changed

6 files changed

+128
-21
lines changed

README.md

+8
Original file line numberDiff line numberDiff line change
@@ -362,6 +362,14 @@ Joins a channel: `/join #channel`
362362

363363
Leaves a channel: `/leave #channel`
364364

365+
#### load
366+
367+
Load a plugin: `/load /plugins/rss.lua`
368+
369+
#### unload
370+
371+
Unload a plugin: `/unload /plugins/rss.lua`
372+
365373
## Deploy
366374

367375
### Docker

main.go

+24
Original file line numberDiff line numberDiff line change
@@ -474,6 +474,30 @@ func runCommand(
474474
}
475475

476476
handleCustomCommand(args, client, event, appConfig)
477+
case "load":
478+
if !isFromAdmin(appConfig.Admins, event) {
479+
break
480+
}
481+
482+
if len(args) < 2 { //nolint: mnd,gomnd
483+
client.Cmd.Reply(event, errNotEnoughArgs.Error())
484+
485+
break
486+
}
487+
488+
RunScript(args[1], client, appConfig)
489+
case "unload":
490+
if !isFromAdmin(appConfig.Admins, event) {
491+
break
492+
}
493+
494+
if len(args) < 2 { //nolint: mnd,gomnd
495+
client.Cmd.Reply(event, errNotEnoughArgs.Error())
496+
497+
break
498+
}
499+
500+
appConfig.deleteLstate(args[1])
477501
default:
478502
client.Cmd.Reply(event, errUnknCmd.Error())
479503
}

plugins.go

+7
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
package main
22

33
import (
4+
"context"
45
"log"
56
"net/http"
67
"reflect"
@@ -285,6 +286,12 @@ func RunScript(scriptPath string, client *girc.Client, appConfig *TomlConfig) {
285286
luaState := lua.NewState()
286287
defer luaState.Close()
287288

289+
ctx, cancel := context.WithCancel(context.Background())
290+
291+
luaState.SetContext(ctx)
292+
293+
appConfig.insertLState(scriptPath, luaState, cancel)
294+
288295
luaState.PreloadModule("milla", millaModuleLoaderClosure(luaState, client, appConfig))
289296
gluasocket.Preload(luaState)
290297
gluaxmlpath.Preload(luaState)

plugins/rss.lua

+39-3
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
#!/usr/bin/env lua5.1
2+
13
local milla = require("milla")
24
local yaml = require("yaml")
35
local http = require("http")
@@ -10,35 +12,64 @@ local function read_file(file)
1012
return content
1113
end
1214

13-
local function get_rss_feed()
15+
local function sleep(n) os.execute("sleep " .. tonumber(n)) end
16+
17+
local function get_config()
1418
local yaml_config = read_file("./plugins/rss.yaml")
1519
local config = yaml.parse(yaml_config)
20+
return config
21+
end
22+
23+
local function get_rss_feed(config)
1624
local titles = {}
1725
local author_names = {}
1826
local uris = {}
1927
local rss_feed_list = {}
2028

2129
for _, v in pairs(config.rssfeeds) do
2230
local response, err = http.request("GET", v.url)
31+
if err ~= nil then
32+
milla.send_message(err, "")
33+
goto continue
34+
end
2335
local node, err = xmlpath.loadxml(response.body)
36+
if err ~= nil then
37+
milla.send_message(err, "")
38+
goto continue
39+
end
2440

2541
local path, err = xmlpath.compile("//entry/title")
42+
if err ~= nil then
43+
milla.send_message(err, "")
44+
goto continue
45+
end
2646
local iterator = path:iter(node)
2747
for _, match in ipairs(iterator) do
2848
table.insert(titles, match:string())
2949
end
3050

3151
path, err = xmlpath.compile("//entry/author/name")
52+
-- local path, err = xmlpath.compile("//entry/title")
53+
if err ~= nil then
54+
milla.send_message(err, "")
55+
goto continue
56+
end
3257
iterator = path:iter(node)
3358
for _, match in ipairs(iterator) do
3459
table.insert(author_names, match:string())
3560
end
3661

3762
path, err = xmlpath.compile("//entry/author/uri")
63+
-- local path, err = xmlpath.compile("//entry/title")
64+
if err ~= nil then
65+
milla.send_message(err, "")
66+
goto continue
67+
end
3868
iterator = path:iter(node)
3969
for _, match in ipairs(iterator) do
4070
table.insert(uris, match:string())
4171
end
72+
::continue::
4273
end
4374

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

5283
local function rss_feed()
53-
local rss_feeds = get_rss_feed()
54-
for _, v in pairs(rss_feeds) do milla.send_message(v, "#rssfeed") end
84+
local config = get_config()
85+
while true do
86+
for _, v in pairs(get_rss_feed(config)) do
87+
milla.send_message(v, config.channel)
88+
sleep(config.period)
89+
end
90+
end
5591
end
5692

5793
rss_feed()

plugins/rss.yaml

+2
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
period: 3600
2+
channel: "#rssfeed"
13
rssfeeds:
24
- name: "one"
35
url: "https://www.youtube.com/feeds/videos.xml?channel_id=UCaiL2GDNpLYH6Wokkk1VNcg"

types.go

+48-18
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,11 @@
11
package main
22

33
import (
4+
"context"
45
"time"
56

67
"github.com/jackc/pgx/v5/pgxpool"
8+
lua "github.com/yuin/gopher-lua"
79
)
810

911
type LogModel struct {
@@ -20,6 +22,11 @@ type CustomCommand struct {
2022
Prompt string `toml:"prompt"`
2123
}
2224

25+
type LuaLstates struct {
26+
LuaState *lua.LState
27+
Cancel context.CancelFunc
28+
}
29+
2330
type TomlConfig struct {
2431
IrcServer string `toml:"ircServer"`
2532
IrcNick string `toml:"ircNick"`
@@ -49,30 +56,53 @@ type TomlConfig struct {
4956
WebIRCAddress string `toml:"webIRCAddress"`
5057
Plugins []string `toml:"plugins"`
5158
CustomCommands map[string]CustomCommand `toml:"customCommands"`
52-
Temp float64 `toml:"temp"`
53-
RequestTimeout int `toml:"requestTimeout"`
54-
MillaReconnectDelay int `toml:"millaReconnectDelay"`
55-
IrcPort int `toml:"ircPort"`
56-
KeepAlive int `toml:"keepAlive"`
57-
MemoryLimit int `toml:"memoryLimit"`
58-
PingDelay int `toml:"pingDelay"`
59-
PingTimeout int `toml:"pingTimeout"`
60-
TopP float32 `toml:"topP"`
61-
TopK int32 `toml:"topK"`
62-
EnableSasl bool `toml:"enableSasl"`
63-
SkipTLSVerify bool `toml:"skipTLSVerify"`
64-
UseTLS bool `toml:"useTLS"`
65-
DisableSTSFallback bool `toml:"disableSTSFallback"`
66-
AllowFlood bool `toml:"allowFlood"`
67-
Debug bool `toml:"debug"`
68-
Out bool `toml:"out"`
69-
AdminOnly bool `toml:"adminOnly"`
59+
LuaStates map[string]LuaLstates
60+
Temp float64 `toml:"temp"`
61+
RequestTimeout int `toml:"requestTimeout"`
62+
MillaReconnectDelay int `toml:"millaReconnectDelay"`
63+
IrcPort int `toml:"ircPort"`
64+
KeepAlive int `toml:"keepAlive"`
65+
MemoryLimit int `toml:"memoryLimit"`
66+
PingDelay int `toml:"pingDelay"`
67+
PingTimeout int `toml:"pingTimeout"`
68+
TopP float32 `toml:"topP"`
69+
TopK int32 `toml:"topK"`
70+
EnableSasl bool `toml:"enableSasl"`
71+
SkipTLSVerify bool `toml:"skipTLSVerify"`
72+
UseTLS bool `toml:"useTLS"`
73+
DisableSTSFallback bool `toml:"disableSTSFallback"`
74+
AllowFlood bool `toml:"allowFlood"`
75+
Debug bool `toml:"debug"`
76+
Out bool `toml:"out"`
77+
AdminOnly bool `toml:"adminOnly"`
7078
pool *pgxpool.Pool
7179
Admins []string `toml:"admins"`
7280
IrcChannels []string `toml:"ircChannels"`
7381
ScrapeChannels []string `toml:"scrapeChannels"`
7482
}
7583

84+
func (config *TomlConfig) insertLState(
85+
name string,
86+
luaState *lua.LState,
87+
cancel context.CancelFunc,
88+
) {
89+
if config.LuaStates == nil {
90+
config.LuaStates = make(map[string]LuaLstates)
91+
}
92+
config.LuaStates[name] = LuaLstates{
93+
LuaState: luaState,
94+
Cancel: cancel,
95+
}
96+
}
97+
98+
func (config *TomlConfig) deleteLstate(name string) {
99+
if config.LuaStates == nil {
100+
return
101+
}
102+
config.LuaStates[name].Cancel()
103+
delete(config.LuaStates, name)
104+
}
105+
76106
type AppConfig struct {
77107
Ircd map[string]TomlConfig `toml:"ircd"`
78108
}

0 commit comments

Comments
 (0)