2
0
mirror of https://github.com/ComradCollective/Comrad synced 2024-11-16 00:12:48 +00:00
Comrad/comrad/backend/switchboard.py

66 lines
2.0 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__),'..')),'..')))
2020-09-29 13:09:56 +00:00
from comrad import *
from comrad.backend import *
2020-09-13 06:15:38 +00:00
from getpass import getpass
2020-09-05 21:11:42 +00:00
# external imports
from flask import Flask, request, jsonify
from flask_classful import FlaskView
2020-09-13 06:15:05 +00:00
OP_PASS = None
2020-09-05 21:11:42 +00:00
class TheSwitchboard(FlaskView, Logger):
2020-09-06 20:26:55 +00:00
default_methods = ['GET']
2020-09-12 09:27:00 +00:00
excluded_methods = ['phone','op','send','printt','log','status']
2020-09-08 15:36:01 +00:00
@property
def op(self):
2020-09-13 06:15:05 +00:00
global OP_PASS
2020-09-29 13:09:56 +00:00
from comrad.backend.the_operator import TheOperator
2020-09-08 15:36:01 +00:00
if type(self)==TheOperator: return self
if hasattr(self,'_op'): return self._op
global OPERATOR,OPERATOR_KEYCHAIN
if OPERATOR: return OPERATOR
2020-09-13 07:00:21 +00:00
self._op=OPERATOR=TheOperator()
2020-09-08 15:36:01 +00:00
return OPERATOR
2020-09-09 15:14:51 +00:00
def get(self,data_b64_str_esc):
2020-09-13 07:34:01 +00:00
clear_screen()
2020-09-29 13:09:56 +00:00
from comrad.cli.artcode import ART_OLDPHONE4
2020-09-13 08:37:22 +00:00
# self.log(f'Incoming call!: {data_b64_str_esc}')
2020-09-09 15:14:51 +00:00
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-13 07:00:21 +00:00
# global OP_PASS
# OP_PASS = getpass('@op pass? ')
2020-09-07 17:53:16 +00:00
TELEPHONE = TheTelephone()
2020-09-13 07:00:21 +00:00
OPERATOR = TheOperator()
2020-09-13 07:34:01 +00:00
# print(OPERATOR,'!?',OPERATOR.keychain())
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-14 13:06:50 +00:00
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)