From a21aad95110bdccecede5093a9a74ec4ef58b31d Mon Sep 17 00:00:00 2001 From: Bob Mottram Date: Wed, 16 Feb 2022 11:10:44 +0000 Subject: [PATCH] xml vcard format --- daemon.py | 10 +++++-- epicyon.py | 18 +++++++++++- pgp.py | 80 +++++++++++++++++++++++++++++++++++++++++++++++++++++- session.py | 7 ++++- 4 files changed, 110 insertions(+), 5 deletions(-) diff --git a/daemon.py b/daemon.py index 3cc21070d..19926f4e9 100644 --- a/daemon.py +++ b/daemon.py @@ -32,6 +32,7 @@ from metadata import metadata_custom_emoji from enigma import get_enigma_pub_key from enigma import set_enigma_pub_key from pgp import actor_to_vcard +from pgp import actor_to_vcard_xml from pgp import get_email_address from pgp import set_email_address from pgp import get_pgp_pub_key @@ -1227,11 +1228,16 @@ class PubServer(BaseHTTPRequestHandler): self._404() self.server.vcard_is_active = False return True - vcard_str = actor_to_vcard(actor_json) + if 'vcard+xml' in accept_str: + vcard_str = actor_to_vcard_xml(actor_json) + header_type = 'text/vcard+xml; charset=utf-8' + else: + vcard_str = actor_to_vcard(actor_json) + header_type = 'text/vcard; charset=utf-8' if vcard_str: msg = vcard_str.encode('utf-8') msglen = len(msg) - self._set_headers('text/vcard; charset=utf-8', msglen, + self._set_headers(header_type, msglen, None, calling_domain, True) self._write(msg) print('vcard sent to ' + str(referer_domain)) diff --git a/epicyon.py b/epicyon.py index 831b367ab..635deeda9 100644 --- a/epicyon.py +++ b/epicyon.py @@ -291,6 +291,9 @@ parser.add_argument('--postsraw', dest='postsraw', type=str, help='Show raw json of posts for the given handle') parser.add_argument('--vcard', dest='vcard', type=str, default=None, help='Show the vcard for a given activitypub actor url') +parser.add_argument('--vcardxml', dest='vcardxml', type=str, default=None, + help='Show the xml vcard for a given ' + + 'activitypub actor url') parser.add_argument('--json', dest='json', type=str, default=None, help='Show the json for a given activitypub url') parser.add_argument('--htmlpost', dest='htmlpost', type=str, default=None, @@ -977,7 +980,20 @@ if args.vcard: domain = '' if args.domain: domain = args.domain - test_vcard = get_vcard(session, args.vcard, + test_vcard = get_vcard(False, session, args.vcard, + None, debug, __version__, http_prefix, domain) + if test_vcard: + print(test_vcard) + sys.exit() + +if args.vcardxml: + session = create_session(None) + if not args.domain: + args.domain = get_config_param(base_dir, 'domain') + domain = '' + if args.domain: + domain = args.domain + test_vcard = get_vcard(True, session, args.vcard, None, debug, __version__, http_prefix, domain) if test_vcard: print(test_vcard) diff --git a/pgp.py b/pgp.py index a18b87941..681a7a48a 100644 --- a/pgp.py +++ b/pgp.py @@ -637,7 +637,7 @@ def actor_to_vcard(actor: {}) -> str: vcard_str += 'VERSION:4.0\n' vcard_str += 'REV:' + actor['published'] + '\n' vcard_str += 'FN:' + actor['name'] + '\n' - vcard_str += 'N:' + actor['preferredUsername'] + '\n' + vcard_str += 'NICKNAME:' + actor['preferredUsername'] + '\n' vcard_str += 'URL:' + actor['url'] + '\n' blog_address = get_blog_address(actor) if blog_address: @@ -682,3 +682,81 @@ def actor_to_vcard(actor: {}) -> str: 'ADR:;;;' + city_name + ';;;\n' vcard_str += 'END:VCARD\n' return vcard_str + + +def actor_to_vcard_xml(actor: {}) -> str: + """Returns a xml formatted vcard for a given actor + """ + vcard_str = '\n' + vcard_str += '\n' + vcard_str += ' \n' + vcard_str += ' ' + actor['name'] + '\n' + vcard_str += ' ' + \ + actor['preferredUsername'] + '\n' + vcard_str += ' ' + actor['summary'] + '\n' + email_address = get_email_address(actor) + if email_address: + vcard_str += ' ' + email_address + '\n' + xmpp_address = get_xmpp_address(actor) + if xmpp_address: + vcard_str += ' ' + \ + 'xmpp' + \ + '' + xmpp_address + '\n' + jami_address = get_jami_address(actor) + if jami_address: + vcard_str += ' ' + \ + 'jami' + \ + '' + jami_address + '\n' + matrix_address = get_matrix_address(actor) + if matrix_address: + vcard_str += ' ' + \ + 'matrix' + \ + '' + matrix_address + '\n' + briar_address = get_briar_address(actor) + if briar_address: + vcard_str += ' ' + \ + 'briar' + \ + '' + briar_address + '\n' + cwtch_address = get_cwtch_address(actor) + if cwtch_address: + vcard_str += ' ' + \ + 'cwtch' + \ + '' + cwtch_address + '\n' + vcard_str += ' ' + \ + 'profile' + \ + '' + actor['url'] + '\n' + blog_address = get_blog_address(actor) + if blog_address: + vcard_str += ' ' + \ + 'blog' + \ + '' + blog_address + '\n' + vcard_str += ' ' + actor['published'] + '\n' + if actor['icon']['url']: + vcard_str += \ + ' ' + actor['icon']['url'] + '\n' + pgp_key = get_pgp_pub_key(actor) + if pgp_key: + pgp_key_encoded = \ + base64.b64encode(pgp_key.encode('utf-8')).decode('utf-8') + vcard_str += \ + ' ' + \ + '' + \ + 'data' + \ + 'application/pgp-keys;base64' + \ + '' + \ + '' + pgp_key_encoded + '\n' + if actor.get('hasOccupation'): + if len(actor['hasOccupation']) > 0: + if actor['hasOccupation'][0].get('name'): + vcard_str += \ + ' ' + \ + actor['hasOccupation'][0]['name'] + '\n' + if actor['hasOccupation'][0].get('occupationLocation'): + city_name = \ + actor['hasOccupation'][0]['occupationLocation']['name'] + vcard_str += \ + ' ' + city_name + '\n' + + vcard_str += ' \n' + vcard_str += '\n' + return vcard_str diff --git a/session.py b/session.py index 7e74bcf9b..5e4c57862 100644 --- a/session.py +++ b/session.py @@ -246,7 +246,8 @@ def get_json(signing_priv_key_pem: str, None, quiet, debug, True) -def get_vcard(session, url: str, params: {}, debug: bool, +def get_vcard(xml_format: bool, + session, url: str, params: {}, debug: bool, version: str = '1.3.0', http_prefix: str = 'https', domain: str = 'testdomain', timeout_sec: int = 20, quiet: bool = False) -> {}: @@ -258,6 +259,10 @@ def get_vcard(session, url: str, params: {}, debug: bool, headers = { 'Accept': 'text/vcard' } + if xml_format: + headers = { + 'Accept': 'text/vcard+xml' + } session_params = {} session_headers = {} if headers: