protected String getEnvelopeRecipient()

in server/protocols/fetchmail/src/main/java/org/apache/james/fetchmail/MessageProcessor.java [931:997]


    protected String getEnvelopeRecipient(MimeMessage msg) throws MessagingException {
        String res = getCustomRecipientHeader();
        if (res != null && res.length() > 0) {
            String[] headers = msg.getHeader(getCustomRecipientHeader());
            if (headers != null) {
                String mailFor = headers[0];
                if (mailFor.startsWith("<") && mailFor.endsWith(">")) {
                    mailFor = mailFor.substring(1, (mailFor.length() - 1));
                }
                return mailFor;
            }
        } else {
            try {
                Enumeration<String> enumeration = msg.getMatchingHeaderLines(new String[]{"Received"});
                while (enumeration.hasMoreElements()) {
                    String received = enumeration.nextElement();

                    int nextSearchAt = 0;
                    int i = 0;
                    int start = 0;
                    int end = 0;
                    boolean usableAddress = false;
                    while (!usableAddress && (i != -1)) {
                        i = received.indexOf("for ", nextSearchAt);
                        if (i > 0) {
                            start = i + 4;
                            end = 0;
                            nextSearchAt = start;
                            for (int c = start; c < received.length(); c++) {
                                char ch = received.charAt(c);
                                switch (ch) {
                                    case '<':
                                        continue;
                                    case '@':
                                        usableAddress = true;
                                        continue;
                                    case ' ':
                                        end = c;
                                        break;
                                    case ';':
                                        end = c;
                                        break;
                                }
                                if (end > 0) {
                                    break;
                                }
                            }
                        }
                    }
                    if (usableAddress) {
                        // lets try and grab the email address
                        String mailFor = received.substring(start, end);

                        // strip the <> around the address if there are any
                        if (mailFor.startsWith("<") && mailFor.endsWith(">")) {
                            mailFor = mailFor.substring(1, (mailFor.length() - 1));
                        }

                        return mailFor;
                    }
                }
            } catch (MessagingException me) {
                logStatusWarn("No Received headers found.");
            }
        }
        return null;
    }