From 6db48920947f89f150e5fcd8b1fc96df51e98fd0 Mon Sep 17 00:00:00 2001 From: Matěj Cepl Date: Sat, 20 Jul 2013 23:45:08 +0200 Subject: Separate translateObjectToContact to a separate module. --- .gitmodules | 3 + LDIF2Contact | 1 + importAddrBook.js | 305 ------------------------------------------------------ index.html | 1 + 4 files changed, 5 insertions(+), 305 deletions(-) create mode 160000 LDIF2Contact diff --git a/.gitmodules b/.gitmodules index 0a8a914..717610d 100644 --- a/.gitmodules +++ b/.gitmodules @@ -1,3 +1,6 @@ [submodule "parser"] path = parser url = git://github.com/mcepl/parseLDIF.git +[submodule "LDIF2Contact"] + path = LDIF2Contact + url = luther:/srv/git/LDIF2Contact.git diff --git a/LDIF2Contact b/LDIF2Contact new file mode 160000 index 0000000..1bef669 --- /dev/null +++ b/LDIF2Contact @@ -0,0 +1 @@ +Subproject commit 1bef66956a3ff6ac6f4112f443935b47b77ce0d2 diff --git a/importAddrBook.js b/importAddrBook.js index 518002b..97f4466 100644 --- a/importAddrBook.js +++ b/importAddrBook.js @@ -24,311 +24,6 @@ const emailFields = [ "mail" ]; - function ValueError(message) { - this.name = "ValueError"; - this.message = message || "Unknown Value"; - } - ValueError.prototype = new Error(); - ValueError.prototype.constructor = ValueError; - - /** - * Translate objects created from LDIF files to Contacts - * - * @param Array of LDIF objects - * @return Contact with all fields filled - * @throws ValueError for unknown field in the LDIF object - * - * ContactsAPI: - * @see https://wiki.mozilla.org/WebAPI/ContactsAPI - * @spec http://www.w3.org/TR/contacts-manager-api/ - * - * Thunderbid LDAP Schema: - * @see https://developer.mozilla.org/docs/Thunderbird/LDAP_Support - * @see https://tools.ietf.org/html/rfc4519 - */ - function translateObjectToContact(inRec) { - var contact = {}, - year, month, day, curRec = null; - - /** - * Find the proper record (or create new one) in the multi-value - * attribute - * - * @param idx name of the attribute to which the record belongs - * @param subClass Function constructor of the record if there is - * none - * @param subType String with the type of the record - * @return record of the proper class - * - * Uses global variable contact. - */ - 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 = 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 = createNewContact(subType); - contact[idx].push(cAddr); - } - else { - cAddr = contact[idx]; - } - } - // Array - else { - cAddr = contact[idx].filter(function (addr) { - return addr.type.indexOf(subType) !== -1; - }); - if (cAddr.length === 0) { - cAddr = createNewContact(subType); - contact[idx].push(cAddr); - } - else { - cAddr = cAddr[0]; - } - } - - return cAddr; - } - - /** - * Manages squeezing two-line address into one value field - * - * @param type String ['home', 'work'] - * @param target String ContactsAddress attribute to be set (e.g., - * streetAddress) - * @param first String first line index in inRec - * @param second String second line index in inRec - * @return None - * sets variable inRec local to outer function - * deletes both fields from inRec so as to avoid duplication - */ - 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 (["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"); - curRec.countryName = inRec[key]; - } - else if (key === "cn") { - contact.name = [inRec[key]]; - } - else if (key === "description") { - contact.note = [inRec[key]]; - } - else if (key === "facsimiletelephonenumber") { - curRec = findSubElement("tel", "fax"); - curRec.value = inRec[key]; - } - else if (key === "givenName") { - contact.givenName = [inRec[key]]; - } - else if (key === "homePhone") { - curRec = findSubElement("tel", "home"); - curRec.value = inRec[key]; - } - else if (key === "l") { - curRec = findSubElement("adr", "work"); - curRec.locality = inRec[key]; - } - else if (key === "mail") { - curRec = findSubElement("email", "PREF"); - curRec.value = inRec[key]; - } - else if (key === "mozillaSecondEmail") { - curRec = findSubElement("email"); - curRec.value = inRec[key]; - } - else if (key === "mobile") { - curRec = findSubElement("tel", "mobile"); - curRec.value = inRec[key]; - } - else if (key === "mozillaHomeCountryName") { - curRec = findSubElement("adr", "home"); - curRec.countryName = inRec[key]; - } - else if (key === "mozillaHomeLocalityName") { - curRec = findSubElement("adr", "home"); - curRec.locality = inRec[key]; - } - else if (key === "mozillaHomePostalCode") { - curRec = findSubElement("adr", "home"); - curRec.postalCode = inRec[key]; - } - else if (key === "mozillaHomeState") { - curRec = findSubElement("adr", "home"); - curRec.region = inRec[key]; - } - else if (["mozillaHomeStreet", "mozillaHomeStreet2"]. - indexOf(key) !== -1) { - secondLineInAddress("home", "streetAddress", - "mozillaHomeStreet", "mozillaHomeStreet2"); - } - else if (key === "mozillaWorkUrl") { - curRec = findSubElement("url", "work"); - curRec.value = inRec[key]; - } - else if (key === "mozillaHomeUrl") { - curRec = findSubElement("url", "home"); - curRec.value = inRec[key]; - } - else if (key === "mozillaNickname") { - contact.nickname = [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]]; - } - else if (key === "st") { - curRec = findSubElement("adr", "work"); - curRec.region = inRec[key]; - } - else if (["street", "mozillaWorkStreet2"]. - indexOf(key) !== -1) { - secondLineInAddress("work", "streetAddress", - "street", "mozillaWorkStreet2"); - } - else if (key === "telephoneNumber") { - curRec = findSubElement("tel", "work"); - curRec.value = inRec[key]; - } - else if (key === "title") { - contact.jobTitle = [inRec[key]]; - } - // Unknown attribute - else { - throw new ValueError("Unknown attribute " + key + - " with value:\n" + inRec[key]); - } - } - - // Per RFC 4519 section 3.12 cn and sn attributes are always - // required - if (! contact.hasOwnProperty('name')) { - if (contact.hasOwnProperty('familyName') && - contact.hasOwnProperty('givenName')) { - contact.name = contact.givenName + " " + contact.familyName; - } - else if (contact.hasOwnProperty("org")) { - contact.name = contact.org; - } - else if (contact.hasOwnProperty("jobTitle")) { - contact.name = contact.jobTitle; - } - } - - if (Object.keys(contact).length > 0) { - var outObj = new mozContact(); - outObj.init(contact); - return contact; - } - return null; - } - - /** * Not used presently and not debugged. */ diff --git a/index.html b/index.html index afc5db0..a4d092c 100644 --- a/index.html +++ b/index.html @@ -40,6 +40,7 @@ 0 % + -- cgit