-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.py
440 lines (401 loc) · 14.2 KB
/
main.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
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
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
#!/usr/bin/env python3
import argparse
import socket
import getpass
import os
import email_parser
import pop3
def handle_args():
parser = argparse.ArgumentParser(usage="main.py\nFollow instructions")
parser.add_argument('--config', '-c', help='path to config file with login,'
' password and host')
parser.add_argument('--type', '-t', help='type of socket')
parser.add_argument('--host', '-ho', help='host')
parser.add_argument('--user', '-u', help='email address')
parser.add_argument('--pswd', '-p', help='password')
parser.add_argument('--save', '-s', action='store_true', help=
'true/false argument for saving attachments '
'and text of message')
return parser.parse_args()
def handle_msgs(current, res, save):
try:
for msg in res:
if msg == -1:
return -1
if not msg:
print("something went wrong")
continue
if "-ERR" in msg or "no such message" in msg:
print("No such message")
return -2
current += 1
try:
# print(msg)
print(email_parser.Parser(msg, current, save))
except UnicodeEncodeError:
print("I can't print this message, cuz fcking retard "
"console doesn't support these characters")
except UnicodeDecodeError:
# print(e)
# import sys, traceback
# print(traceback.print_tb(sys.exc_info()[2]))
# print(sys.exc_traceback)
# traceback.print_stack()
print("INCORRECT ENCODING IN MESSAGE")
def get_host_value(args, get_host, user):
if not args.host:
host = input("Please enter domain your email or grant "
"this built-in function:\n")
if not host:
while "@" not in user:
print('incorrect username. Enter correct username plz')
user = input()
host = get_host(user)
else:
host = args.host
return host
def get_pswd_value(args):
pswd = ''
if not args.pswd:
while not pswd:
print("Please enter your password:\n")
pswd = getpass.getpass()
if pswd:
break
else:
pswd = args.pswd
return pswd
def get_user_value(args):
user = ''
while not args.user or user:
user = input('Please enter your login:\r\n')
if user:
break
else:
user = args.user
return user
def fill_commands_dict(change, change_save, commands, connect, get_stat,
get_subject_list, pop, list_, save_to):
commands['connect'] = connect
commands['get'] = pop.get_several_messages
commands['delete'] = pop.dele
commands['quit'] = pop.quit
commands['list'] = list_
commands['change'] = change
commands['saving'] = change_save
commands['stat'] = get_stat
commands['top'] = pop.top
commands['saveto'] = save_to
keys = list(commands.keys())
for key in keys:
commands[key.upper()] = commands[key]
def main():
def get_host(user):
return 'pop.'+user[user.index('@') + 1:]
args = handle_args()
CONFIG = ''
if args.config:
CONFIG = args.config
def get_value(line):
if len(line.split()) < 2:
return
return line.split()[1]
def get_info_from_config():
user = host = pswd = ''
if not os.path.exists(CONFIG):
user, pswd, host = get_info()
else:
with open(CONFIG, 'r') as config_file:
for line in config_file:
if line.upper().startswith('USER'):
user = get_value(line)
if not user:
user = get_user_value(args)
if line.upper().startswith('PSWD'):
pswd = get_value(line)
if not pswd:
pswd = get_pswd_value(args)
if line.upper().startswith('HOST'):
host = get_value(line)
if not host:
host = get_host_value(args, get_host, user)
if not user:
user = get_user_value(args)
if not pswd:
pswd = get_pswd_value(args)
if not host:
host = get_host_value(args, get_host, user)
return user, pswd, host
def get_info():
user = get_user_value(args)
pswd = get_pswd_value(args)
host = get_host_value(args, get_host, user)
return user, pswd, host
if args.config:
user, pswd, host = get_info_from_config()
else:
user, pswd, host = get_info()
save = False
if args.save:
save = True
type_ = args.type
pop = pop3.POP3(host, type_=type_)
commands = {}
def connect():
nonlocal host, user, pswd
pop.host = host
created = pop.create_socket()
while created == -1:
print('incorrect host')
user, pswd, host = change()
pop.host = host
created = pop.create_socket()
ans = pop.user(user)
ps = pop.pass_(pswd)
ans += ps
stat = pop.stat()
if '-ERR' in ans or stat == -1 or stat == (-1, -1):
return -1
def change():
args.host = ''
args.user = ''
args.pswd = ''
return get_info()
def save_to(message_num, address):
msg = next(commands['get'](message_num, message_num))
if msg == -1:
return -1
if not msg or (msg.startswith('-ERR') and 'no such' in msg):
print('No such message')
return
if not os.path.exists(address):
os.mkdir(address)
parsed_message = email_parser.Parser(msg, message_num)
if not address.endswith('/'):
address += '/'
with open(address + str(message_num) + '.txt', 'w') as tfile:
tfile.write(msg)
for att in parsed_message.attachments:
att.save_attachment(address)
def get_subject_list(*args):
if not args:
current_ind = 1
while True:
msg = commands['top'](current_ind)
if msg == -1:
print('Connection is lost')
yield -1
return
if not msg or '-ERR' in msg or 'no such message' in msg:
# print('No such message')
yield -2
break
try:
info = dict()
email_parser.Parser.get_info(msg, info)
subj = (email_parser.Parser.get_decoded_string(
info['subject'], info=info)
if "subject" in info else "No subject")
yield (current_ind, subj)
except UnicodeDecodeError:
yield "Incorrect encoding in message"
current_ind += 1
elif len(args) == 1:
index = int(args[0])
msg = commands['top'](index)
if msg == -1:
yield -1
return
if not msg or '-ERR' in msg or 'no such message' in msg:
print('No such message')
yield -2
return
# break
try:
info = dict()
email_parser.Parser.get_info(msg, info)
subj = (email_parser.Parser.get_decoded_string(
info['subject'], info=info)
if "subject" in info else "No subject")
yield (index, subj)
except UnicodeDecodeError:
yield "Incorrect encoding in message"
else:
for i in range(int(args[0]), int(args[1]) + 1):
msg = commands['top'](i)
if msg == -1:
yield -1
return
if not msg or '-ERR' in msg or 'no such message' in msg:
print('No such message')
yield -2
break
try:
info = dict()
email_parser.Parser.get_info(msg, info)
subj = (email_parser.Parser.get_decoded_string(
info['subject'], info=info)
if "subject" in info else "No subject")
yield (i, subj)
except UnicodeDecodeError:
yield "Incorrect encoding in message"
def list_(*args):
msg = pop.list(*args)
if msg == -1:
return -1
if msg.startswith('-ERR') and 'no such' in msg:
print('No such message')
return
print(msg)
def get_stat():
num_messages, size_messages = pop.stat()
print("Messages:", num_messages)
print("Total size:", size_messages)
def change_save():
nonlocal save
print("state", save)
if save:
save = False
print("save emails and attachments are disabled")
else:
save = True
print("save emails and attachments are enabled")
fill_commands_dict(change, change_save, commands, connect, get_stat,
get_subject_list, pop, list_, save_to)
help_dict = dict()
help_dict['get'] = ("usage: get iLeft [iRight]"
"displays a message (the message list of the interval)")
def help_():
print("COMMANDS:")
for command in {x.lower() for x in commands}:
print(command)
help_()
commands['help'] = help_
connected = False
while True:
if not connected:
print("Connecting..")
while connect() == -1:
print("incorrect login/pswd/host")
print("try with the correct data")
user, pswd, host = change()
print("DONE.")
connected = True
line = input("Your command: ").split()
if not line:
continue
command = line[0]
if command.lower() == 'quit':
break
if command.lower() == 'change':
user, pswd, host = change()
connect()
connected = True
continue
if not connected and command != 'connect':
# print('The first command must be \"connect\"')
connect()
connected = True
continue
# if command.lower() == 'list':
if command.lower() == 'top':
subj_list = get_subject_list(*line[1:])
for subj in subj_list:
if subj == -1:
connected = False
continue
if subj == -2:
break
try:
print(subj[0], "Subject: ", subj[1])
except UnicodeEncodeError:
print("I can't print this subj, cuz fcking retard "
"console doesn't support these characters")
continue
if command.lower() not in commands:
print("INCORRECT COMMAND")
continue
if command.lower() == 'saveto':
if len(line[1:]) < 2 or len(line) > 3:
print('USE: saveto message_number path_to_save')
print('INCORRECT COMMAND')
continue
try:
msg_num = int(line[1:][0])
except ValueError:
print("INCORRECT COMMAND")
continue
addr = line[2]
res = commands['saveto'](msg_num, addr)
if res == -1:
connected = False
continue
continue
try:
command_args = tuple(int(arg) for arg in line[1:])
except ValueError:
print('INCORRECT COMMAND')
continue
if command.lower() == 'stat' and len(command_args) > 0:
print('INCORRECT COMMAND')
continue
if (command.lower() == 'get' or command.lower() == 'delete') and\
not len(command_args):
print('INCORRECT COMMAND')
continue
# if command == ''
if command.lower() == 'get' and len(command_args) == 1:
command_args += (command_args[0],)
if command.lower() == 'connect':
if connected:
continue
else:
try:
if connect() == -1:
connected = False
print('INCORRECT USERNAME/PASSWORD')
exit()
else:
connected = True
continue
except socket.gaierror:
print("INCORRECT HOST")
pop.host = input('plz enter correct host:\n')
connected = False
continue
if not pop.sock._closed:
if command != 'connect':
res = commands[command.lower()](*command_args)
if res == -1:
print("DISCONNECTED")
connected = False
continue
else:
print('try to restart')
commands['quit']()
if command.lower() == 'get':
current = command_args[0]
error = handle_msgs(current, res, save)
if error == -1:
connected = False
continue
if __name__ == "__main__":
# try:
main()
# except Exception as e:
# print(e.args)
# print("Уважаемые котики-наркотики, прошу вас перестать вводить всякую "
# "фигню")
# host = 'pop.e1.ru'
# user = '[email protected]'
# pswd = 'asdasds'
# pop = pop3.POP3(host)
# pop.create_socket()
# # print(pop._send_command("sasdd"))
# print(pop.user('[email protected]'))
# # print(pop.user('[email protected]'))
# # print(pop.user(user))
# # print(pop.pass_(pswd))
# print(pop.pass_('pop3test1'))
# print(repr(pop.retr(2)))
# # print(pop.retr(2))