File reading exception handling

merge-requests/30/head
Bob Mottram 2021-11-26 12:28:20 +00:00
parent 48154066bf
commit cb0675cf5e
8 changed files with 236 additions and 124 deletions

View File

@ -132,6 +132,7 @@ def authorizeBasic(baseDir: str, path: str, authHeader: str,
print('DEBUG: passwords file missing')
return False
providedPassword = plain.split(':')[1]
try:
with open(passwordFile, 'r') as passfile:
for line in passfile:
if not line.startswith(nickname + ':'):
@ -143,6 +144,9 @@ def authorizeBasic(baseDir: str, path: str, authHeader: str,
if debug:
print('DEBUG: Password check failed for ' + nickname)
return success
except OSError:
print('EX: failed to open password file')
return False
print('DEBUG: Did not find credentials for ' + nickname +
' in ' + passwordFile)
return False
@ -272,15 +276,15 @@ def recordLoginFailure(baseDir: str, ipAddress: str,
if not os.path.isfile(failureLog):
writeType = 'w+'
currTime = datetime.datetime.utcnow()
currTimeStr = currTime.strftime("%Y-%m-%d %H:%M:%SZ")
try:
with open(failureLog, writeType) as fp:
# here we use a similar format to an ssh log, so that
# systems such as fail2ban can parse it
fp.write(currTime.strftime("%Y-%m-%d %H:%M:%SZ") + ' ' +
fp.write(currTimeStr + ' ' +
'ip-127-0-0-1 sshd[20710]: ' +
'Disconnecting invalid user epicyon ' +
ipAddress + ' port 443: ' +
'Too many authentication failures [preauth]\n')
except OSError:
print('EX: recordLoginFailure failed ' + str(failureLog))
pass

View File

@ -94,11 +94,13 @@ def addBlock(baseDir: str, nickname: str, domain: str,
try:
with open(followingFilename, 'r') as followingFile:
followingStr = followingFile.read()
followingStr = followingStr.replace(blockHandle + '\n', '')
except OSError:
print('EX: Unable to read following ' + followingFilename)
return False
if followingStr:
followingStr = followingStr.replace(blockHandle + '\n', '')
try:
with open(followingFilename, 'w+') as followingFile:
followingFile.write(followingStr)
@ -114,11 +116,13 @@ def addBlock(baseDir: str, nickname: str, domain: str,
try:
with open(followersFilename, 'r') as followersFile:
followersStr = followersFile.read()
followersStr = followersStr.replace(blockHandle + '\n', '')
except OSError:
print('EX: Unable to read followers ' + followersFilename)
return False
if followersStr:
followersStr = followersStr.replace(blockHandle + '\n', '')
try:
with open(followersFilename, 'w+') as followersFile:
followersFile.write(followersStr)
@ -145,8 +149,8 @@ def removeGlobalBlock(baseDir: str,
unblockHandle = unblockNickname + '@' + unblockDomain
if os.path.isfile(unblockingFilename):
if unblockHandle in open(unblockingFilename).read():
with open(unblockingFilename, 'r') as fp:
try:
with open(unblockingFilename, 'r') as fp:
with open(unblockingFilename + '.new', 'w+') as fpnew:
for line in fp:
handle = \
@ -157,15 +161,21 @@ def removeGlobalBlock(baseDir: str,
print('EX: failed to remove global block ' +
unblockingFilename + ' ' + str(e))
return False
if os.path.isfile(unblockingFilename + '.new'):
os.rename(unblockingFilename + '.new', unblockingFilename)
try:
os.rename(unblockingFilename + '.new',
unblockingFilename)
except OSError:
print('EX: unable to rename ' + unblockingFilename)
return False
return True
else:
unblockHashtag = unblockNickname
if os.path.isfile(unblockingFilename):
if unblockHashtag + '\n' in open(unblockingFilename).read():
with open(unblockingFilename, 'r') as fp:
try:
with open(unblockingFilename, 'r') as fp:
with open(unblockingFilename + '.new', 'w+') as fpnew:
for line in fp:
blockLine = \
@ -176,8 +186,14 @@ def removeGlobalBlock(baseDir: str,
print('EX: failed to remove global hashtag block ' +
unblockingFilename + ' ' + str(e))
return False
if os.path.isfile(unblockingFilename + '.new'):
os.rename(unblockingFilename + '.new', unblockingFilename)
try:
os.rename(unblockingFilename + '.new',
unblockingFilename)
except OSError:
print('EX: unable to rename 2 ' + unblockingFilename)
return False
return True
return False
@ -191,8 +207,8 @@ def removeBlock(baseDir: str, nickname: str, domain: str,
unblockHandle = unblockNickname + '@' + unblockDomain
if os.path.isfile(unblockingFilename):
if unblockHandle in open(unblockingFilename).read():
with open(unblockingFilename, 'r') as fp:
try:
with open(unblockingFilename, 'r') as fp:
with open(unblockingFilename + '.new', 'w+') as fpnew:
for line in fp:
handle = line.replace('\n', '').replace('\r', '')
@ -202,8 +218,13 @@ def removeBlock(baseDir: str, nickname: str, domain: str,
print('EX: failed to remove block ' +
unblockingFilename + ' ' + str(e))
return False
if os.path.isfile(unblockingFilename + '.new'):
try:
os.rename(unblockingFilename + '.new', unblockingFilename)
except OSError:
print('EX: unable to rename 3 ' + unblockingFilename)
return False
return True
return False
@ -237,8 +258,11 @@ def getDomainBlocklist(baseDir: str) -> str:
globalBlockingFilename = baseDir + '/accounts/blocking.txt'
if not os.path.isfile(globalBlockingFilename):
return blockedStr
try:
with open(globalBlockingFilename, 'r') as fpBlocked:
blockedStr += fpBlocked.read()
except OSError:
print('EX: unable to read ' + globalBlockingFilename)
return blockedStr
@ -258,6 +282,7 @@ def updateBlockedCache(baseDir: str,
globalBlockingFilename = baseDir + '/accounts/blocking.txt'
if not os.path.isfile(globalBlockingFilename):
return blockedCacheLastUpdated
try:
with open(globalBlockingFilename, 'r') as fpBlocked:
blockedLines = fpBlocked.readlines()
# remove newlines
@ -266,6 +291,8 @@ def updateBlockedCache(baseDir: str,
# update the cache
blockedCache.clear()
blockedCache += blockedLines
except OSError as e:
print('EX: unable to read ' + globalBlockingFilename + ' ' + str(e))
return currTime
@ -305,6 +332,7 @@ def isBlockedDomain(baseDir: str, domain: str,
# instance block list
globalBlockingFilename = baseDir + '/accounts/blocking.txt'
if os.path.isfile(globalBlockingFilename):
try:
with open(globalBlockingFilename, 'r') as fpBlocked:
blockedStr = fpBlocked.read()
if '*@' + domain in blockedStr:
@ -312,6 +340,9 @@ def isBlockedDomain(baseDir: str, domain: str,
if shortDomain:
if '*@' + shortDomain in blockedStr:
return True
except OSError as e:
print('EX: unable to read ' + globalBlockingFilename +
' ' + str(e))
else:
allowFilename = baseDir + '/accounts/allowedinstances.txt'
# instance allow list
@ -572,10 +603,10 @@ def mutePost(baseDir: str, nickname: str, domain: str, port: int,
try:
with open(postFilename + '.muted', 'w+') as muteFile:
muteFile.write('\n')
print('MUTE: ' + postFilename + '.muted file added')
except OSError:
print('EX: Failed to save mute file ' + postFilename + '.muted')
return
print('MUTE: ' + postFilename + '.muted file added')
# if the post is in the recent posts cache then mark it as muted
if recentPostsCache.get('index'):
@ -886,6 +917,7 @@ def setBrochMode(baseDir: str, domainFull: str, enabled: bool) -> None:
followingFilename = accountDir + '/' + followFileType
if not os.path.isfile(followingFilename):
continue
try:
with open(followingFilename, 'r') as f:
followList = f.readlines()
for handle in followList:
@ -895,6 +927,9 @@ def setBrochMode(baseDir: str, domainFull: str, enabled: bool) -> None:
handleDomain = handle.split('@')[1]
if handleDomain not in allowedDomains:
allowedDomains.append(handleDomain)
except OSError as e:
print('EX: failed to read ' + followingFilename +
' ' + str(e))
break
# write the allow file

19
blog.py
View File

@ -72,8 +72,12 @@ def _noOfBlogReplies(baseDir: str, httpPrefix: str, translate: {},
removals = []
replies = 0
lines = []
try:
with open(postFilename, 'r') as f:
lines = f.readlines()
except OSError:
print('EX: failed to read blog ' + postFilename)
for replyPostId in lines:
replyPostId = replyPostId.replace('\n', '').replace('\r', '')
replyPostId = replyPostId.replace('.json', '')
@ -135,12 +139,21 @@ def _getBlogReplies(baseDir: str, httpPrefix: str, translate: {},
'/postcache/' + \
postId.replace('/', '#') + '.html'
if os.path.isfile(postFilename):
try:
with open(postFilename, 'r') as postFile:
return postFile.read() + '\n'
except OSError:
print('EX: unable to read blog 3 ' + postFilename)
return ''
lines = []
try:
with open(postFilename, 'r') as f:
lines = f.readlines()
except OSError:
print('EX: unable to read blog 4 ' + postFilename)
if lines:
repliesStr = ''
for replyPostId in lines:
replyPostId = replyPostId.replace('\n', '').replace('\r', '')
@ -151,8 +164,11 @@ def _getBlogReplies(baseDir: str, httpPrefix: str, translate: {},
replyPostId.replace('/', '#') + '.html'
if not os.path.isfile(postFilename):
continue
try:
with open(postFilename, 'r') as postFile:
repliesStr += postFile.read() + '\n'
except OSError:
print('EX: unable to read blog replies ' + postFilename)
rply = _getBlogReplies(baseDir, httpPrefix, translate,
nickname, domain, domainFull,
replyPostId, depth+1)
@ -770,8 +786,11 @@ def htmlEditBlog(mediaInstance: bool, translate: {},
editBlogText = '<h1">' + translate['Write your post text below.'] + '</h1>'
if os.path.isfile(baseDir + '/accounts/newpost.txt'):
try:
with open(baseDir + '/accounts/newpost.txt', 'r') as file:
editBlogText = '<p>' + file.read() + '</p>'
except OSError:
print('EX: unable to read ' + baseDir + '/accounts/newpost.txt')
cssFilename = baseDir + '/epicyon-profile.css'
if os.path.isfile(baseDir + '/epicyon.css'):

View File

@ -71,8 +71,12 @@ def undoBookmarksCollectionEntry(recentPostsCache: {},
if bookmarkIndex not in open(bookmarksIndexFilename).read():
return
indexStr = ''
try:
with open(bookmarksIndexFilename, 'r') as indexFile:
indexStr = indexFile.read().replace(bookmarkIndex + '\n', '')
except OSError:
print('EX: unable to read ' + bookmarksIndexFilename)
if indexStr:
try:
with open(bookmarksIndexFilename, 'w+') as bookmarksIndexFile:
bookmarksIndexFile.write(indexStr)

View File

@ -23,8 +23,12 @@ def getHashtagCategory(baseDir: str, hashtag: str) -> str:
if not os.path.isfile(categoryFilename):
return ''
categoryStr = None
try:
with open(categoryFilename, 'r') as fp:
categoryStr = fp.read()
except OSError:
print('EX: unable to read category ' + categoryFilename)
if categoryStr:
return categoryStr
return ''
@ -161,15 +165,21 @@ def setHashtagCategory(baseDir: str, hashtag: str, category: str,
# don't overwrite any existing categories
if os.path.isfile(categoryFilename):
return False
categoryWritten = False
try:
with open(categoryFilename, 'w+') as fp:
fp.write(category)
if update:
updateHashtagCategories(baseDir)
return True
categoryWritten = True
except OSError as e:
print('EX: unable to write category ' + categoryFilename +
' ' + str(e))
if categoryWritten:
if update:
updateHashtagCategories(baseDir)
return True
return False

10
city.py
View File

@ -196,16 +196,23 @@ def spoofGeolocation(baseDir: str,
default_latdirection, default_longdirection,
"", "", 0)
cities = []
try:
with open(locationsFilename, 'r') as f:
cities = f.readlines()
except OSError:
print('EX: unable to read locations ' + locationsFilename)
nogo = []
if nogoList:
nogo = nogoList
else:
if os.path.isfile(nogoFilename):
nogoList = []
try:
with open(nogoFilename, 'r') as f:
nogoList = f.readlines()
except OSError:
print('EX: unable to read ' + nogoFilename)
for line in nogoList:
if line.startswith(city + ':'):
polygon = parseNogoString(line)
@ -295,8 +302,11 @@ def getSpoofedCity(city: str, baseDir: str, nickname: str, domain: str) -> str:
city = ''
cityFilename = acctDir(baseDir, nickname, domain) + '/city.txt'
if os.path.isfile(cityFilename):
try:
with open(cityFilename, 'r') as fp:
city = fp.read().replace('\n', '')
except OSError:
print('EX: unable to read ' + cityFilename)
return city

View File

@ -178,9 +178,14 @@ def dangerousCSS(filename: str, allowLocalNetworkAccess: bool) -> bool:
if not os.path.isfile(filename):
return False
content = None
try:
with open(filename, 'r') as fp:
content = fp.read().lower()
except OSError:
print('EX: unable to read css file ' + filename)
if content:
cssMatches = ('behavior:', ':expression', '?php', '.php',
'google', 'regexp', 'localhost',
'127.0.', '192.168', '10.0.', '@import')
@ -221,8 +226,11 @@ def switchWords(baseDir: str, nickname: str, domain: str, content: str,
acctDir(baseDir, nickname, domain) + '/replacewords.txt'
if not os.path.isfile(switchWordsFilename):
return content
try:
with open(switchWordsFilename, 'r') as fp:
rules = fp.readlines()
except OSError:
print('EX: unable to read switches ' + switchWordsFilename)
for line in rules:
replaceStr = line.replace('\n', '').replace('\r', '')
@ -779,8 +787,11 @@ def _loadAutoTags(baseDir: str, nickname: str, domain: str) -> []:
filename = acctDir(baseDir, nickname, domain) + '/autotags.txt'
if not os.path.isfile(filename):
return []
try:
with open(filename, 'r') as f:
return f.readlines()
except OSError:
print('EX: unable to read auto tags ' + filename)
return []
@ -853,8 +864,12 @@ def addHtmlTags(baseDir: str, httpPrefix: str,
petnames = None
if '@' in words:
if os.path.isfile(followingFilename):
following = []
try:
with open(followingFilename, 'r') as f:
following = f.readlines()
except OSError:
print('EX: unable to read ' + followingFilename)
for handle in following:
pet = getPetName(baseDir, nickname, domain, handle)
if pet:

View File

@ -2355,13 +2355,16 @@ class PubServer(BaseHTTPRequestHandler):
else:
if os.path.isdir(accountDir):
nwFilename = newswireBlockedFilename
nwWritten = False
try:
with open(nwFilename, 'w+') as noNewswireFile:
noNewswireFile.write('\n')
refreshNewswire(self.server.baseDir)
nwWritten = True
except OSError as e:
print('EX: unable to write ' + nwFilename +
' ' + str(e))
if nwWritten:
refreshNewswire(self.server.baseDir)
usersPathStr = \
usersPath + '/' + self.server.defaultTimeline + \
'?page=' + str(pageNumber)
@ -2398,13 +2401,16 @@ class PubServer(BaseHTTPRequestHandler):
else:
if os.path.isdir(accountDir):
featFilename = featuresBlockedFilename
featWritten = False
try:
with open(featFilename, 'w+') as noFeaturesFile:
noFeaturesFile.write('\n')
refreshNewswire(self.server.baseDir)
featWritten = True
except OSError as e:
print('EX: unable to write ' + featFilename +
' ' + str(e))
if featWritten:
refreshNewswire(self.server.baseDir)
usersPathStr = \
usersPath + '/' + self.server.defaultTimeline + \
'?page=' + str(pageNumber)
@ -3906,8 +3912,11 @@ class PubServer(BaseHTTPRequestHandler):
if fields.get('editedLinks'):
linksStr = fields['editedLinks']
try:
with open(linksFilename, 'w+') as linksFile:
linksFile.write(linksStr)
except OSError:
print('EX: _linksUpdate unable to write ' + linksFilename)
else:
if os.path.isfile(linksFilename):
try:
@ -3923,8 +3932,11 @@ class PubServer(BaseHTTPRequestHandler):
aboutStr = fields['editedAbout']
if not dangerousMarkup(aboutStr,
allowLocalNetworkAccess):
try:
with open(aboutFilename, 'w+') as aboutFile:
aboutFile.write(aboutStr)
except OSError:
print('EX: unable to write about ' + aboutFilename)
else:
if os.path.isfile(aboutFilename):
try:
@ -3937,8 +3949,11 @@ class PubServer(BaseHTTPRequestHandler):
TOSStr = fields['editedTOS']
if not dangerousMarkup(TOSStr,
allowLocalNetworkAccess):
try:
with open(TOSFilename, 'w+') as TOSFile:
TOSFile.write(TOSStr)
except OSError:
print('EX: unable to write TOS ' + TOSFilename)
else:
if os.path.isfile(TOSFilename):
try: