Advertisement
Guest User

Untitled

a guest
Jun 22nd, 2017
51
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 10.92 KB | None | 0 0
  1. from spy_details import spy, Spy, ChatMessage, friends, STATUS_MESSAGES, \
  2. block_menu, friends_menu, menu_choices, block_list
  3. from steganography.steganography import Steganography
  4. from datetime import datetime
  5. import getpass
  6. from termcolor import colored, cprint
  7.  
  8. """ This program is used to send and receive secret messages """
  9. """ The default password for existing user(Mr. Gaur) is 'lessordinary """
  10.  
  11. begin = 'Press any key to open the application'
  12. any_key = raw_input(begin)
  13.  
  14. print 'The game is ON !!!'
  15.  
  16. # Asking the user to create a new account or continue as existing user
  17. question = 'Do you want to continue as ' + spy.salutation + ' ' + spy.name + ' (Y/N)? '
  18. existing = raw_input(question)
  19.  
  20. # To only receive input as 'Y' and 'N'
  21. while (existing.upper() != 'Y') and (existing.upper() != 'N'):
  22. print 'Invalid Input'
  23. existing = raw_input(question)
  24.  
  25. # Function to start chat of spy with respect to the credentials provided
  26.  
  27.  
  28. def start_chat(spy):
  29.  
  30. spy.name = spy.salutation + " " + spy.name
  31.  
  32. # Test age of spy to be less than 50 and greater than 12
  33. if spy.age > 12 and spy.age < 50:
  34. password_test = getpass.getpass(prompt='Please enter your password again to continue ')
  35.  
  36. # To check whether the password to the account is right or not
  37. while password_test != initial_password:
  38.  
  39. print('The password is incorrect..!!!')
  40. password_test = getpass.getpass(prompt='Please enter the right password to continue ')
  41.  
  42. else:
  43. print('\n')
  44.  
  45. print 'Authentication complete. \nWelcome! ' + spy.name + ' \nAge: ' \
  46. + str(spy.age) + ' \nRating: ' + str(spy.rating) + ' \nSpy Chat mai aapka swaagat hai'
  47.  
  48. show_menu = True
  49.  
  50. while show_menu:
  51. menu()
  52. else:
  53. print 'Sorry you are not of the correct age to be a spy'
  54.  
  55. # Function menu to take input from user for menu choices and act accordingly
  56.  
  57.  
  58. def menu():
  59. menu_choice = raw_input(menu_choices)
  60.  
  61. if len(menu_choice) > 0:
  62. menu_choice = int(menu_choice)
  63.  
  64. if menu_choice == 1:
  65. add_status()
  66. elif menu_choice == 2:
  67. friends_sub_menu()
  68. elif menu_choice == 3:
  69. send_message()
  70. elif menu_choice == 4:
  71. read_message()
  72. elif menu_choice == 5:
  73. read_chat_history()
  74. elif menu_choice == 6:
  75. print 'Application Closed'
  76. else:
  77. show_menu = False
  78.  
  79.  
  80. # Function add status to update status
  81.  
  82.  
  83. def add_status():
  84.  
  85. updated_status_message = None
  86.  
  87. if spy.current_status_message != None:
  88.  
  89. print 'Your current status message is %s \n' % spy.current_status_message
  90. else:
  91. print 'You don\'t have any status message currently \n'
  92.  
  93. default = raw_input("Do you want to select from the older status (y/n)? ")
  94.  
  95. if default.upper() == "N":
  96. new_status_message = raw_input("What status message do you want to set? ")
  97.  
  98. if len(new_status_message) > 0:
  99. STATUS_MESSAGES.append(new_status_message)
  100. updated_status_message = new_status_message
  101.  
  102. elif default.upper() == 'Y':
  103.  
  104. item_position = 1
  105.  
  106. for message in STATUS_MESSAGES:
  107. print '%d. %s' % (item_position, message)
  108. item_position = item_position + 1
  109.  
  110. message_selection = int(raw_input("\nChoose from the above messages "))
  111.  
  112. if len(STATUS_MESSAGES) >= message_selection:
  113. updated_status_message = STATUS_MESSAGES[message_selection - 1]
  114.  
  115. else:
  116. print 'The option you chose is not valid! Press either y or n.'
  117.  
  118. if updated_status_message:
  119. print 'Your updated status message is: %s' % updated_status_message
  120. else:
  121. print "You current don't have a status update"
  122.  
  123. return updated_status_message
  124.  
  125. # Function Friends Sub Menu to deal with Friends Settings
  126.  
  127.  
  128. def friends_sub_menu():
  129.  
  130. friends_menu_choice = raw_input(friends_menu)
  131.  
  132. if len(friends_menu_choice) > 0:
  133. friends_menu_choice = int(friends_menu_choice)
  134.  
  135. if friends_menu_choice == 1:
  136. number_of_friends = add_friend()
  137. print 'You have %d friends' % number_of_friends
  138.  
  139. elif friends_menu_choice == 2:
  140. remove_friend()
  141. elif friends_menu_choice == 3:
  142. block_submenu()
  143. elif friends_menu_choice == 4:
  144. menu()
  145. else:
  146. print 'Invalid Entry'
  147. friends_sub_menu()
  148.  
  149. # Function to select a friend to later apply various functions as needed
  150.  
  151.  
  152. def select_a_friend():
  153. item_number = 0
  154.  
  155. for friend in friends:
  156. print '%d. %s %s aged %d with rating %.2f is online' % (item_number + 1, friend.salutation, friend.name,
  157. friend.age, friend.rating)
  158. item_number = item_number + 1
  159.  
  160. friend_choice = raw_input("Choose from your friends")
  161.  
  162. # '-1' because indexing starts from zero in list
  163.  
  164. friend_choice_position = int(friend_choice) - 1
  165.  
  166. return friend_choice_position
  167.  
  168.  
  169. # Function to add a new friend
  170.  
  171.  
  172. def add_friend():
  173.  
  174. new_friend = Spy('', '', 0, 0.0, 0)
  175.  
  176. new_friend.name = raw_input("Please add your friend's name: ")
  177.  
  178. new_friend.salutation = raw_input("Are they Mr. or Ms.?: ")
  179.  
  180. # To only input salutation as 'Mr' or 'Ms'
  181.  
  182. while new_friend.salutation != 'Mr' and new_friend.salutation != 'Ms':
  183. print "Invalid Input" \
  184. "\nPlease enter either 'Mr' or 'Ms' (case sensitive)"
  185. new_friend.salutation = raw_input()
  186.  
  187. new_friend.age = int(raw_input("Age?"))
  188.  
  189. new_friend.rating = float(raw_input("Spy rating?"))
  190.  
  191. if len(new_friend.name) > 0 and new_friend.age > 12 and new_friend.rating >= spy.rating:
  192. friends.append(new_friend)
  193. print 'Friend Added!'
  194. else:
  195. print 'Sorry! Invalid entry. We can\'t add spy with the details you provided'
  196.  
  197. return len(friends)
  198.  
  199. # Function to remove friends you can't kill
  200.  
  201.  
  202. def remove_friend():
  203.  
  204. friend_choice = select_a_friend()
  205. friend_choice_position = int(friend_choice) - 1
  206.  
  207. # The selected friend is removed from the friend list permanently
  208.  
  209. del friends[friend_choice_position]
  210. print 'Friend removed successfully'
  211.  
  212. # Function block submenu created to block or unblock friends
  213.  
  214.  
  215. def block_submenu():
  216.  
  217. block_menu_choice = raw_input(block_menu)
  218.  
  219. if len(block_menu_choice) > 0:
  220. block_menu_choice = int(block_menu_choice)
  221.  
  222. if block_menu_choice == 1:
  223. block_friend()
  224. elif block_menu_choice == 2:
  225. unblock_friend()
  226. elif block_menu_choice == 3:
  227. friends_sub_menu()
  228. elif block_menu_choice == 4:
  229. menu()
  230. else:
  231. print 'Invalid Entry\n Please enter a value between 1-4'
  232. block_submenu()
  233.  
  234. # Function to block those annoying friends
  235.  
  236.  
  237. def block_friend():
  238. friend_choice = select_a_friend()
  239. friend_choice_position = int(friend_choice) - 1
  240.  
  241. # The selected friend is removed from the friend list
  242. # and appended to the block list
  243.  
  244. block_list.append(friends.pop(friend_choice_position))
  245. block_list.sort()
  246. print 'Friend blocked successfully'
  247.  
  248.  
  249. # Function to unblock a frenemy
  250.  
  251.  
  252. def unblock_friend():
  253. item_number = 0
  254.  
  255. for friend in block_list:
  256. print '%d. %s %s aged %d with rating %.2f is online' % \
  257. (item_number + 1, friend.salutation, friend.name, friend.age, friend.rating)
  258. item_number = item_number + 1
  259.  
  260. friend_choice = raw_input("Choose from your friends")
  261.  
  262. friend_choice_position = int(friend_choice) - 1
  263.  
  264. # The selected friend is removed from the block list
  265. # and appended to the friend list
  266.  
  267. friends.append(block_list.pop(friend_choice_position))
  268. friends.sort()
  269. print 'Friend un-blocked successfully'
  270.  
  271. # Send encoded messages to your friends
  272.  
  273.  
  274. def send_message():
  275.  
  276. friend_choice = select_a_friend()
  277.  
  278. original_image = raw_input("What is the name of the image?")
  279. output_path = "output.jpg"
  280. text = raw_input("What do you want to say? "
  281. "\nWarning: If length of text exceeds 100,"
  282. "\nyou will be removed\n")
  283.  
  284. # To make sure message field is not blank
  285.  
  286. while len(text) <= 0:
  287. text = raw_input("Message field can't be left blank. Please enter something\n")
  288. Steganography.encode(original_image, output_path, text)
  289.  
  290. # Removing the friend if message length exceeds 100
  291.  
  292. if len(text) > 100:
  293. del friends[friend_choice]
  294. print 'The friend has been removed for exceeding the text limit'
  295.  
  296. # How to behave when special messages are sent or received
  297.  
  298. if text == 'SOS':
  299. text = 'Bhai bacha le'
  300. elif text == 'MAYDAY':
  301. text = "We're screwed bro"
  302.  
  303. new_chat = ChatMessage(text, True)
  304.  
  305. friends[friend_choice].chats.append(new_chat)
  306.  
  307. chat_length = len(friends[friend_choice].chats)
  308.  
  309. # To provide the average length of word a spy has used
  310.  
  311. spy.total_length_text = spy.total_length_text + len(text)
  312. average_text_length = spy.total_length_text/chat_length
  313. print 'The average text length of this user is ' + str(average_text_length)
  314.  
  315. print "Your secret message image is ready!"
  316.  
  317. # Read decoded messages from your friends
  318.  
  319.  
  320. def read_message():
  321.  
  322. sender = select_a_friend()
  323.  
  324. output_path = raw_input("What is the name of the file?")
  325.  
  326. secret_text = Steganography.decode(output_path)
  327.  
  328. new_chat = ChatMessage(secret_text, False)
  329.  
  330. friends[sender].chats.append(new_chat)
  331.  
  332. print "Your secret message has been saved!"
  333.  
  334. # Read old chats and relish the memories
  335.  
  336.  
  337. def read_chat_history():
  338.  
  339. read_for = select_a_friend()
  340.  
  341. print '\n6'
  342.  
  343. for chat in friends[read_for].chats:
  344.  
  345. # Variables 'time' and 'color_name' created to provide colored outputs
  346.  
  347. time = colored(chat.time.strftime("%H:%M"), 'blue')
  348. color_name = colored(friends[read_for].name, 'red')
  349. if chat.sent_by_me:
  350. print '[%s] %s: %s' % (time, colored('You said', 'red'), chat.message)
  351. else:
  352. print '[%s] %s said: %s' % (time, color_name, chat.message)
  353.  
  354. # Initialize function start_chat(spy) if it's an existing user
  355.  
  356. if existing.upper() == "Y":
  357. initial_password = 'lessordinary'
  358. start_chat(spy)
  359. else:
  360.  
  361. # Ask for new account creation for new users
  362.  
  363. spy = Spy('', '', 0, 0.0)
  364.  
  365. spy.name = raw_input("Welcome to spy chat, you must tell me your spy name first: ")
  366.  
  367. if len(spy.name) > 0:
  368. spy.salutation = raw_input("Should I call you Mr. or Ms.?: ")
  369.  
  370. # To make sure salutation is either 'Mr' or 'Ms' only
  371.  
  372. while spy.salutation != 'Mr' and spy.salutation != 'Ms':
  373. print "Invalid Input" \
  374. "\nPlease enter either 'Mr' or 'Ms' (case sensitive)"
  375. spy.salutation = raw_input()
  376.  
  377. spy.age = int(raw_input("What is your age?"))
  378.  
  379. spy.rating = float(raw_input("What is your spy rating?"))
  380.  
  381. # Asking the new user to set a password for his account
  382.  
  383. initial_password = getpass.getpass(prompt='Enter a password for your SpyChat account ')
  384.  
  385. start_chat(spy)
  386. else:
  387. print 'Please add a valid spy name'
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement