This is an automated email from the ASF dual-hosted git repository. humbedooh pushed a commit to branch master in repository https://gitbox.apache.org/repos/asf/incubator-ponymail-foal.git
commit 69a19a49dc329e8e53f0fd36d374ea9375a56b9a Author: Daniel Gruno <[email protected]> AuthorDate: Tue Sep 8 12:19:05 2020 +0200 Add endpoint for downloading mbox archives --- server/endpoints/mbox.py | 71 ++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 71 insertions(+) diff --git a/server/endpoints/mbox.py b/server/endpoints/mbox.py new file mode 100644 index 0000000..14b811e --- /dev/null +++ b/server/endpoints/mbox.py @@ -0,0 +1,71 @@ +#!/usr/bin/env python3 +# -*- coding: utf-8 -*- +# Licensed to the Apache Software Foundation (ASF) under one or more +# contributor license agreements. See the NOTICE file distributed with +# this work for additional information regarding copyright ownership. +# The ASF licenses this file to You under the Apache License, Version 2.0 +# (the "License"); you may not use this file except in compliance with +# the License. You may obtain a copy of the License at +# +# http://www.apache.org/licenses/LICENSE-2.0 +# +# Unless required by applicable law or agreed to in writing, software +# distributed under the License is distributed on an "AS IS" BASIS, +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +# See the License for the specific language governing permissions and +# limitations under the License. + +"""Endpoint for returning emails in mbox format as a single archive""" +import plugins.server +import plugins.session +import plugins.mbox +import plugins.defuzzer +import re +import typing +import aiohttp.web + + +async def process( + server: plugins.server.BaseServer, + session: plugins.session.SessionObject, + indata: dict, +) -> typing.Union[dict, aiohttp.web.Response]: + + query_defuzzed = plugins.defuzzer.defuzz(indata) + results = await plugins.mbox.query( + session, query_defuzzed, query_limit=server.config.database.max_hits, + ) + + sources = [] + for email in results: + source = await plugins.mbox.get_source(session, permalink=email["mid"]) + if source: + stext = source["_source"]["source"] + # Convert to mboxrd format + mboxrd_source = "" + line_no = 0 + for line in stext.split("\n"): + line_no += 1 + if line_no > 1 and re.match(r"^>*From\s+", line): + line = ">" + line + mboxrd_source += line + "\n" + sources.append(mboxrd_source) + + # Figure out a sane filename + xlist = re.sub(r"[^-_.a-z0-9]+", "_", indata.get("list", "_")) + xdomain = re.sub(r"[^-_.a-z0-9]+", "_", indata.get("domain", "_")) + dlfile = f"{xlist}-{xdomain}.mbox" + + # Return mbox archive with filename + return aiohttp.web.Response( + headers={ + "Content-Type": "application/mbox", + "Content-Disposition": f"attachment; filename={dlfile}", + }, + status=200, + text="\n\n".join(sources), + ) + + +def register(server: plugins.server.BaseServer): + return plugins.server.Endpoint(process)
