Basic IRC Client
Its an IRC client of course, all in lua.
Lua Player's API made it really easy to make the first file I'm going to show you, the Client.lua which I consider to be more like a class:
Client.lua
- Code:
Client = {}
Client.__index = Client
Client.channels = {}
Client.autojoin = {}
Client.Nickname = ""
Client.Username = ""
Client.Password = ""
Client.socket = "placeholder"
function Client.addChannel(String)
Client.autojoin[#Client.autojoin+1] = String
end
function Client:JoinChan(Name)
Client.channels[#Client.channels+1] = {Name, {}}
end
function Client:PartChan(Name)
for i=1,#Client.channels do
if Client.channels[i][1] = Name then
table.remove(Client.channels, i)
break
end
end
end
function Client:AutoJoin()
for i=1,#Client.autojoin do
Client.socket:send("JOIN " .. Client.autojoin[i] .. "\r\n")
end
end
function Client:Exit()
Client.socket:send("QUIT Vuluna IRC out!\r\n")
Client.socket:close()
end
function Client:connect(server, port)
Client.socket = Socket.connect(server, port)
while not Client.socket:isConnected() do System.sleep(100) end
end
function Client.setUserInfo(N,U)
Client.Nickname = N
Client.Username = U
end
function Client.setUserInfo(N,U,P)
Client.Nickname = N
Client.Username = U
Client.Password = P
end
function Client:ident()
Client.socket:send("USER "..Client.Username.." 2 0 LuaPlayerHM\r\n")
Client.socket:send("NICK "..Client.Nickname.."\r\n")
if Client.Password ~= "" then Client.socket:send("PASS "..Client.Password .. "\r\n") end
end
function Client:recv()
return Client.socket:recv(512)
end
function Client.new()
local cli = {}
setmetatable(cli, Client)
cli.Nickname = ""
cli.Username = ""
cli.Password = ""
return cli
end
function Client:send(str)
Client.socket:send(str .. "\r\n")
end
Very simply, load it up with your basic dofile("Client.lua") then we can go through the functions:
Everythign should start with Client.new()
This takes no arguments and returns a new client to you for use. That said you're free to have more than one and use them for things. Making an IRC Client that connects to multiple networks perhaps.
next, you'll want to setup your user information.
I've included a second definition that accepts the password parameter.
Its time to connect now that all the user data is setup.
Initialize the wifi if it hasn't been already, then run this:
Just using freenode as an example. connect where ever you'd like.
Now, lets send our information RIGHT after that.
Lets... See whats going on. I know there's a lot being gathered from the server.
I didn't include a command that sends a message to nickserv as not every IRC Server uses Anope services. But, you might want to try this with send if the server you're using uses nickserv(freenode does, naturally)
Now, when we want to leave, lets end this safely.
Exit tells the server that we're leaving and it shuts down the socket easily.
This is a basic connection.
You can also use the autojoin table, and commands as well as the part and join commands. These are both meant to be handled by your application. the application would read the input from the server and determine when is appropriate to send/use these commands.
You can use the channels table to hold messages from that channel by simply adding into the table there (Client.channels[index][2]) It would probably be better to use a key here now that I think about it, but eh you get the general idea.
You should fill the autojoin list up some sort of way(Using Client.addChannel() of course.), then finally use the Client:AutoJoin() command to join the channels in that list. There's no remove function as its not likely you'll need one.
Please do use and enjoy!
~RoxFox64~