Tidying of encrypted message detection

merge-requests/30/head
Bob Mottram 2021-03-12 12:04:34 +00:00
parent f10b71a132
commit 3b5dba87c3
6 changed files with 33 additions and 34 deletions

View File

@ -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 '<p>&quot;' in content:
@ -109,8 +110,7 @@ def htmlReplaceQuoteMarks(content: str) -> str:
"""Replaces quotes with html formatting
"hello" becomes <q>hello</q>
"""
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 '&quot;' 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:

View File

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

20
pgp.py
View File

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

View File

@ -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('<p>', '').replace('</p>', ' ')
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'])

View File

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

View File

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