diff --git a/petnames.py b/petnames.py index 305f1d591..7046a5d94 100644 --- a/petnames.py +++ b/petnames.py @@ -29,7 +29,7 @@ def setPetName(baseDir: str, nickname: str, domain: str, if os.path.isfile(petnamesFilename): with open(petnamesFilename, 'r') as petnamesFile: petnamesStr = petnamesFile.read() - if petnamesStr.startswith(petname + ' '): + if entry in petnamesStr: return True if ' ' + handle + '\n' in petnamesStr: petnamesList = petnamesStr.split('\n') diff --git a/tests.py b/tests.py index 4b86d499d..e42a6c930 100644 --- a/tests.py +++ b/tests.py @@ -1387,6 +1387,8 @@ def testClientToServer(): httpPrefix, cachedWebfingers, personCache, True, __version__) + alicePetnamesFilename = aliceDir + '/accounts/' + \ + 'alice@' + aliceDomain + '/petnames.txt' aliceFollowingFilename = \ aliceDir + '/accounts/alice@' + aliceDomain + '/following.txt' bobFollowersFilename = \ @@ -1395,7 +1397,8 @@ def testClientToServer(): if os.path.isfile(bobFollowersFilename): if 'alice@' + aliceDomain + ':' + str(alicePort) in \ open(bobFollowersFilename).read(): - if os.path.isfile(aliceFollowingFilename): + if os.path.isfile(aliceFollowingFilename) and \ + os.path.isfile(alicePetnamesFilename): if 'bob@' + bobDomain + ':' + str(bobPort) in \ open(aliceFollowingFilename).read(): break @@ -1403,6 +1406,9 @@ def testClientToServer(): assert os.path.isfile(bobFollowersFilename) assert os.path.isfile(aliceFollowingFilename) + assert os.path.isfile(alicePetnamesFilename) + assert 'bob bob@' + bobDomain in \ + open(alicePetnamesFilename).read() print('alice@' + aliceDomain + ':' + str(alicePort) + ' in ' + bobFollowersFilename) assert 'alice@' + aliceDomain + ':' + str(alicePort) in \ diff --git a/utils.py b/utils.py index 3bd40da77..8607c4c63 100644 --- a/utils.py +++ b/utils.py @@ -522,6 +522,37 @@ def getDomainFromActor(actor: str) -> (str, int): return domain, port +def setDefaultPetName(baseDir: str, nickname: str, domain: str, + followNickname: str, followDomain: str) -> None: + """Sets a default petname + This helps especially when using onion or i2p address + """ + if ':' in domain: + domain = domain.split(':')[0] + userPath = baseDir + '/accounts/' + nickname + '@' + domain + petnamesFilename = userPath + '/petnames.txt' + + petnameLookupEntry = followNickname + ' ' + \ + followNickname + '@' + followDomain + '\n' + if not os.path.isfile(petnamesFilename): + # if there is no existing petnames lookup file + with open(petnamesFilename, 'w+') as petnamesFile: + petnamesFile.write(petnameLookupEntry) + return + + with open(petnamesFilename, 'r') as petnamesFile: + petnamesStr = petnamesFile.read() + if petnamesStr: + petnamesList = petnamesStr.split('\n') + for pet in petnamesList: + if pet.startswith(followNickname + ' '): + # petname already exists + return + # petname doesn't already exist + with open(petnamesFilename, 'a+') as petnamesFile: + petnamesFile.write(petnameLookupEntry) + + def followPerson(baseDir: str, nickname: str, domain: str, followNickname: str, followDomain: str, federationList: [], debug: bool, @@ -593,9 +624,9 @@ def followPerson(baseDir: str, nickname: str, domain: str, with open(filename, 'w+') as f: f.write(handleToFollow + '\n') - # Default to adding new follows to the calendar. - # Possibly this could be made optional if followFile.endswith('following.txt'): + # Default to adding new follows to the calendar. + # Possibly this could be made optional # if following a person add them to the list of # calendar follows print('DEBUG: adding ' + @@ -603,6 +634,9 @@ def followPerson(baseDir: str, nickname: str, domain: str, nickname + '@' + domain) addPersonToCalendar(baseDir, nickname, domain, followNickname, followDomain) + # add a default petname + setDefaultPetName(baseDir, nickname, domain, + followNickname, followDomain) return True