mirror of https://gitlab.com/bashrc2/epicyon
File reading exception handling
parent
48154066bf
commit
cb0675cf5e
30
auth.py
30
auth.py
|
@ -132,17 +132,21 @@ def authorizeBasic(baseDir: str, path: str, authHeader: str,
|
||||||
print('DEBUG: passwords file missing')
|
print('DEBUG: passwords file missing')
|
||||||
return False
|
return False
|
||||||
providedPassword = plain.split(':')[1]
|
providedPassword = plain.split(':')[1]
|
||||||
with open(passwordFile, 'r') as passfile:
|
try:
|
||||||
for line in passfile:
|
with open(passwordFile, 'r') as passfile:
|
||||||
if not line.startswith(nickname + ':'):
|
for line in passfile:
|
||||||
continue
|
if not line.startswith(nickname + ':'):
|
||||||
storedPassword = \
|
continue
|
||||||
line.split(':')[1].replace('\n', '').replace('\r', '')
|
storedPassword = \
|
||||||
success = _verifyPassword(storedPassword, providedPassword)
|
line.split(':')[1].replace('\n', '').replace('\r', '')
|
||||||
if not success:
|
success = _verifyPassword(storedPassword, providedPassword)
|
||||||
if debug:
|
if not success:
|
||||||
print('DEBUG: Password check failed for ' + nickname)
|
if debug:
|
||||||
return success
|
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 +
|
print('DEBUG: Did not find credentials for ' + nickname +
|
||||||
' in ' + passwordFile)
|
' in ' + passwordFile)
|
||||||
return False
|
return False
|
||||||
|
@ -272,15 +276,15 @@ def recordLoginFailure(baseDir: str, ipAddress: str,
|
||||||
if not os.path.isfile(failureLog):
|
if not os.path.isfile(failureLog):
|
||||||
writeType = 'w+'
|
writeType = 'w+'
|
||||||
currTime = datetime.datetime.utcnow()
|
currTime = datetime.datetime.utcnow()
|
||||||
|
currTimeStr = currTime.strftime("%Y-%m-%d %H:%M:%SZ")
|
||||||
try:
|
try:
|
||||||
with open(failureLog, writeType) as fp:
|
with open(failureLog, writeType) as fp:
|
||||||
# here we use a similar format to an ssh log, so that
|
# here we use a similar format to an ssh log, so that
|
||||||
# systems such as fail2ban can parse it
|
# 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]: ' +
|
'ip-127-0-0-1 sshd[20710]: ' +
|
||||||
'Disconnecting invalid user epicyon ' +
|
'Disconnecting invalid user epicyon ' +
|
||||||
ipAddress + ' port 443: ' +
|
ipAddress + ' port 443: ' +
|
||||||
'Too many authentication failures [preauth]\n')
|
'Too many authentication failures [preauth]\n')
|
||||||
except OSError:
|
except OSError:
|
||||||
print('EX: recordLoginFailure failed ' + str(failureLog))
|
print('EX: recordLoginFailure failed ' + str(failureLog))
|
||||||
pass
|
|
||||||
|
|
133
blocking.py
133
blocking.py
|
@ -94,11 +94,13 @@ def addBlock(baseDir: str, nickname: str, domain: str,
|
||||||
try:
|
try:
|
||||||
with open(followingFilename, 'r') as followingFile:
|
with open(followingFilename, 'r') as followingFile:
|
||||||
followingStr = followingFile.read()
|
followingStr = followingFile.read()
|
||||||
followingStr = followingStr.replace(blockHandle + '\n', '')
|
|
||||||
except OSError:
|
except OSError:
|
||||||
print('EX: Unable to read following ' + followingFilename)
|
print('EX: Unable to read following ' + followingFilename)
|
||||||
return False
|
return False
|
||||||
|
|
||||||
|
if followingStr:
|
||||||
|
followingStr = followingStr.replace(blockHandle + '\n', '')
|
||||||
|
|
||||||
try:
|
try:
|
||||||
with open(followingFilename, 'w+') as followingFile:
|
with open(followingFilename, 'w+') as followingFile:
|
||||||
followingFile.write(followingStr)
|
followingFile.write(followingStr)
|
||||||
|
@ -114,11 +116,13 @@ def addBlock(baseDir: str, nickname: str, domain: str,
|
||||||
try:
|
try:
|
||||||
with open(followersFilename, 'r') as followersFile:
|
with open(followersFilename, 'r') as followersFile:
|
||||||
followersStr = followersFile.read()
|
followersStr = followersFile.read()
|
||||||
followersStr = followersStr.replace(blockHandle + '\n', '')
|
|
||||||
except OSError:
|
except OSError:
|
||||||
print('EX: Unable to read followers ' + followersFilename)
|
print('EX: Unable to read followers ' + followersFilename)
|
||||||
return False
|
return False
|
||||||
|
|
||||||
|
if followersStr:
|
||||||
|
followersStr = followersStr.replace(blockHandle + '\n', '')
|
||||||
|
|
||||||
try:
|
try:
|
||||||
with open(followersFilename, 'w+') as followersFile:
|
with open(followersFilename, 'w+') as followersFile:
|
||||||
followersFile.write(followersStr)
|
followersFile.write(followersStr)
|
||||||
|
@ -145,39 +149,51 @@ def removeGlobalBlock(baseDir: str,
|
||||||
unblockHandle = unblockNickname + '@' + unblockDomain
|
unblockHandle = unblockNickname + '@' + unblockDomain
|
||||||
if os.path.isfile(unblockingFilename):
|
if os.path.isfile(unblockingFilename):
|
||||||
if unblockHandle in open(unblockingFilename).read():
|
if unblockHandle in open(unblockingFilename).read():
|
||||||
with open(unblockingFilename, 'r') as fp:
|
try:
|
||||||
try:
|
with open(unblockingFilename, 'r') as fp:
|
||||||
with open(unblockingFilename + '.new', 'w+') as fpnew:
|
with open(unblockingFilename + '.new', 'w+') as fpnew:
|
||||||
for line in fp:
|
for line in fp:
|
||||||
handle = \
|
handle = \
|
||||||
line.replace('\n', '').replace('\r', '')
|
line.replace('\n', '').replace('\r', '')
|
||||||
if unblockHandle not in line:
|
if unblockHandle not in line:
|
||||||
fpnew.write(handle + '\n')
|
fpnew.write(handle + '\n')
|
||||||
except OSError as e:
|
except OSError as e:
|
||||||
print('EX: failed to remove global block ' +
|
print('EX: failed to remove global block ' +
|
||||||
unblockingFilename + ' ' + str(e))
|
unblockingFilename + ' ' + str(e))
|
||||||
return False
|
return False
|
||||||
|
|
||||||
if os.path.isfile(unblockingFilename + '.new'):
|
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
|
return True
|
||||||
else:
|
else:
|
||||||
unblockHashtag = unblockNickname
|
unblockHashtag = unblockNickname
|
||||||
if os.path.isfile(unblockingFilename):
|
if os.path.isfile(unblockingFilename):
|
||||||
if unblockHashtag + '\n' in open(unblockingFilename).read():
|
if unblockHashtag + '\n' in open(unblockingFilename).read():
|
||||||
with open(unblockingFilename, 'r') as fp:
|
try:
|
||||||
try:
|
with open(unblockingFilename, 'r') as fp:
|
||||||
with open(unblockingFilename + '.new', 'w+') as fpnew:
|
with open(unblockingFilename + '.new', 'w+') as fpnew:
|
||||||
for line in fp:
|
for line in fp:
|
||||||
blockLine = \
|
blockLine = \
|
||||||
line.replace('\n', '').replace('\r', '')
|
line.replace('\n', '').replace('\r', '')
|
||||||
if unblockHashtag not in line:
|
if unblockHashtag not in line:
|
||||||
fpnew.write(blockLine + '\n')
|
fpnew.write(blockLine + '\n')
|
||||||
except OSError as e:
|
except OSError as e:
|
||||||
print('EX: failed to remove global hashtag block ' +
|
print('EX: failed to remove global hashtag block ' +
|
||||||
unblockingFilename + ' ' + str(e))
|
unblockingFilename + ' ' + str(e))
|
||||||
return False
|
return False
|
||||||
|
|
||||||
if os.path.isfile(unblockingFilename + '.new'):
|
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 True
|
||||||
return False
|
return False
|
||||||
|
|
||||||
|
@ -191,19 +207,24 @@ def removeBlock(baseDir: str, nickname: str, domain: str,
|
||||||
unblockHandle = unblockNickname + '@' + unblockDomain
|
unblockHandle = unblockNickname + '@' + unblockDomain
|
||||||
if os.path.isfile(unblockingFilename):
|
if os.path.isfile(unblockingFilename):
|
||||||
if unblockHandle in open(unblockingFilename).read():
|
if unblockHandle in open(unblockingFilename).read():
|
||||||
with open(unblockingFilename, 'r') as fp:
|
try:
|
||||||
try:
|
with open(unblockingFilename, 'r') as fp:
|
||||||
with open(unblockingFilename + '.new', 'w+') as fpnew:
|
with open(unblockingFilename + '.new', 'w+') as fpnew:
|
||||||
for line in fp:
|
for line in fp:
|
||||||
handle = line.replace('\n', '').replace('\r', '')
|
handle = line.replace('\n', '').replace('\r', '')
|
||||||
if unblockHandle not in line:
|
if unblockHandle not in line:
|
||||||
fpnew.write(handle + '\n')
|
fpnew.write(handle + '\n')
|
||||||
except OSError as e:
|
except OSError as e:
|
||||||
print('EX: failed to remove block ' +
|
print('EX: failed to remove block ' +
|
||||||
unblockingFilename + ' ' + str(e))
|
unblockingFilename + ' ' + str(e))
|
||||||
return False
|
return False
|
||||||
|
|
||||||
if os.path.isfile(unblockingFilename + '.new'):
|
if os.path.isfile(unblockingFilename + '.new'):
|
||||||
os.rename(unblockingFilename + '.new', unblockingFilename)
|
try:
|
||||||
|
os.rename(unblockingFilename + '.new', unblockingFilename)
|
||||||
|
except OSError:
|
||||||
|
print('EX: unable to rename 3 ' + unblockingFilename)
|
||||||
|
return False
|
||||||
return True
|
return True
|
||||||
return False
|
return False
|
||||||
|
|
||||||
|
@ -237,8 +258,11 @@ def getDomainBlocklist(baseDir: str) -> str:
|
||||||
globalBlockingFilename = baseDir + '/accounts/blocking.txt'
|
globalBlockingFilename = baseDir + '/accounts/blocking.txt'
|
||||||
if not os.path.isfile(globalBlockingFilename):
|
if not os.path.isfile(globalBlockingFilename):
|
||||||
return blockedStr
|
return blockedStr
|
||||||
with open(globalBlockingFilename, 'r') as fpBlocked:
|
try:
|
||||||
blockedStr += fpBlocked.read()
|
with open(globalBlockingFilename, 'r') as fpBlocked:
|
||||||
|
blockedStr += fpBlocked.read()
|
||||||
|
except OSError:
|
||||||
|
print('EX: unable to read ' + globalBlockingFilename)
|
||||||
return blockedStr
|
return blockedStr
|
||||||
|
|
||||||
|
|
||||||
|
@ -258,14 +282,17 @@ def updateBlockedCache(baseDir: str,
|
||||||
globalBlockingFilename = baseDir + '/accounts/blocking.txt'
|
globalBlockingFilename = baseDir + '/accounts/blocking.txt'
|
||||||
if not os.path.isfile(globalBlockingFilename):
|
if not os.path.isfile(globalBlockingFilename):
|
||||||
return blockedCacheLastUpdated
|
return blockedCacheLastUpdated
|
||||||
with open(globalBlockingFilename, 'r') as fpBlocked:
|
try:
|
||||||
blockedLines = fpBlocked.readlines()
|
with open(globalBlockingFilename, 'r') as fpBlocked:
|
||||||
# remove newlines
|
blockedLines = fpBlocked.readlines()
|
||||||
for index in range(len(blockedLines)):
|
# remove newlines
|
||||||
blockedLines[index] = blockedLines[index].replace('\n', '')
|
for index in range(len(blockedLines)):
|
||||||
# update the cache
|
blockedLines[index] = blockedLines[index].replace('\n', '')
|
||||||
blockedCache.clear()
|
# update the cache
|
||||||
blockedCache += blockedLines
|
blockedCache.clear()
|
||||||
|
blockedCache += blockedLines
|
||||||
|
except OSError as e:
|
||||||
|
print('EX: unable to read ' + globalBlockingFilename + ' ' + str(e))
|
||||||
return currTime
|
return currTime
|
||||||
|
|
||||||
|
|
||||||
|
@ -305,13 +332,17 @@ def isBlockedDomain(baseDir: str, domain: str,
|
||||||
# instance block list
|
# instance block list
|
||||||
globalBlockingFilename = baseDir + '/accounts/blocking.txt'
|
globalBlockingFilename = baseDir + '/accounts/blocking.txt'
|
||||||
if os.path.isfile(globalBlockingFilename):
|
if os.path.isfile(globalBlockingFilename):
|
||||||
with open(globalBlockingFilename, 'r') as fpBlocked:
|
try:
|
||||||
blockedStr = fpBlocked.read()
|
with open(globalBlockingFilename, 'r') as fpBlocked:
|
||||||
if '*@' + domain in blockedStr:
|
blockedStr = fpBlocked.read()
|
||||||
return True
|
if '*@' + domain in blockedStr:
|
||||||
if shortDomain:
|
|
||||||
if '*@' + shortDomain in blockedStr:
|
|
||||||
return True
|
return True
|
||||||
|
if shortDomain:
|
||||||
|
if '*@' + shortDomain in blockedStr:
|
||||||
|
return True
|
||||||
|
except OSError as e:
|
||||||
|
print('EX: unable to read ' + globalBlockingFilename +
|
||||||
|
' ' + str(e))
|
||||||
else:
|
else:
|
||||||
allowFilename = baseDir + '/accounts/allowedinstances.txt'
|
allowFilename = baseDir + '/accounts/allowedinstances.txt'
|
||||||
# instance allow list
|
# instance allow list
|
||||||
|
@ -572,10 +603,10 @@ def mutePost(baseDir: str, nickname: str, domain: str, port: int,
|
||||||
try:
|
try:
|
||||||
with open(postFilename + '.muted', 'w+') as muteFile:
|
with open(postFilename + '.muted', 'w+') as muteFile:
|
||||||
muteFile.write('\n')
|
muteFile.write('\n')
|
||||||
print('MUTE: ' + postFilename + '.muted file added')
|
|
||||||
except OSError:
|
except OSError:
|
||||||
print('EX: Failed to save mute file ' + postFilename + '.muted')
|
print('EX: Failed to save mute file ' + postFilename + '.muted')
|
||||||
return
|
return
|
||||||
|
print('MUTE: ' + postFilename + '.muted file added')
|
||||||
|
|
||||||
# if the post is in the recent posts cache then mark it as muted
|
# if the post is in the recent posts cache then mark it as muted
|
||||||
if recentPostsCache.get('index'):
|
if recentPostsCache.get('index'):
|
||||||
|
@ -886,15 +917,19 @@ def setBrochMode(baseDir: str, domainFull: str, enabled: bool) -> None:
|
||||||
followingFilename = accountDir + '/' + followFileType
|
followingFilename = accountDir + '/' + followFileType
|
||||||
if not os.path.isfile(followingFilename):
|
if not os.path.isfile(followingFilename):
|
||||||
continue
|
continue
|
||||||
with open(followingFilename, 'r') as f:
|
try:
|
||||||
followList = f.readlines()
|
with open(followingFilename, 'r') as f:
|
||||||
for handle in followList:
|
followList = f.readlines()
|
||||||
if '@' not in handle:
|
for handle in followList:
|
||||||
continue
|
if '@' not in handle:
|
||||||
handle = handle.replace('\n', '')
|
continue
|
||||||
handleDomain = handle.split('@')[1]
|
handle = handle.replace('\n', '')
|
||||||
if handleDomain not in allowedDomains:
|
handleDomain = handle.split('@')[1]
|
||||||
allowedDomains.append(handleDomain)
|
if handleDomain not in allowedDomains:
|
||||||
|
allowedDomains.append(handleDomain)
|
||||||
|
except OSError as e:
|
||||||
|
print('EX: failed to read ' + followingFilename +
|
||||||
|
' ' + str(e))
|
||||||
break
|
break
|
||||||
|
|
||||||
# write the allow file
|
# write the allow file
|
||||||
|
|
63
blog.py
63
blog.py
|
@ -72,20 +72,24 @@ def _noOfBlogReplies(baseDir: str, httpPrefix: str, translate: {},
|
||||||
removals = []
|
removals = []
|
||||||
replies = 0
|
replies = 0
|
||||||
lines = []
|
lines = []
|
||||||
with open(postFilename, 'r') as f:
|
try:
|
||||||
lines = f.readlines()
|
with open(postFilename, 'r') as f:
|
||||||
for replyPostId in lines:
|
lines = f.readlines()
|
||||||
replyPostId = replyPostId.replace('\n', '').replace('\r', '')
|
except OSError:
|
||||||
replyPostId = replyPostId.replace('.json', '')
|
print('EX: failed to read blog ' + postFilename)
|
||||||
if locatePost(baseDir, nickname, domain, replyPostId):
|
|
||||||
replyPostId = replyPostId.replace('.replies', '')
|
for replyPostId in lines:
|
||||||
replies += \
|
replyPostId = replyPostId.replace('\n', '').replace('\r', '')
|
||||||
1 + _noOfBlogReplies(baseDir, httpPrefix, translate,
|
replyPostId = replyPostId.replace('.json', '')
|
||||||
nickname, domain, domainFull,
|
if locatePost(baseDir, nickname, domain, replyPostId):
|
||||||
replyPostId, depth+1)
|
replyPostId = replyPostId.replace('.replies', '')
|
||||||
else:
|
replies += \
|
||||||
# remove post which no longer exists
|
1 + _noOfBlogReplies(baseDir, httpPrefix, translate,
|
||||||
removals.append(replyPostId)
|
nickname, domain, domainFull,
|
||||||
|
replyPostId, depth+1)
|
||||||
|
else:
|
||||||
|
# remove post which no longer exists
|
||||||
|
removals.append(replyPostId)
|
||||||
|
|
||||||
# remove posts from .replies file if they don't exist
|
# remove posts from .replies file if they don't exist
|
||||||
if lines and removals:
|
if lines and removals:
|
||||||
|
@ -135,12 +139,21 @@ def _getBlogReplies(baseDir: str, httpPrefix: str, translate: {},
|
||||||
'/postcache/' + \
|
'/postcache/' + \
|
||||||
postId.replace('/', '#') + '.html'
|
postId.replace('/', '#') + '.html'
|
||||||
if os.path.isfile(postFilename):
|
if os.path.isfile(postFilename):
|
||||||
with open(postFilename, 'r') as postFile:
|
try:
|
||||||
return postFile.read() + '\n'
|
with open(postFilename, 'r') as postFile:
|
||||||
|
return postFile.read() + '\n'
|
||||||
|
except OSError:
|
||||||
|
print('EX: unable to read blog 3 ' + postFilename)
|
||||||
return ''
|
return ''
|
||||||
|
|
||||||
with open(postFilename, 'r') as f:
|
lines = []
|
||||||
lines = f.readlines()
|
try:
|
||||||
|
with open(postFilename, 'r') as f:
|
||||||
|
lines = f.readlines()
|
||||||
|
except OSError:
|
||||||
|
print('EX: unable to read blog 4 ' + postFilename)
|
||||||
|
|
||||||
|
if lines:
|
||||||
repliesStr = ''
|
repliesStr = ''
|
||||||
for replyPostId in lines:
|
for replyPostId in lines:
|
||||||
replyPostId = replyPostId.replace('\n', '').replace('\r', '')
|
replyPostId = replyPostId.replace('\n', '').replace('\r', '')
|
||||||
|
@ -151,8 +164,11 @@ def _getBlogReplies(baseDir: str, httpPrefix: str, translate: {},
|
||||||
replyPostId.replace('/', '#') + '.html'
|
replyPostId.replace('/', '#') + '.html'
|
||||||
if not os.path.isfile(postFilename):
|
if not os.path.isfile(postFilename):
|
||||||
continue
|
continue
|
||||||
with open(postFilename, 'r') as postFile:
|
try:
|
||||||
repliesStr += postFile.read() + '\n'
|
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,
|
rply = _getBlogReplies(baseDir, httpPrefix, translate,
|
||||||
nickname, domain, domainFull,
|
nickname, domain, domainFull,
|
||||||
replyPostId, depth+1)
|
replyPostId, depth+1)
|
||||||
|
@ -770,8 +786,11 @@ def htmlEditBlog(mediaInstance: bool, translate: {},
|
||||||
editBlogText = '<h1">' + translate['Write your post text below.'] + '</h1>'
|
editBlogText = '<h1">' + translate['Write your post text below.'] + '</h1>'
|
||||||
|
|
||||||
if os.path.isfile(baseDir + '/accounts/newpost.txt'):
|
if os.path.isfile(baseDir + '/accounts/newpost.txt'):
|
||||||
with open(baseDir + '/accounts/newpost.txt', 'r') as file:
|
try:
|
||||||
editBlogText = '<p>' + file.read() + '</p>'
|
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'
|
cssFilename = baseDir + '/epicyon-profile.css'
|
||||||
if os.path.isfile(baseDir + '/epicyon.css'):
|
if os.path.isfile(baseDir + '/epicyon.css'):
|
||||||
|
|
|
@ -71,8 +71,12 @@ def undoBookmarksCollectionEntry(recentPostsCache: {},
|
||||||
if bookmarkIndex not in open(bookmarksIndexFilename).read():
|
if bookmarkIndex not in open(bookmarksIndexFilename).read():
|
||||||
return
|
return
|
||||||
indexStr = ''
|
indexStr = ''
|
||||||
with open(bookmarksIndexFilename, 'r') as indexFile:
|
try:
|
||||||
indexStr = indexFile.read().replace(bookmarkIndex + '\n', '')
|
with open(bookmarksIndexFilename, 'r') as indexFile:
|
||||||
|
indexStr = indexFile.read().replace(bookmarkIndex + '\n', '')
|
||||||
|
except OSError:
|
||||||
|
print('EX: unable to read ' + bookmarksIndexFilename)
|
||||||
|
if indexStr:
|
||||||
try:
|
try:
|
||||||
with open(bookmarksIndexFilename, 'w+') as bookmarksIndexFile:
|
with open(bookmarksIndexFilename, 'w+') as bookmarksIndexFile:
|
||||||
bookmarksIndexFile.write(indexStr)
|
bookmarksIndexFile.write(indexStr)
|
||||||
|
|
|
@ -23,10 +23,14 @@ def getHashtagCategory(baseDir: str, hashtag: str) -> str:
|
||||||
if not os.path.isfile(categoryFilename):
|
if not os.path.isfile(categoryFilename):
|
||||||
return ''
|
return ''
|
||||||
|
|
||||||
with open(categoryFilename, 'r') as fp:
|
categoryStr = None
|
||||||
categoryStr = fp.read()
|
try:
|
||||||
if categoryStr:
|
with open(categoryFilename, 'r') as fp:
|
||||||
return categoryStr
|
categoryStr = fp.read()
|
||||||
|
except OSError:
|
||||||
|
print('EX: unable to read category ' + categoryFilename)
|
||||||
|
if categoryStr:
|
||||||
|
return categoryStr
|
||||||
return ''
|
return ''
|
||||||
|
|
||||||
|
|
||||||
|
@ -161,15 +165,21 @@ def setHashtagCategory(baseDir: str, hashtag: str, category: str,
|
||||||
# don't overwrite any existing categories
|
# don't overwrite any existing categories
|
||||||
if os.path.isfile(categoryFilename):
|
if os.path.isfile(categoryFilename):
|
||||||
return False
|
return False
|
||||||
|
|
||||||
|
categoryWritten = False
|
||||||
try:
|
try:
|
||||||
with open(categoryFilename, 'w+') as fp:
|
with open(categoryFilename, 'w+') as fp:
|
||||||
fp.write(category)
|
fp.write(category)
|
||||||
if update:
|
categoryWritten = True
|
||||||
updateHashtagCategories(baseDir)
|
|
||||||
return True
|
|
||||||
except OSError as e:
|
except OSError as e:
|
||||||
print('EX: unable to write category ' + categoryFilename +
|
print('EX: unable to write category ' + categoryFilename +
|
||||||
' ' + str(e))
|
' ' + str(e))
|
||||||
|
|
||||||
|
if categoryWritten:
|
||||||
|
if update:
|
||||||
|
updateHashtagCategories(baseDir)
|
||||||
|
return True
|
||||||
|
|
||||||
return False
|
return False
|
||||||
|
|
||||||
|
|
||||||
|
|
32
city.py
32
city.py
|
@ -196,21 +196,28 @@ def spoofGeolocation(baseDir: str,
|
||||||
default_latdirection, default_longdirection,
|
default_latdirection, default_longdirection,
|
||||||
"", "", 0)
|
"", "", 0)
|
||||||
cities = []
|
cities = []
|
||||||
with open(locationsFilename, 'r') as f:
|
try:
|
||||||
cities = f.readlines()
|
with open(locationsFilename, 'r') as f:
|
||||||
|
cities = f.readlines()
|
||||||
|
except OSError:
|
||||||
|
print('EX: unable to read locations ' + locationsFilename)
|
||||||
|
|
||||||
nogo = []
|
nogo = []
|
||||||
if nogoList:
|
if nogoList:
|
||||||
nogo = nogoList
|
nogo = nogoList
|
||||||
else:
|
else:
|
||||||
if os.path.isfile(nogoFilename):
|
if os.path.isfile(nogoFilename):
|
||||||
with open(nogoFilename, 'r') as f:
|
nogoList = []
|
||||||
nogoList = f.readlines()
|
try:
|
||||||
for line in nogoList:
|
with open(nogoFilename, 'r') as f:
|
||||||
if line.startswith(city + ':'):
|
nogoList = f.readlines()
|
||||||
polygon = parseNogoString(line)
|
except OSError:
|
||||||
if polygon:
|
print('EX: unable to read ' + nogoFilename)
|
||||||
nogo.append(polygon)
|
for line in nogoList:
|
||||||
|
if line.startswith(city + ':'):
|
||||||
|
polygon = parseNogoString(line)
|
||||||
|
if polygon:
|
||||||
|
nogo.append(polygon)
|
||||||
|
|
||||||
city = city.lower()
|
city = city.lower()
|
||||||
for cityName in cities:
|
for cityName in cities:
|
||||||
|
@ -295,8 +302,11 @@ def getSpoofedCity(city: str, baseDir: str, nickname: str, domain: str) -> str:
|
||||||
city = ''
|
city = ''
|
||||||
cityFilename = acctDir(baseDir, nickname, domain) + '/city.txt'
|
cityFilename = acctDir(baseDir, nickname, domain) + '/city.txt'
|
||||||
if os.path.isfile(cityFilename):
|
if os.path.isfile(cityFilename):
|
||||||
with open(cityFilename, 'r') as fp:
|
try:
|
||||||
city = fp.read().replace('\n', '')
|
with open(cityFilename, 'r') as fp:
|
||||||
|
city = fp.read().replace('\n', '')
|
||||||
|
except OSError:
|
||||||
|
print('EX: unable to read ' + cityFilename)
|
||||||
return city
|
return city
|
||||||
|
|
||||||
|
|
||||||
|
|
39
content.py
39
content.py
|
@ -178,9 +178,14 @@ def dangerousCSS(filename: str, allowLocalNetworkAccess: bool) -> bool:
|
||||||
if not os.path.isfile(filename):
|
if not os.path.isfile(filename):
|
||||||
return False
|
return False
|
||||||
|
|
||||||
with open(filename, 'r') as fp:
|
content = None
|
||||||
content = fp.read().lower()
|
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',
|
cssMatches = ('behavior:', ':expression', '?php', '.php',
|
||||||
'google', 'regexp', 'localhost',
|
'google', 'regexp', 'localhost',
|
||||||
'127.0.', '192.168', '10.0.', '@import')
|
'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'
|
acctDir(baseDir, nickname, domain) + '/replacewords.txt'
|
||||||
if not os.path.isfile(switchWordsFilename):
|
if not os.path.isfile(switchWordsFilename):
|
||||||
return content
|
return content
|
||||||
with open(switchWordsFilename, 'r') as fp:
|
try:
|
||||||
rules = fp.readlines()
|
with open(switchWordsFilename, 'r') as fp:
|
||||||
|
rules = fp.readlines()
|
||||||
|
except OSError:
|
||||||
|
print('EX: unable to read switches ' + switchWordsFilename)
|
||||||
|
|
||||||
for line in rules:
|
for line in rules:
|
||||||
replaceStr = line.replace('\n', '').replace('\r', '')
|
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'
|
filename = acctDir(baseDir, nickname, domain) + '/autotags.txt'
|
||||||
if not os.path.isfile(filename):
|
if not os.path.isfile(filename):
|
||||||
return []
|
return []
|
||||||
with open(filename, 'r') as f:
|
try:
|
||||||
return f.readlines()
|
with open(filename, 'r') as f:
|
||||||
|
return f.readlines()
|
||||||
|
except OSError:
|
||||||
|
print('EX: unable to read auto tags ' + filename)
|
||||||
return []
|
return []
|
||||||
|
|
||||||
|
|
||||||
|
@ -853,12 +864,16 @@ def addHtmlTags(baseDir: str, httpPrefix: str,
|
||||||
petnames = None
|
petnames = None
|
||||||
if '@' in words:
|
if '@' in words:
|
||||||
if os.path.isfile(followingFilename):
|
if os.path.isfile(followingFilename):
|
||||||
with open(followingFilename, 'r') as f:
|
following = []
|
||||||
following = f.readlines()
|
try:
|
||||||
for handle in following:
|
with open(followingFilename, 'r') as f:
|
||||||
pet = getPetName(baseDir, nickname, domain, handle)
|
following = f.readlines()
|
||||||
if pet:
|
except OSError:
|
||||||
petnames.append(pet + '\n')
|
print('EX: unable to read ' + followingFilename)
|
||||||
|
for handle in following:
|
||||||
|
pet = getPetName(baseDir, nickname, domain, handle)
|
||||||
|
if pet:
|
||||||
|
petnames.append(pet + '\n')
|
||||||
|
|
||||||
# extract mentions and tags from words
|
# extract mentions and tags from words
|
||||||
longWordsList = []
|
longWordsList = []
|
||||||
|
|
31
daemon.py
31
daemon.py
|
@ -2355,13 +2355,16 @@ class PubServer(BaseHTTPRequestHandler):
|
||||||
else:
|
else:
|
||||||
if os.path.isdir(accountDir):
|
if os.path.isdir(accountDir):
|
||||||
nwFilename = newswireBlockedFilename
|
nwFilename = newswireBlockedFilename
|
||||||
|
nwWritten = False
|
||||||
try:
|
try:
|
||||||
with open(nwFilename, 'w+') as noNewswireFile:
|
with open(nwFilename, 'w+') as noNewswireFile:
|
||||||
noNewswireFile.write('\n')
|
noNewswireFile.write('\n')
|
||||||
refreshNewswire(self.server.baseDir)
|
nwWritten = True
|
||||||
except OSError as e:
|
except OSError as e:
|
||||||
print('EX: unable to write ' + nwFilename +
|
print('EX: unable to write ' + nwFilename +
|
||||||
' ' + str(e))
|
' ' + str(e))
|
||||||
|
if nwWritten:
|
||||||
|
refreshNewswire(self.server.baseDir)
|
||||||
usersPathStr = \
|
usersPathStr = \
|
||||||
usersPath + '/' + self.server.defaultTimeline + \
|
usersPath + '/' + self.server.defaultTimeline + \
|
||||||
'?page=' + str(pageNumber)
|
'?page=' + str(pageNumber)
|
||||||
|
@ -2398,13 +2401,16 @@ class PubServer(BaseHTTPRequestHandler):
|
||||||
else:
|
else:
|
||||||
if os.path.isdir(accountDir):
|
if os.path.isdir(accountDir):
|
||||||
featFilename = featuresBlockedFilename
|
featFilename = featuresBlockedFilename
|
||||||
|
featWritten = False
|
||||||
try:
|
try:
|
||||||
with open(featFilename, 'w+') as noFeaturesFile:
|
with open(featFilename, 'w+') as noFeaturesFile:
|
||||||
noFeaturesFile.write('\n')
|
noFeaturesFile.write('\n')
|
||||||
refreshNewswire(self.server.baseDir)
|
featWritten = True
|
||||||
except OSError as e:
|
except OSError as e:
|
||||||
print('EX: unable to write ' + featFilename +
|
print('EX: unable to write ' + featFilename +
|
||||||
' ' + str(e))
|
' ' + str(e))
|
||||||
|
if featWritten:
|
||||||
|
refreshNewswire(self.server.baseDir)
|
||||||
usersPathStr = \
|
usersPathStr = \
|
||||||
usersPath + '/' + self.server.defaultTimeline + \
|
usersPath + '/' + self.server.defaultTimeline + \
|
||||||
'?page=' + str(pageNumber)
|
'?page=' + str(pageNumber)
|
||||||
|
@ -3906,8 +3912,11 @@ class PubServer(BaseHTTPRequestHandler):
|
||||||
|
|
||||||
if fields.get('editedLinks'):
|
if fields.get('editedLinks'):
|
||||||
linksStr = fields['editedLinks']
|
linksStr = fields['editedLinks']
|
||||||
with open(linksFilename, 'w+') as linksFile:
|
try:
|
||||||
linksFile.write(linksStr)
|
with open(linksFilename, 'w+') as linksFile:
|
||||||
|
linksFile.write(linksStr)
|
||||||
|
except OSError:
|
||||||
|
print('EX: _linksUpdate unable to write ' + linksFilename)
|
||||||
else:
|
else:
|
||||||
if os.path.isfile(linksFilename):
|
if os.path.isfile(linksFilename):
|
||||||
try:
|
try:
|
||||||
|
@ -3923,8 +3932,11 @@ class PubServer(BaseHTTPRequestHandler):
|
||||||
aboutStr = fields['editedAbout']
|
aboutStr = fields['editedAbout']
|
||||||
if not dangerousMarkup(aboutStr,
|
if not dangerousMarkup(aboutStr,
|
||||||
allowLocalNetworkAccess):
|
allowLocalNetworkAccess):
|
||||||
with open(aboutFilename, 'w+') as aboutFile:
|
try:
|
||||||
aboutFile.write(aboutStr)
|
with open(aboutFilename, 'w+') as aboutFile:
|
||||||
|
aboutFile.write(aboutStr)
|
||||||
|
except OSError:
|
||||||
|
print('EX: unable to write about ' + aboutFilename)
|
||||||
else:
|
else:
|
||||||
if os.path.isfile(aboutFilename):
|
if os.path.isfile(aboutFilename):
|
||||||
try:
|
try:
|
||||||
|
@ -3937,8 +3949,11 @@ class PubServer(BaseHTTPRequestHandler):
|
||||||
TOSStr = fields['editedTOS']
|
TOSStr = fields['editedTOS']
|
||||||
if not dangerousMarkup(TOSStr,
|
if not dangerousMarkup(TOSStr,
|
||||||
allowLocalNetworkAccess):
|
allowLocalNetworkAccess):
|
||||||
with open(TOSFilename, 'w+') as TOSFile:
|
try:
|
||||||
TOSFile.write(TOSStr)
|
with open(TOSFilename, 'w+') as TOSFile:
|
||||||
|
TOSFile.write(TOSStr)
|
||||||
|
except OSError:
|
||||||
|
print('EX: unable to write TOS ' + TOSFilename)
|
||||||
else:
|
else:
|
||||||
if os.path.isfile(TOSFilename):
|
if os.path.isfile(TOSFilename):
|
||||||
try:
|
try:
|
||||||
|
|
Loading…
Reference in New Issue