69 lines
1.5 KiB
Python
Executable File
69 lines
1.5 KiB
Python
Executable File
#!/usr/bin/python
|
|
# Copyright 2015 (c) Anna Schumaker.
|
|
#
|
|
|
|
import slack
|
|
import sys
|
|
import quopri
|
|
from email.parser import Parser
|
|
|
|
|
|
headers = Parser().parsestr(sys.stdin.read())
|
|
|
|
|
|
#
|
|
# Only allow replies that come from the slack user's email address.
|
|
#
|
|
sender = headers["sender"].replace(".", "")
|
|
target = slack.user().email().replace(".", "")
|
|
if sender != target:
|
|
print("Reply not from user!")
|
|
sys.exit(1)
|
|
|
|
|
|
#
|
|
# Look for our marker text to help find thread id
|
|
#
|
|
reply = "--- Reply above this line ---"
|
|
payload = headers.get_payload()
|
|
if payload.find(reply) == -1:
|
|
print("Reply line missing!")
|
|
sys.exit(1)
|
|
split = headers.get_payload().split(reply)
|
|
|
|
|
|
#
|
|
# We should have a two element list. The thread id
|
|
# is in the text at the start of the second element.
|
|
#
|
|
quoted = quopri.decodestring(split[-1]).decode()
|
|
thread_id = quoted.split()[0]
|
|
if thread_id.find("thread=") == -1:
|
|
print("Thread ID missing!")
|
|
sys.exit(1)
|
|
thread_id = thread_id[7:]
|
|
|
|
|
|
#
|
|
# Loop over each line of the reply, strip out special characters,
|
|
# and remove any "Reply sent on <whenever>" text. Note that we
|
|
# make the following assumptions:
|
|
#
|
|
# - The email reply will always have "Reply sent on <whenever>" text
|
|
# - There will always be a blank line between content and replied-on text
|
|
#
|
|
lines = []
|
|
for line in split[0].split("\n"):
|
|
lines += [ line.strip() ]
|
|
while lines[-1] != "":
|
|
lines.pop(len(lines) - 1)
|
|
text = ' '.join(lines).strip()
|
|
|
|
|
|
thread = slack.find_thread(thread_id)
|
|
if thread == None:
|
|
print("No such thread: %s!" % thread_id)
|
|
sys.exit(1)
|
|
|
|
thread.post(text)
|