1 36 package org.columba.ristretto.message; 37 38 import java.nio.charset.Charset ; 39 import java.util.Date ; 40 import java.util.LinkedList ; 41 import java.util.regex.Matcher ; 42 import java.util.regex.Pattern ; 43 44 import org.columba.ristretto.coder.EncodedWord; 45 import org.columba.ristretto.parser.AddressParser; 46 import org.columba.ristretto.parser.DateParser; 47 import org.columba.ristretto.parser.ParserException; 48 49 55 public class BasicHeader { 56 57 60 public final static int HIGHEST = 5; 61 64 public final static int HIGH = 4; 65 68 public final static int NORMAL = 3; 69 72 public final static int LOW = 2; 73 76 public final static int LOWEST = 1; 77 78 private static final Pattern whiteSpaceTokenizer = Pattern.compile("\\s*([^\\s]+)"); 79 80 private Header header; 81 82 87 public BasicHeader( Header header ) { 88 this.header = header; 89 } 90 91 97 public Address getFrom() { 98 return getAddress(header.get("From")); 99 } 100 101 107 public Address getBeenThere() { 108 Address result = getAddress(header.get("X-BeenThere")); 109 if( result == null ) { 110 result = getAddress(header.get("X-Beenthere")); 111 } 112 113 return result; 114 } 115 116 122 public Address[] getTo() { 123 return getMailboxlist(header.get("To")); 124 } 125 126 132 public Address[] getCc() { 133 return getMailboxlist(header.get("Cc")); 134 } 135 136 142 public Address[] getBcc() { 143 return getMailboxlist(header.get("Bcc")); 144 } 145 146 152 public Address[] getReplyTo() { 153 return getMailboxlist(header.get("Reply-To")); 154 } 155 156 162 public String getSubject() { 163 String subject = header.get("Subject"); 164 if( subject != null) return EncodedWord.decode( subject ).toString(); 165 else return null; 166 } 167 168 173 public String getMessageID() { 174 return header.get("Message-ID"); 175 } 176 177 183 public String [] getReferences() { 184 String references = header.get("References"); 185 return getWhitespaceSeparatedList(references); 186 } 187 188 194 public String [] getInReplyTo() { 195 String references = header.get("In-Reply-To"); 196 return getWhitespaceSeparatedList(references); 197 } 198 199 208 public int getPriority() { 209 String priority = header.get("X-Priority"); 210 if( priority != null ) { 211 try { 212 return Integer.parseInt(priority); 213 } catch (NumberFormatException e) { 214 return NORMAL; 215 } 216 } 217 return NORMAL; 218 } 219 220 227 public Date getDate() { 228 String date = header.get("Date"); 229 if( date != null ) { 230 try { 231 return DateParser.parse( removeComments(date) ); 232 } catch (ParserException e) { 233 return new Date (); 234 } 235 } 236 return new Date (); 237 } 238 239 246 public void setFrom( Address address ) { 247 header.set("From", address.toString() ); 248 } 249 250 257 public void setTo( Address[] addresses ) { 258 setAddressList(addresses, "To"); 259 } 260 261 268 public void setCc( Address[] addresses ) { 269 setAddressList(addresses, "Cc"); 270 } 271 272 279 public void setBcc( Address[] addresses ) { 280 setAddressList(addresses, "Bcc"); 281 } 282 283 290 public void setReplyTo( Address[] addresses ) { 291 setAddressList(addresses, "Reply-To"); 292 } 293 294 301 public void setDate( Date date ) { 302 header.set("Date",MessageDate.toString(date)); 303 } 304 305 320 public void setMessageID(String messageid) { 321 header.set( "Message-ID", messageid); 322 } 323 324 334 public void setReferences( String [] messageids ) { 335 setMessageIDList( "References", messageids ); 336 } 337 338 348 public void setInReplyTo( String [] messageids ) { 349 setMessageIDList( "In-Reply-To", messageids ); 350 } 351 352 358 public void setPriority( int priority ) { 359 header.set( "X-Priority", Integer.toString(priority)); 360 } 361 362 370 public void setSubject( String subject, Charset charset ) { 371 header.set("Subject", EncodedWord.encode(subject, charset,EncodedWord.QUOTED_PRINTABLE)); 372 } 373 374 private void setMessageIDList( String key, String [] messageids) { 375 StringBuffer field = new StringBuffer ( messageids[0] ); 376 for( int i=1; i<messageids.length; i++) { 377 field.append(' '); 378 field.append(messageids[i]); 379 } 380 381 header.set(key, EncodedWord.encode(field, Charset.forName("UTF-8"), EncodedWord.QUOTED_PRINTABLE).toString()); 382 } 383 384 private void setAddressList(Address[] addresses, String fieldKey) { 385 StringBuffer addressfield = new StringBuffer (addresses[0].toString()); 386 387 for( int i=1; i<addresses.length; i++) { 388 addressfield.append( ", "); 389 addressfield.append( addresses[i].toString() ); 390 } 391 392 header.set( fieldKey, addressfield.toString()); 393 } 394 395 private String [] getWhitespaceSeparatedList(String references) { 396 if( references != null ) { 397 LinkedList referenceList = new LinkedList (); 398 Matcher matcher = whiteSpaceTokenizer.matcher(references); 399 while( matcher.matches() ) { 400 referenceList.add(matcher.group(1)); 401 } 402 String [] result = new String [referenceList.size()]; 403 referenceList.toArray((Object []) result); 404 return result; 405 } 406 return new String [0]; 407 } 408 409 private Address[] getMailboxlist(String from) { 410 if( from != null) { 411 try { 412 return AddressParser.parseMailboxList( EncodedWord.decode(removeComments(from)) ); 413 } catch (ParserException e) { 414 e.printStackTrace(); 415 } 416 } 417 return new Address[] {}; 418 } 419 420 private Address getAddress(String from) { 421 if( from != null) { 422 try { 423 return AddressParser.parseAddress( EncodedWord.decode(removeComments(from)) ); 424 } catch (ParserException e) { 425 e.printStackTrace(); 426 } 427 } 428 return null; 429 } 430 431 432 435 public int count() { 436 return header.count(); 437 } 438 439 445 public String get(String key) { 446 return header.get(key); 447 } 448 449 455 public void set(String key, Object value) { 456 header.set(key, value); 457 } 458 459 private static final int PLAIN = 0; 460 private static final int QUOTED = 1; 461 private static final int COMMENT = 2; 462 463 464 471 public static final String removeComments(String value) { 472 StringBuffer result = new StringBuffer ( value.length() ); 473 474 int state = PLAIN; 475 int depth = 0; 476 char current; 477 478 for( int i=0; i < value.length(); i++) { 479 current = value.charAt(i); 480 481 switch( current ) { 482 case( '\"' ) : { 483 if( state == COMMENT ) break; 484 485 if( state == QUOTED ) state = PLAIN; 486 else state = QUOTED; 487 result.append(current); 488 break; 489 } 490 491 case( '(' ) : { 492 if( state == QUOTED ) { 493 result.append(current); 494 break; 495 } 496 497 if( state == COMMENT ) { 498 depth++; 499 } else { 500 state = COMMENT; 501 depth = 1; 502 } 503 break; 504 } 505 506 case( ')') : { 507 if( state == QUOTED ) { 508 result.append(current); 509 break; 510 } 511 if( state == COMMENT ) { 512 depth--; 513 if( depth == 0) state = PLAIN; 514 break; 515 } 516 result.append(current); 517 break; 518 } 519 520 default : { 521 if( state != COMMENT ) result.append( current); 522 } 523 524 } 525 } 526 527 return result.toString(); 528 } 529 530 531 } 532 | Popular Tags |