diff --git a/README.md b/README.md index 5abfbae..592f007 100644 --- a/README.md +++ b/README.md @@ -10,6 +10,8 @@ It's based on aiosmtpd and paho-mqtt. I made a docker image because like any hipster dev I like docker. At least it's based on alpine so there's that. +Protip: `docker exec emqtt find attachments -type f -ctime +20 -delete` + ## Run it 1. Create venv and activate it. Or don't. @@ -27,6 +29,7 @@ I made a docker image because like any hipster dev I like docker. At least it's * MQTT_RESET_TIME=300 * MQTT_RESET_PAYLOAD=OFF * SAVE_ATTACHMENTS=True + * SAVE_ATTACHMENTS_DURING_RESET_TIME=False * DEBUG=False 1. Go. diff --git a/emqtt.py b/emqtt.py index a8b3155..6ee4411 100755 --- a/emqtt.py +++ b/emqtt.py @@ -22,6 +22,7 @@ 'MQTT_RESET_TIME': '300', 'MQTT_RESET_PAYLOAD': 'OFF', 'SAVE_ATTACHMENTS': 'True', + 'SAVE_ATTACHMENTS_DURING_RESET_TIME': 'False', 'DEBUG': 'False' } config = { @@ -29,8 +30,11 @@ for setting, default in defaults.items() } # Boolify -config['DEBUG'] = config['DEBUG'] == 'True' -config['SAVE_ATTACHMENTS'] = config['SAVE_ATTACHMENTS'] == 'True' +for key, value in config.items(): + if value == 'True': + config[key] = True + elif value == 'False': + config[key] = False level = logging.DEBUG if config['DEBUG'] == 'True' else logging.INFO @@ -53,8 +57,18 @@ def __init__(self, loop): async def handle_DATA(self, server, session, envelope): log.debug('Message from %s', envelope.mail_from) msg = email.message_from_bytes(envelope.original_content, policy=default) + log.debug( + 'Message data (truncated): %s', + envelope.content.decode('utf8', errors='replace')[:250] + ) + topic = '{}/{}'.format(config['MQTT_TOPIC'], envelope.mail_from.replace('@', '')) + self.mqtt_publish(topic, config['MQTT_PAYLOAD']) - if config['SAVE_ATTACHMENTS']: + # Save attached files if configured to do so. + if config['SAVE_ATTACHMENTS'] and ( + # Don't save them during reset time unless configured to do so. + topic not in self.handles + or config['SAVE_ATTACHMENTS_DURING_RESET_TIME']): for att in msg.iter_attachments(): # Just save images if not att.get_content_type().startswith('image'): @@ -66,14 +80,12 @@ async def handle_DATA(self, server, session, envelope): with open(file_path, 'wb') as f: f.write(image_data) - log.debug( - 'Message data (truncated): %s', - envelope.content.decode('utf8', errors='replace')[:250] - ) - topic = '{}/{}'.format(config['MQTT_TOPIC'], envelope.mail_from.replace('@', '')) - self.mqtt_publish(topic, config['MQTT_PAYLOAD']) + # Cancel any current scheduled resets of this topic + if topic in self.handles: + self.handles.pop(topic).cancel() if self.reset_time: + # Schedule a reset of this topic self.handles[topic] = self.loop.call_later( self.reset_time, self.mqtt_publish, @@ -83,10 +95,6 @@ async def handle_DATA(self, server, session, envelope): return '250 Message accepted for delivery' def mqtt_publish(self, topic, payload): - # Cancel any current scheduled resets of this topic - if topic in self.handles: - self.handles.pop(topic).cancel() - log.info('Publishing "%s" to %s', payload, topic) try: publish.single(