Gorcupt

3-2

Mar 28th, 2022
27
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.50 KB | None | 0 0
  1. import os
  2. from termcolor import cprint
  3.  
  4. _help = False
  5. _user = ""
  6.  
  7.  
  8. def main():
  9. cprint('Введите help - для получения справки\n')
  10. db = loadPass()
  11. flagAuth = False
  12. while True:
  13. inp = input()
  14. x = inp.split()
  15. cprint('Введите help - для получения справки\n')
  16. print(inp, '\n')
  17. if x[0] == 'help':
  18. help_m()
  19. elif x[0] == 'auth':
  20. flagAuth = auth_m(db, x)
  21. elif x[0] == 'exit':
  22. cprint('До свидания!\n')
  23. break
  24. elif checkAuth(flagAuth):
  25. if x[0] == 'list':
  26. list_m()
  27. elif x[0] == 'read':
  28. read_m(x)
  29. elif x[0] == 'send':
  30. send_m(x)
  31. else:
  32. cprint('Ошибка: Неизвестная команда\n')
  33.  
  34.  
  35. def checkAuth(flag):
  36. if flag:
  37. return True
  38. else:
  39. if _help:
  40. help_m()
  41. cprint('Ошибка: Сначала авторизуйтесь\n', 'red')
  42. return False
  43.  
  44.  
  45. def help_m():
  46. global _help
  47. _help = True
  48. cprint('auth user pass - авторизоваться, user - логин, pass - пароль', 'yellow')
  49. cprint('list — показать список сообщений', 'yellow')
  50. cprint('read msg — вывести сообщение под номером msg', 'yellow')
  51. cprint('send user — ввод сообщения для пользователя', 'yellow')
  52. cprint('exit — выход\n', 'yellow')
  53.  
  54.  
  55. def list_m():
  56. if not _help:
  57. help_m()
  58. for file in os.listdir('.\\messages\\' + _user):
  59. if os.path.isfile(os.path.join('.\\messages\\' + _user, file)):
  60. print(file)
  61. print()
  62.  
  63.  
  64. def send_m(x):
  65. if not _help:
  66. help_m()
  67. path = '.\\messages\\' + x[1]
  68. count = len(os.listdir(path))
  69. theme = input("\nВведите тему письма: ")
  70. fileName = str(count + 1) + ' ' + theme
  71. file = open(path + '\\' + fileName + '.txt', 'w')
  72. file.write(input("\nВведите сообщение: "))
  73. file.close()
  74. print("\nСообщение отправлено.")
  75.  
  76.  
  77. def read_m(x):
  78. if not _help:
  79. help_m()
  80. for file in os.listdir('.\\messages\\' + _user):
  81. for split_file in file.split():
  82. if split_file[0] == x[1]:
  83. file_r = open('.\\messages\\' + _user + '\\' + file, 'r')
  84. print(file_r.readline())
  85.  
  86.  
  87. def loadPass():
  88. db = {}
  89. with open("pass.txt") as file:
  90. for line in file:
  91. key, value = line.split()
  92. db[key] = value
  93. return db
  94.  
  95.  
  96. def auth_m(db, x):
  97. global _help
  98. global _user
  99. if not _help:
  100. help_m()
  101. if len(x) < 3:
  102. cprint('Ошибка: Введите логин и пароль\n', 'red')
  103. return
  104. flagAuth = False
  105. for user in db:
  106. if user == x[1]:
  107. if db.get(user) == x[2]:
  108. cprint('Авторизация прошла успешно\n', 'blue')
  109. flagAuth = True
  110. _user = x[1]
  111. mkdir()
  112. break
  113. else:
  114. cprint('Ошибка: Неверный логин или пароль\n', 'red')
  115. return flagAuth
  116.  
  117.  
  118. def mkdir():
  119. global _user
  120. if _user != "":
  121. if not os.path.exists(".\\messages\\" + _user):
  122. os.mkdir(".\\messages\\" + _user)
  123.  
  124.  
  125. main()
  126.  
Advertisement
Add Comment
Please, Sign In to add comment