Advertisement
PaymentOption

Simple Mail Sender

Oct 6th, 2012
66
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 5.71 KB | None | 0 0
  1. tArgs = {...} -- Capture arguments that are passed when this program is run.
  2. sMailIndicator = "MAIL_MESSAGE" -- The indicator in rednet messages that identifies them as mail messages.
  3.  
  4. -- Returns a file hanlde if the desired path is valid.
  5. function getFileHandle(sPath, cMode)
  6.     if not fs.isDir(sPath) then
  7.         local fileHandle = fs.open(sPath, cMode)
  8.         if fileHandle then
  9.             return fileHandle
  10.         else
  11.             return nil
  12.         end
  13.     else
  14.         return nil
  15.     end
  16. end
  17.  
  18. -- Takes a file handle in read mode, then loops through it using fs.readLine(),
  19. -- finally returning the length of the file in number of lines.
  20. function getFieLength(sPath)
  21.     -- Make sure that the given file path is valid.
  22.     if not fs.isDir(sPath) and fs.exists(sPath) then
  23.         -- Get a handle on the file to read from.
  24.         local fileHandle = io.open(sPath, 'r')
  25.         local nLines = 0
  26.         -- Validate the file handle.
  27.         if fileHandle then
  28.             -- Loop through every line of the file and return the number of lines in the file.
  29.             for nLine in fileHandle:lines() do
  30.                 nLines = nLines + 1
  31.             end
  32.             fileHandle:close() -- Close the file handle.
  33.  
  34.             -- Return the number of lines in the file.
  35.             return nLines
  36.         else
  37.             return 0
  38.         end
  39.     else
  40.         return 0
  41.     end
  42. end
  43.  
  44. -- Reads a file and sends it as an email to the requested id.
  45. function sendMail(nReceiver, sPath)
  46.     -- Check if the parameters given are valid.
  47.     if nReceiver and sPath then
  48.         -- If the file handle is valid, then go ahead and get the length of the file to send.
  49.         local nFileLength_InLines = getFieLength(sPath)
  50.         -- Get a handle on the file to read and send.
  51.         local mailFileHandle = getFileHandle(sPath, 'r')
  52.         local sMessage = sMailIndicator -- Make sure to start the message with the mail indicator so it is recognized as mail by the receiver.
  53.         -- Validate the file handle.
  54.         if mailFileHandle then
  55.             for nLine = 1, nFileLength_InLines do
  56.                 -- Add the line to the current message.
  57.                 sMessage = sMessage .. mailFileHandle.readLine() .. '\n' -- Not sure if the \n character already exists on the line.
  58.             end
  59.  
  60.             -- Send the message over rednet to the desired computer.
  61.             if type(nReceiver) == "number" or tonumber(nReceiver) then
  62.                 rednet.send(tonumber(nReceiver), sMessage)
  63.                 return true
  64.             else
  65.                 return false
  66.             end
  67.         else
  68.             return false
  69.         end
  70.     else
  71.         return false
  72.     end
  73. end
  74.  
  75. -- Prints the proper syntax to be used when calling this program from the shell.
  76. function printUsage()
  77.     print(shell.getRunningProgram() .. " <receiver_ID> <message_file_name>")
  78. end
  79.  
  80. -- Opens any modem that exists, throws an error if a modem isn't found.
  81. function openExistantModem()
  82.     local tSides = rs.getSides()
  83.  
  84.     for nSideIndex, sSide in ipairs(tSides) do
  85.         if peripheral.isPresent(sSide) and peripheral.getType(sSide) == "modem" then
  86.             rednet.open(sSide)
  87.             return true
  88.         end
  89.     end
  90.  
  91.     -- If the loop executed and did not return, then there is not modem on the computer.
  92.     -- Throw an error.
  93.     error("No modem found!")
  94. end
  95.  
  96. -- Make sure that the arguments are plenty and valid for this program to work.
  97. function checkArguments()
  98.     -- The program takes two arguments; check if there are exactly that many.
  99.     if #tArgs >= 2 then
  100.         -- Check if the id passed is either already a number, or can be cast to a number.
  101.         if type(tArgs[1]) == "number" or tonumber(tArgs[1]) then
  102.             local nReceiver = tonumber(tArgs[1]) -- Establish a variable for the receiver ID passed as an argument to the program.
  103.             local sPath = tArgs[2] -- Establish a variable for the path of the message file to be read then sent.
  104.             -- Check if the passed file path is valid.
  105.             if fs.exists(sPath) and not fs.isDir(sPath) then
  106.                 -- Now that all of the argument checks have been passed, return a success so the program may proceed.
  107.                 return true
  108.             -- If the passed message file path is not valid, print the proper syntax of the program and return a failure.
  109.             else
  110.                 printUsage()
  111.                 return false
  112.             end
  113.         -- If the passed ID is not valid, print the proper syntax of the program and return a failure.
  114.         else
  115.             printUsage()
  116.             return false
  117.         end
  118.     -- If the arguments are not enough or are invalid, print the proper usage of the program and return a failure.
  119.     else
  120.         printUsage()
  121.         return false
  122.     end
  123. end
  124.  
  125. -- Make sure all of the arguments are valid for the program to execute properly.
  126. if checkArguments() then
  127.     -- Open any modem that exists on the computer.
  128.     openExistantModem()
  129.     -- Attempt to send the mail requested from the passed file.
  130.     -- tArgs[1] is the receiver id, and tArgs[2] is the file path for the message that will need to be read, then sent.
  131.     if sendMail(tonumber(tArgs[1]), tArgs[2]) then
  132.         print("Mail sent!")
  133.     else
  134.         print("Send failure.")
  135.     end
  136. -- If the argument checks are not valid, then DO NOT attempt to run the program.
  137. else
  138.     return -- Escape the program. (Programs are run as functions underneath the shell, so we can return from them using a retunr call
  139.         -- in the global scope.)
  140. end
  141.  
  142. -- SUPERFLOUS METHOD: MAIL IS READ FROM A PRE-EXISTING FILE; DOES NOT NEED TO HAVE ITS OWN METHOD TO COMPOSE MAIL.
  143. --[[
  144. -- Runs the edit program on a temporary file and then sends the contents of the message to the desired computer.
  145. function composeMail(nReceiver)
  146.     if type(nReceiver) == "number" or tonumber(nReceiver) then
  147.         -- Make sure that the temporary file doesn't exist. If it does, then delete it.
  148.         if fs.exsists(".tmpFile") then
  149.             fs.delete(".tmpFile")
  150.         end
  151.         -- Run the 'edit' program for the user to enter their message.
  152.         shell.run("edit", ".tmpFile")
  153.         -- Send the message.
  154.         if sendMail(nReceiver, ".tmpFile") then
  155.             print("Message sent to " .. nReceiver .. '!')
  156.         else
  157.             print("Message send failure.")
  158.         end
  159.  
  160.         return
  161.     else
  162.         error("Receipient ID must be a positive intenger.")
  163.     end
  164. end
  165. --]]
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement