From e8aeba7b4ce9e03e513bb907572c9f5be8fb2923 Mon Sep 17 00:00:00 2001 From: Bob Mottram Date: Mon, 4 May 2020 13:58:24 +0000 Subject: [PATCH] Update webfinger aliases when profile is saved --- daemon.py | 6 ++++ webfinger.py | 82 +++++++++++++++++++++++++++++++++++++++++++++++++ webinterface.py | 4 +-- 3 files changed, 90 insertions(+), 2 deletions(-) diff --git a/daemon.py b/daemon.py index 8f6fb992..18a2f50b 100644 --- a/daemon.py +++ b/daemon.py @@ -20,6 +20,7 @@ from webfinger import parseHandle from webfinger import webfingerMeta from webfinger import webfingerNodeInfo from webfinger import webfingerLookup +from webfinger import webfingerUpdate from metadata import metaDataInstance from metadata import metaDataNodeInfo from pgp import getEmailAddress @@ -5726,6 +5727,11 @@ class PubServer(BaseHTTPRequestHandler): if actorChanged: randomizeActorImages(actorJson) saveJson(actorJson, actorFilename) + webfingerUpdate(self.server.baseDir, + nickname, + self.server.domain, + self.server.onionDomain, + self.server.cachedWebfingers) # also copy to the actors cache and # personCache in memory storePersonInCache(self.server.baseDir, diff --git a/webfinger.py b/webfinger.py index 61fa5374..992be6be 100644 --- a/webfinger.py +++ b/webfinger.py @@ -267,3 +267,85 @@ def webfingerLookup(path: str, baseDir: str, if not wfJson: wfJson = {"nickname": "unknown"} return wfJson + + +def webfingerUpdateFromProfile(wfJson: {}, actorJson: {}) -> bool: + """Updates webfinger Email/blog/xmpp links from profile + Returns true if one or more tags has been changed + """ + if not actorJson.get('attachment'): + return False + + changed = False + + webfingerPropertyName = { + "xmpp": "xmpp", + "email": "mailto", + "ssb": "ssb", + "tox": "toxId" + } + + for propertyValue in actorJson['attachment']: + if not propertyValue.get('name'): + continue + propertyName = propertyValue['name'].lower() + if not (propertyName.startswith('ssb') or + propertyName.startswith('xmpp') or + propertyName.startswith('email') or + propertyName.startswith('tox')): + continue + if not propertyValue.get('type'): + continue + if not propertyValue.get('value'): + continue + if propertyValue['type'] != 'PropertyValue': + continue + + newValue = propertyValue['value'].strip() + aliasIndex = 0 + found = False + for alias in wfJson['aliases']: + if alias.startswith(webfingerPropertyName[propertyName] + ':'): + found = True + break + aliasIndex += 1 + newAlias = webfingerPropertyName[propertyName] + ':' + newValue + if found: + if wfJson['aliases'][aliasIndex] != newAlias: + changed = True + wfJson['aliases'][aliasIndex] = newAlias + else: + wfJson['aliases'].append(newAlias) + changed = True + return changed + + +def webfingerUpdate(baseDir: str, nickname: str, domain: str, + onionDomain: str, + cachedWebfingers: {}) -> None: + handle = nickname + '@' + domain + wfSubdir = '/wfendpoints' + if not os.path.isdir(baseDir + wfSubdir): + return + + filename = baseDir + wfSubdir + '/' + handle.lower() + '.json' + onionify = False + if onionDomain: + if onionDomain in handle: + handle = handle.replace(onionDomain, domain) + onionify = True + if not onionify: + wfJson = loadJson(filename) + else: + wfJson = loadJsonOnionify(filename, domain, onionDomain) + if not wfJson: + return + + actorFilename = baseDir + '/accounts/' + handle.lower() + '.json' + actorJson = loadJson(actorFilename) + if not actorJson: + return + + if webfingerUpdateFromProfile(wfJson, actorJson): + if saveJson(wfJson, filename): + cachedWebfingers[handle] = wfJson diff --git a/webinterface.py b/webinterface.py index 2251fd99..1c5fd432 100644 --- a/webinterface.py +++ b/webinterface.py @@ -79,7 +79,7 @@ def getBlogAddress(actorJson: {}) -> str: for propertyValue in actorJson['attachment']: if not propertyValue.get('name'): continue - if not propertyValue['name'].lower().startswith('Blog'): + if not propertyValue['name'].lower().startswith('blog'): continue if not propertyValue.get('type'): continue @@ -116,7 +116,7 @@ def setBlogAddress(actorJson: {}, blogAddress: str) -> None: continue if not propertyValue.get('type'): continue - if not propertyValue['name'].lower().startswith('Blog'): + if not propertyValue['name'].lower().startswith('blog'): continue propertyFound = propertyValue break