Skip to content

Commit

Permalink
Added option to skip saving attachments during reset time - defaults …
Browse files Browse the repository at this point in the history
…to True
  • Loading branch information
wicol committed Jan 5, 2018
1 parent e679c7b commit ef0f771
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 13 deletions.
3 changes: 3 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand All @@ -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.
Expand Down
34 changes: 21 additions & 13 deletions emqtt.py
Original file line number Diff line number Diff line change
Expand Up @@ -22,15 +22,19 @@
'MQTT_RESET_TIME': '300',
'MQTT_RESET_PAYLOAD': 'OFF',
'SAVE_ATTACHMENTS': 'True',
'SAVE_ATTACHMENTS_DURING_RESET_TIME': 'False',
'DEBUG': 'False'
}
config = {
setting: os.environ.get(setting, default)
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

Expand All @@ -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'):
Expand All @@ -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,
Expand All @@ -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(
Expand Down

0 comments on commit ef0f771

Please sign in to comment.