-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathmail_inbound.py
More file actions
366 lines (302 loc) · 11.3 KB
/
mail_inbound.py
File metadata and controls
366 lines (302 loc) · 11.3 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
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
"""
This program is free software: you can redistribute it under the terms
of the GNU General Public License, v. 3.0. If a copy of the GNU General
Public License was not distributed with this file, see <https://www.gnu.org/licenses/>.
"""
import asyncio
import base64
import datetime
import imaplib
import re
import socket
import ssl
import struct
import time
import traceback
from concurrent.futures import ThreadPoolExecutor
import sentry_sdk
from email_reply_parser import EmailReplyParser
from imap_tools import (
AND,
MailBox,
MailboxLoginError,
MailboxLogoutError,
MailMessage,
)
from sms_outbound import (
QUEUEDROID_SUPPORTED_REGION_CODES,
get_phonenumber_region_code,
send_with_queuedroid,
send_with_twilio,
)
from translations import Localization
from utils import get_config_value, get_env_var, get_logger
from vault_grpc_client import authenticate_bridge_entity, encrypt_payload
IMAP_SERVER = get_env_var("BRIDGE_IMAP_SERVER", strict=True)
IMAP_PORT = int(get_env_var("BRIDGE_IMAP_PORT", 993))
IMAP_USERNAME = get_env_var("BRIDGE_IMAP_USERNAME", strict=True)
IMAP_PASSWORD = get_env_var("BRIDGE_IMAP_PASSWORD", strict=True)
MAIL_FOLDERS = [
folder.strip()
for folder in get_env_var("BRIDGE_IMAP_MAIL_FOLDER", "INBOX").split(",")
]
SSL_CERTIFICATE = get_env_var("SSL_CERTIFICATE_FILE", strict=True)
SSL_KEY = get_env_var("SSL_CERTIFICATE_KEY_FILE", strict=True)
MOCK_REPLY_SMS = get_env_var("MOCK_REPLY_SMS")
MOCK_REPLY_SMS = (
MOCK_REPLY_SMS.lower() == "true" if MOCK_REPLY_SMS is not None else False
)
ALIAS_PHONENUMBER_PREFIX = get_env_var("ALIAS_PHONE_NUMBER_PREFIX", "")
ALIAS_PHONENUMBER_SUFFIX = get_env_var("ALIAS_PHONE_NUMBER_SUFFIX", "")
SIMPLELOGIN_PRIMARY_DOMAIN = get_env_var("SL_PRIMARY_DOMAIN", "relaysms.me")
ALIAS_EMAIL_PATTERNS = re.compile(
get_config_value("patterns", "alias_email").format(
suffix=re.escape(ALIAS_PHONENUMBER_SUFFIX),
prefix=re.escape(ALIAS_PHONENUMBER_PREFIX),
domain=re.escape(SIMPLELOGIN_PRIMARY_DOMAIN),
)
)
SMS_REPLY_TEMPLATE = get_config_value("templates", "sms_reply")
logger = get_logger("mail.inbound")
loc = Localization()
t = loc.translate
sentry_sdk.init(
dsn=get_env_var("SENTRY_DSN"),
server_name="Bridge",
traces_sample_rate=float(
get_env_var("SENTRY_TRACES_SAMPLE_RATE", default_value=1.0)
),
profiles_sample_rate=float(
get_env_var("SENTRY_PROFILES_SAMPLE_RATE", default_value=1.0)
),
)
sentry_sdk.set_tag("project", "Bridge")
sentry_sdk.set_tag("service_name", "Bridge Service")
def authenticate_phonenumber(phone_number: str):
"""
Authenticate a phone number using the bridge entity.
Args:
phone_number (str): The phone number to authenticate.
"""
response, error = authenticate_bridge_entity(phone_number=phone_number)
if error:
return (None, f"{error.details()} -- {error.code()}")
return response, None
def delete_email(mailbox: MailBox, email_uid: int) -> None:
"""
Delete an email from the mailbox.
Args:
mailbox (MailBox): The mailbox object.
email_uid (int): The UID of the email to delete.
"""
try:
if email_uid:
mailbox.delete(email_uid)
logger.debug("Successfully deleted email UID: %s", email_uid)
except Exception as e:
logger.error("Error deleting email UID %s: %s", email_uid, e)
raise
def format_recipients(recipients: list) -> list[str]:
"""
Format recipients into readable strings.
Args:
recipients (list): List of recipient objects.
Returns:
list[str]: Formatted recipient strings.
"""
return [
f"{recipient.name} <{recipient.email}>" if recipient.name else recipient.email
for recipient in (recipients or [])
]
def process_incoming_email(mailbox: MailBox, email: MailMessage) -> None:
"""
Process an incoming email.
Args:
mailbox (MailBox): The mailbox object.
email (MailMessage): The email object to process.
"""
email_uid = email.uid
message_body = EmailReplyParser.parse_reply(email.text)
subject = email.subject
receivers = email.to
if not email.from_:
logger.warning("No valid 'From' found. Discarding email.")
delete_email(mailbox, email_uid)
return
if not receivers:
logger.warning("No valid 'To' found. Discarding email.")
delete_email(mailbox, email_uid)
return
sender = (
f"{email.from_values.name} <{email.from_values.email}>"
if email.from_values.name
else email.from_values.email
)
cc_recipients = ",".join(format_recipients(email.cc_values))
bcc_recipients = ",".join(format_recipients(email.bcc_values))
email_timestamp = email.date.timestamp()
phone_number = None
alias_address = None
for receiver in receivers:
match = ALIAS_EMAIL_PATTERNS.search(receiver)
if match:
phone_number = match.group(1)
alias_address = receiver
break
logger.debug("Invalid alias email: %s.", receiver)
if not phone_number:
logger.info("Invalid alias reply. Discarding message.")
delete_email(mailbox, email_uid)
return
e_164_phonenumber = f"+{phone_number}"
authenticated, auth_error = authenticate_phonenumber(e_164_phonenumber)
if not authenticated or not authenticated.success:
logger.error("Authentication failed: %s", auth_error or authenticated.message)
delete_email(mailbox, email_uid)
return
reply_payload = (
alias_address + sender + cc_recipients + bcc_recipients + subject + message_body
)
logger.debug("Constructed Payload: %s", reply_payload)
encrypted_payload, encryption_error = encrypt_payload(
phone_number=e_164_phonenumber, payload_plaintext=reply_payload
)
if encryption_error:
logger.error("Encryption failed: %s", encryption_error)
delete_email(mailbox, email_uid)
return
logger.debug("Encrypted Payload: %s", encrypted_payload.payload_ciphertext)
bridge_letter = b"e"
content_ciphertext = base64.b64decode(encrypted_payload.payload_ciphertext)
try:
loc.set_locale(authenticated.language)
except ValueError as e:
logger.error(e)
sms_payload = SMS_REPLY_TEMPLATE.format(
reply_prompt=t("sms_reply_prompt"),
payload=base64.b64encode(
bytes([len(alias_address)])
+ bytes([len(sender)])
+ bytes([len(cc_recipients)])
+ bytes([len(bcc_recipients)])
+ bytes([len(subject)])
+ struct.pack("<H", len(message_body))
+ struct.pack("<H", len(content_ciphertext))
+ bridge_letter
+ content_ciphertext
).decode("utf-8"),
timestamp=email_timestamp,
).replace("\\n", "\n")
region_code = get_phonenumber_region_code(e_164_phonenumber)
if MOCK_REPLY_SMS:
is_sent = True
sentry_sdk.capture_message(sms_payload, level="info")
elif region_code in QUEUEDROID_SUPPORTED_REGION_CODES:
is_sent = False
is_sent = send_with_queuedroid(e_164_phonenumber, message=sms_payload)
else:
is_sent = False
max_retries = 3
for attempt in range(1, max_retries + 1):
logger.info("Attempting to send SMS (attempt %d/%d)", attempt, max_retries)
is_sent = send_with_twilio(e_164_phonenumber, message=sms_payload)
if is_sent:
logger.info("SMS sent successfully on attempt %d", attempt)
break
logger.warning("SMS sending failed on attempt %d", attempt)
if attempt < max_retries:
logger.info("Retrying SMS sending...")
time.sleep(2)
else:
logger.error("All %d SMS sending attempts failed", max_retries)
if is_sent:
delete_email(mailbox, email_uid)
timestamp = datetime.datetime.now().strftime("%Y-%m-%d %H:%M:%S")
publish_alert = f"Successfully processed and sent reply at {timestamp}."
sentry_sdk.capture_message(publish_alert, level="info")
logger.info(publish_alert)
else:
delete_email(mailbox, email_uid)
timestamp = datetime.datetime.now().strftime("%Y-%m-%d %H:%M:%S")
failure_alert = (
f"Failed to send SMS after {max_retries} attempts. "
f"Email deleted at {timestamp}."
)
sentry_sdk.capture_message(failure_alert, level="warning")
logger.warning(failure_alert)
def check_folder(folder: str, ssl_context) -> None:
"""
Check a specific folder for emails.
Args:
folder (str): The folder name to check.
ssl_context: The SSL context for the connection.
"""
try:
with MailBox(IMAP_SERVER, IMAP_PORT, ssl_context=ssl_context).login(
IMAP_USERNAME, IMAP_PASSWORD
) as mailbox:
mailbox.folder.set(folder)
logger.debug("Checking folder: %s", folder)
for msg in mailbox.fetch(
criteria=AND(seen=False),
bulk=50,
mark_seen=False,
):
process_incoming_email(mailbox, msg)
except Exception as e:
logger.error("Error checking folder %s: %s", folder, e)
async def check_all_folders_async(ssl_context) -> None:
"""
Check all folders asynchronously.
Args:
ssl_context: The SSL context for the connection.
"""
loop = asyncio.get_event_loop()
with ThreadPoolExecutor(max_workers=len(MAIL_FOLDERS)) as executor:
tasks = [
loop.run_in_executor(executor, check_folder, folder, ssl_context)
for folder in MAIL_FOLDERS
]
await asyncio.gather(*tasks)
def main() -> None:
"""
Main function to run the email processing loop.
"""
ssl_context = ssl.create_default_context()
ssl_context.load_cert_chain(certfile=SSL_CERTIFICATE, keyfile=SSL_KEY)
while True:
connection_start_time = time.monotonic()
connection_live_time = 0.0
try:
with MailBox(IMAP_SERVER, IMAP_PORT, ssl_context=ssl_context).login(
IMAP_USERNAME, IMAP_PASSWORD
) as mailbox:
logger.info(
"Connected to mailbox %s on %s", IMAP_SERVER, time.asctime()
)
while connection_live_time < 29 * 60:
try:
responses = mailbox.idle.wait(timeout=20)
if responses:
logger.debug("IMAP IDLE responses: %s", responses)
asyncio.run(check_all_folders_async(ssl_context))
except KeyboardInterrupt:
logger.info("Exiting gracefully...")
return
connection_live_time = time.monotonic() - connection_start_time
except (
TimeoutError,
ConnectionError,
imaplib.IMAP4.abort,
MailboxLoginError,
MailboxLogoutError,
socket.herror,
socket.gaierror,
socket.timeout,
) as e:
logger.error("Error occurred: %s", e)
logger.error(traceback.format_exc())
logger.info("Reconnecting in a minute...")
time.sleep(60)
if __name__ == "__main__":
main()