2
0
mirror of https://github.com/ComradCollective/Comrad synced 2024-11-09 13:10:44 +00:00
Comrad/komrade/backend/switchboard.py
quadrismegistus 82903af78d bin
2020-09-14 14:06:50 +01:00

66 lines
2.0 KiB
Python

# internal imports
import os,sys; sys.path.append(os.path.abspath(os.path.join(os.path.abspath(os.path.join(os.path.dirname(__file__),'..')),'..')))
from komrade import *
from komrade.backend import *
from getpass import getpass
# external imports
from flask import Flask, request, jsonify
from flask_classful import FlaskView
OP_PASS = None
class TheSwitchboard(FlaskView, Logger):
default_methods = ['GET']
excluded_methods = ['phone','op','send','printt','log','status']
@property
def op(self):
global OP_PASS
from komrade.backend.the_operator import TheOperator
if type(self)==TheOperator: return self
if hasattr(self,'_op'): return self._op
global OPERATOR,OPERATOR_KEYCHAIN
if OPERATOR: return OPERATOR
self._op=OPERATOR=TheOperator()
return OPERATOR
def get(self,data_b64_str_esc):
clear_screen()
from komrade.cli.artcode import ART_OLDPHONE4
# self.log(f'Incoming call!: {data_b64_str_esc}')
if not data_b64_str_esc:
self.log('empty request!')
return OPERATOR_INTERCEPT_MESSAGE
# unenescape
data_b64_str = data_b64_str_esc.replace('_','/')
# encode to binary
data_b64 = data_b64_str.encode()
data_b = b64decode(data_b64)
# ask operator to answer phone and request
resp_data_b = self.op.answer_phone(data_b)
# decode to str
resp_data_b64 = b64encode(resp_data_b)
resp_data_b64_str = resp_data_b64.decode()
# return as str
return resp_data_b64_str
def run_forever(port='8080'):
# global OP_PASS
# OP_PASS = getpass('@op pass? ')
TELEPHONE = TheTelephone()
OPERATOR = TheOperator()
# print(OPERATOR,'!?',OPERATOR.keychain())
app = Flask(__name__)
TheSwitchboard.register(app, route_base='/op/', route_prefix=None)
app.run(debug=True, port=port, host='0.0.0.0')
if __name__ == '__main__':
port = '8080' if len(sys.argv)<2 or not sys.argv[1].isdigit() else sys.argv[1]
run_forever(port = port)