-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathMFA_imap_Mail.py
More file actions
335 lines (272 loc) · 9.94 KB
/
MFA_imap_Mail.py
File metadata and controls
335 lines (272 loc) · 9.94 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
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
# MFA_imap_Mail.py
# Uses IMAP Push to watch for email arrival and parse MFA PINs.
# Updated with automatic color aging and expiry inside Idler.idle().
import imaplib2
import time
from threading import *
import email
import configparser
import ssl
import socket
import os
from html.parser import HTMLParser
import re
from datetime import datetime
from typing import List
import pytz
# Configurable delay and max timeout between reconnection attempts
RETRY_DELAY_SECONDS = 30
MAX_RETRIES = 20
# number of notifications shown
STACK_SIZE = 5
# minutes before a code expires
CODE_DURATION = 5
# ============================================================
# HTML Strip Utility
# ============================================================
class StripHTML(HTMLParser):
def __init__(self):
super().__init__()
self.result = []
def handle_data(self, data):
self.result.append(data)
def get_text(self):
return ''.join(self.result)
def strip_html(text):
remover = StripHTML()
remover.feed(text)
return remover.get_text()
# ============================================================
# Notification Object
# ============================================================
class Notification:
id: str
time: datetime
code: int
body: str
color: str
def __init__(self, id_=None, time_=None, code_=None, body_=None, color_="green"):
self.id = id_
self.time = time_
self.code = code_
self.body = body_
self.color = color_
# ============================================================
# FixedStack for Notifications
# ============================================================
class FixedStack:
stack: List[Notification]
def __init__(self, stack):
self.stack = []
def push(self, data: Notification):
self.stack.append(data)
if len(self.stack) > STACK_SIZE:
self.stack.reverse()
self.stack.pop()
self.stack.reverse()
def remove(self, notification):
return self.stack.remove(notification)
notificationStack = FixedStack([])
# ============================================================
# Idler (IMAP IDLE watcher)
# ============================================================
class Idler(object):
def __init__(self, conn):
self.thread = Thread(target=self.idle)
self.M = conn
self.event = Event()
def start(self):
self.thread.start()
def stop(self):
self.event.set()
def join(self):
self.thread.join()
def idle(self):
# Initial sync
self.dosync_wrapper()
# Loop forever
while True:
if self.event.is_set():
return
self.needsync = False
def callback(args):
if not self.event.is_set():
self.needsync = True
self.event.set()
# Start idle
self.M.idle(callback=callback)
# Wait for server OR timer-wake
self.event.wait()
# Clear flag for next idle
self.event.clear()
# Perform sync if triggered
if self.needsync:
self.dosync_wrapper()
# ====================================================
# AGE NOTIFICATIONS & EXPIRE OLD ONES
# Always runs, even without new emails
# ====================================================
now = datetime.now(pytz.timezone("US/Central"))
to_remove = []
for note in notificationStack.stack:
mins_old = (now - note.time).total_seconds() / 60
# Expire codes
if mins_old >= CODE_DURATION:
to_remove.append(note)
continue
# Color fractions
fraction = mins_old / CODE_DURATION
if fraction < 1/3:
note.color = "green"
elif fraction < 2/3:
note.color = "yellow"
else:
note.color = "blue"
# Remove expired notifications
for note in to_remove:
try:
notificationStack.remove(note)
except:
pass
# ============================================================
# Sync wrapper (handles disconnect)
# ============================================================
def dosync_wrapper(self):
try:
self.dosync2()
except (imaplib2.IMAP4.abort, imaplib2.IMAP4.error, socket.error) as conn_error:
print(f"Error occurred while fetching MFA code: {conn_error}")
print("Attempting to reconnect...")
try:
new_conn = connect_imap()
self.M = new_conn
global M
M = new_conn
try:
self.dosync2()
except Exception as e:
print("Secondary error during sync:", e)
except Exception as e:
print("Failed to reconnect:", e)
# ============================================================
# Primary email fetch/parser
# ============================================================
def dosync2(self):
time.sleep(.2)
resp_code, mails = M.search(None, 'FROM', '"Bambu Lab"')
if len(mails[0]) > 0:
dat = mails[0].decode().split()[-800:]
mail_id = dat[-1]
try:
resp_code, mail_data = M.fetch(mail_id, '(RFC822)')
message = email.message_from_bytes(mail_data[0][1]).as_string()
message = " ".join(strip_html(message).split())
except:
time.sleep(1)
return self.dosync2()
try:
body = re.search('below:([\\s\\S]*)This code', message).group()
body = str(body)
body= body.strip()
body = repr(body)
print(body)
codeStr = re.search(r"[0-9]{6}", body).group()
print(codeStr)
code = re.search("\\d{6}", codeStr).group()
print(code)
dateStr = re.search(
"Delivery-date: [A-Za-z]{3}, \\d{2} [A-Za-z]{3} \\d{4} \\d{2}:\\d{2}:\\d{2} -\\d{4}",
message
).group()
date = re.search(
"\\d{2} [A-Za-z]{3} \\d{4} \\d{2}:\\d{2}:\\d{2} -\\d{4}",
dateStr
).group()
t = time.strptime(date, "%d %b %Y %H:%M:%S %z")
localized_time = datetime(*t[:6], tzinfo=pytz.FixedOffset(t.tm_gmtoff // 60))
mins_old = (
datetime.now(pytz.timezone("US/Central")) - localized_time
).total_seconds() / 60
if mins_old < CODE_DURATION:
# Assign initial color
fraction = mins_old / CODE_DURATION
if fraction < 1/3:
color = "green"
elif fraction < 2/3:
color = "yellow"
else:
color = "blue"
notificationStack.push(
Notification(
id_=mail_id,
time_=localized_time,
code_=code,
body_=body,
color_=color
)
)
except:
pass
# ============================================================
# IMAP Connection
# ============================================================
def connect_imap():
global M
for attempt in range(MAX_RETRIES):
try:
new_M = imaplib2.IMAP4_SSL(HOST)
new_M.login(USERNAME, PASSWORD)
new_M.select(source_folder)
print("IMAP connection successful")
M = new_M
return new_M
except Exception as e:
print(f"Attempt {attempt + 1} failed: {e}")
if attempt < MAX_RETRIES - 1:
time.sleep(RETRY_DELAY_SECONDS)
else:
raise
# ============================================================
# Notification Pump for UI Process
# ============================================================
def pump_notifications(out_q):
while True:
try:
out_q.put(list(notificationStack.stack))
except Exception as e:
print("Pump error:", e)
time.sleep(0.5)
# ============================================================
# Start IMAP Loop
# ============================================================
def start_imap_loop():
global M, idler
print("Connecting to email server...")
M = connect_imap()
idler = Idler(M)
idler.start()
try:
while True:
time.sleep(1)
except KeyboardInterrupt:
print("Stopping IMAP loop...")
finally:
if idler:
idler.stop()
idler.join()
if M:
try: M.close()
except: pass
try: M.logout()
except: pass
# ============================================================
# Config (does NOT start execution on import)
# ============================================================
config = configparser.ConfigParser()
config.read('MFA_Mail.cfg')
HOST = config['DEFAULT']['HOST']
USERNAME = config['DEFAULT']['USER']
PASSWORD = config['DEFAULT']['PASS']
source_folder = "INBOX"
if __name__ == "__main__":
start_imap_loop()