From fdee904cd6aad4d335c36ff12699397b4d8f1e5c Mon Sep 17 00:00:00 2001 From: XeonSquared Date: Sun, 18 Oct 2015 02:41:07 +1100 Subject: [PATCH] Very basic stuff done, bot now pings and has a parsing function. More stuff after I get a drink --- init.lua | 57 ++++++++++++++++++++++++++++++++++++++++++++++++++++++-- 1 file changed, 55 insertions(+), 2 deletions(-) diff --git a/init.lua b/init.lua index 3b25d8d..45b277c 100644 --- a/init.lua +++ b/init.lua @@ -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()