SHARE
TWEET

Untitled

a guest Feb 23rd, 2019 64 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. tMails = {}
  2.  
  3. local bGo = true
  4.  
  5.  
  6.  
  7. local function split(sMail)
  8.  
  9.  local tPositions = {0,0,0,0,0}
  10.  local tContents= {"","","",""}
  11.  
  12.  local last = 0
  13.  local first
  14.  for i = 1, # tPositions do --determine separator locations
  15.   first, last = string.find(sMail,"#|#",last+1)
  16.   tPositions[i] = last
  17.  end -- end for
  18.  
  19.  
  20.  for i = 1,((# tPositions) - 1) do --get contents
  21.   tContents[i] = string.sub(sMail, tPositions[i]+1,tPositions[i+1]-3)
  22.  end -- end for
  23.  
  24.  return tContents[1],tContents[2],tContents[3],tContents[4]
  25.  
  26. end -- end split
  27.  
  28. local function sendMail(header, dst, src, msg)
  29.  
  30.  rednet.send(tonumber(dst), "#|#"..header.."#|#"..dst.."#|#"..src.."#|#"..msg.."#|#")
  31.  
  32. end -- end sendMail
  33.  
  34. local function reply(dst, msg)
  35.  
  36.  src = os.getComputerID()
  37.  header = "$REPLY"
  38.  
  39.  sendMail(header, dst, src, msg)
  40.  
  41. end -- end reply
  42.  
  43. print("Booting server...")
  44. rednet.open("top")
  45. while bGo do
  46.  
  47.  local id,msg,dist = rednet.receive()
  48.  print("From:  "..id.." : "..msg)
  49.  local header,dst,src,mesg = split(msg)
  50.  
  51.  if header == "$REQUEST" then
  52.   for i,v in ipairs(tMails) do
  53.    if v[2] == src then
  54.     sendMail(v[1],v[2],v[3],v[4])
  55.    end --end if
  56.   end -- end for
  57.  
  58.  elseif header == "$MAIL" then
  59.   local newMail = {header,dst,src,mesg}
  60.   table.insert(tMails,newMail)
  61.   reply(id,"mail got")
  62.  
  63.  elseif header == "$REPLY" then
  64.   if mesg=="mails got" then
  65.    for i,v in ipairs(tMails) do
  66.     if v[2] == src then
  67.      table.remove(tMails,i)
  68.     end -- end if
  69.    end -- end for
  70.   end -- end if
  71.  end -- end if
  72.  
  73.  
  74. end -- end while
RAW Paste Data
We use cookies for various purposes including analytics. By continuing to use Pastebin, you agree to our use of cookies as described in the Cookies Policy. OK, I Understand
 
Top