Merge branch 'main' into lang_de
This commit is contained in:
@@ -1,11 +1,9 @@
|
||||
package de.srsoftware.widerhall.data;
|
||||
|
||||
import de.srsoftware.widerhall.Configuration;
|
||||
import de.srsoftware.widerhall.Util;
|
||||
import de.srsoftware.widerhall.mail.ImapClient;
|
||||
import de.srsoftware.widerhall.mail.MessageHandler;
|
||||
import de.srsoftware.widerhall.mail.SmtpClient;
|
||||
import org.json.simple.JSONObject;
|
||||
import org.slf4j.Logger;
|
||||
import org.slf4j.LoggerFactory;
|
||||
|
||||
@@ -31,6 +29,14 @@ import static de.srsoftware.widerhall.data.User.PERMISSION_ADMIN;
|
||||
* this class encapsulates a MailingList db object
|
||||
*/
|
||||
public class MailingList implements MessageHandler {
|
||||
public static final String KEY_FORWARD_FROM = "forward_from";
|
||||
public static final String KEY_FORWARD_ATTACHED = "forward_attached";
|
||||
public static final String KEY_HIDE_RECEIVERS = "hide_receivers";
|
||||
public static final String KEY_REPLY_TO_LIST = "reply_to_list";
|
||||
public static final String KEY_OPEN_FOR_GUESTS = "open_for_guests";
|
||||
public static final String KEY_OPEN_FOR_SUBSCRIBERS = "open_for_subscribers";
|
||||
public static final String KEY_ARCHIVE = "archive";
|
||||
public static final String KEY_MODS_CAN_EDIT_MODS = "edit_mods";
|
||||
private static final Logger LOG = LoggerFactory.getLogger(MailingList.class);
|
||||
private static final String IMAP_HOST = "imap_host";
|
||||
private static final String IMAP_PORT = "imap_port";
|
||||
@@ -42,14 +48,16 @@ public class MailingList implements MessageHandler {
|
||||
private static final String SMTP_PASS = "smtp_pass";
|
||||
public static final String TABLE_NAME = "Lists";
|
||||
private static final int STATE_PENDING = 0;
|
||||
private static final int STATE_ENABLED = 1;
|
||||
private static final int STATE_PUBLIC = 2;
|
||||
public static final int STATE_FORWARD_FROM = 4;
|
||||
public static final int STATE_FORWARD_ATTACHED = 8;
|
||||
public static final int STATE_HIDE_RECEIVERS = 16;
|
||||
public static final int STATE_REPLY_TO_LIST = 32;
|
||||
public static final int STATE_OPEN = 64;
|
||||
public static final int STATE_PUBLIC_ARCHIVE = 128;
|
||||
private static final int STATE_ENABLED = 1; // do we process incoming messages?
|
||||
private static final int STATE_PUBLIC = 2; // can guests see this ML?
|
||||
public static final int STATE_FORWARD_FROM = 4; // set original sender as FROM when forwarding?
|
||||
public static final int STATE_FORWARD_ATTACHED = 8; // forward messages as attachment?
|
||||
public static final int STATE_HIDE_RECEIVERS = 16; // send using BCC receivers
|
||||
public static final int STATE_REPLY_TO_LIST = 32; // set REPLY TO field to list address?
|
||||
public static final int STATE_OPEN_FOR_GUESTS = 64; // allow anyone to send via this list?
|
||||
public static final int STATE_PUBLIC_ARCHIVE = 128; // save received messages in archive?
|
||||
public static final int STATE_OPEN_FOR_SUBSCRIBERS = 256; // allow mods to send via this list?
|
||||
public static final int STATE_MODS_CAN_EDIT_MODS = 512; // allow mods to make subscribers to mods?
|
||||
private static final int VISIBLE = 1;
|
||||
private static final int HIDDEN = 0;
|
||||
private static final int DEFAULT_STATE = STATE_PENDING|STATE_HIDE_RECEIVERS|STATE_PUBLIC_ARCHIVE;
|
||||
@@ -131,17 +139,6 @@ public class MailingList implements MessageHandler {
|
||||
Database.open().query(sql).compile().run();
|
||||
}
|
||||
|
||||
/**
|
||||
* load the set of mailing lists a given user is allowed to edit
|
||||
* @param user
|
||||
* @return
|
||||
*/
|
||||
public static Set<MailingList> editableBy(User user) {
|
||||
var list = new HashSet<MailingList>();
|
||||
for (String key : ListMember.listsOwnedBy(user)) list.add(load(key));
|
||||
return list;
|
||||
}
|
||||
|
||||
public String email() {
|
||||
return email;
|
||||
}
|
||||
@@ -212,7 +209,7 @@ public class MailingList implements MessageHandler {
|
||||
try {
|
||||
return members().stream().map(ListMember::user).map(User::email).anyMatch(senderEmail::equals);
|
||||
} catch (SQLException e) {
|
||||
LOG.warn("hasMember() failded for {}",email(),e);
|
||||
LOG.warn("hasMember() fehlgeschlagen für {}",email(),e);
|
||||
}
|
||||
return false;
|
||||
}
|
||||
@@ -229,6 +226,20 @@ public class MailingList implements MessageHandler {
|
||||
return setFlag(STATE_HIDE_RECEIVERS,hide);
|
||||
}
|
||||
|
||||
public boolean isAllowedSender(String senderEmail){
|
||||
if (senderEmail == null || senderEmail.isBlank()) return false;
|
||||
try {
|
||||
var user = User.load(senderEmail);
|
||||
if (user == null) return this.isOpenForGuests();
|
||||
var member = ListMember.load(this,user);
|
||||
if (member == null) return this.isOpenForGuests();
|
||||
if (member.isModerator()) return true;
|
||||
if (member.isSubscriber()) return this.isOpenForSubscribers();
|
||||
} catch (SQLException e) {
|
||||
LOG.warn("Laden des Nutzers zu {} fehlgeschlagen",senderEmail,e);
|
||||
}
|
||||
return this.isOpenForGuests();
|
||||
}
|
||||
/**
|
||||
* test, whether the current ML is subscribable by a given user
|
||||
* @param user
|
||||
@@ -246,6 +257,14 @@ public class MailingList implements MessageHandler {
|
||||
}
|
||||
}
|
||||
|
||||
public boolean isOpenForGuests(){
|
||||
return hasState(STATE_OPEN_FOR_GUESTS);
|
||||
}
|
||||
|
||||
public boolean isOpenForSubscribers(){
|
||||
return hasState(STATE_OPEN_FOR_GUESTS|STATE_OPEN_FOR_SUBSCRIBERS);
|
||||
}
|
||||
|
||||
/**
|
||||
* Load a ML object by it's identifying email address.
|
||||
* This is a cached method: if the ML has been loaded before, the already-loaded object will be returned.
|
||||
@@ -267,10 +286,32 @@ public class MailingList implements MessageHandler {
|
||||
return ml;
|
||||
}
|
||||
|
||||
/**
|
||||
* Load a ML object by it's identifying email address.
|
||||
* This is a cached method: if the ML has been loaded before, the already-loaded object will be returned.
|
||||
* @param listEmails
|
||||
* @return
|
||||
*/
|
||||
public static Set<MailingList> loadAll(Collection<String> listEmails) {
|
||||
if (listEmails == null) return null;
|
||||
if (listEmails.isEmpty()) return Set.of();
|
||||
var list = new HashSet<MailingList>();
|
||||
try {
|
||||
var rs = Database.open()
|
||||
.select(TABLE_NAME)
|
||||
.where(EMAIL,listEmails)
|
||||
.compile().exec();
|
||||
while (rs.next()) list.add(MailingList.from(rs));
|
||||
} catch (SQLException e) {
|
||||
LOG.debug("Konnte Mailing-Listen nicht laden: ",e);
|
||||
}
|
||||
return list;
|
||||
}
|
||||
|
||||
public boolean mayBeAlteredBy(User user) {
|
||||
if (user.hashPermission(PERMISSION_ADMIN)) return true;
|
||||
try {
|
||||
if (ListMember.load(this,user).isOwner()) return true;
|
||||
if (ListMember.load(this,user).isModerator()) return true;
|
||||
} catch (SQLException e) {
|
||||
LOG.debug("Fehler beim Laden des Listenmitglieds für ({}, {})",user.email(),email());
|
||||
}
|
||||
@@ -315,6 +356,28 @@ public class MailingList implements MessageHandler {
|
||||
return map;
|
||||
}
|
||||
|
||||
/**
|
||||
* load the set of mailing lists a given user is allowed to edit
|
||||
* @param user
|
||||
* @return
|
||||
*/
|
||||
public static List<MailingList> moderatedBy(User user) {
|
||||
return ListMember.listsOf(user)
|
||||
.stream()
|
||||
.filter(listMember -> listMember.isModerator())
|
||||
.map(ListMember::list)
|
||||
.toList();
|
||||
|
||||
}
|
||||
|
||||
public boolean modsMayEditMods(){
|
||||
return hasState(STATE_MODS_CAN_EDIT_MODS);
|
||||
}
|
||||
|
||||
public MailingList modsMayNominateMods(boolean allowed) throws SQLException {
|
||||
return setFlag(STATE_MODS_CAN_EDIT_MODS,allowed);
|
||||
}
|
||||
|
||||
public String name(){
|
||||
return name;
|
||||
}
|
||||
@@ -336,7 +399,7 @@ public class MailingList implements MessageHandler {
|
||||
Address from = message.getFrom()[0];
|
||||
if (from instanceof InternetAddress internetAddress){
|
||||
var senderEmail = ((InternetAddress) from).getAddress();
|
||||
if (!hasState(STATE_OPEN) && !this.hashMember(senderEmail)){
|
||||
if (!isAllowedSender(senderEmail)) {
|
||||
retainMessage(message);
|
||||
sentRetentionNotification(senderEmail);
|
||||
return;
|
||||
@@ -346,8 +409,12 @@ public class MailingList implements MessageHandler {
|
||||
forward(message);
|
||||
}
|
||||
|
||||
public MailingList open(boolean open) throws SQLException {
|
||||
return setFlag(STATE_OPEN,open);
|
||||
public MailingList openForGuests(boolean open) throws SQLException {
|
||||
return setFlag(STATE_OPEN_FOR_GUESTS,open);
|
||||
}
|
||||
|
||||
public MailingList openForSubscribers(boolean open) throws SQLException {
|
||||
return setFlag(STATE_OPEN_FOR_SUBSCRIBERS,open);
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -450,7 +517,7 @@ public class MailingList implements MessageHandler {
|
||||
private void sendConfirmationRequest(User user, String token) throws MessagingException, UnsupportedEncodingException {
|
||||
var subject = t("Bitte bestätigen Sie ihr Listen-Abonnement");
|
||||
var config = Configuration.instance();
|
||||
var url = new StringBuilder(config.baseUrl()).append("/confirm?token=").append(token);
|
||||
var url = new StringBuilder(config.baseUrl()).append("/web/confirm?token=").append(token);
|
||||
var text = t("Botte gehen Sie zu {} um das Abonnieren der Liste abzuschließen!",url);
|
||||
smtp.send(email(),name(),user.email(),subject,text);
|
||||
}
|
||||
@@ -463,13 +530,13 @@ public class MailingList implements MessageHandler {
|
||||
.map(ListMember::user)
|
||||
.map(User::email)
|
||||
.collect(Collectors.joining(", "));
|
||||
var subject = t("List '{}' requires attention!",name());
|
||||
var text = t("This list received an email from {}, who is not member of the list.\nThe email has been moved to the '{}' folder.\nYou may manually forward this message or drop it.",senderEmail,RETAINED_FOLDER);
|
||||
var subject = t("Liste '{}' erfordert Aufmerksamkeit!",name());
|
||||
var text = t("Diese Liste hat eine E-Mail von {} empfangen. Der Absender ist nicht Mitglied der Liste.\nDie Email wurde in den '{}'-Ordner verschoben.\nSie können die Nachricht manuell weiterleiten oder verwerfen.",senderEmail,RETAINED_FOLDER);
|
||||
smtp.send(email(), name(), receivers,subject,text);
|
||||
|
||||
subject = t("Your message to {} was rejected!",email());
|
||||
text = t("You have tried to send a message to the list '{}', which failed. This is because you are not a member of this list.\n",name());
|
||||
if (hasState(STATE_PUBLIC)) text += t("You may go to {} and subscribe to the list, then try again.",Configuration.instance().baseUrl());
|
||||
subject = t("Ihre Nachricht an {} wurde zurückgewiesen!",email());
|
||||
text = t("Sie haben versucht, eine Nachricht an die Liste '{}' zu senden. Das wurde verweigert, da Sie nicht Mitglied der Liste sind.\n",name());
|
||||
if (hasState(STATE_PUBLIC)) text += t("Sie können zu {} gehen und die Liste abonnieren. Versuchen Sie es danach erneut.",Configuration.instance().baseUrl());
|
||||
smtp.send(email(), name(), senderEmail,subject,text);
|
||||
} catch (SQLException e){
|
||||
LOG.error("Failed to load list of owners of mailing list. Retention notification was not sent to owners of {}",email(),e);
|
||||
@@ -492,7 +559,8 @@ public class MailingList implements MessageHandler {
|
||||
if (hasState(STATE_FORWARD_ATTACHED)) map.put("forward_attached",HIDDEN);
|
||||
if (hasState(STATE_HIDE_RECEIVERS)) map.put("hide_receivers",HIDDEN);
|
||||
if (hasState(STATE_REPLY_TO_LIST)) map.put("reply_to_list",HIDDEN);
|
||||
if (hasState(STATE_OPEN)) map.put("open",VISIBLE);
|
||||
if (hasState(STATE_OPEN_FOR_GUESTS)) map.put("open_for_guests",HIDDEN);
|
||||
if (hasState(STATE_OPEN_FOR_SUBSCRIBERS)) map.put("open_for_subscribers",HIDDEN);
|
||||
if (hasState(STATE_PUBLIC_ARCHIVE)) map.put("archive",VISIBLE);
|
||||
return map;
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user