Advertisement
osmarks

ChatWhy

Oct 14th, 2018
197
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 1.35 KB | None | 0 0
  1. local cb = peripheral.find "chat_box"
  2. local receiver = settings.get "cc2chat.receiver" or "gollark"
  3.  
  4. local rawterm = term.native()
  5.  
  6. local queue = ""
  7.  
  8. local redirect = {}
  9.  
  10. for k, v in pairs(rawterm) do redirect[k] = v end
  11.  
  12. function redirect.write(text)
  13.     queue = queue .. text
  14.     rawterm.write(text)
  15. end
  16.  
  17. function redirect.setCursorPos(x, y)
  18.     local cx, cy = term.getCursorPos()
  19.     local dx, dy = x - cx, y - cy
  20.     if dx > 0 then
  21.         queue = queue .. (" "):rep(dx)
  22.     end
  23.     if dy > 0 then
  24.         queue = queue .. (" "):rep(dy)
  25.     end
  26.     rawterm.setCursorPos(x, y)
  27. end
  28.  
  29. term.redirect(redirect)
  30.  
  31. local function evconvert()
  32.     while true do
  33.         local _, user, message = os.pullEvent "chat"
  34.         local raw = false
  35.         for x in message:gmatch "!(.*)" do message = x raw = true end
  36.         if user == receiver or settings.get "cc2chat.insecure" then
  37.             --[[for i = 1, #message do
  38.                 local char = message:sub(i, i)
  39.                 os.queueEvent("key", string.byte(char))
  40.                 os.queueEvent("char", char)
  41.             end]]
  42.             os.queueEvent("paste", message)
  43.             if not raw then
  44.                 os.queueEvent("key", 28) -- enter
  45.             end
  46.         end
  47.     end
  48. end
  49.  
  50. local function sendbatch()
  51.     while true do
  52.         if #queue > 0 then
  53.             cb.tell(receiver, queue, os.getComputerLabel():sub(1, 16))
  54.             queue = ""
  55.         end
  56.         sleep(0.1)
  57.     end
  58. end
  59.  
  60. parallel.waitForAll(
  61.     evconvert,
  62.     sendbatch,
  63.     function() shell.run "shell" end
  64. )
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement