Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- --[[
- Ice-mail. Mail server
- By: Aron (Gulligagardinen)
- Terms Of Use:
- This file is provided as it is, you may not delete this terms of use protocol from the source code.
- You may change what you want in this code. I even encourage you to do changes after your own preferences! =D
- Provided that you credit the original maker: Gulligagardinen.
- You may use this program in any kind of UI or OS, provided that you credit the original maker: Gulligagardinen.
- If you experience any bugs or glitches, please contact me.
- --]]
- --Mail Inbox
- local mail = {}
- --Main variables that contains general information
- local modem;
- local sSide = "top" --Change this to the side where the modem is.
- --Modem functions: open, close and isOpen
- local function open()
- modem = peripheral.wrap(sSide)
- modem.open(65535)
- end
- local function close()
- if sSide ~= "" then
- modem.closeAll()
- end
- end
- local function isOpen()
- if modem.isOpen(65535) then
- return true;
- end
- return false;
- end
- --Send and receive functions
- local function send(channel, str, rChannel)
- if isOpen() then
- local rec;
- if not rChannel then rec = 65535 else rec = rChannel end
- if sSide ~= "" then
- modem.transmit(channel, rec, str)
- else
- error("No chosen modem side")
- end
- else
- error("Please make sure that the modem is opened")
- end
- end
- local function receive()
- if isOpen() then
- local event, side, sCh, rCh, msg, sd = os.pullEvent("modem_message")
- return sCh, rCh, msg; --Sender Channel, Reply Channel, Message
- else
- error("Please make sure that the modem is opened")
- end
- end
- --Server handling
- local function receiveCall()
- local sCh, rCh, msg = receive()
- if msg == "pleaseIndex" then
- if mail[rCh] then
- send(rCh, #mail[rCh])
- --print("Sent Amount of messages for "..tostring(rCh))
- end
- elseif msg:find("indexTextMsg") then
- if mail[rCh] then
- local txt = tonumber(msg:sub(14, #msg))
- if mail[rCh][txt] then
- send(rCh, mail[rCh][txt].msg, mail[rCh][txt].sender)
- --print("Forwarding a message for "..tostring(rCh))
- end
- end
- elseif msg:find("]") then
- local mark = msg:find("]")
- if not mail[tonumber(msg:sub(1, mark-1))] then
- mail[tonumber(msg:sub(1, mark-1))] = {[1] = {msg = msg:sub(mark+1, #msg), sender = rCh}}
- else
- mail[tonumber(msg:sub(1, mark-1))][#mail[tonumber(msg:sub(1, mark-1))]+1] = {msg = msg:sub(mark+1, #msg), sender = rCh}
- end
- --print("Received a message for "..msg:sub(1, mark-1))
- elseif msg:find("removeIndex") then
- if mail[rCh] then
- if mail[rCh][tonumber(msg:sub(13, #msg))] then
- table.remove(mail[rCh], tonumber(msg:sub(13, #msg)))
- --print("Removed a message for "..tostring(rCh))
- end
- end
- end
- end
- --Main loop
- term.clear() term.setCursorPos(1, 1)
- while true do
- open()
- receiveCall()
- end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement