Forum    News    Downloads    Saved Games


Basic IRC Client

<<

roxfox64

User avatar

Brew Guru
Brew Guru

Posts: 2287

Joined: June 12 2007

Location: Smyrna, GA Current Status: Its true... I am a fur.

Thanks given: 0

Thanks received: 2 times

Post Mon Jan 10, 2011 4:56 pm

Basic IRC Client

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.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.

  Code:
CLI = Client.new()


next, you'll want to setup your user information.

  Code:
CLI.setUserInfo("IRCNickname","IRCUsername")


I've included a second definition that accepts the password parameter.

  Code:
CLI.setUserInfo("IRCNickname","IRCUsername","PaSsWoRd")


Its time to connect now that all the user data is setup.
Initialize the wifi if it hasn't been already, then run this:

  Code:
CLI:connect("irc.freenode.net", 6667)


Just using freenode as an example. connect where ever you'd like.

Now, lets send our information RIGHT after that.

  Code:
CLI:ident()


Lets... See whats going on. I know there's a lot being gathered from the server.

  Code:
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)

  Code:
CLI:send("NICKSERV IDENTIFY MyPassword")


Now, when we want to leave, lets end this safely.

  Code:
CLI:Exit()


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~
5.00M33-6
Image
Image Veemon
Image
Image
<<

crait

User avatar

Brewology Administrator
Brewology Administrator

Posts: 6488

Joined: August 11 2006

Location: Narnia!

Thanks given: 195 times

Thanks received: 46 times

Post Tue Jan 11, 2011 1:26 pm

Re: Basic IRC Client

This is great, RoxFox64! :D Now, I'll modify it into a spammer. ;D
<<

roxfox64

User avatar

Brew Guru
Brew Guru

Posts: 2287

Joined: June 12 2007

Location: Smyrna, GA Current Status: Its true... I am a fur.

Thanks given: 0

Thanks received: 2 times

Post Tue Jan 11, 2011 2:11 pm

Re: Basic IRC Client

Figures~
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.
5.00M33-6
Image
Image Veemon
Image
Image
<<

monkeymaximus

User avatar

Brewery Owner
Brewery Owner

Posts: 1704

Joined: June 23 2005

Location: San Diego and Los Angeles and Seattle

Thanks given: 49 times

Thanks received: 37 times

Post Tue Feb 22, 2011 10:32 am

Re: Basic IRC Client

nice tutorial :)
[img]https://brewology.com/images/mm/monkeymaximus.jpg[/img]
http://img522.imageshack.us/img522/8907/monkeymaximus2ph.jpg

Return to Developers & Programming

Who is online

Users browsing this forum: No registered users and 5 guests

Powered by phpBB © 2000, 2002, 2005, 2007 phpBB Group.
Designed by ST Software for blacklist.org.