Very basic stuff done, bot now pings and has a parsing function. More stuff after I get a drink

This commit is contained in:
Izaya 2015-10-18 02:41:07 +11:00
parent 6a951a0e1e
commit fdee904cd6

View File

@ -4,10 +4,11 @@ local serialization = require "serialization"
local config = {}
function loadconfig()
local f = io.open("config.lua","rb")
local f = io.open("./config.lua","rb")
local content = f:read("*a")
f:close()
config = serialization.unserialize(dat)
print(content)
config = serialization.unserialize(content)
end
function saveconfig()
@ -16,3 +17,55 @@ function saveconfig()
f:write(newconfig)
f:close()
end
function reply(chan,msg)
end
function os.sleep(n)
os.execute("sleep ".. tostring(tonumber(n)))
end
function parse(line)
if string.find(line, "PING :") == 1 then
local _,pingid = string.match(line,"([^,]+):([^,]+)")
writeln("PONG :"..pingid)
print("Pinged: "..pingid)
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
connection:settimeout(10)
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
line = connection:receive()
print(line)
until line == nil
print("Sent everything relevant. Joining channels.")
for k,v in pairs(config.channels) do
connection:send("JOIN " .. v.."\n")
end
repeat
line = connection:receive()
if line ~= nil and line ~= "timeout" then
print(line)
parse(line)
end
if line == nil then line = "" end
until string.find(line,"ERROR :Closing link:") ~= nil
print(connection:receive())
end
main()