BendymelonCoding

PDATerm Server V0.2

Nov 29th, 2015
1,095
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 5.46 KB | None | 0 0
  1. --[[
  2.   _____  _____       _______                  
  3.  |  __ \|  __ \   /\|__   __|                
  4.  | |__) | |  | | /  \  | | ___ _ __ _ __ ___  
  5.  |  ___/| |  | |/ /\ \ | |/ _ \ '__| '_ ` _ \
  6.  | |    | |__| / ____ \| |  __/ |  | | | | | |
  7.  |_|    |_____/_/    \_\_|\___|_|  |_| |_| |_|
  8.  
  9. --]]
  10.  
  11. --Variables
  12. --Initializes the info files
  13. local servFile = fs.open("/services", "r")
  14. local userFile = fs.open("/users", "r")
  15. local phoneNumFile = fs.open("/pnums", "r")
  16. local mailFile = fs.open("/mail", "r")
  17. local planFile = fs.open("/plans", "r")
  18.  
  19. local plandb = {}
  20. local userID = {}
  21. local phoneNumbers = {}
  22. local mailDatabase = {}
  23. local services = {}
  24.  
  25. local logFile = fs.open("/logs", "a")
  26.  
  27. local serverName = "MCNET";
  28.  
  29.  
  30. local function log(msgType, message)
  31.     print ("[" .. msgType .. "]\t" .. message);
  32.     logFile.writeLine("[" .. msgType .. "]\t" .. message)
  33. end
  34.  
  35. local function loadUserInfo()
  36.     log("INFO", "Loading user info...")
  37.  
  38.     totalUserCount = userFile.readLine()
  39.     log("INFO", "Detected " .. totalUserCount .. " user files. ")
  40.    
  41.     for line2 = 1, totalUserCount do
  42.         curUserID = userFile.readLine()
  43.         curUserPhone = phoneNumFile.readLine()
  44.         curUserPlan = planFile.readLine()
  45.  
  46.         table.insert(userID, curUserId)
  47.         table.insert(phoneNumbers, curUserPhone)
  48.         table.insert(plandb, curUserPlan)
  49.  
  50.         log("INFO", "Loaded user id " .. curUserID)
  51.     end
  52. end
  53.  
  54. local function writeUserInfo()
  55.     log("INFO", "Writing user info...")
  56.  
  57.     --Clears the data file and write the data in the array
  58.     --to the data file
  59. end
  60.  
  61. local function loadServices()
  62.     --TODO: 69
  63. end
  64.  
  65. --Creates and verifies a new phone number
  66. local function generatePhoneNum()
  67.     math.randomseed(os.time())
  68.  
  69.     while true do
  70.         number = math.random(1000, 9999)
  71.         --indicates if the current generated number is used
  72.         flag = false;
  73.  
  74.         for i = 1, table.getn(phoneNumbers) do
  75.             if (phoneNumbers[i] == number) then
  76.                 flag = true
  77.             end
  78.         end
  79.  
  80.         if (not flag) then
  81.             return number
  82.         end
  83.     end
  84. end
  85.  
  86. --Opens rednet on the first side
  87. local function openRednet()
  88.     local listOfSides = rs.getSides()
  89.     local listofPossibles = {}
  90.     c1 = 0
  91.     for c1 = 1,6 do
  92.         if peripheral.isPresent(tostring(listOfSides[c1])) and peripheral.getType(listOfSides[c1]) == "modem" then
  93.             table.insert(listofPossibles,tostring(listOfSides[c1]))
  94.         end
  95.  
  96.         if c1 == 6 and table.maxn(listofPossibles) == 0 then
  97.             print("No Modem Detected!")
  98.             return nil
  99.         end
  100.  
  101.         if c1 == 6 and table.maxn(listofPossibles) ~= 0 then
  102.             rednet.open(listofPossibles[1])
  103.             return listofPossibles[1]
  104.         end
  105.     end
  106. end
  107.  
  108. local function userExists(curUserId)
  109.     for i = 1, table.getn(userID) do
  110.         if (userID[i] == curUserId) then
  111.             return true
  112.         end
  113.     end
  114.  
  115.     return false
  116. end
  117.  
  118. local function getUserInfo(user)
  119.     if (not userExists(user)) then
  120.         return nil
  121.     end
  122.  
  123.     for i = 1, table.getn(userID) do
  124.         if (userID[i] == user) then
  125.             return {userID[i], phoneNumbers[i]}
  126.         end
  127.     end
  128.  
  129.     return nil
  130. end
  131.  
  132. local function createUser()
  133.     local newUserID = table.getn(userID) + 1
  134.     local newPhoneNumber = generatePhoneNum
  135.    
  136.     table.insert(userID, newUserID)
  137.     table.insert(phoneNumbers, newPhoneNumber)
  138.    
  139.     tmpUser = {newUserID, newPhoneNumber}
  140. end
  141.  
  142. -- Function to check if all files exist
  143. local function chkfiles()
  144.     log("INFO", "Checking files...")
  145.  
  146.     if (not fs.exists("/services")) then
  147.         log("WARN", "/services not found. downloading.")
  148.         shell.run("pastebin get SQvMcfK0 services")
  149.     end
  150.  
  151.     if (not fs.exists("/users")) then
  152.         log("WARN", "/users not found. downloading.")
  153.         shell.run("pastebin get SQvMcfK0 users")
  154.     end
  155.  
  156.     if (not fs.exists("/mail")) then
  157.         log("WARN", "/mail not found. downloading.")
  158.         shell.run("pastebin get SQvMcfK0 mail")
  159.     end
  160.  
  161.     if(not fs.exists("/userids")) then
  162.         log("WARN", "/userids not found. downloading.")
  163.         shell.run("pastebin get SQvMcfK0 userids")
  164.     end
  165.  
  166.     if (not fs.exists("/pnums")) then
  167.         log("WARN", "/pnums not found. downloading.")
  168.         shell.run("pastebin get SQvMcfK0 pnums")
  169.     end
  170.  
  171.     return true
  172. end
  173.  
  174. function main()
  175.    
  176.     --readUserFiles()
  177.    
  178.     term.clear();
  179.     term.setCursorPos(1, 1);
  180.     term.setTextColor(colors.red);
  181.     print ("          Phone Carrier Server\n");
  182.     term.setTextColor(colors.white);
  183.    
  184.     while true do
  185.         print ("\n\nListening for Requests...");
  186.         id, msg = rednet.receive()
  187.  
  188.         if (msg == "$SERVICES") then
  189.             --TODO: Handle services
  190.         end
  191.  
  192.         if (msg == "$REGISTER") then
  193.             if (not userExists(id)) then
  194.                 local newUser = tmpUser
  195.                 rednet.send(id, textutils.serialize(newUser))
  196.             else
  197.                 rednet.send(id, "User already registered. ")
  198.             end
  199.  
  200.         end
  201.        
  202.         if (msg == "$INFO") then
  203.             rednet.send(id, "$SERVERID " .. serverName)
  204.         end
  205.        
  206.         if (msg == "$GETC") then
  207.             if plandb[id] == "1" then
  208.                 txtlim = 100
  209.                 weblim = 100
  210.             end
  211.             if plandb[id] == "2" then
  212.                 txtlim = 2000
  213.                 weblim = 100000
  214.             end
  215.             sendpnum = phoneNumbers[id]
  216.             sendr = { txtlim, weblim, sendpnum }
  217.             rednet.send(id, textutils.serialize(sendr))
  218.             rednet.send(id, serverName)
  219.         end
  220.     end
  221. end
  222. chkfiles()
  223. openRednet()
  224. main()
Advertisement
Add Comment
Please, Sign In to add comment