[fix] Handle incomming mms from mmsd

This commit is contained in:
Alex 2021-05-01 23:11:15 +02:00
parent f780e8273a
commit f032511cac

View file

@ -13,6 +13,7 @@ if sys.version_info[0] == 3 and sys.version_info[1] > 8:
import argparse import argparse
import configparser import configparser
import re
import time import time
import getpass import getpass
import socket import socket
@ -48,12 +49,14 @@ class Handler(FileSystemEventHandler):
def on_any_event(event): def on_any_event(event):
if event.is_directory: if event.is_directory:
return None return None
elif event.event_type == 'created' or event.event_type == 'modified':
elif event.event_type == 'created': if m.is_mmsd_mms_file(event.src_path):
if '.status' in event.src_path: print(f"New MMS found : {event.src_path}.", file=sys.stderr)
mms_path = event.src_path[:-7] m.convert(event.src_path)
print(f"New MMS found : {event.src_path}.") elif event.event_type == 'moved':
m.convert(mms_path) if m.is_mmsd_mms_file(event.dest_path):
print(f"New MMS found : {event.dest_path}.", file=sys.stderr)
m.convert(event.dest_path)
class MMS2Mail: class MMS2Mail:
@ -61,18 +64,23 @@ class MMS2Mail:
self.config = configparser.ConfigParser() self.config = configparser.ConfigParser()
self.config.read(f"{Path.home()}/.mms/modemmanager/mms2mail.ini") self.config.read(f"{Path.home()}/.mms/modemmanager/mms2mail.ini")
self.mailer = Mailer({'manager.use': 'immediate', 'transport.use': 'mbox', 'transport.file': self.config.get('mail','mailbox', fallback=f"/var/mail/{getpass.getuser()}")}) self.mailer = Mailer({'manager.use': 'immediate', 'transport.use': 'mbox', 'transport.file': self.config.get('mail','mailbox', fallback=f"/var/mail/{getpass.getuser()}")})
self.pattern = re.compile('[0-9A-F]{40}$')
def convert(self, path): def convert(self, path):
print(path, file=sys.stderr)
self.mailer.start() self.mailer.start()
if Path(f"{path}.mail").is_file() and args.daemon:
print(f"Already converted MMS : doing nothing ({path})", file=sys.stderr)
return
status = configparser.ConfigParser() status = configparser.ConfigParser()
status.read_file(open(f"{path}.status")) status.read_file(open(f"{path}.status"))
if 'downloaded' in status['info']['state'] or 'received' in status['info']['state']: if status['info']['state'] == 'downloaded' or status['info']['state'] == 'received':
print(f"New incomming MMS : converting to Mail ({path})") print(f"New incomming MMS : converting to Mail ({path})", file=sys.stderr)
else: else:
print(f"New outgoing MMS : doing nothing ({path})") print(f"New outgoing MMS : doing nothing ({path})", file=sys.stderr)
return return
mms = MMSMessage.from_file(path)
mms = MMSMessage.from_file(path)
mms_from, mms_from_type = mms.headers['From'].split('/') mms_from, mms_from_type = mms.headers['From'].split('/')
mms_to, mms_to_type = mms.headers['To'].split('/') mms_to, mms_to_type = mms.headers['To'].split('/')
@ -81,8 +89,10 @@ class MMS2Mail:
message.date = mms.headers['Date'] message.date = mms.headers['Date']
message.headers = [('X-MMS-From', mms.headers['From']), ('X-MMS-To', mms.headers['To']), ('X-MMS-ID', mms.headers['Message-ID'])] message.headers = [('X-MMS-From', mms.headers['From']), ('X-MMS-To', mms.headers['To']), ('X-MMS-ID', mms.headers['Message-ID'])]
message.plain = f"MMS from {mms_from}" message.plain = f"MMS from {mms_from}"
if self.config.getboolean('mail','attach_mms', fallback=False): if self.config.getboolean('mail','attach_mms', fallback=False):
message.attach(path, None, None, None, False, "mms.bin") message.attach(path, None, None, None, False, "mms.bin")
for data_part in mms.data_parts: for data_part in mms.data_parts:
datacontent=data_part.headers['Content-Type'] datacontent=data_part.headers['Content-Type']
if datacontent is not None: if datacontent is not None:
@ -91,9 +101,13 @@ class MMS2Mail:
if 'Name' in datacontent[1]: if 'Name' in datacontent[1]:
filename = datacontent[1]['Name'] filename = datacontent[1]['Name']
message.attach(filename,data_part.data) message.attach(filename,data_part.data)
Path(f"{path}.mail").touch()
self.mailer.send(message) self.mailer.send(message)
self.mailer.stop() self.mailer.stop()
def is_mmsd_mms_file(self,path):
return self.pattern.search(path)
if __name__ == '__main__': if __name__ == '__main__':
parser = argparse.ArgumentParser() parser = argparse.ArgumentParser()
mode = parser.add_mutually_exclusive_group() mode = parser.add_mutually_exclusive_group()