Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- 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 chkfiles()
- if fs.exists("/services") then
- if fs.exists("/users") then
- if fs.exists("/mail") then
- return true
- else
- mail = fs.open("/mail", "w")
- end
- else
- shell.run("pastebin get SQvMcfK0 users")
- end
- else
- shell.run("pastebin get SQvMcfK0 services")
- end
- end
- local function readLines(filename)
- local ctr = 0
- for a in io.lines(filename) do
- ctr = ctr + 1
- end
- return ctr
- end
- function main()
- openRednet()
- local services = fs.open("/services", "r");
- local userFile = fs.open("/users", "r");
- local mailFile = fs.open("/mail", "r");
- local userID = {};
- local mailDatabase = {};
- local userCount = readLines("users")
- local serviceCount = readLines("services")
- local serviceName = {};
- local serviceAddress = {};
- local serverName = "JimNet Central Server";
- --for i = 0, serviceCount - 1 do
- --serviceName[i] = services.readLine();
- --serviceAddress[i] = services.readLine();
- --print ("Loaded Service. NAME:" .. serviceName[i] .. ", ADDR:" .. serviceAddress[i]);
- --end
- -- Disabled Services, I see no use for it at the moment.
- for i = 0, userCount - 1 do
- userID = userFile.readLine();
- mailDatabase[i] = {};
- mailCount = readLines("mail")
- for j = 0, mailCount - 1 do
- mailDatabase[i][j] = mailFile.readLine();
- end
- print ("Loaded Mail. ID:" .. userID .. ", MAILCOUNT: " .. mailCount);
- end
- sleep(1);
- 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();
- print ("Incoming Message. ID:" .. id .. ", MSG:" .. msg);
- if (msg == "$REGISTER") then
- for i = 0, userCount-1 do
- if (userID[i] == id) then
- rednet.send(id, "$FAILED");
- print ("Terminal #" .. id .. " registration failed.");
- else
- userID[userCount] = id;
- userCount = count + 1;
- print ("Terminal #" .. id .. " joined the network.");
- term.setTextColor(colors.red);
- print ("Updating Databases...");
- end
- end
- end
- if (msg == "$SERVICES") then
- rednet.send(id, tostring(serviceCount));
- for i = 0, serviceCount - 1 do
- rednet.send(id, serviceName[i]);
- rednet.send(id, serviceAddress[i]);
- end
- end
- if (msg == "$INFO") then
- rednet.send(id, serverName);
- end
- end
- end
- chkfiles()
- main()
Advertisement
Add Comment
Please, Sign In to add comment