2015-05-26 21:18:50 -04:00
|
|
|
#!/usr/bin/python
|
|
|
|
# Copyright 2015 (c) Anna Schumaker.
|
|
|
|
#
|
2016-11-17 10:28:53 -05:00
|
|
|
import re
|
2015-05-26 21:18:50 -04:00
|
|
|
import slack
|
|
|
|
import sys
|
|
|
|
from email.parser import Parser
|
|
|
|
|
|
|
|
headers = Parser().parsestr(sys.stdin.read())
|
|
|
|
|
2015-06-16 11:05:32 -04:00
|
|
|
|
|
|
|
#
|
|
|
|
# Only allow replies that come from the slack user's email address.
|
|
|
|
#
|
2016-11-17 10:28:53 -05:00
|
|
|
sender = headers.get("sender", headers.get("from"))
|
|
|
|
match = re.search("<(.*?)@(.*?)>", sender)
|
|
|
|
addr = match.group(0)[1:-1] if match else sender
|
|
|
|
if addr.replace(".", "") != slack.user().email().replace(".", ""):
|
2015-06-16 11:05:32 -04:00
|
|
|
print("Reply not from user!")
|
2015-05-26 21:18:50 -04:00
|
|
|
sys.exit(1)
|
|
|
|
|
|
|
|
|
2016-11-17 10:49:44 -05:00
|
|
|
#
|
|
|
|
# Find the message payload (this is tricky for multipart messages)
|
|
|
|
#
|
|
|
|
if headers.is_multipart():
|
|
|
|
for part in headers.get_payload():
|
|
|
|
if part.get_content_type() == "text/plain":
|
2016-11-18 09:54:36 -05:00
|
|
|
payload = part.get_payload(decode = True)
|
2016-11-17 10:49:44 -05:00
|
|
|
else:
|
2016-11-18 09:54:36 -05:00
|
|
|
payload = headers.get_payload(decode = True)
|
2016-11-17 10:49:44 -05:00
|
|
|
|
2016-11-18 09:54:36 -05:00
|
|
|
if type(payload) is bytes:
|
|
|
|
payload = payload.decode()
|
2016-11-17 10:49:44 -05:00
|
|
|
|
2015-06-16 11:05:32 -04:00
|
|
|
#
|
2016-11-17 10:28:53 -05:00
|
|
|
# Find the thread id in the message payload
|
2015-06-16 11:05:32 -04:00
|
|
|
#
|
2016-11-17 10:28:53 -05:00
|
|
|
reply = "--- Reply above this line ---(\s*?)thread=(.*?)\n"
|
2016-11-17 10:49:44 -05:00
|
|
|
match = re.search(reply, payload)
|
2016-11-17 10:28:53 -05:00
|
|
|
if match == None:
|
2015-06-16 11:05:32 -04:00
|
|
|
print("Reply line missing!")
|
|
|
|
sys.exit(1)
|
2015-05-26 21:18:50 -04:00
|
|
|
|
2016-11-17 10:28:53 -05:00
|
|
|
thread_id = match.group(0).rsplit("=")[-1].rstrip()
|
2015-05-26 21:18:50 -04:00
|
|
|
|
2015-05-27 11:10:47 -04:00
|
|
|
|
2015-06-16 11:05:32 -04:00
|
|
|
#
|
2016-11-17 10:28:53 -05:00
|
|
|
# Parse the message payload to find the text that was sent.
|
|
|
|
# Note that we make the following assumptions:
|
2015-06-16 11:05:32 -04:00
|
|
|
#
|
2016-11-17 10:28:53 -05:00
|
|
|
# - Quoted text will always begin with ">"
|
|
|
|
# - The email reply will always have "Reply sent on <whenever>" text
|
2015-06-16 11:05:32 -04:00
|
|
|
#
|
2016-11-17 10:49:44 -05:00
|
|
|
message = re.sub(">(.*?)\n", "", payload).rstrip()
|
2016-11-17 10:28:53 -05:00
|
|
|
split = message.split("\n")
|
|
|
|
message = "\n".join(split[:-1]).rstrip()
|
2015-06-16 11:05:32 -04:00
|
|
|
|
|
|
|
|
2016-11-17 10:28:53 -05:00
|
|
|
#
|
|
|
|
# Post the message to the thread
|
|
|
|
#
|
2015-06-16 11:05:32 -04:00
|
|
|
thread = slack.find_thread(thread_id)
|
|
|
|
if thread == None:
|
2015-06-16 11:22:41 -04:00
|
|
|
print("No such thread: %s!" % thread_id)
|
2015-06-16 11:05:32 -04:00
|
|
|
sys.exit(1)
|
|
|
|
|
2016-11-17 10:28:53 -05:00
|
|
|
if thread.post(message) == False:
|
|
|
|
print("Error posting to thread: %s" % thread_id)
|
|
|
|
sys.exit(1)
|