From 19496e737cd39fc1ddb88b71ecf39c728093f4f6 Mon Sep 17 00:00:00 2001 From: Anna Schumaker Date: Tue, 9 Jun 2015 10:27:55 -0400 Subject: [PATCH] slackmail: Support emailing channels and private groups Signed-off-by: Anna Schumaker --- slackmail.py | 35 +++++++++++++++++++---------------- 1 file changed, 19 insertions(+), 16 deletions(-) diff --git a/slackmail.py b/slackmail.py index 26eac46..9fd5b6c 100644 --- a/slackmail.py +++ b/slackmail.py @@ -7,31 +7,34 @@ from email.mime.text import MIMEText import textwrap -s = smtplib.SMTP("localhost") -repl = "--- Reply above this line ---".ljust(40) +smtp = smtplib.SMTP("localhost") +reply = "--- Reply above this line ---".ljust(40) +def mail_thread(thread): + if not thread.is_member(): + return + if (thread.unread_count() == 0) or (thread.unread_count() == None): + return - -for channel in slack.channels.list(): - if not channel.is_member(): - continue - if channel.unread_count() == 0 or channel.unread_count() == None: - continue - - chan = ("channel=%s" % channel.id()).rjust(40) - text = [ "%s%s" % (repl, chan), ""] - text += [ str(channel) ] + [ "" ] - for message in channel.read(): + header = ("thread=%s" % thread.id()).rjust(40) + header = "%s%s" % (reply, header) + text = [ header, "" ] + for message in thread.read(): text += [ str(message) ] print("\n".join(text)) print() msg = MIMEText("\n".join(text)) - msg["Subject"] = "[%s] %s" % (slack.team(), channel.name()) + msg["Subject"] = "[%s] %s" % (slack.team(), thread.name()) msg["From"] = "SlackMail@OcarinaProject.net" msg["To"] = slack.user().email() - s.sendmail(slack.team(), slack.user().email(), msg.as_string()) + smtp.sendmail(slack.team(), slack.user().email(), msg.as_string()) +def mail_threads(thread_list): + for thread in thread_list: + mail_thread(thread) -s.quit() +mail_threads(slack.channels.list()) +mail_threads(slack.groups.list()) +smtp.quit()