// Kontakte mit dem Handy abgleichen (Henning, 04.11.2026): vCard lesen und schreiben (RFC 2426/6350, wie Apple sie liefert). // Das Programm pflegt nur Name, Firma, Telefon, Mobil, E-Mail und Adresse; alles andere in der Karte (Foto, // Geburtstag, weitere Nummern) bleibt beim Zurückschreiben unverändert stehen. export interface KontaktFelder { name: string; firma: string | null; telefon: string | null; mobil: string | null; email: string | null; strasse: string | null; plz: string | null; ort: string | null; } export const FELDER: (keyof KontaktFelder)[] = ["name", "firma", "telefon", "mobil", "email", "strasse", "plz", "ort"]; interface Zeile { /** Gruppe wie „item1“ bei Apple */ gruppe: string; name: string; params: string; wert: string; } /** Fortsetzungszeilen zusammenführen und in Eigenschaften zerlegen */ function zeilen(text: string): Zeile[] { const roh = text.replace(/\r\n|\r/g, "\n").replace(/\n[ \t]/g, "").split("\n").filter((z) => z.trim()); return roh.map((z) => { const i = z.indexOf(":"); const kopf = i < 0 ? z : z.slice(0, i); const wert = i < 0 ? "" : z.slice(i + 1); const [nameTeil, ...params] = kopf.split(";"); const punkt = nameTeil!.lastIndexOf("."); return { gruppe: punkt < 0 ? "" : nameTeil!.slice(0, punkt), name: nameTeil!.slice(punkt + 1).toUpperCase(), params: params.join(";"), wert }; }); } const ent = (s: string) => s.replace(/\\n/gi, "\n").replace(/\\([,;\\])/g, "$1").trim(); const esc = (s: string) => s.replace(/\\/g, "\\\\").replace(/\n/g, "\\n").replace(/([,;])/g, "\\$1"); /** Strukturierte Werte (N, ADR, ORG) an unmaskierten Semikolons teilen */ const teile = (s: string) => s.split(/(? (s && s.trim() ? s.trim() : null); const istMobil = (z: Zeile) => /type=[^;:]*\b(cell|iphone|mobile)\b/i.test(z.params) || /\bcell\b/i.test(z.params); const istTelefon = (z: Zeile) => z.name === "TEL" && !/\bfax\b/i.test(z.params); /** Einzelne Karten aus einer .vcf-Datei mit vielen Kontakten */ export function kartenAus(text: string): string[] { return [...text.replace(/\r\n|\r/g, "\n").matchAll(/BEGIN:VCARD[\s\S]*?END:VCARD/gi)].map((m) => m[0]); } export function uidVon(karte: string): string | null { return leer(zeilen(karte).find((z) => z.name === "UID")?.wert); } /** Letzte Änderung laut Karte (REV), falls angegeben */ export function revVon(karte: string): string | null { const w = leer(zeilen(karte).find((z) => z.name === "REV")?.wert); if (!w) return null; const m = /^(\d{4})-?(\d{2})-?(\d{2})(?:T(\d{2}):?(\d{2}):?(\d{2}))?/.exec(w); return m ? `${m[1]}-${m[2]}-${m[3]}T${m[4] ?? "00"}:${m[5] ?? "00"}:${m[6] ?? "00"}Z` : null; } export function felderAus(karte: string): KontaktFelder { const z = zeilen(karte); const erstes = (n: string) => z.find((x) => x.name === n); const org = leer(teile(erstes("ORG")?.wert ?? "").filter(Boolean).join(", ")); const n = teile(erstes("N")?.wert ?? ""); const ausN = leer([n[3], n[1], n[2], n[0], n[4]].filter((x) => x && x.trim()).join(" ")); const fn = leer(ent(erstes("FN")?.wert ?? "")); const tels = z.filter(istTelefon); const mobil = tels.find(istMobil); const telefon = tels.find((t) => t !== mobil); const adr = z.find((x) => x.name === "ADR"); const a = adr ? teile(adr.wert) : []; return { name: fn ?? ausN ?? org ?? "(ohne Namen)", firma: org, telefon: leer(telefon?.wert), mobil: leer(mobil?.wert), email: leer(erstes("EMAIL")?.wert), strasse: leer([a[1], a[2]].filter((x) => x && x.trim()).join(" ")), plz: leer(a[5]), ort: leer(a[3]), }; } /** Gleiche Inhalte erkennen, ohne auf Schreibweise von Leerzeichen zu achten */ export function fingerabdruck(f: Partial): string { return JSON.stringify(FELDER.map((k) => (f[k] ?? "").toString().replace(/\s+/g, " ").trim())); } /** Telefonnummern vergleichbar machen: nur Ziffern, +49/0049 wie 0 */ export function nummer(t: string | null | undefined): string { const d = (t ?? "").replace(/^\+/, "00").replace(/\D/g, ""); return d.replace(/^0049/, "0"); } /** * Karte mit den Feldern aus dem Programm. Eine vorhandene Karte wird nur in den gepflegten Zeilen geändert; * ohne Karte entsteht eine neue (vCard 3.0, wie Apple sie anlegt). */ export function karteMit(f: KontaktFelder, uid: string, alt: string | null, jetzt = new Date()): string { const z = alt ? zeilen(alt).filter((x) => !["BEGIN", "END"].includes(x.name)) : [{ gruppe: "", name: "VERSION", params: "", wert: "3.0" }]; const setze = (finde: (x: Zeile) => boolean, name: string, params: string, wert: string | null) => { const i = z.findIndex(finde); if (!wert) { if (i >= 0) z.splice(i, 1); return; } if (i >= 0) z[i] = { ...z[i]!, wert }; else z.push({ gruppe: "", name, params, wert }); }; const firmaKontakt = !!f.firma && f.name === f.firma; const altFn = ent(z.find((x) => x.name === "FN")?.wert ?? ""); setze((x) => x.name === "FN", "FN", "", esc(f.name)); // N: Nachname;Vorname – bei Firmen bleibt N leer wie bei Apple const worte = f.name.trim().split(/\s+/); const nWert = firmaKontakt ? ";;;;" : worte.length > 1 ? `${esc(worte.at(-1)!)};${esc(worte.slice(0, -1).join(" "))};;;` : `${esc(f.name)};;;;`; // N nur neu setzen, wenn sich der Name geändert hat; sonst bleibt Apples Aufteilung (z. B. Titel, Namenszusatz) if (!z.some((x) => x.name === "N") || altFn !== f.name) setze((x) => x.name === "N", "N", "", nWert); setze((x) => x.name === "ORG", "ORG", "", f.firma ? `${esc(f.firma)};` : null); setze((x) => istTelefon(x) && istMobil(x), "TEL", "type=CELL;type=VOICE", f.mobil); setze((x) => istTelefon(x) && !istMobil(x), "TEL", "type=WORK;type=VOICE", f.telefon); setze((x) => x.name === "EMAIL", "EMAIL", "type=INTERNET;type=WORK", f.email); const adr = f.strasse || f.plz || f.ort ? `;;${esc(f.strasse ?? "")};${esc(f.ort ?? "")};;${esc(f.plz ?? "")};` : null; const altAdr = z.find((x) => x.name === "ADR"); // Land und Region einer vorhandenen Adresse bleiben stehen const adrWert = adr && altAdr ? (() => { const t = altAdr.wert.split(/(? x.name === "ADR", "ADR", "type=WORK", adrWert); setze((x) => x.name === "UID", "UID", "", uid); setze((x) => x.name === "REV", "REV", "", jetzt.toISOString().replace(/\.\d+Z$/, "Z")); const zeile = (x: Zeile) => `${x.gruppe ? `${x.gruppe}.` : ""}${x.name}${x.params ? `;${x.params}` : ""}:${x.wert}`; return ["BEGIN:VCARD", ...z.map(zeile), "END:VCARD"].join("\r\n") + "\r\n"; }