epicyon/followingCalendar.py

151 lines
5.7 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-12-29 21:55:09 +00:00
def _dir_acct(base_dir: str, nickname: str, domain: str) -> str:
2021-12-25 16:17:53 +00:00
return base_dir + '/accounts/' + nickname + '@' + domain
2021-07-13 21:59:53 +00:00
2021-07-18 09:55:49 +00:00
2021-12-29 21:55:09 +00:00
def _port_domain_remove(domain: str) -> str:
2021-06-26 14:21:24 +00:00
"""If the domain has a port appended then remove it
eg. mydomain.com:80 becomes mydomain.com
2021-12-26 18:17:37 +00:00
same as remove_domain_port in utils.py
2021-06-26 14:21:24 +00:00
"""
if ':' in domain:
if domain.startswith('did:'):
return domain
domain = domain.split(':')[0]
return domain
2020-07-03 19:21:48 +00:00
2021-12-29 21:55:09 +00:00
def receiving_calendar_events(base_dir: str, nickname: str, domain: str,
followingNickname: str,
followingDomain: str) -> bool:
2020-07-03 19:21:48 +00:00
"""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 = \
2021-12-29 21:55:09 +00:00
_dir_acct(base_dir, 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 = \
2021-12-29 21:55:09 +00:00
_dir_acct(base_dir, 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:
2021-12-29 21:55:09 +00:00
print('EX: receiving_calendar_events ' + followingFilename)
2021-11-26 14:35:26 +00:00
if followingHandles:
2021-11-25 21:18:53 +00:00
try:
with open(calendarFilename, 'w+') as fp:
fp.write(followingHandles)
except OSError:
2021-12-29 21:55:09 +00:00
print('EX: receiving_calendar_events 2 ' + calendarFilename)
2020-07-03 19:21:48 +00:00
return handle + '\n' in open(calendarFilename).read()
2021-12-29 21:55:09 +00:00
def _receive_calendar_events(base_dir: 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-12-29 21:55:09 +00:00
domain = _port_domain_remove(domain)
followingFilename = \
_dir_acct(base_dir, 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 = \
2021-12-29 21:55:09 +00:00
_dir_acct(base_dir, 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:
2021-12-29 21:55:09 +00:00
print('EX: _receive_calendar_events ' + 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:
2021-12-29 21:55:09 +00:00
print('EX: _receive_calendar_events 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-12-29 21:55:09 +00:00
print('EX: _receive_calendar_events 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-12-29 21:55:09 +00:00
print('EX: _receive_calendar_events 4 ' + calendarFilename)
2020-07-03 19:21:48 +00:00
2021-12-27 16:18:52 +00:00
def add_person_to_calendar(base_dir: str, nickname: str, domain: str,
followingNickname: str,
followingDomain: str) -> None:
2021-12-29 21:55:09 +00:00
_receive_calendar_events(base_dir, nickname, domain,
followingNickname, followingDomain, True)
2020-07-03 19:21:48 +00:00
2021-12-29 21:55:09 +00:00
def remove_person_from_calendar(base_dir: str, nickname: str, domain: str,
followingNickname: str,
followingDomain: str) -> None:
_receive_calendar_events(base_dir, nickname, domain,
followingNickname, followingDomain, False)