Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- --[[
- Rednet tools ver 2.5 made by JustGekto
- Free to use modify and distribute
- ONLY IF LINK TO ORIGINAL PRESENT
- --]]
- --Varibles
- local pause = false
- local settings = {}
- local xc,yc = term.getCursorPos()
- local mode = 1
- local messages = {}
- local np = print
- local xp,yp = 0,0
- scripts = {}
- xm,ym = term.getSize()
- --Functions
- local function toboolean(text)
- if text == "true" then
- return true
- else
- return false
- end
- end
- local function clear()
- shell.run("clear")
- end
- local function open(nChanel)
- peripheral.call(side, "open", nChanel )
- end
- local function close(nChanel)
- peripheral.call(side, "close", nChanel )
- end
- local function stringToTable(text)
- local done,result = pcall(loadstring("return "..text))
- if done then
- return result
- else
- term.setTextColor(colors.red)
- print("There was an error converting table:")
- print(result)
- sleep(1)
- return nil
- end
- end
- local function isTable(text)
- if string.byte(text) == 123 and string.byte(text,#text) == 125 then
- return true
- else
- return false
- end
- end
- local function mode_string()
- if mode == 1 then
- return "r"
- else
- return "s"
- end
- end
- local function print(text,b)
- if b ~= true then
- table.insert(messages,text)
- end
- np(text)
- end
- local function addMessage(msg)
- table.insert(messages,msg)
- end
- local function findModem()
- local PeriList = peripheral.getNames()
- for i=1,#PeriList do
- if peripheral.getType(PeriList[i]) == "modem" then
- return PeriList[i]
- end
- end
- end
- local function getn(table)
- local i = 0
- for key,val in pairs(table) do
- i = i+1
- end
- return i
- end
- function table_print (tt, indent, done)
- done = done or {}
- indent = indent or 0
- if type(tt) == "table" then
- for key, value in pairs (tt) do
- io.write(string.rep (" ", indent)) -- indent it
- if type (value) == "table" and not done [value] then
- done [value] = true
- io.write(string.format("%s = {\n", tostring (key)));
- addMessage(string.rep (" ", indent)..string.format("%s = {", tostring (key)))
- io.write(string.rep (" ", indent)) -- indent it
- table_print (value, indent + 1, done)
- io.write(string.rep (" ", indent)) -- indent it
- io.write("}\n");
- addMessage(string.rep (" ", indent).."}")
- else
- if type(value) == "string" then
- io.write(string.format("%s = \"%s\",\n", tostring (key), value))
- addMessage(string.rep (" ", indent)..string.format("%s = \"%s\",", tostring (key), value))
- else
- io.write(string.format("%s = %s,\n", tostring (key), tostring(value)))
- addMessage(string.rep (" ", indent)..string.format("%s = %s,", tostring (key), tostring(value)))
- end
- end
- end
- else
- io.write(tt .. "\n")
- end
- end
- local function send( nRecipient,nSender,message)
- nMessageID = math.random( 1, 2147483647 )
- local tMessage = {
- nMessageID = nMessageID,
- nRecipient = nRecipient,
- message = message,
- sProtocol = sProtocol,
- }
- peripheral.call(side,"transmit",nRecipient,nSender,tMessage)
- peripheral.call(side,"transmit",65533,nSender,tMessage)
- end
- local function DrawTopOLD()
- local x,y = term.getCursorPos()
- term.setCursorPos(1,1)
- term.setBackgroundColor(colors.lightGray)
- term.clearLine()
- term.setTextColor(colors.white)
- term.setCursorPos(1,1)
- term.write("Mode:"..mode_string())
- term.setCursorPos(8,1)
- term.write("Chanel:"..chanel)
- term.setCursorPos(xm-2,1)
- term.write("E")
- term.setCursorPos(xm-4,1)
- term.write("C")
- term.setCursorPos(xm-6,1)
- term.write("S")
- term.setTextColor(colors.white)
- term.setBackgroundColor(colors.red)
- term.setCursorPos(xm,1)
- term.write("X")
- term.setBackgroundColor(colors.white)
- term.setTextColor(colors.gray)
- term.setCursorPos(x,y)
- end
- local function DrawTop()
- local x,y = term.getCursorPos()
- term.setCursorPos(1,1)
- term.setBackgroundColor(colors.lightGray)
- term.clearLine()
- term.setCursorPos(1,1)
- term.setTextColor(colors.white)
- term.write("Menu")
- term.setCursorPos(6,1)
- term.setTextColor(colors.white)
- term.write("Chanel:"..chanel)
- term.setTextColor(colors.white)
- term.setCursorPos(20,1)
- if pause then
- term.setTextColor(colors.red)
- term.write("||")
- else
- term.setTextColor(colors.green)
- term.write(string.char(16))
- end
- term.setCursorPos(xm,1)
- term.setBackgroundColor(colors.red)
- term.setTextColor(colors.white)
- term.write("X")
- term.setBackgroundColor(colors.white)
- term.setTextColor(colors.gray)
- term.setCursorPos(x,y)
- end
- local function DrawMain()
- term.setBackgroundColor(colors.white)
- clear()
- DrawTop()
- term.setBackgroundColor(colors.white)
- term.setTextColor(colors.gray)
- term.setCursorPos(1,2)
- end
- local function addScript()
- term.setBackgroundColor(colors.white)
- clear()
- DrawTop()
- term.setCursorPos(1,2)
- print("Name :",true)
- print("On id message:",true)
- print("With message :",true)
- print("To :",true)
- print("Send",true)
- print("Message :",true)
- print("To :",true)
- print("From :",true)
- term.setCursorPos(15,2)
- local name = read()
- term.setCursorPos(15,3)
- local sid = tonumber(read())
- term.setCursorPos(15,4)
- local sm = read()
- term.setCursorPos(15,5)
- local nr = tonumber(read())
- term.setCursorPos(15,7)
- local fm = read() -- fake message
- if isTable(fm) then
- fm = stringToTable(fm)
- end
- term.setCursorPos(15,8)
- local ri = tonumber(read()) -- fake receiver
- term.setCursorPos(15,9)
- local fs = tonumber(read()) -- fake sender
- open(chanel)
- mode = 1
- local data = {sid = sid,sm = sm,nr = nr,fm = fm,ri = ri,fs = fs,name = name}
- table.insert(scripts,data)
- term.setTextColor(colors.green)
- print("Saved!",true)
- term.setTextColor(colors.gray)
- sleep(0.3)
- end
- local function SaveScript()
- term.setBackgroundColor(colors.white)
- clear()
- DrawTop()
- term.setCursorPos(1,2)
- term.setTextColor(colors.black)
- for i=1,#scripts do
- x,y = term.getCursorPos()
- if x+#scripts[i].name+1 <= xm then
- term.write(scripts[i].name)
- if i ~= #scripts then
- term.write(",")
- end
- else
- term.setCursorPos(1,y+1)
- end
- end
- x,y = term.getCursorPos()
- term.setCursorPos(1,y+1)
- term.write("Name:")
- local name = read()
- for i=1,#scripts do
- if scripts[i].name == name then
- local fname = "RT/scripts/"..name..".rts"
- local s = scripts[i]
- file = fs.open(fname,"w")
- file.close()
- config.write(fname,"sid",s.sid)
- config.write(fname,"sm",s.sm)
- config.write(fname,"nr",s.nr)
- config.write(fname,"fm",s.fm)
- config.write(fname,"ri",s.ri)
- config.write(fname,"fs",s.fs)
- config.write(fname,"name",s.name)
- term.setTextColor(colors.green)
- print("Saved as "..fname,true)
- term.setTextColor(colors.lightGray)
- sleep(1)
- end
- end
- end
- function GetFileExtension(name)
- if name~= nil then
- return name:match("^.+(%..+)$")
- end
- end
- local function LoadScript()
- term.setBackgroundColor(colors.white)
- clear()
- DrawTop()
- term.setCursorPos(1,2)
- term.setTextColor(colors.black)
- local files = fs.list("RT/scripts")
- for i=1,#files do
- if GetFileExtension(files[i]) ~= ".rts" then
- table.remove(files,i)
- end
- end
- for i=1,#files do
- x,y = term.getCursorPos()
- if x+#files[i] <= xm then
- term.write(files[i])
- if i ~= #files then
- term.write(",")
- end
- else
- term.setCursorPos(1,y+1)
- end
- end
- x,y = term.getCursorPos()
- term.setCursorPos(1,y+1)
- term.write("Name:")
- local name = read()
- for i=1,#files do
- if name == files[i] then
- local data = {}
- local fname = "RT/scripts/"..files[i]
- data.sid = tonumber(config.read(fname,"sid"))
- data.sm = config.read(fname,"sm")
- data.nr = tonumber(config.read(fname,"nr"))
- data.fm = config.read(fname,"fm")
- data.ri = tonumber(config.read(fname,"ri"))
- data.fs = tonumber(config.read(fname,"fs"))
- data.name = config.read(fname,"name")
- table.insert(scripts,data)
- term.setTextColor(colors.green)
- print("Loaded "..files[i],true)
- term.setTextColor(colors.lightGray)
- sleep(1)
- end
- end
- end
- local function ExecuteScripts(data)
- id = data[4]
- msg = data[5].message
- receiver = data[5].nRecipient
- for i=1,#scripts do
- if id == scripts[i]["sid"] then
- if scripts[i]["sm"] == msg then
- if receiver == scripts[i]["nr"] then
- send(scripts[i]["ri"],scripts[i]["fs"],scripts[i]["fm"])
- print("Script \""..scripts[i]["name"].."\"",true)
- end
- end
- end
- end
- end
- local function FakeMessage()
- term.setBackgroundColor(colors.white)
- clear()
- DrawTop()
- term.setBackgroundColor(colors.white)
- term.setTextColor(colors.gray)
- term.setCursorPos(1,2)
- print("Sender id:",true)
- print("Receiver id:",true)
- print("Message :",true)
- term.setCursorPos(13,2)
- local s = tonumber(read())
- term.setCursorPos(13,3)
- local r = tonumber(read())
- term.setCursorPos(13,4)
- local m = read()
- if isTable(m) then
- m = stringToTable(m)
- end
- if m == nil then
- term.setTextColor(colors.red)
- print("Error!",true)
- term.setTextColor(colors.gray)
- return
- end
- open(chanel)
- send( r,s,m)
- mode = 1
- term.setTextColor(colors.green)
- print("Sent!",true)
- sleep(0.3)
- term.setTextColor(colors.gray)
- sleep(0.3)
- end
- local function RedrawMain()
- term.setBackgroundColor(colors.white)
- clear()
- DrawTop()
- term.setBackgroundColor(colors.white)
- term.setTextColor(colors.gray)
- term.setCursorPos(1,2)
- for i in pairs(messages) do
- print(messages[i],true)
- end
- end
- local function export()
- time = os.time()
- file = fs.open("RT/exports/rednet_data_"..time,"w")
- for i in pairs(messages) do
- file.writeLine(messages[i])
- end
- file.close()
- term.setTextColor(colors.green)
- term.setBackgroundColor(colors.white)
- print("Exported as ".."RT/exports/rednet_data_"..time,true)
- term.setTextColor(colors.gray)
- os.sleep(1)
- RedrawMain()
- end
- local function main()
- while true do
- e = {os.pullEvent()}
- if e[1] == "modem_message" and not pause then
- if type(e[5]) == "table" then
- ExecuteScripts(e)
- if type(e[5].message) == "string" or type(e[5].message) == "number" or type(e[5].message) == "boolean" then
- local msg = "id:"..e[4].." msg:"..tostring(e[5].message).." recipient:"..e[5].nRecipient.." chanel:"..e[3]
- print(msg)
- elseif type(e[5].message) == "table" then
- print("id:"..e[4].." recipient:"..e[5].nRecipient.." chanel:"..e[3].." msg:")
- print("{")
- table_print(e[5].message,1,false)
- print("}")
- end
- else
- local msg = "id:"..e[4].." msg:"..tostring(e[5]).." recipient:"..e[3]
- print(msg)
- end
- elseif e[1] == "mouse_click" then
- local x = e[3]
- local y = e[4]
- if x>=1 and x<= 4 and y == 1 then
- local ans = context.menu(1,2,{"Fake message"},{"Export"},{"Clear"},{"Set default chanel",true},"-",{"Add script"},{"Save script"},{"Load script"},"-",{"Uninstall"})
- if ans == "Fake message" then
- FakeMessage()
- RedrawMain()
- elseif ans == "Export" then
- RedrawMain()
- export()
- elseif ans == "Clear" then
- messages = {}
- DrawMain()
- elseif ans == "Add script" then
- addScript()
- elseif ans == "Save script" then
- SaveScript()
- elseif ans == "Load script" then
- LoadScript()
- elseif ans == "Set default chanel" then
- chanel = 65533
- config.write("RT/settings.cfg","chanel",chanel)
- elseif ans == "Uninstall" then
- shell.run("RT/uninstaller")
- break
- end
- RedrawMain()
- elseif x == xm and y == 1 then
- break
- elseif x >= 6 and x <= 12 + #tostring(chanel) and y == 1 then
- term.setCursorPos(1,1)
- term.setBackgroundColor(colors.lightGray)
- term.clearLine()
- term.setCursorPos(1,1)
- term.setTextColor(colors.white)
- term.write("Menu")
- term.setTextColor(colors.white)
- term.setBackgroundColor(colors.red)
- term.setCursorPos(xm,1)
- term.write("X")
- term.setCursorPos(6,1)
- term.setBackgroundColor(colors.lightGray)
- term.setTextColor(colors.white)
- if settings.oc then
- close(chanel)
- end
- term.write("Chanel:")
- chanel = tonumber(read())
- open(chanel)
- config.write("RT/settings.cfg","chanel",chanel)
- term.setBackgroundColor(colors.white)
- term.setTextColor(colors.gray)
- elseif x >= 20 and x <= 21 and y == 1 then
- pause = not pause
- RedrawMain()
- end
- end
- DrawTop()
- end
- end
- --Pre load checkers
- if pocket then
- term.setTextColor(colors.red)
- print("Warning program not optimized for small screen",false)
- sleep(1)
- end
- if not fs.exists("RT/API/config") then
- print("Some files may have been corrupted!",true)
- print("Running file recover...",true)
- shell.run("pastebin run XZktg6DH ")
- error("",0)
- end
- os.loadAPI("RT/API/config")
- if not fs.exists("RT/API/context") then
- print("Some files may have been corrupted!",true)
- print("Running file recover...",true)
- shell.run("pastebin run XZktg6DH ")
- error("",0)
- end
- os.loadAPI("RT/API/context")
- if fs.exists("RT/settings.cfg") then
- chanel = tonumber(config.read("RT/settings.cfg","chanel"))
- settings.oc = toboolean(config.read("RT/settings.cfg","single_chanel"))
- else
- file = fs.open("RT/settings.cfg","w")
- file.close()
- config.write("RT/settings.cfg","chanel","65533")
- config.write("RT/settings.cfg","single_chanel","false")
- chanel = 65533
- settings.oc = false
- end
- --Start
- side = findModem()
- DrawMain()
- if side == nil then
- error("You need modem to use this program",0)
- end
- open(65533)
- open(chanel)
- main()
- term.setBackgroundColor(colors.black)
- clear()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement