Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- -- NOTE this system is build on the KV storage system and requires its API to run
- os.loadAPI("M3C/utils")
- os.loadAPI("M3C/kvApi")
- -- -- -- STUPID SIMPLE MAIL SYSTEM -- -- --
- -- -- ------S__S---M__S-------- -- --
- -- --<<</___/>_<\___\>>>---- --
- --~~~^^^^~~~~~^^^^~~~~~--
- -- A Simple Implementation of the Stupid Simple Mail Transfer Protocol (SSMTP) Server Daemon
- -- SSMTP is just a length(5) key value pair (a table in lua) containing the following key names in ALL CAPS
- -- TO: uname(Case Sensitive)
- -- FROM: yourusername (Also Case Sensitive)
- -- SUB: subjectgoeshere
- -- MSG: (Message/Body/Data etc)
- -- TS: (timestamp)
- SSMTP =
- {
- ["TO"] = "",
- ["FROM"] = "",
- ["SUB"] = "",
- ["MSG"] = "",
- ["TS"] = 0
- }
- -- Data is stored as {USERNAME,{TO,FROM,SUB,MSG,TS}, ...}}
- -- where USERNAME is the KV key and TS is the key for the list of all emails attatched to a user.
- -- Clear Screen
- function resetTerm()
- term.clear()
- term.setBackgroundColor(colors.white)
- term.setTextColor(colors.blue)
- term.setCursorPos(0,0)
- end
- -- GET MAIL
- function getMail(username)
- print("retrieving mail...")
- kvMail = kvApi.deserializeKvResponse(kvApi.clientGet("kvMail"))
- mailbox = {}
- for index,mail in kvMail do
- if mail.to = username
- mailbox.insert(mail)
- end
- end
- return mailbox
- end
- -- SEND MAIL
- function sendMail(uname, mail)
- kvMail = kvApi.deserializeKvResponse(kvApi.clientGet("kvMail"))
- kvMail.insert(mail)
- kvApi.clientStore("kvMail", textutils.serialize(kvMail))
- print("Mail Sent!")
- end
- -- RETURNS BOOL, TRUE FOR AUTH SUCESS
- function login(user, pass)
- kvUsers = kvApi.deserializeKvResponse(kvApi.clientGet("kvUsers"))
- print(kvUsers)
- for index,data in pairs(kvUsers) do
- if data.username == user and data.password == pass then
- return true
- end
- end
- print("I'm afraid that is incorrect, Sir...")
- return false
- end
- function printMail(mail)
- print(mail.from)
- print(mail.sub)
- print("\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/")
- print(mail.msg)
- print(" ")
- print(mail.ts)
- end
- function createUser(user, pass)
- users = kvApi.deserializeKvResponse(kvApi.clientGet("kvUsers"))
- users.insert(user, pass)
- kvApi.clientStore("kvUsers", textutils.serialize(users))
- end
- -- The Stupid Simple Mail Transfer Protocol was developed exclusivly in and for Computercraft 1.7
- -- Networking
- if(not kvApi.initializeWirelessModems()) then
- print("no modem detected... can't connect to KV")
- exit(1)
- end
- -- MAIN LOOP
- while true do
- print("Welcome to the SSMS!")
- print("Please select from the following...")
- print("(1) LOGIN")
- print("(2) REGISTER")
- input = io.read()
- print("USERNAME: ")
- username = io.read()
- print("PASSWORD: ")
- password = io.read()
- if input == 2 then
- if createUser(username, password) then
- term.clear()
- print("USER REGISTERED!")
- print("attempting to login now")
- else
- print("REGISTRATION ERROR")
- exit(1)
- end
- sleep(2)
- end
- authenticated = login(username, password)
- if authenticated then
- print("User Authenticated!")
- mailbox = getMail(username)
- else
- print("login failed... exiting()")
- exit(1)
- end
- resetTerm()
- print("Greeting, how may I serve you today...")
- print("(1) GET MAIL")
- print("(2) SEND MAIL")
- input = io.read()
- if input == 1 then
- totalMessages = (#mailbox)
- print("You have " .. totalMessages .. " Messages.")
- print("Which mail would you like to read" .. username)
- messageNumber = io.read()
- if ((messageNumber > totalMessages) or (messageNumber < 0)) then
- print "sorry sir, I didn't quite get that..."
- end
- for index,mail in pairs(mailbox) do
- if(index == messageNumber) then
- printMail(mail)
- end
- end
- end
- elseif (input == 2) then
- term.clear()
- print("TO: ")
- to = io.read()
- print("SUBJECT: ")
- sub = io.read()
- print("MESSAGE: ")
- msg = io.read()
- ts = (os.date() .. " " .. os.time())
- message = { TO=to, FROM=playerName, SUB=sub, MSG=msg, TS=ts }
- mailbox.insert(message)
- sendMail(username, mailbox)
- else
- resetTerm()
- end
- end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement