Exception handling

merge-requests/30/head
Bob Mottram 2021-11-26 14:35:26 +00:00
parent cb0675cf5e
commit 8a623417bf
5 changed files with 514 additions and 253 deletions

451
daemon.py
View File

@ -4746,7 +4746,7 @@ class PubServer(BaseHTTPRequestHandler):
with open(cityFilename, 'w+') as fp:
fp.write(fields['cityDropdown'])
except OSError:
print('EX: unable to write ' + cityFilename)
print('EX: unable to write city ' + cityFilename)
# change displayed name
if fields.get('displayNickname'):
@ -5390,14 +5390,21 @@ class PubServer(BaseHTTPRequestHandler):
if ',' in fields['moderators']:
# if the list was given as comma separated
mods = fields['moderators'].split(',')
with open(moderatorsFile, 'w+') as modFile:
for modNick in mods:
modNick = modNick.strip()
modDir = baseDir + \
'/accounts/' + modNick + \
'@' + domain
if os.path.isdir(modDir):
modFile.write(modNick + '\n')
try:
with open(moderatorsFile,
'w+') as modFile:
for modNick in mods:
modNick = modNick.strip()
modDir = baseDir + \
'/accounts/' + modNick + \
'@' + domain
if os.path.isdir(modDir):
modFile.write(modNick +
'\n')
except OSError:
print('EX: ' +
'unable to write moderators ' +
moderatorsFile)
for modNick in mods:
modNick = modNick.strip()
@ -5411,15 +5418,22 @@ class PubServer(BaseHTTPRequestHandler):
else:
# nicknames on separate lines
mods = fields['moderators'].split('\n')
with open(moderatorsFile, 'w+') as modFile:
for modNick in mods:
modNick = modNick.strip()
modDir = \
baseDir + \
'/accounts/' + modNick + \
'@' + domain
if os.path.isdir(modDir):
modFile.write(modNick + '\n')
try:
with open(moderatorsFile,
'w+') as modFile:
for modNick in mods:
modNick = modNick.strip()
modDir = \
baseDir + \
'/accounts/' + modNick + \
'@' + domain
if os.path.isdir(modDir):
modFile.write(modNick +
'\n')
except OSError:
print('EX: ' +
'unable to write moderators 2 ' +
moderatorsFile)
for modNick in mods:
modNick = modNick.strip()
@ -5444,14 +5458,18 @@ class PubServer(BaseHTTPRequestHandler):
if ',' in fields['editors']:
# if the list was given as comma separated
eds = fields['editors'].split(',')
with open(editorsFile, 'w+') as edFile:
for edNick in eds:
edNick = edNick.strip()
edDir = baseDir + \
'/accounts/' + edNick + \
'@' + domain
if os.path.isdir(edDir):
edFile.write(edNick + '\n')
try:
with open(editorsFile, 'w+') as edFile:
for edNick in eds:
edNick = edNick.strip()
edDir = baseDir + \
'/accounts/' + edNick + \
'@' + domain
if os.path.isdir(edDir):
edFile.write(edNick + '\n')
except OSError as e:
print('EX: unable to write editors ' +
editorsFile + ' ' + str(e))
for edNick in eds:
edNick = edNick.strip()
@ -5465,15 +5483,20 @@ class PubServer(BaseHTTPRequestHandler):
else:
# nicknames on separate lines
eds = fields['editors'].split('\n')
with open(editorsFile, 'w+') as edFile:
for edNick in eds:
edNick = edNick.strip()
edDir = \
baseDir + \
'/accounts/' + edNick + \
'@' + domain
if os.path.isdir(edDir):
edFile.write(edNick + '\n')
try:
with open(editorsFile,
'w+') as edFile:
for edNick in eds:
edNick = edNick.strip()
edDir = \
baseDir + \
'/accounts/' + edNick + \
'@' + domain
if os.path.isdir(edDir):
edFile.write(edNick + '\n')
except OSError as e:
print('EX: unable to write editors ' +
editorsFile + ' ' + str(e))
for edNick in eds:
edNick = edNick.strip()
@ -5498,14 +5521,20 @@ class PubServer(BaseHTTPRequestHandler):
if ',' in fields['counselors']:
# if the list was given as comma separated
eds = fields['counselors'].split(',')
with open(counselorsFile, 'w+') as edFile:
for edNick in eds:
edNick = edNick.strip()
edDir = baseDir + \
'/accounts/' + edNick + \
'@' + domain
if os.path.isdir(edDir):
edFile.write(edNick + '\n')
try:
with open(counselorsFile,
'w+') as edFile:
for edNick in eds:
edNick = edNick.strip()
edDir = baseDir + \
'/accounts/' + edNick + \
'@' + domain
if os.path.isdir(edDir):
edFile.write(edNick + '\n')
except OSError as e:
print('EX: ' +
'unable to write counselors ' +
counselorsFile + ' ' + str(e))
for edNick in eds:
edNick = edNick.strip()
@ -5519,15 +5548,21 @@ class PubServer(BaseHTTPRequestHandler):
else:
# nicknames on separate lines
eds = fields['counselors'].split('\n')
with open(counselorsFile, 'w+') as edFile:
for edNick in eds:
edNick = edNick.strip()
edDir = \
baseDir + \
'/accounts/' + edNick + \
'@' + domain
if os.path.isdir(edDir):
edFile.write(edNick + '\n')
try:
with open(counselorsFile,
'w+') as edFile:
for edNick in eds:
edNick = edNick.strip()
edDir = \
baseDir + \
'/accounts/' + edNick + \
'@' + domain
if os.path.isdir(edDir):
edFile.write(edNick + '\n')
except OSError as e:
print('EX: ' +
'unable to write counselors ' +
counselorsFile + ' ' + str(e))
for edNick in eds:
edNick = edNick.strip()
@ -5552,14 +5587,18 @@ class PubServer(BaseHTTPRequestHandler):
if ',' in fields['artists']:
# if the list was given as comma separated
eds = fields['artists'].split(',')
with open(artistsFile, 'w+') as edFile:
for edNick in eds:
edNick = edNick.strip()
edDir = baseDir + \
'/accounts/' + edNick + \
'@' + domain
if os.path.isdir(edDir):
edFile.write(edNick + '\n')
try:
with open(artistsFile, 'w+') as edFile:
for edNick in eds:
edNick = edNick.strip()
edDir = baseDir + \
'/accounts/' + edNick + \
'@' + domain
if os.path.isdir(edDir):
edFile.write(edNick + '\n')
except OSError as e:
print('EX: unable to write artists ' +
artistsFile + ' ' + str(e))
for edNick in eds:
edNick = edNick.strip()
@ -5573,15 +5612,19 @@ class PubServer(BaseHTTPRequestHandler):
else:
# nicknames on separate lines
eds = fields['artists'].split('\n')
with open(artistsFile, 'w+') as edFile:
for edNick in eds:
edNick = edNick.strip()
edDir = \
baseDir + \
'/accounts/' + edNick + \
'@' + domain
if os.path.isdir(edDir):
edFile.write(edNick + '\n')
try:
with open(artistsFile, 'w+') as edFile:
for edNick in eds:
edNick = edNick.strip()
edDir = \
baseDir + \
'/accounts/' + edNick + \
'@' + domain
if os.path.isdir(edDir):
edFile.write(edNick + '\n')
except OSError as e:
print('EX: unable to write artists ' +
artistsFile + ' ' + str(e))
for edNick in eds:
edNick = edNick.strip()
@ -5681,16 +5724,25 @@ class PubServer(BaseHTTPRequestHandler):
if onFinalWelcomeScreen:
# initial default setting created via
# the welcome screen
with open(followDMsFilename, 'w+') as fFile:
fFile.write('\n')
try:
with open(followDMsFilename, 'w+') as fFile:
fFile.write('\n')
except OSError:
print('EX: unable to write follow DMs ' +
followDMsFilename)
actorChanged = True
else:
followDMsActive = False
if fields.get('followDMs'):
if fields['followDMs'] == 'on':
followDMsActive = True
with open(followDMsFilename, 'w+') as fFile:
fFile.write('\n')
try:
with open(followDMsFilename,
'w+') as fFile:
fFile.write('\n')
except OSError:
print('EX: unable to write follow DMs 2 ' +
followDMsFilename)
if not followDMsActive:
if os.path.isfile(followDMsFilename):
try:
@ -5708,9 +5760,13 @@ class PubServer(BaseHTTPRequestHandler):
if fields.get('removeTwitter'):
if fields['removeTwitter'] == 'on':
removeTwitterActive = True
with open(removeTwitterFilename,
'w+') as rFile:
rFile.write('\n')
try:
with open(removeTwitterFilename,
'w+') as rFile:
rFile.write('\n')
except OSError:
print('EX: unable to write remove twitter ' +
removeTwitterFilename)
if not removeTwitterActive:
if os.path.isfile(removeTwitterFilename):
try:
@ -5731,8 +5787,12 @@ class PubServer(BaseHTTPRequestHandler):
if fields.get('hideLikeButton'):
if fields['hideLikeButton'] == 'on':
hideLikeButtonActive = True
with open(hideLikeButtonFile, 'w+') as rFile:
rFile.write('\n')
try:
with open(hideLikeButtonFile, 'w+') as rFile:
rFile.write('\n')
except OSError:
print('EX: unable to write hide like ' +
hideLikeButtonFile)
# remove notify likes selection
if os.path.isfile(notifyLikesFilename):
try:
@ -5761,8 +5821,13 @@ class PubServer(BaseHTTPRequestHandler):
if fields.get('hideReactionButton'):
if fields['hideReactionButton'] == 'on':
hideReactionButtonActive = True
with open(hideReactionButtonFile, 'w+') as rFile:
rFile.write('\n')
try:
with open(hideReactionButtonFile,
'w+') as rFile:
rFile.write('\n')
except OSError:
print('EX: unable to write hide reaction ' +
hideReactionButtonFile)
# remove notify Reaction selection
if os.path.isfile(notifyReactionsFilename):
try:
@ -5783,8 +5848,12 @@ class PubServer(BaseHTTPRequestHandler):
# notify about new Likes
if onFinalWelcomeScreen:
# default setting from welcome screen
with open(notifyLikesFilename, 'w+') as rFile:
rFile.write('\n')
try:
with open(notifyLikesFilename, 'w+') as rFile:
rFile.write('\n')
except OSError:
print('EX: unable to write notify likes ' +
notifyLikesFilename)
actorChanged = True
else:
notifyLikesActive = False
@ -5792,8 +5861,13 @@ class PubServer(BaseHTTPRequestHandler):
if fields['notifyLikes'] == 'on' and \
not hideLikeButtonActive:
notifyLikesActive = True
with open(notifyLikesFilename, 'w+') as rFile:
rFile.write('\n')
try:
with open(notifyLikesFilename,
'w+') as rFile:
rFile.write('\n')
except OSError:
print('EX: unable to write notify likes ' +
notifyLikesFilename)
if not notifyLikesActive:
if os.path.isfile(notifyLikesFilename):
try:
@ -5808,8 +5882,12 @@ class PubServer(BaseHTTPRequestHandler):
'/.notifyReactions'
if onFinalWelcomeScreen:
# default setting from welcome screen
with open(notifyReactionsFilename, 'w+') as rFile:
rFile.write('\n')
try:
with open(notifyReactionsFilename, 'w+') as rFile:
rFile.write('\n')
except OSError:
print('EX: unable to write notify reactions ' +
notifyReactionsFilename)
actorChanged = True
else:
notifyReactionsActive = False
@ -5817,9 +5895,14 @@ class PubServer(BaseHTTPRequestHandler):
if fields['notifyReactions'] == 'on' and \
not hideReactionButtonActive:
notifyReactionsActive = True
with open(notifyReactionsFilename,
'w+') as rFile:
rFile.write('\n')
try:
with open(notifyReactionsFilename,
'w+') as rFile:
rFile.write('\n')
except OSError:
print('EX: unable to write ' +
'notify reactions ' +
notifyReactionsFilename)
if not notifyReactionsActive:
if os.path.isfile(notifyReactionsFilename):
try:
@ -5882,8 +5965,12 @@ class PubServer(BaseHTTPRequestHandler):
acctDir(baseDir, nickname, domain) + \
'/filters.txt'
if fields.get('filteredWords'):
with open(filterFilename, 'w+') as filterfile:
filterfile.write(fields['filteredWords'])
try:
with open(filterFilename, 'w+') as filterfile:
filterfile.write(fields['filteredWords'])
except OSError:
print('EX: unable to write filter ' +
filterFilename)
else:
if os.path.isfile(filterFilename):
try:
@ -5898,8 +5985,12 @@ class PubServer(BaseHTTPRequestHandler):
acctDir(baseDir, nickname, domain) + \
'/replacewords.txt'
if fields.get('switchWords'):
with open(switchFilename, 'w+') as switchfile:
switchfile.write(fields['switchWords'])
try:
with open(switchFilename, 'w+') as switchfile:
switchfile.write(fields['switchWords'])
except OSError:
print('EX: unable to write switches ' +
switchFilename)
else:
if os.path.isfile(switchFilename):
try:
@ -5914,8 +6005,12 @@ class PubServer(BaseHTTPRequestHandler):
acctDir(baseDir, nickname, domain) + \
'/autotags.txt'
if fields.get('autoTags'):
with open(autoTagsFilename, 'w+') as autoTagsFile:
autoTagsFile.write(fields['autoTags'])
try:
with open(autoTagsFilename, 'w+') as autoTagsFile:
autoTagsFile.write(fields['autoTags'])
except OSError:
print('EX: unable to write auto tags ' +
autoTagsFilename)
else:
if os.path.isfile(autoTagsFilename):
try:
@ -5930,8 +6025,12 @@ class PubServer(BaseHTTPRequestHandler):
acctDir(baseDir, nickname, domain) + \
'/autocw.txt'
if fields.get('autoCW'):
with open(autoCWFilename, 'w+') as autoCWFile:
autoCWFile.write(fields['autoCW'])
try:
with open(autoCWFilename, 'w+') as autoCWFile:
autoCWFile.write(fields['autoCW'])
except OSError:
print('EX: unable to write auto CW ' +
autoCWFilename)
else:
if os.path.isfile(autoCWFilename):
try:
@ -5946,8 +6045,12 @@ class PubServer(BaseHTTPRequestHandler):
acctDir(baseDir, nickname, domain) + \
'/blocking.txt'
if fields.get('blocked'):
with open(blockedFilename, 'w+') as blockedfile:
blockedfile.write(fields['blocked'])
try:
with open(blockedFilename, 'w+') as blockedfile:
blockedfile.write(fields['blocked'])
except OSError:
print('EX: unable to write blocked accounts ' +
blockedFilename)
else:
if os.path.isfile(blockedFilename):
try:
@ -5964,8 +6067,13 @@ class PubServer(BaseHTTPRequestHandler):
acctDir(baseDir, nickname, domain) + \
'/dmAllowedinstances.txt'
if fields.get('dmAllowedInstances'):
with open(dmAllowedInstancesFilename, 'w+') as aFile:
aFile.write(fields['dmAllowedInstances'])
try:
with open(dmAllowedInstancesFilename,
'w+') as aFile:
aFile.write(fields['dmAllowedInstances'])
except OSError:
print('EX: unable to write allowed DM instances ' +
dmAllowedInstancesFilename)
else:
if os.path.isfile(dmAllowedInstancesFilename):
try:
@ -5981,8 +6089,12 @@ class PubServer(BaseHTTPRequestHandler):
acctDir(baseDir, nickname, domain) + \
'/allowedinstances.txt'
if fields.get('allowedInstances'):
with open(allowedInstancesFilename, 'w+') as aFile:
aFile.write(fields['allowedInstances'])
try:
with open(allowedInstancesFilename, 'w+') as aFile:
aFile.write(fields['allowedInstances'])
except OSError:
print('EX: unable to write allowed instances ' +
allowedInstancesFilename)
else:
if os.path.isfile(allowedInstancesFilename):
try:
@ -6036,8 +6148,13 @@ class PubServer(BaseHTTPRequestHandler):
baseDir + '/accounts/peertube.txt'
if fields.get('ptInstances'):
self.server.peertubeInstances.clear()
with open(peertubeInstancesFile, 'w+') as aFile:
aFile.write(fields['ptInstances'])
try:
with open(peertubeInstancesFile,
'w+') as aFile:
aFile.write(fields['ptInstances'])
except OSError:
print('EX: unable to write peertube ' +
peertubeInstancesFile)
ptInstancesList = \
fields['ptInstances'].split('\n')
if ptInstancesList:
@ -6063,8 +6180,12 @@ class PubServer(BaseHTTPRequestHandler):
acctDir(baseDir, nickname, domain) + \
'/gitprojects.txt'
if fields.get('gitProjects'):
with open(gitProjectsFilename, 'w+') as aFile:
aFile.write(fields['gitProjects'].lower())
try:
with open(gitProjectsFilename, 'w+') as aFile:
aFile.write(fields['gitProjects'].lower())
except OSError:
print('EX: unable to write git ' +
gitProjectsFilename)
else:
if os.path.isfile(gitProjectsFilename):
try:
@ -6314,8 +6435,13 @@ class PubServer(BaseHTTPRequestHandler):
return
else:
if os.path.isfile(faviconFilename):
with open(faviconFilename, 'rb') as favFile:
favBinary = favFile.read()
favBinary = None
try:
with open(faviconFilename, 'rb') as favFile:
favBinary = favFile.read()
except OSError:
print('EX: unable to read favicon ' + faviconFilename)
if favBinary:
self._set_headers_etag(faviconFilename,
favType,
favBinary, None,
@ -6360,8 +6486,13 @@ class PubServer(BaseHTTPRequestHandler):
filename = path.split('/exports/', 1)[1]
filename = baseDir + '/exports/' + filename
if os.path.isfile(filename):
with open(filename, 'rb') as fp:
exportBinary = fp.read()
exportBinary = None
try:
with open(filename, 'rb') as fp:
exportBinary = fp.read()
except OSError:
print('EX: unable to read theme export ' + filename)
if exportBinary:
exportType = 'application/zip'
self._set_headers_etag(filename, exportType,
exportBinary, None,
@ -6409,8 +6540,13 @@ class PubServer(BaseHTTPRequestHandler):
return
else:
if os.path.isfile(fontFilename):
with open(fontFilename, 'rb') as fontFile:
fontBinary = fontFile.read()
fontBinary = None
try:
with open(fontFilename, 'rb') as fontFile:
fontBinary = fontFile.read()
except OSError:
print('EX: unable to load font ' + fontFilename)
if fontBinary:
self._set_headers_etag(fontFilename,
fontType,
fontBinary, None,
@ -6833,8 +6969,13 @@ class PubServer(BaseHTTPRequestHandler):
lastModifiedTimeStr = \
lastModifiedTime.strftime('%a, %d %b %Y %H:%M:%S GMT')
with open(mediaFilename, 'rb') as avFile:
mediaBinary = avFile.read()
mediaBinary = None
try:
with open(mediaFilename, 'rb') as avFile:
mediaBinary = avFile.read()
except OSError:
print('EX: unable to read media binary ' + mediaFilename)
if mediaBinary:
self._set_headers_etag(mediaFilename, mediaFileType,
mediaBinary, None,
None, True,
@ -6866,8 +7007,11 @@ class PubServer(BaseHTTPRequestHandler):
ontologyFileType = 'application/ld+json'
if os.path.isfile(ontologyFilename):
ontologyFile = None
with open(ontologyFilename, 'r') as fp:
ontologyFile = fp.read()
try:
with open(ontologyFilename, 'r') as fp:
ontologyFile = fp.read()
except OSError:
print('EX: unable to read ontology ' + ontologyFilename)
if ontologyFile:
ontologyFile = \
ontologyFile.replace('static.datafoodconsortium.org',
@ -6905,8 +7049,13 @@ class PubServer(BaseHTTPRequestHandler):
return
mediaImageType = getImageMimeType(emojiFilename)
with open(emojiFilename, 'rb') as avFile:
mediaBinary = avFile.read()
mediaBinary = None
try:
with open(emojiFilename, 'rb') as avFile:
mediaBinary = avFile.read()
except OSError:
print('EX: unable to read emoji image ' + emojiFilename)
if mediaBinary:
self._set_headers_etag(emojiFilename,
mediaImageType,
mediaBinary, None,
@ -6953,8 +7102,13 @@ class PubServer(BaseHTTPRequestHandler):
return
else:
if os.path.isfile(mediaFilename):
with open(mediaFilename, 'rb') as avFile:
mediaBinary = avFile.read()
mediaBinary = None
try:
with open(mediaFilename, 'rb') as avFile:
mediaBinary = avFile.read()
except OSError:
print('EX: unable to read icon image ' + mediaFilename)
if mediaBinary:
mimeType = mediaFileMimeType(mediaFilename)
self._set_headers_etag(mediaFilename,
mimeType,
@ -6995,8 +7149,13 @@ class PubServer(BaseHTTPRequestHandler):
self._304()
return
if os.path.isfile(mediaFilename):
with open(mediaFilename, 'rb') as avFile:
mediaBinary = avFile.read()
mediaBinary = None
try:
with open(mediaFilename, 'rb') as avFile:
mediaBinary = avFile.read()
except OSError:
print('EX: unable to read help image ' + mediaFilename)
if mediaBinary:
mimeType = mediaFileMimeType(mediaFilename)
self._set_headers_etag(mediaFilename,
mimeType,
@ -7020,8 +7179,13 @@ class PubServer(BaseHTTPRequestHandler):
# The file has not changed
self._304()
return
with open(mediaFilename, 'rb') as avFile:
mediaBinary = avFile.read()
mediaBinary = None
try:
with open(mediaFilename, 'rb') as avFile:
mediaBinary = avFile.read()
except OSError:
print('EX: unable to read cached avatar ' + mediaFilename)
if mediaBinary:
mimeType = mediaFileMimeType(mediaFilename)
self._set_headers_etag(mediaFilename,
mimeType,
@ -12419,8 +12583,13 @@ class PubServer(BaseHTTPRequestHandler):
return True
mediaFileType = getImageMimeType(mediaFilename)
with open(mediaFilename, 'rb') as avFile:
mediaBinary = avFile.read()
mediaBinary = None
try:
with open(mediaFilename, 'rb') as avFile:
mediaBinary = avFile.read()
except OSError:
print('EX: unable to read binary ' + mediaFilename)
if mediaBinary:
self._set_headers_etag(mediaFilename,
mediaFileType,
mediaBinary, None,
@ -12489,8 +12658,13 @@ class PubServer(BaseHTTPRequestHandler):
lastModifiedTime.strftime('%a, %d %b %Y %H:%M:%S GMT')
mediaImageType = getImageMimeType(avatarFile)
with open(avatarFilename, 'rb') as avFile:
mediaBinary = avFile.read()
mediaBinary = None
try:
with open(avatarFilename, 'rb') as avFile:
mediaBinary = avFile.read()
except OSError:
print('EX: unable to read avatar ' + avatarFilename)
if mediaBinary:
self._set_headers_etag(avatarFilename, mediaImageType,
mediaBinary, None,
refererDomain, True,
@ -15900,8 +16074,13 @@ class PubServer(BaseHTTPRequestHandler):
# check that the file exists
filename = self.server.baseDir + self.path
if os.path.isfile(filename):
with open(filename, 'r', encoding='utf-8') as File:
content = File.read()
content = None
try:
with open(filename, 'r', encoding='utf-8') as File:
content = File.read()
except OSError:
print('EX: unable to read file ' + filename)
if content:
contentJson = json.loads(content)
msg = json.dumps(contentJson,
ensure_ascii=False).encode('utf-8')
@ -15964,8 +16143,14 @@ class PubServer(BaseHTTPRequestHandler):
print('EX: do_HEAD unable to read ' +
mediaTagFilename)
else:
with open(mediaFilename, 'rb') as avFile:
mediaBinary = avFile.read()
mediaBinary = None
try:
with open(mediaFilename, 'rb') as avFile:
mediaBinary = avFile.read()
except OSError:
print('EX: unable to read media binary ' +
mediaFilename)
if mediaBinary:
etag = md5(mediaBinary).hexdigest() # nosec
try:
with open(mediaTagFilename, 'w+') as etagFile:

View File

@ -18,8 +18,11 @@ def addFilter(baseDir: str, nickname: str, domain: str, words: str) -> bool:
if os.path.isfile(filtersFilename):
if words in open(filtersFilename).read():
return False
with open(filtersFilename, 'a+') as filtersFile:
filtersFile.write(words + '\n')
try:
with open(filtersFilename, 'a+') as filtersFile:
filtersFile.write(words + '\n')
except OSError:
print('EX: unable to append filters ' + filtersFilename)
return True
@ -35,8 +38,11 @@ def addGlobalFilter(baseDir: str, words: str) -> bool:
if os.path.isfile(filtersFilename):
if words in open(filtersFilename).read():
return False
with open(filtersFilename, 'a+') as filtersFile:
filtersFile.write(words + '\n')
try:
with open(filtersFilename, 'a+') as filtersFile:
filtersFile.write(words + '\n')
except OSError:
print('EX: unable to append filters ' + filtersFilename)
return True
@ -50,12 +56,15 @@ def removeFilter(baseDir: str, nickname: str, domain: str,
if words not in open(filtersFilename).read():
return False
newFiltersFilename = filtersFilename + '.new'
with open(filtersFilename, 'r') as fp:
with open(newFiltersFilename, 'w+') as fpnew:
for line in fp:
line = line.replace('\n', '')
if line != words:
fpnew.write(line + '\n')
try:
with open(filtersFilename, 'r') as fp:
with open(newFiltersFilename, 'w+') as fpnew:
for line in fp:
line = line.replace('\n', '')
if line != words:
fpnew.write(line + '\n')
except OSError as e:
print('EX: unable to remove filter ' + filtersFilename + ' ' + str(e))
if os.path.isfile(newFiltersFilename):
os.rename(newFiltersFilename, filtersFilename)
return True
@ -71,12 +80,16 @@ def removeGlobalFilter(baseDir: str, words: str) -> bool:
if words not in open(filtersFilename).read():
return False
newFiltersFilename = filtersFilename + '.new'
with open(filtersFilename, 'r') as fp:
with open(newFiltersFilename, 'w+') as fpnew:
for line in fp:
line = line.replace('\n', '')
if line != words:
fpnew.write(line + '\n')
try:
with open(filtersFilename, 'r') as fp:
with open(newFiltersFilename, 'w+') as fpnew:
for line in fp:
line = line.replace('\n', '')
if line != words:
fpnew.write(line + '\n')
except OSError as e:
print('EX: unable to remove global filter ' +
filtersFilename + ' ' + str(e))
if os.path.isfile(newFiltersFilename):
os.rename(newFiltersFilename, filtersFilename)
return True
@ -100,22 +113,25 @@ def _isFilteredBase(filename: str, content: str) -> bool:
if not os.path.isfile(filename):
return False
with open(filename, 'r') as fp:
for line in fp:
filterStr = line.replace('\n', '').replace('\r', '')
if not filterStr:
continue
if len(filterStr) < 2:
continue
if '+' not in filterStr:
if filterStr in content:
try:
with open(filename, 'r') as fp:
for line in fp:
filterStr = line.replace('\n', '').replace('\r', '')
if not filterStr:
continue
if len(filterStr) < 2:
continue
if '+' not in filterStr:
if filterStr in content:
return True
else:
filterWords = filterStr.replace('"', '').split('+')
for word in filterWords:
if word not in content:
return False
return True
else:
filterWords = filterStr.replace('"', '').split('+')
for word in filterWords:
if word not in content:
return False
return True
except OSError as e:
print('EX: _isFilteredBase ' + filename + ' ' + str(e))
return False

207
follow.py
View File

@ -58,24 +58,32 @@ def createInitialLastSeen(baseDir: str, httpPrefix: str) -> None:
lastSeenDir = accountDir + '/lastseen'
if not os.path.isdir(lastSeenDir):
os.mkdir(lastSeenDir)
with open(followingFilename, 'r') as fp:
followingHandles = fp.readlines()
for handle in followingHandles:
if '#' in handle:
continue
if '@' not in handle:
continue
handle = handle.replace('\n', '')
nickname = handle.split('@')[0]
domain = handle.split('@')[1]
if nickname.startswith('!'):
nickname = nickname[1:]
actor = localActorUrl(httpPrefix, nickname, domain)
lastSeenFilename = \
lastSeenDir + '/' + actor.replace('/', '#') + '.txt'
if not os.path.isfile(lastSeenFilename):
followingHandles = []
try:
with open(followingFilename, 'r') as fp:
followingHandles = fp.readlines()
except OSError:
print('EX: createInitialLastSeen ' + followingFilename)
for handle in followingHandles:
if '#' in handle:
continue
if '@' not in handle:
continue
handle = handle.replace('\n', '')
nickname = handle.split('@')[0]
domain = handle.split('@')[1]
if nickname.startswith('!'):
nickname = nickname[1:]
actor = localActorUrl(httpPrefix, nickname, domain)
lastSeenFilename = \
lastSeenDir + '/' + actor.replace('/', '#') + '.txt'
if not os.path.isfile(lastSeenFilename):
try:
with open(lastSeenFilename, 'w+') as fp:
fp.write(str(100))
except OSError:
print('EX: createInitialLastSeen 2 ' +
lastSeenFilename)
break
@ -124,16 +132,20 @@ def _removeFromFollowBase(baseDir: str,
break
if not actorFound:
return
with open(approveFollowsFilename + '.new', 'w+') as approvefilenew:
with open(approveFollowsFilename, 'r') as approvefile:
if not acceptDenyActor:
for approveHandle in approvefile:
if not approveHandle.startswith(acceptOrDenyHandle):
approvefilenew.write(approveHandle)
else:
for approveHandle in approvefile:
if acceptDenyActor not in approveHandle:
approvefilenew.write(approveHandle)
try:
with open(approveFollowsFilename + '.new', 'w+') as approvefilenew:
with open(approveFollowsFilename, 'r') as approvefile:
if not acceptDenyActor:
for approveHandle in approvefile:
if not approveHandle.startswith(acceptOrDenyHandle):
approvefilenew.write(approveHandle)
else:
for approveHandle in approvefile:
if acceptDenyActor not in approveHandle:
approvefilenew.write(approveHandle)
except OSError as e:
print('EX: _removeFromFollowBase ' +
approveFollowsFilename + ' ' + str(e))
os.rename(approveFollowsFilename + '.new', approveFollowsFilename)
@ -218,8 +230,11 @@ def getFollowerDomains(baseDir: str, nickname: str, domain: str) -> []:
return []
lines = []
with open(followersFile, 'r') as fpFollowers:
lines = fpFollowers.readlines()
try:
with open(followersFile, 'r') as fpFollowers:
lines = fpFollowers.readlines()
except OSError:
print('EX: getFollowerDomains ' + followersFile)
domainsList = []
for handle in lines:
@ -251,8 +266,11 @@ def isFollowerOfPerson(baseDir: str, nickname: str, domain: str,
alreadyFollowing = False
followersStr = ''
with open(followersFile, 'r') as fpFollowers:
followersStr = fpFollowers.read()
try:
with open(followersFile, 'r') as fpFollowers:
followersStr = fpFollowers.read()
except OSError:
print('EX: isFollowerOfPerson ' + followersFile)
if handle in followersStr:
alreadyFollowing = True
@ -294,8 +312,13 @@ def unfollowAccount(baseDir: str, nickname: str, domain: str,
print('DEBUG: handle to unfollow ' + handleToUnfollow +
' is not in ' + filename)
return
with open(filename, 'r') as f:
lines = f.readlines()
lines = []
try:
with open(filename, 'r') as f:
lines = f.readlines()
except OSError:
print('EX: unfollowAccount ' + filename)
if lines:
try:
with open(filename, 'w+') as f:
for line in lines:
@ -312,8 +335,11 @@ def unfollowAccount(baseDir: str, nickname: str, domain: str,
if os.path.isfile(unfollowedFilename):
if handleToUnfollowLower not in \
open(unfollowedFilename).read().lower():
with open(unfollowedFilename, 'a+') as f:
f.write(handleToUnfollow + '\n')
try:
with open(unfollowedFilename, 'a+') as f:
f.write(handleToUnfollow + '\n')
except OSError:
print('EX: unable to append ' + unfollowedFilename)
else:
try:
with open(unfollowedFilename, 'w+') as f:
@ -371,8 +397,13 @@ def _getNoOfFollows(baseDir: str, nickname: str, domain: str,
if not os.path.isfile(filename):
return 0
ctr = 0
with open(filename, 'r') as f:
lines = f.readlines()
lines = []
try:
with open(filename, 'r') as f:
lines = f.readlines()
except OSError:
print('EX: _getNoOfFollows ' + filename)
if lines:
for line in lines:
if '#' in line:
continue
@ -483,39 +514,43 @@ def getFollowingFeed(baseDir: str, domain: str, port: int, path: str,
currPage = 1
pageCtr = 0
totalCtr = 0
with open(filename, 'r') as f:
lines = f.readlines()
for line in lines:
if '#' not in line:
if '@' in line and not line.startswith('http'):
# nickname@domain
pageCtr += 1
totalCtr += 1
if currPage == pageNumber:
line2 = \
line.lower().replace('\n', '').replace('\r', '')
nick = line2.split('@')[0]
dom = line2.split('@')[1]
if not nick.startswith('!'):
# person actor
url = localActorUrl(httpPrefix, nick, dom)
else:
# group actor
url = httpPrefix + '://' + dom + '/c/' + nick
following['orderedItems'].append(url)
elif ((line.startswith('http') or
line.startswith('hyper')) and
hasUsersPath(line)):
# https://domain/users/nickname
pageCtr += 1
totalCtr += 1
if currPage == pageNumber:
appendStr = \
line.lower().replace('\n', '').replace('\r', '')
following['orderedItems'].append(appendStr)
if pageCtr >= followsPerPage:
pageCtr = 0
currPage += 1
lines = []
try:
with open(filename, 'r') as f:
lines = f.readlines()
except OSError:
print('EX: getFollowingFeed ' + filename)
for line in lines:
if '#' not in line:
if '@' in line and not line.startswith('http'):
# nickname@domain
pageCtr += 1
totalCtr += 1
if currPage == pageNumber:
line2 = \
line.lower().replace('\n', '').replace('\r', '')
nick = line2.split('@')[0]
dom = line2.split('@')[1]
if not nick.startswith('!'):
# person actor
url = localActorUrl(httpPrefix, nick, dom)
else:
# group actor
url = httpPrefix + '://' + dom + '/c/' + nick
following['orderedItems'].append(url)
elif ((line.startswith('http') or
line.startswith('hyper')) and
hasUsersPath(line)):
# https://domain/users/nickname
pageCtr += 1
totalCtr += 1
if currPage == pageNumber:
appendStr = \
line.lower().replace('\n', '').replace('\r', '')
following['orderedItems'].append(appendStr)
if pageCtr >= followsPerPage:
pageCtr = 0
currPage += 1
following['totalItems'] = totalCtr
lastPage = int(totalCtr / followsPerPage)
if lastPage < 1:
@ -568,8 +603,13 @@ def _noOfFollowRequests(baseDir: str,
if not os.path.isfile(approveFollowsFilename):
return 0
ctr = 0
with open(approveFollowsFilename, 'r') as f:
lines = f.readlines()
lines = []
try:
with open(approveFollowsFilename, 'r') as f:
lines = f.readlines()
except OSError:
print('EX: _noOfFollowRequests ' + approveFollowsFilename)
if lines:
if followType == "onion":
for fileLine in lines:
if '.onion' in fileLine:
@ -607,8 +647,11 @@ def _storeFollowRequest(baseDir: str,
alreadyFollowing = False
followersStr = ''
with open(followersFilename, 'r') as fpFollowers:
followersStr = fpFollowers.read()
try:
with open(followersFilename, 'r') as fpFollowers:
followersStr = fpFollowers.read()
except OSError:
print('EX: _storeFollowRequest ' + followersFilename)
if approveHandle in followersStr:
alreadyFollowing = True
@ -649,8 +692,11 @@ def _storeFollowRequest(baseDir: str,
if os.path.isfile(approveFollowsFilename):
if approveHandle not in open(approveFollowsFilename).read():
with open(approveFollowsFilename, 'a+') as fp:
fp.write(approveHandleStored + '\n')
try:
with open(approveFollowsFilename, 'a+') as fp:
fp.write(approveHandleStored + '\n')
except OSError:
print('EX: _storeFollowRequest 2 ' + approveFollowsFilename)
else:
if debug:
print('DEBUG: ' + approveHandleStored +
@ -660,7 +706,7 @@ def _storeFollowRequest(baseDir: str,
with open(approveFollowsFilename, 'w+') as fp:
fp.write(approveHandleStored + '\n')
except OSError:
print('EX: unable to write ' + approveFollowsFilename)
print('EX: _storeFollowRequest 3 ' + approveFollowsFilename)
# store the follow request in its own directory
# We don't rely upon the inbox because items in there could expire
@ -1053,11 +1099,14 @@ def sendFollowRequest(session, baseDir: str,
if os.path.isfile(unfollowedFilename):
if followHandle in open(unfollowedFilename).read():
unfollowedFile = None
with open(unfollowedFilename, 'r') as fp:
unfollowedFile = fp.read()
try:
with open(unfollowedFilename, 'r') as fp:
unfollowedFile = fp.read()
except OSError:
print('EX: sendFollowRequest ' + unfollowedFilename)
if unfollowedFile:
unfollowedFile = \
unfollowedFile.replace(followHandle + '\n', '')
if unfollowedFile:
try:
with open(unfollowedFilename, 'w+') as fp:
fp.write(unfollowedFile)

View File

@ -44,13 +44,18 @@ def receivingCalendarEvents(baseDir: str, nickname: str, domain: str,
if not os.path.isfile(followingFilename):
return False
# create a new calendar file from the following file
with open(followingFilename, 'r') as followingFile:
followingHandles = followingFile.read()
followingHandles = None
try:
with open(followingFilename, 'r') as followingFile:
followingHandles = followingFile.read()
except OSError:
print('EX: receivingCalendarEvents ' + followingFilename)
if followingHandles:
try:
with open(calendarFilename, 'w+') as fp:
fp.write(followingHandles)
except OSError:
print('EX: unable to write ' + calendarFilename)
print('EX: receivingCalendarEvents 2 ' + calendarFilename)
return handle + '\n' in open(calendarFilename).read()
@ -83,14 +88,20 @@ def _receiveCalendarEvents(baseDir: str, nickname: str, domain: str,
followingHandles = ''
if os.path.isfile(calendarFilename):
print('Calendar file exists')
with open(calendarFilename, 'r') as calendarFile:
followingHandles = calendarFile.read()
try:
with open(calendarFilename, 'r') as calendarFile:
followingHandles = calendarFile.read()
except OSError:
print('EX: _receiveCalendarEvents ' + calendarFilename)
else:
# create a new calendar file from the following file
print('Creating calendar file ' + calendarFilename)
followingHandles = ''
with open(followingFilename, 'r') as followingFile:
followingHandles = followingFile.read()
try:
with open(followingFilename, 'r') as followingFile:
followingHandles = followingFile.read()
except OSError:
print('EX: _receiveCalendarEvents 2 ' + calendarFilename)
if add:
try:
with open(calendarFilename, 'w+') as fp:
@ -110,7 +121,7 @@ def _receiveCalendarEvents(baseDir: str, nickname: str, domain: str,
with open(calendarFilename, 'w+') as fp:
fp.write(followingHandles)
except OSError:
print('EX: unable to write ' + calendarFilename)
print('EX: _receiveCalendarEvents 3 ' + calendarFilename)
else:
print(handle + ' not in followingCalendar.txt')
# not already in the calendar file
@ -121,7 +132,7 @@ def _receiveCalendarEvents(baseDir: str, nickname: str, domain: str,
with open(calendarFilename, 'w+') as fp:
fp.write(followingHandles)
except OSError:
print('EX: unable to write ' + calendarFilename)
print('EX: _receiveCalendarEvents 4 ' + calendarFilename)
def addPersonToCalendar(baseDir: str, nickname: str, domain: str,

2
git.py
View File

@ -217,5 +217,5 @@ def receiveGitPatch(baseDir: str, nickname: str, domain: str,
patchFile.write(patchStr)
return True
except OSError as e:
print('EX: unable to write patch ' + patchFilename + ' ' + str(e))
print('EX: receiveGitPatch ' + patchFilename + ' ' + str(e))
return False