86 lines
3.7 KiB
JavaScript
86 lines
3.7 KiB
JavaScript
// Gaia contacts imported from a user-selected UTF-8 vCard file.
|
|
function unescapeText(value) {
|
|
return value.replace(/\\([nN,;\\])/g, (_, char) => /n/i.test(char) ? '\n' : char);
|
|
}
|
|
|
|
// Split before decoding: escaped commas and semicolons belong to the field.
|
|
function components(value, separator) {
|
|
const result = [''];
|
|
for (let i = 0; i < value.length; i++) {
|
|
if (value[i] === separator) result.push('');
|
|
else {
|
|
result[result.length - 1] += value[i];
|
|
if (value[i] === '\\' && i + 1 < value.length) result[result.length - 1] += value[++i];
|
|
}
|
|
}
|
|
return result;
|
|
}
|
|
|
|
export function readVCards(raw) {
|
|
const cards = [];
|
|
let card;
|
|
const lines = raw.replace(/^\uFEFF/, '').replace(/\r\n?/g, '\n').replace(/\n[ \t]/g, '').split('\n');
|
|
for (const line of lines) {
|
|
if (!line.trim()) continue;
|
|
if (/^BEGIN:VCARD$/i.test(line)) {
|
|
if (card) throw Error('INVALID_VCARD');
|
|
card = {name: [], tel: [], email: []};
|
|
continue;
|
|
}
|
|
if (!card) throw Error('INVALID_VCARD');
|
|
if (/^END:VCARD$/i.test(line)) {
|
|
if (!card.name.length) {
|
|
const name = ['honorificPrefix','givenName','additionalName','familyName','honorificSuffix']
|
|
.flatMap(key => card[key] || []).join(' ');
|
|
if (name) card.name.push(name);
|
|
}
|
|
if (!card.name.length && !card.tel.length && !card.email.length && !card.org?.length && !card.adr?.length && !card.url?.length && !card.nickname?.length) throw Error('INVALID_VCARD');
|
|
cards.push(card);
|
|
card = null;
|
|
continue;
|
|
}
|
|
const field = /^((?:[^":]|"[^"]*")*):(.*)$/.exec(line);
|
|
if (!field || !field[2]) continue;
|
|
const [property, ...params] = field[1].split(';');
|
|
const key = property.split('.').pop().toUpperCase();
|
|
const value = unescapeText(field[2]);
|
|
if (key === 'FN') card.name = [value];
|
|
if (key === 'N') {
|
|
const fields = ['familyName','givenName','additionalName','honorificPrefix','honorificSuffix'];
|
|
components(field[2], ';').slice(0, fields.length).forEach((part, i) => {
|
|
if (part) card[fields[i]] = components(part, ',').map(unescapeText);
|
|
});
|
|
}
|
|
if (key === 'NICKNAME' || key === 'CATEGORIES') {
|
|
const name = key === 'NICKNAME' ? 'nickname' : 'category';
|
|
(card[name] ||= []).push(...components(field[2], ',').map(unescapeText));
|
|
}
|
|
if (key === 'ORG' || key === 'NOTE' || key === 'TITLE') {
|
|
const name = {ORG:'org', NOTE:'note', TITLE:'jobTitle'}[key];
|
|
(card[name] ||= []).push(key === 'ORG' ? components(field[2], ';').map(unescapeText).join(' / ') : value);
|
|
}
|
|
if (['TEL','EMAIL','URL','ADR'].includes(key)) {
|
|
const types = params.flatMap(param => {
|
|
if (param.includes('=') && !/^TYPE=/i.test(param)) return [];
|
|
return param.replace(/^TYPE=/i, '').replace(/"/g, '').toLowerCase().split(',');
|
|
}).filter(Boolean).map(type => type === 'cell' ? 'mobile' : type);
|
|
const preferred = types.includes('pref') || params.some(param => /^PREF=1$/i.test(param));
|
|
const labels = types.filter(type => type !== 'pref');
|
|
const entry = {type: labels.length ? labels : ['other']};
|
|
if (preferred) entry.pref = true;
|
|
if (key === 'ADR') {
|
|
const [box, extended, street, locality, region, postalCode, countryName] =
|
|
components(field[2], ';').map(unescapeText);
|
|
Object.assign(entry, {streetAddress:[box, extended, street].filter(Boolean).join('\n'),
|
|
locality, region, postalCode, countryName});
|
|
} else {
|
|
entry.value = key === 'TEL' ? value.replace(/^tel:/i, '') :
|
|
key === 'EMAIL' ? value.replace(/^mailto:/i, '') : value;
|
|
if (!entry.value) continue;
|
|
}
|
|
(card[key.toLowerCase()] ||= []).push(entry);
|
|
}
|
|
}
|
|
if (card || !cards.length) throw Error('INVALID_VCARD');
|
|
return cards;
|
|
}
|