Advertisement
Guest User

ssms

a guest
Jan 8th, 2019
164
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 3.37 KB | None | 0 0
  1.  
  2. -- NOTE this system is build on the KV storage system and requires its API to run
  3. os.loadAPI("M3C/utils")
  4. os.loadAPI("M3C/kvApi")
  5.  
  6. -- -- -- STUPID SIMPLE MAIL SYSTEM -- -- --
  7.    -- -- ------S__S---M__S-------- -- --
  8.       -- --<<</___/<<<\___\>>>---- --
  9.          --~~~^^^^~~~~~^^^^!~~~~~--
  10. -- A Simple Implementation of the Stupid Simple Mail Transfer Protocol (SSMTP) Server Daemon
  11. -- SSMTP is just a length(5) key value pair (a table in lua) containing the following key names in ALL CAPS
  12.  
  13. -- TO: uname(Case Sensitive)
  14. -- FROM: yourusername (Also Case Sensitive)
  15. -- SUB: subjectgoeshere
  16. -- MSG: (Message/Body/Data etc)
  17. -- TS: (timestamp)
  18. SSMTP = {TO = "", FROM = "", SUB = "", MSG = "", TS = 0}
  19. -- Data is stored as {UUID,{TO,FROM,SUB,MSG,TS}, ...}}
  20. -- where UUID is the KV key and TS is the key for the list of all emails attatched to a user.
  21.  
  22. -- GET (1) MAIL
  23. function getMail(key)
  24.   return kvApi.clientGet(key)
  25. end
  26.  
  27. -- SEND MAIL
  28. function updateMailbox(playerUUID, mail)
  29.   return kvApi.clientStore(playerUUID, mail)
  30. end
  31.  
  32. -- EITHER RETURNS FALSE OR RETURNS THE USERS MAILBOX
  33. function login(user, pass)
  34.     mailUsers = kvApi.clientGet("kvUsers")
  35.     for x,y in pairs(mailUsers) do
  36.         if x == user and y == pass then
  37.           username = user
  38.           return kvApi.clientGet(username)
  39.         end
  40.     end
  41.     echo "I'm afraid that is incorrect Sir..."
  42.     return false
  43. end
  44.  
  45. function createUser(user, pass)
  46.     users = kvApi.clientGet("kvUsers")
  47.     users.insert(user, pass)
  48.     kvApi.clientStore("kvUsers", mailUsers)
  49. end
  50.  
  51. -- The Stupid Simple Mail Transfer Protocol was developed exclusivly in and for Computercraft 1.7
  52.  
  53. -- Networking
  54. if(not kvApi.initializeWirelessModems()) then
  55.   echo ("no modem detected... can't connect to KV")
  56.   exit(1)
  57. end
  58.  
  59. -- MAIN LOOP
  60. while true do
  61.   print("Welcome to the SSMS!")
  62.   sleep (2)
  63.   term.clear()
  64.   print("Please select from the following...")
  65.   print("(1) LOGIN")
  66.   print("(2) REGISTER")
  67.   input = io.read()
  68.   print("USERNAME: ")
  69.   username = io.read()
  70.   print("PASSWORD: ")
  71.   password = io.read()
  72.   if input == 2 then
  73.     createUser(username, password)
  74.     TERM.clear()
  75.     print("USER REGISTERED!")
  76.     print("attempting to login now...")
  77.     sleep(2)
  78.   end
  79.  
  80.   mailbox = login(username, password)
  81.   if not mailbox then
  82.     print("login failed... exiting()")
  83.     exit(1)
  84.   end
  85.   term.clear()
  86.   print("Greeting, how may I serve you today...")
  87.   echo ("(1) GET MAIL")
  88.   echo ("(2) SEND MAIL")
  89.   input = io.read("*n")
  90.   if input == 1 then
  91.     totalMessages = (#mailbox)    
  92.     echo ("You have " .. totalMessages .. " Messages.")
  93.     echo ("Which mail would you like to read" .. username)
  94.     messageNumber = io.read("*n")
  95.     if ((messageNumber > totalMessages) or (messageNumber < 0)) then
  96.       echo "sorry sir, I didn't quite get that..."
  97.     end
  98.     i = 0
  99.     for k,v in pairs(mailbox) do
  100.       i = i + 1
  101.       if i == messageNumber then
  102.         -- TODO: printMail()
  103.         print(v)
  104.       end
  105.     end
  106.   elseif (input == 2) then
  107.     term.clear()
  108.     print("TO: ")
  109.     to = io.read()
  110.     print("SUBJECT: ")
  111.     sub = io.read()
  112.     print("MESSAGE: ")
  113.     msg = io.read()
  114.     ts = (os.date() .. " " .. os.time())
  115.     message = { TO=to, FROM=playerName, SUB=sub, MSG=msg, TS=ts }
  116.     mailbox.insert(ts, message)
  117.     updateMailbox(username, mailbox)
  118.   else
  119.     term.clear()
  120.   end
  121. end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement