1 package org.columba.mail.composer; 19 20 import java.io.IOException ; 21 import java.io.InputStream ; 22 import java.nio.charset.Charset ; 23 import java.nio.charset.UnsupportedCharsetException ; 24 import java.util.ArrayList ; 25 import java.util.Collections ; 26 import java.util.Iterator ; 27 import java.util.regex.Matcher ; 28 import java.util.regex.Pattern ; 29 30 import org.columba.addressbook.facade.IContactFacade; 31 import org.columba.addressbook.facade.IContactItem; 32 import org.columba.addressbook.facade.IFolder; 33 import org.columba.addressbook.facade.IFolderFacade; 34 import org.columba.addressbook.facade.IModelFacade; 35 import org.columba.api.exception.ServiceNotFoundException; 36 import org.columba.api.exception.StoreException; 37 import org.columba.core.io.StreamUtils; 38 import org.columba.core.xml.XmlElement; 39 import org.columba.mail.config.AccountItem; 40 import org.columba.mail.config.AccountList; 41 import org.columba.mail.config.MailConfig; 42 import org.columba.mail.connector.FacadeUtil; 43 import org.columba.mail.connector.ServiceConnector; 44 import org.columba.mail.gui.composer.ComposerModel; 45 import org.columba.ristretto.coder.Base64DecoderInputStream; 46 import org.columba.ristretto.coder.CharsetDecoderInputStream; 47 import org.columba.ristretto.coder.QuotedPrintableDecoderInputStream; 48 import org.columba.ristretto.message.Address; 49 import org.columba.ristretto.message.Header; 50 import org.columba.ristretto.message.MimeHeader; 51 import org.columba.ristretto.message.MimePart; 52 import org.columba.ristretto.message.StreamableMimePart; 53 54 66 public class MessageBuilderHelper { 67 68 78 public static boolean isAlreadyReply(String subject, String pattern) { 79 if (subject == null) { 80 return false; 81 } 82 83 if (subject.length() == 0) { 84 return false; 85 } 86 87 String str = subject.toLowerCase(); 88 89 if (str.startsWith(pattern) == true) { 91 return true; 92 } 93 94 int index = str.indexOf(pattern); 96 97 if (index != -1) { 98 return true; 99 } 100 101 return false; 102 } 103 104 115 public static String createReplySubject(String subject) { 116 if (!isAlreadyReply(subject, "re:")) { 118 subject = "Re: " + subject; 119 } 120 121 return subject; 122 } 123 124 136 public static String createForwardSubject(String subject) { 137 if (!isAlreadyReply(subject, "fwd:")) { 139 subject = "Fwd: " + subject; 140 } 141 142 return subject; 143 } 144 145 155 public static String createTo(Header header) { 156 String replyTo = (String ) header.get("Reply-To"); 157 String from = (String ) header.get("From"); 158 159 if (replyTo == null) { 160 if (from != null) { 162 return from; 163 } else { 164 return ""; 165 } 166 } else { 167 return replyTo; 168 } 169 } 170 171 183 public static String createToAll(Header header) { 184 String sender = ""; 185 String replyTo = (String ) header.get("Reply-To"); 186 String from = (String ) header.get("From"); 187 String to = (String ) header.get("To"); 188 String cc = (String ) header.get("Cc"); 189 190 if (replyTo == null) { 192 sender = from; 193 } else { 194 sender = replyTo; 195 } 196 197 StringBuffer buf = new StringBuffer (); 199 buf.append(sender); 200 201 if (to != null) { 202 buf.append(", "); 203 buf.append(to); 204 } 205 206 if (cc != null) { 207 buf.append(", "); 208 buf.append(cc); 209 } 210 211 return buf.toString(); 212 } 213 214 224 public static String createToMailinglist(Header header) { 225 String sender = (String ) header.get("X-BeenThere"); 227 228 if (sender == null) { 229 sender = (String ) header.get("X-Beenthere"); 230 } 231 232 if (sender == null) { 233 sender = (String ) header.get("Reply-To"); 234 } 235 236 if (sender == null) { 237 sender = (String ) header.get("From"); 238 } 239 240 return sender; 241 } 242 243 262 public static void createMailingListHeaderItems(Header header, 263 ComposerModel model) { 264 String messageId = (String ) header.get("Message-ID"); 265 266 if (messageId == null) { 267 messageId = (String ) header.get("Message-Id"); 268 } 269 270 if (messageId != null) { 271 model.setHeaderField("In-Reply-To", messageId); 272 273 String references = (String ) header.get("References"); 274 275 if (references != null) { 276 references = references + " " + messageId; 277 references = removeDoubleEntries(references); 278 279 model.setHeaderField("References", references); 280 } 281 } 282 } 283 284 private static String removeDoubleEntries(String input) { 285 Pattern separatorPattern = Pattern.compile("\\s*(<[^\\s<>]+>)\\s*"); 286 ArrayList entries = new ArrayList (); 287 Matcher matcher = separatorPattern.matcher(input); 288 while (matcher.find()) { 289 entries.add(matcher.group(1)); 290 } 291 292 Collections.sort(entries); 293 294 Iterator it = entries.iterator(); 295 StringBuffer result = new StringBuffer (); 296 297 String last = (String ) it.next(); 298 result.append(last); 299 300 while (it.hasNext()) { 301 String next = (String ) it.next(); 302 if (!next.equals(last)) { 303 last = next; 304 result.append(' '); 305 result.append(last); 306 } 307 } 308 309 return result.toString(); 310 } 311 312 318 public static AccountItem getAccountItem(Integer accountUid) { 319 if (MailConfig.getInstance() == null) 320 return null; 321 322 AccountList list = MailConfig.getInstance().getAccountList(); 323 AccountItem accountItem = null; 324 list.getDefaultAccount(); 325 326 if (accountUid != null) { 327 accountItem = list.uidGet(accountUid.intValue()); 328 } 329 330 if (accountItem == null) { 332 accountItem = list.getDefaultAccount(); 333 } 334 335 339 return accountItem; 340 } 341 342 350 public static String createBodyText(MimePart mimePart) throws IOException { 351 StreamableMimePart bodyPart = (StreamableMimePart) mimePart; 352 String charsetName = bodyPart.getHeader() 353 .getContentParameter("charset"); 354 int encoding = bodyPart.getHeader().getContentTransferEncoding(); 355 356 InputStream body = bodyPart.getInputStream(); 357 358 switch (encoding) { 359 case MimeHeader.QUOTED_PRINTABLE: { 360 body = new QuotedPrintableDecoderInputStream(body); 361 362 break; 363 } 364 365 case MimeHeader.BASE64: { 366 body = new Base64DecoderInputStream(body); 367 368 break; 369 } 370 } 371 372 if (charsetName != null) { 373 Charset charset; 374 375 try { 376 charset = Charset.forName(charsetName); 377 } catch (UnsupportedCharsetException e) { 378 charset = Charset.forName(System.getProperty("file.encoding")); 379 } 380 381 body = new CharsetDecoderInputStream(body, charset); 382 } 383 384 String bodyMsg = StreamUtils.readCharacterStream(body).toString(); 385 return bodyMsg; 386 } 387 388 401 public static String createQuotedBodyText(CharSequence bodyText, 402 boolean html) throws IOException { 403 String quotedBodyText; 405 406 if (html) { 407 412 428 quotedBodyText = bodyText.toString(); 429 } else { 430 quotedBodyText = bodyText.toString().replaceAll("(?m)^(.*)$", 432 "> $1"); 433 } 434 435 return quotedBodyText; 436 } 437 438 443 public static boolean isHTMLEnabled() { 444 if (MailConfig.getInstance() == null) 445 return false; 446 447 XmlElement optionsElement = MailConfig.getInstance().get( 449 "composer_options").getElement("/options"); 450 XmlElement htmlElement = optionsElement.getElement("html"); 451 452 if (htmlElement == null) { 454 htmlElement = optionsElement.addSubElement("html"); 455 } 456 457 String enableHtml = htmlElement.getAttribute("enable", "false"); 459 460 return Boolean.valueOf(enableHtml).booleanValue(); 461 } 462 463 464 470 public static void addAddressesToAddressbook(Address[] addresses) { 471 472 try { 473 IContactFacade contactFacade = ServiceConnector.getContactFacade(); 474 IFolderFacade folderFacade = ServiceConnector.getFolderFacade(); 475 IModelFacade modelFacade = ServiceConnector.getModelFacade(); 476 IFolder folder = folderFacade.getCollectedAddresses(); 477 for (int i = 0; i < addresses.length; i++) { 478 try { 479 IContactItem contactItem = modelFacade.createContactItem(); 480 Address addr = addresses[i]; 481 FacadeUtil.getInstance().initContactItem(contactItem, addr.getDisplayName(), addr.getMailAddress()); 482 contactFacade.addContact(folder.getId(), contactItem); 483 } catch (StoreException e) { 484 e.printStackTrace(); 485 } 486 487 } 488 } catch (ServiceNotFoundException e) { 489 e.printStackTrace(); 490 } 491 492 } 493 } | Popular Tags |