-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcode.py
More file actions
146 lines (129 loc) · 4.65 KB
/
code.py
File metadata and controls
146 lines (129 loc) · 4.65 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
class MailServer:
def __init__(self):
self.users = []
self.current_user = None
self.main_menu()
def register(self):
email = input("Email: ")
while email in [x.email for x in self.users]:
email = input("Email already exists, try another one: ")
while not email:
email = input("Email: ")
password = None
while not password:
password = input("Pasword: ")
user = User(email, password)
self.users.append(user)
print("\n\n########")
print("Account Created successfully !")
print("########")
self.main_menu()
def login(self):
broke = False
email, password = None, None
while not email:
email = input("Email: ")
while not password:
password = input("Password: ")
user = list(filter(lambda x: x.email == email and x.password == password, self.users))
while not user :
print("\nEmail or Password is wrong !\nTry again\n")
print("type 'exit' to go back to main menu")
email = input("Email: ")
if email == 'exit':
broke = True
break
password = input("Password: ")
user = list(filter(lambda x: x.email == email and x.password == password, self.users))
if broke:
self.main_menu()
else:
user = list(user)[0]
self.current_user = user
self.user_menu(True)
def get_user_by_email(self, email):
user = filter(lambda x: x.email == email, self.users)
return list(user) and list(user)[0] or None
def change_password(self, user, new_pass):
user.password = new_pass
print("\n####\nPassword updated successfully!\n#### ")
def send_new_email(self) :
dest = input("Destination email: ")
subject = input("Subject: ")
body = input("body: ")
email = Email(self.current_user, dest, subject, body)
self.current_user.sent.append(email)
dest_user = list(filter(lambda x: x.email == dest, self.users))
if dest_user:
dest_user[0].inbox.append(email)
print("\n####\nEmail sent successfully !\n#### ")
def view_inbox(self):
if not self.current_user.get_inbox():
print("###\nEmpty!!\n###")
for i, email in enumerate(self.current_user.get_inbox()):
print(f"{i+1}.")
print("From: ", email.sender)
print("Subject: ", email.subject)
print("Body: ", email.text)
print("------")
def view_sent(self):
if not self.current_user.get_sent_emails():
print("###\nEmpty!!\n###")
for i, email in enumerate(self.current_user.get_sent_emails()):
print(f"{i+1}.")
print("To: ", email.sender.email)
print("Subject: ", email.subject)
print("Body: ", email.text)
print("------")
def logout(self):
self.current_user = None
def main_menu(self):
print("\n\nEnter command:")
print("register --> Create new account")
print("login --> Log in to your account")
command = input(">")
if command == "register":
self.register()
elif command == "login":
self.login()
def user_menu(self, init=False):
if init:
print("\n\nWelcome " + self.current_user.email)
print("Enter command:")
print("send --> Send an email")
print("inbox --> Show your inbox")
print("sent --> Show your sent emails")
print("logout --> Log out of account")
command = input(">")
if command == "send":
self.send_new_email()
self.user_menu()
elif command == "inbox":
self.view_inbox()
self.user_menu()
elif command == "sent":
self.view_sent()
self.user_menu()
elif command == "logout":
self.logout()
self.main_menu()
class User:
def __init__(self, email, password):
self.email = email
self.password = password
self.inbox = []
self.sent = []
def get_inbox(self):
return self.inbox
def get_sent_emails(self):
return self.sent
class Email:
def __init__(self, sender, receiver, subject, text):
self.sender = sender
self.receiver = receiver
self.subject = subject
self.text = text
self.is_read = False
def read(self):
self.is_read = True
MailServer()