-
Notifications
You must be signed in to change notification settings - Fork 6
/
Copy pathmain.lua
executable file
·221 lines (197 loc) · 4.99 KB
/
main.lua
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
#!/usr/bin/env lua5.2
local lfs = require "lfs"
local irce = require "irce"
local cqueues = require "cqueues"
local onerror = require "http.connection_common".onerror
local ca = require "cqueues.auxlib"
local cc = require "cqueues.condition"
local cs = require "cqueues.socket"
local function log(...)
io.stderr:write(string.format(...), "\n")
end
local plugins = {}
local cq = cqueues.new()
local function connect(irc, config)
local sock
do
local err, errno
sock, err, errno = ca.fileresult(cs.connect {
host = config.host;
port = config.port or 6667;
})
if not sock then
return nil, err, errno
end
end
sock:onerror(onerror)
sock:setmode("t", "bn") -- Binary mode, no output buffering
if config.tls then
local ok, err, errno = sock:starttls(config.tls)
if not ok then
return nil, err, errno
end
end
irc:set_send_func(function(self, message) -- luacheck: ignore 212
return sock:write(message)
end)
cqueues.running():wrap(function()
for line in sock:lines() do
irc:process(line)
end
log("Disconnected.")
sock:shutdown()
irc:on_disconnect()
end)
return true
end
local function start(config)
if options == nil then
options = {}
end
local irc = irce.new()
irc:load_module(require "irce.modules.base")
irc:load_module(require "irce.modules.channel")
irc:load_module(require "irce.modules.message")
irc:load_module(require "irce.modules.motd")
local last_connect = 0
local function try_connect(self)
local now = os.time()
local since_last = now - last_connect
local timeout = config.reconnect_timeout or 30
if since_last < timeout then
log("Disconnecting too fast from %s:%d", config.host, config.port or 6667)
cqueues.sleep(timeout - since_last)
end
log("Reconnecting to %s:%d", config.host, config.port or 6667)
last_connect = now
local ok, err = connect(self, config)
if not ok then
log(err)
return try_connect(self)
end
end
function irc:on_disconnect()
try_connect(self)
end
-- Print to local console
irc:set_callback(irce.RAW, function(self, send, message) -- luacheck: ignore 212
print(("%s %s"):format(send and ">>>" or "<<<", message))
end)
-- Handle nick conflict
irc:set_callback("433", function(self, sender, info) -- luacheck: ignore 212
local old_nick = info[2]
local new_nick = "[" .. old_nick .. "]"
self:NICK(new_nick)
end)
local nickserv_identified = false
local nickserv_cond = cc.new()
-- When nickserv identify succeeds
irc:load_module({
hooks = {
NOTICE = function(_, _, sender, _, message, pm)
if not pm or
not sender[1]:lower() == "nickserv" or
not message:match("ou are now identified")
then
return
end
nickserv_identified = true
nickserv_cond:signal()
end;
}
})
local has_welcome = false
local welcome_cond = cc.new()
irc:set_callback("001", function()
has_welcome = true
welcome_cond:signal()
end)
function irc:load_plugins()
for file in lfs.dir("./plugins") do
if file:sub(1,1) ~= "." and file:match(".lua$") then
local func, err = loadfile("./plugins/"..file)
if func == nil then
log("Failed to parse plugin %s: %s", file, err)
else
local ok, plugin = pcall(func)
if not ok then
log("Failed to run plugin %s: %s", file, plugin)
else
ok, err = self:load_module(plugin)
if not ok then
log("Failed to load plugin %s: %s", file, err)
else
log("Successfully loaded plugin %s", file)
plugins[file] = plugin
end
end
end
end
end
end
function irc:unload_plugins()
for k, v in pairs(plugins) do
local ok, err = self:unload_module(v)
if not ok then
io.stderr:write(string.format("Failed to unload %s: %s\n", k, err))
end
plugins[k] = nil
end
end
function irc:reload_plugins()
self:unload_plugins()
self:load_plugins()
end
try_connect(irc, config)
irc:load_plugins()
-- Do connecting
assert(irc:NICK(config.nick))
assert(irc:USER(config.user or os.getenv"USER", "hashbang-bot"))
-- Once server has sent "welcome" line
cq:wrap(function()
while not has_welcome do
welcome_cond:wait()
end
local nickserv = config.nickserv
if nickserv then
-- identify with nickserv
local msg = nickserv.password
if nickserv.username then
msg = nickserv.username .. " " .. msg
end
irc:PRIVMSG("nickserv", "identify " .. msg)
end
-- join channels
for c, channel_config in pairs(config.channels) do
if channel_config.needs_registration then
cq:wrap(function()
while not nickserv_identified do
-- wait for nickserv
nickserv_cond:wait()
end
irc:JOIN(c)
end)
else
irc:JOIN(c)
end
end
end)
-- Send a PING every minute
cqueues.running():wrap(function()
local ping_counter = 0
while true do
cqueues.sleep(60)
ping_counter = ping_counter + 1
irc:PING(string.format("%d", ping_counter))
end
end)
end
local config = dofile "config.lua"
for _, conf in pairs(config) do
cq:wrap(start, conf)
end
local ok, err, _, thd = cq:loop()
if not ok then
err = debug.traceback(thd, err)
error(err)
end