1 17 18 package org.apache.james.transport.mailets; 19 20 import org.apache.james.util.RFC2822Headers; 21 import org.apache.mailet.GenericMailet; 22 import org.apache.mailet.Mail; 23 import org.apache.mailet.MailAddress; 24 import org.apache.mailet.MailetException; 25 26 import javax.mail.MessagingException ; 27 import javax.mail.internet.MimeMessage ; 28 import javax.mail.internet.MimeMultipart ; 29 import java.io.IOException ; 30 import java.util.Collection ; 31 import java.util.Vector ; 32 33 39 public abstract class GenericListserv extends GenericMailet { 40 41 44 public abstract Collection getMembers() throws MessagingException ; 45 46 49 public abstract boolean isMembersOnly() throws MessagingException ; 50 51 54 public abstract boolean isAttachmentsAllowed() throws MessagingException ; 55 56 59 public abstract boolean isReplyToList() throws MessagingException ; 60 61 66 public MailAddress getListservAddress() throws MessagingException { 67 return null; 68 } 69 70 73 public abstract String getSubjectPrefix() throws MessagingException ; 74 75 82 public boolean isPrefixAutoBracketed() throws MessagingException { 83 return true; } 85 86 101 static private String normalizeSubject(final String subj, final String prefix) { 102 106 StringBuffer subject = new StringBuffer (subj); 107 int prefixLength = prefix.length(); 108 109 111 int index = subject.toString().indexOf(prefix); 113 if (index != 0) { 114 if (index > 0) { 116 subject.delete(index, index + prefixLength); 117 } 118 subject.insert(0, prefix); } 120 121 String match = "Re:"; 123 index = subject.toString().indexOf(match, prefixLength); 124 125 while(index > -1) { 126 subject.replace(index, index + match.length(), "RE:"); 128 index = subject.toString().indexOf(match, prefixLength); 129 } 131 132 match ="RE:"; 134 int indexRE = subject.toString().indexOf(match, prefixLength) + match.length(); 135 index = subject.toString().indexOf(match, indexRE); 136 while(index > 0) { 137 subject.delete(index, index + match.length()); 139 index = subject.toString().indexOf(match, indexRE); 140 } 142 143 match = " "; 145 index = subject.toString().indexOf(match, prefixLength); 146 while(index > -1) { 147 subject.replace(index, index + match.length(), " "); 149 index = subject.toString().indexOf(match, prefixLength); 150 } 152 153 154 156 return subject.toString(); 157 } 158 159 162 public final void service(Mail mail) throws MessagingException { 163 try { 164 Collection members = getMembers(); 165 166 if (isMembersOnly() && !members.contains(mail.getSender())) { 168 getMailetContext().bounce(mail, "Only members of this listserv are allowed to send a message to this address."); 170 mail.setState(Mail.GHOST); 171 return; 172 } 173 174 if (!isAttachmentsAllowed() && mail.getMessage().getContent() instanceof MimeMultipart ) { 176 getMailetContext().bounce(mail, "You cannot send attachments to this listserv."); 177 mail.setState(Mail.GHOST); 178 return; 179 } 180 181 MimeMessage message = new MimeMessage (mail.getMessage()); 183 message.removeHeader(RFC2822Headers.RETURN_PATH); 185 186 MailAddress listservAddr = getListservAddress(); 188 if (listservAddr == null) { 189 listservAddr = (MailAddress)mail.getRecipients().iterator().next(); 191 } 192 193 if (listservAddr.equals(message.getHeader("X-been-there"))) { 197 mail.setState(Mail.GHOST); 198 return; 199 } 200 201 String prefix = getSubjectPrefix(); 203 if (prefix != null) { 204 if (isPrefixAutoBracketed()) { 205 StringBuffer prefixBuffer = 206 new StringBuffer (64) 207 .append("[") 208 .append(prefix) 209 .append("] "); 210 prefix = prefixBuffer.toString(); 211 } 212 String subj = message.getSubject(); 213 if (subj == null) { 214 subj = ""; 215 } 216 subj = normalizeSubject(subj, prefix); 217 AbstractRedirect.changeSubject(message, subj); 218 } 219 220 if (isReplyToList()) { 222 message.setHeader(RFC2822Headers.REPLY_TO, listservAddr.toString()); 223 } 224 message.setHeader("X-been-there", listservAddr.toString()); 227 228 getMailetContext().sendMail(getMailetContext().getPostmaster(), members, message); 231 232 mail.setState(Mail.GHOST); 234 } catch (IOException ioe) { 235 throw new MailetException("Error creating listserv message", ioe); 236 } 237 } 238 } 239 | Popular Tags |