Added be-handle-mail and some example emails.
authorW. Trevor King <wking@drexel.edu>
Tue, 14 Jul 2009 21:10:51 +0000 (17:10 -0400)
committerW. Trevor King <wking@drexel.edu>
Tue, 14 Jul 2009 21:10:51 +0000 (17:10 -0400)
So far, it parses the emails and executes the specified task.

Todo: email the sender back with the output/errors/exit-status/etc.

interfaces/email/interactive/be-handle-mail [new file with mode: 0755]
interfaces/email/interactive/examples/blank [new file with mode: 0644]
interfaces/email/interactive/examples/comment [new file with mode: 0644]
interfaces/email/interactive/examples/invalid_command [new file with mode: 0644]
interfaces/email/interactive/examples/invalid_subject [new file with mode: 0644]
interfaces/email/interactive/examples/list [new file with mode: 0644]
interfaces/email/interactive/examples/missing_command [new file with mode: 0644]
interfaces/email/interactive/examples/new [new file with mode: 0644]
interfaces/email/interactive/examples/show [new file with mode: 0644]

diff --git a/interfaces/email/interactive/be-handle-mail b/interfaces/email/interactive/be-handle-mail
new file mode 100755 (executable)
index 0000000..a524389
--- /dev/null
@@ -0,0 +1,83 @@
+#!/usr/bin/env python
+#
+# Copyright
+"""Provide and email interface to the distributed bugtracker Bugs
+Everywhere.  Recieves incoming email via procmail and allows users to
+select actions with their subject lines.  Subject lines follow the
+format
+  [be-bug] command (options) (args)
+With the body of the email being used as the final argument for the
+commands "new" and "comment", and ignored otherwise.  The options and
+arguments are split on whitespace, so don't use whitespace inside a
+single argument.
+
+Eventually we'll commit after every message.
+"""
+
+import libbe.cmdutil
+import email
+import sys
+
+ALLOWED_COMMANDS = ["new", "comment", "list", "show", "help"]
+
+class InvalidEmail (ValueError):
+    def __init__(self, msg, message):
+        ValueError.__init__(self, message)
+        self.msg = msg
+
+class InvalidSubject (InvalidEmail):
+    pass
+
+class InvalidCommand (InvalidEmail):
+    def __init__(self, msg, command):
+        message = "Invalid command '%s'" % command
+        ValueError.__init__(self, msg, message)
+        self.command = command
+
+def get_body_type(msg):
+    for part in msg.walk():
+        if part.is_multipart():
+            continue
+        return (part.get_payload(decode=1), part.get_content_type())
+
+def handle_message(msg_text):
+    p=email.Parser.Parser()
+    msg=p.parsestr(msg_text)
+    
+    if "subject" not in msg:
+        raise InvalidSubject(msg, "Email must contain a subject")
+    author = msg['from']
+    args = msg["subject"].split()
+    if len(args) < 1 or args[0] != "[be-bug]":
+        raise InvalidSubject(msg, "Subject must start with '[be-bug] '")        
+    elif len(args) < 2:
+        raise InvalidCommand(msg, "")
+    command = args[1]
+    if command not in ALLOWED_COMMANDS:
+        raise InvalidCommand(msg, command)
+    if len(args) > 2:
+        command_args = args[2:]
+    else:
+        command_args = []
+    if command in ["new", "comment"]:
+        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
+            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                
+            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)
+    return libbe.cmdutil.execute(command, command_args)
+
+def main():
+    msg_text = sys.stdin.read()
+    sys.exit(handle_message(msg_text))
+
+if __name__ == "__main__":
+    main()
diff --git a/interfaces/email/interactive/examples/blank b/interfaces/email/interactive/examples/blank
new file mode 100644 (file)
index 0000000..e69de29
diff --git a/interfaces/email/interactive/examples/comment b/interfaces/email/interactive/examples/comment
new file mode 100644 (file)
index 0000000..1d60748
--- /dev/null
@@ -0,0 +1,9 @@
+From jdoe@example.com Fri Apr 18 11:18:58 2008
+Message-ID: <xyz@example.com>
+Date: Fri, 18 Apr 2008 12:00:00 +0000
+From: John Doe <jdoe@example.com>
+Content-Type: text/plain; charset=UTF-8
+Content-Transfer-Encoding: 8bit
+Subject: [be-bug] comment a1d
+
+We sure do.
diff --git a/interfaces/email/interactive/examples/invalid_command b/interfaces/email/interactive/examples/invalid_command
new file mode 100644 (file)
index 0000000..4d18f09
--- /dev/null
@@ -0,0 +1,9 @@
+From jdoe@example.com Fri Apr 18 11:18:58 2008
+Message-ID: <abcd@example.com>
+Date: Fri, 18 Apr 2008 12:00:00 +0000
+From: John Doe <jdoe@example.com>
+Content-Type: text/plain; charset=UTF-8
+Content-Transfer-Encoding: 8bit
+Subject: [be-bug] close
+
+Dummy content
diff --git a/interfaces/email/interactive/examples/invalid_subject b/interfaces/email/interactive/examples/invalid_subject
new file mode 100644 (file)
index 0000000..e148d0b
--- /dev/null
@@ -0,0 +1,9 @@
+From jdoe@example.com Fri Apr 18 11:18:58 2008
+Message-ID: <abcd@example.com>
+Date: Fri, 18 Apr 2008 12:00:00 +0000
+From: John Doe <jdoe@example.com>
+Content-Type: text/plain; charset=UTF-8
+Content-Transfer-Encoding: 8bit
+Subject: Spam!
+
+Dummy content
diff --git a/interfaces/email/interactive/examples/list b/interfaces/email/interactive/examples/list
new file mode 100644 (file)
index 0000000..333315f
--- /dev/null
@@ -0,0 +1,9 @@
+From jdoe@example.com Fri Apr 18 11:18:58 2008
+Message-ID: <abcd@example.com>
+Date: Fri, 18 Apr 2008 12:00:00 +0000
+From: John Doe <jdoe@example.com>
+Content-Type: text/plain; charset=UTF-8
+Content-Transfer-Encoding: 8bit
+Subject: [be-bug] list --status all
+
+Dummy content
diff --git a/interfaces/email/interactive/examples/missing_command b/interfaces/email/interactive/examples/missing_command
new file mode 100644 (file)
index 0000000..fefe41b
--- /dev/null
@@ -0,0 +1,9 @@
+From jdoe@example.com Fri Apr 18 11:18:58 2008
+Message-ID: <abcd@example.com>
+Date: Fri, 18 Apr 2008 12:00:00 +0000
+From: John Doe <jdoe@example.com>
+Content-Type: text/plain; charset=UTF-8
+Content-Transfer-Encoding: 8bit
+Subject: [be-bug] abcde
+
+Dummy content
diff --git a/interfaces/email/interactive/examples/new b/interfaces/email/interactive/examples/new
new file mode 100644 (file)
index 0000000..7ac6dce
--- /dev/null
@@ -0,0 +1,9 @@
+From jdoe@example.com Fri Apr 18 11:18:58 2008
+Message-ID: <abcd@example.com>
+Date: Fri, 18 Apr 2008 12:00:00 +0000
+From: John Doe <jdoe@example.com>
+Content-Type: text/plain; charset=UTF-8
+Content-Transfer-Encoding: 8bit
+Subject: [be-bug] new
+
+Need tests for the email interface.
diff --git a/interfaces/email/interactive/examples/show b/interfaces/email/interactive/examples/show
new file mode 100644 (file)
index 0000000..3ff56f4
--- /dev/null
@@ -0,0 +1,9 @@
+From jdoe@example.com Fri Apr 18 11:18:58 2008
+Message-ID: <abcd@example.com>
+Date: Fri, 18 Apr 2008 12:00:00 +0000
+From: John Doe <jdoe@example.com>
+Content-Type: text/plain; charset=UTF-8
+Content-Transfer-Encoding: 8bit
+Subject: [be-bug] show --xml 361
+
+Dummy content