1 17 18 package org.apache.james.fetchmail; 19 20 import java.net.InetAddress ; 21 import java.net.UnknownHostException ; 22 import java.util.HashSet ; 23 import java.util.Set ; 24 import java.util.StringTokenizer ; 25 26 import javax.mail.internet.ParseException ; 27 28 import org.apache.avalon.framework.configuration.Configuration; 29 import org.apache.avalon.framework.configuration.ConfigurationException; 30 import org.apache.avalon.framework.logger.Logger; 31 import org.apache.james.services.MailServer; 32 import org.apache.mailet.MailAddress; 33 import org.apache.james.services.UsersRepository; 34 35 42 class ParsedConfiguration 43 { 44 47 private Logger fieldLogger; 48 49 53 private String fieldJavaMailFolderName = "INBOX"; 54 55 56 60 private String fieldJavaMailProviderName = "pop3"; 61 62 63 67 public String getJavaMailFolderName() 68 { 69 return fieldJavaMailFolderName; 70 } 71 72 73 77 public String getJavaMailProviderName() 78 { 79 return fieldJavaMailProviderName; 80 } 81 82 83 87 protected void setJavaMailFolderName(String javaMailFolderName) 88 { 89 fieldJavaMailFolderName = javaMailFolderName; 90 } 91 92 93 97 protected void setJavaMailProviderName(String javaMailProviderName) 98 { 99 fieldJavaMailProviderName = javaMailProviderName; 100 } 101 102 103 104 105 109 private boolean fieldFetchAll = false; 110 111 112 115 private String fieldFetchTaskName; 116 117 118 121 private String fieldHost; 122 123 127 private boolean fieldLeave = false; 128 129 133 private boolean fieldLeaveBlacklisted = true; 134 135 140 private boolean fieldLeaveRemoteRecipient = true; 141 142 147 private boolean fieldLeaveUserUndefined = true; 148 149 154 private boolean fieldLeaveUndeliverable = true; 155 156 157 161 private boolean fieldMarkSeen = true; 162 163 167 private boolean fieldMarkBlacklistedSeen = false; 168 169 173 private boolean fieldMarkRemoteRecipientSeen = false; 174 175 179 private boolean fieldMarkUserUndefinedSeen = false; 180 181 185 private boolean fieldMarkUndeliverableSeen = false; 186 187 191 private boolean fieldDeferRecipientNotFound = false; 192 193 194 197 private boolean fieldRecurse = false; 198 199 200 203 private MailServer fieldServer; 204 205 206 209 private String fieldDefaultDomainName; 210 211 215 private boolean fieldRejectUserUndefined; 216 217 221 private int fieldRemoteReceivedHeaderIndex = -1; 222 223 228 private boolean fieldLeaveRemoteReceivedHeaderInvalid = true; 229 230 234 private boolean fieldMarkRemoteReceivedHeaderInvalidSeen = false; 235 236 239 private boolean fieldRejectRemoteReceivedHeaderInvalid; 240 241 244 private boolean fieldRejectRecipientNotFound; 245 246 250 private boolean fieldLeaveRecipientNotFound; 251 252 256 private boolean fieldMarkRecipientNotFoundSeen; 257 258 261 private boolean fieldRejectBlacklisted; 262 263 267 private boolean fieldRejectRemoteRecipient; 268 269 272 private Set fieldBlacklist; 273 274 278 private int fieldMaxMessageSizeLimit = 0; 279 280 283 private boolean fieldRejectMaxMessageSizeExceeded; 284 285 288 private boolean fieldLeaveMaxMessageSizeExceeded; 289 290 294 private boolean fieldMarkMaxMessageSizeExceededSeen; 295 296 299 private UsersRepository fieldLocalUsers; 300 301 302 303 306 private ParsedConfiguration() 307 { 308 super(); 309 } 310 311 319 public ParsedConfiguration(Configuration configuration, Logger logger, MailServer server, UsersRepository localUsers) throws ConfigurationException 320 { 321 this(); 322 setLogger(logger); 323 setServer(server); 324 setLocalUsers(localUsers); 325 configure(configuration); 326 } 327 328 331 protected void configure(Configuration conf) throws ConfigurationException 332 { 333 setHost(conf.getChild("host").getValue()); 334 335 setFetchTaskName(conf.getAttribute("name")); 336 setJavaMailProviderName( 337 conf.getChild("javaMailProviderName").getValue()); 338 setJavaMailFolderName(conf.getChild("javaMailFolderName").getValue()); 339 setRecurse(conf.getChild("recursesubfolders").getValueAsBoolean()); 340 341 Configuration recipientNotFound = conf.getChild("recipientnotfound"); 342 setDeferRecipientNotFound( 343 recipientNotFound.getAttributeAsBoolean("defer")); 344 setRejectRecipientNotFound( 345 recipientNotFound.getAttributeAsBoolean("reject")); 346 setLeaveRecipientNotFound( 347 recipientNotFound.getAttributeAsBoolean("leaveonserver")); 348 setMarkRecipientNotFoundSeen( 349 recipientNotFound.getAttributeAsBoolean("markseen")); 350 351 Configuration defaultDomainName = conf.getChild("defaultdomain", false); 352 if (null != defaultDomainName) 353 setDefaultDomainName(defaultDomainName.getValue()); 354 355 setFetchAll(conf.getChild("fetchall").getValueAsBoolean()); 356 357 Configuration fetched = conf.getChild("fetched"); 358 setLeave(fetched.getAttributeAsBoolean("leaveonserver")); 359 setMarkSeen(fetched.getAttributeAsBoolean("markseen")); 360 361 Configuration remoterecipient = conf.getChild("remoterecipient"); 362 setRejectRemoteRecipient( 363 remoterecipient.getAttributeAsBoolean("reject")); 364 setLeaveRemoteRecipient( 365 remoterecipient.getAttributeAsBoolean("leaveonserver")); 366 setMarkRemoteRecipientSeen( 367 remoterecipient.getAttributeAsBoolean("markseen")); 368 369 Configuration blacklist = conf.getChild("blacklist"); 370 setBlacklist(blacklist.getValue("")); 371 setRejectBlacklisted(blacklist.getAttributeAsBoolean("reject")); 372 setLeaveBlacklisted(blacklist.getAttributeAsBoolean("leaveonserver")); 373 setMarkBlacklistedSeen(blacklist.getAttributeAsBoolean("markseen")); 374 375 Configuration userundefined = conf.getChild("userundefined"); 376 setRejectUserUndefined(userundefined.getAttributeAsBoolean("reject")); 377 setLeaveUserUndefined( 378 userundefined.getAttributeAsBoolean("leaveonserver")); 379 setMarkUserUndefinedSeen( 380 userundefined.getAttributeAsBoolean("markseen")); 381 382 Configuration undeliverable = conf.getChild("undeliverable"); 383 setLeaveUndeliverable( 384 undeliverable.getAttributeAsBoolean("leaveonserver")); 385 setMarkUndeliverableSeen( 386 undeliverable.getAttributeAsBoolean("markseen")); 387 388 Configuration remotereceivedheader = conf.getChild("remotereceivedheader", false); 389 if (null != remotereceivedheader) 390 { 391 setRemoteReceivedHeaderIndex( 392 remotereceivedheader.getAttributeAsInteger("index")); 393 setRejectRemoteReceivedHeaderInvalid( 394 remotereceivedheader.getAttributeAsBoolean("reject")); 395 setLeaveRemoteReceivedHeaderInvalid( 396 remotereceivedheader.getAttributeAsBoolean("leaveonserver")); 397 setMarkRemoteReceivedHeaderInvalidSeen( 398 remotereceivedheader.getAttributeAsBoolean("markseen")); 399 } 400 401 Configuration maxmessagesize = conf.getChild("maxmessagesize", false); 402 if (null != maxmessagesize) 403 { 404 setMaxMessageSizeLimit( 405 maxmessagesize.getAttributeAsInteger("limit") * 1024); 406 setRejectMaxMessageSizeExceeded( 407 maxmessagesize.getAttributeAsBoolean("reject")); 408 setLeaveMaxMessageSizeExceeded( 409 maxmessagesize.getAttributeAsBoolean("leaveonserver")); 410 setMarkMaxMessageSizeExceededSeen( 411 maxmessagesize.getAttributeAsBoolean("markseen")); 412 } 413 414 if (getLogger().isDebugEnabled()) 415 { 416 getLogger().info( 417 "Configured FetchMail fetch task " + getFetchTaskName()); 418 } 419 } 420 421 422 423 424 428 public boolean isFetchAll() 429 { 430 return fieldFetchAll; 431 } 432 433 437 public String getFetchTaskName() 438 { 439 return fieldFetchTaskName; 440 } 441 442 446 public String getHost() 447 { 448 return fieldHost; 449 } 450 451 455 public boolean isLeave() 456 { 457 return fieldLeave; 458 } 459 460 464 public boolean isMarkSeen() 465 { 466 return fieldMarkSeen; 467 } 468 469 476 protected boolean isOpenReadOnly() 477 { 478 return isLeave() 479 && !isMarkSeen() 480 && isLeaveBlacklisted() 481 && !isMarkBlacklistedSeen() 482 && isLeaveRemoteRecipient() 483 && !isMarkRemoteRecipientSeen() 484 && isLeaveUserUndefined() 485 && !isMarkUserUndefinedSeen() 486 && isLeaveUndeliverable() 487 && !isMarkUndeliverableSeen() 488 && isLeaveMaxMessageSizeExceeded() 489 && !isMarkMaxMessageSizeExceededSeen() 490 && isLeaveRemoteReceivedHeaderInvalid() 491 && !isMarkRemoteReceivedHeaderInvalidSeen() 492 ; 493 } 494 495 499 public boolean isRecurse() 500 { 501 return fieldRecurse; 502 } 503 504 508 public MailServer getServer() 509 { 510 return fieldServer; 511 } 512 513 517 protected void setFetchAll(boolean fetchAll) 518 { 519 fieldFetchAll = fetchAll; 520 } 521 522 526 protected void setFetchTaskName(String fetchTaskName) 527 { 528 fieldFetchTaskName = fetchTaskName; 529 } 530 531 535 protected void setHost(String host) 536 { 537 fieldHost = host; 538 } 539 540 544 protected void setLeave(boolean keep) 545 { 546 fieldLeave = keep; 547 } 548 549 553 protected void setMarkSeen(boolean markSeen) 554 { 555 fieldMarkSeen = markSeen; 556 } 557 558 562 protected void setRecurse(boolean recurse) 563 { 564 fieldRecurse = recurse; 565 } 566 567 571 protected void setServer(MailServer server) 572 { 573 fieldServer = server; 574 } 575 576 580 public Logger getLogger() 581 { 582 return fieldLogger; 583 } 584 585 589 protected void setLogger(Logger logger) 590 { 591 fieldLogger = logger; 592 } 593 594 598 public UsersRepository getLocalUsers() 599 { 600 return fieldLocalUsers; 601 } 602 603 607 protected void setLocalUsers(UsersRepository localUsers) 608 { 609 fieldLocalUsers = localUsers; 610 } 611 612 616 public boolean isLeaveBlacklisted() 617 { 618 return fieldLeaveBlacklisted; 619 } 620 621 625 public boolean isMarkBlacklistedSeen() 626 { 627 return fieldMarkBlacklistedSeen; 628 } 629 630 634 protected void setLeaveBlacklisted(boolean keepRejected) 635 { 636 fieldLeaveBlacklisted = keepRejected; 637 } 638 639 643 protected void setMarkBlacklistedSeen(boolean markRejectedSeen) 644 { 645 fieldMarkBlacklistedSeen = markRejectedSeen; 646 } 647 648 652 public Set getBlacklist() 653 { 654 return fieldBlacklist; 655 } 656 657 661 protected void setBlacklist(Set blacklist) 662 { 663 fieldBlacklist = blacklist; 664 } 665 666 670 protected void setBlacklist(String blacklistValue) 671 throws ConfigurationException 672 { 673 StringTokenizer st = new StringTokenizer (blacklistValue, ", \t", false); 674 Set blacklist = new HashSet (); 675 String token = null; 676 while (st.hasMoreTokens()) 677 { 678 try 679 { 680 token = st.nextToken(); 681 blacklist.add(new MailAddress(token)); 682 } 683 catch (ParseException pe) 684 { 685 throw new ConfigurationException( 686 "Invalid blacklist mail address specified: " + token); 687 } 688 } 689 setBlacklist(blacklist); 690 } 691 692 696 public boolean isRejectUserUndefined() 697 { 698 return fieldRejectUserUndefined; 699 } 700 701 705 protected void setRejectUserUndefined(boolean localRecipientsOnly) 706 { 707 fieldRejectUserUndefined = localRecipientsOnly; 708 } 709 710 714 public boolean isMarkUserUndefinedSeen() 715 { 716 return fieldMarkUserUndefinedSeen; 717 } 718 719 723 protected void setMarkUserUndefinedSeen(boolean markExternalSeen) 724 { 725 fieldMarkUserUndefinedSeen = markExternalSeen; 726 } 727 728 732 public boolean isLeaveUserUndefined() 733 { 734 return fieldLeaveUserUndefined; 735 } 736 737 741 protected void setLeaveUserUndefined(boolean leaveExternal) 742 { 743 fieldLeaveUserUndefined = leaveExternal; 744 } 745 746 750 public boolean isLeaveRemoteRecipient() 751 { 752 return fieldLeaveRemoteRecipient; 753 } 754 755 759 public boolean isMarkRemoteRecipientSeen() 760 { 761 return fieldMarkRemoteRecipientSeen; 762 } 763 764 768 protected void setLeaveRemoteRecipient(boolean leaveRemoteRecipient) 769 { 770 fieldLeaveRemoteRecipient = leaveRemoteRecipient; 771 } 772 773 777 protected void setMarkRemoteRecipientSeen(boolean markRemoteRecipientSeen) 778 { 779 fieldMarkRemoteRecipientSeen = markRemoteRecipientSeen; 780 } 781 782 786 public boolean isRejectRemoteRecipient() 787 { 788 return fieldRejectRemoteRecipient; 789 } 790 791 795 protected void setRejectRemoteRecipient(boolean rejectRemoteRecipient) 796 { 797 fieldRejectRemoteRecipient = rejectRemoteRecipient; 798 } 799 800 804 public String getDefaultDomainName() 805 { 806 String defaultDomainName = null; 807 if (null == (defaultDomainName = getDefaultDomainNameBasic())) 808 { 809 updateDefaultDomainName(); 810 return getDefaultDomainName(); 811 } 812 return defaultDomainName; 813 } 814 815 819 private String getDefaultDomainNameBasic() 820 { 821 return fieldDefaultDomainName; 822 } 823 824 828 protected void setDefaultDomainName(String defaultDomainName) throws ConfigurationException 829 { 830 validateDefaultDomainName(defaultDomainName); 831 setDefaultDomainNameBasic(defaultDomainName); 832 } 833 834 838 private void setDefaultDomainNameBasic(String defaultDomainName) 839 { 840 fieldDefaultDomainName = defaultDomainName; 841 } 842 843 847 protected void validateDefaultDomainName(String defaultDomainName) throws ConfigurationException 848 { 849 if (!getServer().isLocalServer(defaultDomainName)) 850 { 851 throw new ConfigurationException( 852 "Default domain name is not a local server: " 853 + defaultDomainName); 854 } 855 } 856 857 860 protected String computeDefaultDomainName() 861 { 862 String hostName = null; 863 try 864 { 865 InetAddress addr1 = java.net.InetAddress.getLocalHost(); 869 InetAddress addr2 = addr1.getByName(addr1.getHostAddress()); 870 hostName = addr2.getHostName(); 871 } 872 catch (UnknownHostException ue) 873 { 874 hostName = "localhost"; 875 } 876 return hostName; 877 } 878 879 882 protected void updateDefaultDomainName() 883 { 884 setDefaultDomainNameBasic(computeDefaultDomainName()); 885 } 886 887 891 public boolean isLeaveUndeliverable() 892 { 893 return fieldLeaveUndeliverable; 894 } 895 896 900 public boolean isMarkUndeliverableSeen() 901 { 902 return fieldMarkUndeliverableSeen; 903 } 904 905 909 protected void setLeaveUndeliverable(boolean leaveUndeliverable) 910 { 911 fieldLeaveUndeliverable = leaveUndeliverable; 912 } 913 914 918 protected void setMarkUndeliverableSeen(boolean markUndeliverableSeen) 919 { 920 fieldMarkUndeliverableSeen = markUndeliverableSeen; 921 } 922 923 927 public boolean isRejectBlacklisted() 928 { 929 return fieldRejectBlacklisted; 930 } 931 932 936 protected void setRejectBlacklisted(boolean rejectBlacklisted) 937 { 938 fieldRejectBlacklisted = rejectBlacklisted; 939 } 940 941 945 public boolean isLeaveRecipientNotFound() 946 { 947 return fieldLeaveRecipientNotFound; 948 } 949 950 954 public boolean isMarkRecipientNotFoundSeen() 955 { 956 return fieldMarkRecipientNotFoundSeen; 957 } 958 959 963 public boolean isRejectRecipientNotFound() 964 { 965 return fieldRejectRecipientNotFound; 966 } 967 968 972 protected void setLeaveRecipientNotFound(boolean leaveRecipientNotFound) 973 { 974 fieldLeaveRecipientNotFound = leaveRecipientNotFound; 975 } 976 977 981 protected void setMarkRecipientNotFoundSeen(boolean markRecipientNotFoundSeen) 982 { 983 fieldMarkRecipientNotFoundSeen = markRecipientNotFoundSeen; 984 } 985 986 990 protected void setRejectRecipientNotFound(boolean rejectRecipientNotFound) 991 { 992 fieldRejectRecipientNotFound = rejectRecipientNotFound; 993 } 994 995 999 public boolean isDeferRecipientNotFound() 1000 { 1001 return fieldDeferRecipientNotFound; 1002 } 1003 1004 1008 protected void setDeferRecipientNotFound(boolean deferRecipientNotFound) 1009 { 1010 fieldDeferRecipientNotFound = deferRecipientNotFound; 1011 } 1012 1013 1017 public int getRemoteReceivedHeaderIndex() 1018 { 1019 return fieldRemoteReceivedHeaderIndex; 1020 } 1021 1022 1026 protected void setRemoteReceivedHeaderIndex(int remoteReceivedHeaderIndex) 1027 { 1028 fieldRemoteReceivedHeaderIndex = remoteReceivedHeaderIndex; 1029 } 1030 1031 1035 public boolean isLeaveMaxMessageSizeExceeded() 1036 { 1037 return fieldLeaveMaxMessageSizeExceeded; 1038 } 1039 1040 1044 public boolean isMarkMaxMessageSizeExceededSeen() 1045 { 1046 return fieldMarkMaxMessageSizeExceededSeen; 1047 } 1048 1049 1053 public int getMaxMessageSizeLimit() 1054 { 1055 return fieldMaxMessageSizeLimit; 1056 } 1057 1058 1062 public boolean isRejectMaxMessageSizeExceeded() 1063 { 1064 return fieldRejectMaxMessageSizeExceeded; 1065 } 1066 1067 1071 protected void setLeaveMaxMessageSizeExceeded(boolean leaveMaxMessageSize) 1072 { 1073 fieldLeaveMaxMessageSizeExceeded = leaveMaxMessageSize; 1074 } 1075 1076 1080 protected void setMarkMaxMessageSizeExceededSeen(boolean markMaxMessageSizeSeen) 1081 { 1082 fieldMarkMaxMessageSizeExceededSeen = markMaxMessageSizeSeen; 1083 } 1084 1085 1089 protected void setMaxMessageSizeLimit(int maxMessageSizeLimit) 1090 { 1091 fieldMaxMessageSizeLimit = maxMessageSizeLimit; 1092 } 1093 1094 1098 protected void setRejectMaxMessageSizeExceeded(boolean rejectMaxMessageSize) 1099 { 1100 fieldRejectMaxMessageSizeExceeded = rejectMaxMessageSize; 1101 } 1102 1103 1107 public boolean isLeaveRemoteReceivedHeaderInvalid() 1108 { 1109 return fieldLeaveRemoteReceivedHeaderInvalid; 1110 } 1111 1112 1116 public boolean isMarkRemoteReceivedHeaderInvalidSeen() 1117 { 1118 return fieldMarkRemoteReceivedHeaderInvalidSeen; 1119 } 1120 1121 1125 public boolean isRejectRemoteReceivedHeaderInvalid() 1126 { 1127 return fieldRejectRemoteReceivedHeaderInvalid; 1128 } 1129 1130 1134 protected void setLeaveRemoteReceivedHeaderInvalid(boolean leaveRemoteReceivedHeaderInvalid) 1135 { 1136 fieldLeaveRemoteReceivedHeaderInvalid = 1137 leaveRemoteReceivedHeaderInvalid; 1138 } 1139 1140 1144 protected void setMarkRemoteReceivedHeaderInvalidSeen(boolean markRemoteReceivedHeaderInvalidSeen) 1145 { 1146 fieldMarkRemoteReceivedHeaderInvalidSeen = 1147 markRemoteReceivedHeaderInvalidSeen; 1148 } 1149 1150 1154 protected void setRejectRemoteReceivedHeaderInvalid(boolean rejectRemoteReceivedHeaderInvalid) 1155 { 1156 fieldRejectRemoteReceivedHeaderInvalid = 1157 rejectRemoteReceivedHeaderInvalid; 1158 } 1159 1160} 1161 | Popular Tags |