epicyon/followingCalendar.py

150 lines
5.6 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"
2021-09-10 16:14:50 +00:00
__email__ = "bob@libreserver.org"
2020-07-03 19:21:48 +00:00
__status__ = "Production"
2021-06-15 15:08:12 +00:00
__module_group__ = "Calendar"
2020-07-03 19:21:48 +00:00
import os
2021-06-26 14:21:24 +00:00
2021-07-13 21:59:53 +00:00
def _dirAcct(baseDir: str, nickname: str, domain: str) -> str:
return baseDir + '/accounts/' + nickname + '@' + domain
2021-07-18 09:55:49 +00:00
2021-06-26 14:21:24 +00:00
def _portDomainRemove(domain: str) -> str:
"""If the domain has a port appended then remove it
eg. mydomain.com:80 becomes mydomain.com
same as removeDomainPort in utils.py
"""
if ':' in domain:
if domain.startswith('did:'):
return domain
domain = domain.split(':')[0]
return domain
2020-07-03 19:21:48 +00:00
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
2021-07-13 21:59:53 +00:00
calendarFilename = \
_dirAcct(baseDir, nickname, domain) + '/followingCalendar.txt'
2020-07-03 19:21:48 +00:00
handle = followingNickname + '@' + followingDomain
if not os.path.isfile(calendarFilename):
2021-07-13 21:59:53 +00:00
followingFilename = \
_dirAcct(baseDir, nickname, domain) + '/following.txt'
2020-07-03 19:35:32 +00:00
if not os.path.isfile(followingFilename):
return False
# create a new calendar file from the following file
2021-11-26 14:35:26 +00:00
followingHandles = None
try:
with open(followingFilename, 'r') as followingFile:
followingHandles = followingFile.read()
except OSError:
print('EX: receivingCalendarEvents ' + followingFilename)
if followingHandles:
2021-11-25 21:18:53 +00:00
try:
with open(calendarFilename, 'w+') as fp:
fp.write(followingHandles)
except OSError:
2021-11-26 14:35:26 +00:00
print('EX: receivingCalendarEvents 2 ' + calendarFilename)
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
2021-06-26 14:21:24 +00:00
domain = _portDomainRemove(domain)
2021-07-13 21:59:53 +00:00
followingFilename = _dirAcct(baseDir, nickname, domain) + '/following.txt'
2020-07-03 19:21:48 +00:00
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
2021-07-13 21:59:53 +00:00
calendarFilename = \
_dirAcct(baseDir, nickname, domain) + '/followingCalendar.txt'
2020-07-03 19:21:48 +00:00
# get the contents of the calendar file, which is
# a set of handles
followingHandles = ''
if os.path.isfile(calendarFilename):
print('Calendar file exists')
2021-11-26 14:35:26 +00:00
try:
with open(calendarFilename, 'r') as calendarFile:
followingHandles = calendarFile.read()
except OSError:
print('EX: _receiveCalendarEvents ' + calendarFilename)
2020-07-03 19:21:48 +00:00
else:
# create a new calendar file from the following file
print('Creating calendar file ' + calendarFilename)
followingHandles = ''
2021-11-26 14:35:26 +00:00
try:
with open(followingFilename, 'r') as followingFile:
followingHandles = followingFile.read()
except OSError:
print('EX: _receiveCalendarEvents 2 ' + calendarFilename)
if add:
2021-11-25 21:18:53 +00:00
try:
with open(calendarFilename, 'w+') as fp:
fp.write(followingHandles + handle + '\n')
except OSError:
2021-11-25 22:22:54 +00:00
print('EX: unable to write ' + calendarFilename)
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', '')
2021-11-25 21:18:53 +00:00
try:
with open(calendarFilename, 'w+') as fp:
fp.write(followingHandles)
except OSError:
2021-11-26 14:35:26 +00:00
print('EX: _receiveCalendarEvents 3 ' + calendarFilename)
2020-07-03 19:21:48 +00:00
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'
2021-11-25 21:18:53 +00:00
try:
with open(calendarFilename, 'w+') as fp:
fp.write(followingHandles)
except OSError:
2021-11-26 14:35:26 +00:00
print('EX: _receiveCalendarEvents 4 ' + calendarFilename)
2020-07-03 19:21:48 +00:00
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)