Mercurial Hosting > nabble
diff src/nabble/model/MailSubsystem.java @ 0:7ecd1a4ef557
add content
author | Franklin Schmidt <fschmidt@gmail.com> |
---|---|
date | Thu, 21 Mar 2019 19:15:52 -0600 |
parents | |
children | 72765b66e2c3 |
line wrap: on
line diff
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/src/nabble/model/MailSubsystem.java Thu Mar 21 19:15:52 2019 -0600 @@ -0,0 +1,133 @@ +package nabble.model; + +import fschmidt.util.mail.Content; +import fschmidt.util.mail.Mail; +import fschmidt.util.mail.MailAddress; +import fschmidt.util.mail.MailHome; +import fschmidt.util.mail.MailParseException; +import fschmidt.util.mail.MixedMultipartContent; +import fschmidt.util.mail.PlainTextContent; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; + +import java.util.ArrayList; +import java.util.Date; +import java.util.List; + + +final class MailSubsystem { + private static final Logger logger = LoggerFactory.getLogger(MailSubsystem.class); + + private MailSubsystem() {} // never + + /** + * Send an error message. + * + * @param mail a mail caused a message, may be null + * @param mailTo sent error message to + * @param subject message subject + * @param errorText error text + */ + static void sendErrorMail(Mail mail, MailAddress mailTo, String subject, String errorText) { + Mail errMail = MailHome.newMail(); + errMail.setFrom(new MailAddress(ModelHome.noReply)); + errMail.setTo(mailTo); + errMail.setSubject(subject); + errMail.setSentDate(new Date()); + Content content = (mail != null) ? + new MixedMultipartContent(new Content[]{ + new PlainTextContent(errorText), + mail, + }) : + new MixedMultipartContent(new Content[]{ + new PlainTextContent(errorText) + }); + errMail.setContent(content); + ModelHome.send(errMail); + } + +/* + static String stripBrackets(String s) { + if (s == null) return null; + int start = s.lastIndexOf('<'); + int end = s.lastIndexOf('>'); + if (start >= 0 && end > start) + s = s.substring(start + 1, end); + return s.trim(); + } +*/ + static String stripBrackets(String s) { + int start = s.lastIndexOf('<'); + int end = s.indexOf('>'); + if( start == -1 && end == -1 ) + return s; + if( start == -1 || end < start ) + throw new MailParseException("no brackets found in '"+s+"'"); + return s.substring(start + 1, end); + } + + static List<String> stripMultiBrackets(final String s) { + List<String> list = new ArrayList<String>(); + for( int i=0; i<s.length(); ) { + int start = s.indexOf('<',i); + int end = s.indexOf('>',i); + if( start == -1 && end == -1 ) + break; + if( start == -1 || end < start ) + throw new MailParseException("malformed brackets found in '"+s+"'"); + list.add( s.substring(start + 1, end) ); + i = end + 1; + } + return list; + } + + + + + /** + * Prepare message headers for sending + * + * @param mail to prepare + * @param node node to be sent + */ + static void setHeaders(Mail mail, NodeImpl node) { + mail.setSentDate(new Date()); + mail.setMessageID("<" + node.getOrGenerateMessageID() + ">"); + NodeImpl parent = node.getParentImpl(); + if (parent != null && parent.getKind() != Node.Kind.APP) { + mail.setHeader("In-Reply-To", "<" + parent.getOrGenerateMessageID() + ">"); + mail.setHeader("References", getReferences(parent, 0).trim()); + } + } + + private static final int MAX_REFERENCES = 10; + + private static String getReferences(NodeImpl node, int count) { + return (node == null || node.getKind() == Node.Kind.APP || count==MAX_REFERENCES) ? + "" : + getReferences(node.getParentImpl(), count+1) + " <" + node.getOrGenerateMessageID() + ">"; + } + + + static String getReturnPath(Mail mail) { + String[] a = mail.getHeader("Return-path"); + return a==null ? null : MailSubsystem.stripBrackets(a[0]); + } + + static void bounce(Mail mail,String msg) { + Mail bounce = MailHome.newMail(); + bounce.setFrom( new MailAddress(ModelHome.noReply,"Nabble") ); + bounce.setTo( new MailAddress(getReturnPath(mail)) ); + bounce.setSubject( "Delivery Status Notification (Failure)" ); + bounce.setHeader( "X-Failed-Recipients", mail.getHeader("Envelope-To") ); + StringBuilder content = new StringBuilder(); + content + .append( msg ) + .append( "\n----- Original message -----\n\n" ) + .append( mail.getRawInput() ) + ; + bounce.setContent(new PlainTextContent(content.toString())); + ModelHome.send(bounce); + } +} +