diff --git a/config.lua b/config.lua index 809b51e..42d46c2 100644 --- a/config.lua +++ b/config.lua @@ -1 +1 @@ -{server="localhost",realname="yuki",port=6667,channels={"#SKSDev"},hostname="lain",servername="lain",username="yuki",nick="yukichan"} \ No newline at end of file +{server="irc.quakenet.org",nick="yukichan",hostname="lain",servername="lain",realname="yuki",channels={"#SKSDev","#v","#ssss"},prefix=":",username="yuki",port=6667,admins={"ShadowKatStudios"}} diff --git a/init.lua b/init.lua index 45b277c..ee1c858 100644 --- a/init.lua +++ b/init.lua @@ -2,6 +2,7 @@ local socket = require "socket" local serialization = require "serialization" local config = {} +local cmds={} function loadconfig() local f = io.open("./config.lua","rb") @@ -13,7 +14,7 @@ end function saveconfig() local newconfig = serialization.serialize(config) - local f = io.open("config.lua","rb") + local f = io.open("config.lua","wb") f:write(newconfig) f:close() end @@ -26,11 +27,103 @@ function os.sleep(n) os.execute("sleep ".. tostring(tonumber(n))) end +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 + +function parsemsg(nick,chan,message) + if message:sub(-2) == "o/" and nick ~= "Shocky" and nick ~= "yukichan" then + os.sleep(4) + sendchan(chan,"\\o") + end + if string.find(message,":") == 1 then + 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 + 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) + end + elseif cmds[tCommand[1]] ~= nil then + pcall(cmds[tCommand[1]],nick,chan,tCommand,message) + end + end +end + function parse(line) if string.find(line, "PING :") == 1 then local _,pingid = string.match(line,"([^,]+):([^,]+)") writeln("PONG :"..pingid) print("Pinged: "..pingid) + 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 nick,s = string.match(s,"([^,]+)!([^,]+)") -- am + local _,s = string.match(s,"([^,]+) PRIVMSG ([^,]+)") -- a + local chan, msg = string.match(s,"([^,]+) :([^,]+)") -- terrible + if chan == config.nick then chan = nick end + print(nick .. " " .. chan .. " " .. msg) --person + parsemsg(nick,chan,msg) end end @@ -42,7 +135,9 @@ function main() print("Opening connection to "..ip) local connection = socket.connect(ip,config.port) function writeln(l) connection:send(l.."\n") end - connection:settimeout(10) + function sendchan(chan,msg) writeln("PRIVMSG "..chan.." :"..msg) end + function readln() return connection:receive() end + connection:settimeout(2) print("Connected!") os.sleep(1) connection:receive() -- drop a line @@ -52,7 +147,8 @@ function main() repeat line = connection:receive() print(line) - until line == nil + until string.match(line,"+i") ~= false + os.sleep(2) print("Sent everything relevant. Joining channels.") for k,v in pairs(config.channels) do connection:send("JOIN " .. v.."\n") @@ -61,7 +157,7 @@ function main() line = connection:receive() if line ~= nil and line ~= "timeout" then print(line) - parse(line) + pcall(parse,line) end if line == nil then line = "" end until string.find(line,"ERROR :Closing link:") ~= nil