-
Notifications
You must be signed in to change notification settings - Fork 0
/
180010017_mailclient.py
88 lines (69 loc) · 2.69 KB
/
180010017_mailclient.py
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
import socket
import pickle
import argparse
import sys
from assets.core_modules.smtp.custom_mail_template import Mail
from assets.core_modules.databases.custom_database_handler import DatabaseHandler
from assets.core_modules.exeptions.custom_exeptions import WrongEmailFormat, EmptySubject, UserDoesNotExists
from assets.core_modules.clients.users import User
USER_OPTIONS = '''
1.Manage Mail: Shows the stored mails of logged in user only
2.Send Mail: Allows the user to send a mail
3.Quit: Quits the program
'''
def authenticate_user():
"""Collects username and password
Returns:
User: an object of 'User' class
"""
username = input('Enter Name: ')
password = input('Enter Password: ')
user = User(username, password)
while not user.is_authenticated:
print('Wrong Credentials ! Try again.\n')
username = input('Enter Name: ')
password = input('Enter Password: ')
user.update_credentials(username, password)
return user
def interact_with_user(ip, port):
"""Interacts with user providing them with various options to operate on
their mail box.
Args:
ip (Int): Ip address for the connection
port (Int): port number for the connection
Raises:
ValueError: If the user selects values different from given options
"""
user = authenticate_user()
while True:
print(USER_OPTIONS)
try:
response = int(input('Your Choice: '))
if response > 3 or response < 1:
raise ValueError
elif response == 1:
user.operate_on_inbox(ip=ip, port=port)
elif response == 2:
user.send_email(ip=ip, port=port)
else:
print(f'Process Terminating .....')
sys.exit(0)
except KeyboardInterrupt:
print('Use Given Options To Exit !!')
except ValueError:
print('Enter valid values')
except Exception as e:
print(f'Error {e} occured')
break
def Main():
# For easy parsing of command line arguments
parser = argparse.ArgumentParser(description='Command Line Argument Parser for client')
parser.add_argument('-ip', '--ip_address', type=str, help='IP Address to be connected to', required=True)
parser.add_argument('-p', '--port', type=int, help='Port of Server to be connected to', required=True)
# Extract arguments passed from user, and verify the arguments are as expected
args = parser.parse_args()
# if user doesn't enter a valid port number
# if user doesn't enter a valid ip address
interact_with_user(args.ip_address, args.port)
if __name__ == '__main__':
Main()