2020-07-03 19:21:48 +00:00
|
|
|
__filename__ = "followingCalendar.py"
|
|
|
|
__author__ = "Bob Mottram"
|
|
|
|
__license__ = "AGPL3+"
|
2024-01-21 19:01:20 +00:00
|
|
|
__version__ = "1.5.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
|
|
|
|
|
|
|
|
2024-05-12 12:35:26 +00:00
|
|
|
def _data_dir2(base_dir) -> str:
|
|
|
|
"""Returns the directory where account data is stored
|
|
|
|
"""
|
|
|
|
return base_dir + '/accounts'
|
|
|
|
|
|
|
|
|
2022-06-11 16:14:16 +00:00
|
|
|
def _text_in_file2(text: str, filename: str,
|
2024-05-01 12:16:06 +00:00
|
|
|
case_sensitive: bool) -> bool:
|
2022-06-11 16:14:16 +00:00
|
|
|
"""is the given text in the given file?
|
|
|
|
"""
|
|
|
|
if not case_sensitive:
|
|
|
|
text = text.lower()
|
2024-07-23 12:09:20 +00:00
|
|
|
content = ''
|
2022-06-11 16:14:16 +00:00
|
|
|
try:
|
2024-07-16 12:20:58 +00:00
|
|
|
with open(filename, 'r', encoding='utf-8') as fp_file:
|
|
|
|
content = fp_file.read()
|
2022-06-11 16:14:16 +00:00
|
|
|
except OSError:
|
2024-05-12 13:53:49 +00:00
|
|
|
print('EX: unable to find text in missing file 2 ' + filename)
|
2024-07-23 12:09:20 +00:00
|
|
|
if content:
|
|
|
|
if not case_sensitive:
|
|
|
|
content = content.lower()
|
|
|
|
if text in content:
|
|
|
|
return True
|
2022-06-11 16:14:16 +00:00
|
|
|
return False
|
|
|
|
|
|
|
|
|
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
|
|
|
|
"""
|
2024-05-12 12:35:26 +00:00
|
|
|
return _data_dir2(base_dir) + '/' + 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-06-09 14:46:30 +00:00
|
|
|
with open(following_filename, 'r',
|
2024-07-14 13:01:46 +00:00
|
|
|
encoding='utf-8') as fp_following:
|
|
|
|
following_handles = fp_following.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-06-09 14:46:30 +00:00
|
|
|
with open(calendar_filename, 'w+',
|
|
|
|
encoding='utf-8') as fp_cal:
|
2022-01-02 13:18:11 +00:00
|
|
|
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)
|
2022-12-30 21:07:19 +00:00
|
|
|
return _text_in_file2(handle + '\n', calendar_filename, False)
|
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):
|
2020-09-03 12:16:24 +00:00
|
|
|
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-12-30 21:17:03 +00:00
|
|
|
if not _text_in_file2(handle + '\n', following_filename, False):
|
2022-01-02 13:18:11 +00:00
|
|
|
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):
|
2020-09-03 12:16:24 +00:00
|
|
|
print('Calendar file exists')
|
2021-11-26 14:35:26 +00:00
|
|
|
try:
|
2022-06-09 14:46:30 +00:00
|
|
|
with open(calendar_filename, 'r',
|
2024-07-14 13:01:46 +00:00
|
|
|
encoding='utf-8') as fp_calendar:
|
|
|
|
following_handles = fp_calendar.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-06-09 14:46:30 +00:00
|
|
|
with open(following_filename, 'r',
|
2024-07-14 13:01:46 +00:00
|
|
|
encoding='utf-8') as fp_following:
|
|
|
|
following_handles = fp_following.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)
|
2020-09-03 12:16:24 +00:00
|
|
|
if add:
|
2021-11-25 21:18:53 +00:00
|
|
|
try:
|
2022-06-09 14:46:30 +00:00
|
|
|
with open(calendar_filename, 'w+',
|
|
|
|
encoding='utf-8') as fp_cal:
|
2022-01-02 13:18:11 +00:00
|
|
|
fp_cal.write(following_handles + handle + '\n')
|
2021-11-25 21:18:53 +00:00
|
|
|
except OSError:
|
2024-07-02 22:16:13 +00:00
|
|
|
print('EX: _receive_calendar_events unable to write ' +
|
|
|
|
calendar_filename)
|
2020-07-03 19:21:48 +00:00
|
|
|
|
|
|
|
# already in the calendar file?
|
2022-12-30 21:17:03 +00:00
|
|
|
if handle + '\n' in following_handles or \
|
|
|
|
handle + '\n' in following_handles.lower():
|
2020-09-03 12:16:24 +00:00
|
|
|
print(handle + ' exists in followingCalendar.txt')
|
2020-07-03 19:21:48 +00:00
|
|
|
if add:
|
|
|
|
# already added
|
|
|
|
return
|
|
|
|
# remove from calendar file
|
2022-12-30 21:33:14 +00:00
|
|
|
new_following_handles = ''
|
|
|
|
following_handles_list = following_handles.split('\n')
|
|
|
|
handle_lower = handle.lower()
|
|
|
|
for followed in following_handles_list:
|
|
|
|
if followed.lower() != handle_lower:
|
|
|
|
new_following_handles += followed + '\n'
|
|
|
|
following_handles = new_following_handles
|
|
|
|
# save the result
|
2022-12-30 21:07:19 +00:00
|
|
|
try:
|
|
|
|
with open(calendar_filename, 'w+',
|
|
|
|
encoding='utf-8') as fp_cal:
|
|
|
|
fp_cal.write(following_handles)
|
|
|
|
except OSError:
|
|
|
|
print('EX: _receive_calendar_events 3 ' + calendar_filename)
|
2020-07-03 19:21:48 +00:00
|
|
|
else:
|
2020-09-03 12:16:24 +00:00
|
|
|
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-06-09 14:46:30 +00:00
|
|
|
with open(calendar_filename, 'w+',
|
|
|
|
encoding='utf-8') as fp_cal:
|
2022-01-02 13:18:11 +00:00
|
|
|
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)
|