3 # Copyright (C) 2014 W. Trevor King <wking@tremily.us>
5 # Redistribution and use in source and binary forms, with or without
6 # modification, are permitted provided that the following conditions are met:
8 # * Redistributions of source code must retain the above copyright notice, this
9 # list of conditions and the following disclaimer.
11 # * Redistributions in binary form must reproduce the above copyright notice,
12 # this list of conditions and the following disclaimer in the documentation
13 # and/or other materials provided with the distribution.
15 # THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
16 # AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
17 # IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
18 # ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE
19 # LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
20 # CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
21 # SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
22 # INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
23 # CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
24 # ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
25 # POSSIBILITY OF SUCH DAMAGE.
27 """Serve a JSON API for getting/setting notmuch tags with nmbug commits."""
41 app = flask.Flask(__name__)
42 app.config['CORS_HEADERS'] = 'Content-Type'
45 TAG_PREFIX = os.getenv('NMBPREFIX', 'notmuch::')
49 @app.route('/tags', methods=['GET'])
52 database = notmuch.Database(path=NOTMUCH_PATH)
54 for t in database.get_all_tags():
55 if t.startswith(TAG_PREFIX):
56 tags.add(t[len(TAG_PREFIX):])
59 return flask.Response(
60 response=json.dumps(sorted(tags)),
61 mimetype='application/json')
64 def _message_tags(message):
66 tag[len(TAG_PREFIX):] for tag in message.get_tags()
67 if tag.startswith(TAG_PREFIX))
70 @app.route('/mid/<message_id>', methods=['GET', 'POST'])
71 def message_id_tags(message_id):
72 if flask.request.method == 'POST':
73 changes = flask.request.get_json()
75 return flask.Response(status=400)
76 database = notmuch.Database(
78 mode=notmuch.Database.MODE.READ_WRITE)
80 message = database.find_message(message_id)
82 return flask.Response(status=404)
83 database.begin_atomic()
85 for change in changes:
86 if change.startswith('+'):
87 message.add_tag(TAG_PREFIX + change[1:])
88 elif change.startswith('-'):
89 message.remove_tag(TAG_PREFIX + change[1:])
91 return flask.Response(status=400)
94 tags = _message_tags(message=message)
97 nmbug.commit(message='nmhive: {} {}'.format(
98 message_id, ' '.join(changes)))
99 elif flask.request.method == 'GET':
100 database = notmuch.Database(path=NOTMUCH_PATH)
102 message = database.find_message(message_id)
104 return flask.Response(status=404)
105 tags = _message_tags(message=message)
108 return flask.Response(
109 response=json.dumps(tags),
110 mimetype='application/json')
113 @app.route('/gmane/<group>/<int:article>', methods=['GET'])
114 def gmane_message_id(group, article):
115 url = 'http://download.gmane.org/{}/{}/{}'.format(
116 group, article, article + 1)
117 response = urllib.request.urlopen(url=url, timeout=3)
118 mbox_bytes = response.read()
119 with tempfile.NamedTemporaryFile(prefix='nmbug-', suffix='.mbox') as f:
121 mbox = mailbox.mbox(path=f.name)
122 _, message = mbox.popitem()
123 message_id = message['message-id']
124 return flask.Response(
125 response=message_id.lstrip('<').rstrip('>'),
126 mimetype='text/plain')
129 if __name__ == '__main__':
132 parser = argparse.ArgumentParser(description=__doc__)
134 '-H', '--host', default='127.0.0.1',
135 help='The hostname to listen on.')
137 '-p', '--port', type=int, default=5000,
138 help='The port to listen on.')
140 '-d', '--debug', type=bool, default=False,
141 help='Run Flask in debug mode (e.g. show errors).')
143 args = parser.parse_args()
145 app.debug = args.debug
146 app.run(host=args.host, port=args.port)