epicyon/followingCalendar.py

157 lines
5.9 KiB
Python
Raw Normal View History

2020-07-03 19:21:48 +00:00
__filename__ = "followingCalendar.py"
__author__ = "Bob Mottram"
__license__ = "AGPL3+"
2022-02-03 13:58:20 +00:00
__version__ = "1.3.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:
2022-01-02 13:18:11 +00:00
"""Returns the directory of an account
"""
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,
2022-01-02 13:18:11 +00:00
following_nickname: str,
following_domain: str) -> bool:
2020-07-03 19:21:48 +00:00
"""Returns true if receiving calendar events from the given
account from following.txt
"""
2022-01-02 13:18:11 +00:00
if following_nickname == nickname and following_domain == domain:
2020-07-11 22:39:30 +00:00
# reminder post
return True
2022-01-02 13:18:11 +00:00
calendar_filename = \
2021-12-29 21:55:09 +00:00
_dir_acct(base_dir, nickname, domain) + '/followingCalendar.txt'
2022-01-02 13:18:11 +00:00
handle = following_nickname + '@' + following_domain
if not os.path.isfile(calendar_filename):
following_filename = \
2021-12-29 21:55:09 +00:00
_dir_acct(base_dir, nickname, domain) + '/following.txt'
2022-01-02 13:18:11 +00:00
if not os.path.isfile(following_filename):
2020-07-03 19:35:32 +00:00
return False
# create a new calendar file from the following file
2022-01-02 13:18:11 +00:00
following_handles = None
2021-11-26 14:35:26 +00:00
try:
2022-01-02 13:18:11 +00:00
with open(following_filename, 'r') as following_file:
following_handles = following_file.read()
2021-11-26 14:35:26 +00:00
except OSError:
2022-01-02 13:18:11 +00:00
print('EX: receiving_calendar_events ' + following_filename)
if following_handles:
2021-11-25 21:18:53 +00:00
try:
2022-01-02 13:18:11 +00:00
with open(calendar_filename, 'w+') as fp_cal:
fp_cal.write(following_handles)
2021-11-25 21:18:53 +00:00
except OSError:
2022-01-02 13:18:11 +00:00
print('EX: receiving_calendar_events 2 ' + calendar_filename)
return handle + '\n' in open(calendar_filename).read()
2020-07-03 19:21:48 +00:00
2021-12-29 21:55:09 +00:00
def _receive_calendar_events(base_dir: str, nickname: str, domain: str,
2022-01-02 13:18:11 +00:00
following_nickname: str,
following_domain: str,
2021-12-29 21:55:09 +00:00
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)
2022-01-02 13:18:11 +00:00
following_filename = \
2021-12-29 21:55:09 +00:00
_dir_acct(base_dir, nickname, domain) + '/following.txt'
2022-01-02 13:18:11 +00:00
if not os.path.isfile(following_filename):
print("WARN: following.txt doesn't exist for " +
nickname + '@' + domain)
2020-07-03 19:21:48 +00:00
return
2022-01-02 13:18:11 +00:00
handle = following_nickname + '@' + following_domain
2020-07-03 19:21:48 +00:00
# check that you are following this handle
2022-01-02 13:18:11 +00:00
if handle + '\n' not in open(following_filename).read():
print('WARN: ' + handle + ' is not in ' + following_filename)
2020-07-03 19:21:48 +00:00
return
2022-01-02 13:18:11 +00:00
calendar_filename = \
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
2022-01-02 13:18:11 +00:00
following_handles = ''
if os.path.isfile(calendar_filename):
print('Calendar file exists')
2021-11-26 14:35:26 +00:00
try:
2022-01-02 13:18:11 +00:00
with open(calendar_filename, 'r') as calendar_file:
following_handles = calendar_file.read()
2021-11-26 14:35:26 +00:00
except OSError:
2022-01-02 13:18:11 +00:00
print('EX: _receive_calendar_events ' + calendar_filename)
2020-07-03 19:21:48 +00:00
else:
# create a new calendar file from the following file
2022-01-02 13:18:11 +00:00
print('Creating calendar file ' + calendar_filename)
following_handles = ''
2021-11-26 14:35:26 +00:00
try:
2022-01-02 13:18:11 +00:00
with open(following_filename, 'r') as following_file:
following_handles = following_file.read()
2021-11-26 14:35:26 +00:00
except OSError:
2022-01-02 13:18:11 +00:00
print('EX: _receive_calendar_events 2 ' + calendar_filename)
if add:
2021-11-25 21:18:53 +00:00
try:
2022-01-02 13:18:11 +00:00
with open(calendar_filename, 'w+') as fp_cal:
fp_cal.write(following_handles + handle + '\n')
2021-11-25 21:18:53 +00:00
except OSError:
2022-01-02 13:18:11 +00:00
print('EX: unable to write ' + calendar_filename)
2020-07-03 19:21:48 +00:00
# already in the calendar file?
2022-01-02 13:18:11 +00:00
if handle + '\n' in following_handles:
print(handle + ' exists in followingCalendar.txt')
2020-07-03 19:21:48 +00:00
if add:
# already added
return
# remove from calendar file
2022-01-02 13:18:11 +00:00
following_handles = following_handles.replace(handle + '\n', '')
2021-11-25 21:18:53 +00:00
try:
2022-01-02 13:18:11 +00:00
with open(calendar_filename, 'w+') as fp_cal:
fp_cal.write(following_handles)
2021-11-25 21:18:53 +00:00
except OSError:
2022-01-02 13:18:11 +00:00
print('EX: _receive_calendar_events 3 ' + calendar_filename)
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
2022-01-02 13:18:11 +00:00
following_handles += handle + '\n'
2021-11-25 21:18:53 +00:00
try:
2022-01-02 13:18:11 +00:00
with open(calendar_filename, 'w+') as fp_cal:
fp_cal.write(following_handles)
2021-11-25 21:18:53 +00:00
except OSError:
2022-01-02 13:18:11 +00:00
print('EX: _receive_calendar_events 4 ' + calendar_filename)
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,
2022-01-02 13:18:11 +00:00
following_nickname: str,
following_domain: str) -> None:
"""Add a person to the calendar
"""
2021-12-29 21:55:09 +00:00
_receive_calendar_events(base_dir, nickname, domain,
2022-01-02 13:18:11 +00:00
following_nickname, following_domain, 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,
2022-01-02 13:18:11 +00:00
following_nickname: str,
following_domain: str) -> None:
"""Remove a person from the calendar
"""
2021-12-29 21:55:09 +00:00
_receive_calendar_events(base_dir, nickname, domain,
2022-01-02 13:18:11 +00:00
following_nickname, following_domain, False)