Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- local args = {...}
- local modem = peripheral.find("modem")
- local user = args[1]
- local server = tonumber(args[2])
- local event, side, sChannel, id, data, dist, bg, chatBox, sid, H
- local debug = args[3]
- if debug then
- H = 2
- else
- H = 1
- end
- modem.open(os.getComputerID())
- local function send(msg)
- modem.transmit(server, os.getComputerID(), msg)
- end
- local function receive()
- event, side, sChannel, id, data, dist = os.pullEvent("modem_message")
- end
- local function writeMsg()
- data = data .. " "
- local recursive
- if #data > 50 and data:find("%s") then
- recursive = true
- local space = data:find("%s", 50)
- local line = data:sub(0, space - 1)
- data = data:sub(space + 1)
- bg.write(line)
- else
- recursive = false
- bg.write(data)
- end
- local x, y = bg.getCursorPos()
- if y >= 15 then
- bg.scroll(1)
- bg.setCursorPos(1, y)
- else
- bg.setCursorPos(1, y + 1)
- end
- if recursive then
- writeMsg()
- end
- end
- local function sendMsg(msg)
- send(sid .. ":" .. msg)
- receive()
- writeMsg()
- end
- local function init()
- term.clear()
- botBar = window.create(term.native(), 1, 17, 52, 3)
- botBar.setBackgroundColor(colors.gray)
- botBar.clear()
- bg = window.create(term.native(), 1, 2, 52, 15)
- bg.setBackgroundColor(colors.lightGray)
- bg.clear()
- bg.setCursorPos(1, 2)
- topBar = window.create(term.native(), 1, 1, 52, 1)
- topBar.setBackgroundColor(colors.blue)
- topBar.clear()
- topBar.setCursorPos(1, 1)
- topBar.write("Cloud Chat")
- if debug == "true" then
- topBar.setCursorPos(45,1)
- topBar.write("Debug")
- end
- chatBox = window.create(term.native(), 2, 18, 49, H)
- chatBox.setBackgroundColor(colors.black)
- chatBox.clear()
- end
- local function input()
- chatBox.setCursorPos(1, 1)
- term.redirect(chatBox)
- data = read()
- end
- init()
- send(user)
- receive()
- sid = tonumber(data)
- if sid == nil then
- writeMsg()
- sleep(2)
- os.pullEvent()
- term.clear()
- term.setCursorPos(1, 1)
- return
- else
- modem.open(id)
- end
- while true do
- chatBox.clear()
- event = parallel.waitForAny(input, receive)
- if event == 1 and sid ~= nil and data ~= "" then
- sendMsg(data)
- elseif event == 2 then
- writeMsg()
- end
- end
Add Comment
Please, Sign In to add comment