Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #!/usr/local/bin/lua
- --[[
- botClass.lua
- This contains the botClass object (table) and a few other functions.
- ]]--
- function explode ( seperator, str )
- local pos, arr = 0, {}
- for st, sp in function() return string.find( str, seperator, pos, true ) end do -- for each divider found
- table.insert( arr, string.sub( str, pos, st-1 ) ) -- Attach chars left of current divider
- pos = sp + 1 -- Jump past current divider
- end
- table.insert( arr, string.sub( str, pos ) ) -- Attach chars right of last divider
- return arr
- end
- function explodeLimit(seperator,str,limit)
- local originalTable = explode(seperator,str)
- local returnTable = {}
- local j=1
- for i,v in pairs(originalTable) do
- if j <= limit then
- returnTable[j] = v
- else
- returnTable[limit] = returnTable[limit]..seperator..v
- end
- j=j+1
- end
- return returnTable
- end
- function tableSize(table)
- local n=0
- for i in ipairs(table) do
- n=n+1
- end
- return n
- end
- ircBot={}
- function ircBot.writeRaw(this,rawStr)
- this.socket:send(rawStr.."\r\n")
- end
- function ircBot.quit(this,quitMsg)
- if quitMsg == nil then
- quitMsg = "Leaving..."
- end
- this:writeRaw("QUIT :"..quitMsg)
- end
- function ircBot.join(this,channel)
- if channel == nil then
- channel = this.channel
- end
- this:writeRaw("JOIN :"..channel)
- end
- function ircBot.part(this,channel,msg)
- if channel == nil then
- channel = this.channel
- end
- if msg == nil then
- msg = ""
- end
- this:writeRaw("PART "..channel.." :"..msg)
- end
- function ircBot.mode(this,modestr,channel)
- if channel == nil then
- channel = this.channel
- end
- this:writeRaw("MODE "..channel.." "..modestr)
- end
- function ircBot.privmsg(this,msg,channel)
- if channel == nil then
- channel = this.channel
- end
- this:writeRaw("PRIVMSG "..channel.." :"..msg)
- end
- function ircBot.notice(this,msg,channel)
- if channel == nil then
- channel = this.channel
- end
- this:writeRaw("NOTICE "..channel.." :"..msg)
- end
- function ircBot.ctcp(this,msg,client)
- this:notice(""..msg.."",client);
- end
- function ircBot.nick(this,nick)
- if nick == nil then
- nick = this.nickName
- end
- this.nickName = nick
- this:writeRaw("NICK "..nick)
- end
- function ircBot.getBuffer(this)
- this.buffer = this.socket:receive("*l")
- end
- function ircBot.defaultLoop(this)
- local buffer = this.buffer
- if string.sub(buffer,0,6) == "PING :" then
- local pingCode = explode(":",buffer)
- this:writeRaw("PONG "..pingCode[2])
- end
- if string.find(buffer," ") then
- local parts = explodeLimit(" ",buffer,4)
- if tableSize(parts) >= 4 then
- local hostParts = explode("!",string.sub(parts[1],1))
- if tableSize(hostParts) >= 2 then
- local thisNick = string.sub(hostParts[1],2)
- local thisHost = hostParts[2]
- local msgType = parts[2]
- local target = parts[3]
- local content = string.sub(parts[4],2)
- if msgType == "PRIVMSG" then
- if target == this.nickName then
- local strlen = string.len(content)
- if string.sub(content,0,1) == "" and string.sub(content,strlen,strlen) == "" then
- local ctcp = explode(" ",string.gsub(content,"",""))
- if ctcp[1] == "PING" then
- this:ctcp("PING "..ctcp[2],thisNick)
- elseif ctcp[1] == "VERSION" then
- this:ctcp("VERSION "..this.version,thisNick)
- end
- end
- end
- end
- end
- end
- end
- end
- function ircBot.init()
- this = ircBot
- this.userName="luaBot"
- this.nickName="luaBot"
- this.realName="luaBot"
- this.network=""
- this.port=6667
- this.channel="#test"
- this.socket=nil
- this.buffer=""
- this.loop=nil
- this.version="Leica's Lua Bot v0.2"
- return this
- end
- function ircBot.connect(this,network,port)
- if port == nil then
- port = 6667
- end
- this.network = network
- this.port = port
- this.socket = require("socket")
- this.socket = socket:tcp()
- this.socket:connect(network,port)
- this:writeRaw("USER "..this.userName.." 0 0 "..this.realName)
- this:nick()
- this:mode("+B",this.nickName)
- this:join()
- while this.buffer ~= nil do
- this:getBuffer()
- if this.buffer == nil then
- break
- end
- this:defaultLoop()
- if not this.loop ~= nil then
- this:loop()
- end
- end
- end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement