Necrotico

OC Remote Control

Sep 14th, 2021 (edited)
4,323
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 3.24 KB | None | 0 0
  1. -- Remote Control
  2. -- When installed on a computer, that computer can have files transfered to/from it and commands executed.
  3. -- Note that after this program is installed, keyboard input can only be given after a certain command is send to the computer.
  4.  
  5. -- This program is generally useful as a "boot-loader" kind of program, something to allow the updating and execution of a main program without the need of internet cards or monitor/keyboards.
  6.  
  7. local component = require("component")
  8. local event = require("event")
  9. local m = component.modem -- get primary modem component
  10. local fs = require("filesystem")
  11. local shell = require("shell")
  12. local process = require("process")
  13.  
  14. local PORT_FILE = "/home/port.txt"
  15. local PORT = 17
  16.  
  17. local function readFile(path)
  18.     if not fs.exists(path) then
  19.         return false
  20.     else
  21.         local f_h = fs.open(path, "r")
  22.         local rtn = f_h:read(fs.size(path))
  23.         f_h:close()
  24.         return rtn
  25.     end
  26. end
  27. local function writeFile(path, data_string)
  28.     local f_h = fs.open(path, "w")
  29.     f_h:write(data_string)
  30.     f_h:close()
  31. end
  32.  
  33. local function getMessage()
  34.     local _, _, from, port, _, message = event.pull("modem_message")
  35.     return message
  36. end
  37. local function sendMessage(msg)
  38.     m.broadcast(PORT, msg)
  39. end
  40.  
  41. local function sendFile(fileName, fileRaw) -- Copy this function to programs that will communicate with this one. Also copy the getMessage and sendMessage functions.
  42.     local file_length = fileRaw:len()
  43.     local sent_amount = 0
  44.  
  45.     sendMessage("download:start")
  46.     getMessage()
  47.     sendMessage(fileName)
  48.     getMessage()
  49.     while sent_amount < file_length do
  50.         local send_length = math.min(file_length, sent_amount + 500)
  51.         local send_string = fileRaw:sub(sent_amount + 1, send_length)
  52.         sent_amount = send_length
  53.         sendMessage(send_string)
  54.         print("Sending File (" .. tostring(file_length - sent_amount) .. " bytes remaining)")
  55.         getMessage()
  56.     end
  57.     print("File sent!")
  58.     sendMessage("download:end")
  59. end
  60. local function downloadFile()
  61.     sendMessage("download:name")
  62.     local file_name = getMessage()
  63.     local file_raw = ""
  64.     sendMessage("download:next")
  65.     while true do
  66.         local msg = getMessage()
  67.         if msg == "download:end" then
  68.             break
  69.         else
  70.             file_raw = file_raw .. msg
  71.         end
  72.         sendMessage("download:next")
  73.     end
  74.     writeFile("/home/" .. file_name, file_raw)
  75. end
  76.  
  77. local argv = {...}
  78.  
  79. local function init_rc()
  80.     local rc_path = shell.resolve(process.info().path)
  81.     if not string.find(rc_path, ".lua") then
  82.         rc_path = rc_path .. ".lua"
  83.     end
  84.     writeFile("/home/.shrc", rc_path)
  85.     writeFile(PORT_FILE, argv[2])
  86. end
  87.  
  88. if argv[1] == "init" then
  89.     PORT = tonumber(argv[2])
  90.     init_rc()
  91. else
  92.     if fs.exists(PORT_FILE) then
  93.         PORT = tonumber(readFile(PORT_FILE))
  94.     end
  95. end
  96.  
  97. m.open(PORT)
  98. m.setStrength(64)
  99.  
  100. while true do
  101.     local msg = getMessage()
  102.     if msg == "download:start" then
  103.         downloadFile()
  104.     elseif msg == "execute" then
  105.         sendMessage("execute:command")
  106.         shell.execute(getMessage())
  107.     elseif msg == "exit" then
  108.         break
  109.     end
  110. end
  111. print("Remote Control Terminated.")
  112.  
Add Comment
Please, Sign In to add comment