-
-
Notifications
You must be signed in to change notification settings - Fork 4
Expand file tree
/
Copy pathimap_scanner.py
More file actions
194 lines (153 loc) · 6.21 KB
/
Copy pathimap_scanner.py
File metadata and controls
194 lines (153 loc) · 6.21 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
import imaplib
import email
import email.header
import os
import logging
import time
import threading
import re
import settings
import status
from vars import unknown_dir
def get_imap_config():
return settings.loadConfig().get("imap", {})
def decode_header_value(value):
"""Decode potentially encoded email header value."""
parts = email.header.decode_header(value)
decoded = []
for part, charset in parts:
if isinstance(part, bytes):
decoded.append(part.decode(charset or "utf-8", errors="replace"))
else:
decoded.append(part)
return "".join(decoded)
def sanitize_filename(filename):
"""Remove or replace unsafe characters from filename."""
filename = decode_header_value(filename)
filename = re.sub(r'[^\w\s\-.]', '_', filename)
filename = filename.strip()
if not filename:
filename = "attachment"
if not filename.lower().endswith(".pdf"):
filename += ".pdf"
return filename
def unique_path(directory, filename):
"""Return a unique file path by appending a counter if the file already exists."""
base, ext = os.path.splitext(filename)
candidate = os.path.join(directory, filename)
counter = 1
while os.path.exists(candidate):
candidate = os.path.join(directory, f"{base}_{counter}{ext}")
counter += 1
return candidate
def scan_imap():
"""
Connect to the configured IMAP mailbox, download PDF attachments from
unseen emails directly into the unknown folder for manual review.
Returns the number of PDF files downloaded.
"""
imap_config = get_imap_config()
if not imap_config.get("enabled", False):
logging.info("IMAP scanning is disabled in config")
return 0
host = imap_config.get("host", "")
port = int(imap_config.get("port", 993))
use_ssl = imap_config.get("ssl", True)
username = imap_config.get("username", "")
password = imap_config.get("password", "")
folder = imap_config.get("folder", "INBOX")
processed_folder = imap_config.get("processed_folder", "")
delete_after = imap_config.get("delete_after_import", False)
if not host or not username or not password:
logging.error("IMAP config incomplete: host, username and password are required")
return 0
downloaded = 0
try:
logging.info(f"IMAP: Connecting to {host}:{port} (SSL={use_ssl})")
if use_ssl:
mail = imaplib.IMAP4_SSL(host, port)
else:
mail = imaplib.IMAP4(host, port)
mail.login(username, password)
status, _ = mail.select(folder)
if status != "OK":
logging.error(f"IMAP: Could not select folder '{folder}'")
mail.logout()
return 0
status, messages = mail.search(None, "UNSEEN")
if status != "OK":
logging.error("IMAP: Search for unseen messages failed")
mail.logout()
return 0
email_ids = [eid for eid in messages[0].split() if eid]
logging.info(f"IMAP: Found {len(email_ids)} unseen message(s)")
for email_id in email_ids:
try:
status, msg_data = mail.fetch(email_id, "(RFC822)")
if status != "OK" or not msg_data or msg_data[0] is None:
continue
msg = email.message_from_bytes(msg_data[0][1])
has_pdf = False
for part in msg.walk():
content_type = part.get_content_type()
content_disposition = part.get_content_disposition() or ""
filename = part.get_filename()
is_pdf = (
content_type == "application/pdf"
or (
"attachment" in content_disposition
and filename
and filename.lower().endswith(".pdf")
)
)
if not is_pdf or not filename:
continue
safe_name = sanitize_filename(filename)
dest_path = unique_path(unknown_dir, safe_name)
payload = part.get_payload(decode=True)
if not payload:
logging.warning(f"IMAP: Empty payload for attachment '{filename}', skipping")
continue
with open(dest_path, "wb") as f:
f.write(payload)
logging.info(f"IMAP: Saved attachment '{safe_name}' to {dest_path}")
has_pdf = True
downloaded += 1
if has_pdf:
if delete_after:
mail.store(email_id, "+FLAGS", "\\Deleted")
else:
if processed_folder:
try:
mail.copy(email_id, processed_folder)
except Exception as copy_err:
logging.warning(f"IMAP: Could not copy to '{processed_folder}': {copy_err}")
mail.store(email_id, "+FLAGS", "\\Seen")
except Exception as e:
logging.error(f"IMAP: Error processing message {email_id}: {e}")
if delete_after:
mail.expunge()
mail.logout()
except Exception as e:
logging.error(f"IMAP: Connection error: {e}")
return 0
if downloaded > 0:
logging.info(f"IMAP: Downloaded {downloaded} PDF(s) to unknown folder")
status.set_update_needed(True)
return downloaded
def _imap_cron():
"""Background thread that periodically scans the IMAP mailbox."""
while True:
imap_config = get_imap_config()
interval = float(imap_config.get("scan_interval", 300))
if imap_config.get("enabled", False):
try:
count = scan_imap()
if count:
logging.info(f"IMAP cron: processed {count} PDF(s)")
except Exception as e:
logging.exception(f"IMAP cron error: {e}")
time.sleep(interval)
# Start background thread on module import
_imap_thread = threading.Thread(target=_imap_cron, daemon=True)
_imap_thread.start()