Advertisement
Gulligagardinen

Ice-Mail-server

Jul 21st, 2013
409
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 2.80 KB | None | 0 0
  1. --[[
  2.     Ice-mail. Mail server
  3.     By: Aron (Gulligagardinen)
  4.    
  5.     Terms Of Use:
  6.         This file is provided as it is, you may not delete this terms of use protocol from the source code.
  7.        
  8.         You may change what you want in this code. I even encourage you to do changes after your own preferences! =D
  9.         Provided that you credit the original maker: Gulligagardinen.
  10.        
  11.         You may use this program in any kind of UI or OS, provided that you credit the original maker: Gulligagardinen.
  12.        
  13.        
  14.     If you experience any bugs or glitches, please contact me.
  15. --]]
  16.  
  17. --Mail Inbox
  18. local mail = {}
  19.  
  20. --Main variables that contains general information
  21. local modem;
  22. local sSide = "top" --Change this to the side where the modem is.
  23.  
  24. --Modem functions: open, close and isOpen
  25. local function open()
  26.     modem = peripheral.wrap(sSide)
  27.     modem.open(65535)
  28. end
  29.  
  30. local function close()
  31.     if sSide ~= "" then
  32.         modem.closeAll()
  33.     end
  34. end
  35.  
  36. local function isOpen()
  37.     if modem.isOpen(65535) then
  38.         return true;
  39.     end
  40.     return false;
  41. end
  42.  
  43. --Send and receive functions
  44. local function send(channel, str, rChannel)
  45.     if isOpen() then
  46.         local rec;
  47.         if not rChannel then rec = 65535 else rec = rChannel end
  48.         if sSide ~= "" then
  49.             modem.transmit(channel, rec, str)
  50.         else
  51.             error("No chosen modem side")
  52.         end
  53.     else
  54.         error("Please make sure that the modem is opened")
  55.     end
  56. end
  57.  
  58. local function receive()
  59.     if isOpen() then
  60.         local event, side, sCh, rCh, msg, sd = os.pullEvent("modem_message")
  61.         return sCh, rCh, msg; --Sender Channel, Reply Channel, Message
  62.     else
  63.         error("Please make sure that the modem is opened")
  64.     end
  65. end
  66.  
  67. --Server handling
  68. local function receiveCall()
  69.     local sCh, rCh, msg = receive()
  70.     if msg == "pleaseIndex" then
  71.         if mail[rCh] then
  72.             send(rCh, #mail[rCh])
  73.             --print("Sent Amount of messages for "..tostring(rCh))
  74.         end
  75.     elseif msg:find("indexTextMsg") then
  76.         if mail[rCh] then
  77.             local txt = tonumber(msg:sub(14, #msg))
  78.             if mail[rCh][txt] then
  79.                 send(rCh, mail[rCh][txt].msg, mail[rCh][txt].sender)
  80.                 --print("Forwarding a message for "..tostring(rCh))
  81.             end
  82.         end
  83.     elseif msg:find("]") then
  84.         local mark = msg:find("]")
  85.         if not mail[tonumber(msg:sub(1, mark-1))] then
  86.             mail[tonumber(msg:sub(1, mark-1))] = {[1] = {msg = msg:sub(mark+1, #msg), sender = rCh}}
  87.         else
  88.             mail[tonumber(msg:sub(1, mark-1))][#mail[tonumber(msg:sub(1, mark-1))]+1] = {msg = msg:sub(mark+1, #msg), sender = rCh}
  89.         end
  90.         --print("Received a message for "..msg:sub(1, mark-1))
  91.     elseif msg:find("removeIndex") then
  92.         if mail[rCh] then
  93.             if mail[rCh][tonumber(msg:sub(13, #msg))] then
  94.                 table.remove(mail[rCh], tonumber(msg:sub(13, #msg)))
  95.                 --print("Removed a message for "..tostring(rCh))
  96.             end
  97.         end
  98.     end
  99. end
  100.  
  101. --Main loop
  102. term.clear() term.setCursorPos(1, 1)
  103. while true do
  104.     open()
  105.     receiveCall()
  106. end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement