From c5e15161bddd705ee1453830f3e0300054c882b6 Mon Sep 17 00:00:00 2001 From: Anna Schumaker Date: Tue, 16 Jun 2015 11:05:32 -0400 Subject: [PATCH] slackpost: Updates for current email formatting - Look for thread= lines - Support posting to both channels and private groups Signed-off-by: Anna Schumaker --- slack/__init__.py | 7 +++++ slack/channels.py | 3 --- slack/chat.py | 2 +- slack/threads.py | 4 ++- slackpost.py | 67 ++++++++++++++++++++++++++++++++++------------- 5 files changed, 60 insertions(+), 23 deletions(-) diff --git a/slack/__init__.py b/slack/__init__.py index 31bc673..4ec27e9 100644 --- a/slack/__init__.py +++ b/slack/__init__.py @@ -35,3 +35,10 @@ def team(): def user(): return __USER + +def find_thread(thread_id): + [ res ] = [ c for c in channels.list() if c.id() == thread_id ] or [ None ] + if res: + return res + [ res ] = [ g for g in groups.list() if g.id() == thread_id ] or [ None ] + return res diff --git a/slack/channels.py b/slack/channels.py index 9f4a4f7..ef5401f 100644 --- a/slack/channels.py +++ b/slack/channels.py @@ -25,9 +25,6 @@ class Channel(threads.Thread): def is_member(self): return self.__member - def post(self, text): - chat.postMessage(self.__id, text) - def history(channel, timestamp): diff --git a/slack/chat.py b/slack/chat.py index a7319a6..a9d5d2c 100644 --- a/slack/chat.py +++ b/slack/chat.py @@ -51,6 +51,6 @@ class Message: -def postMessage(channel, text): +def post_message(channel, text): api.call("chat.postMessage", token = auth.token(), channel = channel, text = text, as_user = True, parse = "full") diff --git a/slack/threads.py b/slack/threads.py index 606224b..eb902ca 100644 --- a/slack/threads.py +++ b/slack/threads.py @@ -63,8 +63,10 @@ class Thread: self.do_mark_messages(m_list[-1].ts()) return m_list - def unread_count(self): if self.__unread == None: self.fetch_info() return self.__unread + + def post(self, text): + chat.post_message(self.__id, text) diff --git a/slackpost.py b/slackpost.py index d9332f4..ef7c637 100755 --- a/slackpost.py +++ b/slackpost.py @@ -6,31 +6,62 @@ import slack import sys from email.parser import Parser + headers = Parser().parsestr(sys.stdin.read()) -if headers['from'] != "Anna Schumaker ": + +# +# 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) -# Determine which channel to post to -chan_id = headers["X-Slack-Channel"] -channel = None - -for c in slack.channels.list(): - if c.id() == chan_id: - channel = c - break - -if channel == None: +# +# 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) -lines = headers.get_payload().strip().split("\n") -for i in range(len(lines)): - if len(lines[i]) == 0: - continue - if lines[i][0] == ">": - break -lines = lines[0:i-1] +# +# We should have a two element list. The thread id +# is in the text at the start of the second element. +# +thread_id = split[-1].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 " text. Note that we +# make the following assumptions: +# +# - The email reply will always have "Reply sent on " 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() +print(text) + + +thread = slack.find_thread(thread_id) +if thread == None: + print("No such thread!") + sys.exit(1) + channel.post(text)