Advertisement
Guest User

Untitled

a guest
Jan 12th, 2019
650
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.22 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 =
  19. {
  20. ["TO"] = "",
  21. ["FROM"] = "",
  22. ["SUB"] = "",
  23. ["MSG"] = "",
  24. ["TS"] = 0
  25. }
  26. -- Data is stored as {USERNAME,{TO,FROM,SUB,MSG,TS}, ...}}
  27. -- where USERNAME is the KV key and TS is the key for the list of all emails attatched to a user.
  28.  
  29. -- Clear Screen
  30. function resetTerm()
  31. term.clear()
  32. term.setBackgroundColor(colors.white)
  33. term.setTextColor(colors.blue)
  34. term.setCursorPos(0,0)
  35. end
  36.  
  37. -- GET MAIL
  38. function getMail(username)
  39. print("retrieving mail...")
  40. kvMail = kvApi.deserializeKvResponse(kvApi.clientGet("kvMail"))
  41. mailbox = {}
  42. for index,mail in kvMail do
  43. if mail.to = username
  44. mailbox.insert(mail)
  45. end
  46. end
  47. return mailbox
  48. end
  49.  
  50. -- SEND MAIL
  51. function sendMail(uname, mail)
  52. kvMail = kvApi.deserializeKvResponse(kvApi.clientGet("kvMail"))
  53. kvMail.insert(mail)
  54. kvApi.clientStore("kvMail", textutils.serialize(kvMail))
  55. print("Mail Sent!")
  56. end
  57.  
  58. -- RETURNS BOOL, TRUE FOR AUTH SUCESS
  59. function login(user, pass)
  60. kvUsers = kvApi.deserializeKvResponse(kvApi.clientGet("kvUsers"))
  61. print(kvUsers)
  62. for index,data in pairs(kvUsers) do
  63. if data.username == user and data.password == pass then
  64. return true
  65. end
  66. end
  67. print("I'm afraid that is incorrect, Sir...")
  68. return false
  69. end
  70.  
  71. function printMail(mail)
  72. print(mail.from)
  73. print(mail.sub)
  74. print("\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/")
  75. print(mail.msg)
  76. print(" ")
  77. print(mail.ts)
  78. end
  79.  
  80. function createUser(user, pass)
  81. users = kvApi.deserializeKvResponse(kvApi.clientGet("kvUsers"))
  82. users.insert(user, pass)
  83. kvApi.clientStore("kvUsers", textutils.serialize(users))
  84. end
  85.  
  86. -- The Stupid Simple Mail Transfer Protocol was developed exclusivly in and for Computercraft 1.7
  87.  
  88. -- Networking
  89. if(not kvApi.initializeWirelessModems()) then
  90. print("no modem detected... can't connect to KV")
  91. exit(1)
  92. end
  93.  
  94. -- MAIN LOOP
  95. while true do
  96. print("Welcome to the SSMS!")
  97. print("Please select from the following...")
  98. print("(1) LOGIN")
  99. print("(2) REGISTER")
  100. input = io.read()
  101. print("USERNAME: ")
  102. username = io.read()
  103. print("PASSWORD: ")
  104. password = io.read()
  105. if input == 2 then
  106. if createUser(username, password) then
  107. term.clear()
  108. print("USER REGISTERED!")
  109. print("attempting to login now")
  110. else
  111. print("REGISTRATION ERROR")
  112. exit(1)
  113. end
  114. sleep(2)
  115. end
  116. authenticated = login(username, password)
  117. if authenticated then
  118. print("User Authenticated!")
  119. mailbox = getMail(username)
  120. else
  121. print("login failed... exiting()")
  122. exit(1)
  123. end
  124. resetTerm()
  125. print("Greeting, how may I serve you today...")
  126. print("(1) GET MAIL")
  127. print("(2) SEND MAIL")
  128. input = io.read()
  129. if input == 1 then
  130. totalMessages = (#mailbox)
  131. print("You have " .. totalMessages .. " Messages.")
  132. print("Which mail would you like to read" .. username)
  133. messageNumber = io.read()
  134. if ((messageNumber > totalMessages) or (messageNumber < 0)) then
  135. print "sorry sir, I didn't quite get that..."
  136. end
  137. for index,mail in pairs(mailbox) do
  138. if(index == messageNumber) then
  139. printMail(mail)
  140. end
  141. end
  142. end
  143. elseif (input == 2) then
  144. term.clear()
  145. print("TO: ")
  146. to = io.read()
  147. print("SUBJECT: ")
  148. sub = io.read()
  149. print("MESSAGE: ")
  150. msg = io.read()
  151. ts = (os.date() .. " " .. os.time())
  152. message = { TO=to, FROM=playerName, SUB=sub, MSG=msg, TS=ts }
  153. mailbox.insert(message)
  154. sendMail(username, mailbox)
  155. else
  156. resetTerm()
  157. end
  158. end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement