| 1 | """ |
|---|
| 2 | EmailPostHandler: |
|---|
| 3 | process email sent via a POST request |
|---|
| 4 | """ |
|---|
| 5 | |
|---|
| 6 | from mail2trac.email2trac import mail2project |
|---|
| 7 | from trac.core import * |
|---|
| 8 | from trac.web.api import IRequestHandler |
|---|
| 9 | |
|---|
| 10 | class EmailPostHandler(Component): |
|---|
| 11 | |
|---|
| 12 | implements(IRequestHandler) |
|---|
| 13 | |
|---|
| 14 | ### methods for IRequestHandler |
|---|
| 15 | |
|---|
| 16 | """Extension point interface for request handlers.""" |
|---|
| 17 | |
|---|
| 18 | def match_request(self, req): |
|---|
| 19 | """Return whether the handler wants to process the given request.""" |
|---|
| 20 | match = req.path_info.strip('/') == 'mail2trac' and req.method == 'POST' |
|---|
| 21 | if match: |
|---|
| 22 | # XXX fix up the nonce so that anonymous POSTs are allowed |
|---|
| 23 | req.args['__FORM_TOKEN'] = req.form_token |
|---|
| 24 | return match |
|---|
| 25 | |
|---|
| 26 | def process_request(self, req): |
|---|
| 27 | """Process the request. For ClearSilver, return a (template_name, |
|---|
| 28 | content_type) tuple, where `template` is the ClearSilver template to use |
|---|
| 29 | (either a `neo_cs.CS` object, or the file name of the template), and |
|---|
| 30 | `content_type` is the MIME type of the content. For Genshi, return a |
|---|
| 31 | (template_name, data, content_type) tuple, where `data` is a dictionary |
|---|
| 32 | of substitutions for the template. |
|---|
| 33 | |
|---|
| 34 | For both templating systems, "text/html" is assumed if `content_type` is |
|---|
| 35 | `None`. |
|---|
| 36 | |
|---|
| 37 | Note that if template processing should not occur, this method can |
|---|
| 38 | simply send the response itself and not return anything. |
|---|
| 39 | """ |
|---|
| 40 | message = req.args.get('message', '') |
|---|
| 41 | if message: |
|---|
| 42 | status = 200 |
|---|
| 43 | message = message.encode('utf-8') |
|---|
| 44 | try: |
|---|
| 45 | mail2project(self.env, message) |
|---|
| 46 | except Exception, e: |
|---|
| 47 | req.send(str(e), content_type='text/plain', status=500) |
|---|
| 48 | else: |
|---|
| 49 | status = 204 |
|---|
| 50 | req.send(message, content_type='text/plain', status=status) |
|---|