Advertisement
gravitowl

GraviMail Server

Mar 9th, 2021 (edited)
70
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.77 KB | None | 0 0
  1. -- GLOBALS
  2. local master = -1
  3. local side = "top"
  4. local myID = os.getComputerID()
  5. local w, h = term.getSize()
  6.  
  7. local mails = {}
  8.  
  9. -- Functions & Stuff
  10. function addLeadingZero(amount, int)
  11. return string.format("%"..string.rep("0", amount).."2d", tonumber(int))
  12. end
  13.  
  14. function writeToFile(data, filename)
  15. local file = fs.open(filename, "w")
  16. file.write(textutils.serialise(data))
  17. file.close()
  18. end
  19.  
  20. function readFromFile(filename)
  21. if not fs.exists(filename) then
  22. return
  23. end
  24. local file = fs.open(filename, "r")
  25. local data = file.readAll()
  26. file.close()
  27. return data
  28. end
  29.  
  30. function split(str, pat)
  31. local t = { }
  32. local fpat = "(.-)"..pat
  33. local last_end = 1
  34. local s, e, cap = str:find(fpat, 1)
  35. while s do
  36. if s ~= 1 or cap ~= "" then
  37. table.insert(t,cap)
  38. end
  39. last_end = e+1
  40. s, e, cap = str:find(fpat, last_end)
  41. end
  42. if last_end <= #str then
  43. cap = str:sub(last_end)
  44. table.insert(t, cap)
  45. end
  46. return t
  47.  
  48. end
  49.  
  50. function printHeader()
  51. term.setCursorPos(1, 1)
  52. print(string.rep("-", w))
  53. term.setCursorPos(w/2 - #"MAIL SERVER"/2, 2)
  54. print("MAIL SERVER")
  55. term.setCursorPos(1, 3)
  56. print(string.rep("-", w))
  57. print("")
  58. end
  59.  
  60. function addMail(mailData)
  61. table.insert(mails, {
  62. id = mailData[1],
  63. from = mailData[2],
  64. date = mailData[3].."."..mailData[4].."."..mailData[5],
  65. time = mailData[6]..":"..addLeadingZero(1, mailData[7]),
  66. body = mailData[8]
  67. })
  68.  
  69. writeToFile(mails, "mailDB")
  70. end
  71.  
  72. function requestMails(id)
  73. local foundMails = ""
  74. if #mails > 0 then
  75. for i, mail in pairs(mails) do
  76. if mail["id"] == id then
  77. foundMails = foundMails.."!NM!"
  78. foundMails = foundMails.."!ED!"..mail["id"].."!ED!"..mail["from"].."!ED!"..mail["date"].."!ED!"..mail["time"].."!ED!"..mail["body"].."!ED!"
  79. table.remove(mails, i)
  80. end
  81. end
  82. end
  83. local resp = "$CLIENT-"..id.."-"
  84. resp = resp.."$RESPONSE"..foundMails
  85. print(resp)
  86. rednet.send(master, resp)
  87. writeToFile(mails, "mailDB")
  88. end
  89.  
  90. function processRequest(request, senderId)
  91. if string.find(request, "$EMAIL") then
  92. request = string.gsub(request, "$EMAIL", "")
  93. local values = split(request, "!ED!")
  94. if #values == 8 then
  95. addMail(values)
  96. end
  97. elseif string.find(request, "$REQUEST") then
  98. request = string.gsub(request, "$REQUEST", "")
  99. local values = split(request, "!ED!")
  100. if #values >= 2 and tonumber(values[1]) then
  101. if values[2] == "EMAILS" then
  102. requestMails(values[1])
  103. end
  104. end
  105. end
  106. end
  107.  
  108. -- Connection Logic
  109.  
  110. mails = readFromFile("mailDB") ~= nil and textutils.unserialise(readFromFile("mailDB")) or {}
  111.  
  112. if not fs.exists("gravimail/server/pref") then
  113. error("GraviMail server not installed via installer, make sure to do that!")
  114. return
  115. end
  116.  
  117. local pref = fs.open("gravimail/server/pref", "r").readAll()
  118. pref = textutils.unserialise(pref)
  119.  
  120. side = pref.side
  121. master = tonumber(pref.master)
  122.  
  123. if master == -1 then
  124. print("Bad ID!")
  125. return
  126. end
  127.  
  128. term.clear()
  129. printHeader()
  130.  
  131. rednet.open(side)
  132.  
  133. local id, message
  134. while true do
  135. print("Requesting admission on "..master)
  136. rednet.send(master, "$MASTER$ADDSERVER-"..myID.."-MAIL")
  137. id, message = rednet.receive(5)
  138. if message == "$ACK" then
  139. print("Confirmed.")
  140. break
  141. end
  142. end
  143.  
  144. while true do
  145. local cx, cy = term.getCursorPos()
  146. printHeader()
  147. term.setCursorPos(cx, cy)
  148. print("Listening for messages...")
  149. id, message = rednet.receive()
  150. processRequest(message, id)
  151. end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement