|
|
|
#!/usr/bin/env python3
|
|
|
|
# This program runs a flask daemon to provide communications with flowroute n stuff.
|
|
|
|
import time
|
|
|
|
import datetime
|
|
|
|
import pprint
|
|
|
|
import configparser
|
|
|
|
import json
|
|
|
|
import re
|
|
|
|
import flask
|
|
|
|
#from authlib.client import OAuth2Session
|
|
|
|
#import google.oauth2.credentials
|
|
|
|
#import googleapiclient.discovery
|
|
|
|
|
|
|
|
import appdb, appsms, app_settings, app_auth
|
|
|
|
#import google_auth
|
|
|
|
import callback_sms
|
|
|
|
|
|
|
|
config = configparser.ConfigParser()
|
|
|
|
config.read('config.ini')
|
|
|
|
app_debug = config.get("app","debug")
|
|
|
|
|
|
|
|
app = flask.Flask(__name__)
|
|
|
|
app.secret_key = config.get("auth","FN_FLASK_SECRET_KEY")
|
|
|
|
|
|
|
|
#app.register_blueprint(google_auth.app)
|
|
|
|
app.register_blueprint(callback_sms.app)
|
|
|
|
app.register_blueprint(app_settings.app)
|
|
|
|
app.register_blueprint(app_auth.app)
|
|
|
|
|
|
|
|
loginMsg = "You are not logged in."
|
|
|
|
|
|
|
|
if app_debug == '1':
|
|
|
|
app.debug = True
|
|
|
|
else:
|
|
|
|
app.debug = False
|
|
|
|
|
|
|
|
@app.route('/')
|
|
|
|
def index():
|
|
|
|
'''This is the root index. If not logged in it displays homepage.html'''
|
|
|
|
if flask.session.get('loginid'):
|
|
|
|
user_info = appdb.getUserInfo(flask.session['email'],flask.session['loginid'])
|
|
|
|
loggedin = True
|
|
|
|
|
|
|
|
rows = appdb.getDIDsbyAccount(flask.session['account_id'])
|
|
|
|
return flask.render_template('index.html',
|
|
|
|
name = user_info[2],
|
|
|
|
picture = user_info[8],
|
|
|
|
dids = rows,
|
|
|
|
loggedin = True)
|
|
|
|
else:
|
|
|
|
return flask.render_template('homepage.html', loggedin = False)
|
|
|
|
|
|
|
|
@app.route('/landing')
|
|
|
|
def landingPage():
|
|
|
|
'''This renders the landing page'''
|
|
|
|
#user_info = google_auth.get_user_info()
|
|
|
|
if flask.session['loginid']:
|
|
|
|
user_info = appdb.getUserInfo(flask.session['email'],flask.session['loginid'])
|
|
|
|
#Going to replace google_auth with a local authentication.
|
|
|
|
if app_auth.is_logged_in():
|
|
|
|
loggedin = True
|
|
|
|
else:
|
|
|
|
loggedin = False
|
|
|
|
return flask.render_template('landing.html', user_info = user_info, loggedin = loggedin)
|
|
|
|
|
|
|
|
@app.route('/single/<int:number>', methods=['GET'])
|
|
|
|
def manageSingleSMS(number):
|
|
|
|
'''This renders a view for a single SMS number and its associated messages'''
|
|
|
|
if not app_auth.is_logged_in():
|
|
|
|
return flask.render_template('deny.html',denymsg = loginMsg, loggedin = False)
|
|
|
|
if flask.session['loginid']:
|
|
|
|
user_info = appdb.getUserInfo(flask.session['email'],flask.session['loginid'])
|
|
|
|
#refreshtoken = google_auth.getRefreshToken()
|
|
|
|
#googleid = google_auth.getGoogleId()
|
|
|
|
#userid = appdb.getUserIDfromGoogleID(googleid)
|
|
|
|
#result = appdb.authIdforDID(userid,number)
|
|
|
|
|
|
|
|
prettynum = appsms.prettyPhone(number)
|
|
|
|
if appdb.validateFrom(int(number)) and result:
|
|
|
|
return flask.render_template('single.html',srcnumber = number, prettynum = prettynum, loggedin = True)
|
|
|
|
else:
|
|
|
|
return flask.render_template('notvalid.html', srcnumber = number, prettynum = prettynum, loggedin = True)
|
|
|
|
|
|
|
|
#Gotta redo this logic
|
|
|
|
@app.route('/getNumber/<int:number>',methods=['GET'])
|
|
|
|
def getNumMessages(number):
|
|
|
|
'''Return the messages from a single DID in json form'''
|
|
|
|
#This gets the mssages based on the provided from or two DID
|
|
|
|
if not app_auth.is_logged_in():
|
|
|
|
return json.dumps({'error': 'Unable to send SMS, you are not logged in'})
|
|
|
|
|
|
|
|
#refreshtoken = google_auth.getRefreshToken()
|
|
|
|
#googleid = google_auth.getGoogleId()
|
|
|
|
if 'userid' in session:
|
|
|
|
userid = session['userid']
|
|
|
|
#userid = appdb.getUserIDfromGoogleID(googleid)
|
|
|
|
result = appdb.authIdforDID(userid,number)
|
|
|
|
smslog = appdb.getNumSMSLog(number,10)
|
|
|
|
#if not result:
|
|
|
|
# return json.dumps({'error': 'You are not allowed to use the requested DID'})
|
|
|
|
|
|
|
|
i = 0
|
|
|
|
msgjson = ""
|
|
|
|
for line in smslog:
|
|
|
|
prettyto = appsms.prettyPhone(line[7])
|
|
|
|
prettyfrom = appsms.prettyPhone(line[6])
|
|
|
|
if i >= 1:
|
|
|
|
msgjson = msgjson + ',' + json.dumps({'to':prettyto,'from':prettyfrom,'body':line[9],'timestamp': line[4],'status': line[10]})
|
|
|
|
else:
|
|
|
|
msgjson = json.dumps({'to':prettyto,'from':prettyfrom,'body':line[9],'timestamp': line[4], 'status': line[10]})
|
|
|
|
i += 1
|
|
|
|
|
|
|
|
|
|
|
|
msgArrayJson = '['+msgjson +']'
|
|
|
|
return msgArrayJson
|
|
|
|
|
|
|
|
@app.route('/submitMessage', methods=['POST'])
|
|
|
|
def submitMessage():
|
|
|
|
#This is to submit a message.
|
|
|
|
|
|
|
|
if not app_auth.is_logged_in():
|
|
|
|
return json.dumps({'error': 'Unable to send SMS'})
|
|
|
|
|
|
|
|
message = flask.request.form['message']
|
|
|
|
fromDid = flask.request.form['fromdid']
|
|
|
|
targetDid = flask.request.form['targetdid']
|
|
|
|
#user_info = google_auth.get_user_info()
|
|
|
|
#refreshtoken = google_auth.getRefreshToken()
|
|
|
|
#googleid = google_auth.getGoogleId()
|
|
|
|
#userid = appdb.getUserIDfromGoogleID(googleid)
|
|
|
|
if 'userid' in session:
|
|
|
|
userid = session['userid']
|
|
|
|
result = appdb.authIdforDID(userid,fromDid)
|
|
|
|
|
|
|
|
if userid != result:
|
|
|
|
if app_debug == '1':
|
|
|
|
pprint.pprint(userid)
|
|
|
|
pprint.pprint(result)
|
|
|
|
return json.dumps({'error': 'Unauthorized UserID of ' + str(userid) + " and DID id of " + str(result) + " and fromDID " + str(fromDid)})
|
|
|
|
|
|
|
|
if appdb.validateFrom(fromDid) == False:
|
|
|
|
return json.dumps({'error': 'Unauthorized source phone number.'})
|
|
|
|
|
|
|
|
uglyphone = appsms.uglyPhone(targetDid)
|
|
|
|
|
|
|
|
#pprint.pprint('Got ' + message + ',' + fromDid)
|
|
|
|
msg_id = appsms.sendsms(uglyphone,fromDid,message)
|
|
|
|
if msg_id == False: #This sends the sms!
|
|
|
|
returndata = json.dumps({'error': 'Unable to send SMS'})
|
|
|
|
else:
|
|
|
|
msgTS = time.strftime("%Y-%m-%dT%H:%m:%SZ")
|
|
|
|
appdb.logsms_db(msg_id, msgTS, 'outbound', uglyphone, fromDid, 0.0040, 'pending', message, result)
|
|
|
|
returndata = json.dumps({"msg" : message, "fromdid" : fromDid, 'targetdid' : targetDid})
|
|
|
|
return returndata
|
|
|
|
|
|
|
|
@app.route('/testAjax')
|
|
|
|
def testAjax():
|
|
|
|
return json.dumps({"msg" : 'Success!'})
|
|
|
|
|
|
|
|
@app.route('/inbox')
|
|
|
|
def inbox():
|
|
|
|
if app_auth.is_logged_in():
|
|
|
|
loggedin = True
|
|
|
|
else:
|
|
|
|
loggedin = False
|
|
|
|
loginId = flask.session['loginid']
|
|
|
|
results = appdb.getSMSbyAccount(loginId,10)
|
|
|
|
pprint.pprint(results)
|
|
|
|
return flask.render_template('inbox.html', loggedin=loggedin)
|
|
|
|
|
|
|
|
@app.route('/launch')
|
|
|
|
def launchPage():
|
|
|
|
if app_auth.is_logged_in():
|
|
|
|
loggedin = True
|
|
|
|
else:
|
|
|
|
loggedin = False
|
|
|
|
if app_debug == '1':
|
|
|
|
pprint.pprint(loggedin)
|
|
|
|
pprint.pprint("loggedin")
|
|
|
|
return flask.render_template('launch.html',loggedin=loggedin)
|
|
|
|
|
|
|
|
@app.route('/pp')
|
|
|
|
def PrivacyPolicy():
|
|
|
|
if app_debug == '1':
|
|
|
|
pprint.pprint(flask.session)
|
|
|
|
if app_auth.is_logged_in():
|
|
|
|
loggedin = True
|
|
|
|
else:
|
|
|
|
loggedin = False
|
|
|
|
return flask.render_template('pp.html',loggedin=loggedin)
|
|
|
|
|
|
|
|
@app.route('/tos')
|
|
|
|
def tos():
|
|
|
|
if app_auth.is_logged_in():
|
|
|
|
loggedin = True
|
|
|
|
else:
|
|
|
|
loggedin = False
|
|
|
|
return flask.render_template('tos.html',loggedin=loggedin)
|
|
|
|
|
|
|
|
@app.route('/about')
|
|
|
|
def about():
|
|
|
|
if app_auth.is_logged_in():
|
|
|
|
loggedin = True
|
|
|
|
else:
|
|
|
|
loggedin = False
|
|
|
|
return flask.render_template('about.html',loggedin=loggedin)
|
|
|
|
|
|
|
|
if __name__ == '__main__':
|
|
|
|
app.run(
|
|
|
|
host="0.0.0.0",
|
|
|
|
port=int("8890")
|
|
|
|
)
|