2021-05-01 06:43:13 +00:00
|
|
|
#!/usr/bin/python3
|
2021-05-05 03:49:38 +00:00
|
|
|
"""An mms to mail converter for mmsd."""
|
|
|
|
# upstream bug dirty fix
|
|
|
|
# https://github.com/marrow/mailer/issues/87#issuecomment-689586587
|
2021-05-01 07:32:26 +00:00
|
|
|
import sys
|
|
|
|
if sys.version_info[0] == 3 and sys.version_info[1] > 7:
|
|
|
|
sys.modules["cgi.parse_qsl"] = None
|
2021-05-05 03:49:38 +00:00
|
|
|
# upstream bug dirty fix
|
|
|
|
# https://github.com/marrow/mailer/issues/87#issuecomment-713319548
|
2021-05-01 07:32:26 +00:00
|
|
|
import base64
|
|
|
|
if sys.version_info[0] == 3 and sys.version_info[1] > 8:
|
|
|
|
def encodestring(value):
|
2021-05-05 03:49:38 +00:00
|
|
|
"""
|
|
|
|
Encode string in base64.
|
|
|
|
|
|
|
|
:param value: the string to encode.
|
|
|
|
:type value: str
|
|
|
|
|
|
|
|
:rtype str
|
|
|
|
:return: the base64 encoded string
|
|
|
|
"""
|
2021-05-01 07:32:26 +00:00
|
|
|
return base64.b64encode(value)
|
|
|
|
base64.encodestring = encodestring
|
2021-05-05 03:49:38 +00:00
|
|
|
# end bugfix
|
2021-05-01 07:32:26 +00:00
|
|
|
|
2021-05-01 06:43:13 +00:00
|
|
|
import argparse
|
|
|
|
import configparser
|
|
|
|
import getpass
|
|
|
|
import socket
|
2021-05-06 20:12:10 +00:00
|
|
|
import mimetypes
|
2021-05-01 06:43:13 +00:00
|
|
|
from pathlib import Path
|
2021-05-04 10:58:23 +00:00
|
|
|
|
2021-05-01 06:43:13 +00:00
|
|
|
from messaging.mms.message import MMSMessage
|
|
|
|
from marrow.mailer import Mailer, Message
|
|
|
|
|
2021-05-05 03:49:38 +00:00
|
|
|
from gi.repository import GLib
|
2021-05-04 10:58:23 +00:00
|
|
|
import dbus
|
|
|
|
import dbus.mainloop.glib
|
2021-05-01 06:43:13 +00:00
|
|
|
|
2021-05-05 03:49:38 +00:00
|
|
|
|
2021-05-01 06:43:13 +00:00
|
|
|
class MMS2Mail:
|
2021-05-04 10:58:23 +00:00
|
|
|
"""
|
2021-05-05 03:49:38 +00:00
|
|
|
The class handling the conversion between MMS and mail format.
|
2021-05-01 06:43:13 +00:00
|
|
|
|
2021-05-04 10:58:23 +00:00
|
|
|
MMS support is provided by python-messaging
|
|
|
|
Mail support is provided by marrow.mailer
|
|
|
|
"""
|
2021-05-05 03:49:38 +00:00
|
|
|
|
2021-05-05 14:25:46 +00:00
|
|
|
def __init__(self, delete=False, force_read=False):
|
|
|
|
"""
|
|
|
|
Return class instance.
|
|
|
|
|
|
|
|
:param delete: delete MMS after conversion
|
|
|
|
:type delete: bool
|
|
|
|
|
|
|
|
:param force_read: force converting an already read MMS (batch mode)
|
|
|
|
:type force_read: bool
|
|
|
|
"""
|
|
|
|
self.delete = delete
|
|
|
|
self.force_read = force_read
|
2021-05-01 06:43:13 +00:00
|
|
|
self.config = configparser.ConfigParser()
|
|
|
|
self.config.read(f"{Path.home()}/.mms/modemmanager/mms2mail.ini")
|
2021-05-05 14:25:46 +00:00
|
|
|
self.attach_mms = self.config.getboolean('mail', 'attach_mms',
|
|
|
|
fallback=False)
|
2021-05-05 03:49:38 +00:00
|
|
|
self.domain = self.config.get('mail', 'domain',
|
|
|
|
fallback=socket.getfqdn())
|
|
|
|
self.user = self.config.get('mail', 'user', fallback=getpass.getuser())
|
|
|
|
mbox_file = self.config.get('mail', 'mailbox',
|
|
|
|
fallback=f"/var/mail/{self.user}")
|
|
|
|
self.mailer = Mailer({'manager.use': 'immediate',
|
|
|
|
'transport.use': 'mbox',
|
|
|
|
'transport.file': mbox_file})
|
2021-05-04 10:58:23 +00:00
|
|
|
dbus.mainloop.glib.DBusGMainLoop(set_as_default=True)
|
|
|
|
self.bus = dbus.SessionBus()
|
2021-05-01 06:43:13 +00:00
|
|
|
|
2021-05-04 10:58:23 +00:00
|
|
|
def get_bus(self):
|
|
|
|
"""
|
2021-05-05 03:49:38 +00:00
|
|
|
Return the DBus SessionBus.
|
2021-05-04 10:58:23 +00:00
|
|
|
|
|
|
|
:rtype dbus.SessionBus()
|
|
|
|
:return: an active SessionBus
|
|
|
|
"""
|
|
|
|
return self.bus
|
|
|
|
|
2021-05-05 03:49:38 +00:00
|
|
|
def mark_mms_read(self, dbus_path):
|
2021-05-04 10:58:23 +00:00
|
|
|
"""
|
2021-05-05 14:25:46 +00:00
|
|
|
Ask mmsd to mark the mms as read.
|
2021-05-04 10:58:23 +00:00
|
|
|
|
|
|
|
:param dbus_path: the mms dbus path
|
2021-05-05 03:49:38 +00:00
|
|
|
:type dbus_path: str
|
2021-05-04 10:58:23 +00:00
|
|
|
"""
|
2021-05-05 03:49:38 +00:00
|
|
|
message = dbus.Interface(self.bus.get_object('org.ofono.mms',
|
|
|
|
dbus_path),
|
|
|
|
'org.ofono.mms.Message')
|
2021-05-05 14:25:46 +00:00
|
|
|
print(f"Marking MMS as read {dbus_path}", file=sys.stderr)
|
2021-05-04 10:58:23 +00:00
|
|
|
message.MarkRead()
|
|
|
|
|
2021-05-05 14:25:46 +00:00
|
|
|
def delete_mms(self, dbus_path):
|
2021-05-04 10:58:23 +00:00
|
|
|
"""
|
2021-05-05 14:25:46 +00:00
|
|
|
Ask mmsd to delete the mms.
|
2021-05-05 03:49:38 +00:00
|
|
|
|
2021-05-04 10:58:23 +00:00
|
|
|
:param dbus_path: the mms dbus path
|
2021-05-05 03:49:38 +00:00
|
|
|
:type dbus_path: str
|
2021-05-04 10:58:23 +00:00
|
|
|
"""
|
2021-05-05 14:25:46 +00:00
|
|
|
message = dbus.Interface(self.bus.get_object('org.ofono.mms',
|
|
|
|
dbus_path),
|
|
|
|
'org.ofono.mms.Message')
|
|
|
|
print(f"Deleting MMS {dbus_path}", file=sys.stderr)
|
|
|
|
message.Delete()
|
|
|
|
|
|
|
|
def check_mms(self, path):
|
|
|
|
"""
|
|
|
|
Check wether the provided file would be converted.
|
|
|
|
|
|
|
|
:param path: the mms filesystem path
|
|
|
|
:type path: str
|
|
|
|
:rtype bool
|
|
|
|
"""
|
|
|
|
# Check for mmsd data file
|
2021-05-04 10:58:23 +00:00
|
|
|
if not Path(f"{path}").is_file():
|
2021-05-05 03:49:38 +00:00
|
|
|
print("MMS file not found : aborting", file=sys.stderr)
|
2021-05-05 14:25:46 +00:00
|
|
|
return False
|
2021-05-04 10:58:23 +00:00
|
|
|
# Check for mmsd status file
|
2021-05-01 06:43:13 +00:00
|
|
|
status = configparser.ConfigParser()
|
2021-05-04 10:58:23 +00:00
|
|
|
if not Path(f"{path}.status").is_file():
|
2021-05-05 03:49:38 +00:00
|
|
|
print("MMS status file not found : aborting", file=sys.stderr)
|
2021-05-05 14:25:46 +00:00
|
|
|
return False
|
2021-05-01 06:43:13 +00:00
|
|
|
status.read_file(open(f"{path}.status"))
|
2021-05-04 10:58:23 +00:00
|
|
|
# Allow only incoming MMS for the time beeing
|
2021-05-05 14:25:46 +00:00
|
|
|
if not (status['info']['state'] == 'downloaded' or
|
2021-05-05 03:49:38 +00:00
|
|
|
status['info']['state'] == 'received'):
|
2021-05-05 14:25:46 +00:00
|
|
|
print("Outgoing MMS : aborting", file=sys.stderr)
|
|
|
|
return False
|
|
|
|
if not (self.force_read or not status.getboolean('info', 'read')):
|
|
|
|
print("Already converted MMS : aborting", file=sys.stderr)
|
|
|
|
return False
|
|
|
|
return True
|
|
|
|
|
|
|
|
def convert(self, path, dbus_path=None):
|
|
|
|
"""
|
|
|
|
Convert a provided mms file to a mail stored in a mbox.
|
|
|
|
|
|
|
|
:param path: the mms filesystem path
|
|
|
|
:type path: str
|
|
|
|
|
|
|
|
:param dbus_path: the mms dbus path
|
|
|
|
:type dbus_path: str
|
|
|
|
"""
|
|
|
|
# Check if the provided file present
|
|
|
|
if not self.check_mms(path):
|
|
|
|
print("MMS file not convertible.", file=sys.stderr)
|
2021-05-01 06:43:13 +00:00
|
|
|
return
|
2021-05-05 14:25:46 +00:00
|
|
|
# Generate its dbus path, for future operation (mark as read, delete)
|
|
|
|
if not dbus_path:
|
|
|
|
dbus_path = f"/org/ofono/mms/modemmanager/{path.split('/')[-1]}"
|
2021-05-01 06:43:13 +00:00
|
|
|
|
2021-05-01 21:11:15 +00:00
|
|
|
mms = MMSMessage.from_file(path)
|
2021-05-05 03:49:38 +00:00
|
|
|
|
2021-05-04 10:58:23 +00:00
|
|
|
self.mailer.start()
|
|
|
|
message = Message()
|
2021-05-01 06:43:13 +00:00
|
|
|
|
2021-05-04 10:58:23 +00:00
|
|
|
# Generate Mail Headers
|
2021-05-05 03:49:38 +00:00
|
|
|
mms_from, mms_from_type = mms.headers.get('From',
|
|
|
|
'unknown/undef').split('/')
|
|
|
|
message.author = f"{mms_from}@{self.domain}"
|
|
|
|
mms_from, mms_from_type = mms.headers.get('To',
|
|
|
|
'unknown/undef').split('/')
|
|
|
|
message.to = f"{self.user}@{self.domain}"
|
2021-05-01 21:11:15 +00:00
|
|
|
|
2021-05-04 10:58:23 +00:00
|
|
|
if 'Subject' in mms.headers and mms.headers['Subject']:
|
|
|
|
message.subject = mms.headers['Subject']
|
2021-05-05 03:49:38 +00:00
|
|
|
else:
|
2021-05-04 10:58:23 +00:00
|
|
|
message.subject = f"MMS from {mms_from}"
|
|
|
|
|
|
|
|
if 'Date' in mms.headers and mms.headers['Date']:
|
|
|
|
message.date = mms.headers['Date']
|
2021-05-01 21:11:15 +00:00
|
|
|
|
2021-05-04 10:58:23 +00:00
|
|
|
# Recopy MMS HEADERS
|
|
|
|
for header in mms.headers:
|
2021-05-05 03:49:38 +00:00
|
|
|
message.headers.append((f"X-MMS-{header}",
|
|
|
|
f"{mms.headers[header]}"))
|
|
|
|
|
2021-05-06 20:12:10 +00:00
|
|
|
message.plain = " "
|
|
|
|
data_id = 1
|
2021-05-01 06:43:13 +00:00
|
|
|
for data_part in mms.data_parts:
|
2021-05-05 03:49:38 +00:00
|
|
|
datacontent = data_part.headers['Content-Type']
|
2021-05-01 06:43:13 +00:00
|
|
|
if datacontent is not None:
|
|
|
|
if 'text/plain' in datacontent[0]:
|
2021-05-06 20:12:10 +00:00
|
|
|
encoding = datacontent[1].get('Charset', 'utf-8')
|
|
|
|
plain = data_part.data.decode(encoding)
|
|
|
|
message.plain += plain + '\n'
|
|
|
|
continue
|
|
|
|
extension = mimetypes.guess_extension(datacontent[0])
|
|
|
|
filename = datacontent[1].get('Name', str(data_id))
|
|
|
|
message.attach(filename + extension, data_part.data)
|
|
|
|
data_id = data_id + 1
|
|
|
|
|
2021-05-05 03:49:38 +00:00
|
|
|
# Add MMS binary file, for debugging purpose or reparsing in the future
|
2021-05-05 14:25:46 +00:00
|
|
|
if self.attach_mms:
|
2021-05-06 20:12:10 +00:00
|
|
|
message.attach(path, None, None, None, False, path.split('/')[-1])
|
2021-05-04 10:58:23 +00:00
|
|
|
|
2021-05-05 03:49:38 +00:00
|
|
|
# Creating an empty file stating the mms as been converted
|
2021-05-05 14:25:46 +00:00
|
|
|
self.mark_mms_read(dbus_path)
|
|
|
|
|
|
|
|
if self.delete:
|
|
|
|
self.delete_mms(dbus_path)
|
2021-05-04 10:58:23 +00:00
|
|
|
|
|
|
|
# Write the mail
|
2021-05-01 06:43:13 +00:00
|
|
|
self.mailer.send(message)
|
2021-05-01 08:28:31 +00:00
|
|
|
self.mailer.stop()
|
2021-05-01 06:43:13 +00:00
|
|
|
|
2021-05-05 03:49:38 +00:00
|
|
|
|
2021-05-07 10:12:08 +00:00
|
|
|
class DbusWatcher():
|
|
|
|
"""Use DBus Signal notification to watch for new MMS."""
|
2021-05-04 10:58:23 +00:00
|
|
|
|
2021-05-07 10:12:08 +00:00
|
|
|
def __init__(self, mms2mail):
|
2021-05-04 10:58:23 +00:00
|
|
|
"""
|
2021-05-07 10:12:08 +00:00
|
|
|
Return a DBusWatcher instance.
|
2021-05-04 10:58:23 +00:00
|
|
|
|
2021-05-07 10:12:08 +00:00
|
|
|
:param mms2mail: An mms2mail instance to convert new mms
|
|
|
|
:type mms2mail: mms2mail()
|
2021-05-04 10:58:23 +00:00
|
|
|
"""
|
2021-05-07 10:12:08 +00:00
|
|
|
self.mms2mail = mms2mail
|
2021-05-05 03:49:38 +00:00
|
|
|
|
|
|
|
def run(self):
|
|
|
|
"""Run the watcher mainloop."""
|
2021-05-07 10:12:08 +00:00
|
|
|
bus = self.mms2mail.get_bus()
|
2021-05-04 10:58:23 +00:00
|
|
|
bus.add_signal_receiver(self.message_added,
|
2021-05-05 03:49:38 +00:00
|
|
|
bus_name="org.ofono.mms",
|
|
|
|
signal_name="MessageAdded",
|
|
|
|
member_keyword="member",
|
|
|
|
path_keyword="path",
|
|
|
|
interface_keyword="interface")
|
2021-05-04 10:58:23 +00:00
|
|
|
mainloop = GLib.MainLoop()
|
2021-05-07 10:12:08 +00:00
|
|
|
print("Starting DBus watcher mainloop")
|
|
|
|
try:
|
|
|
|
mainloop.run()
|
|
|
|
except KeyboardInterrupt:
|
|
|
|
print("Stopping DBus watcher mainloop")
|
|
|
|
mainloop.quit()
|
2021-05-04 10:58:23 +00:00
|
|
|
|
2021-05-05 03:49:38 +00:00
|
|
|
def message_added(self, name, value, member, path, interface):
|
|
|
|
"""Trigger conversion on MessageAdded signal."""
|
2021-05-04 10:58:23 +00:00
|
|
|
if value['Status'] == 'downloaded' or value['Status'] == 'received':
|
|
|
|
print(f"New incoming MMS found ({name.split('/')[-1]})")
|
2021-05-07 10:12:08 +00:00
|
|
|
self.mms2mail.convert(value['Attachments'][0][2], name)
|
2021-05-04 10:58:23 +00:00
|
|
|
else:
|
|
|
|
print(f"New outgoing MMS found ({name.split('/')[-1]})")
|
2021-05-01 21:11:15 +00:00
|
|
|
|
2021-05-05 03:49:38 +00:00
|
|
|
|
2021-05-07 10:12:08 +00:00
|
|
|
def main():
|
|
|
|
"""Run the different functions handling mms and mail."""
|
2021-05-01 06:43:13 +00:00
|
|
|
parser = argparse.ArgumentParser()
|
|
|
|
mode = parser.add_mutually_exclusive_group()
|
2021-05-05 03:49:38 +00:00
|
|
|
mode.add_argument("-d", "--daemon",
|
2021-05-07 10:12:08 +00:00
|
|
|
help="Use dbus signal from mmsd to trigger conversion",
|
|
|
|
action='store_true', dest='watcher')
|
2021-05-05 03:49:38 +00:00
|
|
|
mode.add_argument("-f", "--file", nargs='+',
|
2021-05-05 14:25:46 +00:00
|
|
|
help="Parse specified mms files and quit", dest='files')
|
|
|
|
parser.add_argument('--delete', action='store_true', dest='delete',
|
|
|
|
help="Ask mmsd to delete the converted MMS")
|
|
|
|
parser.add_argument('--force-read', action='store_true',
|
|
|
|
dest='force_read', help="Force conversion even if MMS \
|
|
|
|
is marked as read")
|
2021-05-01 06:43:13 +00:00
|
|
|
args = parser.parse_args()
|
|
|
|
|
2021-05-05 14:25:46 +00:00
|
|
|
m = MMS2Mail(args.delete, args.force_read)
|
2021-05-05 03:49:38 +00:00
|
|
|
|
2021-05-04 10:58:23 +00:00
|
|
|
if args.files:
|
|
|
|
for mms_file in args.files:
|
|
|
|
m.convert(mms_file)
|
2021-05-07 10:12:08 +00:00
|
|
|
elif args.watcher:
|
|
|
|
print("Starting mms2mail in daemon mode")
|
|
|
|
w = DbusWatcher(m)
|
2021-05-01 06:43:13 +00:00
|
|
|
w.run()
|
|
|
|
else:
|
|
|
|
parser.print_help()
|
2021-05-07 10:12:08 +00:00
|
|
|
|
|
|
|
|
|
|
|
if __name__ == '__main__':
|
|
|
|
main()
|