Attempting an IRC client via Luasocket
Posted: Fri Jul 25, 2008 9:17 pm
Ok, I wanted to try and make an IRC client through Löve. I thought it'd be interesting.
I know of Luasocket for networking, so I figure I could just plop that where needed and include it in the main.lua file. Everything seemed fine. Didn't return errors(I should've had it assert if nil at the beginning. Argh). Then I tried to call the socket.tcp() object from update.
Run-time error: main.lua:43: attempt to index global 'client' (a nil value)
That's no good at all. socket.tcp() returns nil on error.
I really don't know the problem here. Am I applying the luasocket module wrong? Is it added incorrectly? I notice in a 3.2 topic in the other section, you mention modules now being static. Does that mean you have to compile with them included?
I'd love some help, thanks in advance.
Here is the code:
I know of Luasocket for networking, so I figure I could just plop that where needed and include it in the main.lua file. Everything seemed fine. Didn't return errors(I should've had it assert if nil at the beginning. Argh). Then I tried to call the socket.tcp() object from update.
Run-time error: main.lua:43: attempt to index global 'client' (a nil value)
That's no good at all. socket.tcp() returns nil on error.
I really don't know the problem here. Am I applying the luasocket module wrong? Is it added incorrectly? I notice in a 3.2 topic in the other section, you mention modules now being static. Does that mean you have to compile with them included?
I'd love some help, thanks in advance.
Here is the code:
Code: Select all
function load()
love.graphics.setCaption("LoveIRC")
font = love.graphics.newFont(love.default_font, 10)
love.graphics.setFont(font);
love.graphics.setColor(200, 200, 200);
--I think I'll store the messages sent in one giant array with a subarray for values of the message..
messages = {}
-- messages[1] = { name="Daman", message="aaaaaaaaaaaaaaaaaaaaaaaa", timesent="043213" } -- Timesent format is hh|mm|ss
-- messages[2] = { name="Seleia", message="bbbbbbbbbbbbbbbbbbbb", timesent="043214" }
--Here will be where we store the message we want to send
str = ""
--And let's keep this for stuff we receive
rcv = ""
--Just another variable. Usermessage boolean
umsg = 0
--Let's include the module. Pretty sure this makes /everything/ under it apply only to the module code. Love functions have left the building for the rest of load()
local socket = require("socket.core")
module("socket")
client = socket.tcp()
host = "cortex.datarealms.com"
nick = "Loveuser"
site = "google.com"
--Connect. Please connect.
client:connect(host, 6667)
--Let's check if we are connected and if so send the stuff we need to to the server to make ourselves recognized. This is the part where I'm unsure of the luasocket end of things. Perhaps I need to prefix the data with the host address? No clue
client:send("NICK " .. nick, 1, -1)
client:send("USER \"" .. site .. "\" \"" .. host .. "\" :doesnotmatter", 1, -1)
--We'll have to do a the time reply in update, as we need to wait for it to auth us and perform 439 & 931
end
function update(dt)
--Tell me if you can figure out a better way to handle this.
rcv = client:receive('*a')
if rcv then
if string.sub(rcv, 1, string.find(rcv," ")) == host then -- Handle messages from server(ping, motd, etc) differently
umsg = 0
else
umsg = 1
end
--Adding a table to messages with: name, message, timesent, umsg
table.insert(messages, { name=string.sub(rcv, 1, string.find(rcv," ")), message=string.sub(rcv, string.find(rcv," "), string.len(rcv)), timesent=os.date("%H%M%S"), umsg=umsg })
end
--Reply to ctcp time.
if string.find(rcv, "\001TIME\001") then
client:send("NOTICE " .. host .. " :\001TIME" .. os.date() .. "\001", 1, -1)
end
--Unsetting these so the if rcv will only activate when there's a new message.
umsg = 0
rcv = ""
end
function draw()
--If it's a user, add formatting.
for k,v in ipairs(messages) do
if v.umsg then
love.graphics.drawf(v.timesent .. " <" .. v.name .. "> " .. v.message, 300, (400 + font:getHeight() * k), 600)
else
love.graphics.drawf(v.timesent .. v.name .. v.message, 300, (400 + font:getHeight() * k), 600)
end
end
--DEBUG DEBUG DEBUG
love.graphics.draw("Recieved string: " .. rcv, 100, 100)
love.graphics.draw("Network: " .. host, 100, 90)
end