2020-05-20 01:42:35 +03:00
|
|
|
/* This Source Code Form is subject to the terms of the Mozilla Public
|
|
|
|
* License, v. 2.0. If a copy of the MPL was not distributed with this
|
|
|
|
* file, you can obtain one at http://mozilla.org/MPL/2.0/. */
|
|
|
|
|
2024-04-22 12:57:29 +03:00
|
|
|
import ICAL from "resource:///modules/calendar/Ical.sys.mjs";
|
2020-05-20 01:42:35 +03:00
|
|
|
|
2022-06-15 20:10:42 +03:00
|
|
|
const lazy = {};
|
|
|
|
|
2024-02-23 15:50:48 +03:00
|
|
|
ChromeUtils.defineESModuleGetters(lazy, {
|
|
|
|
AddrBookCard: "resource:///modules/AddrBookCard.sys.mjs",
|
2022-03-01 05:36:08 +03:00
|
|
|
});
|
|
|
|
|
2020-05-20 01:42:35 +03:00
|
|
|
/**
|
|
|
|
* Utilities for working with vCard data. This file uses ICAL.js as parser and
|
|
|
|
* formatter to avoid reinventing the wheel.
|
2022-11-23 07:43:25 +03:00
|
|
|
*
|
2020-05-20 01:42:35 +03:00
|
|
|
* @see RFC 6350.
|
|
|
|
*/
|
|
|
|
|
2024-02-23 15:50:48 +03:00
|
|
|
export var VCardUtils = {
|
2020-08-20 10:43:15 +03:00
|
|
|
_decodeQuotedPrintable(value) {
|
2023-10-26 12:32:05 +03:00
|
|
|
const bytes = [];
|
2020-08-20 10:43:15 +03:00
|
|
|
for (let b = 0; b < value.length; b++) {
|
|
|
|
if (value[b] == "=") {
|
|
|
|
bytes.push(parseInt(value.substr(b + 1, 2), 16));
|
|
|
|
b += 2;
|
|
|
|
} else {
|
|
|
|
bytes.push(value.charCodeAt(b));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return new TextDecoder().decode(new Uint8Array(bytes));
|
|
|
|
},
|
2020-06-06 08:34:01 +03:00
|
|
|
_parse(vProps) {
|
2023-10-26 12:32:05 +03:00
|
|
|
const vPropMap = new Map();
|
2020-06-06 08:34:01 +03:00
|
|
|
for (let index = 0; index < vProps.length; index++) {
|
2022-06-08 03:06:07 +03:00
|
|
|
let { name, params, value } = vProps[index];
|
2020-06-08 13:30:26 +03:00
|
|
|
|
2020-06-06 08:34:01 +03:00
|
|
|
// Work out which type in typeMap, if any, this property belongs to.
|
|
|
|
|
|
|
|
// To make the next piece easier, the type param must always be an array
|
|
|
|
// of lower-case strings.
|
|
|
|
let type = params.type || [];
|
|
|
|
if (type) {
|
|
|
|
if (Array.isArray(type)) {
|
|
|
|
type = type.map(t => t.toLowerCase());
|
2020-05-20 01:42:35 +03:00
|
|
|
} else {
|
2020-06-06 08:34:01 +03:00
|
|
|
type = [type.toLowerCase()];
|
2020-06-02 08:05:29 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-06-06 08:34:01 +03:00
|
|
|
// Special cases for address and telephone types.
|
2020-06-02 08:05:29 +03:00
|
|
|
if (name == "adr") {
|
2020-06-06 08:34:01 +03:00
|
|
|
name = type.includes("home") ? "adr.home" : "adr.work";
|
2020-06-02 08:05:29 +03:00
|
|
|
}
|
|
|
|
if (name == "tel") {
|
|
|
|
name = "tel.work";
|
2023-10-26 12:32:05 +03:00
|
|
|
for (const t of type) {
|
2020-06-02 08:05:29 +03:00
|
|
|
if (["home", "work", "cell", "pager", "fax"].includes(t)) {
|
|
|
|
name = `tel.${t}`;
|
|
|
|
break;
|
|
|
|
}
|
2020-05-20 01:42:35 +03:00
|
|
|
}
|
|
|
|
}
|
2021-11-16 18:56:41 +03:00
|
|
|
// Preserve URL if no URL with type work is given take for `url.work` the URL without any type.
|
|
|
|
if (name == "url") {
|
|
|
|
name = type.includes("home") ? "url.home" : name;
|
|
|
|
name = type.includes("work") ? "url.work" : name;
|
|
|
|
}
|
2020-06-06 08:34:01 +03:00
|
|
|
|
2021-11-16 18:56:41 +03:00
|
|
|
// Special treatment for `url`, which is not in the typeMap.
|
|
|
|
if (!(name in typeMap) && name != "url") {
|
2020-06-06 08:34:01 +03:00
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
// The preference param is 1-100, lower numbers indicate higher
|
|
|
|
// preference. If not specified, the value is least preferred.
|
2023-10-26 12:32:05 +03:00
|
|
|
const pref = parseInt(params.pref, 10) || 101;
|
2020-06-06 08:34:01 +03:00
|
|
|
|
|
|
|
if (!vPropMap.has(name)) {
|
|
|
|
vPropMap.set(name, []);
|
|
|
|
}
|
|
|
|
vPropMap.get(name).push({ index, pref, value });
|
|
|
|
}
|
|
|
|
|
2021-11-16 18:56:41 +03:00
|
|
|
// If no URL with type is specified assume its the Work Web Page (WebPage 1).
|
|
|
|
if (vPropMap.has("url") && !vPropMap.has("url.work")) {
|
|
|
|
vPropMap.set("url.work", vPropMap.get("url"));
|
|
|
|
}
|
|
|
|
// AbCard only supports Work Web Page or Home Web Page. Get rid of the URL without type.
|
|
|
|
vPropMap.delete("url");
|
|
|
|
|
2023-10-26 12:32:05 +03:00
|
|
|
for (const props of vPropMap.values()) {
|
2020-06-06 08:34:01 +03:00
|
|
|
// Sort the properties by preference, or by the order they appeared.
|
|
|
|
props.sort((a, b) => {
|
|
|
|
if (a.pref == b.pref) {
|
|
|
|
return a.index - b.index;
|
|
|
|
}
|
|
|
|
return a.pref - b.pref;
|
|
|
|
});
|
|
|
|
}
|
|
|
|
return vPropMap;
|
|
|
|
},
|
2022-03-01 04:43:00 +03:00
|
|
|
/**
|
|
|
|
* ICAL.js's parser only supports vCard 3.0 and 4.0. To maintain
|
|
|
|
* interoperability with other applications, here we convert vCard 2.1
|
|
|
|
* cards into a "good-enough" mimic of vCard 4.0 so that the parser will
|
|
|
|
* read it without throwing an error.
|
|
|
|
*
|
|
|
|
* @param {string} vCard
|
2022-11-23 07:43:25 +03:00
|
|
|
* @returns {string}
|
2022-03-01 04:43:00 +03:00
|
|
|
*/
|
|
|
|
translateVCard21(vCard) {
|
|
|
|
if (!/\bVERSION:2.1\b/i.test(vCard)) {
|
|
|
|
return vCard;
|
|
|
|
}
|
2020-09-28 12:16:49 +03:00
|
|
|
|
2022-03-01 04:43:00 +03:00
|
|
|
// Convert known type parameters to valid vCard 4.0, ignore unknown ones.
|
|
|
|
vCard = vCard.replace(/\n(([A-Z]+)(;[\w-]*)+):/gi, (match, key) => {
|
2023-10-26 12:32:05 +03:00
|
|
|
const parts = key.split(";");
|
|
|
|
const newParts = [parts[0]];
|
2022-03-01 04:43:00 +03:00
|
|
|
for (let i = 1; i < parts.length; i++) {
|
|
|
|
if (parts[i] == "") {
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
if (
|
|
|
|
["HOME", "WORK", "FAX", "PAGER", "CELL"].includes(
|
|
|
|
parts[i].toUpperCase()
|
|
|
|
)
|
|
|
|
) {
|
|
|
|
newParts.push(`TYPE=${parts[i]}`);
|
|
|
|
} else if (parts[i].toUpperCase() == "PREF") {
|
|
|
|
newParts.push("PREF=1");
|
|
|
|
} else if (parts[i].toUpperCase() == "QUOTED-PRINTABLE") {
|
|
|
|
newParts.push("ENCODING=QUOTED-PRINTABLE");
|
|
|
|
}
|
2020-09-28 12:16:49 +03:00
|
|
|
}
|
2022-03-01 04:43:00 +03:00
|
|
|
return "\n" + newParts.join(";") + ":";
|
|
|
|
});
|
|
|
|
|
|
|
|
// Join quoted-printable wrapped lines together. This regular expression
|
|
|
|
// only matches lines that are quoted-printable and end with `=`.
|
2023-10-26 12:32:05 +03:00
|
|
|
const quotedNewLineRegExp =
|
|
|
|
/(;ENCODING=QUOTED-PRINTABLE[;:][^\r\n]*)=\r?\n/i;
|
2022-03-01 04:43:00 +03:00
|
|
|
while (vCard.match(quotedNewLineRegExp)) {
|
|
|
|
vCard = vCard.replace(quotedNewLineRegExp, "$1");
|
2020-06-08 13:30:26 +03:00
|
|
|
}
|
|
|
|
|
2022-03-01 04:43:00 +03:00
|
|
|
// Strip the version.
|
|
|
|
return vCard.replace(/(\r?\n)VERSION:2.1\r?\n/i, "$1");
|
|
|
|
},
|
2022-06-14 17:44:53 +03:00
|
|
|
/**
|
|
|
|
* Return a new AddrBookCard from the provided vCard string.
|
|
|
|
*
|
2023-01-08 12:25:54 +03:00
|
|
|
* @param {string} vCard - The vCard string.
|
|
|
|
* @param {string} [uid] - An optional UID to be used for the new card,
|
|
|
|
* overriding any UID specified in the vCard string.
|
|
|
|
* @returns {AddrBookCard}
|
2022-06-14 17:44:53 +03:00
|
|
|
*/
|
|
|
|
vCardToAbCard(vCard, uid) {
|
2022-03-01 04:43:00 +03:00
|
|
|
vCard = this.translateVCard21(vCard);
|
|
|
|
|
2023-10-26 12:32:05 +03:00
|
|
|
const abCard = new lazy.AddrBookCard();
|
2022-03-01 05:36:08 +03:00
|
|
|
abCard.setProperty("_vCard", vCard);
|
2022-06-14 17:44:53 +03:00
|
|
|
|
2023-10-26 12:32:05 +03:00
|
|
|
const vCardUID = abCard.vCardProperties.getFirstValue("uid");
|
2022-06-14 17:44:53 +03:00
|
|
|
if (uid || vCardUID) {
|
|
|
|
abCard.UID = uid || vCardUID;
|
|
|
|
if (abCard.UID != vCardUID) {
|
|
|
|
abCard.vCardProperties.clearValues("uid");
|
|
|
|
abCard.vCardProperties.addValue("uid", abCard.UID);
|
|
|
|
}
|
2020-06-06 08:34:01 +03:00
|
|
|
}
|
|
|
|
|
2020-05-20 01:42:35 +03:00
|
|
|
return abCard;
|
|
|
|
},
|
2022-03-01 05:36:08 +03:00
|
|
|
abCardToVCard(abCard, version) {
|
|
|
|
if (abCard.supportsVCard && abCard.getProperty("_vCard")) {
|
|
|
|
return abCard.vCardProperties.toVCard();
|
2020-05-20 01:42:35 +03:00
|
|
|
}
|
|
|
|
|
2022-03-01 05:36:08 +03:00
|
|
|
// Collect all of the AB card properties into a Map.
|
2023-10-26 12:32:05 +03:00
|
|
|
const abProps = new Map(
|
2022-03-01 05:36:08 +03:00
|
|
|
Array.from(abCard.properties, p => [p.name, p.value])
|
|
|
|
);
|
|
|
|
abProps.set("UID", abCard.UID);
|
2020-05-20 01:42:35 +03:00
|
|
|
|
2022-03-01 05:36:08 +03:00
|
|
|
return this.propertyMapToVCard(abProps, version);
|
2020-05-20 01:42:35 +03:00
|
|
|
},
|
2022-03-01 05:36:08 +03:00
|
|
|
propertyMapToVCard(abProps, version = "4.0") {
|
2023-10-26 12:32:05 +03:00
|
|
|
const vProps = [["version", {}, "text", version]];
|
2020-05-20 01:42:35 +03:00
|
|
|
|
|
|
|
// Add the properties to the vCard.
|
2023-10-26 12:32:05 +03:00
|
|
|
for (const vPropName of Object.keys(typeMap)) {
|
|
|
|
for (const vProp of typeMap[vPropName].fromAbCard(abProps, vPropName)) {
|
2022-03-01 05:36:08 +03:00
|
|
|
if (vProp[3] !== null && vProp[3] !== undefined && vProp[3] !== "") {
|
2020-06-06 08:34:01 +03:00
|
|
|
vProps.push(vProp);
|
|
|
|
}
|
2020-05-20 01:42:35 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// If there's only one address or telephone number, don't specify type.
|
2023-10-26 12:32:05 +03:00
|
|
|
const adrProps = vProps.filter(p => p[0] == "adr");
|
2020-05-20 01:42:35 +03:00
|
|
|
if (adrProps.length == 1) {
|
|
|
|
delete adrProps[0][1].type;
|
|
|
|
}
|
2023-10-26 12:32:05 +03:00
|
|
|
const telProps = vProps.filter(p => p[0] == "tel");
|
2020-05-20 01:42:35 +03:00
|
|
|
if (telProps.length == 1) {
|
|
|
|
delete telProps[0][1].type;
|
|
|
|
}
|
|
|
|
|
2022-03-01 05:36:08 +03:00
|
|
|
if (abProps.has("UID")) {
|
|
|
|
vProps.push(["uid", {}, "text", abProps.get("UID")]);
|
|
|
|
}
|
2020-05-20 01:42:35 +03:00
|
|
|
return ICAL.stringify(["vcard", vProps]);
|
|
|
|
},
|
|
|
|
};
|
|
|
|
|
2024-02-23 15:50:48 +03:00
|
|
|
export function VCardService() {}
|
2020-05-15 05:51:12 +03:00
|
|
|
VCardService.prototype = {
|
2020-07-13 02:45:03 +03:00
|
|
|
QueryInterface: ChromeUtils.generateQI(["nsIMsgVCardService"]),
|
2020-05-15 05:51:12 +03:00
|
|
|
classID: Components.ID("{e2e0f615-bc5a-4441-a16b-a26e75949376}"),
|
|
|
|
|
2024-06-05 07:12:36 +03:00
|
|
|
/**
|
|
|
|
* Translates a vCard string into a nsIAbCard.
|
|
|
|
*
|
|
|
|
* @param {string} vCard
|
|
|
|
* @returns {nsIAbCard} - A card containing the vCard data, or null if no
|
|
|
|
* data was given.
|
|
|
|
*/
|
2021-02-23 06:30:35 +03:00
|
|
|
vCardToAbCard(vCard) {
|
|
|
|
return vCard ? VCardUtils.vCardToAbCard(vCard) : null;
|
|
|
|
},
|
2024-06-05 07:12:36 +03:00
|
|
|
/**
|
|
|
|
* Translates a nsIAbCard into a vCard.
|
|
|
|
*
|
|
|
|
* @param {nsIAbCard} abCard
|
|
|
|
* @returns {string} - A vCard representation of `abCard`, or null if
|
|
|
|
* `abCard` is falsy.
|
|
|
|
*/
|
|
|
|
abCardToVCard(abCard) {
|
|
|
|
return abCard ? VCardUtils.abCardToVCard(abCard) : null;
|
2020-05-15 05:51:12 +03:00
|
|
|
},
|
|
|
|
};
|
|
|
|
|
2024-02-23 15:50:48 +03:00
|
|
|
export function VCardMimeConverter() {}
|
2020-05-15 05:51:12 +03:00
|
|
|
VCardMimeConverter.prototype = {
|
2020-07-13 02:45:03 +03:00
|
|
|
QueryInterface: ChromeUtils.generateQI(["nsISimpleMimeConverter"]),
|
2020-05-15 05:51:12 +03:00
|
|
|
classID: Components.ID("{dafab386-bd4c-4238-bb48-228fbc98ba29}"),
|
|
|
|
|
2022-10-17 05:47:49 +03:00
|
|
|
mailChannel: null,
|
2020-05-15 05:51:12 +03:00
|
|
|
uri: null,
|
|
|
|
convertToHTML(contentType, data) {
|
|
|
|
function escapeHTML(template, ...parts) {
|
2023-10-26 12:32:05 +03:00
|
|
|
const arr = [];
|
2020-05-15 05:51:12 +03:00
|
|
|
for (let i = 0; i < parts.length; i++) {
|
|
|
|
arr.push(template[i]);
|
|
|
|
arr.push(
|
|
|
|
parts[i]
|
|
|
|
.replace(/&/g, "&")
|
|
|
|
.replace(/"/g, """)
|
|
|
|
.replace(/</g, "<")
|
|
|
|
.replace(/>/g, ">")
|
|
|
|
);
|
|
|
|
}
|
|
|
|
arr.push(template[template.length - 1]);
|
|
|
|
return arr.join("");
|
|
|
|
}
|
|
|
|
|
2020-12-16 13:35:02 +03:00
|
|
|
let abCard;
|
|
|
|
try {
|
|
|
|
abCard = VCardUtils.vCardToAbCard(data);
|
|
|
|
} catch (e) {
|
|
|
|
// We were given invalid vcard data.
|
|
|
|
return "";
|
|
|
|
}
|
|
|
|
|
2023-10-26 12:32:05 +03:00
|
|
|
const escapedVCard = encodeURIComponent(data);
|
2020-05-15 05:51:12 +03:00
|
|
|
|
|
|
|
let propertiesTable = `<table class="moz-vcard-properties-table">`;
|
|
|
|
propertiesTable += escapeHTML`<tr><td class="moz-vcard-title-property">${abCard.displayName}`;
|
|
|
|
if (abCard.primaryEmail) {
|
|
|
|
propertiesTable += escapeHTML` <<a href="mailto:${abCard.primaryEmail}" private>${abCard.primaryEmail}</a>>`;
|
|
|
|
}
|
|
|
|
propertiesTable += `</td></tr>`;
|
2023-10-26 12:32:05 +03:00
|
|
|
for (const propName of ["JobTitle", "Department", "Company"]) {
|
|
|
|
const propValue = abCard.getProperty(propName, "");
|
2020-05-15 05:51:12 +03:00
|
|
|
if (propValue) {
|
|
|
|
propertiesTable += escapeHTML`<tr><td class="moz-vcard-property">${propValue}</td></tr>`;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
propertiesTable += `</table>`;
|
|
|
|
|
2024-03-21 20:44:49 +03:00
|
|
|
// VCardChild.sys.mjs and VCardParent.sys.mjs handle clicking on this link.
|
2020-05-15 05:51:12 +03:00
|
|
|
return `<html>
|
|
|
|
<body>
|
|
|
|
<table class="moz-vcard-table">
|
|
|
|
<tr>
|
2021-08-11 14:34:17 +03:00
|
|
|
<td valign="top"><a class="moz-vcard-badge" href="data:text/vcard,${escapedVCard}"></a></td>
|
2020-05-15 05:51:12 +03:00
|
|
|
<td>
|
|
|
|
${propertiesTable}
|
|
|
|
</td>
|
|
|
|
</tr>
|
|
|
|
</table>
|
|
|
|
</body>
|
|
|
|
</html>`;
|
|
|
|
},
|
|
|
|
};
|
2020-05-20 01:42:35 +03:00
|
|
|
|
2024-02-23 15:50:48 +03:00
|
|
|
export const BANISHED_PROPERTIES = [
|
2022-03-01 05:36:08 +03:00
|
|
|
"UID",
|
|
|
|
"PrimaryEmail",
|
|
|
|
"SecondEmail",
|
|
|
|
"DisplayName",
|
|
|
|
"NickName",
|
|
|
|
"Notes",
|
|
|
|
"Company",
|
|
|
|
"Department",
|
|
|
|
"JobTitle",
|
|
|
|
"BirthDay",
|
|
|
|
"BirthMonth",
|
|
|
|
"BirthYear",
|
|
|
|
"AnniversaryDay",
|
|
|
|
"AnniversaryMonth",
|
|
|
|
"AnniversaryYear",
|
|
|
|
"LastName",
|
|
|
|
"FirstName",
|
|
|
|
"AdditionalNames",
|
|
|
|
"NamePrefix",
|
|
|
|
"NameSuffix",
|
|
|
|
"HomePOBox",
|
|
|
|
"HomeAddress2",
|
|
|
|
"HomeAddress",
|
|
|
|
"HomeCity",
|
|
|
|
"HomeState",
|
|
|
|
"HomeZipCode",
|
|
|
|
"HomeCountry",
|
|
|
|
"WorkPOBox",
|
|
|
|
"WorkAddress2",
|
|
|
|
"WorkAddress",
|
|
|
|
"WorkCity",
|
|
|
|
"WorkState",
|
|
|
|
"WorkZipCode",
|
|
|
|
"WorkCountry",
|
|
|
|
"HomePhone",
|
|
|
|
"WorkPhone",
|
|
|
|
"FaxNumber",
|
|
|
|
"PagerNumber",
|
|
|
|
"CellularNumber",
|
|
|
|
"WebPage1",
|
|
|
|
"WebPage2",
|
2022-10-10 11:51:51 +03:00
|
|
|
"Custom1",
|
|
|
|
"Custom2",
|
|
|
|
"Custom3",
|
|
|
|
"Custom4",
|
2022-03-01 05:36:08 +03:00
|
|
|
];
|
|
|
|
|
2020-06-06 08:34:01 +03:00
|
|
|
/** Helper functions for typeMap. */
|
2020-05-20 01:42:35 +03:00
|
|
|
|
|
|
|
function singleTextProperty(
|
|
|
|
abPropName,
|
|
|
|
vPropName,
|
|
|
|
vPropParams = {},
|
|
|
|
vPropType = "text"
|
|
|
|
) {
|
|
|
|
return {
|
|
|
|
/**
|
|
|
|
* Formats nsIAbCard properties into an array for use by ICAL.js.
|
|
|
|
*
|
|
|
|
* @param {Map} map - A map of address book properties to map.
|
2020-06-06 08:34:01 +03:00
|
|
|
* @yields {Array} - Values in a jCard array for use with ICAL.js.
|
2020-05-20 01:42:35 +03:00
|
|
|
*/
|
2020-06-06 08:34:01 +03:00
|
|
|
*fromAbCard(map) {
|
|
|
|
yield [vPropName, { ...vPropParams }, vPropType, map.get(abPropName)];
|
2020-05-20 01:42:35 +03:00
|
|
|
},
|
|
|
|
/**
|
|
|
|
* Parses a vCard value into properties usable by nsIAbCard.
|
|
|
|
*
|
2022-11-23 07:43:25 +03:00
|
|
|
* @param {string} value - vCard string to map to an address book card property.
|
|
|
|
* @yields {string[]} - Any number of key, value pairs to set on the nsIAbCard.
|
2020-05-20 01:42:35 +03:00
|
|
|
*/
|
2020-06-06 08:34:01 +03:00
|
|
|
*toAbCard(value) {
|
2020-05-20 01:42:35 +03:00
|
|
|
if (typeof value != "string") {
|
|
|
|
console.warn(`Unexpected value for ${vPropName}: ${value}`);
|
2020-06-06 08:34:01 +03:00
|
|
|
return;
|
2020-05-20 01:42:35 +03:00
|
|
|
}
|
2020-06-06 08:34:01 +03:00
|
|
|
yield [abPropName, value];
|
2020-05-20 01:42:35 +03:00
|
|
|
},
|
|
|
|
};
|
|
|
|
}
|
|
|
|
function dateProperty(abCardPrefix, vPropName) {
|
|
|
|
return {
|
2020-06-06 08:34:01 +03:00
|
|
|
*fromAbCard(map) {
|
2023-10-26 12:32:05 +03:00
|
|
|
const year = map.get(`${abCardPrefix}Year`);
|
|
|
|
const month = map.get(`${abCardPrefix}Month`);
|
|
|
|
const day = map.get(`${abCardPrefix}Day`);
|
2022-03-01 05:36:08 +03:00
|
|
|
|
|
|
|
if (!year && !month && !day) {
|
2020-06-06 08:34:01 +03:00
|
|
|
return;
|
2020-05-20 01:42:35 +03:00
|
|
|
}
|
2022-03-01 05:36:08 +03:00
|
|
|
|
2023-10-26 12:32:05 +03:00
|
|
|
const dateValue = new ICAL.VCardTime({}, null, "date");
|
2022-03-01 05:36:08 +03:00
|
|
|
// Set the properties directly instead of using the VCardTime
|
|
|
|
// constructor argument, which causes null values to become 0.
|
|
|
|
dateValue.year = year ? Number(year) : null;
|
|
|
|
dateValue.month = month ? Number(month) : null;
|
|
|
|
dateValue.day = day ? Number(day) : null;
|
|
|
|
|
2020-06-06 08:34:01 +03:00
|
|
|
yield [vPropName, {}, "date", dateValue.toString()];
|
2020-05-20 01:42:35 +03:00
|
|
|
},
|
2020-06-06 08:34:01 +03:00
|
|
|
*toAbCard(value) {
|
2022-10-04 02:02:13 +03:00
|
|
|
try {
|
2023-10-26 12:32:05 +03:00
|
|
|
const dateValue = ICAL.VCardTime.fromDateAndOrTimeString(value);
|
2022-10-04 02:02:13 +03:00
|
|
|
yield [`${abCardPrefix}Year`, String(dateValue.year ?? "")];
|
|
|
|
yield [`${abCardPrefix}Month`, String(dateValue.month ?? "")];
|
|
|
|
yield [`${abCardPrefix}Day`, String(dateValue.day ?? "")];
|
|
|
|
} catch (ex) {
|
2022-11-30 13:43:13 +03:00
|
|
|
console.error(ex);
|
2022-10-04 02:02:13 +03:00
|
|
|
}
|
2020-05-20 01:42:35 +03:00
|
|
|
},
|
|
|
|
};
|
|
|
|
}
|
|
|
|
function multiTextProperty(abPropNames, vPropName, vPropParams = {}) {
|
|
|
|
return {
|
2020-06-06 08:34:01 +03:00
|
|
|
*fromAbCard(map) {
|
2020-05-20 01:42:35 +03:00
|
|
|
if (abPropNames.every(name => !map.has(name))) {
|
2020-06-06 08:34:01 +03:00
|
|
|
return;
|
2020-05-20 01:42:35 +03:00
|
|
|
}
|
2023-10-26 12:32:05 +03:00
|
|
|
const vPropValues = abPropNames.map(name => map.get(name) || "");
|
2022-03-01 05:36:08 +03:00
|
|
|
if (vPropValues.some(Boolean)) {
|
|
|
|
yield [vPropName, { ...vPropParams }, "text", vPropValues];
|
|
|
|
}
|
2020-05-20 01:42:35 +03:00
|
|
|
},
|
2020-06-06 08:34:01 +03:00
|
|
|
*toAbCard(value) {
|
2020-05-24 12:03:54 +03:00
|
|
|
if (Array.isArray(value)) {
|
2023-10-26 12:32:05 +03:00
|
|
|
for (const abPropName of abPropNames) {
|
|
|
|
const valuePart = value.shift();
|
2020-05-24 12:03:54 +03:00
|
|
|
if (abPropName && valuePart) {
|
2021-07-23 06:12:45 +03:00
|
|
|
yield [
|
|
|
|
abPropName,
|
|
|
|
Array.isArray(valuePart) ? valuePart.join(" ") : valuePart,
|
|
|
|
];
|
2020-05-24 12:03:54 +03:00
|
|
|
}
|
2020-05-20 01:42:35 +03:00
|
|
|
}
|
2020-05-24 12:03:54 +03:00
|
|
|
} else if (typeof value == "string") {
|
|
|
|
// Only one value was given.
|
2020-06-06 08:34:01 +03:00
|
|
|
yield [abPropNames[0], value];
|
2020-05-24 12:03:54 +03:00
|
|
|
} else {
|
|
|
|
console.warn(`Unexpected value for ${vPropName}: ${value}`);
|
2020-05-20 01:42:35 +03:00
|
|
|
}
|
|
|
|
},
|
|
|
|
};
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Properties we support for conversion between nsIAbCard and vCard.
|
|
|
|
*
|
|
|
|
* Keys correspond to vCard property keys, with the type appended where more
|
|
|
|
* than one type is supported (e.g. work and home).
|
|
|
|
*
|
|
|
|
* Values are objects with toAbCard and fromAbCard functions which convert
|
|
|
|
* property values in each direction. See the docs on the object returned by
|
|
|
|
* singleTextProperty.
|
|
|
|
*/
|
2020-06-06 08:34:01 +03:00
|
|
|
var typeMap = {
|
2022-03-01 05:36:08 +03:00
|
|
|
fn: singleTextProperty("DisplayName", "fn"),
|
2020-06-06 08:34:01 +03:00
|
|
|
email: {
|
|
|
|
*fromAbCard(map) {
|
|
|
|
yield ["email", { pref: "1" }, "text", map.get("PrimaryEmail")];
|
|
|
|
yield ["email", {}, "text", map.get("SecondEmail")];
|
|
|
|
},
|
|
|
|
toAbCard: singleTextProperty("PrimaryEmail", "email", { pref: "1" })
|
|
|
|
.toAbCard,
|
|
|
|
},
|
2020-05-20 01:42:35 +03:00
|
|
|
nickname: singleTextProperty("NickName", "nickname"),
|
|
|
|
note: singleTextProperty("Notes", "note"),
|
|
|
|
org: multiTextProperty(["Company", "Department"], "org"),
|
|
|
|
title: singleTextProperty("JobTitle", "title"),
|
|
|
|
bday: dateProperty("Birth", "bday"),
|
|
|
|
anniversary: dateProperty("Anniversary", "anniversary"),
|
2020-06-30 07:22:07 +03:00
|
|
|
n: multiTextProperty(
|
|
|
|
["LastName", "FirstName", "AdditionalNames", "NamePrefix", "NameSuffix"],
|
|
|
|
"n"
|
|
|
|
),
|
2020-05-20 01:42:35 +03:00
|
|
|
"adr.home": multiTextProperty(
|
|
|
|
[
|
2020-06-30 07:22:07 +03:00
|
|
|
"HomePOBox",
|
2020-05-20 01:42:35 +03:00
|
|
|
"HomeAddress2",
|
|
|
|
"HomeAddress",
|
|
|
|
"HomeCity",
|
|
|
|
"HomeState",
|
|
|
|
"HomeZipCode",
|
|
|
|
"HomeCountry",
|
|
|
|
],
|
|
|
|
"adr",
|
|
|
|
{ type: "home" }
|
|
|
|
),
|
|
|
|
"adr.work": multiTextProperty(
|
|
|
|
[
|
2020-06-30 07:22:07 +03:00
|
|
|
"WorkPOBox",
|
2020-05-20 01:42:35 +03:00
|
|
|
"WorkAddress2",
|
|
|
|
"WorkAddress",
|
|
|
|
"WorkCity",
|
|
|
|
"WorkState",
|
|
|
|
"WorkZipCode",
|
|
|
|
"WorkCountry",
|
|
|
|
],
|
|
|
|
"adr",
|
|
|
|
{ type: "work" }
|
|
|
|
),
|
|
|
|
"tel.home": singleTextProperty("HomePhone", "tel", { type: "home" }),
|
|
|
|
"tel.work": singleTextProperty("WorkPhone", "tel", { type: "work" }),
|
|
|
|
"tel.fax": singleTextProperty("FaxNumber", "tel", { type: "fax" }),
|
|
|
|
"tel.pager": singleTextProperty("PagerNumber", "tel", { type: "pager" }),
|
|
|
|
"tel.cell": singleTextProperty("CellularNumber", "tel", { type: "cell" }),
|
2021-11-16 18:56:41 +03:00
|
|
|
"url.work": singleTextProperty("WebPage1", "url", { type: "work" }, "url"),
|
|
|
|
"url.home": singleTextProperty("WebPage2", "url", { type: "home" }, "url"),
|
2022-08-03 06:47:58 +03:00
|
|
|
"x-custom1": singleTextProperty("Custom1", "x-custom1"),
|
|
|
|
"x-custom2": singleTextProperty("Custom2", "x-custom2"),
|
|
|
|
"x-custom3": singleTextProperty("Custom3", "x-custom3"),
|
|
|
|
"x-custom4": singleTextProperty("Custom4", "x-custom4"),
|
2020-05-20 01:42:35 +03:00
|
|
|
};
|
2022-03-01 04:43:00 +03:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Any value that can be represented in a vCard. A value can be a boolean,
|
|
|
|
* number, string, or an array, depending on the data. A top-level array might
|
|
|
|
* contain primitives and/or second-level arrays of primitives.
|
|
|
|
*
|
|
|
|
* @see ICAL.design
|
|
|
|
* @see RFC6350
|
|
|
|
*
|
2023-01-08 12:25:54 +03:00
|
|
|
* @typedef {boolean|number|string|vCardValue[]} vCardValue
|
2022-03-01 04:43:00 +03:00
|
|
|
*/
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Represents a single entry in a vCard ("contentline" in RFC6350 terms).
|
|
|
|
* The name, params, type and value are as returned by ICAL.
|
|
|
|
*/
|
2024-02-23 15:50:48 +03:00
|
|
|
export class VCardPropertyEntry {
|
2022-03-01 04:43:00 +03:00
|
|
|
#name = null;
|
|
|
|
#params = null;
|
|
|
|
#type = null;
|
|
|
|
#value = null;
|
|
|
|
_original = null;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @param {string} name
|
|
|
|
* @param {object} params
|
|
|
|
* @param {string} type
|
|
|
|
* @param {vCardValue} value
|
|
|
|
*/
|
|
|
|
constructor(name, params, type, value) {
|
|
|
|
this.#name = name;
|
|
|
|
this.#params = params;
|
|
|
|
this.#type = type;
|
2022-03-01 05:36:08 +03:00
|
|
|
if (params.encoding?.toUpperCase() == "QUOTED-PRINTABLE") {
|
|
|
|
if (Array.isArray(value)) {
|
|
|
|
value = value.map(VCardUtils._decodeQuotedPrintable);
|
|
|
|
} else {
|
|
|
|
value = VCardUtils._decodeQuotedPrintable(value);
|
|
|
|
}
|
|
|
|
delete params.encoding;
|
|
|
|
delete params.charset;
|
|
|
|
}
|
2022-03-01 04:43:00 +03:00
|
|
|
this.#value = value;
|
|
|
|
this._original = this;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @type {string}
|
|
|
|
*/
|
|
|
|
get name() {
|
|
|
|
return this.#name;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @type {object}
|
|
|
|
*/
|
|
|
|
get params() {
|
|
|
|
return this.#params;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @type {string}
|
|
|
|
*/
|
|
|
|
get type() {
|
|
|
|
return this.#type;
|
|
|
|
}
|
|
|
|
set type(type) {
|
|
|
|
this.#type = type;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @type {vCardValue}
|
|
|
|
*/
|
|
|
|
get value() {
|
|
|
|
return this.#value;
|
|
|
|
}
|
|
|
|
set value(value) {
|
|
|
|
this.#value = value;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Clone this object.
|
|
|
|
*
|
2022-11-23 07:43:25 +03:00
|
|
|
* @returns {VCardPropertyEntry}
|
2022-03-01 04:43:00 +03:00
|
|
|
*/
|
|
|
|
clone() {
|
|
|
|
let cloneValue;
|
|
|
|
if (Array.isArray(this.#value)) {
|
|
|
|
cloneValue = this.#value.map(v => (Array.isArray(v) ? v.slice() : v));
|
|
|
|
} else {
|
|
|
|
cloneValue = this.#value;
|
|
|
|
}
|
|
|
|
|
2023-10-26 12:32:05 +03:00
|
|
|
const clone = new VCardPropertyEntry(
|
2022-03-01 04:43:00 +03:00
|
|
|
this.#name,
|
|
|
|
{ ...this.#params },
|
|
|
|
this.#type,
|
|
|
|
cloneValue
|
|
|
|
);
|
|
|
|
clone._original = this;
|
|
|
|
return clone;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @param {VCardPropertyEntry} other
|
|
|
|
*/
|
|
|
|
equals(other) {
|
|
|
|
if (other.constructor.name != "VCardPropertyEntry") {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
return this._original == other._original;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Represents an entire vCard as a collection of `VCardPropertyEntry` objects.
|
|
|
|
*/
|
2024-02-23 15:50:48 +03:00
|
|
|
export class VCardProperties {
|
2022-03-01 04:43:00 +03:00
|
|
|
/**
|
|
|
|
* All of the vCard entries in this object.
|
|
|
|
*
|
|
|
|
* @type {VCardPropertyEntry[]}
|
|
|
|
*/
|
|
|
|
entries = [];
|
|
|
|
|
2022-05-31 12:07:13 +03:00
|
|
|
/**
|
|
|
|
* @param {?string} version - The version of vCard to use. Valid values are
|
|
|
|
* "3.0" and "4.0". If unspecified, vCard 3.0 will be used.
|
|
|
|
*/
|
|
|
|
constructor(version) {
|
|
|
|
if (version) {
|
|
|
|
if (!["3.0", "4.0"].includes(version)) {
|
|
|
|
throw new Error(`Unsupported vCard version: ${version}`);
|
|
|
|
}
|
|
|
|
this.addEntry(new VCardPropertyEntry("version", {}, "text", version));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-03-01 04:43:00 +03:00
|
|
|
/**
|
|
|
|
* Parse a vCard into a VCardProperties object.
|
|
|
|
*
|
|
|
|
* @param {string} vCard
|
2022-11-23 07:43:25 +03:00
|
|
|
* @returns {VCardProperties}
|
2022-03-01 04:43:00 +03:00
|
|
|
*/
|
2022-07-13 12:25:29 +03:00
|
|
|
static fromVCard(vCard, { isGoogleCardDAV = false } = {}) {
|
2022-03-01 04:43:00 +03:00
|
|
|
vCard = VCardUtils.translateVCard21(vCard);
|
|
|
|
|
2023-10-26 12:32:05 +03:00
|
|
|
const rv = new VCardProperties();
|
|
|
|
const [, properties] = ICAL.parse(vCard);
|
|
|
|
for (const property of properties) {
|
2022-09-27 03:00:46 +03:00
|
|
|
let [name, params, type, value] = property;
|
|
|
|
if (property.length > 4) {
|
|
|
|
// The jCal format stores multiple values as the 4th...nth items.
|
|
|
|
// VCardPropertyEntry has only one place for a value, so store an
|
|
|
|
// array instead. This applies to CATEGORIES and NICKNAME types in
|
|
|
|
// vCard 4 and also NOTE in vCard 3.
|
|
|
|
value = property.slice(3);
|
|
|
|
}
|
2022-07-13 12:25:29 +03:00
|
|
|
if (isGoogleCardDAV) {
|
|
|
|
// Google escapes the characters \r : , ; and \ unnecessarily, in
|
|
|
|
// violation of RFC6350. Removing the escaping at this point means no
|
|
|
|
// other code requires a special case for it.
|
|
|
|
if (Array.isArray(value)) {
|
|
|
|
value = value.map(v => v.replace(/\\r/g, "\r").replace(/\\:/g, ":"));
|
|
|
|
} else {
|
|
|
|
value = value.replace(/\\r/g, "\r").replace(/\\:/g, ":");
|
|
|
|
if (["phone-number", "uri"].includes(type)) {
|
|
|
|
value = value.replace(/\\([,;\\])/g, "$1");
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2022-03-01 04:43:00 +03:00
|
|
|
rv.addEntry(new VCardPropertyEntry(name, params, type, value));
|
|
|
|
}
|
|
|
|
return rv;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Parse a Map of Address Book properties into a VCardProperties object.
|
|
|
|
*
|
|
|
|
* @param {Map<string, string>} propertyMap
|
2023-01-08 12:25:54 +03:00
|
|
|
* @param {string} [version="4.0"]
|
2022-11-23 07:43:25 +03:00
|
|
|
* @returns {VCardProperties}
|
2022-03-01 04:43:00 +03:00
|
|
|
*/
|
|
|
|
static fromPropertyMap(propertyMap, version = "4.0") {
|
2023-10-26 12:32:05 +03:00
|
|
|
const rv = new VCardProperties(version);
|
2022-03-01 04:43:00 +03:00
|
|
|
|
2023-10-26 12:32:05 +03:00
|
|
|
for (const vPropName of Object.keys(typeMap)) {
|
|
|
|
for (const vProp of typeMap[vPropName].fromAbCard(
|
|
|
|
propertyMap,
|
|
|
|
vPropName
|
|
|
|
)) {
|
2022-03-01 04:43:00 +03:00
|
|
|
if (vProp[3] !== null && vProp[3] !== undefined && vProp[3] !== "") {
|
|
|
|
rv.addEntry(new VCardPropertyEntry(...vProp));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return rv;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Used to determine the default value type when adding values.
|
|
|
|
* Either `ICAL.design.vcard` for (vCard 4.0) or `ICAL.design.vcard3` (3.0).
|
|
|
|
*
|
|
|
|
* @type {ICAL.design.designSet}
|
|
|
|
*/
|
2022-05-31 12:07:13 +03:00
|
|
|
designSet = ICAL.design.vcard3;
|
2022-03-01 04:43:00 +03:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Add an entry to this object.
|
|
|
|
*
|
2023-01-08 12:25:54 +03:00
|
|
|
* @param {VCardPropertyEntry} entry - The entry to add.
|
2022-11-23 07:43:25 +03:00
|
|
|
* @returns {boolean} - If the entry was added.
|
2022-03-01 04:43:00 +03:00
|
|
|
*/
|
|
|
|
addEntry(entry) {
|
|
|
|
if (entry.constructor.name != "VCardPropertyEntry") {
|
|
|
|
throw new Error("Not a VCardPropertyEntry");
|
|
|
|
}
|
|
|
|
|
2022-05-31 12:07:13 +03:00
|
|
|
if (this.entries.find(e => e.equals(entry))) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2022-03-01 04:43:00 +03:00
|
|
|
if (entry.name == "version") {
|
|
|
|
if (entry.value == "3.0") {
|
|
|
|
this.designSet = ICAL.design.vcard3;
|
|
|
|
} else if (entry.value == "4.0") {
|
|
|
|
this.designSet = ICAL.design.vcard;
|
|
|
|
} else {
|
|
|
|
throw new Error(`Unsupported vCard version: ${entry.value}`);
|
|
|
|
}
|
2022-05-31 12:07:13 +03:00
|
|
|
// Version must be the first entry, so clear out any existing values
|
|
|
|
// and add it to the start of the collection.
|
|
|
|
this.clearValues("version");
|
|
|
|
this.entries.unshift(entry);
|
|
|
|
return true;
|
2022-05-27 11:55:19 +03:00
|
|
|
}
|
|
|
|
|
2022-03-01 04:43:00 +03:00
|
|
|
this.entries.push(entry);
|
2022-05-27 11:55:19 +03:00
|
|
|
return true;
|
2022-03-01 04:43:00 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Add an entry to this object by name and value.
|
|
|
|
*
|
|
|
|
* @param {string} name
|
|
|
|
* @param {string} value
|
2022-11-23 07:43:25 +03:00
|
|
|
* @returns {VCardPropertyEntry}
|
2022-03-01 04:43:00 +03:00
|
|
|
*/
|
|
|
|
addValue(name, value) {
|
2023-10-26 12:32:05 +03:00
|
|
|
for (const entry of this.getAllEntries(name)) {
|
2022-03-01 04:43:00 +03:00
|
|
|
if (entry.value == value) {
|
|
|
|
return entry;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-10-26 12:32:05 +03:00
|
|
|
const newEntry = new VCardPropertyEntry(
|
2022-03-01 04:43:00 +03:00
|
|
|
name,
|
|
|
|
{},
|
|
|
|
this.designSet.property[name].defaultType,
|
|
|
|
value
|
|
|
|
);
|
|
|
|
this.entries.push(newEntry);
|
|
|
|
return newEntry;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Remove an entry from this object.
|
|
|
|
*
|
2023-01-08 12:25:54 +03:00
|
|
|
* @param {VCardPropertyEntry} entry - The entry to remove.
|
2022-11-23 07:43:25 +03:00
|
|
|
* @returns {boolean} - If an entry was found and removed.
|
2022-03-01 04:43:00 +03:00
|
|
|
*/
|
|
|
|
removeEntry(entry) {
|
|
|
|
if (entry.constructor.name != "VCardPropertyEntry") {
|
|
|
|
throw new Error("Not a VCardPropertyEntry");
|
|
|
|
}
|
|
|
|
|
2023-10-26 12:32:05 +03:00
|
|
|
const index = this.entries.findIndex(e => e.equals(entry));
|
2022-03-01 04:43:00 +03:00
|
|
|
if (index >= 0) {
|
|
|
|
this.entries.splice(index, 1);
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Remove entries from this object by name and value. All entries matching
|
|
|
|
* the name and value will be removed.
|
|
|
|
*
|
|
|
|
* @param {string} name
|
|
|
|
* @param {string} value
|
|
|
|
*/
|
|
|
|
removeValue(name, value) {
|
2023-10-26 12:32:05 +03:00
|
|
|
for (const entry of this.getAllEntries(name)) {
|
2022-03-01 04:43:00 +03:00
|
|
|
if (entry.value == value) {
|
|
|
|
this.removeEntry(entry);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Remove entries from this object by name. All entries matching the name
|
|
|
|
* will be removed.
|
|
|
|
*
|
|
|
|
* @param {string} name
|
|
|
|
*/
|
|
|
|
clearValues(name) {
|
2023-10-26 12:32:05 +03:00
|
|
|
for (const entry of this.getAllEntries(name)) {
|
2022-03-01 04:43:00 +03:00
|
|
|
this.removeEntry(entry);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Get the first value matching the given name, or null if no entry matches.
|
|
|
|
*
|
|
|
|
* @param {string} name
|
2022-11-23 07:43:25 +03:00
|
|
|
* @returns {?vCardValue}
|
2022-03-01 04:43:00 +03:00
|
|
|
*/
|
|
|
|
getFirstValue(name) {
|
2023-10-26 12:32:05 +03:00
|
|
|
const entry = this.entries.find(e => e.name == name);
|
2022-03-01 04:43:00 +03:00
|
|
|
if (entry) {
|
|
|
|
return entry.value;
|
|
|
|
}
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Get all values matching the given name.
|
|
|
|
*
|
|
|
|
* @param {string} name
|
2022-11-23 07:43:25 +03:00
|
|
|
* @returns {vCardValue[]}
|
2022-03-01 04:43:00 +03:00
|
|
|
*/
|
|
|
|
getAllValues(name) {
|
|
|
|
return this.getAllEntries(name).map(e => e.value);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Get all values matching the given name, sorted in order of preference.
|
|
|
|
* Preference is determined by the `pref` parameter if it exists, then by
|
|
|
|
* the position in `entries`.
|
|
|
|
*
|
|
|
|
* @param {string} name
|
2022-11-23 07:43:25 +03:00
|
|
|
* @returns {vCardValue[]}
|
2022-03-01 04:43:00 +03:00
|
|
|
*/
|
|
|
|
getAllValuesSorted(name) {
|
|
|
|
return this.getAllEntriesSorted(name).map(e => e.value);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Get the first entry matching the given name, or null if no entry matches.
|
|
|
|
*
|
|
|
|
* @param {string} name
|
2022-11-23 07:43:25 +03:00
|
|
|
* @returns {?VCardPropertyEntry}
|
2022-03-01 04:43:00 +03:00
|
|
|
*/
|
|
|
|
getFirstEntry(name) {
|
|
|
|
return this.entries.find(e => e.name == name) ?? null;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Get all entries matching the given name.
|
|
|
|
*
|
|
|
|
* @param {string} name
|
2022-11-23 07:43:25 +03:00
|
|
|
* @returns {VCardPropertyEntry[]}
|
2022-03-01 04:43:00 +03:00
|
|
|
*/
|
|
|
|
getAllEntries(name) {
|
|
|
|
return this.entries.filter(e => e.name == name);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Get all entries matching the given name, sorted in order of preference.
|
|
|
|
* Preference is determined by the `pref` parameter if it exists, then by
|
|
|
|
* the position in `entries`.
|
|
|
|
*
|
|
|
|
* @param {string} name
|
2022-11-23 07:43:25 +03:00
|
|
|
* @returns {VCardPropertyEntry[]}
|
2022-03-01 04:43:00 +03:00
|
|
|
*/
|
|
|
|
getAllEntriesSorted(name) {
|
|
|
|
let nextPref = 101;
|
2023-10-26 12:32:05 +03:00
|
|
|
const entries = this.getAllEntries(name).map(e => {
|
2022-03-01 04:43:00 +03:00
|
|
|
return { entry: e, pref: e.params.pref || nextPref++ };
|
|
|
|
});
|
|
|
|
entries.sort((a, b) => a.pref - b.pref);
|
|
|
|
return entries.map(e => e.entry);
|
|
|
|
}
|
|
|
|
|
2022-05-19 06:33:11 +03:00
|
|
|
/**
|
|
|
|
* Get all entries matching the given group.
|
|
|
|
*
|
|
|
|
* @param {string} group
|
2022-11-23 07:43:25 +03:00
|
|
|
* @returns {VCardPropertyEntry[]}
|
2022-05-19 06:33:11 +03:00
|
|
|
*/
|
|
|
|
getGroupedEntries(group) {
|
|
|
|
return this.entries.filter(e => e.params.group == group);
|
|
|
|
}
|
|
|
|
|
2022-03-01 04:43:00 +03:00
|
|
|
/**
|
|
|
|
* Clone this object.
|
|
|
|
*
|
2022-11-23 07:43:25 +03:00
|
|
|
* @returns {VCardProperties}
|
2022-03-01 04:43:00 +03:00
|
|
|
*/
|
|
|
|
clone() {
|
2023-10-26 12:32:05 +03:00
|
|
|
const copy = new VCardProperties();
|
2022-03-01 04:43:00 +03:00
|
|
|
copy.entries = this.entries.map(e => e.clone());
|
|
|
|
return copy;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Get a Map of Address Book properties from this object.
|
|
|
|
*
|
2022-11-23 07:43:25 +03:00
|
|
|
* @returns {Map<string, string>} propertyMap
|
2022-03-01 04:43:00 +03:00
|
|
|
*/
|
|
|
|
toPropertyMap() {
|
2023-10-26 12:32:05 +03:00
|
|
|
const vPropMap = VCardUtils._parse(this.entries.map(e => e.clone()));
|
|
|
|
const propertyMap = new Map();
|
2022-03-01 04:43:00 +03:00
|
|
|
|
2023-10-26 12:32:05 +03:00
|
|
|
for (const [name, props] of vPropMap) {
|
2022-03-01 04:43:00 +03:00
|
|
|
// Store the value(s) on the abCard.
|
2023-10-26 12:32:05 +03:00
|
|
|
for (const [abPropName, abPropValue] of typeMap[name].toAbCard(
|
2022-03-01 04:43:00 +03:00
|
|
|
props[0].value
|
|
|
|
)) {
|
|
|
|
if (abPropValue) {
|
|
|
|
propertyMap.set(abPropName, abPropValue);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
// Special case for email, which can also have a second preference.
|
|
|
|
if (name == "email" && props.length > 1) {
|
|
|
|
propertyMap.set("SecondEmail", props[1].value);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return propertyMap;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Serialize this object into a vCard.
|
|
|
|
*
|
2022-11-23 07:43:25 +03:00
|
|
|
* @returns {string} vCard
|
2022-03-01 04:43:00 +03:00
|
|
|
*/
|
|
|
|
toVCard() {
|
2023-10-26 12:32:05 +03:00
|
|
|
const jCal = this.entries.map(e => {
|
2022-09-27 03:00:46 +03:00
|
|
|
if (Array.isArray(e.value)) {
|
2023-10-26 12:32:05 +03:00
|
|
|
const design = this.designSet.property[e.name];
|
2022-09-27 03:00:46 +03:00
|
|
|
if (design.multiValue == "," && !design.structuredValue) {
|
|
|
|
// The jCal format stores multiple values as the 4th...nth items,
|
|
|
|
// but VCardPropertyEntry stores them as an array. This applies to
|
|
|
|
// CATEGORIES and NICKNAME types in vCard 4 and also NOTE in vCard 3.
|
|
|
|
return [e.name, e.params, e.type, ...e.value];
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return [e.name, e.params, e.type, e.value];
|
|
|
|
});
|
|
|
|
return ICAL.stringify(["vcard", jCal]);
|
2022-03-01 04:43:00 +03:00
|
|
|
}
|
|
|
|
}
|