Search in sources :

Example 1 with MimeAddressHeader

use of com.zimbra.common.mime.MimeAddressHeader in project zm-mailbox by Zimbra.

the class MilterHandlerTest method testGetToCcAddressHeaderAscii.

@Test
public void testGetToCcAddressHeaderAscii() throws IOException {
    String to = "To" + '\0' + "admin@example.com; admin2@example.com";
    List<String> expctedEmails = new ArrayList<String>();
    expctedEmails.add("admin@example.com");
    expctedEmails.add("admin2@example.com");
    MimeAddressHeader mh = MilterHandler.getToCcAddressHeader(to.getBytes("iso-8859-1"));
    Assert.assertEquals(2, mh.getAddresses().size());
    for (InternetAddress addrs : mh.getAddresses()) {
        expctedEmails.contains(addrs.getAddress());
    }
    to = "To" + '\0';
    mh = MilterHandler.getToCcAddressHeader(to.getBytes("iso-8859-1"));
    Assert.assertNull(mh);
}
Also used : InternetAddress(com.zimbra.common.mime.InternetAddress) MimeAddressHeader(com.zimbra.common.mime.MimeAddressHeader) ArrayList(java.util.ArrayList) Test(org.junit.Test)

Example 2 with MimeAddressHeader

use of com.zimbra.common.mime.MimeAddressHeader in project zm-mailbox by Zimbra.

the class MilterHandler method getToCcAddressHeader.

static MimeAddressHeader getToCcAddressHeader(byte[] bytes) {
    MimeAddressHeader mHeader = null;
    try {
        int i = 0;
        String key = null;
        if (bytes.length > 0) {
            while (i < bytes.length && bytes[i] != 0x00) {
                i++;
            }
        }
        key = new String(Arrays.copyOfRange(bytes, 0, i));
        if (!StringUtil.isNullOrEmpty(key) && (key.toLowerCase().equals(TO_HEADER) || key.toLowerCase().equals(CC_HEADER))) {
            if (bytes.length > i + 1) {
                byte[] values = Arrays.copyOfRange(bytes, i + 1, bytes.length);
                mHeader = new MimeAddressHeader(key, values);
            }
        }
    } catch (Exception e) {
        ZimbraLog.milter.warn("Error parsing header.", e);
    }
    return mHeader;
}
Also used : MimeAddressHeader(com.zimbra.common.mime.MimeAddressHeader) MalformedInputException(java.nio.charset.MalformedInputException) ServiceException(com.zimbra.common.service.ServiceException) IOException(java.io.IOException)

Example 3 with MimeAddressHeader

use of com.zimbra.common.mime.MimeAddressHeader in project zm-mailbox by Zimbra.

the class MilterHandler method getAddrListsFromHeaders.

private void getAddrListsFromHeaders(MilterPacket command) {
    MimeAddressHeader mHeader = getToCcAddressHeader(command.getData());
    if (mHeader != null) {
        for (InternetAddress address : mHeader.getAddresses()) {
            if (address.getAddress() != null) {
                visibleAddresses.add(address.getAddress().toLowerCase());
                ZimbraLog.milter.debug("Visible value %s", address.getAddress());
            }
        }
    }
}
Also used : InternetAddress(com.zimbra.common.mime.InternetAddress) MimeAddressHeader(com.zimbra.common.mime.MimeAddressHeader)

Example 4 with MimeAddressHeader

use of com.zimbra.common.mime.MimeAddressHeader in project zm-mailbox by Zimbra.

the class MilterHandlerTest method testGetToCcAddressHeaderNonAscii.

@Test
public void testGetToCcAddressHeaderNonAscii() throws IOException {
    String to = "To" + '\0' + "Ré你好 < toadmin@example.com>";
    String cc = "CC" + '\0' + "𝔘𝔫𝔦𝔠𝔬𝔡你好 <ccadmin@example.com>";
    String from = "from" + '\0' + "é̦世界 <admin@example.com>";
    MimeAddressHeader mh = MilterHandler.getToCcAddressHeader(to.getBytes("utf-8"));
    Assert.assertNotNull(mh);
    Assert.assertEquals("toadmin@example.com", mh.getAddresses().get(0).getAddress());
    mh = MilterHandler.getToCcAddressHeader(cc.getBytes("utf-8"));
    Assert.assertNotNull(mh);
    Assert.assertEquals(1, mh.getAddresses().size());
    Assert.assertEquals("ccadmin@example.com", mh.getAddresses().get(0).getAddress());
    mh = MilterHandler.getToCcAddressHeader(from.getBytes("utf-8"));
    Assert.assertNull(mh);
}
Also used : MimeAddressHeader(com.zimbra.common.mime.MimeAddressHeader) Test(org.junit.Test)

Example 5 with MimeAddressHeader

use of com.zimbra.common.mime.MimeAddressHeader in project zm-mailbox by Zimbra.

the class MilterHandlerTest method testGetToCcAddressHeaderNonAscii1.

@Test
public void testGetToCcAddressHeaderNonAscii1() throws IOException {
    List<String> expctedEmails = new ArrayList<String>();
    expctedEmails.add("toadmin@example.com");
    expctedEmails.add("test@example.com");
    expctedEmails.add("test2@example.com");
    String to = "To" + '\0' + "Ré你好 <toadmin@example.com>,test@example.com,<test2@example.com>";
    MimeAddressHeader mh = MilterHandler.getToCcAddressHeader(to.getBytes("iso-8859-1"));
    Assert.assertEquals(3, mh.getAddresses().size());
    for (InternetAddress addrs : mh.getAddresses()) {
        expctedEmails.contains(addrs.getAddress());
    }
}
Also used : InternetAddress(com.zimbra.common.mime.InternetAddress) MimeAddressHeader(com.zimbra.common.mime.MimeAddressHeader) ArrayList(java.util.ArrayList) Test(org.junit.Test)

Aggregations

MimeAddressHeader (com.zimbra.common.mime.MimeAddressHeader)5 InternetAddress (com.zimbra.common.mime.InternetAddress)3 Test (org.junit.Test)3 ArrayList (java.util.ArrayList)2 ServiceException (com.zimbra.common.service.ServiceException)1 IOException (java.io.IOException)1 MalformedInputException (java.nio.charset.MalformedInputException)1