0
|
1 package nabble.model;
|
|
2
|
|
3 import fschmidt.util.mail.Content;
|
|
4 import fschmidt.util.mail.Mail;
|
|
5 import fschmidt.util.mail.MailAddress;
|
|
6 import fschmidt.util.mail.MailHome;
|
|
7 import fschmidt.util.mail.MailParseException;
|
|
8 import fschmidt.util.mail.MixedMultipartContent;
|
|
9 import fschmidt.util.mail.PlainTextContent;
|
|
10 import org.slf4j.Logger;
|
|
11 import org.slf4j.LoggerFactory;
|
|
12
|
|
13 import java.util.ArrayList;
|
|
14 import java.util.Date;
|
|
15 import java.util.List;
|
|
16
|
|
17
|
|
18 final class MailSubsystem {
|
|
19 private static final Logger logger = LoggerFactory.getLogger(MailSubsystem.class);
|
|
20
|
|
21 private MailSubsystem() {} // never
|
|
22
|
|
23 /**
|
|
24 * Send an error message.
|
|
25 *
|
|
26 * @param mail a mail caused a message, may be null
|
|
27 * @param mailTo sent error message to
|
|
28 * @param subject message subject
|
|
29 * @param errorText error text
|
|
30 */
|
|
31 static void sendErrorMail(Mail mail, MailAddress mailTo, String subject, String errorText) {
|
|
32 Mail errMail = MailHome.newMail();
|
|
33 errMail.setFrom(new MailAddress(ModelHome.noReply));
|
|
34 errMail.setTo(mailTo);
|
|
35 errMail.setSubject(subject);
|
|
36 errMail.setSentDate(new Date());
|
|
37 Content content = (mail != null) ?
|
|
38 new MixedMultipartContent(new Content[]{
|
|
39 new PlainTextContent(errorText),
|
|
40 mail,
|
|
41 }) :
|
|
42 new MixedMultipartContent(new Content[]{
|
|
43 new PlainTextContent(errorText)
|
|
44 });
|
|
45 errMail.setContent(content);
|
|
46 ModelHome.send(errMail);
|
|
47 }
|
|
48
|
|
49 /*
|
|
50 static String stripBrackets(String s) {
|
|
51 if (s == null) return null;
|
|
52 int start = s.lastIndexOf('<');
|
|
53 int end = s.lastIndexOf('>');
|
|
54 if (start >= 0 && end > start)
|
|
55 s = s.substring(start + 1, end);
|
|
56 return s.trim();
|
|
57 }
|
|
58 */
|
|
59 static String stripBrackets(String s) {
|
|
60 int start = s.lastIndexOf('<');
|
|
61 int end = s.indexOf('>');
|
|
62 if( start == -1 && end == -1 )
|
|
63 return s;
|
|
64 if( start == -1 || end < start )
|
|
65 throw new MailParseException("no brackets found in '"+s+"'");
|
|
66 return s.substring(start + 1, end);
|
|
67 }
|
|
68
|
|
69 static List<String> stripMultiBrackets(final String s) {
|
|
70 List<String> list = new ArrayList<String>();
|
|
71 for( int i=0; i<s.length(); ) {
|
|
72 int start = s.indexOf('<',i);
|
|
73 int end = s.indexOf('>',i);
|
|
74 if( start == -1 && end == -1 )
|
|
75 break;
|
|
76 if( start == -1 || end < start )
|
|
77 throw new MailParseException("malformed brackets found in '"+s+"'");
|
|
78 list.add( s.substring(start + 1, end) );
|
|
79 i = end + 1;
|
|
80 }
|
|
81 return list;
|
|
82 }
|
|
83
|
|
84
|
|
85
|
|
86
|
|
87 /**
|
|
88 * Prepare message headers for sending
|
|
89 *
|
|
90 * @param mail to prepare
|
|
91 * @param node node to be sent
|
|
92 */
|
|
93 static void setHeaders(Mail mail, NodeImpl node) {
|
|
94 mail.setSentDate(new Date());
|
|
95 mail.setMessageID("<" + node.getOrGenerateMessageID() + ">");
|
|
96 NodeImpl parent = node.getParentImpl();
|
|
97 if (parent != null && parent.getKind() != Node.Kind.APP) {
|
|
98 mail.setHeader("In-Reply-To", "<" + parent.getOrGenerateMessageID() + ">");
|
|
99 mail.setHeader("References", getReferences(parent, 0).trim());
|
|
100 }
|
|
101 }
|
|
102
|
|
103 private static final int MAX_REFERENCES = 10;
|
|
104
|
|
105 private static String getReferences(NodeImpl node, int count) {
|
|
106 return (node == null || node.getKind() == Node.Kind.APP || count==MAX_REFERENCES) ?
|
|
107 "" :
|
|
108 getReferences(node.getParentImpl(), count+1) + " <" + node.getOrGenerateMessageID() + ">";
|
|
109 }
|
|
110
|
|
111
|
|
112 static String getReturnPath(Mail mail) {
|
|
113 String[] a = mail.getHeader("Return-path");
|
|
114 return a==null ? null : MailSubsystem.stripBrackets(a[0]);
|
|
115 }
|
|
116
|
|
117 static void bounce(Mail mail,String msg) {
|
|
118 Mail bounce = MailHome.newMail();
|
|
119 bounce.setFrom( new MailAddress(ModelHome.noReply,"Nabble") );
|
|
120 bounce.setTo( new MailAddress(getReturnPath(mail)) );
|
|
121 bounce.setSubject( "Delivery Status Notification (Failure)" );
|
|
122 bounce.setHeader( "X-Failed-Recipients", mail.getHeader("Envelope-To") );
|
|
123 StringBuilder content = new StringBuilder();
|
|
124 content
|
|
125 .append( msg )
|
|
126 .append( "\n----- Original message -----\n\n" )
|
|
127 .append( mail.getRawInput() )
|
|
128 ;
|
|
129 bounce.setContent(new PlainTextContent(content.toString()));
|
|
130 ModelHome.send(bounce);
|
|
131 }
|
|
132 }
|
|
133
|