Advertisement
Guest User

Untitled

a guest
Sep 19th, 2017
126
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 5.30 KB | None | 0 0
  1. import os
  2.  
  3.  
  4. class User:
  5.  
  6.  
  7. # Domain name for emails and location for inboxes
  8. global emailDomain
  9. emailDomain = "@test.local"
  10. global inboxLocation
  11. inboxLocation = "E:/Users/Dylan/Desktop/Inboxes/"
  12.  
  13.  
  14. # User Creation
  15. def __init__(self, firstName, lastName, password):
  16. self.firstName = firstName
  17. self.lastName = lastName
  18. self.userName = firstName[0] + lastName
  19. self.address = self.userName + emailDomain
  20. self.password = password
  21. self.inbox = inboxLocation + self.address
  22.  
  23. # Create user inbox if it doesn't exist
  24. if not os.path.exists(self.inbox):
  25. os.mkdir(self.inbox)
  26.  
  27.  
  28. # Creates a header at the top of every page with the menu title and username
  29. def getHeader(self, currentMenu):
  30. header = (currentMenu + " | " + self.userName + "n")
  31. print(header)
  32.  
  33.  
  34. # Display information about a user
  35. def getInfo(self):
  36. print("User Information for %sn" % self.address)
  37. print("First Name: %s" % self.firstName)
  38. print("Last Name: %s" % self.lastName)
  39. print("Email Address: %s" % self.address)
  40. print("Inbox Location: %sn" % self.inbox)
  41.  
  42. input("n[Enter to Continue...]n")
  43. self.mainMenu()
  44.  
  45.  
  46. # Create and send an email to a user's inbox
  47. def sendEmail(self):
  48. self.getHeader("New Email")
  49.  
  50. sender = self.address
  51. receiver = input("Sender Address: ")
  52. subject = input("Subject: ")
  53. body = input("Body:n")
  54.  
  55. # Create email file and store it in users inbox
  56. fileName = (inboxLocation + receiver + "/" + self.userName + " - " + subject + ".txt")
  57. emailFile = open(fileName, "w")
  58. emailContent = ("from: " + sender + "n" +
  59. "to: " + receiver + "n" +
  60. "subject: " + subject + "nn" +
  61. body)
  62. emailFile.write(emailContent)
  63. emailFile.close()
  64.  
  65. # Confirm that email was sent
  66. if os.path.isfile(fileName):
  67. print("Email Sent! Returning to main menu.nn")
  68. self.mainMenu()
  69. else:
  70. print("Email Transmission failure, please try again.")
  71. self.sendEmail()
  72.  
  73.  
  74. # Look through a list of emails in inbox, delete function to be added
  75. def readEmails(self):
  76. self.getHeader("Inbox")
  77.  
  78. # Iterate through inbox and assign numbers to each email for user selection
  79. print("Select an Email to readnnM) Main Menu")
  80. emailList = (os.listdir(self.inbox))
  81. num = 0
  82. for email in emailList:
  83. print(str(num) + ". " + email)
  84. num += 1
  85.  
  86. # Ensure user input is a both a number and within range
  87. readChoice = input("nn>>> ")
  88. try:
  89.  
  90. # Exit to email if selected
  91. if readChoice == "M" or readChoice == "m":
  92. self.mainMenu()
  93.  
  94. # Open selected email
  95. else:
  96. readChoice = int(readChoice)
  97. readEmail = open(self.inbox + "/" + emailList[readChoice], "r")
  98. print(readEmail.read())
  99. readEmail.close()
  100.  
  101. input("nn[Enter to Continue...]n")
  102. self.readEmails()
  103.  
  104. # Catch bad inputs
  105. except TypeError or IndexError:
  106. print("Please enter a valid number")
  107. self.readEmail()
  108.  
  109.  
  110. # Main menu method for moving in and out of menus
  111. def mainMenu(self):
  112. self.getHeader("Main Menu")
  113. choice = input("A) Send EmailnB) InboxnC) My ProfilenD) Logoutnn>>> ")
  114. if choice == "a" or choice == 'A':
  115. self.sendEmail()
  116. elif choice == "b" or choice == 'B':
  117. self.readEmails()
  118. elif choice == "c" or choice == 'C':
  119. self.getInfo()
  120. elif choice == "d" or choice == 'D':
  121. self.logout()
  122. else:
  123. print("Invalid option, try again")
  124. self.mainMenu()
  125.  
  126.  
  127. # Logout method for changing users
  128. def logout(self):
  129. self.getHeader("logout")
  130. logoutChoice = input("are you sure you would like to log out?nn>>> ")
  131. if logoutChoice == "Yes" or logoutChoice == "yes" or logoutChoice == "Y" or logoutChoice == "y":
  132. login()
  133.  
  134.  
  135. # Login method called immediately after accounts are created
  136. def login():
  137. print("Please enter your credentialsn")
  138. try:
  139. userEmail = input("Username: ")
  140. password = input("Password: ")
  141.  
  142. # Raises a NameError if user is not found
  143. userEmail = eval(userEmail)
  144.  
  145. if password == userEmail.password:
  146. print("Login successful, Welcome to your inbox", userEmail.address)
  147. userEmail.mainMenu()
  148.  
  149. # Raise NameError if password is incorrect
  150. else:
  151. raise NameError
  152.  
  153. # Handle NameError, inform user of mistake, restart login method
  154. except NameError:
  155. print("Username or password is incorrectnn")
  156. login()
  157.  
  158.  
  159. # Creates users on start of program, method to create users will be added.
  160. # Users will be stored in file with hashed passwords.
  161. def userCreation():
  162. global jblow
  163. global bblow
  164. jblow = User("joe", "blow", "pass123")
  165. strudel = User("sylvester", "trudel", "P@ssw0rd")
  166.  
  167.  
  168. # Main method, initializes the program
  169. def main():
  170. userCreation()
  171. login()
  172.  
  173.  
  174. # Run Main
  175. main()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement