From 17a0c198426e59abaa4ca77c82e9000a70a63d2b Mon Sep 17 00:00:00 2001 From: Matěj Cepl Date: Sat, 1 Jun 2013 21:55:40 +0200 Subject: Add more fields from LDIF files. * birth day can be in two forms * organizational unit * second lines of addresses --- importAddrBook.js | 142 +++++++++++++++++++++++++++++++++++++++++++++--------- 1 file changed, 118 insertions(+), 24 deletions(-) (limited to 'importAddrBook.js') diff --git a/importAddrBook.js b/importAddrBook.js index 42322ee..2a16f86 100644 --- a/importAddrBook.js +++ b/importAddrBook.js @@ -48,7 +48,7 @@ */ function translateObjectToContact(inRec) { var contact = {}, - pomStr = "", curRec = null; + year, month, day, curRec = null; /** * Find the proper record (or create new one) in the multi-value @@ -65,20 +65,24 @@ function findSubElement(idx, subType) { var cAddr = null; + function createNewContact(subType) { + var cont = {}; + if (subType) { + cont.type = [subType]; + } + return cont; + } + // No contact.adr at all if (!contact.hasOwnProperty(idx)) { - cAddr = { - 'type': [subType] - }; + cAddr = createNewContact(subType); contact[idx] = cAddr; } // Single-element property else if (!Array.isArray(contact[idx])) { if (contact[idx].type.indexOf(subType) === -1) { contact[idx] = [contact[idx]]; - cAddr = { - 'type': [subType] - }; + cAddr = createNewContact(subType); contact[idx].push(cAddr); } else { @@ -91,9 +95,7 @@ return addr.type.indexOf(subType) !== -1; }); if (cAddr.length === 0) { - cAddr = { - 'type': [subType] - }; + cAddr = createNewContact(subType); contact[idx].push(cAddr); } else { @@ -104,11 +106,53 @@ return cAddr; } + // secondLineInAddress("home", "mozillaHomeStreet", "mozillaHomeStreet2") + function secondLineInAddress(type, target, first, second) { + var curRec = findSubElement("adr", type); + if (second in inRec) { + curRec[target] = inRec[first] + "\n" + inRec[second]; + } + else if (first in inRec) { + curRec[target] = inRec[first]; + } + + if (first in inRec) { + delete inRec[first]; + } + if (second in inRec) { + delete inRec[second]; + } + } + for (var key in inRec) { - if (key === "birthyear") { - pomStr = inRec[key]; - contact.bday = new Date(pomStr.slice(0,4), pomStr.slice(4,6), - pomStr.slice(6,8)); + if (["birthyear", "birthmonth", "birthday"].indexOf(key) !== -1) { + // We have alternatively either whole date in birthyear field, + // e.g. 19940221, or we have all three properties set. + year = inRec.birthyear || "1970"; // lowest year in Unix time + month = inRec.birthmonth || null; + day = inRec.birthday || null; + + if (year.length === 8) { + contact.bday = new Date(parseInt(year.slice(0,4), 10), + parseInt(year.slice(4,6), 10) - 1, + parseInt(year.slice(6,8), 10)); + if (inRec.birthday) { + delete inRec.birthday; + } + if (inRec.birthmonth) { + delete inRec.birthmonth; + } + } + else if (month && day) { + contact.bday = new Date(parseInt(year, 10), + parseInt(month, 10) - 1, parseInt(day, 10)); + delete inRec.birthday; + delete inRec.birthmonth; + delete inRec.birthyear; + } + else { + throw new ValueError("Wrong value of birthday!"); + } } else if (key === "c") { curRec = findSubElement("adr", "work"); @@ -136,7 +180,11 @@ curRec.locality = inRec[key]; } else if (key === "mail") { - curRec = findSubElement("email", "work"); + curRec = findSubElement("email", "PREF"); + curRec.value = inRec[key]; + } + else if (key === "mozillaSecondEmail") { + curRec = findSubElement("email"); curRec.value = inRec[key]; } else if (key === "mobile") { @@ -159,9 +207,13 @@ curRec = findSubElement("adr", "home"); curRec.region = inRec[key]; } - else if (key === "mozillaHomeStreet") { - curRec = findSubElement("adr", "home"); - curRec.streetAddress = inRec[key]; + else if (["mozillaHomeStreet", "mozillaHomeStreet2"]. + indexOf(key) !== -1) { + secondLineInAddress("home", "mozillaHomeStreet", "mozillaHomeStreet2"); + } + else if (key === "mozillaWorkUrl") { + curRec = findSubElement("url", "work"); + curRec.value = inRec[key]; } else if (key === "mozillaHomeUrl") { curRec = findSubElement("url", "home"); @@ -170,8 +222,50 @@ else if (key === "mozillaNickname") { contact.nickname = [inRec[key]]; } - else if (key === "o") { - contact.org = [inRec[key]]; + // Per W3C Contacts API + // http://www.w3.org/TR/contacts-manager-api/#widl-ContactProperties-org + // org of type array of DOMString + // A string or set thereof representing the organization(s) + // the contact belongs to. It maps to vCard's ORG attribute + // + // Per RFC 6350: + // ============= + // 6.6.4. ORG + // + // Purpose: To specify the organizational name and units associated + // with the vCard. + // + // Value type: A single structured text value consisting of components + // separated by the SEMICOLON character (U+003B). + // + // Special notes: The property is based on the X.520 Organization Name + // and Organization Unit attributes [CCITT.X520.1988]. The property + // value is a structured type consisting of the organization name, + // followed by zero or more levels of organizational unit names. + // + // Example: A property value consisting of an organizational name, + // organizational unit #1 name, and organizational unit #2 name. + // + // ORG:ABC\, Inc.;North American Division;Marketing + else if (["o", "ou"].indexOf(key) !== -1) { + if ('ou' in inRec) { + if ('o' in inRec) { + contact.org = inRec.o + ";" + inRec.ou; + } + else { + throw new ValueError( + "Organizational unit without an organization!"); + } + delete inRec.ou; + } + else { + contact.org = inRec.o; + } + delete inRec.o; + } + else if (key === "postalCode") { + curRec = findSubElement("adr", "work"); + curRec.postalCode = inRec[key]; } else if (key === "sn") { contact.familyName = [inRec[key]]; @@ -180,9 +274,9 @@ curRec = findSubElement("adr", "work"); curRec.region = inRec[key]; } - else if (key === "street") { - curRec = findSubElement("adr", "work"); - curRec.streetAddress = inRec[key]; + else if (["street", "mozillaWorkStreet2"]. + indexOf(key) !== -1) { + secondLineInAddress("work", "street", "mozillaWorkStreet2"); } else if (key === "telephoneNumber") { curRec = findSubElement("tel", "work"); @@ -203,7 +297,7 @@ if (! contact.hasOwnProperty('name')) { if (contact.hasOwnProperty('familyName') && contact.hasOwnProperty('givenName')) { - contact.name = contact.givenName + " " + contact.familyName; + contact.name = contact.givenName + " " + contact.familyName; } else if (contact.hasOwnProperty("org")) { contact.name = contact.org; -- cgit