epicyon/followingCalendar.py

186 lines
6.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
def _text_in_file2(text: str, filename: str,
case_sensitive: bool = True) -> bool:
"""is the given text in the given file?
"""
if not case_sensitive:
text = text.lower()
try:
with open(filename, 'r', encoding='utf-8') as file:
content = file.read()
if content:
if not case_sensitive:
content = content.lower()
if text in content:
return True
except OSError:
print('EX: unable to find text in missing file ' + filename)
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
"""
2022-12-18 15:29:54 +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-06-09 14:46:30 +00:00
with open(following_filename, 'r',
encoding='utf-8') as following_file:
2022-01-02 13:18:11 +00:00
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-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):
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):
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',
encoding='utf-8') as calendar_file:
2022-01-02 13:18:11 +00:00
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-06-09 14:46:30 +00:00
with open(following_filename, 'r',
encoding='utf-8') as following_file:
2022-01-02 13:18:11 +00:00
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-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:
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-12-30 21:17:03 +00:00
if handle + '\n' in following_handles or \
handle + '\n' in following_handles.lower():
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', '')
2022-12-30 21:07:19 +00:00
following_handles = \
following_handles.replace(handle.lower() + '\n', '')
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:
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)