Compare commits

..

No commits in common. "af75b864df450cd949215f3a042f8e4889e05698" and "06c4d75f277217b6026d49ce8da180954ecefd8d" have entirely different histories.

View file

@ -19,7 +19,7 @@ try:
import threading import threading
from time import time, localtime, strftime, sleep from time import time, localtime, strftime, sleep
import configparser import configparser
except Exception as e: except ImportException as e:
print('Import error: {}'.format(e)) print('Import error: {}'.format(e))
@ -117,17 +117,6 @@ def display_peercert(cert):
elif len(cert) == 0: elif len(cert) == 0:
logging.debug('Peer certificate was not valid') logging.debug('Peer certificate was not valid')
else: else:
logging.debug('--- Peer cert ---')
for key in cert.keys():
if key in ('subject', 'serialNumber', 'notBefore', 'notAfter'):
if key == 'subject':
logging.debug(f'Subject: {cert[key]}')
for i in cert[key]:
logging.debug(f'{i[0][0]}: {i[0][1]}')
else:
logging.debug(f'{key}: {cert[key]}')
logging.debug('-----------------')
try:
logging.debug('Peer certificate commonName: {}'.format( logging.debug('Peer certificate commonName: {}'.format(
cert['subject'][5][0][1])) cert['subject'][5][0][1]))
logging.debug('Peer certificate serialNumber: {}'.format( logging.debug('Peer certificate serialNumber: {}'.format(
@ -136,9 +125,6 @@ def display_peercert(cert):
cert['notBefore'])) cert['notBefore']))
logging.debug('Peer certificate notAfter: {}'.format( logging.debug('Peer certificate notAfter: {}'.format(
cert['notAfter'])) cert['notAfter']))
except Exception as error:
logging.debug('Cert display failed')
logging.debug(error)
def receive_buffer_is_valid(raw_data): def receive_buffer_is_valid(raw_data):
''' '''
@ -186,7 +172,7 @@ def read_api(api):
return False return False
return api_json_data return api_json_data
def change_status(status, timestamp, api_template, filename): def change_status(status, timestamp, filename):
''' '''
Write the new status together with a timestamp into the Space API JSON. Write the new status together with a timestamp into the Space API JSON.
param 1: byte object param 1: byte object
@ -194,15 +180,19 @@ def change_status(status, timestamp, api_template, filename):
return: boolean return: boolean
''' '''
logging.debug('Change status API') logging.debug('Change status API')
# todo: use walrus operator := when migrating to python >= 3.8
data = read_api(filename)
if data is False:
return False
if os.access(filename, os.W_OK): if os.access(filename, os.W_OK):
logging.debug('API file is writable') logging.debug('API file is writable')
with open(filename, 'w') as api_file: with open(filename, 'w') as api_file:
logging.debug('API file successfull writable opened') logging.debug('API file successfull writable opened')
api_template["state"]["open"] = status data["state"]["open"] = status
api_template["state"]["lastchange"] = timestamp data["state"]["lastchange"] = timestamp
try: try:
# json.dump(data, api_file, indent=4) json.dump(data, api_file, indent=4)
json.dump(api_template, api_file, indent=4)
except Exception as e: except Exception as e:
logging.error('Failed to change API file') logging.error('Failed to change API file')
logging.error('{}'.format(e)) logging.error('{}'.format(e))
@ -345,45 +335,6 @@ def main():
'token': 'aaaaa-bbbbb-ccccc-ddddd-eeeee' 'token': 'aaaaa-bbbbb-ccccc-ddddd-eeeee'
} }
} }
api_template = {
"api": "0.13",
"space": "Krautspace",
"url": "https://kraut.space",
"logo": "https://status.krautspace.de/images/krautspace_pixelbanner.png",
"location": {
"address": "Hackspace Jena e. V., Krautgasse 26, 07743 Jena, Germany",
"lat": 50.9292,
"lon": 11.5826
},
"state": {
"open": False,
"lastchange": 1563499131,
"icon": {
"open":"https://status.krautspace.de/images/status-open.png",
"closed":"https://status.krautspace.de/images/status-closed.png"
}
},
"feeds": {
"calendar": {
"type": "ical",
"url": "https://cloud.kraut.space/remote.php/dav/public-calendars/2EkPGt3PF6WwYsA3?export"
}
},
"contact": {
"mastodon": "@krautspace@chaos.social",
"matrix": "#krautchan:matrix.kraut.space",
"email": "office@krautspace.de"
},
"issue_report_channels": [
"matrix",
"email"
],
"projects": [
"https://git.kraut.space/Krautspace"
]
}
logging.info('Try to read config file') logging.info('Try to read config file')
configfile = './apistatusd.conf' configfile = './apistatusd.conf'
config = configparser.ConfigParser() config = configparser.ConfigParser()
@ -451,7 +402,7 @@ def main():
raw_data = Connection.recv(1) raw_data = Connection.recv(1)
if receive_buffer_is_valid(raw_data) is True: if receive_buffer_is_valid(raw_data) is True:
status, timestamp = get_status_and_time(raw_data) status, timestamp = get_status_and_time(raw_data)
if change_status(status, timestamp, api_template, config['api']['api']) is True: if change_status(status, timestamp, config['api']['api']) is True:
answer = raw_data answer = raw_data
if config['mastodon']['send'].lower() == 'true': if config['mastodon']['send'].lower() == 'true':
logging.debug('Toot is set to true') logging.debug('Toot is set to true')