From 0c373984af33a15a57e3c75779cc7392689c021f Mon Sep 17 00:00:00 2001 From: Nikolai R Kristiansen Date: Sun, 29 Mar 2015 15:46:43 +0200 Subject: [PATCH] Initial --- .gitignore | 4 +++ README.md | 6 ++++ mailman_api.py | 71 ++++++++++++++++++++++++++++++++++++++++++++++++ requirements.txt | 1 + 4 files changed, 82 insertions(+) create mode 100644 .gitignore create mode 100644 README.md create mode 100644 mailman_api.py create mode 100644 requirements.txt diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..bc56ebf --- /dev/null +++ b/.gitignore @@ -0,0 +1,4 @@ +*.swp +*.pyc +.idea +venv diff --git a/README.md b/README.md new file mode 100644 index 0000000..1dc3bf6 --- /dev/null +++ b/README.md @@ -0,0 +1,6 @@ +## Installation + virtualenv venv + . venv/bin/activate + pip install -r requirements.txt + python mailman_api.py + diff --git a/mailman_api.py b/mailman_api.py new file mode 100644 index 0000000..a040710 --- /dev/null +++ b/mailman_api.py @@ -0,0 +1,71 @@ +# coding: utf-8 +from __future__ import unicode_literals +from flask import Flask, request, jsonify +import logging +import sys +import re + +sys.path.append('/usr/lib/mailman/') + +try: + from Mailman import Errors, MailList, mm_cfg, Post, Utils +except ImportError: + logging.error('Could not import Mailman module') + + +def matches_regex(pattern, _string): + return re.search(pattern, _string) is not None + + +app = Flask(__name__) + + +@app.route('/lists/', methods=['GET']) +def list_lists_with_members(): + domain_name = request.args.get('domain__name') + source_regex = request.args.get('source_regex') + dest_regex = request.args.get('destination_regex') + + lists_w_members = [] + for listname in Utils.list_names(): + if listname == 'mailman': + continue + + mlist = MailList.MailList(listname, lock=False) + source = mlist.GetListEmail() + + # Correct domain + if domain_name and not source.split()[-1].endswith(domain_name): + continue + + # Matching source + if source_regex and not matches_regex(source_regex, source): + continue + + destinations = mlist.getMembers() + + # Matching at least one destination + if dest_regex and not matches_regex(dest_regex, destinations.join("\n")): + continue + + lists_w_members.append({ + 'destinations': destinations, + 'name': source, + 'type': 'mailman', + 'admin_url': 'https://lists.neuf.no/admin/{}/'.format(listname), + 'admin_type': 'selfservice', + 'num': len(destinations) + }) + + return jsonify({ + 'lists': lists_w_members, + 'num': len(lists_w_members) + }) + + +@app.route('/', methods=['GET']) +def list_lists(): + return jsonify({'lists': Utils.list_names()}) + +if __name__ == '__main__': + app.run() diff --git a/requirements.txt b/requirements.txt new file mode 100644 index 0000000..632a1ef --- /dev/null +++ b/requirements.txt @@ -0,0 +1 @@ +Flask==0.10.1