49 lines
1.6 KiB
Python
49 lines
1.6 KiB
Python
VCARD_KEY_KEY = "KEY:data:application/pgp-keys;base64,"
|
|
|
|
|
|
def parse_vcard(vcard: str) -> (str, str, str):
|
|
"""Parse a vCard and return the email, public key, and display name.
|
|
|
|
:param vcard: the vCard string
|
|
:return: a tuple with the email, the public key, and the display name
|
|
"""
|
|
email, public_key, display_name = ("", "", "")
|
|
for line in vcard.splitlines():
|
|
if line.startswith("EMAIL:"):
|
|
email = line.strip("EMAIL:")
|
|
elif line.startswith(VCARD_KEY_KEY):
|
|
public_key = line.strip(VCARD_KEY_KEY)
|
|
elif line.startswith("FN:"):
|
|
display_name = line.strip("FN:")
|
|
return email, public_key, display_name
|
|
|
|
|
|
def construct_vcard(email: str, public_key: str, display_name: str) -> str:
|
|
"""Construct a VCard 4.0 string with the email address and the public key
|
|
|
|
:param email: the email address of the requested contact
|
|
:param public_key: the public key of the requested contact
|
|
:param display_name: the display name of the requested contact
|
|
:return: the VCard string
|
|
"""
|
|
lines = [
|
|
"BEGIN:VCARD",
|
|
"VERSION:4.0",
|
|
f"EMAIL:{email}",
|
|
f"KEY:data:application/pgp-keys;base64,{public_key}",
|
|
]
|
|
if display_name:
|
|
lines.append(f"FN:{display_name}")
|
|
lines.append("END:VCARD")
|
|
return "\n".join(lines)
|
|
|
|
|
|
def save_vcard(path: str, content: str) -> None:
|
|
"""Save the VCard string to the blob directory
|
|
|
|
:param path: the /tmp path of the .vcf file
|
|
:param content: the VCard content
|
|
"""
|
|
with open(path, "w+") as f:
|
|
f.write(content)
|