1 /*
  2  * Copyright (c) 2019, Oracle and/or its affiliates. All rights reserved.
  3  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
  4  *
  5  * This code is free software; you can redistribute it and/or modify it
  6  * under the terms of the GNU General Public License version 2 only, as
  7  * published by the Free Software Foundation.
  8  *
  9  * This code is distributed in the hope that it will be useful, but WITHOUT
 10  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
 11  * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
 12  * version 2 for more details (a copy is included in the LICENSE file that
 13  * accompanied this code).
 14  *
 15  * You should have received a copy of the GNU General Public License version
 16  * 2 along with this work; if not, write to the Free Software Foundation,
 17  * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
 18  *
 19  * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
 20  * or visit www.oracle.com if you need additional information or have any
 21  * questions.
 22  */
 23 package org.openjdk.skara.bots.mlbridge;
 24 
 25 import org.openjdk.skara.bot.*;
 26 import org.openjdk.skara.email.EmailAddress;
 27 import org.openjdk.skara.host.HostedRepository;
 28 import org.openjdk.skara.host.network.URIBuilder;
 29 import org.openjdk.skara.json.*;
 30 import org.openjdk.skara.mailinglist.MailingListServerFactory;
 31 
 32 import java.nio.file.Path;
 33 import java.util.*;
 34 import java.util.regex.Pattern;
 35 import java.util.stream.Collectors;
 36 
 37 public class MailingListBridgeBotFactory implements BotFactory {
 38     @Override
 39     public String name() {
 40         return "mlbridge";
 41     }
 42 
 43     @Override
 44     public List<Bot> create(BotConfiguration configuration) {
 45         var ret = new ArrayList<Bot>();
 46         var specific = configuration.specific();
 47 
 48         var from = EmailAddress.from(specific.get("name").asString(), specific.get("mail").asString());
 49         var ignoredUsers = specific.get("ignored").get("users").stream()
 50                                    .map(JSONValue::asString)
 51                                    .collect(Collectors.toSet());
 52         var ignoredComments = specific.get("ignored").get("comments").stream()
 53                                       .map(JSONValue::asString)
 54                                       .map(pattern -> Pattern.compile(pattern, Pattern.MULTILINE | Pattern.DOTALL))
 55                                       .collect(Collectors.toSet());
 56         var listArchive = URIBuilder.base(specific.get("server").get("archive").asString()).build();
 57         var listSmtp = specific.get("server").get("smtp").asString();
 58 
 59         var webrevRepo = configuration.repository(specific.get("webrevs").get("repository").asString());
 60         var webrevRef = configuration.repositoryRef(specific.get("webrevs").get("repository").asString());
 61         var webrevWeb = specific.get("webrevs").get("web").asString();
 62 
 63         var archiveRepo = configuration.repository(specific.get("archive").asString());
 64 
 65         var allListNames = new HashSet<EmailAddress>();
 66         var allRepositories = new HashSet<HostedRepository>();
 67 
 68         var readyLabels = specific.get("ready").get("labels").stream()
 69                 .map(JSONValue::asString)
 70                 .collect(Collectors.toSet());
 71         var readyComments = specific.get("ready").get("comments").stream()
 72                 .map(JSONValue::asObject)
 73                 .collect(Collectors.toMap(obj -> obj.get("user").asString(),
 74                                           obj -> Pattern.compile(obj.get("pattern").asString())));
 75 
 76         for (var repoConfig : specific.get("repositories").asArray()) {
 77             var repo = repoConfig.get("repository").asString();
 78             var censusRepo = configuration.repository(repoConfig.get("census").asString());
 79             var censusRef = configuration.repositoryRef(repoConfig.get("census").asString());
 80 
 81             var list = EmailAddress.parse(repoConfig.get("list").asString());
 82             var folder = repoConfig.contains("folder") ? repoConfig.get("folder").asString() : configuration.repositoryName(repo);
 83             var bot = new MailingListBridgeBot(from, configuration.repository(repo), archiveRepo,
 84                                                censusRepo, censusRef,
 85                                                list, ignoredUsers, ignoredComments, listArchive, listSmtp,
 86                                                webrevRepo, webrevRef, Path.of(folder),
 87                                                URIBuilder.base(webrevWeb).build(), readyLabels, readyComments);
 88             ret.add(bot);
 89 
 90             allListNames.add(list);
 91             allRepositories.add(configuration.repository(repo));
 92         }
 93 
 94         var mailmanServer = MailingListServerFactory.createMailmanServer(listArchive, listSmtp);
 95         var allLists = allListNames.stream()
 96                                    .map(name -> mailmanServer.getList(name.toString()))
 97                                    .collect(Collectors.toSet());
 98 
 99         var bot = new MailingListArchiveReaderBot(from, allLists, allRepositories);
100         ret.add(bot);
101 
102         return ret;
103     }
104 }