1 17 18 package org.apache.james.fetchmail; 19 20 import java.io.InputStream; 21 import java.net.InetAddress; 22 import java.net.UnknownHostException; 23 import java.util.ArrayList; 24 import java.util.Collection; 25 import java.util.Enumeration; 26 import java.util.Iterator; 27 import java.util.StringTokenizer; 28 29 import javax.mail.Address; 30 import javax.mail.Flags; 31 import javax.mail.Folder; 32 import javax.mail.MessagingException; 33 import javax.mail.Session; 34 import javax.mail.internet.InternetAddress; 35 import javax.mail.internet.InternetHeaders; 36 import javax.mail.internet.MimeMessage; 37 import javax.mail.internet.ParseException; 38 39 import org.apache.james.core.MailImpl; 40 import org.apache.james.util.RFC2822Headers; 41 import org.apache.mailet.Mail; 42 import org.apache.mailet.MailAddress; 43 44 161 public class MessageProcessor extends ProcessorAbstract 162 { 163 private MimeMessage fieldMessageIn; 164 165 168 private boolean fieldRecipientNotFound = false; 169 170 173 private boolean fieldRemoteRecipient = true; 174 175 178 private Boolean fieldRemoteReceivedHeaderInvalid; 179 180 183 private boolean fieldUserUndefined = false; 184 185 188 private Boolean fieldMaxMessageSizeExceeded; 189 190 191 194 static final private String fieldRFC2822RECEIVEDHeaderFields = 195 "from by via with id for ;"; 196 197 200 private boolean fieldBlacklistedRecipient = false; 201 202 205 private String fieldRemoteDomain; 206 207 210 private String fieldRemoteAddress; 211 212 215 private String fieldRemoteHostName; 216 217 222 private MessageProcessor(Account account) 223 { 224 super(account); 225 } 226 227 233 234 MessageProcessor( 235 MimeMessage messageIn, 236 Account account) 237 { 238 this(account); 239 setMessageIn(messageIn); 240 } 241 242 243 248 public void process() throws MessagingException 249 { 250 if (getLogger().isDebugEnabled()) 252 { 253 StringBuffer logMessageBuffer = 254 new StringBuffer("Attempting delivery of message with id. "); 255 logMessageBuffer.append(getMessageIn().getMessageID()); 256 getLogger().debug(logMessageBuffer.toString()); 257 } 258 259 MailAddress intendedRecipient = getIntendedRecipient(); 261 setRecipientNotFound(null == intendedRecipient); 262 263 if (isRecipientNotFound()) 264 { 265 if (isDeferRecipientNotFound()) 266 { 267 268 String messageID = getMessageIn().getMessageID(); 269 if (!getDeferredRecipientNotFoundMessageIDs() 270 .contains(messageID)) 271 { 272 getDeferredRecipientNotFoundMessageIDs().add(messageID); 273 if (getLogger().isDebugEnabled()) 274 { 275 StringBuffer messageBuffer = 276 new StringBuffer("Deferred processing of message for which the intended recipient could not be found. Message ID: "); 277 messageBuffer.append(messageID); 278 getLogger().debug(messageBuffer.toString()); 279 } 280 return; 281 } 282 else 283 { 284 getDeferredRecipientNotFoundMessageIDs().remove(messageID); 285 if (getLogger().isDebugEnabled()) 286 { 287 StringBuffer messageBuffer = 288 new StringBuffer("Processing deferred message for which the intended recipient could not be found. Message ID: "); 289 messageBuffer.append(messageID); 290 getLogger().debug(messageBuffer.toString()); 291 } 292 } 293 } 294 295 if (isRejectRecipientNotFound()) 296 { 297 rejectRecipientNotFound(); 298 return; 299 } 300 intendedRecipient = getRecipient(); 301 StringBuffer messageBuffer = 302 new StringBuffer("Intended recipient not found. Using configured recipient as new envelope recipient - "); 303 messageBuffer.append(intendedRecipient); 304 messageBuffer.append('.'); 305 logStatusInfo(messageBuffer.toString()); 306 } 307 308 setBlacklistedRecipient(isBlacklistedRecipient(intendedRecipient)); 310 setRemoteRecipient(!isLocalServer(intendedRecipient)); 311 setUserUndefined(!isLocalRecipient(intendedRecipient)); 312 313 if (isRejectBlacklisted() && isBlacklistedRecipient()) 315 { 316 rejectBlacklistedRecipient(intendedRecipient); 317 return; 318 } 319 320 if (isRejectRemoteRecipient() && isRemoteRecipient()) 321 { 322 rejectRemoteRecipient(intendedRecipient); 323 return; 324 } 325 326 if (isRejectUserUndefined() && isUserUndefined()) 327 { 328 rejectUserUndefined(intendedRecipient); 329 return; 330 } 331 332 if (isRejectMaxMessageSizeExceeded() 333 && isMaxMessageSizeExceeded().booleanValue()) 334 { 335 rejectMaxMessageSizeExceeded(getMessageIn().getSize()); 336 return; 337 } 338 339 if (isRejectRemoteReceivedHeaderInvalid() 340 && isRemoteReceivedHeaderInvalid().booleanValue()) 341 { 342 rejectRemoteReceivedHeaderInvalid(); 343 return; 344 } 345 346 Mail mail = null; 355 try 356 { 357 mail = createMail(createMessage(), intendedRecipient); 358 } 359 catch (ParseException ex) 360 { 361 handleParseException(ex); 362 return; 363 } 364 catch (UnknownHostException ex) 365 { 366 handleUnknownHostException(ex); 367 return; 368 } 369 370 addMailAttributes(mail); 371 addErrorMessages(mail); 372 373 if (isBouncing()) 375 { 376 handleBouncing(mail); 377 return; 378 } 379 380 sendMail(mail); 382 } 383 384 389 protected void rejectRemoteRecipient(MailAddress recipient) 390 throws MessagingException 391 { 392 if (!isLeaveRemoteRecipient()) 394 setMessageDeleted(); 395 396 if (isMarkRemoteRecipientSeen()) 397 setMessageSeen(); 398 399 StringBuffer messageBuffer = 400 new StringBuffer("Rejected mail intended for remote recipient: "); 401 messageBuffer.append(recipient); 402 messageBuffer.append('.'); 403 logStatusInfo(messageBuffer.toString()); 404 405 return; 406 } 407 408 413 protected void rejectBlacklistedRecipient(MailAddress recipient) 414 throws MessagingException 415 { 416 if (!isLeaveBlacklisted()) 418 setMessageDeleted(); 419 if (isMarkBlacklistedSeen()) 420 setMessageSeen(); 421 422 StringBuffer messageBuffer = 423 new StringBuffer("Rejected mail intended for blacklisted recipient: "); 424 messageBuffer.append(recipient); 425 messageBuffer.append('.'); 426 logStatusInfo(messageBuffer.toString()); 427 428 return; 429 } 430 431 435 protected void rejectRecipientNotFound() throws MessagingException 436 { 437 if (!isLeaveRecipientNotFound()) 439 setMessageDeleted(); 440 441 if (isMarkRecipientNotFoundSeen()) 442 setMessageSeen(); 443 444 StringBuffer messageBuffer = 445 new StringBuffer("Rejected mail for which a sole intended recipient could not be found."); 446 messageBuffer.append(" Recipients: "); 447 Address[] allRecipients = getMessageIn().getAllRecipients(); 448 for (int i = 0; i < allRecipients.length; i++) 449 { 450 messageBuffer.append(allRecipients[i]); 451 messageBuffer.append(' '); 452 } 453 messageBuffer.append('.'); 454 logStatusInfo(messageBuffer.toString()); 455 return; 456 } 457 458 463 protected void rejectUserUndefined(MailAddress recipient) 464 throws MessagingException 465 { 466 if (!isLeaveUserUndefined()) 468 setMessageDeleted(); 469 470 if (isMarkUserUndefinedSeen()) 471 setMessageSeen(); 472 473 StringBuffer messageBuffer = 474 new StringBuffer("Rejected mail intended for undefined user: "); 475 messageBuffer.append(recipient); 476 messageBuffer.append('.'); 477 logStatusInfo(messageBuffer.toString()); 478 479 return; 480 } 481 482 487 protected void rejectMaxMessageSizeExceeded(int messageSize) 488 throws MessagingException 489 { 490 if (!isLeaveMaxMessageSizeExceeded()) 492 setMessageDeleted(); 493 494 if (isMarkMaxMessageSizeExceededSeen()) 495 setMessageSeen(); 496 497 StringBuffer messageBuffer = 498 new StringBuffer("Rejected mail exceeding message size limit. Message size: "); 499 messageBuffer.append(messageSize/1024); 500 messageBuffer.append("KB."); 501 logStatusInfo(messageBuffer.toString()); 502 503 return; 504 } 505 506 510 protected void rejectRemoteReceivedHeaderInvalid() 511 throws MessagingException 512 { 513 if (!isLeaveRemoteReceivedHeaderInvalid()) 515 setMessageDeleted(); 516 517 if (isMarkRemoteReceivedHeaderInvalidSeen()) 518 setMessageSeen(); 519 520 StringBuffer messageBuffer = 521 new StringBuffer("Rejected mail with an invalid Received: header at index "); 522 messageBuffer.append(getRemoteReceivedHeaderIndex()); 523 messageBuffer.append("."); 524 logStatusInfo(messageBuffer.toString()); 525 return; 526 } 527 528 538 protected MimeMessage createMessage() throws MessagingException 539 { 540 MimeMessage messageOut = null; 542 if (isMaxMessageSizeExceeded().booleanValue()) 543 messageOut = createEmptyMessage(); 544 else 545 messageOut = new MimeMessage(getMessageIn()); 546 547 messageOut.addHeader("X-fetched-from", getFetchTaskName()); 551 552 return messageOut; 553 } 554 555 563 protected MimeMessage createEmptyMessage() 564 throws MessagingException 565 { 566 MimeMessage messageOut = new MimeMessage(getSession()); 568 569 Enumeration headersInEnum = getMessageIn().getAllHeaderLines(); 571 while (headersInEnum.hasMoreElements()) 572 messageOut.addHeaderLine((String) headersInEnum.nextElement()); 573 messageOut.setSubject(getMessageIn().getSubject()); 574 575 messageOut.setText(""); 577 578 messageOut.saveChanges(); 580 581 return messageOut; 582 } 583 584 592 protected Mail createMail(MimeMessage message, MailAddress recipient) 593 throws MessagingException, UnknownHostException 594 { 595 Collection recipients = new ArrayList(1); 596 recipients.add(recipient); 597 MailImpl mail = 598 new MailImpl(getServer().getId(), getSender(), recipients, message); 599 if (getRemoteAddress() == null || getRemoteHostName() == null) 602 { 603 mail.setRemoteAddr("127.0.0.1"); 604 mail.setRemoteHost("localhost"); 605 } 606 else 607 { 608 mail.setRemoteAddr(getRemoteAddress()); 609 mail.setRemoteHost(getRemoteHostName()); 610 } 611 612 if (getLogger().isDebugEnabled()) 613 { 614 StringBuffer messageBuffer = 615 new StringBuffer("Created mail with name: "); 616 messageBuffer.append(mail.getName()); 617 messageBuffer.append(", sender: "); 618 messageBuffer.append(mail.getSender()); 619 messageBuffer.append(", recipients: "); 620 Iterator recipientIterator = mail.getRecipients().iterator(); 621 while (recipientIterator.hasNext()) 622 { 623 messageBuffer.append(recipientIterator.next()); 624 messageBuffer.append(' '); 625 } 626 messageBuffer.append(", remote address: "); 627 messageBuffer.append(mail.getRemoteAddr()); 628 messageBuffer.append(", remote host name: "); 629 messageBuffer.append(mail.getRemoteHost()); 630 messageBuffer.append('.'); 631 getLogger().debug(messageBuffer.toString()); 632 } 633 return mail; 634 } 635 636 637 643 protected MailAddress getSender() throws MessagingException 644 { 645 String from = "FETCHMAIL-SERVICE"; 646 try { 647 from = ((InternetAddress) getMessageIn().getFrom()[0]).getAddress().trim(); 648 } 649 catch (Exception _) { 650 getLogger().info("Could not identify sender -- using default value"); 651 } 652 653 InternetAddress internetAddress = null; 654 655 if (from.indexOf('@') < 0) 657 { 658 StringBuffer fromBuffer = new StringBuffer(from); 659 fromBuffer.append('@'); 660 fromBuffer.append(getDefaultDomainName()); 661 internetAddress = new InternetAddress(fromBuffer.toString()); 662 } 663 else 664 internetAddress = new InternetAddress(from); 665 666 return new MailAddress(internetAddress); 667 } 668 669 683 protected String computeRemoteDomain() throws MessagingException 684 { 685 StringBuffer domainBuffer = new StringBuffer(); 686 String[] headers = null; 687 if (getRemoteReceivedHeaderIndex() > -1) 688 getMessageIn().getHeader(RFC2822Headers.RECEIVED); 689 690 if (null != headers) 691 { 692 if (headers.length > 0) 695 { 696 final String headerTokens = " \n\r"; 697 698 for (int headerIndex = 700 headers.length > getRemoteReceivedHeaderIndex() 701 ? getRemoteReceivedHeaderIndex() 702 : headers.length - 1; 703 headerIndex >= 0 && domainBuffer.length() == 0; 704 headerIndex--) 705 { 706 StringTokenizer tokenizer = 708 new StringTokenizer(headers[headerIndex], headerTokens); 709 boolean inFrom = false; 710 while (!inFrom && tokenizer.hasMoreTokens()) 711 inFrom = tokenizer.nextToken().equals("from"); 712 713 while (inFrom && tokenizer.hasMoreTokens()) 716 { 717 String token = tokenizer.nextToken(); 718 if (inFrom = 719 getRFC2822RECEIVEDHeaderFields().indexOf(token) 720 == -1) 721 { 722 domainBuffer.append(token); 723 domainBuffer.append(' '); 724 } 725 } 726 } 727 } 728 } 729 730 if (domainBuffer.length() == 0) 732 domainBuffer.append("localhost"); 733 734 return domainBuffer.toString().trim(); 735 } 736 737 743 protected void handleBouncing(Mail mail) throws MessagingException 744 { 745 mail.setState(Mail.ERROR); 746 setMessageDeleted(); 747 748 mail.setErrorMessage( 749 "This mail from FetchMail task " 750 + getFetchTaskName() 751 + " seems to be bouncing!"); 752 logStatusError("Message is bouncing! Deleted from message store and moved to the Error repository."); 753 } 754 755 760 protected void handleParseException(ParseException ex) 761 throws MessagingException 762 { 763 if (!isLeaveUndeliverable()) 765 setMessageDeleted(); 766 if (isMarkUndeliverableSeen()) 767 setMessageSeen(); 768 logStatusWarn("Message could not be delivered due to an error parsing a mail address."); 769 if (getLogger().isDebugEnabled()) 770 { 771 StringBuffer messageBuffer = 772 new StringBuffer("UNDELIVERABLE Message ID: "); 773 messageBuffer.append(getMessageIn().getMessageID()); 774 getLogger().debug(messageBuffer.toString(), ex); 775 } 776 } 777 778 783 protected void handleUnknownHostException(UnknownHostException ex) 784 throws MessagingException 785 { 786 if (!isLeaveUndeliverable()) 788 setMessageDeleted(); 789 790 if (isMarkUndeliverableSeen()) 791 setMessageSeen(); 792 793 logStatusWarn("Message could not be delivered due to an error determining the remote domain."); 794 if (getLogger().isDebugEnabled()) 795 { 796 StringBuffer messageBuffer = 797 new StringBuffer("UNDELIVERABLE Message ID: "); 798 messageBuffer.append(getMessageIn().getMessageID()); 799 getLogger().debug(messageBuffer.toString(), ex); 800 } 801 } 802 803 808 protected boolean isLocalRecipient(MailAddress recipient) 809 { 810 return isLocalUser(recipient) && isLocalServer(recipient); 811 } 812 813 818 protected boolean isLocalServer(MailAddress recipient) 819 { 820 return getServer().isLocalServer(recipient.getHost()); 821 } 822 823 828 protected boolean isLocalUser(MailAddress recipient) 829 { 830 return getLocalUsers().containsCaseInsensitive(recipient.getUser()); 831 } 832 833 838 protected boolean isBlacklistedRecipient(MailAddress recipient) 839 { 840 return getBlacklist().contains(recipient); 841 } 842 843 849 protected boolean isBouncing() throws MessagingException 850 { 851 Enumeration enum = 852 getMessageIn().getMatchingHeaderLines( 853 new String[] { "X-fetched-from" }); 854 int count = 0; 855 while (enum.hasMoreElements()) 856 { 857 String header = (String) enum.nextElement(); 858 if (header.equals(getFetchTaskName())) 859 count++; 860 } 861 return count >= 3; 862 } 863 864 869 protected void sendMail(Mail mail) throws MessagingException 870 { 871 getServer().sendMail(mail); 873 874 if (!isLeave()) 876 setMessageDeleted(); 877 878 if (isMarkSeen()) 879 setMessageSeen(); 880 881 StringBuffer messageBuffer = 883 new StringBuffer("Spooled message to recipients: "); 884 Iterator recipientIterator = mail.getRecipients().iterator(); 885 while (recipientIterator.hasNext()) 886 { 887 messageBuffer.append(recipientIterator.next()); 888 messageBuffer.append(' '); 889 } 890 messageBuffer.append('.'); 891 logStatusInfo(messageBuffer.toString()); 892 } 893 894 895 905 906 protected String getEnvelopeRecipient(MimeMessage msg) throws MessagingException 907 { 908 try 909 { 910 Enumeration enum = 911 msg.getMatchingHeaderLines(new String[] { "Received" }); 912 while (enum.hasMoreElements()) 913 { 914 String received = (String) enum.nextElement(); 915 916 int nextSearchAt = 0; 917 int i = 0; 918 int start = 0; 919 int end = 0; 920 boolean hasBracket = false; 921 boolean usableAddress = false; 922 while (!usableAddress && (i != -1)) 923 { 924 hasBracket = false; 925 i = received.indexOf("for ", nextSearchAt); 926 if (i > 0) 927 { 928 start = i + 4; 929 end = 0; 930 nextSearchAt = start; 931 for (int c = start; c < received.length(); c++) 932 { 933 char ch = received.charAt(c); 934 switch (ch) 935 { 936 case '<' : 937 hasBracket = true; 938 continue; 939 case '@' : 940 usableAddress = true; 941 continue; 942 case ' ' : 943 end = c; 944 break; 945 case ';' : 946 end = c; 947 break; 948 } 949 if (end > 0) 950 break; 951 } 952 } 953 } 954 if (usableAddress) 955 { 956 String mailFor = received.substring(start, end); 958 959 if (mailFor.startsWith("<") && mailFor.endsWith(">")) 961 mailFor = mailFor.substring(1, (mailFor.length() - 1)); 962 963 return mailFor; 964 } 965 } 966 } 967 catch (MessagingException me) 968 { 969 logStatusWarn("No Received headers found."); 970 } 971 return null; 972 } 973 974 980 protected MailAddress getIntendedRecipient() throws MessagingException 981 { 982 if (isIgnoreRecipientHeader()) 985 { 986 StringBuffer messageBuffer = 987 new StringBuffer("Ignoring recipient header. Using configured recipient as new envelope recipient: "); 988 messageBuffer.append(getRecipient()); 989 messageBuffer.append('.'); 990 logStatusInfo(messageBuffer.toString()); 991 return getRecipient(); 992 } 993 994 String targetRecipient = getEnvelopeRecipient(getMessageIn()); 997 if (targetRecipient != null) 998 { 999 MailAddress recipient = new MailAddress(targetRecipient); 1000 StringBuffer messageBuffer = 1001 new StringBuffer("Using original envelope recipient as new envelope recipient: "); 1002 messageBuffer.append(recipient); 1003 messageBuffer.append('.'); 1004 logStatusInfo(messageBuffer.toString()); 1005 return recipient; 1006 } 1007 1008 Address[] allRecipients = getMessageIn().getAllRecipients(); 1013 if (allRecipients.length == 1) 1014 { 1015 MailAddress recipient = 1016 new MailAddress((InternetAddress) allRecipients[0]); 1017 StringBuffer messageBuffer = 1018 new StringBuffer("Using sole recipient header address as new envelope recipient: "); 1019 messageBuffer.append(recipient); 1020 messageBuffer.append('.'); 1021 logStatusInfo(messageBuffer.toString()); 1022 return recipient; 1023 } 1024 1025 return null; 1026 } 1027 1028 1032 protected MimeMessage getMessageIn() 1033 { 1034 return fieldMessageIn; 1035 } 1036 1037 1041 protected void setMessageIn(MimeMessage messageIn) 1042 { 1043 fieldMessageIn = messageIn; 1044 } 1045 1046 1050 protected boolean isRemoteRecipient() 1051 { 1052 return fieldRemoteRecipient; 1053 } 1054 1055 1060 protected boolean isPreviouslyUnprocessed() 1061 { 1062 return true; 1063 } 1064 1065 1069 protected void logStatusInfo(String detailMsg) throws MessagingException 1070 { 1071 getLogger().info(getStatusReport(detailMsg).toString()); 1072 } 1073 1074 1078 protected void logStatusWarn(String detailMsg) throws MessagingException 1079 { 1080 getLogger().warn(getStatusReport(detailMsg).toString()); 1081 } 1082 1083 1087 protected void logStatusError(String detailMsg) throws MessagingException 1088 { 1089 getLogger().error(getStatusReport(detailMsg).toString()); 1090 } 1091 1092 1099 protected StringBuffer getStatusReport(String detailMsg) throws MessagingException 1100 { 1101 StringBuffer messageBuffer = new StringBuffer(detailMsg); 1102 if (detailMsg.length() > 0) 1103 messageBuffer.append(' '); 1104 messageBuffer.append("Message ID: "); 1105 messageBuffer.append(getMessageIn().getMessageID()); 1106 messageBuffer.append(". Flags: Seen = "); 1107 messageBuffer.append(new Boolean(isMessageSeen())); 1108 messageBuffer.append(", Delete = "); 1109 messageBuffer.append(new Boolean(isMessageDeleted())); 1110 messageBuffer.append('.'); 1111 return messageBuffer; 1112 } 1113 1114 1118 protected boolean isUserUndefined() 1119 { 1120 return fieldUserUndefined; 1121 } 1122 1123 1127 protected boolean isMessageDeleted() throws MessagingException 1128 { 1129 return getMessageIn().isSet(Flags.Flag.DELETED); 1130 } 1131 1132 1136 protected boolean isMessageSeen() throws MessagingException 1137 { 1138 return getMessageIn().isSet(Flags.Flag.SEEN); 1139 } 1140 1141 1145 protected void setMessageDeleted() throws MessagingException 1146 { 1147 getMessageIn().setFlag(Flags.Flag.DELETED, true); 1148 } 1149 1150 1154 protected void setMessageSeen() throws MessagingException 1155 { 1156 if (!getMessageIn() 1159 .getFolder() 1160 .getPermanentFlags() 1161 .contains(Flags.Flag.SEEN)) 1162 handleMarkSeenNotPermanent(); 1163 else 1164 getMessageIn().setFlag(Flags.Flag.SEEN, true); 1165 } 1166 1167 1177 protected void handleMarkSeenNotPermanent() throws MessagingException 1178 { 1179 getMessageIn().setFlag(Flags.Flag.SEEN, true); 1180 logStatusWarn("Message marked as SEEN, but the folder does not support a permanent SEEN flag."); 1181 } 1182 1183 1187 protected boolean isBlacklistedRecipient() 1188 { 1189 return fieldBlacklistedRecipient; 1190 } 1191 1192 1196 protected void setRemoteRecipient(boolean localRecipient) 1197 { 1198 fieldRemoteRecipient = localRecipient; 1199 } 1200 1201 1205 protected void setUserUndefined(boolean userUndefined) 1206 { 1207 fieldUserUndefined = userUndefined; 1208 } 1209 1210 1214 protected void addMailAttributes(Mail aMail) throws MessagingException 1215 { 1216 aMail.setAttribute( 1217 getAttributePrefix() + "taskName", 1218 getFetchTaskName()); 1219 1220 aMail.setAttribute( 1221 getAttributePrefix() + "folderName", 1222 getMessageIn().getFolder().getFullName()); 1223 1224 if (isRemoteRecipient()) 1225 aMail.setAttribute( 1226 getAttributePrefix() + "isRemoteRecipient", 1227 null); 1228 1229 if (isUserUndefined()) 1230 aMail.setAttribute(getAttributePrefix() + "isUserUndefined", null); 1231 1232 if (isBlacklistedRecipient()) 1233 aMail.setAttribute( 1234 getAttributePrefix() + "isBlacklistedRecipient", 1235 null); 1236 1237 if (isRecipientNotFound()) 1238 aMail.setAttribute( 1239 getAttributePrefix() + "isRecipientNotFound", 1240 null); 1241 1242 if (isMaxMessageSizeExceeded().booleanValue()) 1243 aMail.setAttribute( 1244 getAttributePrefix() + "isMaxMessageSizeExceeded", 1245 new Integer(getMessageIn().getSize()).toString()); 1246 1247 if (isRemoteReceivedHeaderInvalid().booleanValue()) 1248 aMail.setAttribute( 1249 getAttributePrefix() + "isRemoteReceivedHeaderInvalid", 1250 null); 1251 } 1252 1253 1257 protected void addErrorMessages(Mail mail) throws MessagingException 1258 { 1259 if (isMaxMessageSizeExceeded().booleanValue()) 1260 { 1261 StringBuffer msgBuffer = 1262 new StringBuffer("550 - Rejected - This message has been rejected as the message size of "); 1263 msgBuffer.append(getMessageIn().getSize() * 1000 / 1024 / 1000f); 1264 msgBuffer.append("KB exceeds the maximum permitted size of "); 1265 msgBuffer.append(getMaxMessageSizeLimit() / 1024); 1266 msgBuffer.append("KB."); 1267 mail.setErrorMessage(msgBuffer.toString()); 1268 } 1269 } 1270 1271 1275 protected void setBlacklistedRecipient(boolean blacklisted) 1276 { 1277 fieldBlacklistedRecipient = blacklisted; 1278 } 1279 1280 1284 protected boolean isRecipientNotFound() 1285 { 1286 return fieldRecipientNotFound; 1287 } 1288 1289 1293 protected void setRecipientNotFound(boolean recipientNotFound) 1294 { 1295 fieldRecipientNotFound = recipientNotFound; 1296 } 1297 1298 1302 protected String getRemoteDomain() throws MessagingException 1303 { 1304 String remoteDomain; 1305 if (null == (remoteDomain = getRemoteDomainBasic())) 1306 { 1307 updateRemoteDomain(); 1308 return getRemoteDomain(); 1309 } 1310 return remoteDomain; 1311 } 1312 1313 1317 private String getRemoteDomainBasic() 1318 { 1319 return fieldRemoteDomain; 1320 } 1321 1322 1326 protected void setRemoteDomain(String remoteDomain) 1327 { 1328 fieldRemoteDomain = remoteDomain; 1329 } 1330 1331 1334 protected void updateRemoteDomain() throws MessagingException 1335 { 1336 setRemoteDomain(computeRemoteDomain()); 1337 } 1338 1339 1346 protected String computeRemoteAddress() 1347 throws MessagingException, UnknownHostException 1348 { 1349 String domain = getRemoteDomain(); 1350 String address = null; 1351 String validatedAddress = null; 1352 int ipAddressStart = domain.indexOf('['); 1353 int ipAddressEnd = -1; 1354 if (ipAddressStart > -1) 1355 ipAddressEnd = domain.indexOf(']', ipAddressStart); 1356 if (ipAddressEnd > -1) 1357 address = domain.substring(ipAddressStart + 1, ipAddressEnd); 1358 else 1359 { 1360 int hostNameEnd = domain.indexOf(' '); 1361 if (hostNameEnd == -1) 1362 hostNameEnd = domain.length(); 1363 address = domain.substring(0, hostNameEnd); 1364 } 1365 validatedAddress = org.apache.james.dnsserver.DNSServer.getByName(address).getHostAddress(); 1366 1367 return validatedAddress; 1368 } 1369 1370 1377 protected String computeRemoteHostName() 1378 throws MessagingException, UnknownHostException 1379 { 1380 InetAddress addr1 = org.apache.james.dnsserver.DNSServer.getByName(getRemoteAddress()); 1384 InetAddress addr2 = org.apache.james.dnsserver.DNSServer.getByName(addr1.getHostAddress()); 1385 return addr2.getHostName(); 1386 } 1387 1388 1392 protected String getRemoteAddress() 1393 throws MessagingException, UnknownHostException 1394 { 1395 String remoteAddress; 1396 if (null == (remoteAddress = getRemoteAddressBasic())) 1397 { 1398 updateRemoteAddress(); 1399 return getRemoteAddress(); 1400 } 1401 return remoteAddress; 1402 } 1403 1404 1408 private String getRemoteAddressBasic() 1409 { 1410 return fieldRemoteAddress; 1411 } 1412 1413 1417 protected String getRemoteHostName() 1418 throws MessagingException, UnknownHostException 1419 { 1420 String remoteHostName; 1421 if (null == (remoteHostName = getRemoteHostNameBasic())) 1422 { 1423 updateRemoteHostName(); 1424 return getRemoteHostName(); 1425 } 1426 return remoteHostName; 1427 } 1428 1429 1433 private String getRemoteHostNameBasic() 1434 { 1435 return fieldRemoteHostName; 1436 } 1437 1438 1442 protected void setRemoteAddress(String remoteAddress) 1443 { 1444 fieldRemoteAddress = remoteAddress; 1445 } 1446 1447 1450 protected void updateRemoteAddress() 1451 throws MessagingException, UnknownHostException 1452 { 1453 setRemoteAddress(computeRemoteAddress()); 1454 } 1455 1456 1460 protected void setRemoteHostName(String remoteHostName) 1461 { 1462 fieldRemoteHostName = remoteHostName; 1463 } 1464 1465 1468 protected void updateRemoteHostName() 1469 throws MessagingException, UnknownHostException 1470 { 1471 setRemoteHostName(computeRemoteHostName()); 1472 } 1473 1474 1478 public static String getRFC2822RECEIVEDHeaderFields() 1479 { 1480 return fieldRFC2822RECEIVEDHeaderFields; 1481 } 1482 1483 1487 protected Boolean isMaxMessageSizeExceeded() throws MessagingException 1488 { 1489 Boolean isMaxMessageSizeExceeded = null; 1490 if (null 1491 == (isMaxMessageSizeExceeded = isMaxMessageSizeExceededBasic())) 1492 { 1493 updateMaxMessageSizeExceeded(); 1494 return isMaxMessageSizeExceeded(); 1495 } 1496 return isMaxMessageSizeExceeded; 1497 } 1498 1499 1502 protected void updateMaxMessageSizeExceeded() throws MessagingException 1503 { 1504 setMaxMessageSizeExceeded(computeMaxMessageSizeExceeded()); 1505 } 1506 1507 1511 protected Boolean computeMaxMessageSizeExceeded() throws MessagingException 1512 { 1513 if (0 == getMaxMessageSizeLimit()) 1514 return Boolean.FALSE; 1515 return new Boolean(getMessageIn().getSize() > getMaxMessageSizeLimit()); 1516 } 1517 1518 1522 private Boolean isMaxMessageSizeExceededBasic() 1523 { 1524 return fieldMaxMessageSizeExceeded; 1525 } 1526 1527 1531 protected void setMaxMessageSizeExceeded(Boolean maxMessageSizeExceeded) 1532 { 1533 fieldMaxMessageSizeExceeded = maxMessageSizeExceeded; 1534 } 1535 1536 1540 protected Boolean isRemoteReceivedHeaderInvalid() throws MessagingException 1541 { 1542 Boolean isInvalid = null; 1543 if (null == (isInvalid = isRemoteReceivedHeaderInvalidBasic())) 1544 { 1545 updateRemoteReceivedHeaderInvalid(); 1546 return isRemoteReceivedHeaderInvalid(); 1547 } 1548 return isInvalid; 1549 } 1550 1551 1555 protected Boolean computeRemoteReceivedHeaderInvalid() 1556 throws MessagingException 1557 { 1558 Boolean isInvalid = Boolean.FALSE; 1559 try 1560 { 1561 getRemoteAddress(); 1562 } 1563 catch (UnknownHostException e) 1564 { 1565 isInvalid = Boolean.TRUE; 1566 } 1567 return isInvalid; 1568 } 1569 1570 1574 private Boolean isRemoteReceivedHeaderInvalidBasic() 1575 { 1576 return fieldRemoteReceivedHeaderInvalid; 1577 } 1578 1579 1583 protected void setRemoteReceivedHeaderInvalid(Boolean remoteReceivedHeaderInvalid) 1584 { 1585 fieldRemoteReceivedHeaderInvalid = remoteReceivedHeaderInvalid; 1586 } 1587 1588 1591 protected void updateRemoteReceivedHeaderInvalid() throws MessagingException 1592 { 1593 setRemoteReceivedHeaderInvalid(computeRemoteReceivedHeaderInvalid()); 1594 } 1595 1596} 1597 | Popular Tags |