Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- --[[
- _____ _____ _______
- | __ \| __ \ /\|__ __|
- | |__) | | | | / \ | | ___ _ __ _ __ ___
- | ___/| | | |/ /\ \ | |/ _ \ '__| '_ ` _ \
- | | | |__| / ____ \| | __/ | | | | | | |
- |_| |_____/_/ \_\_|\___|_| |_| |_| |_|
- --]]
- --Variables
- --Initializes the info files
- local servFile = fs.open("/services", "r")
- local userFile = fs.open("/users", "r")
- local phoneNumFile = fs.open("/pnums", "r")
- local mailFile = fs.open("/mail", "r")
- local planFile = fs.open("/plans", "r")
- local plandb = {}
- local userID = {}
- local phoneNumbers = {}
- local mailDatabase = {}
- local services = {}
- local logFile = fs.open("/logs", "a")
- local serverName = "MCNET";
- local function log(msgType, message)
- print ("[" .. msgType .. "]\t" .. message);
- logFile.writeLine("[" .. msgType .. "]\t" .. message)
- end
- local function loadUserInfo()
- log("INFO", "Loading user info...")
- totalUserCount = userFile.readLine()
- log("INFO", "Detected " .. totalUserCount .. " user files. ")
- for line2 = 1, totalUserCount do
- curUserID = userFile.readLine()
- curUserPhone = phoneNumFile.readLine()
- curUserPlan = planFile.readLine()
- table.insert(userID, curUserId)
- table.insert(phoneNumbers, curUserPhone)
- table.insert(plandb, curUserPlan)
- log("INFO", "Loaded user id " .. curUserID)
- end
- end
- local function writeUserInfo()
- log("INFO", "Writing user info...")
- --Clears the data file and write the data in the array
- --to the data file
- end
- local function loadServices()
- --TODO: 69
- end
- --Creates and verifies a new phone number
- local function generatePhoneNum()
- math.randomseed(os.time())
- while true do
- number = math.random(1000, 9999)
- --indicates if the current generated number is used
- flag = false;
- for i = 1, table.getn(phoneNumbers) do
- if (phoneNumbers[i] == number) then
- flag = true
- end
- end
- if (not flag) then
- return number
- end
- end
- end
- --Opens rednet on the first side
- local function openRednet()
- local listOfSides = rs.getSides()
- local listofPossibles = {}
- c1 = 0
- for c1 = 1,6 do
- if peripheral.isPresent(tostring(listOfSides[c1])) and peripheral.getType(listOfSides[c1]) == "modem" then
- table.insert(listofPossibles,tostring(listOfSides[c1]))
- end
- if c1 == 6 and table.maxn(listofPossibles) == 0 then
- print("No Modem Detected!")
- return nil
- end
- if c1 == 6 and table.maxn(listofPossibles) ~= 0 then
- rednet.open(listofPossibles[1])
- return listofPossibles[1]
- end
- end
- end
- local function userExists(curUserId)
- for i = 1, table.getn(userID) do
- if (userID[i] == curUserId) then
- return true
- end
- end
- return false
- end
- local function getUserInfo(user)
- if (not userExists(user)) then
- return nil
- end
- for i = 1, table.getn(userID) do
- if (userID[i] == user) then
- return {userID[i], phoneNumbers[i]}
- end
- end
- return nil
- end
- local function createUser()
- local newUserID = table.getn(userID) + 1
- local newPhoneNumber = generatePhoneNum
- table.insert(userID, newUserID)
- table.insert(phoneNumbers, newPhoneNumber)
- tmpUser = {newUserID, newPhoneNumber}
- end
- -- Function to check if all files exist
- local function chkfiles()
- log("INFO", "Checking files...")
- if (not fs.exists("/services")) then
- log("WARN", "/services not found. downloading.")
- shell.run("pastebin get SQvMcfK0 services")
- end
- if (not fs.exists("/users")) then
- log("WARN", "/users not found. downloading.")
- shell.run("pastebin get SQvMcfK0 users")
- end
- if (not fs.exists("/mail")) then
- log("WARN", "/mail not found. downloading.")
- shell.run("pastebin get SQvMcfK0 mail")
- end
- if(not fs.exists("/userids")) then
- log("WARN", "/userids not found. downloading.")
- shell.run("pastebin get SQvMcfK0 userids")
- end
- if (not fs.exists("/pnums")) then
- log("WARN", "/pnums not found. downloading.")
- shell.run("pastebin get SQvMcfK0 pnums")
- end
- return true
- end
- function main()
- --readUserFiles()
- term.clear();
- term.setCursorPos(1, 1);
- term.setTextColor(colors.red);
- print (" Phone Carrier Server\n");
- term.setTextColor(colors.white);
- while true do
- print ("\n\nListening for Requests...");
- id, msg = rednet.receive()
- if (msg == "$SERVICES") then
- --TODO: Handle services
- end
- if (msg == "$REGISTER") then
- if (not userExists(id)) then
- local newUser = tmpUser
- rednet.send(id, textutils.serialize(newUser))
- else
- rednet.send(id, "User already registered. ")
- end
- end
- if (msg == "$INFO") then
- rednet.send(id, "$SERVERID " .. serverName)
- end
- if (msg == "$GETC") then
- if plandb[id] == "1" then
- txtlim = 100
- weblim = 100
- end
- if plandb[id] == "2" then
- txtlim = 2000
- weblim = 100000
- end
- sendpnum = phoneNumbers[id]
- sendr = { txtlim, weblim, sendpnum }
- rednet.send(id, textutils.serialize(sendr))
- rednet.send(id, serverName)
- end
- end
- end
- chkfiles()
- openRednet()
- main()
Advertisement
Add Comment
Please, Sign In to add comment