Browse Source

SMTP: доставка в локальный ящик maildir

tags/2.0.0
Бородин Роман 1 month ago
parent
commit
a0775a5166
4 changed files with 20 additions and 8 deletions
  1. +1
    -6
      mbkmail/imap/fetch.py
  2. +7
    -2
      mbkmail/imap/mailstore.py
  3. +7
    -0
      mbkmail/smtp/delivery.py
  4. +5
    -0
      mbkmail/util.py

+ 1
- 6
mbkmail/imap/fetch.py View File

@@ -1,6 +1,6 @@
import re
from email.utils import getaddresses
from mbkmail.util import flags_d, FLAG_RECENT
from mbkmail.util import flags_d, FLAG_RECENT, crlf_bytes, crlf_str
from datetime import datetime

class UnknownKW(Exception): pass
@@ -26,11 +26,6 @@ async def part_payload(msg, part_n):
return None
return msg

async def crlf_str(string):
return '\r\n'.join(list(map(lambda x: x.strip('\r'), string.split('\n'))))
async def crlf_bytes(b_string):
return b'\r\n'.join(list(map(lambda x: x.strip(b'\r'), b_string.split(b'\n'))))

async def tool_b_params(message):
params = message.get_params()
if params is None:


+ 7
- 2
mbkmail/imap/mailstore.py View File

@@ -10,7 +10,7 @@ from contextlib import asynccontextmanager
import shutil
import asyncio
import re
from mbkmail.util import send_msg, FLAG_DELETED, FLAG_SEEN, flags_d
from mbkmail.util import send_msg, FLAG_DELETED, FLAG_SEEN, flags_d, crlf_bytes
import os
from . import search
from . import fetch
@@ -326,7 +326,7 @@ class TeguMaildir(Maildir):
class MBox:
selected = None
def __init__(self, user_email):
self.lp, self.dom = user_email.split('@')
self.lp, self.dom = user_email.lower().split('@')
path = pathlib.Path(cfg.mail_root) / self.dom / self.lp
path.parent.mkdir(parents=True, exist_ok=True)
self.inbox = TeguMaildir(path)
@@ -336,6 +336,11 @@ class MBox:
loop = asyncio.get_running_loop()
loop.run_until_complete(self.add_folder(folder))
self.subscription = TeguMailboxSubscription(path)
async def deliver(self, message_bytes):
# Здесь можно обрабатывать правила доставки.
msg = MaildirMessage(crlf_bytes(message_bytes))
msg.set_subdir('new')
await self.inbox.add_async(msg)
async def __check_name_(self, name):
if any(x in name for x in mbox_name_bad_chars):
raise MailboxInvalidName


+ 7
- 0
mbkmail/smtp/delivery.py View File

@@ -1,5 +1,6 @@
from smtplib import LMTP
from mbkmail.cfg import cfg
from mbkmail.imap.mailstore import MBox

ERR_DELIVERY_OK = 0
ERR_DELIVERY_FAILED = 1
@@ -12,4 +13,10 @@ async def deliver_msg(sender, user, msg):
lmtp.sendmail(sender, user, msg)
except:
return ERR_DELIVERY_FAILED
else:
try:
mb = MBox(user)
mb.deliver(msg)
except:
return ERR_DELIVERY_FAILED
return ERR_DELIVERY_OK

+ 5
- 0
mbkmail/util.py View File

@@ -29,6 +29,11 @@ flags_d = {

class CmdlineSyntaxError(Exception): pass

async def crlf_str(string):
return '\r\n'.join(list(map(lambda x: x.strip('\r'), string.split('\n'))))
async def crlf_bytes(b_string):
return b'\r\n'.join(list(map(lambda x: x.strip(b'\r'), b_string.split(b'\n'))))

async def send_msg(writer, msg):
writer.write(f'{msg}\r\n'.encode('utf-8'))
await writer.drain()


Loading…
Cancel
Save