Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- local VERSION = "0.1"
- local MODEM = nil
- local NICKNAME = nil
- local ACTIVE = false
- local BUFFER = {}
- local POINTER = 0
- local ONLINE = {}
- local ISONLINE = false
- local ID = os.computerID()
- local LAST_MSG_TARGET = nil
- local CHANNEL = 1
- local SCROLL_POINTER = POINTER
- local WIDTH, HEIGHT = term.getSize()
- local LINES = HEIGHT - 6
- local START_LINE = 5
- local OPERATOR = "Chat"
- -- [ --------------------------------------------------------------------- ] --
- -- Split a string
- function split(str, pat)
- local t = {} -- NOTE: use {n = 0} in Lua-5.0
- if str ~= nil then
- local fpat = "(.-)" .. pat
- local last_end = 1
- local s, e, cap = str:find(fpat, 1)
- while s do
- if s ~= 1 or cap ~= "" then
- table.insert(t,cap)
- end
- last_end = e+1
- s, e, cap = str:find(fpat, last_end)
- end
- if last_end <= #str then
- cap = str:sub(last_end)
- table.insert(t, cap)
- end
- else
- print("##ERROR failed to split ["..str.."] by:"..pat)
- end
- return t
- end
- -- Log a message to file
- function log(message)
- local file = io.open("rednetchat.log", "a")
- file:write("\n" .. message)
- file:close()
- end
- -- Application entry
- function main()
- term.clear()
- term.setCursorPos(1, 1)
- if not setPeripherals() then
- print("[FATAL ERROR] Not able to setup peripherals.")
- return false
- end
- welcome()
- end
- -- Set the attached peripherals. Opens rednet modem and warps monitor
- function setPeripherals()
- local i, side
- for i, side in pairs(rs.getSides()) do
- if peripheral.isPresent(side) then
- if peripheral.getType(side) == "modem" then
- MODEM = side
- if not rednet.isOpen(side) then
- rednet.open(MODEM)
- end
- end
- end
- end
- -- Exit with a fatal error when modem not found
- if MODEM == nil then
- print("[FATAL ERROR] No modem was detected. Plase attach a modem on any side.")
- return false
- end
- return true
- end
- -- Start the welcome screen
- function welcome()
- local x, y
- term.clear()
- writeHeader()
- print("")
- print("")
- print("Scrivi il tuo nickname e premi [enter].")
- print("")
- term.write("Nickname: ")
- x, y = term.getCursorPos()
- while NICKNAME == nil or NICKNAME == "" do
- term.setCursorPos(x, y)
- NICKNAME = read()
- execute("/online")
- appendBuffer("[" .. OPERATOR .. "]: fai /help per i comandi")
- end
- start()
- end
- -- Writes the screen header
- function writeHeader()
- local col
- term.setCursorPos(1, 1)
- term.write("TheGameHydra CHAT " .. VERSION .. "")
- term.setCursorPos(1, 2)
- for col = 1, WIDTH do
- term.write("-")
- end
- end
- -- Writes the list of online users
- function writeOnlineList()
- local i, v, count, x, y, col
- count = 0
- x, y = term.getCursorPos()
- term.setCursorPos(1, HEIGHT - 1)
- for col = 1, WIDTH do
- term.write("-")
- end
- term.setCursorPos(1, HEIGHT)
- term.clearLine()
- term.write("Online: ")
- for i, v in pairs(ONLINE) do
- if count == 0 then
- term.write(i)
- else
- term.write(", " .. i)
- end
- count = count + 1
- end
- if count == 0 then
- term.write("Nobody online in channel " .. CHANNEL)
- end
- term.setCursorPos(x, y)
- end
- -- Start the chat
- function start()
- term.clear()
- writeHeader()
- writeOnlineList()
- ACTIVE = true
- showBuffer()
- parallel.waitForAll(input, watchEvents)
- end
- -- Stop the application
- function stop()
- ACTIVE = false
- end
- -- Reset the application
- function reset()
- execute("/offline")
- if rednet.isOpen(MODEM) then
- rednet.close(MODEM)
- end
- sleep(1.5)
- os.reboot()
- end
- -- Watch all input to provide possible shortcuts (for example usernames)
- function watchEvents()
- local type, param, param2, param3, i, v
- while ACTIVE do
- type, param, param2, param3 = os.pullEvent()
- if type == "key" then
- if param == 200 then -- up
- scroll(-1)
- elseif param == 208 then -- down
- scroll(1)
- elseif param == 201 then -- pageup
- scroll(-12)
- elseif param == 209 then -- pagedown
- scroll(12)
- --else
- -- appendBuffer(tostring(param))
- end
- elseif type == "mouse_scroll" then
- if param == -1 then
- scroll(-1)
- else
- scroll(1)
- end
- elseif type == "rednet_message" then
- receive(param2)
- end
- end
- end
- -- Scroll through the chat
- function scroll(amount)
- SCROLL_POINTER = SCROLL_POINTER + amount
- showBuffer()
- end
- -- Handle input from the prompt
- function input()
- local message, col
- term.setCursorPos(1, 4)
- for col = 1, WIDTH do
- term.write("-")
- end
- while ACTIVE do
- term.setCursorPos(1, 3)
- term.clearLine()
- term.write("[" .. CHANNEL .. "] > ")
- message = read()
- if message ~= nil and message ~= "" then
- execute(message, "local")
- end
- end
- end
- -- Send a message
- function send(message, target)
- local request, serialized, x, encrypted
- request = {protocol = "rnc", nickname = NICKNAME, sender = ID, target = target, channel = CHANNEL, message = message}
- serialized = textutils.serialize(request)
- encrypted = ""
- for x = 1, #serialized do
- encrypted = encrypted .. string.char(serialized:byte(x) + 1)
- end
- if request.target ~= nil then
- rednet.send(request.target, encrypted)
- else
- rednet.broadcast(encrypted)
- end
- end
- -- Recieve a message
- function receive(message)
- local request, decrypted, x
- if message ~= nil and message ~= "" then
- decrypted = ""
- for x = 1, #message do
- decrypted = decrypted .. string.char(message:byte(x) - 1)
- end
- request = textutils.unserialize(decrypted)
- if request.protocol == "rnc" and request.channel == CHANNEL then
- if request.nickname ~= nil and request.nickname ~= "" then
- execute(request, "remote")
- end
- end
- end
- end
- -- Execute a command or add a chat message
- function execute(message, source)
- local command, splitCommand, nickname, id, body, onlineUser
- if message.nickname ~= nil then
- executeRemote(message)
- return
- end
- if message:sub(0, 1) == "/" then
- command = message:sub(2)
- if command == "quit"
- or command == "reset"
- or command == "restart"
- or command == "reboot"
- or command == "stop"
- then
- appendBuffer("[" .. OPERATOR .. "]: Stop applicazione")
- reset()
- elseif command == "online" then
- if not ISONLINE then
- send("/online")
- putOnline()
- appendBuffer("[" .. OPERATOR .. "]: Ora Sei Online")
- ISONLINE = true
- else
- appendBuffer("[" .. OPERATOR .. "]: Gia' sei online")
- end
- elseif command == "offline" then
- if ISONLINE then
- send("/offline")
- takeOffline()
- appendBuffer("[" .. OPERATOR .. "]: Ora tu sei Offline")
- ISONLINE = false
- else
- appendBuffer("[" .. OPERATOR .. "]: Gia' sei offline")
- end
- elseif command:sub(0, 5) == "nick " then
- takeOffline()
- NICKNAME = command:sub(6)
- putOnline()
- appendBuffer("[" .. OPERATOR .. "]: Nome Cambiato")
- elseif command:sub(0, 5) == "slap " then
- appendBuffer(command:sub(6) .. " e' stato ucciso da " .. NICKNAME)
- elseif command:sub(0, 4) == "msg " then
- splitCommand = split(command:sub(5), "%s")
- onlineUser = false
- for nickname, id in pairs(ONLINE) do
- if nickname == splitCommand[1] then
- body = command:sub(5 + splitCommand[1]:len() + 1)
- send(body, id)
- appendBuffer(NICKNAME .. " > " .. nickname .. ": " .. body)
- onlineUser = true
- LAST_MSG_TARGET = nickname
- end
- end
- if not onlineUser then
- appendBuffer("[" .. OPERATOR .. "]: User " .. splitCommand[1] .. " is not online")
- end
- elseif command:sub(0, 2) == "r " then
- if LAST_MSG_TARGET ~= nil then
- execute("/msg " .. LAST_MSG_TARGET .. " " .. command:sub(3), "local")
- else
- appendBuffer("[" .. OPERATOR .. "]: No valid user for message")
- end
- elseif command:sub(0, 5) == "join " then
- if CHANNEL ~= tonumber(command:sub(6)) then
- execute("/offline")
- CHANNEL = tonumber(command:sub(6))
- execute("/online")
- appendBuffer("[" .. OPERATOR .. "]: Entra " .. CHANNEL)
- else
- appendBuffer("[" .. OPERATOR .. "]: e' nel canale " .. CHANNEL)
- end
- elseif command == "help" then
- appendBuffer("[" .. OPERATOR .. "] Commands:")
- appendBuffer("/quit : Esci dalla chat")
- appendBuffer("/msg <nickname> <message> : mandi messaggio privato")
- appendBuffer("/r <message> : rispondi ad un messaggio privato")
- appendBuffer("/join <channel> : cambi canale")
- else
- appendBuffer("[" .. OPERATOR .. "]: Unknown command")
- end
- return
- end
- appendBuffer(NICKNAME .. ": " .. message)
- send(message)
- end
- --
- function putOnline(nickname, id)
- if nickname == nil or id == nil then
- nickname = NICKNAME
- id = ID
- end
- ONLINE[nickname] = id
- writeOnlineList()
- end
- --
- function takeOffline(nickname, id)
- if nickname == nil or id == nil then
- nickname = NICKNAME
- id = ID
- end
- ONLINE[nickname] = nil
- writeOnlineList()
- end
- --
- function executeRemote(request)
- local command
- if request.message:sub(0, 1) == "/" then
- command = request.message:sub(2)
- if command == "online" then
- putOnline(request.nickname, request.sender)
- appendBuffer("[" .. OPERATOR .. "]: " .. request.nickname .. " is now online")
- send("/metoo")
- elseif command == "offline" then
- takeOffline(request.nickname, request.sender)
- appendBuffer("[" .. OPERATOR .. "]: " .. request.nickname .. " is now offline")
- elseif command == "metoo" then
- putOnline(request.nickname, request.sender)
- end
- return
- end
- if request.target ~= nil then
- appendBuffer(request.nickname .. " > " .. NICKNAME .. ": " .. request.message)
- LAST_MSG_TARGET = request.nickname
- else
- appendBuffer(request.nickname .. ": " .. request.message)
- end
- end
- --
- function appendBuffer(message)
- local length
- length = message:len()
- if length > WIDTH then
- table.insert(BUFFER, message:sub(1, WIDTH))
- POINTER = POINTER + 1
- appendBuffer(message:sub(WIDTH + 1))
- else
- table.insert(BUFFER, message)
- POINTER = POINTER + 1
- end
- SCROLL_POINTER = POINTER
- showBuffer()
- end
- --
- function showBuffer()
- local i, line, bufferPointer, x, y, pointer
- pointer = SCROLL_POINTER
- if pointer == 0 then
- return
- elseif SCROLL_POINTER > POINTER then
- SCROLL_POINTER = POINTER
- pointer = POINTER
- elseif POINTER < LINES + 1 then
- SCROLL_POINTER = POINTER
- pointer = POINTER
- elseif POINTER > LINES and SCROLL_POINTER < LINES then
- SCROLL_POINTER = LINES
- pointer = SCROLL_POINTER
- end
- x, y = term.getCursorPos()
- line = START_LINE
- bufferPointer = -(LINES - 1 - pointer)
- for i = bufferPointer, bufferPointer + (LINES - 1) do
- term.setCursorPos(1, line)
- term.clearLine()
- if BUFFER[i] ~= nil then
- term.write(tostring(BUFFER[i]))
- end
- line = line + 1
- end
- term.setCursorPos(x, y)
- end
- -- Fire up the application
- main()
- -- Installation Notes: Install Corresponding Programs in the Folder onto the device before installing this program
- repeat
- print("Enter Command:")
- local command = read()
- string.lower(command)
- if command == "shutdown" then
- shell.run("shutdown")
- end
- if command == "reboot" then
- shell.run("reboot")
- end
- if command == "time" then
- shell.run("time")
- end
- if command == "chat" then
- -- This program runs the chat protocol without breaking the DanetOS security barrier
- print("")
- print("Join or Host? J or H:")
- local jorh = read()
- string.lower(jorh)
- if jorh == "h" then
- print("Enter Hostname:")
- local hostname = read()
- shell.run("chat host " .. hostname)
- end
- if jorh == "j" then
- print("Enter Hostname:")
- local hostname = read()
- print("Enter Username:")
- local username = read()
- shell.run("chat join " .. hostname .. " " .. username)
- end
- end
- if command == "danet" then
- -- Easy DA-NET script for Computercraft use with Rednet API
- -- Written by Rusettsten 5-13-2015
- rednet.open("back")
- print("Enter Receiving Computer's ID:")
- id = tonumber(read())
- print("Enter message/command:")
- message = read()
- rednet.send(id,message)
- rednet.close("back")
- --Records Message for Tampering Evidence
- local day = os.day()
- local curTime = os.time()
- local histWrite = fs.open("DanetHist/D-" .. day .. "-" .. curTime, "w")
- histWrite.writeLine("ID:" .. id .. " Message:" .. message)
- histWrite.close()
- print("Success!")
- end
- if command == "danet-p" then
- --This is the exact same as Danet, except it allows for the communication of a password and verification with a server before it can be completed
- rednet.open("back")
- print("Enter Computer ID:")
- local id = tonumber(read())
- print("Enter Computer Pass:")
- local pass = read("*")
- print("Loading...")
- rednet.send(id,pass)
- id2,verify = rednet.receive(20)
- if verify == "true" then
- print("")
- print("Verification Sucessful!")
- print("Enter Command:")
- local message = read()
- rednet.send(id, message)
- --Records Message for Tampering Evidence
- local day = os.day()
- local curTime = os.time()
- local histWrite = fs.open("DanetHist/DP-" .. day .. "-" .. curTime, "w")
- histWrite.writeLine("ID:" .. id .. " Message:" .. message)
- histWrite.close()
- end
- if verify == "false" then
- print("")
- print("Verification Failed.")
- end
- end
- if command == "danet-r" then
- --This is the DanetOS client for communicating with locked reactors
- rednet.open("back")
- print("Enter ReactComp ID:")
- compID = tonumber(read())
- print("Enter React Comp Pass")
- compPass = read("*")
- print("Loading...")
- rednet.send(compID,compPass)
- reactID,reactVerify = rednet.receive(20)
- if reactVerify == "true" then
- print("Connected to ReactComp!")
- print("Enter Command:")
- reactCom = read()
- string.lower(reactCom)
- rednet.send(compID, reactCom)
- --Records Message for Tampering Evidence
- local day = os.day()
- local curTime = os.time()
- local histWrite = fs.open("DanetHist/DRServ-" .. day .. "-" .. curTime, "w")
- histWrite.writeLine("ID:" .. compID .. " Message:" .. reactCom)
- histWrite.close()
- if reactCom == "fuelreact" or "fuelmax" or "fueltemp" or "fuellevel" or "casingtemp" or "energystored" then
- reactID2,reactResp = rednet.receive(5)
- print(reactResp)
- end
- end
- if reactVerify == "false" then
- print("Verification Failed.")
- end
- end
- if command == "programs" then
- print("")
- print("WARNING: SOME PROGRAMS ARE BLOCKED FOR RUSCOM COPYRIGHT PROTECTION.")
- print("")
- shell.run("programs")
- end
- if command == "adventure" then
- shell.run("adventure")
- end
- if command == ("pastebin" or "shell" or "apis" or "exit" or "move" or "multishell" or "fg" or "bg" or "delete" or "lua" or "list" or "cp" or "dir" or "ls" or "mkdir" or "mv" or "rm" or "rs" or "sh") then
- shell.run("clear")
- print("")
- print("WARNING: ATTEMPTED PROGRAM BLOCKED FOR DANETOS TAMPER PROTECTION.")
- print("Don't worry, these programs will be unblocked for the most part in the future.")
- print("")
- end
- if command == "drive" then
- shell.run("drive")
- end
- if command == "hello" then
- shell.run("hello")
- end
- if command == "gps" then
- shell.run("gps locate")
- end
- if command == "type" then
- shell.run("type")
- print("Note sure what this does...")
- end
- if command == ("clear" or "clr") then
- shell.run("clear")
- end
- if command == "redstone" then
- shell.run("redstone")
- end
- if command == "worm" then
- shell.run("worm")
- end
- if command == "uninstall" then
- shell.run("Uninstall")
- end
- if command == "id" then
- shell.run("id")
- end
- if command == "copy" then
- --Program for copying programs off of disks
- print("Enter program name to copy from disk.")
- progName = read()
- shell.run("copy /disk/" .. progName .. " /" .. progName)
- print("Done!")
- end
- if command == "battleship" then
- --Protocol for battleship, if installed, on a DanetOS computer
- print("Join or Host?")
- jorh = read()
- string.lower(jorh)
- if jorh == "join" then
- shell.run("battleship join")
- end
- if jorh == "host" then
- shell.run("battleship host")
- end
- end
- if command == "tictactoe" then
- shell.run("tictactoe")
- end
- if command == "help" then
- shell.run("Help")
- end
- if command == "color" then
- term.setTextColor(colors.green)
- print("Welcome to DanetOS Color!")
- term.setTextColor(colors.blue)
- print("Enter YOUR color:")
- term.setTextColor(colors.white)
- ycolor = read()
- string.lower(ycolor)
- if ycolor == "white" then
- term.setTextColor(colors.white)
- end
- if ycolor == "orange" then
- term.setTextColor(colors.orange)
- end
- if ycolor == "magenta" then
- term.setTextColor(colors.magenta)
- end
- if ycolor == "lightblue" then
- term.setTextColor(colors.lightBlue)
- end
- if ycolor == "yellow" then
- term.setTextColor(colors.yellow)
- end
- if ycolor == "lime" then
- term.setTextColor(colors.lime)
- end
- if ycolor == "pink" then
- term.setTextColor(colors.pink)
- end
- if ycolor == "gray" then
- term.setTextColor(colors.gray)
- end
- if ycolor == "lightgray" then
- term.setTextColor(colors.lightGray)
- end
- if ycolor == "cyan" then
- term.setTextColor(colors.cyan)
- end
- if ycolor == "purple" then
- term.setTextColor(colors.purple)
- end
- if ycolor == "blue" then
- term.setTextColor(colors.blue)
- end
- if ycolor == "brown" then
- term.setTextColor(colors.brown)
- end
- if ycolor == "green" then
- term.setTextColor(colors.green)
- end
- if ycolor == "red" then
- term.setTextColor(colors.red)
- end
- if ycolor == "black" then
- term.setTextColor(colors.black)
- end
- print("Color Set!")
- end
- if command == "paint" then
- print("Enter name of paint file:")
- paintFile = read()
- shell.run("paint " .. paintFile)
- end
- if command == "histlist" then
- shell.run("clear")
- shell.run("list DanetHist")
- end
- if command == "histview" then
- print("Enter History Log File:")
- histName = read()
- histFile = fs.open("DanetHist/" .. histName,"r")
- histRead = histFile.readAll()
- histFile.close()
- print(histRead)
- end
- if command == "word" then
- shell.run("clear")
- print("DanetWord Version 1.0![Local Print]")
- print("Enter document savename.")
- local wordSaveName = read()
- local wordFile = fs.open("DanetWord/DOC-" .. wordSaveName, "w")
- print("Would you like this document dated with the current in-game time? Y/N")
- local yorn = read()
- string.lower(yorn)
- if yorn == "y" then
- local day = os.day()
- local curTime = os.time()
- wordFile.writeLine("Day: " .. day .. " Time: " .. curTime)
- end
- print("Enter text:")
- local wordText = read()
- print("Enter starting print spacing")
- local wordSkip = tonumber(read())
- wordFile.writeLine(wordText)
- wordFile.close()
- print("Printing to left peripheral printer...")
- local printer = peripheral.wrap("left")
- printer.newPage()
- local printFile = fs.open("DanetWord/DOC-" .. wordSaveName, "r")
- repeat
- local printLine = printFile.readLine()
- printer.write(printLine)
- printer.setCursorPos(1,wordSkip)
- wordSkip = (wordSkip + 1)
- until not line
- printFile.close()
- printer.endPage()
- end
- if command == "update" then
- shell.run("Update")
- end
- until command == "rickroll"
- print("Sorry, that feature hasn't been implemented yet.")
- shell.run("DanetOS")
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement