Advertisement
Laine_prikol

IrcBrige

May 20th, 2018
360
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 2.03 KB | None | 0 0
  1. -- CONFIGURATION:
  2.  
  3. NAME = 'IRC'
  4. SERVER = 'irc.esper.net:6667'
  5. CHANNEL = '#ripirc'
  6. nick = 'Rippez'
  7.  
  8. local thread = require('thread')
  9. local internet = require('internet')
  10. local component = require('component')
  11. local event = require('event')
  12. local chatbox = component.chat_box
  13.  
  14. chatbox.setName(NAME)
  15.  
  16. function sendIRCMessageRaw(message)
  17.   socket:write(message .. '\r\n')
  18.   socket:flush()
  19. end
  20.  
  21. local function login()
  22.   if socket then socket:close() end
  23.   socket = internet.open(SERVER)
  24.   socket:setTimeout(0.05)
  25.   sendIRCMessageRaw('USER ' .. nick .. ' 0 * :' .. nick)
  26.   sendIRCMessageRaw('NICK ' .. nick)
  27. end
  28.  
  29. local t = thread.create(function()
  30.  while true do
  31.   local id, _, nick, msg = event.pull('chat_message')
  32.   if id == 'chat_message' then
  33.    print('Chat message: ', nick, msg)
  34.    sendIRCMessageRaw('PRIVMSG ' .. CHANNEL..' :' .. string.format('[%s] %s', nick, msg))
  35.   end
  36.  end
  37. end)
  38.  
  39. login()
  40.  
  41.  
  42. while true do
  43.  os.sleep(0)
  44. if not socket then login() end
  45.   repeat
  46.     local ok, line = pcall(socket.read, socket)
  47.     if ok then
  48.       if not line then login() end
  49.       print(line)
  50.       local match, prefix = line:match('^(:(%S+) )')
  51.       if prefix then prefix = prefix:match('^[^!]+') end
  52.       if match then line = line:sub(#match + 1) end
  53.       local match, command = line:match('^(([^:]%S*))')
  54.       if match then line = line:sub(#match + 1) end
  55.       repeat
  56.         local match = line:match('^( ([^:]%S*))')
  57.         if match then
  58.           line = line:sub(#match + 1)
  59.         end
  60.       until not match
  61.       local message = line:match('^ :(.*)$')
  62.       if command == '001' or command == '404' then
  63.         sendIRCMessageRaw('JOIN ' .. CHANNEL)
  64.       elseif command == '433' or command == '436' then
  65.         nick = nick .. string.char(math.random(97,122))
  66.         sendIRCMessageRaw('NICK ' .. nick)
  67.       elseif command == 'PING' then
  68.         sendIRCMessageRaw('PONG :' .. message)
  69.       elseif command == 'PRIVMSG' then
  70.         chatbox.say(prefix .. ": " .. message)
  71.       end
  72.     end
  73.   until not ok
  74. end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement