Advertisement
PaymentOption

mailclient

May 20th, 2012
80
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 7.60 KB | None | 0 0
  1. -- Mailing System by PaymentOption --
  2. VERSION = "Alpha 0.0.1"
  3. -------------------------------------
  4.  
  5. rednet.open("top")
  6. rednet.open("back")
  7. rednet.open("bottom")
  8. rednet.open("left")
  9. rednet.open("right")
  10.  
  11. -- VARS --
  12. nReceiver = 0
  13. sBody = ""
  14.  
  15. selection = 1
  16.  
  17. sFileAttachmentPath = ""
  18. screenWidth, screenHeight = term.getSize()
  19. ----------
  20.  
  21. -- Helper Functions --
  22. function cPrint(height, string)
  23.     local xPos = screenWidth/2 - string.len(string)/2
  24.     term.setCursorPos(xPos, height); term.write(string)
  25. end
  26.  
  27. function rPrint(height, string)
  28.     local xPos = screenWidth - string.len(string)
  29.     term.setCursorPos(xPos, height); term.write(string)
  30. end
  31.  
  32. function clear() term.clear(); term.setCursorPos(1,1) end
  33.  
  34. function split(str, pat) -- Not written by me
  35.    local t = {}  -- NOTE: use {n = 0} in Lua-5.0
  36.    local fpat = "(.-)" .. pat
  37.    local last_end = 1
  38.    local s, e, cap = str:find(fpat, 1)
  39.    while s do
  40.       if s ~= 1 or cap ~= "" then
  41.      table.insert(t,cap)
  42.       end
  43.       last_end = e+1
  44.       s, e, cap = str:find(fpat, last_end)
  45.    end
  46.    if last_end <= #str then
  47.       cap = str:sub(last_end)
  48.       table.insert(t, cap)
  49.    end
  50.    return t
  51. end
  52.  
  53. -- Table Utils I did not write --
  54. function tableValToString( v )
  55.   if "string" == type( v ) then
  56.     v = string.gsub( v, "\n", "\\n" )
  57.     if string.match( string.gsub(v,"[^'\"]",""), '^"+$' ) then
  58.       return "'" .. v .. "'"
  59.     end
  60.     return '"' .. string.gsub(v,'"', '\\"' ) .. '"'
  61.   else
  62.     return "table" == type( v ) and tableToString( v ) or
  63.       tostring( v )
  64.   end
  65. end
  66.  
  67. function tableKeyToString( k )
  68.   if "string" == type( k ) and string.match( k, "^[_%a][_%a%d]*$" ) then
  69.     return k
  70.   else
  71.     return "[" .. tableValToString( k ) .. "]"
  72.   end
  73. end
  74.  
  75. function tableToString( tbl )
  76.   local result, done = {}, {}
  77.   for k, v in ipairs( tbl ) do
  78.     table.insert( result, tableValToString( v ) )
  79.     done[ k ] = true
  80.   end
  81.   for k, v in pairs( tbl ) do
  82.     if not done[ k ] then
  83.       table.insert( result,
  84.         tableKeyToString( k ) .. "=" .. table.val_to_str( v ) )
  85.     end
  86.   end
  87.   return table.concat( result, nil)
  88. end
  89. ---------------------------------
  90.  
  91. -- Modified Pastebin program to return the link! --
  92. function pastebinPut(sPath)
  93.  
  94. local sCommand = "put"
  95. if sCommand == "put" then
  96.     -- Upload a file to pastebin.com
  97.     -- Determine file to upload
  98.     local sFile = sPath
  99.     if not fs.exists( sPath ) or fs.isDir( sPath ) then
  100.         return nil
  101.     end
  102.    
  103.     -- Read in the file
  104.     local sName = fs.getName( sPath )
  105.     local file = fs.open( sPath, "r" )
  106.     local sText = file.readAll()
  107.     file.close()
  108.    
  109.     -- POST the contents to pastebin
  110.     local key = "0ec2eb25b6166c0c27a394ae118ad829"
  111.     local response = http.post(
  112.         "http://pastebin.com/api/api_post.php",
  113.         "api_option=paste&"..
  114.         "api_dev_key="..key.."&"..
  115.         "api_paste_format=lua&"..
  116.         "api_paste_name="..textutils.urlEncode(sName).."&"..
  117.         "api_paste_code="..textutils.urlEncode(sText)
  118.         )
  119.        
  120.     if response then
  121.         local sResponse = response.readAll()
  122.         response.close()
  123.                
  124.         local sCode = string.match( sResponse, "[^/]+$" )
  125.         return sCode
  126.  
  127.     else
  128.         return nil
  129.     end
  130. end
  131. end
  132. ---------------------------------------------------
  133. ----------------------
  134.  
  135. -- Menues and other screens --
  136. function printHeader()
  137.     cPrint(1, "Mail Client by PaymentOption")
  138.     rPrint(18, "Version: "..VERSION)
  139. end
  140.  
  141. function printMenu()
  142.     if selection == 1 then cPrint(6, "[ Send Mail ]")
  143.     else                   cPrint(6, "  Send Mail  ") end
  144.    
  145.     if selection == 2 then cPrint(7, "[ Read Mail ]")
  146.     else                   cPrint(7, "  Read Mail  ") end
  147.    
  148.     if selection == 3 then cPrint(8, "[ Exit ]")
  149.     else                   cPrint(8, "  Exit  ") end
  150. end
  151.  
  152. function printSendMailMenu()
  153.     local tTempBody = {}
  154.     local sFile = ""
  155.     cPrint(1, "Send Mail Menu")
  156.     cPrint(17, "Press HOME to finish")
  157.    
  158.     term.setCursorPos(1, 4); term.write("Recipient: "); nReceiver = tonumber(read())
  159.     term.setCursorPos(1, 5); term.write("Body:")
  160.     term.setCursorPos(1, 6); term.write(string.rep("-", screenWidth))
  161.     term.setCursorPos(1, 7); tTempBody = getBody(tTempBody)
  162.    
  163.     term.setCursorPos(1, 2); term.write("Attach File? Y/N"); sFile = tostring(read())
  164.     for i=1, #tTempBody do
  165.         sBody = sBody..tTempBody[i]
  166.     end
  167.         if sFile ~= "N" and sFile ~= "n" then
  168.             term.setCursorPos(1, 2); term.clearLine()
  169.             term.write("File Path: "); sFileAttachmentPath = tostring(read()); attachFile(sFileAttachmentPath, sBody)
  170.         else
  171.             sendMail(nReceiver, sBody) --; sBody = tableTostring(tTempBody)
  172.         end
  173. end
  174.  
  175. function printDownloadMenu()
  176.     if selection == 1 then cPrint(11, "[ Yes ]  No  ")
  177.     else                   cPrint(11, "  Yes  [ No ]") end
  178. end
  179.  
  180. function printReceiveMenu()
  181.     cPrint(8, "MESSAGE RECEIVED. READ?")
  182.     if selection == 1 then cPrint(9, "[ Yes ]  No  ")
  183.     else                   cPrint(10,"  Yes  [ No ]") end
  184. end
  185. ------------------------------
  186.  
  187. -- Input related functions --
  188. function getBody()
  189.     local tTempBody = {}
  190.     local iterator = 1
  191.     term.setCursorBlink(true)
  192.    
  193.     while true do
  194.         sEvent, param = os.pullEvent() 
  195.         if sEvent == "key" then
  196.             if param == 199 then return tTempBody end
  197.             if param == 14 then
  198.                 term.setCursorPos(1,7); term.clearLine()
  199.                 for i=1, #tTempBody-1 do
  200.                     write(tTempBody[i])
  201.                 end
  202.                 iterator = iterator-1
  203.             end
  204.         elseif sEvent == "char" then
  205.             tTempBody[iterator] = tostring(param)
  206.             iterator = iterator+1
  207.             write(param)
  208.         end
  209.     end
  210. end
  211. -----------------------------
  212.  
  213. -- Netowrking Functions --
  214. function attachFile(sPath, sBody)
  215.     local downloadCode = pastebinPut(sPath)
  216.    
  217.     if downloadCode ~= nil then
  218.         sBody = "89387f98oq3o98d\n"..downloadCode.."\n"..sBody
  219.         sendMail(nReceiver, sBody)
  220.     else
  221.         term.setCursorPos(1, 2); term.clearLine(); term.write("Error")
  222.     end
  223. end
  224.  
  225. function sendMail(nReciever, sBody)
  226.     rednet.send(nReceiver, sBody)
  227. end
  228.  
  229. function receiveMail(nSender, sMessage) -- How does os.pullEvent() react to a rednet message?
  230.     if string.find(sMessage, "89387f98oq3o98d") then
  231.         local file = fs.open(".tempFile", "w")
  232.         file.write(sMessage)
  233.         file.close()
  234.        
  235.         file = fs.open(".tempFile", "r")
  236.         local fileContents = file.readLine()
  237.         fileContents = file.readLine() -- Reach the second line containing the file link
  238.        
  239.         selection = 1
  240.         cPrint(10, "MESSAGE RECEIVED: FILE ATTACHED")
  241.         while true do
  242.             printDownloadMenu()
  243.             event, key = os.pullEvent("key")
  244.            
  245.             if key == 205 and selection == 1 then selection = 2
  246.             elseif key == 203 and selection == 2 then selection = 1
  247.            
  248.             elseif key == 28 and selection == 1 then shell.run("pastebin", "get", tostring(fileContents), "Attachment")
  249.             elseif key == 28 and selection == 2 then break
  250.             end
  251.         end
  252.     else
  253.         clear()
  254.         printMail(sSender, sMessage)
  255.     end
  256. end
  257. --------------------------
  258.  
  259. while true do
  260.     clear()
  261.     printHeader()
  262.     printMenu()
  263.    
  264.     event, param1, param2 = os.pullEvent()
  265.    
  266.     if event == "key" and param1 == 200 and selection > 1 then selection = selection-1
  267.     elseif event == "key" and param1 == 208 and selection < 3 then selection = selection+1
  268.    
  269.     elseif event == "key" and param1 == 28 and selection == 1 then clear(); printSendMailMenu()
  270.     elseif event == "key" and param1 == 28 and selection == 2 then cPrint(7, "UNDER CONSTRUCTION!"); sleep(1.3)
  271.     elseif event == "key" and param1 == 28 and selection == 3 then break
  272.    
  273.     elseif event == "rednet_message" then
  274.         while true do
  275.             printReceiveMenu()
  276.             event, key = os.pullEvent("key")
  277.             if key == 205 and selection < 2 then selection = selection+1
  278.             elseif key == 203 and selection > 1 then selection = selection-1
  279.            
  280.             elseif key == 28 and selection == 1 then receiveMessage(param1, tostring(param2))
  281.             elseif key == 28 and selection == 2 then break
  282.             end
  283.         end
  284.     end
  285. end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement