2
0
mirror of https://github.com/ComradCollective/Comrad synced 2024-11-17 21:25:37 +00:00
Comrad/komrade/backend/switchboard.py

55 lines
1.8 KiB
Python
Raw Normal View History

2020-09-05 21:11:42 +00:00
# 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 *
2020-09-06 06:50:23 +00:00
from komrade.backend import *
2020-09-05 21:11:42 +00:00
# external imports
from flask import Flask, request, jsonify
from flask_classful import FlaskView
class TheSwitchboard(FlaskView, Logger):
2020-09-06 20:26:55 +00:00
default_methods = ['GET']
excluded_methods = ['phone','op','send']
2020-09-08 15:36:01 +00:00
@property
def op(self):
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
2020-09-09 15:14:51 +00:00
def get(self,data_b64_str_esc):
self.log('Incoming call!:',data_b64_str_esc)
if not data_b64_str_esc:
2020-09-05 22:41:43 +00:00
self.log('empty request!')
return OPERATOR_INTERCEPT_MESSAGE
2020-09-09 15:14:51 +00:00
2020-09-05 22:51:34 +00:00
# unenescape
2020-09-09 15:14:51 +00:00
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
2020-09-09 21:38:32 +00:00
resp_data_b = self.op.answer_phone(data_b)
2020-09-09 15:14:51 +00:00
# decode to str
2020-09-09 20:23:24 +00:00
resp_data_b64 = b64encode(resp_data_b)
2020-09-09 20:25:13 +00:00
resp_data_b64_str = resp_data_b64.decode()
2020-09-09 15:14:51 +00:00
# return as str
2020-09-09 20:25:13 +00:00
return resp_data_b64_str
2020-09-05 21:11:42 +00:00
def run_forever(port='8080'):
2020-09-09 14:39:51 +00:00
global OPERATOR,TELEPHONE,TELEPHONE_KEYCHAIN,OPERATOR_KEYCHAIN,OMEGA_KEY
OPERATOR_KEYCHAIN,TELEPHONE_KEYCHAIN,OMEGA_KEY=connect_phonelines()
2020-09-07 17:53:16 +00:00
TELEPHONE = TheTelephone()
OPERATOR = TheOperator()
2020-09-05 21:11:42 +00:00
app = Flask(__name__)
2020-09-05 21:47:29 +00:00
TheSwitchboard.register(app, route_base='/op/', route_prefix=None)
2020-09-07 18:26:19 +00:00
app.run(debug=False, port=port, host='0.0.0.0')