epicyon/followingCalendar.py

112 lines
4.3 KiB
Python
Raw Normal View History

2020-07-03 19:21:48 +00:00
__filename__ = "followingCalendar.py"
__author__ = "Bob Mottram"
__license__ = "AGPL3+"
2021-01-26 10:07:42 +00:00
__version__ = "1.2.0"
2020-07-03 19:21:48 +00:00
__maintainer__ = "Bob Mottram"
__email__ = "bob@freedombone.net"
__status__ = "Production"
import os
def receivingCalendarEvents(baseDir: str, nickname: str, domain: str,
followingNickname: str,
followingDomain: str) -> bool:
"""Returns true if receiving calendar events from the given
account from following.txt
"""
2020-07-11 22:39:30 +00:00
if followingNickname == nickname and followingDomain == domain:
# reminder post
return True
2020-07-03 19:21:48 +00:00
calendarFilename = baseDir + '/accounts/' + \
nickname + '@' + domain + '/followingCalendar.txt'
handle = followingNickname + '@' + followingDomain
if not os.path.isfile(calendarFilename):
2020-07-03 19:35:32 +00:00
followingFilename = baseDir + '/accounts/' + \
nickname + '@' + domain + '/following.txt'
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()
2020-07-12 20:04:58 +00:00
with open(calendarFilename, 'w+') as fp:
2020-07-03 19:35:32 +00:00
fp.write(followingHandles)
2020-07-03 19:21:48 +00:00
return handle + '\n' in open(calendarFilename).read()
def _receiveCalendarEvents(baseDir: str, nickname: str, domain: str,
followingNickname: str,
followingDomain: str,
add: bool) -> None:
2020-07-03 19:21:48 +00:00
"""Adds or removes a handle from the following.txt list into a list
indicating whether to receive calendar events from that account
"""
# check that a following file exists
if ':' in domain:
domain = domain.split(':')[0]
2020-07-03 19:21:48 +00:00
followingFilename = baseDir + '/accounts/' + \
nickname + '@' + domain + '/following.txt'
if not os.path.isfile(followingFilename):
print("WARN: following.txt doesn't exist for " +
nickname + '@' + domain)
2020-07-03 19:21:48 +00:00
return
handle = followingNickname + '@' + followingDomain
# check that you are following this handle
if handle + '\n' not in open(followingFilename).read():
print('WARN: ' + handle + ' is not in ' + followingFilename)
2020-07-03 19:21:48 +00:00
return
calendarFilename = baseDir + '/accounts/' + \
nickname + '@' + domain + '/followingCalendar.txt'
# get the contents of the calendar file, which is
# a set of handles
followingHandles = ''
if os.path.isfile(calendarFilename):
print('Calendar file exists')
2020-07-03 19:21:48 +00:00
with open(calendarFilename, 'r') as calendarFile:
followingHandles = calendarFile.read()
else:
# create a new calendar file from the following file
print('Creating calendar file ' + calendarFilename)
followingHandles = ''
2020-07-03 19:21:48 +00:00
with open(followingFilename, 'r') as followingFile:
followingHandles = followingFile.read()
if add:
2020-07-12 20:04:58 +00:00
with open(calendarFilename, 'w+') as fp:
fp.write(followingHandles + handle + '\n')
2020-07-03 19:21:48 +00:00
# already in the calendar file?
if handle + '\n' in followingHandles:
print(handle + ' exists in followingCalendar.txt')
2020-07-03 19:21:48 +00:00
if add:
# already added
return
# remove from calendar file
followingHandles = followingHandles.replace(handle + '\n', '')
2020-07-12 20:04:58 +00:00
with open(calendarFilename, 'w+') as fp:
2020-07-03 19:21:48 +00:00
fp.write(followingHandles)
else:
print(handle + ' not in followingCalendar.txt')
2020-07-03 19:21:48 +00:00
# not already in the calendar file
if add:
# append to the list of handles
followingHandles += handle + '\n'
2020-07-12 20:04:58 +00:00
with open(calendarFilename, 'w+') as fp:
2020-07-03 19:21:48 +00:00
fp.write(followingHandles)
def addPersonToCalendar(baseDir: str, nickname: str, domain: str,
followingNickname: str,
followingDomain: str) -> None:
_receiveCalendarEvents(baseDir, nickname, domain,
followingNickname, followingDomain, True)
2020-07-03 19:21:48 +00:00
def removePersonFromCalendar(baseDir: str, nickname: str, domain: str,
followingNickname: str,
followingDomain: str) -> None:
_receiveCalendarEvents(baseDir, nickname, domain,
followingNickname, followingDomain, False)