Advertisement
Guest User

Terminal.exe

a guest
May 26th, 2015
269
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 1.91 KB | None | 0 0
  1. os.loadAPI("Apnet")
  2. Apnet.Initialize("Terminal", "false")
  3.  
  4. local function getTokens(str)
  5.  local result = {}
  6.  local i = 0
  7.  for token in  string.gmatch(str, "[a-zA-Z0-9*]+") do
  8.   i = i + 1
  9.   result[i] = token
  10.  end
  11.  result.Count = i
  12.  return result
  13. end
  14.  
  15. local function handleDetails(tokens)
  16.  local details = Apnet.GetComputerDetails()
  17.  print("ID: " .. details.ComputerId)
  18.  print("Label: " .. details.ComputerLabel)
  19.  print("Type: " .. details.ComputerType)
  20. end
  21.  
  22. local function handleNext(tokens)
  23.  local msg = Apnet.GetNextInboundMessage()
  24.  if (msg == nil) then
  25.   print("nil")
  26.  elseif (tokens.Count == 1) then
  27.   print(msg.Content)
  28.  elseif (tokens[2] == "details") then
  29.   print(textutils.serialize(msg))
  30.  end
  31. end
  32.  
  33. local function handleSend(tokens)
  34.  if (tokens.Count < 6) then
  35.   print("Not enough arguments")
  36.  else
  37.   local msg = Apnet.NewMessage()
  38.   if (tokens[2] ~= "*") then
  39.    msg.Destination.ComputerId = tonumber(tokens[2])
  40.   else
  41.    msg.Destination.ComputerId = tokens[2]
  42.   end
  43.   msg.Destination.ComputerLabel = tokens[3]
  44.   msg.Destination.ComputerType = tokens[4]
  45.   msg.MessageType = tokens[5]
  46.   msg.MessageSubType = tokens[6]
  47.   if (tokens.Count > 6) then
  48.    msg.Content = tokens[7]
  49.    if (tokens.Count > 7) then
  50.     for i = 8, tokens.Count do
  51.      msg.Content = msg.Content .. " " .. tokens[i]
  52.     end
  53.    end
  54.   end
  55.    
  56.   Apnet.Send(msg)
  57.  end
  58. end
  59.  
  60. local function processInput(tokens)
  61.  if (tokens[1] == "details") then
  62.   handleDetails(tokens)
  63.  elseif (tokens[1] == "next") then
  64.   handleNext(tokens)
  65.  elseif (tokens[1] == "send") then
  66.   handleSend(tokens)
  67.  elseif (tokens[1] ~= "exit") then
  68.   print("Unrecognized command")
  69.  end
  70. end
  71.  
  72. local function inputLoop()
  73.  local string input = "default"
  74.  
  75.  while (input ~= "exit") do
  76.   print("Enter a command")
  77.   input = read()
  78.   local tokens = getTokens(input)
  79.   processInput(tokens)
  80.  end
  81. end
  82.  
  83. parallel.waitForAny(Apnet.Listen, inputLoop)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement