mirror of https://gitlab.com/bashrc2/epicyon
Replace file operations with functions
parent
7e9d013dd5
commit
946c584b98
20
git.py
20
git.py
|
|
@ -17,6 +17,7 @@ from utils import get_attachment_property_value
|
|||
from utils import remove_html
|
||||
from utils import get_attributed_to
|
||||
from utils import string_contains
|
||||
from data import save_string
|
||||
|
||||
|
||||
def _git_format_content(content: str) -> str:
|
||||
|
|
@ -213,17 +214,14 @@ def receive_git_patch(base_dir: str, nickname: str, domain: str,
|
|||
patch_str = \
|
||||
_git_add_from_handle(patch_str,
|
||||
'@' + from_nickname + '@' + from_domain)
|
||||
try:
|
||||
with open(patch_filename, 'w+', encoding='utf-8') as fp_patch:
|
||||
fp_patch.write(patch_str)
|
||||
patch_notify_filename = \
|
||||
acct_dir(base_dir, nickname, domain) + '/.newPatchContent'
|
||||
with open(patch_notify_filename, 'w+',
|
||||
encoding='utf-8') as fp_patch_notify:
|
||||
fp_patch_notify.write(patch_str)
|
||||
return True
|
||||
except OSError as ex:
|
||||
print('EX: receive_git_patch ' + patch_filename + ' ' + str(ex))
|
||||
if save_string(patch_str, patch_filename,
|
||||
'EX: receive_git_patch ' + patch_filename + ' 1 [ex]'):
|
||||
patch_notify_filename = \
|
||||
acct_dir(base_dir, nickname, domain) + '/.newPatchContent'
|
||||
if save_string(patch_str, patch_notify_filename,
|
||||
'EX: receive_git_patch ' +
|
||||
patch_filename + ' 2 [ex]'):
|
||||
return True
|
||||
return False
|
||||
|
||||
|
||||
|
|
|
|||
488
happening.py
488
happening.py
|
|
@ -36,6 +36,7 @@ from context import get_individual_post_context
|
|||
from session import get_method
|
||||
from auth import create_basic_auth_header
|
||||
from conversation import post_id_to_convthread_id
|
||||
from data import load_list
|
||||
from data import load_string
|
||||
from data import save_string
|
||||
from data import append_string
|
||||
|
|
@ -298,100 +299,98 @@ def get_todays_events(base_dir: str, nickname: str, domain: str,
|
|||
|
||||
calendar_post_ids: list[str] = []
|
||||
recreate_events_file: bool = False
|
||||
try:
|
||||
with open(calendar_filename, 'r', encoding='utf-8') as fp_events:
|
||||
for post_id in fp_events:
|
||||
post_id = remove_eol(post_id)
|
||||
post_filename = \
|
||||
locate_post(base_dir, nickname, domain, post_id)
|
||||
if not post_filename:
|
||||
recreate_events_file = True
|
||||
calendar_list: list[str] = \
|
||||
load_list(calendar_filename,
|
||||
'EX: get_todays_events failed to read ' +
|
||||
calendar_filename + ' [ex]')
|
||||
if calendar_list is not None:
|
||||
for post_id in calendar_list:
|
||||
post_id = remove_eol(post_id)
|
||||
post_filename = \
|
||||
locate_post(base_dir, nickname, domain, post_id)
|
||||
if not post_filename:
|
||||
recreate_events_file = True
|
||||
continue
|
||||
|
||||
post_json_object = load_json(post_filename)
|
||||
if not _is_happening_post(post_json_object):
|
||||
continue
|
||||
|
||||
content_language = system_language
|
||||
if post_json_object.get('object'):
|
||||
content = None
|
||||
if post_json_object['object'].get('contentMap'):
|
||||
sys_lang = system_language
|
||||
content_map = post_json_object['object']['contentMap']
|
||||
if content_map.get(sys_lang):
|
||||
content = content_map[sys_lang]
|
||||
content_language = sys_lang
|
||||
if not content:
|
||||
if post_json_object['object'].get('content'):
|
||||
content = post_json_object['object']['content']
|
||||
if content:
|
||||
if not _event_text_match(content, text_match):
|
||||
continue
|
||||
|
||||
public_event = is_public_post(post_json_object)
|
||||
|
||||
post_event: list[dict] = []
|
||||
day_of_month = None
|
||||
for tag in post_json_object['object']['tag']:
|
||||
if not _is_happening_event(tag):
|
||||
continue
|
||||
|
||||
post_json_object = load_json(post_filename)
|
||||
if not _is_happening_post(post_json_object):
|
||||
continue
|
||||
|
||||
content_language = system_language
|
||||
if post_json_object.get('object'):
|
||||
content = None
|
||||
if post_json_object['object'].get('contentMap'):
|
||||
sys_lang = system_language
|
||||
content_map = post_json_object['object']['contentMap']
|
||||
if content_map.get(sys_lang):
|
||||
content = content_map[sys_lang]
|
||||
content_language = sys_lang
|
||||
if not content:
|
||||
if post_json_object['object'].get('content'):
|
||||
content = post_json_object['object']['content']
|
||||
if content:
|
||||
if not _event_text_match(content, text_match):
|
||||
continue
|
||||
|
||||
public_event = is_public_post(post_json_object)
|
||||
|
||||
post_event: list[dict] = []
|
||||
day_of_month = None
|
||||
for tag in post_json_object['object']['tag']:
|
||||
if not _is_happening_event(tag):
|
||||
continue
|
||||
|
||||
# this tag is an event or a place
|
||||
if tag['type'] != 'Event':
|
||||
# tag is a place
|
||||
post_event.append(tag)
|
||||
continue
|
||||
|
||||
# tag is an event
|
||||
if not tag.get('startTime'):
|
||||
continue
|
||||
|
||||
# is the tag for this day?
|
||||
event_time = \
|
||||
date_from_string_format(tag['startTime'],
|
||||
["%Y-%m-%dT%H:%M:%S%z"])
|
||||
event_year = int(event_time.strftime("%Y"))
|
||||
event_month = int(event_time.strftime("%m"))
|
||||
event_day = int(event_time.strftime("%d"))
|
||||
if not (event_year == year and
|
||||
event_month == month_number and
|
||||
event_day == day_number):
|
||||
continue
|
||||
|
||||
day_of_month = str(event_day)
|
||||
if '#statuses#' in post_id:
|
||||
# link to the id so that the event can be
|
||||
# easily deleted
|
||||
tag['post_id'] = post_id.split('#statuses#')[1]
|
||||
tag['id'] = post_id.replace('#', '/')
|
||||
tag['sender'] = post_id.split('#statuses#')[0]
|
||||
tag['sender'] = tag['sender'].replace('#', '/')
|
||||
tag['public'] = public_event
|
||||
tag['language'] = content_language
|
||||
# this tag is an event or a place
|
||||
if tag['type'] != 'Event':
|
||||
# tag is a place
|
||||
post_event.append(tag)
|
||||
|
||||
if not (post_event and day_of_month):
|
||||
continue
|
||||
calendar_post_ids.append(post_id)
|
||||
if not events.get(day_of_month):
|
||||
events[day_of_month]: list[dict] = []
|
||||
events[day_of_month].append(post_event)
|
||||
events[day_of_month] = \
|
||||
_sort_todays_events(events[day_of_month])
|
||||
except OSError as exc:
|
||||
print('EX: get_todays_events failed to read ' +
|
||||
calendar_filename + ' ' + str(exc))
|
||||
|
||||
# tag is an event
|
||||
if not tag.get('startTime'):
|
||||
continue
|
||||
|
||||
# is the tag for this day?
|
||||
event_time = \
|
||||
date_from_string_format(tag['startTime'],
|
||||
["%Y-%m-%dT%H:%M:%S%z"])
|
||||
event_year = int(event_time.strftime("%Y"))
|
||||
event_month = int(event_time.strftime("%m"))
|
||||
event_day = int(event_time.strftime("%d"))
|
||||
if not (event_year == year and
|
||||
event_month == month_number and
|
||||
event_day == day_number):
|
||||
continue
|
||||
|
||||
day_of_month = str(event_day)
|
||||
if '#statuses#' in post_id:
|
||||
# link to the id so that the event can be
|
||||
# easily deleted
|
||||
tag['post_id'] = post_id.split('#statuses#')[1]
|
||||
tag['id'] = post_id.replace('#', '/')
|
||||
tag['sender'] = post_id.split('#statuses#')[0]
|
||||
tag['sender'] = tag['sender'].replace('#', '/')
|
||||
tag['public'] = public_event
|
||||
tag['language'] = content_language
|
||||
post_event.append(tag)
|
||||
|
||||
if not (post_event and day_of_month):
|
||||
continue
|
||||
calendar_post_ids.append(post_id)
|
||||
if not events.get(day_of_month):
|
||||
events[day_of_month]: list[dict] = []
|
||||
events[day_of_month].append(post_event)
|
||||
events[day_of_month] = \
|
||||
_sort_todays_events(events[day_of_month])
|
||||
|
||||
# if some posts have been deleted then regenerate the calendar file
|
||||
if recreate_events_file:
|
||||
try:
|
||||
with open(calendar_filename, 'w+',
|
||||
encoding='utf-8') as fp_calendar:
|
||||
for post_id in calendar_post_ids:
|
||||
fp_calendar.write(post_id + '\n')
|
||||
except OSError:
|
||||
print('EX: unable to recreate events file 1 ' +
|
||||
calendar_filename)
|
||||
text: str = ''
|
||||
for post_id in calendar_post_ids:
|
||||
text += post_id + '\n'
|
||||
save_string(text, calendar_filename,
|
||||
'EX: unable to recreate events file 1 ' +
|
||||
calendar_filename)
|
||||
|
||||
return events
|
||||
|
||||
|
|
@ -625,41 +624,41 @@ def day_events_check(base_dir: str, nickname: str, domain: str,
|
|||
return False
|
||||
|
||||
events_exist: bool = False
|
||||
try:
|
||||
with open(calendar_filename, 'r', encoding='utf-8') as fp_events:
|
||||
for post_id in fp_events:
|
||||
post_id = remove_eol(post_id)
|
||||
post_filename = \
|
||||
locate_post(base_dir, nickname, domain, post_id)
|
||||
if not post_filename:
|
||||
continue
|
||||
calendar_list: list[str] = \
|
||||
load_list(calendar_filename,
|
||||
'EX: day_events_check failed to read ' + calendar_filename)
|
||||
if calendar_list is not None:
|
||||
for post_id in calendar_list:
|
||||
post_id = remove_eol(post_id)
|
||||
post_filename = \
|
||||
locate_post(base_dir, nickname, domain, post_id)
|
||||
if not post_filename:
|
||||
continue
|
||||
|
||||
post_json_object = load_json(post_filename)
|
||||
if not _is_happening_post(post_json_object):
|
||||
continue
|
||||
post_json_object = load_json(post_filename)
|
||||
if not _is_happening_post(post_json_object):
|
||||
continue
|
||||
|
||||
for tag in post_json_object['object']['tag']:
|
||||
if not _is_happening_event(tag):
|
||||
continue
|
||||
# this tag is an event or a place
|
||||
if tag['type'] != 'Event':
|
||||
continue
|
||||
# tag is an event
|
||||
if not tag.get('startTime'):
|
||||
continue
|
||||
event_time = \
|
||||
date_from_string_format(tag['startTime'],
|
||||
["%Y-%m-%dT%H:%M:%S%z"])
|
||||
if int(event_time.strftime("%d")) != day_number:
|
||||
continue
|
||||
if int(event_time.strftime("%m")) != month_number:
|
||||
continue
|
||||
if int(event_time.strftime("%Y")) != year:
|
||||
continue
|
||||
events_exist = True
|
||||
break
|
||||
except OSError:
|
||||
print('EX: day_events_check failed to read ' + calendar_filename)
|
||||
for tag in post_json_object['object']['tag']:
|
||||
if not _is_happening_event(tag):
|
||||
continue
|
||||
# this tag is an event or a place
|
||||
if tag['type'] != 'Event':
|
||||
continue
|
||||
# tag is an event
|
||||
if not tag.get('startTime'):
|
||||
continue
|
||||
event_time = \
|
||||
date_from_string_format(tag['startTime'],
|
||||
["%Y-%m-%dT%H:%M:%S%z"])
|
||||
if int(event_time.strftime("%d")) != day_number:
|
||||
continue
|
||||
if int(event_time.strftime("%m")) != month_number:
|
||||
continue
|
||||
if int(event_time.strftime("%Y")) != year:
|
||||
continue
|
||||
events_exist = True
|
||||
break
|
||||
|
||||
return events_exist
|
||||
|
||||
|
|
@ -685,61 +684,60 @@ def get_this_weeks_events(base_dir: str, nickname: str, domain: str) -> {}:
|
|||
|
||||
calendar_post_ids: list[str] = []
|
||||
recreate_events_file: bool = False
|
||||
try:
|
||||
with open(calendar_filename, 'r', encoding='utf-8') as fp_events:
|
||||
for post_id in fp_events:
|
||||
post_id = remove_eol(post_id)
|
||||
post_filename = \
|
||||
locate_post(base_dir, nickname, domain, post_id)
|
||||
if not post_filename:
|
||||
recreate_events_file = True
|
||||
calendar_list: list[str] = \
|
||||
load_list(calendar_filename,
|
||||
'EX: get_this_weeks_events failed to read ' +
|
||||
calendar_filename)
|
||||
if calendar_list is not None:
|
||||
for post_id in calendar_list:
|
||||
post_id = remove_eol(post_id)
|
||||
post_filename = \
|
||||
locate_post(base_dir, nickname, domain, post_id)
|
||||
if not post_filename:
|
||||
recreate_events_file = True
|
||||
continue
|
||||
|
||||
post_json_object = load_json(post_filename)
|
||||
if not _is_happening_post(post_json_object):
|
||||
continue
|
||||
|
||||
post_event: list[dict] = []
|
||||
week_day_index = None
|
||||
for tag in post_json_object['object']['tag']:
|
||||
if not _is_happening_event(tag):
|
||||
continue
|
||||
|
||||
post_json_object = load_json(post_filename)
|
||||
if not _is_happening_post(post_json_object):
|
||||
# this tag is an event or a place
|
||||
if tag['type'] != 'Event':
|
||||
# tag is a place
|
||||
post_event.append(tag)
|
||||
continue
|
||||
|
||||
post_event: list[dict] = []
|
||||
week_day_index = None
|
||||
for tag in post_json_object['object']['tag']:
|
||||
if not _is_happening_event(tag):
|
||||
continue
|
||||
|
||||
# this tag is an event or a place
|
||||
if tag['type'] != 'Event':
|
||||
# tag is a place
|
||||
post_event.append(tag)
|
||||
continue
|
||||
|
||||
# tag is an event
|
||||
if not tag.get('startTime'):
|
||||
continue
|
||||
event_time = \
|
||||
date_from_string_format(tag['startTime'],
|
||||
["%Y-%m-%dT%H:%M:%S%z"])
|
||||
if now <= event_time <= end_of_week:
|
||||
week_day_index = (event_time - now).days()
|
||||
post_event.append(tag)
|
||||
|
||||
if not (post_event and week_day_index):
|
||||
# tag is an event
|
||||
if not tag.get('startTime'):
|
||||
continue
|
||||
calendar_post_ids.append(post_id)
|
||||
if not events.get(week_day_index):
|
||||
events[week_day_index]: list[dict] = []
|
||||
events[week_day_index].append(post_event)
|
||||
except OSError:
|
||||
print('EX: get_this_weeks_events failed to read ' + calendar_filename)
|
||||
event_time = \
|
||||
date_from_string_format(tag['startTime'],
|
||||
["%Y-%m-%dT%H:%M:%S%z"])
|
||||
if now <= event_time <= end_of_week:
|
||||
week_day_index = (event_time - now).days()
|
||||
post_event.append(tag)
|
||||
|
||||
if not (post_event and week_day_index):
|
||||
continue
|
||||
calendar_post_ids.append(post_id)
|
||||
if not events.get(week_day_index):
|
||||
events[week_day_index]: list[dict] = []
|
||||
events[week_day_index].append(post_event)
|
||||
|
||||
# if some posts have been deleted then regenerate the calendar file
|
||||
if recreate_events_file:
|
||||
try:
|
||||
with open(calendar_filename, 'w+',
|
||||
encoding='utf-8') as fp_calendar:
|
||||
for post_id in calendar_post_ids:
|
||||
fp_calendar.write(post_id + '\n')
|
||||
except OSError:
|
||||
print('EX: unable to recreate events file 2 ' +
|
||||
calendar_filename)
|
||||
text: str = ''
|
||||
for post_id in calendar_post_ids:
|
||||
text += post_id + '\n'
|
||||
save_string(text, calendar_filename,
|
||||
'EX: unable to recreate events file 2 ' +
|
||||
calendar_filename)
|
||||
|
||||
return events
|
||||
|
||||
|
|
@ -762,85 +760,84 @@ def get_calendar_events(base_dir: str, nickname: str, domain: str,
|
|||
|
||||
calendar_post_ids: list[str] = []
|
||||
recreate_events_file: bool = False
|
||||
try:
|
||||
with open(calendar_filename, 'r', encoding='utf-8') as fp_events:
|
||||
for post_id in fp_events:
|
||||
post_id = remove_eol(post_id)
|
||||
post_filename = \
|
||||
locate_post(base_dir, nickname, domain, post_id)
|
||||
if not post_filename:
|
||||
recreate_events_file = True
|
||||
calendar_list: list[str] = \
|
||||
load_list(calendar_filename,
|
||||
'EX: get_calendar_events failed to read ' +
|
||||
calendar_filename)
|
||||
if calendar_list is not None:
|
||||
for post_id in calendar_list:
|
||||
post_id = remove_eol(post_id)
|
||||
post_filename = \
|
||||
locate_post(base_dir, nickname, domain, post_id)
|
||||
if not post_filename:
|
||||
recreate_events_file = True
|
||||
continue
|
||||
|
||||
post_json_object = load_json(post_filename)
|
||||
if not post_json_object:
|
||||
continue
|
||||
if not _is_happening_post(post_json_object):
|
||||
continue
|
||||
if only_show_reminders:
|
||||
if not is_reminder(post_json_object):
|
||||
continue
|
||||
|
||||
post_json_object = load_json(post_filename)
|
||||
if not post_json_object:
|
||||
if post_json_object.get('object'):
|
||||
if post_json_object['object'].get('content'):
|
||||
content = post_json_object['object']['content']
|
||||
if not _event_text_match(content, text_match):
|
||||
continue
|
||||
|
||||
post_event: list[dict] = []
|
||||
day_of_month = None
|
||||
for tag in post_json_object['object']['tag']:
|
||||
if not _is_happening_event(tag):
|
||||
continue
|
||||
if not _is_happening_post(post_json_object):
|
||||
continue
|
||||
if only_show_reminders:
|
||||
if not is_reminder(post_json_object):
|
||||
continue
|
||||
|
||||
if post_json_object.get('object'):
|
||||
if post_json_object['object'].get('content'):
|
||||
content = post_json_object['object']['content']
|
||||
if not _event_text_match(content, text_match):
|
||||
continue
|
||||
|
||||
post_event: list[dict] = []
|
||||
day_of_month = None
|
||||
for tag in post_json_object['object']['tag']:
|
||||
if not _is_happening_event(tag):
|
||||
continue
|
||||
|
||||
# this tag is an event or a place
|
||||
if tag['type'] != 'Event':
|
||||
# tag is a place
|
||||
post_event.append(tag)
|
||||
continue
|
||||
|
||||
# tag is an event
|
||||
if not tag.get('startTime'):
|
||||
continue
|
||||
|
||||
# is the tag for this month?
|
||||
event_time = \
|
||||
date_from_string_format(tag['startTime'],
|
||||
["%Y-%m-%dT%H:%M:%S%z"])
|
||||
event_year = int(event_time.strftime("%Y"))
|
||||
event_month = int(event_time.strftime("%m"))
|
||||
if not (event_year == year and
|
||||
event_month == month_number):
|
||||
continue
|
||||
|
||||
event_day = int(event_time.strftime("%d"))
|
||||
day_of_month = str(event_day)
|
||||
if '#statuses#' in post_id:
|
||||
tag['post_id'] = post_id.split('#statuses#')[1]
|
||||
tag['id'] = post_id.replace('#', '/')
|
||||
tag['sender'] = post_id.split('#statuses#')[0]
|
||||
tag['sender'] = tag['sender'].replace('#', '/')
|
||||
# this tag is an event or a place
|
||||
if tag['type'] != 'Event':
|
||||
# tag is a place
|
||||
post_event.append(tag)
|
||||
|
||||
if not (post_event and day_of_month):
|
||||
continue
|
||||
calendar_post_ids.append(post_id)
|
||||
if not events.get(day_of_month):
|
||||
events[day_of_month]: list[dict] = []
|
||||
events[day_of_month].append(post_event)
|
||||
except OSError:
|
||||
print('EX: get_calendar_events failed to read ' + calendar_filename)
|
||||
|
||||
# tag is an event
|
||||
if not tag.get('startTime'):
|
||||
continue
|
||||
|
||||
# is the tag for this month?
|
||||
event_time = \
|
||||
date_from_string_format(tag['startTime'],
|
||||
["%Y-%m-%dT%H:%M:%S%z"])
|
||||
event_year = int(event_time.strftime("%Y"))
|
||||
event_month = int(event_time.strftime("%m"))
|
||||
if not (event_year == year and
|
||||
event_month == month_number):
|
||||
continue
|
||||
|
||||
event_day = int(event_time.strftime("%d"))
|
||||
day_of_month = str(event_day)
|
||||
if '#statuses#' in post_id:
|
||||
tag['post_id'] = post_id.split('#statuses#')[1]
|
||||
tag['id'] = post_id.replace('#', '/')
|
||||
tag['sender'] = post_id.split('#statuses#')[0]
|
||||
tag['sender'] = tag['sender'].replace('#', '/')
|
||||
post_event.append(tag)
|
||||
|
||||
if not (post_event and day_of_month):
|
||||
continue
|
||||
calendar_post_ids.append(post_id)
|
||||
if not events.get(day_of_month):
|
||||
events[day_of_month]: list[dict] = []
|
||||
events[day_of_month].append(post_event)
|
||||
|
||||
# if some posts have been deleted then regenerate the calendar file
|
||||
if recreate_events_file:
|
||||
try:
|
||||
with open(calendar_filename, 'w+',
|
||||
encoding='utf-8') as fp_calendar:
|
||||
for post_id in calendar_post_ids:
|
||||
fp_calendar.write(post_id + '\n')
|
||||
except OSError:
|
||||
print('EX: unable to recreate events file 3 ' +
|
||||
calendar_filename)
|
||||
text: str = ''
|
||||
for post_id in calendar_post_ids:
|
||||
text += post_id + '\n'
|
||||
save_string(text, calendar_filename,
|
||||
'EX: unable to recreate events file 3 ' +
|
||||
calendar_filename)
|
||||
|
||||
return events
|
||||
|
||||
|
|
@ -869,15 +866,14 @@ def remove_calendar_event(base_dir: str, nickname: str, domain: str,
|
|||
return
|
||||
lines = lines_str.split('\n')
|
||||
print('Removing calendar event: ' + message_id)
|
||||
try:
|
||||
with open(calendar_filename, 'w+', encoding='utf-8') as fp_cal:
|
||||
for line in lines:
|
||||
if message_id in line:
|
||||
continue
|
||||
fp_cal.write(line + '\n')
|
||||
except OSError:
|
||||
print('EX: unable to remove calendar event ' +
|
||||
calendar_filename)
|
||||
text: str = ''
|
||||
for line in lines:
|
||||
if message_id in line:
|
||||
continue
|
||||
text += line + '\n'
|
||||
save_string(text, calendar_filename,
|
||||
'EX: unable to remove calendar event ' +
|
||||
calendar_filename)
|
||||
|
||||
|
||||
def _dav_decode_token(token: str) -> (int, int, str):
|
||||
|
|
|
|||
21
keys.py
21
keys.py
|
|
@ -8,6 +8,7 @@ __status__ = "Production"
|
|||
__module_group__ = "ActivityPub"
|
||||
|
||||
import os
|
||||
from data import load_string
|
||||
|
||||
|
||||
def _get_local_private_key(base_dir: str, nickname: str, domain: str) -> str:
|
||||
|
|
@ -19,11 +20,11 @@ def _get_local_private_key(base_dir: str, nickname: str, domain: str) -> str:
|
|||
key_filename = base_dir + '/keys/private/' + handle.lower() + '.key'
|
||||
if not os.path.isfile(key_filename):
|
||||
return None
|
||||
try:
|
||||
with open(key_filename, 'r', encoding='utf-8') as fp_pem:
|
||||
return fp_pem.read()
|
||||
except OSError:
|
||||
print('EX: _get_local_private_key unable to read ' + key_filename)
|
||||
text = load_string(key_filename,
|
||||
'EX: _get_local_private_key unable to read ' +
|
||||
key_filename)
|
||||
if text is not None:
|
||||
return text
|
||||
return None
|
||||
|
||||
|
||||
|
|
@ -36,11 +37,11 @@ def _get_local_public_key(base_dir: str, nickname: str, domain: str) -> str:
|
|||
key_filename = base_dir + '/keys/public/' + handle.lower() + '.key'
|
||||
if not os.path.isfile(key_filename):
|
||||
return None
|
||||
try:
|
||||
with open(key_filename, 'r', encoding='utf-8') as fp_pem:
|
||||
return fp_pem.read()
|
||||
except OSError:
|
||||
print('EX: _get_local_public_key unable to read ' + key_filename)
|
||||
text = load_string(key_filename,
|
||||
'EX: _get_local_public_key unable to read ' +
|
||||
key_filename)
|
||||
if text is not None:
|
||||
return text
|
||||
return None
|
||||
|
||||
|
||||
|
|
|
|||
24
languages.py
24
languages.py
|
|
@ -20,6 +20,8 @@ from utils import get_config_param
|
|||
from utils import local_actor_url
|
||||
from utils import resembles_url
|
||||
from cache import get_person_from_cache
|
||||
from data import load_string
|
||||
from data import save_string
|
||||
|
||||
|
||||
def get_actor_languages(actor_json: {}) -> str:
|
||||
|
|
@ -354,13 +356,9 @@ def set_default_post_language(base_dir: str, nickname: str, domain: str,
|
|||
"""
|
||||
default_post_language_filename = \
|
||||
acct_dir(base_dir, nickname, domain) + '/.new_post_language'
|
||||
try:
|
||||
with open(default_post_language_filename, 'w+',
|
||||
encoding='utf-8') as fp_lang:
|
||||
fp_lang.write(language)
|
||||
except OSError:
|
||||
print('EX: Unable to write default post language ' +
|
||||
default_post_language_filename)
|
||||
save_string(language, default_post_language_filename,
|
||||
'EX: Unable to write default post language ' +
|
||||
default_post_language_filename)
|
||||
|
||||
|
||||
def load_default_post_languages(base_dir: str) -> {}:
|
||||
|
|
@ -379,13 +377,11 @@ def load_default_post_languages(base_dir: str) -> {}:
|
|||
acct_dir(base_dir, nickname, domain) + '/.new_post_language'
|
||||
if not os.path.isfile(default_post_language_filename):
|
||||
continue
|
||||
try:
|
||||
with open(default_post_language_filename, 'r',
|
||||
encoding='utf-8') as fp_lang:
|
||||
result[nickname] = fp_lang.read()
|
||||
except OSError:
|
||||
print('EX: Unable to read default post language ' +
|
||||
default_post_language_filename)
|
||||
text = load_string(default_post_language_filename,
|
||||
'EX: Unable to read default post language ' +
|
||||
default_post_language_filename)
|
||||
if text:
|
||||
result[nickname] = text
|
||||
break
|
||||
return result
|
||||
|
||||
|
|
|
|||
273
manualapprove.py
273
manualapprove.py
|
|
@ -24,6 +24,10 @@ from utils import is_yggdrasil_address
|
|||
from threads import thread_with_trace
|
||||
from threads import begin_thread
|
||||
from session import create_session
|
||||
from data import save_string
|
||||
from data import load_string
|
||||
from data import append_string
|
||||
from data import load_list
|
||||
|
||||
|
||||
def manual_deny_follow_request2(session, session_onion, session_i2p,
|
||||
|
|
@ -60,13 +64,9 @@ def manual_deny_follow_request2(session, session_onion, session_i2p,
|
|||
remove_from_follow_requests(base_dir, nickname, domain, deny_handle, debug)
|
||||
|
||||
# Store rejected follows
|
||||
try:
|
||||
with open(rejected_follows_filename, 'a+',
|
||||
encoding='utf-8') as fp_rejects:
|
||||
fp_rejects.write(deny_handle + '\n')
|
||||
except OSError:
|
||||
print('EX: manual_deny_follow_request2 unable to append ' +
|
||||
rejected_follows_filename)
|
||||
append_string(deny_handle + '\n', rejected_follows_filename,
|
||||
'EX: manual_deny_follow_request2 unable to append ' +
|
||||
rejected_follows_filename)
|
||||
|
||||
deny_nickname = deny_handle.split('@')[0]
|
||||
deny_domain = remove_eol(deny_handle.split('@')[1])
|
||||
|
|
@ -144,22 +144,14 @@ def _approve_follower_handle(account_dir: str, approve_handle: str) -> None:
|
|||
approved_filename = account_dir + '/approved.txt'
|
||||
if os.path.isfile(approved_filename):
|
||||
if not text_in_file(approve_handle, approved_filename):
|
||||
try:
|
||||
with open(approved_filename, 'a+',
|
||||
encoding='utf-8') as fp_approved:
|
||||
fp_approved.write(approve_handle + '\n')
|
||||
except OSError:
|
||||
print('EX: _approve_follower_handle unable to append ' +
|
||||
approved_filename)
|
||||
append_string(approve_handle + '\n', approved_filename,
|
||||
'EX: _approve_follower_handle unable to append ' +
|
||||
approved_filename)
|
||||
return
|
||||
|
||||
try:
|
||||
with open(approved_filename, 'w+',
|
||||
encoding='utf-8') as fp_approved:
|
||||
fp_approved.write(approve_handle + '\n')
|
||||
except OSError:
|
||||
print('EX: _approve_follower_handle unable to write ' +
|
||||
approved_filename)
|
||||
save_string(approve_handle + '\n', approved_filename,
|
||||
'EX: _approve_follower_handle unable to write ' +
|
||||
approved_filename)
|
||||
|
||||
|
||||
def manual_approve_follow_request(session, session_onion, session_i2p,
|
||||
|
|
@ -194,12 +186,12 @@ def manual_approve_follow_request(session, session_onion, session_i2p,
|
|||
|
||||
# is the handle in the requests file?
|
||||
approve_follows_str: str = ''
|
||||
try:
|
||||
with open(approve_follows_filename, 'r', encoding='utf-8') as fp_foll:
|
||||
approve_follows_str = fp_foll.read()
|
||||
except OSError:
|
||||
print('EX: manual_approve_follow_request unable to read ' +
|
||||
approve_follows_filename)
|
||||
approve_follows_str2: str = \
|
||||
load_string(approve_follows_filename,
|
||||
'EX: manual_approve_follow_request unable to read ' +
|
||||
approve_follows_filename)
|
||||
if approve_follows_str2:
|
||||
approve_follows_str = approve_follows_str2
|
||||
exists: bool = False
|
||||
approve_handle_full = approve_handle
|
||||
if approve_handle in approve_follows_str:
|
||||
|
|
@ -234,118 +226,118 @@ def manual_approve_follow_request(session, session_onion, session_i2p,
|
|||
'" ' + approve_follows_filename)
|
||||
return
|
||||
|
||||
try:
|
||||
with open(approve_follows_filename + '.new', 'w+',
|
||||
encoding='utf-8') as fp_approve_new:
|
||||
update_approved_followers: bool = False
|
||||
follow_activity_filename = None
|
||||
with open(approve_follows_filename, 'r',
|
||||
encoding='utf-8') as fp_approve:
|
||||
for handle_of_follow_requester in fp_approve:
|
||||
# is this the approved follow?
|
||||
appr_handl = approve_handle_full
|
||||
if not handle_of_follow_requester.startswith(appr_handl):
|
||||
# this isn't the approved follow so it will remain
|
||||
# in the requests file
|
||||
fp_approve_new.write(handle_of_follow_requester)
|
||||
continue
|
||||
approve_follows_text: str = ''
|
||||
update_approved_followers: bool = False
|
||||
follow_activity_filename = None
|
||||
approve_follows_list: list[str] = \
|
||||
load_list(approve_follows_filename,
|
||||
'EX: manual_approve_follow_request ' +
|
||||
'unable to write ' + approve_follows_filename +
|
||||
'.new [ex]')
|
||||
if approve_follows_list is not None:
|
||||
for handle_of_follow_requester in approve_follows_list:
|
||||
# is this the approved follow?
|
||||
appr_handl = approve_handle_full
|
||||
if not handle_of_follow_requester.startswith(appr_handl):
|
||||
# this isn't the approved follow so it will remain
|
||||
# in the requests file
|
||||
approve_follows_text += handle_of_follow_requester
|
||||
continue
|
||||
|
||||
handle_of_follow_requester = \
|
||||
remove_eol(handle_of_follow_requester)
|
||||
handle_of_follow_requester = \
|
||||
handle_of_follow_requester.replace('\r', '')
|
||||
port2 = port
|
||||
if ':' in handle_of_follow_requester:
|
||||
port2 = \
|
||||
get_port_from_domain(handle_of_follow_requester)
|
||||
requests_dir = account_dir + '/requests'
|
||||
follow_activity_filename = \
|
||||
requests_dir + '/' + \
|
||||
handle_of_follow_requester + '.follow'
|
||||
if not os.path.isfile(follow_activity_filename):
|
||||
update_approved_followers = True
|
||||
continue
|
||||
follow_json = load_json(follow_activity_filename)
|
||||
if not follow_json:
|
||||
update_approved_followers = True
|
||||
continue
|
||||
approve_nickname = approve_handle.split('@')[0]
|
||||
approve_domain = approve_handle.split('@')[1]
|
||||
approve_domain = remove_eol(approve_domain)
|
||||
approve_domain = approve_domain.replace('\r', '')
|
||||
approve_port = port2
|
||||
if ':' in approve_domain:
|
||||
approve_port = get_port_from_domain(approve_domain)
|
||||
approve_domain = remove_domain_port(approve_domain)
|
||||
handle_of_follow_requester = \
|
||||
remove_eol(handle_of_follow_requester)
|
||||
handle_of_follow_requester = \
|
||||
handle_of_follow_requester.replace('\r', '')
|
||||
port2 = port
|
||||
if ':' in handle_of_follow_requester:
|
||||
port2 = get_port_from_domain(handle_of_follow_requester)
|
||||
requests_dir = account_dir + '/requests'
|
||||
follow_activity_filename = \
|
||||
requests_dir + '/' + handle_of_follow_requester + '.follow'
|
||||
if not os.path.isfile(follow_activity_filename):
|
||||
update_approved_followers = True
|
||||
continue
|
||||
follow_json = load_json(follow_activity_filename)
|
||||
if not follow_json:
|
||||
update_approved_followers = True
|
||||
continue
|
||||
approve_nickname = approve_handle.split('@')[0]
|
||||
approve_domain = approve_handle.split('@')[1]
|
||||
approve_domain = remove_eol(approve_domain)
|
||||
approve_domain = approve_domain.replace('\r', '')
|
||||
approve_port = port2
|
||||
if ':' in approve_domain:
|
||||
approve_port = get_port_from_domain(approve_domain)
|
||||
approve_domain = remove_domain_port(approve_domain)
|
||||
|
||||
curr_domain = domain
|
||||
curr_port = port
|
||||
curr_session = session
|
||||
curr_http_prefix = http_prefix
|
||||
curr_proxy_type = proxy_type
|
||||
if onion_domain and \
|
||||
not curr_domain.endswith('.onion') and \
|
||||
approve_domain.endswith('.onion'):
|
||||
curr_domain = onion_domain
|
||||
curr_port = 80
|
||||
approve_port = 80
|
||||
curr_session = session_onion
|
||||
curr_http_prefix = 'http'
|
||||
curr_proxy_type = 'tor'
|
||||
elif (i2p_domain and
|
||||
not curr_domain.endswith('.i2p') and
|
||||
approve_domain.endswith('.i2p')):
|
||||
curr_domain = i2p_domain
|
||||
curr_port = 80
|
||||
approve_port = 80
|
||||
curr_session = session_i2p
|
||||
curr_http_prefix = 'http'
|
||||
curr_proxy_type = 'i2p'
|
||||
elif (yggdrasil_domain and
|
||||
not is_yggdrasil_address(curr_domain) and
|
||||
is_yggdrasil_address(approve_domain)):
|
||||
curr_domain = yggdrasil_domain
|
||||
curr_port = 80
|
||||
approve_port = 80
|
||||
curr_session = session_yggdrasil
|
||||
curr_http_prefix = 'http'
|
||||
curr_proxy_type = 'yggdrasil'
|
||||
curr_domain = domain
|
||||
curr_port = port
|
||||
curr_session = session
|
||||
curr_http_prefix = http_prefix
|
||||
curr_proxy_type = proxy_type
|
||||
if onion_domain and \
|
||||
not curr_domain.endswith('.onion') and \
|
||||
approve_domain.endswith('.onion'):
|
||||
curr_domain = onion_domain
|
||||
curr_port = 80
|
||||
approve_port = 80
|
||||
curr_session = session_onion
|
||||
curr_http_prefix = 'http'
|
||||
curr_proxy_type = 'tor'
|
||||
elif (i2p_domain and
|
||||
not curr_domain.endswith('.i2p') and
|
||||
approve_domain.endswith('.i2p')):
|
||||
curr_domain = i2p_domain
|
||||
curr_port = 80
|
||||
approve_port = 80
|
||||
curr_session = session_i2p
|
||||
curr_http_prefix = 'http'
|
||||
curr_proxy_type = 'i2p'
|
||||
elif (yggdrasil_domain and
|
||||
not is_yggdrasil_address(curr_domain) and
|
||||
is_yggdrasil_address(approve_domain)):
|
||||
curr_domain = yggdrasil_domain
|
||||
curr_port = 80
|
||||
approve_port = 80
|
||||
curr_session = session_yggdrasil
|
||||
curr_http_prefix = 'http'
|
||||
curr_proxy_type = 'yggdrasil'
|
||||
|
||||
if not curr_session:
|
||||
curr_session = create_session(curr_proxy_type)
|
||||
if not curr_session:
|
||||
curr_session = create_session(curr_proxy_type)
|
||||
|
||||
print('Manual follow accept: Sending Accept for ' +
|
||||
handle + ' follow request from ' +
|
||||
approve_nickname + '@' + approve_domain)
|
||||
actor_url = get_actor_from_post(follow_json)
|
||||
followed_account_accepts(curr_session, base_dir,
|
||||
curr_http_prefix,
|
||||
nickname,
|
||||
curr_domain, curr_port,
|
||||
approve_nickname,
|
||||
approve_domain,
|
||||
approve_port,
|
||||
actor_url,
|
||||
federation_list,
|
||||
follow_json,
|
||||
send_threads, post_log,
|
||||
cached_webfingers,
|
||||
person_cache,
|
||||
debug,
|
||||
project_version, False,
|
||||
signing_priv_key_pem,
|
||||
domain,
|
||||
onion_domain,
|
||||
i2p_domain,
|
||||
yggdrasil_domain,
|
||||
followers_sync_cache,
|
||||
sites_unavailable,
|
||||
system_language,
|
||||
mitm_servers)
|
||||
update_approved_followers = True
|
||||
except OSError as exc:
|
||||
print('EX: manual_approve_follow_request unable to write ' +
|
||||
approve_follows_filename + '.new ' + str(exc))
|
||||
print('Manual follow accept: Sending Accept for ' +
|
||||
handle + ' follow request from ' +
|
||||
approve_nickname + '@' + approve_domain)
|
||||
actor_url = get_actor_from_post(follow_json)
|
||||
followed_account_accepts(curr_session, base_dir,
|
||||
curr_http_prefix,
|
||||
nickname,
|
||||
curr_domain, curr_port,
|
||||
approve_nickname,
|
||||
approve_domain,
|
||||
approve_port,
|
||||
actor_url,
|
||||
federation_list,
|
||||
follow_json,
|
||||
send_threads, post_log,
|
||||
cached_webfingers,
|
||||
person_cache,
|
||||
debug,
|
||||
project_version, False,
|
||||
signing_priv_key_pem,
|
||||
domain,
|
||||
onion_domain,
|
||||
i2p_domain,
|
||||
yggdrasil_domain,
|
||||
followers_sync_cache,
|
||||
sites_unavailable,
|
||||
system_language,
|
||||
mitm_servers)
|
||||
update_approved_followers = True
|
||||
save_string(approve_follows_text, approve_follows_filename + '.new',
|
||||
'EX: manual_approve_follow_request unable to write ' +
|
||||
approve_follows_filename + '.new [ex]')
|
||||
|
||||
followers_filename = account_dir + '/followers.txt'
|
||||
if update_approved_followers:
|
||||
|
|
@ -370,13 +362,10 @@ def manual_approve_follow_request(session, session_onion, session_i2p,
|
|||
else:
|
||||
print('Manual follow accept: first follower accepted for ' +
|
||||
handle + ' is ' + approve_handle_full)
|
||||
try:
|
||||
with open(followers_filename, 'w+',
|
||||
encoding='utf-8') as fp_followers:
|
||||
fp_followers.write(approve_handle_full + '\n')
|
||||
except OSError:
|
||||
print('EX: manual_approve_follow_request unable to write ' +
|
||||
followers_filename)
|
||||
save_string(approve_handle_full + '\n',
|
||||
followers_filename,
|
||||
'EX: manual_approve_follow_request unable to write ' +
|
||||
followers_filename)
|
||||
|
||||
# only update the follow requests file if the follow is confirmed to be
|
||||
# in followers.txt
|
||||
|
|
|
|||
Loading…
Reference in New Issue