Page 1 of 1

Basic IRC Client

PostPosted: Mon Jan 10, 2011 4:56 pm
by roxfox64
Okay, I figure I'll share with you guys part of this project I decided to work on.

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 = {}
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

function Client:JoinChan(Name)
  Client.channels[#Client.channels+1] = {Name, {}}

function Client:PartChan(Name)
for i=1,#Client.channels do
  if Client.channels[i][1] = Name then
   table.remove(Client.channels, i)

function Client:AutoJoin()
for i=1,#Client.autojoin do
  Client.socket:send("JOIN " .. Client.autojoin[i] .. "\r\n")

function Client:Exit()
Client.socket:send("QUIT Vuluna IRC out!\r\n")

function Client:connect(server, port)

  Client.socket = Socket.connect(server, port)
  while not Client.socket:isConnected() do System.sleep(100) end

function Client.setUserInfo(N,U)
  Client.Nickname = N
  Client.Username = U

function Client.setUserInfo(N,U,P)
  Client.Nickname = N
  Client.Username = U
  Client.Password = P

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

function Client:recv()
return Client.socket:recv(512)

  local cli = {}
  setmetatable(cli, Client)

  cli.Nickname = ""
  cli.Username = ""
  cli.Password = ""

return cli

function Client:send(str)
Client.socket:send(str .. "\r\n")

Very simply, load it up with your basic dofile("Client.lua") then we can go through the functions:

Everythign should start with

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:

CLI:connect("", 6667)

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.

string = CLI:recv()

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!


Re: Basic IRC Client

PostPosted: Tue Jan 11, 2011 1:26 pm
by crait
This is great, RoxFox64! :D Now, I'll modify it into a spammer. ;D

Re: Basic IRC Client

PostPosted: Tue Jan 11, 2011 2:11 pm
by roxfox64
It works best with LuaPlayer HM v6 IMO
You'll still need to learn the IRC Protocol yourself as there's still work needing to be done as far as building the client goes.

Re: Basic IRC Client

PostPosted: Tue Feb 22, 2011 10:32 am
by monkeymaximus
nice tutorial :)