Advertisement
CosmicCatnap

Untitled

Jan 8th, 2019
192
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 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(kvApi.initializeWirelessModems()) then
  55. echo "no modem detected... can't connect to KV"
  56. exit(1)
  57. else
  58. echo "Modem Detected!"
  59.  
  60. end
  61.  
  62. -- MAIN LOOP
  63. while true do
  64. echo "Welcome to the SSMS!"
  65. sleep (2)
  66. TERM.clear()
  67. echo "Please select from the following..."
  68. echo "(1) LOGIN"
  69. echo "(2) REGISTER"
  70. input = io.read("n")
  71. echo "USERNAME: "
  72. username = io.read("*l")
  73. echo "PASSWORD: "
  74. password = io.read("*l")
  75. if input == 2 then
  76. createUser(username, password)
  77. TERM.clear()
  78. echo "USER REGISTERED!"
  79. echo "attempting to login now..."
  80. sleep(2)
  81. end
  82.  
  83. mailbox = login(username, password)
  84. if not mailbox
  85. echo "login failed... exiting()"
  86. exit(1)
  87. end
  88. TERM.clear()
  89. echo "Greeting, how may I serve you today..."
  90. echo "(1) GET MAIL"
  91. echo "(2) SEND MAIL"
  92. input = io.read("*n")
  93. if input == 1 then
  94. totalMessages = (#mailbox)
  95. echo ("You have " .. totalMessages .. " Messages.")
  96. echo ("Which mail would you like to read" .. username)
  97. messageNumber = io.read("*n")
  98. if ((messageNumber > totalMessages) or (messageNumber < 0)) then
  99. echo "sorry sir, I didn't quite get that..."
  100. end
  101. i = 0
  102. for k,v in pairs(mailbox)
  103. i = i + 1
  104. if i = messageNumber then
  105. -- TODO: printMail()
  106. print(v)
  107. end
  108. end
  109. elseif (input == 2) then
  110. TERM.clear()
  111. echo "TO: "
  112. to = io.read()
  113. echo "SUBJECT: "
  114. sub = io.read()
  115. echo "MESSAGE: "
  116. msg = io.read()
  117. ts = (os.date() .. " " .. os.time())
  118. message = { TO=to, FROM=playerName, SUB=sub, MSG=msg, TS=ts }
  119. mailbox.insert(ts, message)
  120. updateMailbox(username, mailbox)
  121. else
  122. TERM.clear()
  123. end
  124. end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement