Rewrote the parser, added timeouts

This commit is contained in:
Izaya 2016-01-13 01:35:19 +11:00
parent b978594993
commit ca311aef1e
2 changed files with 22 additions and 5 deletions

View File

@ -23,5 +23,6 @@ autojoin=true,
prefix=":", prefix=":",
username="yuki", username="yuki",
port=6667, port=6667,
timeout=300,
admins={"ShadowKatStudios","XeonSquared"} admins={"ShadowKatStudios","XeonSquared"}
} }

View File

@ -167,11 +167,22 @@ function parse(line)
print("Pinged: "..pingid) print("Pinged: "..pingid)
elseif string.find(line,":") == 1 and string.find(line,"PRIVMSG") ~= nil and string.find(line,"005") == nil then 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 local s = string.sub(line,2) -- I
local nick,s = string.match(s,"([^,]+)!([^,]+)") -- am local ms,me = string.find(s,"!") -- me = match end, ms = match start
local _,s = string.match(s,"([^,]+) PRIVMSG ([^,]+)") -- a local nick = string.sub(s,1,ms-1)
local chan, msg = string.match(s,"([^,]+) :([^,]+)") -- terrible 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
if chan == config.nick then chan = nick end if chan == config.nick then chan = nick end
print(nick .. " " .. chan .. " " .. msg) --person ]]--
print(nick,chan,msg) --person
parsemsg(nick,chan,msg) parsemsg(nick,chan,msg)
end end
end end
@ -199,6 +210,7 @@ function main()
local _,pingid = string.match(line,"([^,]+):([^,]+)") local _,pingid = string.match(line,"([^,]+):([^,]+)")
writeln("PONG :"..pingid) writeln("PONG :"..pingid)
print("Pinged: "..pingid) print("Pinged: "..pingid)
_G.lastping = os.time()
end end
if line ~= "" then if line ~= "" then
print(line) print(line)
@ -211,11 +223,15 @@ function main()
connection:send("JOIN " .. v.."\n") connection:send("JOIN " .. v.."\n")
end end
end end
_G.lastping = os.time()
repeat repeat
line = connection:receive() line = connection:receive()
if line ~= nil and line ~= "timeout" then if line ~= nil and line ~= "timeout" then
print(line) print(line)
pcall(parse,line) pcall(parse,line)
elseif line == "timeout" and os.time() > _G.lastping + config.timeout then
print("Connection to IRC timed out, aborting.")
break
end end
for k,v in ipairs(timers) do for k,v in ipairs(timers) do
local fail, errors = pcall(v,line) local fail, errors = pcall(v,line)