From 6e769484aafb3974878a9247a28a42c0c7acff58 Mon Sep 17 00:00:00 2001 From: Bob Mottram Date: Thu, 11 Jun 2020 13:26:15 +0100 Subject: [PATCH] Reduce duplicate prefixes --- content.py | 4 ++-- epicyon.py | 4 ++-- inbox.py | 4 ++-- utils.py | 14 ++++++++++++++ webfinger.py | 4 ++-- webinterface.py | 12 ++++-------- 6 files changed, 26 insertions(+), 16 deletions(-) diff --git a/content.py b/content.py index 7560d7e8..87fd69a6 100644 --- a/content.py +++ b/content.py @@ -11,6 +11,7 @@ import email.parser from shutil import copyfile from utils import loadJson from utils import fileLastModified +from utils import getLinkPrefixes def switchWords(baseDir: str, nickname: str, domain: str, content: str) -> str: @@ -129,8 +130,7 @@ def addWebLinks(content: str) -> str: if ':' not in content: return content - prefixes = ('https://', 'http://', 'dat://', 'i2p://', 'gnunet://', - 'hyper://', 'gemini://', 'gopher://', 'briar:') + prefixes = getLinkPrefixes() # do any of these prefixes exist within the content? prefixFound = False diff --git a/epicyon.py b/epicyon.py index 4f923da5..ec753c79 100644 --- a/epicyon.py +++ b/epicyon.py @@ -49,6 +49,7 @@ from utils import getDomainFromActor from utils import getNicknameFromActor from utils import followPerson from utils import validNickname +from utils import getProtocolPrefixes from media import archiveMedia from media import getAttachmentMediaType from delete import sendDeleteViaServer @@ -1041,8 +1042,7 @@ if args.actor: args.actor.startswith('http') or \ args.actor.startswith('dat'): # format: https://domain/@nick - prefixes = ('https://', 'http://', 'dat://', 'i2p://', 'gnunet://', - 'hyper://', 'gemini://', 'gopher://') + prefixes = getProtocolPrefixes() for prefix in prefixes: args.actor = args.actor.replace(prefix, '') args.actor = args.actor.replace('/@', '/users/') diff --git a/inbox.py b/inbox.py index 66fb8b01..a439d804 100644 --- a/inbox.py +++ b/inbox.py @@ -10,6 +10,7 @@ import json import os import datetime import time +from utils import getProtocolPrefixes from utils import isBlogPost from utils import removeAvatarFromCache from utils import isPublicPost @@ -1359,8 +1360,7 @@ def receiveAnnounce(recentPostsCache: {}, messageJson['type']) return False - prefixes = ('https://', 'http://', 'dat://', 'i2p://', 'gnunet://', - 'hyper://', 'gemini://', 'gopher://') + prefixes = getProtocolPrefixes() # is the domain of the announce actor blocked? objectDomain = messageJson['object'] for prefix in prefixes: diff --git a/utils.py b/utils.py index 2e642dda..5b9a667f 100644 --- a/utils.py +++ b/utils.py @@ -15,6 +15,20 @@ from pprint import pprint from calendar import monthrange +def getProtocolPrefixes() -> []: + """Returns a list of valid prefixes + """ + return ('https://', 'http://', 'dat://', 'i2p://', 'gnunet://', + 'hyper://', 'gemini://', 'gopher://') + + +def getLinkPrefixes() -> []: + """Returns a list of valid web link prefixes + """ + return ('https://', 'http://', 'dat://', 'i2p://', 'gnunet://', + 'hyper://', 'gemini://', 'gopher://', 'briar:') + + def removeAvatarFromCache(baseDir: str, actorStr: str) -> None: """Removes any existing avatar entries from the cache This avoids duplicate entries with differing extensions diff --git a/webfinger.py b/webfinger.py index b3a0f3d7..dbff7b61 100644 --- a/webfinger.py +++ b/webfinger.py @@ -21,13 +21,13 @@ from cache import getWebfingerFromCache from utils import loadJson from utils import loadJsonOnionify from utils import saveJson +from utils import getProtocolPrefixes def parseHandle(handle: str) -> (str, str): if '.' not in handle: return None, None - prefixes = ('https://', 'http://', 'dat://', 'i2p://', 'gnunet://', - 'hyper://', 'gemini://', 'gopher://') + prefixes = getProtocolPrefixes() handleStr = handle for prefix in prefixes: handleStr = handleStr.replace(prefix, '') diff --git a/webinterface.py b/webinterface.py index 73c3cd1f..aaaa316b 100644 --- a/webinterface.py +++ b/webinterface.py @@ -24,6 +24,7 @@ from ssb import getSSBAddress from tox import getToxAddress from matrix import getMatrixAddress from donate import getDonationUrl +from utils import getProtocolPrefixes from utils import getFileCaseInsensitive from utils import searchBoxPosts from utils import isBlogPost @@ -100,8 +101,7 @@ def getBlogAddress(actorJson: {}) -> str: if propertyValue['type'] != 'PropertyValue': continue propertyValue['value'] = propertyValue['value'].strip() - prefixes = ('https://', 'http://', 'dat://', 'i2p://', 'gnunet://', - 'hyper://', 'gemini://', 'gopher://') + prefixes = getProtocolPrefixes() prefixFound = False for prefix in prefixes: if propertyValue['value'].startswith(prefix): @@ -139,9 +139,7 @@ def setBlogAddress(actorJson: {}, blogAddress: str) -> None: if propertyFound: actorJson['attachment'].remove(propertyFound) - prefixes = ('https://', 'http://', 'dat://', 'i2p://', - 'gnunet://', 'hyper://', 'gemini://', - 'gopher://') + prefixes = getProtocolPrefixes() prefixFound = False for prefix in prefixes: if blogAddress.startswith(prefix): @@ -4007,9 +4005,7 @@ def individualPostAsHtml(recentPostsCache: {}, maxRecentPosts: int, else: postDomain = \ postJsonObject['object']['inReplyTo'] - prefixes = ('https://', 'http://', 'dat://', 'i2p://', - 'gnunet://', 'hyper://', 'gemini://', - 'gopher://') + prefixes = getProtocolPrefixes() for prefix in prefixes: postDomain = postDomain.replace(prefix, '') if '/' in postDomain: