Sirshark10

CloudChat Server V2

May 23rd, 2016
206
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. local modem = peripheral.find("modem")
  2. local event, side, sChannel, id, data, dist
  3. local sessions = {}
  4. local channel = math.random(50000, 60000)
  5. local topBar = window.create(term.native(), 1, 1, 52, 1)
  6. local bg = window.create(term.native(), 1, 2, 52, 15)
  7. local chatBox = window.create(term.native(), 1, 17, 51, 3)
  8.  
  9. chatBox.setBackgroundColor(colors.gray)
  10. chatBox.clear()
  11. bg.setBackgroundColor(colors.lightGray)
  12. bg.clear()
  13. bg.setCursorPos(1, 2)
  14. topBar.setBackgroundColor(colors.blue)
  15. topBar.clear()
  16. topBar.setCursorPos(1, 1)
  17. topBar.write("Cloud Chat                                   Server")
  18.  
  19. modem.open(os.getComputerID())
  20.  
  21.  
  22. local function writeMsg(input)
  23.   input = input .. " "
  24.   local recursive
  25.   if #input > 50 and input:find("%s") then
  26.     recursive = true
  27.     local space = input:find("%s", 50)
  28.     local line = input:sub(0, space - 1)
  29.     input = input:sub(space + 1)
  30.     bg.write(line)
  31.   else
  32.     recursive = false
  33.     bg.write(input)
  34.   end
  35.  
  36.     local x, y = bg.getCursorPos()
  37.     if y >= 15 then
  38.     bg.scroll(1)
  39.     bg.setCursorPos(1, y)
  40.   else
  41.     bg.setCursorPos(1, y + 1)
  42.   end
  43.   if recursive then
  44.     writeMsg(input)
  45.   end
  46. end
  47.  
  48. local function receive()
  49.     event, side, sChannel, id, data, dist = os.pullEvent("modem_message")
  50. end
  51.  
  52. local function send(id, msg)
  53.     modem.transmit(id, os.getComputerID(), msg)
  54. end
  55.  
  56. local function indexOf(table, val)
  57.   for index, tmp in pairs(table) do
  58.     if tmp == val then
  59.       return index
  60.     end
  61.   end
  62.   return nil
  63. end
  64.  
  65. local function getSID()
  66.   local rand = math.random(0, 10000)
  67.   for index, tmp in pairs(sessions) do
  68.     if tmp == rand then
  69.       return getSID()
  70.     end
  71.   end
  72.   return rand
  73. end
  74.  
  75. local function input()
  76.     chatBox.clear()
  77.     chatBox.setCursorPos(1, 1)
  78.     term.redirect(chatBox)
  79.     data = read()
  80. end
  81.  
  82. while true do
  83.     event = parallel.waitForAny(receive, input)
  84.     if event == 1 then
  85.         local sid, user, msg
  86.         if data:find(":") then
  87.             local c = data:find(":")
  88.             sid = tonumber(data:sub(0, c - 1))
  89.             msg = data:sub(c + 1)
  90.             user = indexOf(sessions, sid)
  91.             if user == nil then
  92.                 send(id, "No such session")
  93.             elseif msg:sub(0, 1) == "!" then
  94.                 if msg == "!logout" then
  95.                     sessions[user] = nil
  96.                     send(id, "You have been logged out")
  97.                 elseif msg:find("%s") then
  98.                     local space = msg:find("%s")
  99.                     local command = msg:sub(0, space - 1)
  100.                     if command == "!nick" then
  101.                         local newName = msg:sub(space + 1)
  102.                         sessions[newName] = sid
  103.                         sessions[user] = nil
  104.                         msg = (user .. " changed nick to " .. newName)
  105.                         send(channel, msg)
  106.                         writeMsg(msg)
  107.                     end
  108.                 elseif msg == "!list" then
  109.                     msg = "Users: "
  110.                     for index, data in pairs(sessions) do
  111.                         msg = msg .. " " .. index
  112.                     end
  113.                     send(id, msg)
  114.                 elseif msg == "!whoami" then
  115.                     send(id, user)
  116.                 else
  117.                     send(id, "No such command")
  118.                 end
  119.             else
  120.                 msg = (user .. "> " .. msg)
  121.                 send(channel, msg)
  122.                 writeMsg(msg)
  123.             end
  124.         else
  125.             user = data
  126.             if sessions[user] == nil then
  127.                 sessions[user] = getSID()
  128.                 modem.transmit(id, channel, sessions[user])
  129.             else
  130.                 send(id, "Username in use")
  131.             end
  132.         end
  133.     else
  134.         if data ~= "" then
  135.             send(channel, "Server> " .. data)
  136.             writeMsg("Server> " .. data)
  137.         end
  138.     end
  139. end
Add Comment
Please, Sign In to add comment