aboutsummaryrefslogtreecommitdiffstats
path: root/interfaces/email/interactive/be-handle-mail
blob: 01b75ac283a3a51af041e2e4912a18d3bd1cc92b (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
#!/usr/bin/env python
#
# Copyright (C) 2009 W. Trevor King <wking@drexel.edu>
#
# This program is free software; you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation; either version 2 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License along
# with this program; if not, write to the Free Software Foundation, Inc.,
# 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
"""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 codecs
import cStringIO as StringIO
import email
import email.utils
import libbe.cmdutil, libbe.encoding, libbe.utility
import os
import os.path
import send_pgp_mime
import sys
import time
import traceback

SUBJECT_COMMENT = "[be-bug]"
HANDLER_ADDRESS = "BE Bugs <wking@thor.physics.drexel.edu>"
_THIS_DIR = os.path.abspath(os.path.dirname(__file__))
BE_DIR = _THIS_DIR
LOGPATH = os.path.join(_THIS_DIR, "be-handle-mail.log")
LOGFILE = None
ENCODING = libbe.encoding.get_encoding()

ALLOWED_COMMANDS = ["new", "comment", "list", "show", "help"]

class InvalidEmail (ValueError):
    def __init__(self, msg, info, message):
        ValueError.__init__(self, message)
        self.msg = msg
        self.info = info
    def response(self):
        ret = 1
        out_text = None
        return (ret, out_text, self.stderr_msg(), self.info)
    def stderr_msg(self):
        err_text = [u"Invalid email (particular type unknown):\n",
                    unicode(self), u"",
                    send_pgp_mime.flatten(self.msg)]
        return u"\n".join(err_text)

class InvalidSubject (InvalidEmail):
    def stderr_msg(self):
        err_text = u"\n".join([u"InvalidSubject:\n",
                               unicode(e), u"",
                               u"full subject was:",
                               e.msg["subject"]])
        return err_text

class InvalidCommand (InvalidEmail):
    def __init__(self, msg, info, command):
        message = "Invalid command '%s'" % command
        InvalidEmail.__init__(self, msg, info, message)
        self.command = command
    def stderr_msg(self):
        err_text = u"\n".join([u"InvalidCommand:\n",
                               unicode(self), u"",
                               u"full subject was:",
                               self.msg["subject"]])
        return err_text

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 run_message(msg_text):
    """
    Attempt to execute the email given in the email string msg_text.
    Raises assorted subclasses of InvalidEmail in the case of invalid
    messages, otherwise return the exit code, stdout, and stderr
    produced by the command, as well as a dictionary of information
    gleaned from the email.
    """
    p=email.Parser.Parser()
    msg=p.parsestr(msg_text)
    
    info = {}
    author = send_pgp_mime.source_email(msg, return_realname=True)
    info["author_name"] = author[0]
    info["author_email"] = author[1]
    info["author_addr"] = email.utils.formataddr(
        (info["author_name"], info["author_email"]))
    info["message-id"] = msg["message-id"]
    if LOGFILE != None:
        LOGFILE.write("handling %s\n" % (info["author_addr"]))
        LOGFILE.write("\n%s\n\n" % msg_text)
    if "subject" not in msg:
        raise InvalidSubject(msg, info, "Email must contain a subject")
    args = msg["subject"].split()
    if len(args) < 1 or args[0] != SUBJECT_COMMENT:
        raise InvalidSubject(
            msg, info, "Subject must start with '%s '" % SUBJECT_COMMENT)
    elif len(args) < 2:
        raise InvalidCommand(msg, info, "") # don't accept blank commands
    command = args[1]
    info["command"] = command
    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,mime_type = get_body_type(msg)
        if command == "new":
            if "--reporter" not in args and "-r" not in 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_addr] + command_args
            if "--content-type" not in args and "-c" not in args:
                command_args = ["--content-type", mime_type] + command_args
            if "--alt-id" not in args:
                command_args = ["--alt-id", msg["message-id"]] + command_args
        command_args.append(body)
    info["command-args"] = command_args
    # catch stdout and stderr
    new_stdout = codecs.getwriter(ENCODING)(StringIO.StringIO())
    new_stderr = codecs.getwriter(ENCODING)(StringIO.StringIO())
    orig_stdout = sys.stdout
    orig_stderr = sys.stderr
    sys.stdout = new_stdout
    sys.stderr = new_stderr
    # run the command
    err = None
    os.chdir(BE_DIR)
    try:
        ret = libbe.cmdutil.execute(command, command_args,
                                    manipulate_encodings=False)
    except libbe.cmdutil.GetHelp:
        print libbe.cmdutil.help(command)
    except libbe.cmdutil.GetCompletions:
        err = InvalidCommand(msg, info, "invalid option '--complete'")
    except libbe.cmdutil.UsageError, e:
        err = InvalidCommand(msg, info, e)
    except libbe.cmdutil.UserError, e:
        err = InvalidCommand(msg, info, e)
    # restore stdout and stderr
    sys.stdout.flush()
    sys.stderr.flush()
    sys.stdout = orig_stdout
    sys.stderr = orig_stderr
    out_text = codecs.decode(new_stdout.getvalue(), ENCODING)
    err_text = codecs.decode(new_stderr.getvalue(), ENCODING)
    if err != None:
        raise err
    if LOGFILE != None:
        LOGFILE.write(u"stdout? " + str(type(out_text)))
        LOGFILE.write(u"\n%s\n\n" % out_text)
    return (ret, out_text, err_text, info)

def compose_response(ret, out_text, err_text, info):
    if "author_addr" not in info:
        return None
    if "command" not in info:
        info["command"] = u"-BLANK-"
    if "command_args" not in info:
        info["command_args"] = []
    response_header = [u"From: %s" % HANDLER_ADDRESS,
                       u"To: %s" % info["author_addr"],
                       u"Date: %s" % libbe.utility.time_to_str(time.time()),
                       u"Content-Type: text/plain; charset=%s" % ENCODING,
                       u"Content-Transfer-Encoding: 8bit",
                       u"Subject: %s Re: %s"%(SUBJECT_COMMENT,info["command"]),
                       ]
    if "message-id" in info:
        response_header.append(u"In-reply-to: %s" % info["message-id"])
    response_body = [u"Results of running: (exit code %d)" % ret,
                     u"  %s %s" % (info["command"],
                                   u" ".join(info["command_args"]))]
    if out_text != None and len(out_text) > 0:
        response_body.extend([u"", u"stdout:", u"", out_text])
    if err_text != None and len(err_text) > 0:
        response_body.extend([u"", u"stderr:", u"", err_text])
    response_body.append(u"") # trailing endline
    response_email = send_pgp_mime.Mail(u"\n".join(response_header),
                                        u"\n".join(response_body))
    if LOGFILE != None:
        LOGFILE.write("responding to %s: %s\n"
                % (info["author_addr"], info["command"]))
        LOGFILE.write("\n%s\n\n" % send_pgp_mime.flatten(response_email.plain()))
    return response_email

def open_logfile(logpath=None):
    """
    If logpath=None, default to global LOGPATH.
    Special logpath strings:
     "-"     set LOGFILE to sys.stderr
     "none"  disable logging
    Relative logpaths are expanded relative to _THIS_DIR
    """
    global LOGPATH, LOGFILE
    if logpath != None:
        if logpath == "-":
            LOGPATH = "stderr"
            LOGFILE = sys.stderr
        elif logpath == "none":
            LOGPATH = "none"
            LOGFILE = None
        elif os.path.isabs(logpath):
            LOGPATH = logpath
        else:
            LOGPATH = os.path.join(_THIS_DIR, logpath)
    if LOGFILE == None and LOGPATH != "none":
        LOGFILE = codecs.open(LOGPATH, "a+", ENCODING)
    
def close_logfile():
    if LOGFILE != None and LOGPATH not in ["stderr", "none"]:
        LOGFILE.close()


def main():
    from optparse import OptionParser

    usage="be-handle-mail [options]\n\n%s" % (__doc__)
    parser = OptionParser(usage=usage)
    parser.add_option('-o', '--output', dest='output', action='store_true',
                      help="Don't mail the generated message, print it to stdout instead.  Useful for testing be-handle-mail functionality without the whole mail transfer agent and procmail setup.")
    parser.add_option('-l', '--logfile', dest='logfile', metavar='LOGFILE',
                      help='Set the logfile to LOGFILE.  Relative paths are relative to the location of this be-handle-mail file (%s).  The special value of "-" directs the log output to stderr, and "none" disables logging.' % _THIS_DIR)

    options,args = parser.parse_args()

    msg_text = sys.stdin.read()
    libbe.encoding.set_IO_stream_encodings(ENCODING) # _after_ reading message
    open_logfile(options.logfile)
    try: 
        ret,out_text,err_text,info = run_message(msg_text)
    except InvalidEmail, e:
        ret,out_text,err_text,info = e.response()
    except Exception, e:
        if LOGFILE != None:
            LOGFILE.write("Uncaught exception:\n%s\n" % (e,))
            traceback.print_tb(sys.exc_traceback, file=LOGFILE)
            close_logfile()
        sys.exit(1)
    response_email = compose_response(ret, out_text, err_text, info).plain()
    if options.output == True:
        print send_pgp_mime.flatten(response_email)
    else:
        send_pgp_mime.mail(response_email, send_pgp_mime.sendmail)
    close_logfile()

if __name__ == "__main__":
    main()