1 17 18 package org.apache.james.transport.mailets; 19 20 import org.apache.avalon.framework.component.ComponentManager; 21 import org.apache.james.Constants; 22 import org.apache.james.services.UsersRepository; 23 import org.apache.james.services.UsersStore; 24 import org.apache.james.util.RFC2822Headers; 25 import org.apache.james.util.XMLResources; 26 import org.apache.mailet.GenericMailet; 27 import org.apache.mailet.Mail; 28 import org.apache.mailet.MailAddress; 29 import org.apache.mailet.MailetException; 30 31 import javax.mail.MessagingException ; 32 import javax.mail.internet.MimeMessage ; 33 import javax.mail.internet.MimeMultipart ; 34 import javax.mail.internet.ParseException ; 35 import java.io.IOException ; 36 import java.util.*; 37 38 39 65 public class CommandListservProcessor extends GenericMailet { 66 67 74 protected boolean membersOnly; 75 76 83 protected boolean attachmentsAllowed; 84 85 93 protected boolean replyToList; 94 95 103 protected String subjectPrefix; 104 105 113 protected boolean autoBracket; 114 115 121 protected UsersRepository usersRepository; 122 123 129 protected MailAddress listOwner; 130 131 138 protected String listName; 139 140 143 protected ICommandListservManager commandListservManager; 144 145 148 protected CommandListservFooter commandListservFooter; 149 150 153 protected XMLResources xmlResources; 154 155 158 public void init() throws MessagingException { 159 membersOnly = getBoolean("membersonly", false); 160 attachmentsAllowed = getBoolean("attachmentsallowed", true); 161 replyToList = getBoolean("replytolist", true); 162 subjectPrefix = getString("subjectprefix", null); 163 listName = getString("listName", null); 164 autoBracket = getBoolean("autobracket", true); 165 try { 166 listOwner = new MailAddress(getString("listOwner", null)); 167 initializeResources(); 169 initUsersRepository(); 171 } catch (Exception e) { 172 throw new MessagingException (e.getMessage(), e); 173 } 174 } 175 176 181 public void service(Mail mail) throws MessagingException { 182 try { 183 Collection members = new ArrayList(); 184 members.addAll(getMembers()); 185 MailAddress listservAddr = (MailAddress) mail.getRecipients().iterator().next(); 186 187 if (!checkMembers(members, mail)) { 189 return; 190 } 191 192 if (!checkAnnouncements(mail)) { 194 return; 195 } 196 197 if (!checkBeenThere(listservAddr, mail)) { 199 return; 200 } 201 202 addFooter(mail); 204 205 MimeMessage message = prepareListMessage(mail, listservAddr); 207 208 setSubject(message); 210 211 getMailetContext().sendMail(listOwner, members, message); 214 } catch (IOException ioe) { 215 throw new MailetException("Error creating listserv message", ioe); 216 } finally { 217 mail.setState(Mail.GHOST); 219 } 220 } 221 222 227 protected void addFooter(Mail mail) throws MessagingException { 228 getCommandListservFooter().service(mail); 229 } 230 231 protected void setSubject(MimeMessage message) throws MessagingException { 232 String prefix = subjectPrefix; 233 if (prefix != null) { 234 if (autoBracket) { 235 StringBuffer prefixBuffer = 236 new StringBuffer (64) 237 .append("[") 238 .append(prefix) 239 .append("]"); 240 prefix = prefixBuffer.toString(); 241 } 242 String subj = message.getSubject(); 243 if (subj == null) { 244 subj = ""; 245 } 246 subj = normalizeSubject(subj, prefix); 247 AbstractRedirect.changeSubject(message, subj); 248 } 249 } 250 251 258 protected MimeMessage prepareListMessage(Mail mail, MailAddress listservAddr) throws MessagingException { 259 MimeMessage message = new MimeMessage (mail.getMessage()); 261 262 message.removeHeader(RFC2822Headers.RETURN_PATH); 264 265 message.setHeader("X-been-there", listservAddr.toString()); 268 269 if (replyToList) { 271 message.setHeader(RFC2822Headers.REPLY_TO, listservAddr.toString()); 272 } 273 274 return message; 275 } 276 277 288 protected boolean checkBeenThere(MailAddress listservAddr, Mail mail) throws MessagingException { 289 if (listservAddr.equals(mail.getMessage().getHeader("X-been-there"))) { 290 return false; 291 } 292 return true; 293 } 294 295 302 protected boolean checkAnnouncements(Mail mail) throws IOException , MessagingException { 303 if (!attachmentsAllowed && mail.getMessage().getContent() instanceof MimeMultipart ) { 304 Properties standardProperties = getCommandListservManager().getStandardProperties(); 305 306 getCommandListservManager().onError(mail, 307 xmlResources.getString("invalid.mail.subject", standardProperties), 308 xmlResources.getString("error.attachments", standardProperties)); 309 return false; 310 } 311 return true; 312 } 313 314 322 protected boolean checkMembers(Collection members, Mail mail) throws MessagingException { 323 if (membersOnly && !members.contains(mail.getSender())) { 324 Properties standardProperties = getCommandListservManager().getStandardProperties(); 325 getCommandListservManager().onError(mail, 326 xmlResources.getString("invalid.mail.subject", standardProperties), 327 xmlResources.getString("error.membersonly", standardProperties)); 328 329 return false; 330 } 331 return true; 332 } 333 334 public Collection getMembers() throws ParseException { 335 Collection reply = new Vector(); 336 for (Iterator it = usersRepository.list(); it.hasNext();) { 337 String member = it.next().toString(); 338 try { 339 reply.add(new MailAddress(member)); 340 } catch (Exception e) { 341 StringBuffer logBuffer = 344 new StringBuffer (1024) 345 .append("Invalid subscriber address: ") 346 .append(member) 347 .append(" caused: ") 348 .append(e.getMessage()); 349 log(logBuffer.toString()); 350 } 351 } 352 return reply; 353 } 354 355 361 protected boolean getBoolean(String attrName, boolean defValue) { 362 boolean value = defValue; 363 try { 364 value = new Boolean (getInitParameter(attrName)).booleanValue(); 365 } catch (Exception e) { 366 } 368 return value; 369 } 370 371 377 protected String getString(String attrName, String defValue) { 378 String value = defValue; 379 try { 380 value = getInitParameter(attrName); 381 } catch (Exception e) { 382 } 384 return value; 385 } 386 387 391 protected void initializeResources() throws Exception { 392 xmlResources = getCommandListservManager().initXMLResources(new String []{"List Manager"})[0]; 393 } 394 395 398 protected void initUsersRepository() throws Exception { 399 ComponentManager compMgr = (ComponentManager) getMailetContext().getAttribute(Constants.AVALON_COMPONENT_MANAGER); 400 UsersStore usersStore = (UsersStore) compMgr.lookup("org.apache.james.services.UsersStore"); 401 String repName = getInitParameter("repositoryName"); 402 403 usersRepository = usersStore.getRepository(repName); 404 if (usersRepository == null) throw new Exception ("Invalid user repository: " + repName); 405 } 406 407 422 static private String normalizeSubject(final String subj, final String prefix) { 423 427 StringBuffer subject = new StringBuffer (subj); 428 int prefixLength = prefix.length(); 429 430 432 int index = subject.toString().indexOf(prefix); 434 if (index != 0) { 435 if (index > 0) { 437 subject.delete(index, index + prefixLength); 438 } 439 subject.insert(0, prefix); } 441 442 String match = "Re:"; 444 index = subject.toString().indexOf(match, prefixLength); 445 446 while(index > -1) { 447 subject.replace(index, index + match.length(), "RE:"); 449 index = subject.toString().indexOf(match, prefixLength); 450 } 452 453 match ="RE:"; 455 int indexRE = subject.toString().indexOf(match, prefixLength) + match.length(); 456 index = subject.toString().indexOf(match, indexRE); 457 while(index > 0) { 458 subject.delete(index, index + match.length()); 460 index = subject.toString().indexOf(match, indexRE); 461 } 463 464 match = " "; 466 index = subject.toString().indexOf(match, prefixLength); 467 while(index > -1) { 468 subject.replace(index, index + match.length(), " "); 470 index = subject.toString().indexOf(match, prefixLength); 471 } 473 474 475 477 return subject.toString(); 478 } 479 480 484 protected ICommandListservManager getCommandListservManager() { 485 if (commandListservManager == null) { 486 commandListservManager = (ICommandListservManager) getMailetContext().getAttribute(ICommandListservManager.ID + listName); 487 if (commandListservManager == null) { 488 throw new IllegalStateException ("Unable to find command list manager named: " + listName); 489 } 490 } 491 492 return commandListservManager; 493 } 494 495 499 protected CommandListservFooter getCommandListservFooter() throws MessagingException { 500 if (commandListservFooter == null) { 501 commandListservFooter = new CommandListservFooter(getCommandListservManager()); 502 commandListservFooter.init(getMailetConfig()); 503 } 504 return commandListservFooter; 505 } 506 } 507 | Popular Tags |