Advertisement
eniallator

mRemote

Apr 29th, 2015
6,487
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. -- This is eniallator's Remote program.
  2. -- The way it works is by using a command line interface where you can run commands to perform various tasks.
  3. -- The purpose of this program is to be able to control other computers which are running http://pastebin.com/ss0BX2mM (which i have also made).
  4. -- if you don't know what to do there is a help list and you get it by simply typing "help" and that will list all of the available commands that you can run.
  5.  
  6. version = "1.1.1"
  7. -- Version is used for the updater program i've made here: http://pastebin.com/jSWFQsA1
  8.  
  9. rednet.open("back")
  10. local mProt = "masterRC"
  11. -- mProt is the protocol the computer will be using. You can also configure what side the wireless modem is on
  12.  
  13. local logScreen = true
  14. local logFolder = "mRemoteLogs"
  15. -- logScreen is a boolean variable where you can configure if you want the program to log whats displayed on the screen in the command line. The log folder is configurable to any folder of your choice just make sure its not one you are already using!
  16.  
  17. local typeStartup = "startup "
  18. local typeError = "error   "
  19. local typeInfoMSG = "infoMSG "
  20. local typeReceived = "received"
  21. local typeInput = "input   "
  22. -- The different types of messages as displayed in the log output
  23.  
  24. local help = {"list - list available computers to connect with","connect - connect to a computer","exit - exit the program","protocol - change the rednet protocol","clear - clear the screen"}
  25. -- The help table
  26.  
  27. function printMessage(logType, colour, toPrint)
  28.   term.setTextColor(colour)
  29.   print(toPrint)
  30.   log("[" .. logType .. "] " .. toPrint)
  31. end
  32. -- This function handles any printing to the console and also what colour you want the message in
  33.  
  34. function wordSplit(string)
  35.    local out = {}
  36.    for word in string:gmatch("%S+") do
  37.       table.insert(out, word:lower())
  38.    end
  39.    return out
  40. end
  41. -- This function splits up a string that's passed as an argument into a table at the spaces
  42.  
  43. function log(logMessage)
  44.   if logScreen then
  45.     if fs.exists(logFolder) and fs.isDir(logFolder) then
  46.       openLogFile = fs.open(logFile,"a")
  47.       openLogFile.writeLine(logMessage)
  48.       openLogFile.close()
  49.     end
  50.   end
  51. end
  52. -- This function handles the logging to the logfile in the logFolder folder
  53.  
  54. function prefix(write)
  55.   term.setTextColor(colors.yellow)
  56.   term.write(write)
  57.   term.setTextColor(colors.white)
  58.   prefixLog = write
  59. end
  60. -- Makes a prefix where you call functions on the command line
  61.  
  62. function startUp()
  63.   shell.run("clear")
  64.   printMessage(typeStartup,colors.blue,"Type in a command you want to run or type 'help' for a list of commands.")
  65. end
  66. -- Message displayed when you start up the program
  67.  
  68. function invalidID()
  69.   printMessage(typeError, colors.red, 'Invalid ID. Type "list" to list computers nearby.')
  70. end
  71. -- This is called when the connect function fails
  72.  
  73. if logScreen then
  74.   if fs.exists(logFolder) == false then
  75.     fs.makeDir(logFolder)
  76.   end
  77.  
  78.   if fs.isDir(logFolder) then
  79.     local files = fs.list(logFolder)
  80.  
  81.     if files[1] == nil then
  82.       logFile = logFolder .. "/log#0"
  83.     else
  84.       logFile = logFolder .. "/log#" .. #files
  85.     end
  86.   end
  87. end
  88. -- Makes log folder if there it is not made already and also makes log file
  89.  
  90. startUp()
  91. while true do
  92.   local status = {upload = 1,download = 1}
  93.   prefix(": ")
  94.   local input = read()
  95.   local inputTable = wordSplit(input)
  96.   log("[" .. typeInput .. '] "' .. prefixLog .. input .. '"')
  97.   -- Displays the prefix on screen aswell as gets an input using read()
  98.  
  99.   if inputTable[1] == "help" then
  100.     for l=1,#help do
  101.       if l%2 == 1 then
  102.         printMessage(typeInfoMSG, colors.lightBlue, help[l])
  103.       else
  104.         printMessage(typeInfoMSG, colors.blue, help[l])
  105.       end
  106.     end
  107.     -- Displays the help table
  108.  
  109.   elseif inputTable[1] == "exit" then
  110.     shell.run("clear")
  111.     break
  112.     -- Exit the program
  113.  
  114.   elseif inputTable[1] == "clear" then
  115.     shell.run("clear")
  116.  
  117.   elseif inputTable[1] == "protocol" or inputTable[1] == "prot" then
  118.     if inputTable[2] == nil then
  119.       printMessage(typeInfoMSG,colors.blue,"Type in the new rednet protocol you want to set or leave blank to not set. Current protocol is:")
  120.       printMessage(typeInfoMSG,colors.lightBlue,mProt)
  121.       prefix("protocol: ")
  122.       newProt = read()
  123.     else
  124.       newProt = inputTable[2]
  125.     end
  126.     -- Gets the newProt variable assigned to the user input whether if its in the second argument or gotten from when it prompts you
  127.  
  128.     log('[' .. typeInput .. '] "' .. prefixLog .. newProt .. '"')
  129.  
  130.     if newProt == "" then
  131.       printMessage(typeError,colors.red,"Nil is not a valid protocol.")
  132.     else
  133.       printMessage(typeInfoMSG,colors.green,"Updated protocol.")
  134.       mProt = newProt
  135.     end
  136.     -- Change the rednet protocol from the default
  137.  
  138.   elseif inputTable[1] == "list" or inputTable[1] == "ls" then
  139.     rednet.broadcast("list", mProt)
  140.     printMessage(typeInfoMSG,colors.blue,"id: description")
  141.  
  142.     while true do
  143.       local sendID, msg = rednet.receive(mProt, 0.1)
  144.       if sendID == nil then break end
  145.       printMessage(typeReceived,colors.green,sendID .. ": " .. msg)
  146.  
  147.     end
  148.     -- List all the available computers in the area that are running mControl and that aren't already connected
  149.  
  150.   elseif inputTable[1] == "connect" then
  151.     if inputTable[2] == nil then
  152.  
  153.       printMessage(typeInfoMSG,colors.blue,"Type in the id of the computer you want to connect with")
  154.       prefix("connect: ")
  155.       computerID = read()
  156.     else
  157.  
  158.       computerID = inputTable[2]
  159.     end
  160.  
  161.     log('[' .. typeInput .. '] "' .. prefixLog .. computerID .. '"')
  162.     -- Connect to a computer
  163.  
  164.     if not tonumber(computerID) or tonumber(computerID) > 65535 then
  165.       invalidID()
  166.  
  167.     else
  168.       local computerID = tonumber(computerID)
  169.       rednet.send(computerID, "connect", mProt)
  170.       local connectID, msg = rednet.receive(mProt, 0.1)
  171.       -- Trying to connect to computer using computer ID
  172.  
  173.       if connectID == nil then
  174.         invalidID()
  175.       else
  176.  
  177.         if msg[1] ~= nil then
  178.           printMessage(typeReceived,colors.green,msg[1])
  179.           status = msg[2]
  180.  
  181.         else
  182.           printMessage(typeReceived,colors.green,msg)
  183.  
  184.         end
  185.         -- Displays the message received from computer connecting to. By default this is "Connected!"
  186.  
  187.         local disconnected = false
  188.  
  189.         while true do
  190.         if message ~= nil and #message ~= 0 then
  191.           if message[2] == nil then
  192.             prefix(connectID .. ": ")
  193.  
  194.           else
  195.             prefix(connectID .. "/" .. message[2] .. ": ")
  196.  
  197.           end
  198.         else
  199.           prefix(connectID .. ": ")
  200.  
  201.         end
  202.         -- Checks to see if connected computer sent a table, this is for if the connected computer wants to put something in the following prefix then it can
  203.  
  204.         local command = read()
  205.         local commandTable = wordSplit(command)
  206.         log("[" .. typeInput .. '] "' .. prefixLog .. command .. '"')
  207.         -- Gets a new user input
  208.  
  209.           if commandTable[1] == "exit" then
  210.  
  211.             rednet.send(connectID, {"exit"}, mProt)
  212.             _, exitMessage = rednet.receive(mProt, 0.1)
  213.  
  214.             if exitMessage ~= nil then
  215.  
  216.               printMessage(typeReceived,colors.green,exitMessage)
  217.               break
  218.             else
  219.             -- if connected computer responds then it will break the while loop that connects with the connected computer.
  220.  
  221.               printMessage(typeError,colors.red,'No message received. Do you want to forcibly close this connection?')
  222.               printMessage(typeInfoMSG,colors.blue,'Type "Yes" or leave blank to keep the connection.')
  223.               prefix("exit/Force: ")
  224.               answer = read()
  225.               log("[" .. typeInput .. ']"' .. prefixLog .. answer .. '"')
  226.               -- if connected computer does not respond then you can forcibly close the connection by typing "Yes" with or without capitals or leave blank to stay connected
  227.  
  228.               if  answer:lower() == "yes" or answer:lower() == "y" then
  229.  
  230.                 break
  231.               end
  232.             end
  233.             -- Disconnects from connected computer
  234.  
  235.           elseif commandTable[1] == "upload" and status.upload == 1 then
  236.             if commandTable[2] ~= nil and commandTable[3] ~= nil then
  237.               if fs.exists(commandTable[2]) == true and fs.isDir(commandTable[2]) == false then
  238.  
  239.                 rednet.send(connectID, commandTable, mProt)
  240.                 local _, output = rednet.receive(mProt, 0.1)
  241.                 if output == "send" then
  242.  
  243.                   local fileOpen = fs.open(commandTable[2], 'r')
  244.                   local fileSend = fileOpen.readAll()
  245.                   fileOpen.close()
  246.                   rednet.send(connectID, fileSend, mProt)
  247.                   printMessage(typeReceived,colors.green,"Successfully uploaded file.")
  248.                   -- If everything goes right when using the upload function it will send the file to connected computer
  249.                 elseif output == nil then
  250.  
  251.                   printMessage(typeError,colors.red,"Lost connection.")
  252.                 else
  253.  
  254.                   printMessage(typeReceived,colors.green,output)
  255.                 end
  256.               else
  257.  
  258.                 printMessage(typeError,colors.red,"File does not exist on local computer.")
  259.               end
  260.             else
  261.  
  262.               printMessage(typeError,colors.red,'Invalid syntax. Correct syntax is - "upload [file to upload] [name of file to upload as]"')
  263.             end
  264.             -- Various different error messages in case something went wrong while using the upload function
  265.  
  266.           elseif commandTable[1] == "download" and status.download == 1 then
  267.             if commandTable[2] ~= nil and commandTable[3] ~= nil then
  268.               if fs.exists(commandTable[3]) == false then
  269.  
  270.                 rednet.send(connectID, commandTable, mProt)
  271.                 local _, fileOutput = rednet.receive(mProt, 0.1)
  272.  
  273.                 if fileOutput[1] then
  274.  
  275.                   fileOpen = fs.open(commandTable[3], "w")
  276.                   fileOpen.write(fileOutput[1])
  277.                   fileOpen.close()
  278.                   printMessage(typeReceived,colors.green,fileOutput[2])
  279.                   -- If everything goes right when using the download function it will receive a file from connected computer
  280.                 else
  281.  
  282.                   printMessage(typeReceived,colors.green,fileOutput)
  283.                 end
  284.               else
  285.  
  286.                 printMessage(typeError,colors.red,"File already exists on local computer.")
  287.               end
  288.             else
  289.  
  290.               printMessage(typeError,colors.red,'Invalid syntax. Correct syntax is - "download [file to download] [file name to download as]"')
  291.             end
  292.             -- Various different error messages in case something went wrong while using the download function
  293.  
  294.           elseif commandTable[1] == "reconnect" and disconnected then
  295.  
  296.             rednet.send(connectID, "connect", mProt)
  297.             local _, connectMessage = rednet.receive(mProt,0.1)
  298.  
  299.             if connectMessage then
  300.               -- If the connected computer replies then this will catch it
  301.  
  302.               if connectMessage[1] ~= nil then
  303.  
  304.                 printMessage(typeReceived,colors.green,connectMessage[1])
  305.                 status = connectMessage[2]
  306.               else
  307.  
  308.                 printMessage(typeReceived,colors.green,connectMessage)
  309.               end
  310.               -- Displaying the connection message
  311.  
  312.               disconnected = false
  313.             else
  314.               -- If you can't reconnect then this will catch it
  315.  
  316.               printMessage(typeError, colors.red, "Unable to reconnect.")
  317.             end
  318.  
  319.           else
  320.             rednet.send(connectID, commandTable, mProt)
  321.             local ID, message = rednet.receive(mProt, 0.1)
  322.  
  323.             -- Sends command through if anything before hasn't caught it
  324.             if message ~= nil and #message ~= 0 then
  325.               if message[1] == nil then
  326.                 printMessage(typeReceived,colors.green,message)
  327.  
  328.               else
  329.                 printMessage(typeReceived,colors.green,message[1])
  330.  
  331.               end
  332.  
  333.               disconnected = false
  334.             else
  335.               printMessage(typeError,colors.red,"No message received.")
  336.               disconnected = true
  337.  
  338.               if commandTable[1] == "help" then
  339.  
  340.                 printMessage(typeInfoMSG,colors.blue,"Available unconnected commands are:")
  341.                 printMessage(typeInfoMSG,colors.lightBlue,"reconnect - reconnect to computer if connection has been lost")
  342.                 printMessage(typeInfoMSG,colors.blue,"exit - breaks the connection between computers")
  343.               else
  344.                
  345.                 printMessage(typeInfoMSG,colors.blue,'Type "help" for options.')
  346.               end
  347.             end
  348.             -- Displays message received from connected computer after command has gone through
  349.           end
  350.         end
  351.       end
  352.     end
  353.  
  354.   else
  355.     printMessage(typeError,colors.red,'Invalid command. Type "help" for options.')
  356.   end
  357.   -- Before connection occurs this is the default route for anything that's not recognised
  358. end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement