2015-10-18 00:40:56 +11:00
|
|
|
local socket = require "socket"
|
|
|
|
local serialization = require "serialization"
|
|
|
|
|
2016-01-12 03:58:29 +11:00
|
|
|
config = {}
|
|
|
|
cmds = {}
|
|
|
|
hooks = {}
|
|
|
|
timers = {}
|
2015-10-18 00:40:56 +11:00
|
|
|
|
|
|
|
function loadconfig()
|
2015-10-18 02:41:07 +11:00
|
|
|
local f = io.open("./config.lua","rb")
|
2015-10-18 00:40:56 +11:00
|
|
|
local content = f:read("*a")
|
|
|
|
f:close()
|
2015-10-18 02:41:07 +11:00
|
|
|
print(content)
|
|
|
|
config = serialization.unserialize(content)
|
2016-01-12 03:58:29 +11:00
|
|
|
hooks = {}
|
|
|
|
for k,v in pairs(config.hooks) do
|
|
|
|
print("Loading hook "..v)
|
|
|
|
local fo=io.open("./hooks/" .. v)
|
|
|
|
local c = fo:read("*a")
|
|
|
|
local s,f = pcall(load,c)
|
|
|
|
fo:close()
|
|
|
|
if s then
|
2016-01-13 00:55:44 +11:00
|
|
|
table.insert(hooks,#hooks+1,f)
|
2016-01-12 03:58:29 +11:00
|
|
|
print("Hook "..v.." loaded")
|
|
|
|
else
|
|
|
|
print("Hook "..v.." failed to load:")
|
|
|
|
print(f)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
cmds = {}
|
|
|
|
for k,v in pairs(config.cmds) do
|
|
|
|
local fo=io.open("./cmds/" .. v)
|
|
|
|
local s,f = pcall(load,fo:read("*a"))
|
|
|
|
fo:close()
|
|
|
|
if s then
|
2016-01-13 18:07:52 +11:00
|
|
|
cmds[k]=f
|
2016-01-12 03:58:29 +11:00
|
|
|
print("Command "..v.." loaded")
|
|
|
|
else
|
|
|
|
print("Command "..v.." failed to load:")
|
|
|
|
print(f)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
timers = {}
|
|
|
|
for k,v in pairs(config.timers) do
|
|
|
|
local fo=io.open("./timers/" .. v)
|
|
|
|
local s,f = pcall(load,fo:read("*a"))
|
|
|
|
fo:close()
|
|
|
|
if s then
|
|
|
|
table.insert(timers,f)
|
|
|
|
print("Timer "..v.." loaded")
|
|
|
|
else
|
|
|
|
print("Timer "..v.."Failed to load:")
|
|
|
|
print(f)
|
|
|
|
end
|
|
|
|
end
|
2015-10-18 00:40:56 +11:00
|
|
|
end
|
|
|
|
|
|
|
|
function saveconfig()
|
|
|
|
local newconfig = serialization.serialize(config)
|
2016-01-07 22:38:53 +11:00
|
|
|
local f = io.open("config.lua","wb")
|
2015-10-18 00:40:56 +11:00
|
|
|
f:write(newconfig)
|
|
|
|
f:close()
|
|
|
|
end
|
2015-10-18 02:41:07 +11:00
|
|
|
|
|
|
|
function reply(chan,msg)
|
|
|
|
|
|
|
|
end
|
|
|
|
|
|
|
|
function os.sleep(n)
|
|
|
|
os.execute("sleep ".. tostring(tonumber(n)))
|
|
|
|
end
|
|
|
|
|
2016-01-07 22:38:53 +11:00
|
|
|
function checkAdmin(nick)
|
|
|
|
local pass=false
|
|
|
|
writeln("WHOIS "..nick)
|
|
|
|
repeat
|
|
|
|
line = readln()
|
|
|
|
print(line)
|
|
|
|
until line:find("330") ~= nil or line == nil
|
|
|
|
print ("Line: "..line)
|
|
|
|
if line == nil then return false end -- wat
|
|
|
|
local _,e = line:find(nick.." ")
|
|
|
|
print("Start: "..tostring(e))
|
|
|
|
local n,_ = line:find(" ",e+1)
|
|
|
|
print("End: "..tostring(n))
|
|
|
|
nick = line:sub(e+1,n-1)
|
|
|
|
print("Logged in as "..nick)
|
|
|
|
for k,v in pairs(config.admins) do
|
|
|
|
if nick == v then
|
|
|
|
print(v .. " = " .. nick)
|
|
|
|
pass = true
|
|
|
|
break
|
|
|
|
else
|
|
|
|
print(nick .. " != " .. v)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
return pass
|
|
|
|
end
|
|
|
|
|
|
|
|
function addcommand(fname,str)
|
|
|
|
-- don't use this without pcall
|
|
|
|
cmds[fname] = load(str)
|
|
|
|
end
|
|
|
|
|
2016-01-07 23:03:05 +11:00
|
|
|
leftHanging = {0,false}
|
|
|
|
|
2016-01-07 22:38:53 +11:00
|
|
|
function parsemsg(nick,chan,message)
|
2016-01-13 00:55:44 +11:00
|
|
|
for k,v in pairs(hooks) do
|
2016-01-13 01:41:25 +11:00
|
|
|
-- print("Running hook "..k)
|
2016-01-13 00:55:44 +11:00
|
|
|
local fail,errors = pcall(v,nick,chan,message)
|
|
|
|
if not fail then print(errors) end
|
2016-01-07 22:38:53 +11:00
|
|
|
end
|
2016-01-12 03:58:29 +11:00
|
|
|
if string.find(message,config.prefix) == 1 then
|
2016-01-07 22:38:53 +11:00
|
|
|
local command = message:sub(2) .. " "
|
|
|
|
if command == "" then return end
|
|
|
|
local tCommand = {}
|
|
|
|
for word in command:gmatch("%S+") do
|
|
|
|
table.insert(tCommand,word)
|
|
|
|
end
|
|
|
|
if tCommand[1]:lower() == "echo" then
|
|
|
|
local s = command:sub(6)
|
|
|
|
sendchan(chan,s)
|
|
|
|
elseif tCommand[1] == "echochan" then
|
|
|
|
local s = command:sub(11 + tCommand[2]:len())
|
|
|
|
sendchan(tCommand[2],s)
|
|
|
|
elseif tCommand[1]:lower() == "action" then
|
|
|
|
local s = command:sub(7)
|
|
|
|
sendchan(chan,string.char(1).."ACTION"..s..string.char(1))
|
|
|
|
elseif tCommand[1] == "actionchan" then
|
|
|
|
local s = command:sub(string.len("actionchan")+2+ tCommand[2]:len())
|
|
|
|
sendchan(tCommand[2],string.char(1).."ACTION"..s..string.char(1))
|
|
|
|
elseif tCommand[1] == "rawecho" then
|
|
|
|
if checkAdmin(nick) then
|
|
|
|
writeln(command:sub(9))
|
|
|
|
end
|
2016-01-12 03:58:29 +11:00
|
|
|
elseif tCommand[1] == "drop" then
|
|
|
|
leftHanging = {0, false}
|
2016-01-07 22:38:53 +11:00
|
|
|
elseif tCommand[1] == "join" then
|
|
|
|
writeln("JOIN "..tCommand[2])
|
|
|
|
elseif tCommand[1] == "lua" then
|
|
|
|
if checkAdmin(nick) then
|
|
|
|
local s = command:sub(4)
|
|
|
|
local worked,rval = pcall(load(s))
|
|
|
|
sendchan(chan,tostring(worked).." "..tostring(rval))
|
|
|
|
end
|
|
|
|
elseif tCommand[1] == "whois" then
|
|
|
|
writeln("WHOIS "..tCommand[2])
|
|
|
|
elseif tCommand[1] == "addcmd" and checkAdmin(nick) then
|
|
|
|
--I'll get back to this eventually
|
|
|
|
elseif tCommand[1] == "quit" then
|
|
|
|
if checkAdmin(nick) then
|
|
|
|
sendchan(chan,"Bye! o/")
|
|
|
|
writeln("QUIT :Blame telstra.")
|
|
|
|
print("Killed by "..nick)
|
2016-01-14 16:19:05 +11:00
|
|
|
os.exit(0)
|
|
|
|
end
|
|
|
|
elseif tCommand[1] == "restart" then
|
|
|
|
if checkAdmin(nick) then
|
|
|
|
sendchan(chan,"Bye! o/")
|
|
|
|
writeln("QUIT :Blame telstra.")
|
|
|
|
print("Killed by "..nick)
|
|
|
|
os.exit(1)
|
2016-01-07 22:38:53 +11:00
|
|
|
end
|
2016-01-15 08:33:18 +11:00
|
|
|
elseif tCommand[1] == "debug" then
|
|
|
|
if checkAdmin(nick) then
|
|
|
|
config.debug = not config.debug
|
|
|
|
end
|
2016-01-07 22:38:53 +11:00
|
|
|
elseif cmds[tCommand[1]] ~= nil then
|
2016-01-13 00:55:44 +11:00
|
|
|
local fail, errors = pcall(cmds[tCommand[1]],nick,chan,tCommand,message)
|
|
|
|
if not fail then print(errors) end
|
2016-01-07 22:38:53 +11:00
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2015-10-18 02:41:07 +11:00
|
|
|
function parse(line)
|
|
|
|
if string.find(line, "PING :") == 1 then
|
|
|
|
local _,pingid = string.match(line,"([^,]+):([^,]+)")
|
|
|
|
writeln("PONG :"..pingid)
|
2016-01-15 08:33:18 +11:00
|
|
|
print("[Ping] "..pingid)
|
2016-01-07 22:38:53 +11:00
|
|
|
elseif string.find(line,":") == 1 and string.find(line,"PRIVMSG") ~= nil and string.find(line,"005") == nil then
|
|
|
|
local s = string.sub(line,2) -- I
|
2016-01-13 01:35:19 +11:00
|
|
|
local ms,me = string.find(s,"!") -- me = match end, ms = match start
|
|
|
|
local nick = string.sub(s,1,ms-1)
|
|
|
|
s=string.sub(s,me+1)
|
|
|
|
local ms,me = string.find(s," PRIVMSG ")
|
|
|
|
s=string.sub(s,me+1)
|
|
|
|
local ms,me = string.find(s," :")
|
|
|
|
local chan = string.sub(s,1,ms-1)
|
|
|
|
local msg = string.sub(s,me+1)
|
|
|
|
--[[
|
|
|
|
-- old parser, kept for hysterical rasins
|
|
|
|
local nick,s = string.match(s,"([^,]+)!([^\n]+)") -- am
|
|
|
|
local _,s = string.match(s,"([^,]+) PRIVMSG ([^\n]+)") -- a
|
|
|
|
local chan, msg = string.match(s,"([^,]+) :([^\n]+)") -- terrible
|
2016-01-07 22:38:53 +11:00
|
|
|
if chan == config.nick then chan = nick end
|
2016-01-13 01:35:19 +11:00
|
|
|
]]--
|
2016-01-15 08:33:18 +11:00
|
|
|
print("["..chan.."] <"..nick.."> "..msg) --person
|
2016-01-07 22:38:53 +11:00
|
|
|
parsemsg(nick,chan,msg)
|
2015-10-18 02:41:07 +11:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
function main()
|
|
|
|
print("Loading config.")
|
|
|
|
loadconfig()
|
|
|
|
print("Config loaded, resolving "..config.server)
|
|
|
|
local ip = socket.dns.toip(config.server)
|
|
|
|
print("Opening connection to "..ip)
|
|
|
|
local connection = socket.connect(ip,config.port)
|
|
|
|
function writeln(l) connection:send(l.."\n") end
|
2016-01-07 22:38:53 +11:00
|
|
|
function sendchan(chan,msg) writeln("PRIVMSG "..chan.." :"..msg) end
|
|
|
|
function readln() return connection:receive() end
|
2016-01-12 03:58:29 +11:00
|
|
|
connection:settimeout(1)
|
2015-10-18 02:41:07 +11:00
|
|
|
print("Connected!")
|
|
|
|
os.sleep(1)
|
|
|
|
connection:receive() -- drop a line
|
|
|
|
print("Logging in.")
|
|
|
|
connection:send("NICK "..config.nick.."\n")
|
|
|
|
connection:send("USER "..config.username.." "..config.hostname.." "..config.servername.." "..config.realname.."\n")
|
|
|
|
repeat
|
2016-01-08 00:57:42 +11:00
|
|
|
line = connection:receive() or ""
|
|
|
|
if string.find(line, "PING :") == 1 then
|
|
|
|
local _,pingid = string.match(line,"([^,]+):([^,]+)")
|
|
|
|
writeln("PONG :"..pingid)
|
|
|
|
print("Pinged: "..pingid)
|
2016-01-13 01:35:19 +11:00
|
|
|
_G.lastping = os.time()
|
|
|
|
end
|
2016-01-12 03:58:29 +11:00
|
|
|
if line ~= "" then
|
|
|
|
print(line)
|
|
|
|
end
|
2016-01-08 00:57:42 +11:00
|
|
|
until string.match(line or "","%+i") ~= nil
|
2016-01-07 22:38:53 +11:00
|
|
|
os.sleep(2)
|
2015-10-18 02:41:07 +11:00
|
|
|
print("Sent everything relevant. Joining channels.")
|
2016-01-12 03:58:29 +11:00
|
|
|
if config.autojoin then
|
|
|
|
for k,v in pairs(config.channels) do
|
|
|
|
connection:send("JOIN " .. v.."\n")
|
|
|
|
end
|
2015-10-18 02:41:07 +11:00
|
|
|
end
|
2016-01-13 01:35:19 +11:00
|
|
|
_G.lastping = os.time()
|
2015-10-18 02:41:07 +11:00
|
|
|
repeat
|
|
|
|
line = connection:receive()
|
|
|
|
if line ~= nil and line ~= "timeout" then
|
2016-01-15 08:33:18 +11:00
|
|
|
if config.debug then print(line) end
|
2016-01-07 22:38:53 +11:00
|
|
|
pcall(parse,line)
|
2016-01-14 16:19:05 +11:00
|
|
|
else
|
|
|
|
--[[ if (line == "timeout" or line == nil) and os.time() > _G.lastping + config.timeout then
|
|
|
|
print(line)
|
|
|
|
print("Connection to IRC timed out, aborting.")
|
|
|
|
os.exit(1)
|
|
|
|
end]]--
|
2015-10-18 02:41:07 +11:00
|
|
|
end
|
2016-01-12 03:58:29 +11:00
|
|
|
for k,v in ipairs(timers) do
|
2016-01-13 00:55:44 +11:00
|
|
|
local fail, errors = pcall(v,line)
|
|
|
|
if not fail then print(errors) end
|
2016-01-07 23:03:05 +11:00
|
|
|
end
|
2015-10-18 02:41:07 +11:00
|
|
|
if line == nil then line = "" end
|
|
|
|
until string.find(line,"ERROR :Closing link:") ~= nil
|
|
|
|
print(connection:receive())
|
|
|
|
end
|
|
|
|
|
|
|
|
main()
|