Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- -- GLOBALS
- local master = -1
- local side = "top"
- local myID = os.getComputerID()
- local w, h = term.getSize()
- local mails = {}
- -- Functions & Stuff
- function addLeadingZero(amount, int)
- return string.format("%"..string.rep("0", amount).."2d", tonumber(int))
- end
- function writeToFile(data, filename)
- local file = fs.open(filename, "w")
- file.write(textutils.serialise(data))
- file.close()
- end
- function readFromFile(filename)
- if not fs.exists(filename) then
- return
- end
- local file = fs.open(filename, "r")
- local data = file.readAll()
- file.close()
- return data
- end
- function split(str, pat)
- local t = { }
- local fpat = "(.-)"..pat
- local last_end = 1
- local s, e, cap = str:find(fpat, 1)
- while s do
- if s ~= 1 or cap ~= "" then
- table.insert(t,cap)
- end
- last_end = e+1
- s, e, cap = str:find(fpat, last_end)
- end
- if last_end <= #str then
- cap = str:sub(last_end)
- table.insert(t, cap)
- end
- return t
- end
- function printHeader()
- term.setCursorPos(1, 1)
- print(string.rep("-", w))
- term.setCursorPos(w/2 - #"MAIL SERVER"/2, 2)
- print("MAIL SERVER")
- term.setCursorPos(1, 3)
- print(string.rep("-", w))
- print("")
- end
- function addMail(mailData)
- table.insert(mails, {
- id = mailData[1],
- from = mailData[2],
- date = mailData[3].."."..mailData[4].."."..mailData[5],
- time = mailData[6]..":"..addLeadingZero(1, mailData[7]),
- body = mailData[8]
- })
- writeToFile(mails, "mailDB")
- end
- function requestMails(id)
- local foundMails = ""
- if #mails > 0 then
- for i, mail in pairs(mails) do
- if mail["id"] == id then
- foundMails = foundMails.."!NM!"
- foundMails = foundMails.."!ED!"..mail["id"].."!ED!"..mail["from"].."!ED!"..mail["date"].."!ED!"..mail["time"].."!ED!"..mail["body"].."!ED!"
- table.remove(mails, i)
- end
- end
- end
- local resp = "$CLIENT-"..id.."-"
- resp = resp.."$RESPONSE"..foundMails
- print(resp)
- rednet.send(master, resp)
- writeToFile(mails, "mailDB")
- end
- function processRequest(request, senderId)
- if string.find(request, "$EMAIL") then
- request = string.gsub(request, "$EMAIL", "")
- local values = split(request, "!ED!")
- if #values == 8 then
- addMail(values)
- end
- elseif string.find(request, "$REQUEST") then
- request = string.gsub(request, "$REQUEST", "")
- local values = split(request, "!ED!")
- if #values >= 2 and tonumber(values[1]) then
- if values[2] == "EMAILS" then
- requestMails(values[1])
- end
- end
- end
- end
- -- Connection Logic
- mails = readFromFile("mailDB") ~= nil and textutils.unserialise(readFromFile("mailDB")) or {}
- if not fs.exists("gravimail/server/pref") then
- error("GraviMail server not installed via installer, make sure to do that!")
- return
- end
- local pref = fs.open("gravimail/server/pref", "r").readAll()
- pref = textutils.unserialise(pref)
- side = pref.side
- master = tonumber(pref.master)
- if master == -1 then
- print("Bad ID!")
- return
- end
- term.clear()
- printHeader()
- rednet.open(side)
- local id, message
- while true do
- print("Requesting admission on "..master)
- rednet.send(master, "$MASTER$ADDSERVER-"..myID.."-MAIL")
- id, message = rednet.receive(5)
- if message == "$ACK" then
- print("Confirmed.")
- break
- end
- end
- while true do
- local cx, cy = term.getCursorPos()
- printHeader()
- term.setCursorPos(cx, cy)
- print("Listening for messages...")
- id, message = rednet.receive()
- processRequest(message, id)
- end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement