-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.py
More file actions
190 lines (163 loc) · 5.36 KB
/
main.py
File metadata and controls
190 lines (163 loc) · 5.36 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
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
import imaplib
import email
import os
import json
import sys
script_dir = os.path.dirname(os.path.abspath(__file__))
file_path = os.path.join(script_dir, "data.json")
with open(file_path, "r", encoding="utf-8") as f:
d = f.read()
data = json.loads(d)
class color:
PURPLE = '\033[95m'
BLUE = '\033[94m'
CYAN = '\033[96m'
GREEN = '\033[92m'
YELLOW = '\033[93m'
RED = '\033[91m'
END = '\033[0m'
BOLD = '\033[1m'
UNDERLINE = '\033[4m'
imap = imaplib.IMAP4_SSL("imap.gmail.com")
def print_line():
print("---------------------------")
tried = False
def login():
global tried
while True:
try:
imap.login(data["email"], password = data["imap_pass"])
return
except:
if tried == False:
if data["email"] == "" or data["imap_pass"] == "":
print(f"{color.RED}You have not entered your email credentials.{color.END}")
else:
print(f"{color.RED}Login Credentials are incorrect. Make sure you are using a gmail account and a valid app password (From https://myaccount.google.com/apppasswords).{color.END}")
else:
print(f"{color.RED}Login failed. Please try again.{color.END}")
while True:
mail = input("please Enter your Gmail adress: ")
if not mail.endswith("@gmail.com"):
print(f"{color.RED}The mail you just entered is invalid. Currently we support only Gmail acoounts.{color.END}")
else:
break
while True:
pw = input("Please enter your IMAP password: ")
if not pw:
print(f"{color.RED}Password cannot be empty.{color.END}")
else:
break
data["email"] = mail
data["imap_pass"] = pw
with open(file_path, "w", encoding="utf-8") as f:
json.dump(data, f, ensure_ascii=False, indent=4)
tried = True
login()
imap.select('"INBOX"')
status, messages = imap.search(None, 'ALL')
print_line()
def commands():
print("""Welcome to emailcli. Here are the commands:
c: Shows commands.
Enter: Show the current page again. Default is 1.
r: Reloads the pages
x: Next page
z: Previous page
q: exit the program
mpp <number>: Sets the number of mails will be shown per page.
Number: Go to specified page""")
print_line()
return
commands()
def await_input():
return input("Enter command:")
page = 1
def next_page():
global page
page += 1
def previous_page():
global page
page = max(1,page - 1)
def reload():
imap.select('"INBOX"')
status, messages = imap.search(None, 'ALL')
def space():
pass
def mails_per_page(input):
try:
input = input.split()
except:
print(f"{color.RED}Unexpected command. Example usage 'mpp 5'.{color.END}")
print_line()
return print_results()
try:
data["mails_per_page"] = int(input[1])
except:
print(f"{color.RED}Unexpected command. Example usage 'mpp 5'.{color.END}")
print_line()
return print_results()
with open(file_path, "w", encoding="utf-8") as f:
json.dump(data, f, ensure_ascii=False, indent=4)
def logout():
print("Logged Out!")
imap.logout()
print_line()
sys.exit()
actions = {
"x": next_page,
"z": previous_page,
"r": reload,
"q": logout,
"": space
}
def print_results():
global page
global status
global messages
input = await_input()
print_line()
if input[:3] == "mpp":
mails_per_page(input)
elif input == "c":
commands()
return print_results()
elif input in actions:
actions[input]()
else:
try:
page = int(input)
except:
print(f"{color.RED}'{input}' command not found.{color.END}")
print_line()
return print_results()
start = page * data["mails_per_page"] - (data["mails_per_page"] - 1)
end = page * data["mails_per_page"] + 1
try:
print(f"{color.GREEN}PAGE NUMBER: {page}{color.END}")
print_line()
for i in range(start, end):
num = messages[0].split()[::-1][i-1]
_, msg = imap.fetch(num, "(RFC822)")
message = email.message_from_bytes(msg[0][1])
subject_header = message['Subject']
decoded_subject = email.header.decode_header(subject_header)
subject = decoded_subject[0][0]
message_id = message['Message-ID']
gmail_link = f"https://mail.google.com/mail/u/0/#search/rfc822msgid:{message_id[1:-1]}"
if isinstance(subject, bytes):
subject = subject.decode("utf-8")
print(f"{color.PURPLE}Subject:{color.END}", color.YELLOW + subject+ color.END)
print(f"{color.UNDERLINE}From:{color.END}", message["From"])
print(f"{color.UNDERLINE}Date:{color.END}", message["Date"])
print(f"{color.UNDERLINE}Gmail Link:{color.END}", color.CYAN + gmail_link + color.END)
print_line()
except:
print(f"{color.RED}The number of page you just entered doesn't exist. Please provide another one!{color.END}")
print_line()
print_results()
print_line()
print_results()
print("Logged Out!")
imap.logout()
print_line()