diff --git a/auth.py b/auth.py index 0d861c59f..9db4ad19c 100644 --- a/auth.py +++ b/auth.py @@ -132,17 +132,21 @@ def authorizeBasic(baseDir: str, path: str, authHeader: str, print('DEBUG: passwords file missing') return False providedPassword = plain.split(':')[1] - with open(passwordFile, 'r') as passfile: - for line in passfile: - if not line.startswith(nickname + ':'): - continue - storedPassword = \ - line.split(':')[1].replace('\n', '').replace('\r', '') - success = _verifyPassword(storedPassword, providedPassword) - if not success: - if debug: - print('DEBUG: Password check failed for ' + nickname) - return success + try: + with open(passwordFile, 'r') as passfile: + for line in passfile: + if not line.startswith(nickname + ':'): + continue + storedPassword = \ + line.split(':')[1].replace('\n', '').replace('\r', '') + success = _verifyPassword(storedPassword, providedPassword) + if not success: + if debug: + print('DEBUG: Password check failed for ' + nickname) + return success + except OSError: + print('EX: failed to open password file') + return False print('DEBUG: Did not find credentials for ' + nickname + ' in ' + passwordFile) return False @@ -272,15 +276,15 @@ def recordLoginFailure(baseDir: str, ipAddress: str, if not os.path.isfile(failureLog): writeType = 'w+' currTime = datetime.datetime.utcnow() + currTimeStr = currTime.strftime("%Y-%m-%d %H:%M:%SZ") try: with open(failureLog, writeType) as fp: # here we use a similar format to an ssh log, so that # systems such as fail2ban can parse it - fp.write(currTime.strftime("%Y-%m-%d %H:%M:%SZ") + ' ' + + fp.write(currTimeStr + ' ' + 'ip-127-0-0-1 sshd[20710]: ' + 'Disconnecting invalid user epicyon ' + ipAddress + ' port 443: ' + 'Too many authentication failures [preauth]\n') except OSError: print('EX: recordLoginFailure failed ' + str(failureLog)) - pass diff --git a/blocking.py b/blocking.py index 44bf77b33..f4e821a5b 100644 --- a/blocking.py +++ b/blocking.py @@ -94,11 +94,13 @@ def addBlock(baseDir: str, nickname: str, domain: str, try: with open(followingFilename, 'r') as followingFile: followingStr = followingFile.read() - followingStr = followingStr.replace(blockHandle + '\n', '') except OSError: print('EX: Unable to read following ' + followingFilename) return False + if followingStr: + followingStr = followingStr.replace(blockHandle + '\n', '') + try: with open(followingFilename, 'w+') as followingFile: followingFile.write(followingStr) @@ -114,11 +116,13 @@ def addBlock(baseDir: str, nickname: str, domain: str, try: with open(followersFilename, 'r') as followersFile: followersStr = followersFile.read() - followersStr = followersStr.replace(blockHandle + '\n', '') except OSError: print('EX: Unable to read followers ' + followersFilename) return False + if followersStr: + followersStr = followersStr.replace(blockHandle + '\n', '') + try: with open(followersFilename, 'w+') as followersFile: followersFile.write(followersStr) @@ -145,39 +149,51 @@ def removeGlobalBlock(baseDir: str, unblockHandle = unblockNickname + '@' + unblockDomain if os.path.isfile(unblockingFilename): if unblockHandle in open(unblockingFilename).read(): - with open(unblockingFilename, 'r') as fp: - try: + try: + with open(unblockingFilename, 'r') as fp: with open(unblockingFilename + '.new', 'w+') as fpnew: for line in fp: handle = \ line.replace('\n', '').replace('\r', '') if unblockHandle not in line: fpnew.write(handle + '\n') - except OSError as e: - print('EX: failed to remove global block ' + - unblockingFilename + ' ' + str(e)) - return False + except OSError as e: + print('EX: failed to remove global block ' + + unblockingFilename + ' ' + str(e)) + return False + if os.path.isfile(unblockingFilename + '.new'): - os.rename(unblockingFilename + '.new', unblockingFilename) + try: + os.rename(unblockingFilename + '.new', + unblockingFilename) + except OSError: + print('EX: unable to rename ' + unblockingFilename) + return False return True else: unblockHashtag = unblockNickname if os.path.isfile(unblockingFilename): if unblockHashtag + '\n' in open(unblockingFilename).read(): - with open(unblockingFilename, 'r') as fp: - try: + try: + with open(unblockingFilename, 'r') as fp: with open(unblockingFilename + '.new', 'w+') as fpnew: for line in fp: blockLine = \ line.replace('\n', '').replace('\r', '') if unblockHashtag not in line: fpnew.write(blockLine + '\n') - except OSError as e: - print('EX: failed to remove global hashtag block ' + - unblockingFilename + ' ' + str(e)) - return False + except OSError as e: + print('EX: failed to remove global hashtag block ' + + unblockingFilename + ' ' + str(e)) + return False + if os.path.isfile(unblockingFilename + '.new'): - os.rename(unblockingFilename + '.new', unblockingFilename) + try: + os.rename(unblockingFilename + '.new', + unblockingFilename) + except OSError: + print('EX: unable to rename 2 ' + unblockingFilename) + return False return True return False @@ -191,19 +207,24 @@ def removeBlock(baseDir: str, nickname: str, domain: str, unblockHandle = unblockNickname + '@' + unblockDomain if os.path.isfile(unblockingFilename): if unblockHandle in open(unblockingFilename).read(): - with open(unblockingFilename, 'r') as fp: - try: + try: + with open(unblockingFilename, 'r') as fp: with open(unblockingFilename + '.new', 'w+') as fpnew: for line in fp: handle = line.replace('\n', '').replace('\r', '') if unblockHandle not in line: fpnew.write(handle + '\n') - except OSError as e: - print('EX: failed to remove block ' + - unblockingFilename + ' ' + str(e)) - return False + except OSError as e: + print('EX: failed to remove block ' + + unblockingFilename + ' ' + str(e)) + return False + if os.path.isfile(unblockingFilename + '.new'): - os.rename(unblockingFilename + '.new', unblockingFilename) + try: + os.rename(unblockingFilename + '.new', unblockingFilename) + except OSError: + print('EX: unable to rename 3 ' + unblockingFilename) + return False return True return False @@ -237,8 +258,11 @@ def getDomainBlocklist(baseDir: str) -> str: globalBlockingFilename = baseDir + '/accounts/blocking.txt' if not os.path.isfile(globalBlockingFilename): return blockedStr - with open(globalBlockingFilename, 'r') as fpBlocked: - blockedStr += fpBlocked.read() + try: + with open(globalBlockingFilename, 'r') as fpBlocked: + blockedStr += fpBlocked.read() + except OSError: + print('EX: unable to read ' + globalBlockingFilename) return blockedStr @@ -258,14 +282,17 @@ def updateBlockedCache(baseDir: str, globalBlockingFilename = baseDir + '/accounts/blocking.txt' if not os.path.isfile(globalBlockingFilename): return blockedCacheLastUpdated - with open(globalBlockingFilename, 'r') as fpBlocked: - blockedLines = fpBlocked.readlines() - # remove newlines - for index in range(len(blockedLines)): - blockedLines[index] = blockedLines[index].replace('\n', '') - # update the cache - blockedCache.clear() - blockedCache += blockedLines + try: + with open(globalBlockingFilename, 'r') as fpBlocked: + blockedLines = fpBlocked.readlines() + # remove newlines + for index in range(len(blockedLines)): + blockedLines[index] = blockedLines[index].replace('\n', '') + # update the cache + blockedCache.clear() + blockedCache += blockedLines + except OSError as e: + print('EX: unable to read ' + globalBlockingFilename + ' ' + str(e)) return currTime @@ -305,13 +332,17 @@ def isBlockedDomain(baseDir: str, domain: str, # instance block list globalBlockingFilename = baseDir + '/accounts/blocking.txt' if os.path.isfile(globalBlockingFilename): - with open(globalBlockingFilename, 'r') as fpBlocked: - blockedStr = fpBlocked.read() - if '*@' + domain in blockedStr: - return True - if shortDomain: - if '*@' + shortDomain in blockedStr: + try: + with open(globalBlockingFilename, 'r') as fpBlocked: + blockedStr = fpBlocked.read() + if '*@' + domain in blockedStr: return True + if shortDomain: + if '*@' + shortDomain in blockedStr: + return True + except OSError as e: + print('EX: unable to read ' + globalBlockingFilename + + ' ' + str(e)) else: allowFilename = baseDir + '/accounts/allowedinstances.txt' # instance allow list @@ -572,10 +603,10 @@ def mutePost(baseDir: str, nickname: str, domain: str, port: int, try: with open(postFilename + '.muted', 'w+') as muteFile: muteFile.write('\n') - print('MUTE: ' + postFilename + '.muted file added') except OSError: print('EX: Failed to save mute file ' + postFilename + '.muted') return + print('MUTE: ' + postFilename + '.muted file added') # if the post is in the recent posts cache then mark it as muted if recentPostsCache.get('index'): @@ -886,15 +917,19 @@ def setBrochMode(baseDir: str, domainFull: str, enabled: bool) -> None: followingFilename = accountDir + '/' + followFileType if not os.path.isfile(followingFilename): continue - with open(followingFilename, 'r') as f: - followList = f.readlines() - for handle in followList: - if '@' not in handle: - continue - handle = handle.replace('\n', '') - handleDomain = handle.split('@')[1] - if handleDomain not in allowedDomains: - allowedDomains.append(handleDomain) + try: + with open(followingFilename, 'r') as f: + followList = f.readlines() + for handle in followList: + if '@' not in handle: + continue + handle = handle.replace('\n', '') + handleDomain = handle.split('@')[1] + if handleDomain not in allowedDomains: + allowedDomains.append(handleDomain) + except OSError as e: + print('EX: failed to read ' + followingFilename + + ' ' + str(e)) break # write the allow file diff --git a/blog.py b/blog.py index de240103c..7566e49e7 100644 --- a/blog.py +++ b/blog.py @@ -72,20 +72,24 @@ def _noOfBlogReplies(baseDir: str, httpPrefix: str, translate: {}, removals = [] replies = 0 lines = [] - with open(postFilename, 'r') as f: - lines = f.readlines() - for replyPostId in lines: - replyPostId = replyPostId.replace('\n', '').replace('\r', '') - replyPostId = replyPostId.replace('.json', '') - if locatePost(baseDir, nickname, domain, replyPostId): - replyPostId = replyPostId.replace('.replies', '') - replies += \ - 1 + _noOfBlogReplies(baseDir, httpPrefix, translate, - nickname, domain, domainFull, - replyPostId, depth+1) - else: - # remove post which no longer exists - removals.append(replyPostId) + try: + with open(postFilename, 'r') as f: + lines = f.readlines() + except OSError: + print('EX: failed to read blog ' + postFilename) + + for replyPostId in lines: + replyPostId = replyPostId.replace('\n', '').replace('\r', '') + replyPostId = replyPostId.replace('.json', '') + if locatePost(baseDir, nickname, domain, replyPostId): + replyPostId = replyPostId.replace('.replies', '') + replies += \ + 1 + _noOfBlogReplies(baseDir, httpPrefix, translate, + 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 if lines and removals: @@ -135,12 +139,21 @@ def _getBlogReplies(baseDir: str, httpPrefix: str, translate: {}, '/postcache/' + \ postId.replace('/', '#') + '.html' if os.path.isfile(postFilename): - with open(postFilename, 'r') as postFile: - return postFile.read() + '\n' + try: + with open(postFilename, 'r') as postFile: + return postFile.read() + '\n' + except OSError: + print('EX: unable to read blog 3 ' + postFilename) return '' - with open(postFilename, 'r') as f: - lines = f.readlines() + lines = [] + try: + with open(postFilename, 'r') as f: + lines = f.readlines() + except OSError: + print('EX: unable to read blog 4 ' + postFilename) + + if lines: repliesStr = '' for replyPostId in lines: replyPostId = replyPostId.replace('\n', '').replace('\r', '') @@ -151,8 +164,11 @@ def _getBlogReplies(baseDir: str, httpPrefix: str, translate: {}, replyPostId.replace('/', '#') + '.html' if not os.path.isfile(postFilename): continue - with open(postFilename, 'r') as postFile: - repliesStr += postFile.read() + '\n' + try: + with open(postFilename, 'r') as postFile: + repliesStr += postFile.read() + '\n' + except OSError: + print('EX: unable to read blog replies ' + postFilename) rply = _getBlogReplies(baseDir, httpPrefix, translate, nickname, domain, domainFull, replyPostId, depth+1) @@ -770,8 +786,11 @@ def htmlEditBlog(mediaInstance: bool, translate: {}, editBlogText = '' + translate['Write your post text below.'] + '' if os.path.isfile(baseDir + '/accounts/newpost.txt'): - with open(baseDir + '/accounts/newpost.txt', 'r') as file: - editBlogText = '

' + file.read() + '

' + try: + with open(baseDir + '/accounts/newpost.txt', 'r') as file: + editBlogText = '

' + file.read() + '

' + except OSError: + print('EX: unable to read ' + baseDir + '/accounts/newpost.txt') cssFilename = baseDir + '/epicyon-profile.css' if os.path.isfile(baseDir + '/epicyon.css'): diff --git a/bookmarks.py b/bookmarks.py index 197e8aaeb..8dcb18d95 100644 --- a/bookmarks.py +++ b/bookmarks.py @@ -71,8 +71,12 @@ def undoBookmarksCollectionEntry(recentPostsCache: {}, if bookmarkIndex not in open(bookmarksIndexFilename).read(): return indexStr = '' - with open(bookmarksIndexFilename, 'r') as indexFile: - indexStr = indexFile.read().replace(bookmarkIndex + '\n', '') + try: + with open(bookmarksIndexFilename, 'r') as indexFile: + indexStr = indexFile.read().replace(bookmarkIndex + '\n', '') + except OSError: + print('EX: unable to read ' + bookmarksIndexFilename) + if indexStr: try: with open(bookmarksIndexFilename, 'w+') as bookmarksIndexFile: bookmarksIndexFile.write(indexStr) diff --git a/categories.py b/categories.py index 25db73113..e9dca2153 100644 --- a/categories.py +++ b/categories.py @@ -23,10 +23,14 @@ def getHashtagCategory(baseDir: str, hashtag: str) -> str: if not os.path.isfile(categoryFilename): return '' - with open(categoryFilename, 'r') as fp: - categoryStr = fp.read() - if categoryStr: - return categoryStr + categoryStr = None + try: + with open(categoryFilename, 'r') as fp: + categoryStr = fp.read() + except OSError: + print('EX: unable to read category ' + categoryFilename) + if categoryStr: + return categoryStr return '' @@ -161,15 +165,21 @@ def setHashtagCategory(baseDir: str, hashtag: str, category: str, # don't overwrite any existing categories if os.path.isfile(categoryFilename): return False + + categoryWritten = False try: with open(categoryFilename, 'w+') as fp: fp.write(category) - if update: - updateHashtagCategories(baseDir) - return True + categoryWritten = True except OSError as e: print('EX: unable to write category ' + categoryFilename + ' ' + str(e)) + + if categoryWritten: + if update: + updateHashtagCategories(baseDir) + return True + return False diff --git a/city.py b/city.py index b486c2de0..3f8c0f3af 100644 --- a/city.py +++ b/city.py @@ -196,21 +196,28 @@ def spoofGeolocation(baseDir: str, default_latdirection, default_longdirection, "", "", 0) cities = [] - with open(locationsFilename, 'r') as f: - cities = f.readlines() + try: + with open(locationsFilename, 'r') as f: + cities = f.readlines() + except OSError: + print('EX: unable to read locations ' + locationsFilename) nogo = [] if nogoList: nogo = nogoList else: if os.path.isfile(nogoFilename): - with open(nogoFilename, 'r') as f: - nogoList = f.readlines() - for line in nogoList: - if line.startswith(city + ':'): - polygon = parseNogoString(line) - if polygon: - nogo.append(polygon) + nogoList = [] + try: + with open(nogoFilename, 'r') as f: + nogoList = f.readlines() + except OSError: + print('EX: unable to read ' + nogoFilename) + for line in nogoList: + if line.startswith(city + ':'): + polygon = parseNogoString(line) + if polygon: + nogo.append(polygon) city = city.lower() for cityName in cities: @@ -295,8 +302,11 @@ def getSpoofedCity(city: str, baseDir: str, nickname: str, domain: str) -> str: city = '' cityFilename = acctDir(baseDir, nickname, domain) + '/city.txt' if os.path.isfile(cityFilename): - with open(cityFilename, 'r') as fp: - city = fp.read().replace('\n', '') + try: + with open(cityFilename, 'r') as fp: + city = fp.read().replace('\n', '') + except OSError: + print('EX: unable to read ' + cityFilename) return city diff --git a/content.py b/content.py index e4dcfe99a..b32c9850d 100644 --- a/content.py +++ b/content.py @@ -178,9 +178,14 @@ def dangerousCSS(filename: str, allowLocalNetworkAccess: bool) -> bool: if not os.path.isfile(filename): return False - with open(filename, 'r') as fp: - content = fp.read().lower() + content = None + try: + with open(filename, 'r') as fp: + content = fp.read().lower() + except OSError: + print('EX: unable to read css file ' + filename) + if content: cssMatches = ('behavior:', ':expression', '?php', '.php', 'google', 'regexp', 'localhost', '127.0.', '192.168', '10.0.', '@import') @@ -221,8 +226,11 @@ def switchWords(baseDir: str, nickname: str, domain: str, content: str, acctDir(baseDir, nickname, domain) + '/replacewords.txt' if not os.path.isfile(switchWordsFilename): return content - with open(switchWordsFilename, 'r') as fp: - rules = fp.readlines() + try: + with open(switchWordsFilename, 'r') as fp: + rules = fp.readlines() + except OSError: + print('EX: unable to read switches ' + switchWordsFilename) for line in rules: replaceStr = line.replace('\n', '').replace('\r', '') @@ -779,8 +787,11 @@ def _loadAutoTags(baseDir: str, nickname: str, domain: str) -> []: filename = acctDir(baseDir, nickname, domain) + '/autotags.txt' if not os.path.isfile(filename): return [] - with open(filename, 'r') as f: - return f.readlines() + try: + with open(filename, 'r') as f: + return f.readlines() + except OSError: + print('EX: unable to read auto tags ' + filename) return [] @@ -853,12 +864,16 @@ def addHtmlTags(baseDir: str, httpPrefix: str, petnames = None if '@' in words: if os.path.isfile(followingFilename): - with open(followingFilename, 'r') as f: - following = f.readlines() - for handle in following: - pet = getPetName(baseDir, nickname, domain, handle) - if pet: - petnames.append(pet + '\n') + following = [] + try: + with open(followingFilename, 'r') as f: + following = f.readlines() + except OSError: + print('EX: unable to read ' + followingFilename) + for handle in following: + pet = getPetName(baseDir, nickname, domain, handle) + if pet: + petnames.append(pet + '\n') # extract mentions and tags from words longWordsList = [] diff --git a/daemon.py b/daemon.py index dac884889..1ff0b4e8e 100644 --- a/daemon.py +++ b/daemon.py @@ -2355,13 +2355,16 @@ class PubServer(BaseHTTPRequestHandler): else: if os.path.isdir(accountDir): nwFilename = newswireBlockedFilename + nwWritten = False try: with open(nwFilename, 'w+') as noNewswireFile: noNewswireFile.write('\n') - refreshNewswire(self.server.baseDir) + nwWritten = True except OSError as e: print('EX: unable to write ' + nwFilename + ' ' + str(e)) + if nwWritten: + refreshNewswire(self.server.baseDir) usersPathStr = \ usersPath + '/' + self.server.defaultTimeline + \ '?page=' + str(pageNumber) @@ -2398,13 +2401,16 @@ class PubServer(BaseHTTPRequestHandler): else: if os.path.isdir(accountDir): featFilename = featuresBlockedFilename + featWritten = False try: with open(featFilename, 'w+') as noFeaturesFile: noFeaturesFile.write('\n') - refreshNewswire(self.server.baseDir) + featWritten = True except OSError as e: print('EX: unable to write ' + featFilename + ' ' + str(e)) + if featWritten: + refreshNewswire(self.server.baseDir) usersPathStr = \ usersPath + '/' + self.server.defaultTimeline + \ '?page=' + str(pageNumber) @@ -3167,7 +3173,15 @@ class PubServer(BaseHTTPRequestHandler): self._write(msg) self.server.POSTbusy = False return - elif searchStr.startswith('*'): + elif (searchStr.startswith('*') or + searchStr.endswith(' skill')): + possibleEndings = ( + ' skill' + ) + for possEnding in possibleEndings: + if searchStr.endswith(possEnding): + searchStr = searchStr.replace(possEnding, '') + break # skill search searchStr = searchStr.replace('*', '').strip() skillStr = \ @@ -3187,7 +3201,34 @@ class PubServer(BaseHTTPRequestHandler): self._write(msg) self.server.POSTbusy = False return - elif searchStr.startswith("'"): + elif (searchStr.startswith("'") or + searchStr.endswith(' history') or + searchStr.endswith(' in sent') or + searchStr.endswith(' in outbox') or + searchStr.endswith(' in outgoing') or + searchStr.endswith(' in sent items') or + searchStr.endswith(' in sent posts') or + searchStr.endswith(' in outgoing posts') or + searchStr.endswith(' in my history') or + searchStr.endswith(' in my outbox') or + searchStr.endswith(' in my posts')): + possibleEndings = ( + ' in my posts', + ' in my history', + ' in my outbox', + ' in sent posts', + ' in outgoing posts', + ' in sent items', + ' in history', + ' in outbox', + ' in outgoing', + ' in sent', + ' history' + ) + for possEnding in possibleEndings: + if searchStr.endswith(possEnding): + searchStr = searchStr.replace(possEnding, '') + break # your post history search nickname = getNicknameFromActor(actorStr) searchStr = searchStr.replace("'", '', 1).strip() @@ -3227,7 +3268,35 @@ class PubServer(BaseHTTPRequestHandler): self._write(msg) self.server.POSTbusy = False return - elif searchStr.startswith('-'): + elif (searchStr.startswith('-') or + searchStr.endswith(' in my saved items') or + searchStr.endswith(' in my saved posts') or + searchStr.endswith(' in my bookmarks') or + searchStr.endswith(' in my saved') or + searchStr.endswith(' in my saves') or + searchStr.endswith(' in saved posts') or + searchStr.endswith(' in saved items') or + searchStr.endswith(' in bookmarks') or + searchStr.endswith(' in saved') or + searchStr.endswith(' in saves') or + searchStr.endswith(' bookmark')): + possibleEndings = ( + ' in my bookmarks' + ' in my saved posts' + ' in my saved items' + ' in my saved' + ' in my saves' + ' in saved posts' + ' in saved items' + ' in saved' + ' in saves' + ' in bookmarks' + ' bookmark' + ) + for possEnding in possibleEndings: + if searchStr.endswith(possEnding): + searchStr = searchStr.replace(possEnding, '') + break # bookmark search nickname = getNicknameFromActor(actorStr) searchStr = searchStr.replace('-', '', 1).strip() @@ -3906,8 +3975,11 @@ class PubServer(BaseHTTPRequestHandler): if fields.get('editedLinks'): linksStr = fields['editedLinks'] - with open(linksFilename, 'w+') as linksFile: - linksFile.write(linksStr) + try: + with open(linksFilename, 'w+') as linksFile: + linksFile.write(linksStr) + except OSError: + print('EX: _linksUpdate unable to write ' + linksFilename) else: if os.path.isfile(linksFilename): try: @@ -3923,8 +3995,11 @@ class PubServer(BaseHTTPRequestHandler): aboutStr = fields['editedAbout'] if not dangerousMarkup(aboutStr, allowLocalNetworkAccess): - with open(aboutFilename, 'w+') as aboutFile: - aboutFile.write(aboutStr) + try: + with open(aboutFilename, 'w+') as aboutFile: + aboutFile.write(aboutStr) + except OSError: + print('EX: unable to write about ' + aboutFilename) else: if os.path.isfile(aboutFilename): try: @@ -3937,8 +4012,11 @@ class PubServer(BaseHTTPRequestHandler): TOSStr = fields['editedTOS'] if not dangerousMarkup(TOSStr, allowLocalNetworkAccess): - with open(TOSFilename, 'w+') as TOSFile: - TOSFile.write(TOSStr) + try: + with open(TOSFilename, 'w+') as TOSFile: + TOSFile.write(TOSStr) + except OSError: + print('EX: unable to write TOS ' + TOSFilename) else: if os.path.isfile(TOSFilename): try: @@ -4731,7 +4809,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'): @@ -5375,14 +5453,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() @@ -5396,15 +5481,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() @@ -5429,14 +5521,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() @@ -5450,15 +5546,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() @@ -5483,14 +5584,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() @@ -5504,15 +5611,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() @@ -5537,14 +5650,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() @@ -5558,15 +5675,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() @@ -5666,16 +5787,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: @@ -5693,9 +5823,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: @@ -5716,8 +5850,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: @@ -5746,8 +5884,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: @@ -5768,8 +5911,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 @@ -5777,8 +5924,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: @@ -5793,8 +5945,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 @@ -5802,9 +5958,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: @@ -5867,8 +6028,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: @@ -5883,8 +6048,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: @@ -5899,8 +6068,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: @@ -5915,8 +6088,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: @@ -5931,8 +6108,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: @@ -5949,8 +6130,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: @@ -5966,8 +6152,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: @@ -6021,8 +6211,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: @@ -6048,8 +6243,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: @@ -6299,8 +6498,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, @@ -6345,8 +6549,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, @@ -6394,8 +6603,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, @@ -6818,8 +7032,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, @@ -6851,8 +7070,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', @@ -6890,8 +7112,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, @@ -6938,8 +7165,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, @@ -6980,8 +7212,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, @@ -7005,8 +7242,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, @@ -12404,8 +12646,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, @@ -12474,8 +12721,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, @@ -15885,8 +16137,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') @@ -15949,8 +16206,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: diff --git a/filters.py b/filters.py index 20efe8a5b..2c2de0cb7 100644 --- a/filters.py +++ b/filters.py @@ -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 diff --git a/follow.py b/follow.py index 1708f9a21..a5840dbcf 100644 --- a/follow.py +++ b/follow.py @@ -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) diff --git a/followingCalendar.py b/followingCalendar.py index c179690fe..988173da8 100644 --- a/followingCalendar.py +++ b/followingCalendar.py @@ -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, diff --git a/git.py b/git.py index df0e4a638..ec385c3b2 100644 --- a/git.py +++ b/git.py @@ -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