xml vcard format

merge-requests/22/merge
Bob Mottram 2022-02-16 11:10:44 +00:00
parent 41f1818cda
commit a21aad9511
4 changed files with 110 additions and 5 deletions

View File

@ -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
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))

View File

@ -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)

80
pgp.py
View File

@ -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 = '<?xml version="1.0" encoding="UTF-8"?>\n'
vcard_str += '<vcards xmlns="urn:ietf:params:xml:ns:vcard-4.0">\n'
vcard_str += ' <vcard>\n'
vcard_str += ' <fn><text>' + actor['name'] + '</text></fn>\n'
vcard_str += ' <nickname><text>' + \
actor['preferredUsername'] + '</text></nickname>\n'
vcard_str += ' <note><text>' + actor['summary'] + '</text></note>\n'
email_address = get_email_address(actor)
if email_address:
vcard_str += ' <email><text>' + email_address + '</text></email>\n'
xmpp_address = get_xmpp_address(actor)
if xmpp_address:
vcard_str += ' <impp>' + \
'<parameters><type><text>xmpp</text></type></parameters>' + \
'<text>' + xmpp_address + '</text></impp>\n'
jami_address = get_jami_address(actor)
if jami_address:
vcard_str += ' <impp>' + \
'<parameters><type><text>jami</text></type></parameters>' + \
'<text>' + jami_address + '</text></impp>\n'
matrix_address = get_matrix_address(actor)
if matrix_address:
vcard_str += ' <impp>' + \
'<parameters><type><text>matrix</text></type></parameters>' + \
'<text>' + matrix_address + '</text></impp>\n'
briar_address = get_briar_address(actor)
if briar_address:
vcard_str += ' <impp>' + \
'<parameters><type><text>briar</text></type></parameters>' + \
'<uri>' + briar_address + '</uri></impp>\n'
cwtch_address = get_cwtch_address(actor)
if cwtch_address:
vcard_str += ' <impp>' + \
'<parameters><type><text>cwtch</text></type></parameters>' + \
'<text>' + cwtch_address + '</text></impp>\n'
vcard_str += ' <url>' + \
'<parameters><type><text>profile</text></type></parameters>' + \
'<uri>' + actor['url'] + '</uri></url>\n'
blog_address = get_blog_address(actor)
if blog_address:
vcard_str += ' <url>' + \
'<parameters><type><text>blog</text></type></parameters>' + \
'<uri>' + blog_address + '</uri></url>\n'
vcard_str += ' <rev>' + actor['published'] + '</rev>\n'
if actor['icon']['url']:
vcard_str += \
' <photo><uri>' + actor['icon']['url'] + '</uri></photo>\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 += \
' <key>' + \
'<parameters>' + \
'<type><text>data</text></type>' + \
'<mediatype>application/pgp-keys;base64</mediatype>' + \
'</parameters>' + \
'<text>' + pgp_key_encoded + '</text></key>\n'
if actor.get('hasOccupation'):
if len(actor['hasOccupation']) > 0:
if actor['hasOccupation'][0].get('name'):
vcard_str += \
' <role><text>' + \
actor['hasOccupation'][0]['name'] + '</text></role>\n'
if actor['hasOccupation'][0].get('occupationLocation'):
city_name = \
actor['hasOccupation'][0]['occupationLocation']['name']
vcard_str += \
' <adr><locality>' + city_name + '</locality></adr>\n'
vcard_str += ' </vcard>\n'
vcard_str += '</vcards>\n'
return vcard_str

View File

@ -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: