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

207
follow.py
View File

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

View File

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