Advertisement
Noobyhead99

sugarlanmerDepositClient

Oct 17th, 2013
102
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 7.43 KB | None | 0 0
  1. local tArgs = { ... }
  2.  
  3. local ID
  4. local PIN
  5. local settings
  6. local modem
  7. local drive
  8. local lan
  9. local accelerator
  10.  
  11. local function setup()
  12.   modem.closeAll()
  13.   write("Transmitting modem channel (get this from the ATM server admin) [Default: " .. settings["channel"]["transmit"] .. "]: ")
  14.   settings["channel"]["transmit"] = tonumber(read()) or settings["channel"]["transmit"]
  15.   write("Receiving modem channel (this can be whatever you want) [Default: " .. settings["channel"]["receive"] .. "]: ")
  16.   settings["channel"]["receive"] = tonumber(read()) or settings["channel"]["receive"]
  17.   modem.open(settings["channel"]["transmit"])
  18.   modem.open(settings["channel"]["receive"])
  19.   write("Use cryptographic accelerator if attached [Default: " .. tostring(settings["crypto"]["enabled"]) .. "]: ")
  20.  
  21.   local function setCryptoTrue()
  22.     print("Contacting server for public key (timeout: 5 seconds)...")
  23.     modem.transmit(settings["channel"]["transmit"],settings["channel"]["receive"],{0,os.getComputerID()})
  24.     local setupTimer = os.startTimer(5)
  25.     local eventData
  26.     repeat
  27.       eventData = { os.pullEvent() }
  28.     until ( eventData[1] == "modem_message" and eventData[3] == settings["channel"]["receive"] and eventData[5][1] == 0  and eventData[5][2] == os.getComputerID() ) or ( eventData[1] == "timer" and eventData[2] == setupTimer )
  29.     if eventData[1] == "modem_message" then
  30.       if eventData[5][3] == 0 then
  31.         settings["crypto"]["key"] = eventData[5][4]
  32.         settings["crypto"]["enabled"] = true
  33.         print("Public key received and saved. Cryptographic accelerator support is now enabled.")
  34.       elseif eventData[5][3] == 1 then
  35.         print("The server has reported that it does not support encryption, and no public key was received. Cryptographic accelerator support will now be disabled.")
  36.         settings["crypto"]["enabled"] = false
  37.       else
  38.         print("An unexpected response was received from the server (" .. textutils.serialize(eventData[5]) .. "), and no public key was received. Cryptographic accelerator support will now be disabled.")
  39.         settings["crypto"]["enabled"] = false
  40.       end
  41.     else
  42.       print("The request from the server timed out. No public key was received, and cryptographic accelerator support will now be disabled.")
  43.       settings["crypto"]["enabled"] = false
  44.     end
  45.   end
  46.  
  47.   local function setCryptoFalse()
  48.     settings["crypto"]["enabled"] = false
  49.   end
  50.  
  51.   local cryptoFunctionLookup = {
  52.     ["true"] = setCryptoTrue,
  53.     ["false"] = setCryptoFalse
  54.   }
  55.  
  56.   local selection = read()
  57.   if cryptoFunctionLookup[selection] then
  58.     cryptoFunctionLookup[selection]()
  59.   else
  60.     cryptoFunctionLookup[settings["crypto"]["enabled"]]()
  61.   end
  62.   local fileHandle = io.open(".accountRegistrar_settings","w")
  63.   fileHandle:write(textutils.serialize(settings))
  64.   fileHandle:close()
  65.   print("Configuration complete. Press any key to continue.")
  66.   os.pullEvent("key")
  67.   sleep(0)
  68. end
  69.  
  70. for k,v in pairs(peripheral.getNames()) do
  71.   if peripheral.getType(v) == "modem" then
  72.     modem = peripheral.wrap(v)
  73.   elseif peripheral.getType(v) == "drive" then
  74.     drive = v
  75.   elseif peripheral.getType(v) == "LAN NIC" then
  76.     lan = peripheral.wrap(v)
  77.   elseif peripheral.getType(v) == "cryptographic accelerator" then
  78.     accelerator = peripheral.wrap(v)
  79.   end
  80. end
  81.  
  82. if not modem or not lan then
  83.   print("Please attach a wireless and LAN modem, and restart the program.")
  84.   return
  85. end
  86.  
  87. if not fs.exists(".accountRegistrar_settings") then
  88.   settings = {}
  89.   settings["channel"] = {["transmit"]=1,["receive"]=2}
  90.   settings["crypto"] = {["enabled"]=true}
  91.   term.clear()
  92.   term.setCursorPos(1,1)
  93.   print("First time setup.")
  94.   print("To modify these values later, use 'settings' as an argument for this program.")
  95.   setup()
  96. else
  97.   local fileHandle = io.open(".accountRegistrar_settings","r")
  98.   settings = textutils.unserialize(fileHandle:read())
  99.   fileHandle:close()
  100.   if tArgs[1] == "settings" then
  101.     term.clear()
  102.     term.setCursorPos(1,1)
  103.     setup()
  104.   end
  105.   modem.open(settings["channel"]["transmit"])
  106.   modem.open(settings["channel"]["receive"])
  107. end
  108.  
  109. if not drive or not disk.getLabel(drive) then
  110.   term.clear()
  111.   term.setCursorPos(1,1)
  112.   print("No drive containing valid labeled card found.")
  113.   write("Enter ID: ")
  114.   repeat
  115.     ID = read()
  116.   until #ID > 0
  117. else
  118.   ID = disk.getLabel(drive)
  119. end
  120.  
  121. term.clear()
  122. term.setCursorPos(1,1)
  123. print("Bank Deposit Terminal")
  124. if not ( settings["crypto"]["enabled"] and accelerator ) then
  125.   print("NOTE: This terminal is not using a secure connection! Do not enter any personal information unless you accept and understand the risks.")
  126. end
  127. print("Hello " .. ID)
  128. write("Enter PIN: ")
  129. local PIN = read("*")
  130. if settings["crypto"]["enabled"] and accelerator then
  131.   local key = accelerator.decodeKey("RSA",settings["crypto"]["key"])
  132.   ID = key.encrypt("RSA",ID)
  133.   PIN = key.encrypt("RSA",PIN)
  134. end
  135. print("Contacting collection turtle (timeout: 5 seconds)...")
  136. local turtleTimer = os.startTimer(5)
  137. local receivedResponse = false
  138. lan.send(textutils.serialize({0}))
  139. local eventData
  140. repeat
  141.   local posx, posy
  142.   eventData = { os.pullEvent() }
  143.   if eventData[1] == "lan_message" and textutils.unserialize(eventData[5])[1] == 1 then
  144.     print("Please throw ingots into hopper and press Return to continue")
  145.     print("NOTE: If any ingots are thrown into the hopper when the transaction has completed, no funds will be added!")
  146.     receivedResponse = true
  147.     posx, posy = term.getCursorPos()
  148.   elseif eventData[1] == "lan_message" and textutils.unseralize(eventData[5])[1] == 2 then
  149.     term.setCursorPos(posx,posy)
  150.     print("Current funds added: ")
  151.     print("MC Dollars: " .. textutils.unseralize(eventData[5])[2]*100)
  152.     print("Gold ingots: " .. textutils.unseralize(eventData[5])[2])
  153.   end
  154. until ( eventData[1] == "timer" and eventData[2] == turtleTimer and not receivedResponse ) or ( eventData[1] == "key" and eventData[2] == 28 )
  155. if eventData[1] == "timer" then
  156.   print("The collection turtle did not reply. The program will now terminate.")
  157.   return
  158. end
  159. print("Contacting collection turtle for final value (timeout: 5 seconds)...")
  160. local finalValueTimer = os.startTimer(5)
  161. lan.send(textutils.serialize({3}))
  162. repeat
  163.   eventData = os.pullEvent()
  164. until ( eventData[1] == "timer" and eventData[2] == finalValueTimer ) or ( eventData[1] == "lan_message" and textutils.unseralize(eventData[5])[1] == 4 )
  165. if eventData[1] == "timer" then
  166.   print("The collection turtle did not reply. The program will now terminate.")
  167.   return
  168. end
  169. print("Contacting server (timeout: 5 seconds)...")
  170. modem.transmit(settings["wireless"]["transmit"],settings["wireless"]["receive"],{2,os.getComputerID(),settings["crypto"]["enabled"],ID,PIN,textutils.unserialize(eventData[5])[2]})
  171. local depositTimer = os.startTimer(5)
  172. repeat
  173.   eventData = { os.pullEvent() }
  174. until ( eventData[1] == "modem_message" and eventData[3] == settings["channel"]["receive"] and eventData[5][1] == 2 and eventData[5][2] == os.getComputerID()) or ( eventData[1] == "timer" and eventData[2] == depositTimer )
  175. if eventData[1] == "modem_message" then
  176.   if eventData[5][3] == 0 then
  177.     print("The deposit was completed successfully.")
  178.   else
  179.     print("An unexpected response was received from the server (" .. textutils.serialize(eventData[5]) .. "). The deposit was likely not successful.")
  180.   end
  181. else
  182.   print("The request from the server timed out. The deposit likely did not go through.")
  183. end
  184.  
  185. modem.closeAll()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement