be-handle-mail succesfully sends replies.
authorW. Trevor King <wking@drexel.edu>
Wed, 15 Jul 2009 19:45:27 +0000 (15:45 -0400)
committerW. Trevor King <wking@drexel.edu>
Wed, 15 Jul 2009 19:45:27 +0000 (15:45 -0400)
Although I'm not catching stdout/stderr yet, so the replies aren't
very useful ;).  Still it the send_pgp_mime.py interface is working :).

I've added rudimentary logging (via LOGFILE) to keep track of what
be-handle-mail is up to.  There's also BE_DIR, which sets the
directory that BE lives in (important ;).

The author handling got more consistent, thanks to
send_pgp_mime.source_email (using the new return_realname option) and
email.utils.formataddr().  Now author_addr should look the same
regardless of which phrasing you use to set it (e.g. "NAME <ADDR>" vs
"ADDR (NAME)", and possibly others.)

interfaces/email/interactive/be-handle-mail

index a608074ce5cb0522718e4c7e02f655b7da42f02b..c92fec4780eb61d5d139d8f83f225f9cd71f94da 100755 (executable)
@@ -30,13 +30,18 @@ Eventually we'll commit after every message.
 
 import cStringIO as StringIO
 import email
+import email.utils
 import libbe.cmdutil, libbe.utility
+import os
+import os.path
 import send_pgp_mime
 import sys
 import time
 
 SUBJECT_COMMENT = "[be-bug]"
-HANDLER_ADDRESS = "wking@thor.physics.drexel.edu"
+HANDLER_ADDRESS = "BE Bugs <wking@thor.physics.drexel.edu>"
+LOGFILE = os.path.join(os.path.dirname(__file__), "be-handle-mail.log")
+BE_DIR = os.path.expanduser("~/src/fun/be/be.email")
 
 ALLOWED_COMMANDS = ["new", "comment", "list", "show", "help"]
 
@@ -69,7 +74,15 @@ def run_message(msg_text):
     
     if "subject" not in msg:
         raise InvalidSubject(msg, "Email must contain a subject")
-    author = msg["from"]
+    author = send_pgp_mime.source_email(msg, return_realname=True)
+    author_name = author[0]
+    author_email = author[1]
+    author_addr = email.utils.formataddr((author_name, author_email))
+    if LOGFILE != None:
+        f = file(LOGFILE, "w+")
+        f.write("handling %s\n" % (author_addr))
+        f.write("\n%s\n\n" % msg_text)
+        f.close()
     id = msg["message-id"]
     args = msg["subject"].split()
     if len(args) < 1 or args[0] != SUBJECT_COMMENT:
@@ -87,39 +100,54 @@ def run_message(msg_text):
         body,type = get_body_type(msg)
         if command == "new":
             if "--reporter" not in args and "-r" not in args:
-                command_args = ["--reporter", author] + command_args
+                command_args = ["--reporter", author_addr] + command_args
             body = body.strip().split("\n", 1)[0] # only take first line
         elif command == "comment":
             if "--author" not in args and "-a" not in args:
-                command_args = ["--author", author] + command_args
+                command_args = ["--author", author_addr] + command_args
             if "--content-type" not in args and "-c" not in args:
                 command_args = ["--content-type", type] + command_args
             if "--alt-id" not in args:
                 command_args = ["--alt-id", msg["message-id"]] + command_args
         command_args.append(body)
+    if LOGFILE != None:
+        f = file(LOGFILE, "a+")
+        f.write("ping\n")
+        f.close()
     # catch stdout and stderr
+    new_stdout = StringIO.StringIO()
+    new_stderr = StringIO.StringIO()
     orig_stdout = sys.stdout
     orig_stderr = sys.stderr
-    sys.stdout = StringIO.StringIO()
-    sys.stderr = StringIO.StringIO()
+    sys.stdout = new_stdout
+    sys.stderr = new_stderr
     # run the command
+    os.chdir(BE_DIR)
     ret = libbe.cmdutil.execute(command, command_args)
     # restore stdout and stderr
-    out_text = sys.stdout.getvalue()
-    err_text = sys.stderr.getvalue()
+    sys.stdout.flush()
+    sys.stderr.flush()
     sys.stdout = orig_stdout
     sys.stderr = orig_stderr
+    out_text = new_stdout.getvalue()
+    err_text = new_stderr.getvalue()
 
+    if LOGFILE != None:
+        f = file(LOGFILE, "a+")
+        f.write("pong\n")
+        f.close()
+    author_addr = "wking"
     response_header = [u"From: %s" % HANDLER_ADDRESS,
-                       u"To: %s" % author,
-                       u"Date: %s" % libbe.utility.time_to_str(time.time()),
-                       u"Content-Type: text/plain; charset=%s" % encoding,
-                       u"Content-Transfer-Encoding: 8bit",
-                       u"In-reply-to: %s" % (id),
-                       u"Subject: Re: %s %s" % (SUBJECT_COMMENT, command),
+                       u"To: %s" % author_addr,
+                       "Date: %s" % libbe.utility.time_to_str(time.time()),
+                       "Content-Type: text/plain; charset=%s" % encoding,
+                       "Content-Transfer-Encoding: 8bit",
+                       "In-reply-to: %s" % (id),
+                       u"Subject: %s Re: %s" % (SUBJECT_COMMENT, command),
                        ]
     response_body = [u"Results of running: (exit code %d)" % ret,
-                     u"  %s %s" % (command, " ".join(command_args)),]
+                     u"  %s %s" % (command, " ".join(command_args)),
+                     u""] # trailing endline on body
     if len(out_text) > 0:
         response_body.extend([u"", u"stdout:", u"", out_text])
     if len(err_text) > 0:
@@ -127,12 +155,17 @@ def run_message(msg_text):
     response_body.append(u"")
     response_email = send_pgp_mime.Mail(u"\n".join(response_header),
                                         u"\n".join(response_body))
+    if LOGFILE != None:
+        f = file(LOGFILE, "a+")
+        f.write("responding to %s: %s\n" % (author_addr, command))
+        f.write("\n%s\n\n" % send_pgp_mime.flatten(response_email.plain()))
+        f.close()
     return response_email
 
 def main():
     msg_text = sys.stdin.read()
     response_email = run_message(msg_text)
-    send_pgp_mime.mail(response_email, send_pgp_mime.sendmail)
+    send_pgp_mime.mail(response_email.plain(), send_pgp_mime.sendmail)
 
 if __name__ == "__main__":
     main()