Advertisement
Guest User

mailserver

a guest
Oct 17th, 2015
131
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 3.04 KB | None | 0 0
  1. --MailServer, written by NitrogenFingers--
  2.  
  3. umail = {}
  4. side = "top"
  5.  
  6. function split(str, pat)
  7.   local t = { }
  8.   local fpat = "(.-)"..pat
  9.   local last_end = 1
  10.   local s, e, cap = str:find(fpat, 1)
  11.   while s do
  12.     if s ~= 1 or cap ~= "" then
  13.       table.insert(t,cap)
  14.     end
  15.     last_end = e+1
  16.     s, e, cap = str:find(fpat, last_end)
  17.   end
  18.   if last_end <= #str then
  19.     cap = str:sub(last_end)
  20.     table.insert(t, cap)
  21.   end
  22.   return t
  23. end
  24.  
  25. function writeToFile()
  26.   local file = io.open("archive", "w")
  27.   if file then
  28.     for i=1,#umail do
  29.       file:write("!SP!"..umail[i].ID.."!SP!"..
  30.           umail[i].from.."!SP!"..umail[i].time..
  31.           "!SP!"..umail[i].msg)
  32.     end
  33.     file:close()
  34.   end
  35. end
  36.  
  37. function readFromFile()
  38.   if(fs.exists("archive")) then
  39.     local file = io.open("archive", "r")
  40.     local fullstr = ""
  41.     local fline = file:read()
  42.     while fline do
  43.       fullstr=fullstr..fline
  44.       fline = file:read()
  45.     end
  46.     file:close()
  47.     local t = split(fullstr, "!SP!")
  48.  
  49.     for i=1,#t,4 do
  50.       table.insert(umail, {
  51.         ID = tonumber(t[i]),
  52.         from = t[i+1],
  53.         time = tonumber(t[i+2]),
  54.         msg = t[i+3]
  55.       })
  56.     end
  57.     print("Added "..#umail.." messages.")
  58.   end
  59. end
  60.  
  61. function dispatchRequest(sender)
  62.   resp="$RESPONSE"
  63.   count=0
  64.   mail=""
  65.   idx=1
  66.   while idx<table.getn(umail)+1 do
  67.     if umail[idx].ID==sender then
  68.       count=count+1
  69.       mail=mail.."!SP!"..umail[idx].from
  70.                .."!SP!"..umail[idx].time
  71.                .."!SP!"..umail[idx].msg.." "
  72.       table.remove(umail, idx)
  73.       idx=idx-1
  74.     end
  75.     idx=idx+1
  76.   end
  77.   resp=resp..mail
  78.   print("Sending Response:\n"..resp)
  79.   rednet.send(sender, resp)
  80.   writeToFile()
  81. end
  82.  
  83. function addMail(msg, sender)
  84.   print("Starting mail storage...")
  85.   msg = string.gsub(msg, "$EMAIL", "")
  86.   print("Header removed")
  87.   print(msg)
  88.   msgcpt = split(msg, "!SP!")
  89.   print("String split")
  90.   table.insert(umail, {
  91.     ID = tonumber(msgcpt[1]),
  92.     from = msgcpt[2],
  93.     time = tonumber(msgcpt[3]),
  94.     msg = msgcpt[4]
  95.   })
  96.   print("Message received- to "..msgcpt[1]..", from "
  97.       ..msgcpt[2]..", at "..msgcpt[3])  
  98.   rednet.send(sender, "$ACK")
  99.   writeToFile()
  100.   print("File archived")
  101. end
  102.  
  103. local tArgs = { ... }
  104. if #tArgs==0 then side = "bottom"
  105. else side = tArgs[1] end
  106.  
  107. print("MAIL SERVER OPERATING SYSTEM")
  108. print("Written and developed by NitrogenFingers")
  109. print(string.rep("-", 50))
  110. print("Opening server on "..os.getComputerID().."...")
  111. print("Checking stored emails...")
  112. readFromFile()
  113.  
  114. rednet.open(side)
  115. print("Waiting for requests.")
  116. while true do
  117.   print("Message Count: "..table.getn(umail))
  118.   local sender,msg = rednet.receive()
  119.   print("Message received from "..sender..":"..msg)
  120.   if string.find(msg, "$REQUEST") then
  121.     print("Request format recognized- processing")
  122.     dispatchRequest(sender)
  123.   elseif string.find(msg, "$EMAIL") then
  124.     print("Email format recognized- storing")
  125.     addMail(msg, sender)
  126.   else
  127.     print("Format unrecognized- discarding")
  128.   end
  129. end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement