diff --git a/content.py b/content.py index eb9615af3..cd10d9d73 100644 --- a/content.py +++ b/content.py @@ -16,6 +16,8 @@ from utils import loadJson from utils import fileLastModified from utils import getLinkPrefixes from utils import dangerousMarkup +from utils import isPGPEncrypted +from utils import containsPGPPublicKey from petnames import getPetName @@ -65,8 +67,7 @@ def _removeQuotesWithinQuotes(content: str) -> str: def htmlReplaceEmailQuote(content: str) -> str: """Replaces an email style quote "> Some quote" with html blockquote """ - if '--BEGIN PGP MESSAGE--' in content or \ - '--BEGIN PGP PUBLIC KEY BLOCK--' in content: + if isPGPEncrypted(content) or containsPGPPublicKey(content): return content # replace quote paragraph if '

"' in content: @@ -109,8 +110,7 @@ def htmlReplaceQuoteMarks(content: str) -> str: """Replaces quotes with html formatting "hello" becomes hello """ - if '--BEGIN PGP MESSAGE--' in content or \ - '--BEGIN PGP PUBLIC KEY BLOCK--' in content: + if isPGPEncrypted(content) or containsPGPPublicKey(content): return content if '"' not in content: if '"' not in content: @@ -203,8 +203,7 @@ def dangerousCSS(filename: str, allowLocalNetworkAccess: bool) -> bool: def switchWords(baseDir: str, nickname: str, domain: str, content: str) -> str: """Performs word replacements. eg. Trump -> The Orange Menace """ - if '--BEGIN PGP MESSAGE--' in content or \ - '--BEGIN PGP PUBLIC KEY BLOCK--' in content: + if isPGPEncrypted(content) or containsPGPPublicKey(content): return content switchWordsFilename = baseDir + '/accounts/' + \ nickname + '@' + domain + '/replacewords.txt' @@ -591,8 +590,7 @@ def _addMention(wordStr: str, httpPrefix: str, following: str, petnames: str, def replaceContentDuplicates(content: str) -> str: """Replaces invalid duplicates within content """ - if '--BEGIN PGP MESSAGE--' in content or \ - '--BEGIN PGP PUBLIC KEY BLOCK--' in content: + if isPGPEncrypted(content) or containsPGPPublicKey(content): return content while '<<' in content: content = content.replace('<<', '<') @@ -605,8 +603,7 @@ def replaceContentDuplicates(content: str) -> str: def removeTextFormatting(content: str) -> str: """Removes markup for bold, italics, etc """ - if '--BEGIN PGP MESSAGE--' in content or \ - '--BEGIN PGP PUBLIC KEY BLOCK--' in content: + if isPGPEncrypted(content) or containsPGPPublicKey(content): return content if '<' not in content: return content @@ -625,8 +622,7 @@ def removeLongWords(content: str, maxWordLength: int, """Breaks up long words so that on mobile screens this doesn't disrupt the layout """ - if '--BEGIN PGP MESSAGE--' in content or \ - '--BEGIN PGP PUBLIC KEY BLOCK--' in content: + if isPGPEncrypted(content) or containsPGPPublicKey(content): return content content = replaceContentDuplicates(content) if ' ' not in content: diff --git a/notifications_client.py b/notifications_client.py index e25fdd5df..20b0381b8 100644 --- a/notifications_client.py +++ b/notifications_client.py @@ -17,6 +17,7 @@ from utils import saveJson from utils import getNicknameFromActor from utils import getDomainFromActor from utils import getFullDomain +from utils import isPGPEncrypted from session import createSession from speaker import getSpeakerFromServer from speaker import getSpeakerPitch @@ -31,7 +32,6 @@ from announce import sendAnnounceViaServer from pgp import pgpDecrypt from pgp import hasLocalPGPkey from pgp import pgpEncryptToActor -from pgp import isPGPEncrypted def _waitForKeypress(timeout: int, debug: bool) -> str: diff --git a/pgp.py b/pgp.py index c5c65209d..5724127a4 100644 --- a/pgp.py +++ b/pgp.py @@ -10,6 +10,8 @@ import os import subprocess from pathlib import Path from person import getActorJson +from utils import containsPGPPublicKey +from utils import isPGPEncrypted def getEmailAddress(actorJson: {}) -> str: @@ -369,24 +371,6 @@ def pgpEncryptToActor(content: str, toHandle: str) -> str: return _pgpEncrypt(content, recipientPubKey) -def isPGPEncrypted(content: str) -> bool: - """Returns true if the given content is PGP encrypted - """ - if '--BEGIN PGP MESSAGE--' in content: - if '--END PGP MESSAGE--' in content: - return True - return False - - -def containsPGPPublicKey(content: str) -> bool: - """Returns true if the given content contains a PGP public key - """ - if '--BEGIN PGP PUBLIC KEY BLOCK--' in content: - if '--END PGP PUBLIC KEY BLOCK--' in content: - return True - return False - - def pgpDecrypt(content: str, fromHandle: str) -> str: """ Encrypt using your default pgp key to the given recipient fromHandle can be a handle or actor url diff --git a/speaker.py b/speaker.py index e28b37466..2d158cc30 100644 --- a/speaker.py +++ b/speaker.py @@ -21,6 +21,7 @@ from utils import removeHtml from utils import loadJson from utils import saveJson from utils import getFullDomain +from utils import isPGPEncrypted from content import htmlReplaceQuoteMarks speakerRemoveChars = ('.\n', '. ', ',', ';', '?', '!') @@ -413,7 +414,7 @@ def _postToSpeakerJson(baseDir: str, httpPrefix: str, content = urllib.parse.unquote_plus(postJsonObject['object']['content']) content = html.unescape(content) content = content.replace('

', '').replace('

', ' ') - if '--BEGIN PGP MESSAGE--' not in content: + if not isPGPEncrypted(content): # replace some emoji before removing html if ' <3' in content: content = content.replace(' <3', ' ' + translate['heart']) diff --git a/tests.py b/tests.py index 00dc92605..a3ba76b63 100644 --- a/tests.py +++ b/tests.py @@ -52,6 +52,8 @@ from utils import getStatusNumber from utils import getFollowersOfPerson from utils import removeHtml from utils import dangerousMarkup +from pgp import extractPGPPublicKey +from utils import containsPGPPublicKey from follow import followerOfPerson from follow import unfollowAccount from follow import unfollowerOfAccount @@ -102,8 +104,6 @@ from mastoapiv1 import getNicknameFromMastoApiV1Id from webapp_post import prepareHtmlPostNickname from webapp_utils import markdownToHtml from speaker import speakerReplaceLinks -from pgp import extractPGPPublicKey -from pgp import containsPGPPublicKey testServerAliceRunning = False testServerBobRunning = False diff --git a/utils.py b/utils.py index 1a977d692..2d09cf0e0 100644 --- a/utils.py +++ b/utils.py @@ -2130,3 +2130,21 @@ def isReply(postJsonObject: {}, actor: str) -> bool: if actor in tag['href']: return True return False + + +def containsPGPPublicKey(content: str) -> bool: + """Returns true if the given content contains a PGP public key + """ + if '--BEGIN PGP PUBLIC KEY BLOCK--' in content: + if '--END PGP PUBLIC KEY BLOCK--' in content: + return True + return False + + +def isPGPEncrypted(content: str) -> bool: + """Returns true if the given content is PGP encrypted + """ + if '--BEGIN PGP MESSAGE--' in content: + if '--END PGP MESSAGE--' in content: + return True + return False