Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- -- Mailing System by PaymentOption --
- VERSION = "Alpha 0.0.1"
- -------------------------------------
- rednet.open("top")
- rednet.open("back")
- rednet.open("bottom")
- rednet.open("left")
- rednet.open("right")
- -- VARS --
- nReceiver = 0
- sBody = ""
- selection = 1
- sFileAttachmentPath = ""
- screenWidth, screenHeight = term.getSize()
- ----------
- -- Helper Functions --
- function cPrint(height, string)
- local xPos = screenWidth/2 - string.len(string)/2
- term.setCursorPos(xPos, height); term.write(string)
- end
- function rPrint(height, string)
- local xPos = screenWidth - string.len(string)
- term.setCursorPos(xPos, height); term.write(string)
- end
- function clear() term.clear(); term.setCursorPos(1,1) end
- function split(str, pat) -- Not written by me
- local t = {} -- NOTE: use {n = 0} in Lua-5.0
- local fpat = "(.-)" .. pat
- local last_end = 1
- local s, e, cap = str:find(fpat, 1)
- while s do
- if s ~= 1 or cap ~= "" then
- table.insert(t,cap)
- end
- last_end = e+1
- s, e, cap = str:find(fpat, last_end)
- end
- if last_end <= #str then
- cap = str:sub(last_end)
- table.insert(t, cap)
- end
- return t
- end
- -- Table Utils I did not write --
- function tableValToString( v )
- if "string" == type( v ) then
- v = string.gsub( v, "\n", "\\n" )
- if string.match( string.gsub(v,"[^'\"]",""), '^"+$' ) then
- return "'" .. v .. "'"
- end
- return '"' .. string.gsub(v,'"', '\\"' ) .. '"'
- else
- return "table" == type( v ) and tableToString( v ) or
- tostring( v )
- end
- end
- function tableKeyToString( k )
- if "string" == type( k ) and string.match( k, "^[_%a][_%a%d]*$" ) then
- return k
- else
- return "[" .. tableValToString( k ) .. "]"
- end
- end
- function tableToString( tbl )
- local result, done = {}, {}
- for k, v in ipairs( tbl ) do
- table.insert( result, tableValToString( v ) )
- done[ k ] = true
- end
- for k, v in pairs( tbl ) do
- if not done[ k ] then
- table.insert( result,
- tableKeyToString( k ) .. "=" .. table.val_to_str( v ) )
- end
- end
- return table.concat( result, nil)
- end
- ---------------------------------
- -- Modified Pastebin program to return the link! --
- function pastebinPut(sPath)
- local sCommand = "put"
- if sCommand == "put" then
- -- Upload a file to pastebin.com
- -- Determine file to upload
- local sFile = sPath
- if not fs.exists( sPath ) or fs.isDir( sPath ) then
- return nil
- end
- -- Read in the file
- local sName = fs.getName( sPath )
- local file = fs.open( sPath, "r" )
- local sText = file.readAll()
- file.close()
- -- POST the contents to pastebin
- local key = "0ec2eb25b6166c0c27a394ae118ad829"
- local response = http.post(
- "http://pastebin.com/api/api_post.php",
- "api_option=paste&"..
- "api_dev_key="..key.."&"..
- "api_paste_format=lua&"..
- "api_paste_name="..textutils.urlEncode(sName).."&"..
- "api_paste_code="..textutils.urlEncode(sText)
- )
- if response then
- local sResponse = response.readAll()
- response.close()
- local sCode = string.match( sResponse, "[^/]+$" )
- return sCode
- else
- return nil
- end
- end
- end
- ---------------------------------------------------
- ----------------------
- -- Menues and other screens --
- function printHeader()
- cPrint(1, "Mail Client by PaymentOption")
- rPrint(18, "Version: "..VERSION)
- end
- function printMenu()
- if selection == 1 then cPrint(6, "[ Send Mail ]")
- else cPrint(6, " Send Mail ") end
- if selection == 2 then cPrint(7, "[ Read Mail ]")
- else cPrint(7, " Read Mail ") end
- if selection == 3 then cPrint(8, "[ Exit ]")
- else cPrint(8, " Exit ") end
- end
- function printSendMailMenu()
- local tTempBody = {}
- local sFile = ""
- cPrint(1, "Send Mail Menu")
- cPrint(17, "Press HOME to finish")
- term.setCursorPos(1, 4); term.write("Recipient: "); nReceiver = tonumber(read())
- term.setCursorPos(1, 5); term.write("Body:")
- term.setCursorPos(1, 6); term.write(string.rep("-", screenWidth))
- term.setCursorPos(1, 7); tTempBody = getBody(tTempBody)
- term.setCursorPos(1, 2); term.write("Attach File? Y/N"); sFile = tostring(read())
- for i=1, #tTempBody do
- sBody = sBody..tTempBody[i]
- end
- if sFile ~= "N" and sFile ~= "n" then
- term.setCursorPos(1, 2); term.clearLine()
- term.write("File Path: "); sFileAttachmentPath = tostring(read()); attachFile(sFileAttachmentPath, sBody)
- else
- sendMail(nReceiver, sBody) --; sBody = tableTostring(tTempBody)
- end
- end
- function printDownloadMenu()
- if selection == 1 then cPrint(11, "[ Yes ] No ")
- else cPrint(11, " Yes [ No ]") end
- end
- function printReceiveMenu()
- cPrint(8, "MESSAGE RECEIVED. READ?")
- if selection == 1 then cPrint(9, "[ Yes ] No ")
- else cPrint(10," Yes [ No ]") end
- end
- ------------------------------
- -- Input related functions --
- function getBody()
- local tTempBody = {}
- local iterator = 1
- term.setCursorBlink(true)
- while true do
- sEvent, param = os.pullEvent()
- if sEvent == "key" then
- if param == 199 then return tTempBody end
- if param == 14 then
- term.setCursorPos(1,7); term.clearLine()
- for i=1, #tTempBody-1 do
- write(tTempBody[i])
- end
- iterator = iterator-1
- end
- elseif sEvent == "char" then
- tTempBody[iterator] = tostring(param)
- iterator = iterator+1
- write(param)
- end
- end
- end
- -----------------------------
- -- Netowrking Functions --
- function attachFile(sPath, sBody)
- local downloadCode = pastebinPut(sPath)
- if downloadCode ~= nil then
- sBody = "89387f98oq3o98d\n"..downloadCode.."\n"..sBody
- sendMail(nReceiver, sBody)
- else
- term.setCursorPos(1, 2); term.clearLine(); term.write("Error")
- end
- end
- function sendMail(nReciever, sBody)
- rednet.send(nReceiver, sBody)
- end
- function receiveMail(nSender, sMessage) -- How does os.pullEvent() react to a rednet message?
- if string.find(sMessage, "89387f98oq3o98d") then
- local file = fs.open(".tempFile", "w")
- file.write(sMessage)
- file.close()
- file = fs.open(".tempFile", "r")
- local fileContents = file.readLine()
- fileContents = file.readLine() -- Reach the second line containing the file link
- selection = 1
- cPrint(10, "MESSAGE RECEIVED: FILE ATTACHED")
- while true do
- printDownloadMenu()
- event, key = os.pullEvent("key")
- if key == 205 and selection == 1 then selection = 2
- elseif key == 203 and selection == 2 then selection = 1
- elseif key == 28 and selection == 1 then shell.run("pastebin", "get", tostring(fileContents), "Attachment")
- elseif key == 28 and selection == 2 then break
- end
- end
- else
- clear()
- printMail(sSender, sMessage)
- end
- end
- --------------------------
- while true do
- clear()
- printHeader()
- printMenu()
- event, param1, param2 = os.pullEvent()
- if event == "key" and param1 == 200 and selection > 1 then selection = selection-1
- elseif event == "key" and param1 == 208 and selection < 3 then selection = selection+1
- elseif event == "key" and param1 == 28 and selection == 1 then clear(); printSendMailMenu()
- elseif event == "key" and param1 == 28 and selection == 2 then cPrint(7, "UNDER CONSTRUCTION!"); sleep(1.3)
- elseif event == "key" and param1 == 28 and selection == 3 then break
- elseif event == "rednet_message" then
- while true do
- printReceiveMenu()
- event, key = os.pullEvent("key")
- if key == 205 and selection < 2 then selection = selection+1
- elseif key == 203 and selection > 1 then selection = selection-1
- elseif key == 28 and selection == 1 then receiveMessage(param1, tostring(param2))
- elseif key == 28 and selection == 2 then break
- end
- end
- end
- end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement