Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- tArgs = {...} -- Capture arguments that are passed when this program is run.
- sMailIndicator = "MAIL_MESSAGE" -- The indicator in rednet messages that identifies them as mail messages.
- -- Returns a file hanlde if the desired path is valid.
- function getFileHandle(sPath, cMode)
- if not fs.isDir(sPath) then
- local fileHandle = fs.open(sPath, cMode)
- if fileHandle then
- return fileHandle
- else
- return nil
- end
- else
- return nil
- end
- end
- -- Takes a file handle in read mode, then loops through it using fs.readLine(),
- -- finally returning the length of the file in number of lines.
- function getFieLength(sPath)
- -- Make sure that the given file path is valid.
- if not fs.isDir(sPath) and fs.exists(sPath) then
- -- Get a handle on the file to read from.
- local fileHandle = io.open(sPath, 'r')
- local nLines = 0
- -- Validate the file handle.
- if fileHandle then
- -- Loop through every line of the file and return the number of lines in the file.
- for nLine in fileHandle:lines() do
- nLines = nLines + 1
- end
- fileHandle:close() -- Close the file handle.
- -- Return the number of lines in the file.
- return nLines
- else
- return 0
- end
- else
- return 0
- end
- end
- -- Reads a file and sends it as an email to the requested id.
- function sendMail(nReceiver, sPath)
- -- Check if the parameters given are valid.
- if nReceiver and sPath then
- -- If the file handle is valid, then go ahead and get the length of the file to send.
- local nFileLength_InLines = getFieLength(sPath)
- -- Get a handle on the file to read and send.
- local mailFileHandle = getFileHandle(sPath, 'r')
- local sMessage = sMailIndicator -- Make sure to start the message with the mail indicator so it is recognized as mail by the receiver.
- -- Validate the file handle.
- if mailFileHandle then
- for nLine = 1, nFileLength_InLines do
- -- Add the line to the current message.
- sMessage = sMessage .. mailFileHandle.readLine() .. '\n' -- Not sure if the \n character already exists on the line.
- end
- -- Send the message over rednet to the desired computer.
- if type(nReceiver) == "number" or tonumber(nReceiver) then
- rednet.send(tonumber(nReceiver), sMessage)
- return true
- else
- return false
- end
- else
- return false
- end
- else
- return false
- end
- end
- -- Prints the proper syntax to be used when calling this program from the shell.
- function printUsage()
- print(shell.getRunningProgram() .. " <receiver_ID> <message_file_name>")
- end
- -- Opens any modem that exists, throws an error if a modem isn't found.
- function openExistantModem()
- local tSides = rs.getSides()
- for nSideIndex, sSide in ipairs(tSides) do
- if peripheral.isPresent(sSide) and peripheral.getType(sSide) == "modem" then
- rednet.open(sSide)
- return true
- end
- end
- -- If the loop executed and did not return, then there is not modem on the computer.
- -- Throw an error.
- error("No modem found!")
- end
- -- Make sure that the arguments are plenty and valid for this program to work.
- function checkArguments()
- -- The program takes two arguments; check if there are exactly that many.
- if #tArgs >= 2 then
- -- Check if the id passed is either already a number, or can be cast to a number.
- if type(tArgs[1]) == "number" or tonumber(tArgs[1]) then
- local nReceiver = tonumber(tArgs[1]) -- Establish a variable for the receiver ID passed as an argument to the program.
- local sPath = tArgs[2] -- Establish a variable for the path of the message file to be read then sent.
- -- Check if the passed file path is valid.
- if fs.exists(sPath) and not fs.isDir(sPath) then
- -- Now that all of the argument checks have been passed, return a success so the program may proceed.
- return true
- -- If the passed message file path is not valid, print the proper syntax of the program and return a failure.
- else
- printUsage()
- return false
- end
- -- If the passed ID is not valid, print the proper syntax of the program and return a failure.
- else
- printUsage()
- return false
- end
- -- If the arguments are not enough or are invalid, print the proper usage of the program and return a failure.
- else
- printUsage()
- return false
- end
- end
- -- Make sure all of the arguments are valid for the program to execute properly.
- if checkArguments() then
- -- Open any modem that exists on the computer.
- openExistantModem()
- -- Attempt to send the mail requested from the passed file.
- -- tArgs[1] is the receiver id, and tArgs[2] is the file path for the message that will need to be read, then sent.
- if sendMail(tonumber(tArgs[1]), tArgs[2]) then
- print("Mail sent!")
- else
- print("Send failure.")
- end
- -- If the argument checks are not valid, then DO NOT attempt to run the program.
- else
- return -- Escape the program. (Programs are run as functions underneath the shell, so we can return from them using a retunr call
- -- in the global scope.)
- end
- -- SUPERFLOUS METHOD: MAIL IS READ FROM A PRE-EXISTING FILE; DOES NOT NEED TO HAVE ITS OWN METHOD TO COMPOSE MAIL.
- --[[
- -- Runs the edit program on a temporary file and then sends the contents of the message to the desired computer.
- function composeMail(nReceiver)
- if type(nReceiver) == "number" or tonumber(nReceiver) then
- -- Make sure that the temporary file doesn't exist. If it does, then delete it.
- if fs.exsists(".tmpFile") then
- fs.delete(".tmpFile")
- end
- -- Run the 'edit' program for the user to enter their message.
- shell.run("edit", ".tmpFile")
- -- Send the message.
- if sendMail(nReceiver, ".tmpFile") then
- print("Message sent to " .. nReceiver .. '!')
- else
- print("Message send failure.")
- end
- return
- else
- error("Receipient ID must be a positive intenger.")
- end
- end
- --]]
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement