Advertisement
Guest User

leica

a guest
Dec 27th, 2009
179
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 4.12 KB | None | 0 0
  1. #!/usr/local/bin/lua
  2.  
  3. --[[
  4. botClass.lua
  5. This contains the botClass object (table) and a few other functions.
  6. ]]--
  7.  
  8. function explode ( seperator, str )
  9.     local pos, arr = 0, {}
  10.     for st, sp in function() return string.find( str, seperator, pos, true ) end do -- for each divider found
  11.         table.insert( arr, string.sub( str, pos, st-1 ) ) -- Attach chars left of current divider
  12.         pos = sp + 1 -- Jump past current divider
  13.     end
  14.     table.insert( arr, string.sub( str, pos ) ) -- Attach chars right of last divider
  15.     return arr
  16. end
  17.  
  18. function explodeLimit(seperator,str,limit)
  19.     local originalTable = explode(seperator,str)
  20.     local returnTable = {}
  21.     local j=1
  22.     for i,v in pairs(originalTable) do
  23.         if j <= limit then
  24.             returnTable[j] = v
  25.         else
  26.             returnTable[limit] = returnTable[limit]..seperator..v
  27.         end
  28.         j=j+1
  29.     end
  30.     return returnTable
  31. end
  32.  
  33. function tableSize(table)
  34.     local n=0
  35.     for i in ipairs(table) do
  36.         n=n+1
  37.     end
  38.     return n
  39. end
  40.  
  41. ircBot={}
  42.  
  43. function ircBot.writeRaw(this,rawStr)
  44.     this.socket:send(rawStr.."\r\n")
  45. end
  46.  
  47. function ircBot.quit(this,quitMsg)
  48.     if quitMsg == nil then
  49.         quitMsg = "Leaving..."
  50.     end
  51.     this:writeRaw("QUIT :"..quitMsg)
  52. end
  53.  
  54. function ircBot.join(this,channel)
  55.     if channel == nil then
  56.         channel = this.channel
  57.     end
  58.     this:writeRaw("JOIN :"..channel)
  59. end
  60.  
  61. function ircBot.part(this,channel,msg)
  62.     if channel == nil then
  63.         channel = this.channel
  64.     end
  65.     if msg == nil then
  66.         msg = ""
  67.     end
  68.     this:writeRaw("PART "..channel.." :"..msg)
  69. end
  70.  
  71. function ircBot.mode(this,modestr,channel)
  72.     if channel == nil then
  73.         channel = this.channel
  74.     end
  75.     this:writeRaw("MODE "..channel.." "..modestr)
  76. end
  77.  
  78. function ircBot.privmsg(this,msg,channel)
  79.     if channel == nil then
  80.         channel = this.channel
  81.     end
  82.     this:writeRaw("PRIVMSG "..channel.." :"..msg)
  83. end
  84.  
  85. function ircBot.notice(this,msg,channel)
  86.     if channel == nil then
  87.         channel = this.channel
  88.     end
  89.     this:writeRaw("NOTICE "..channel.." :"..msg)
  90. end
  91.  
  92. function ircBot.ctcp(this,msg,client)
  93.     this:notice(""..msg.."",client);
  94. end
  95.  
  96. function ircBot.nick(this,nick)
  97.     if nick == nil then
  98.         nick = this.nickName
  99.     end
  100.     this.nickName = nick
  101.     this:writeRaw("NICK "..nick)
  102. end
  103.  
  104. function ircBot.getBuffer(this)
  105.     this.buffer = this.socket:receive("*l")
  106. end
  107.  
  108. function ircBot.defaultLoop(this)
  109.     local buffer = this.buffer
  110.     if string.sub(buffer,0,6) == "PING :" then
  111.         local pingCode = explode(":",buffer)
  112.         this:writeRaw("PONG "..pingCode[2])
  113.     end
  114.     if string.find(buffer," ") then
  115.         local parts = explodeLimit(" ",buffer,4)
  116.         if tableSize(parts) >= 4 then
  117.             local hostParts = explode("!",string.sub(parts[1],1))
  118.             if tableSize(hostParts) >= 2 then
  119.                 local thisNick = string.sub(hostParts[1],2)
  120.                 local thisHost = hostParts[2]
  121.                 local msgType  = parts[2]
  122.                 local target   = parts[3]
  123.                 local content  = string.sub(parts[4],2)
  124.                 if msgType == "PRIVMSG" then
  125.                     if target == this.nickName then
  126.                         local strlen = string.len(content)
  127.                         if string.sub(content,0,1) == "" and string.sub(content,strlen,strlen) == "" then
  128.                             local ctcp = explode(" ",string.gsub(content,"",""))
  129.                             if ctcp[1] == "PING" then
  130.                                 this:ctcp("PING "..ctcp[2],thisNick)
  131.                             elseif ctcp[1] == "VERSION" then
  132.                                 this:ctcp("VERSION "..this.version,thisNick)
  133.                             end
  134.                         end
  135.                     end
  136.                 end
  137.             end
  138.         end
  139.     end
  140. end
  141.  
  142. function ircBot.init()
  143.     this = ircBot
  144.     this.userName="luaBot"
  145.     this.nickName="luaBot"
  146.     this.realName="luaBot"
  147.     this.network=""
  148.     this.port=6667
  149.     this.channel="#test"
  150.     this.socket=nil
  151.     this.buffer=""
  152.     this.loop=nil
  153.     this.version="Leica's Lua Bot v0.2"
  154.     return this
  155. end
  156.  
  157. function ircBot.connect(this,network,port)
  158.     if port == nil then
  159.         port = 6667
  160.     end
  161.     this.network = network
  162.     this.port = port
  163.     this.socket = require("socket")
  164.     this.socket = socket:tcp()
  165.     this.socket:connect(network,port)
  166.     this:writeRaw("USER "..this.userName.." 0 0 "..this.realName)
  167.     this:nick()
  168.     this:mode("+B",this.nickName)
  169.     this:join()
  170.     while this.buffer ~= nil do
  171.         this:getBuffer()
  172.         if this.buffer == nil then
  173.             break
  174.         end
  175.         this:defaultLoop()
  176.         if not this.loop ~= nil then
  177.             this:loop()
  178.         end
  179.     end
  180. end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement