Syntax cleaning

A lot of cleaning of syntax after re-installing my computer and having to re-setup mariadb. I will try and make a dump of the example database.
pull/6/head
Hailey Clark 5 years ago
parent af2539da84
commit cb2db16ad0
  1. 70
      app_auth.py
  2. 41
      app_settings.py
  3. 273
      appdb.py
  4. 27
      appsms.py
  5. 32
      callback_sms.py
  6. 1
      database.sql
  7. 213
      smsproj.py
  8. 73
      templates/inbox.html

@ -1,29 +1,31 @@
import hashlib, binascii, os import hashlib
import flask import flask
from flask import Flask, request, redirect, url_for from flask import request, redirect
import hashlib, binascii import binascii
from passlib.hash import pbkdf2_sha256 # from passlib.hash
import functools import functools
import os import os
salt = os.urandom(32)
import appdb import appdb
import pprint import pprint
#import google_auth # import google_auth
import configparser import configparser
salt = os.urandom(32)
config = configparser.ConfigParser() config = configparser.ConfigParser()
config.read('config.ini') config.read('config.ini')
app_debug = config.get("app","debug") app_debug = config.get("app", "debug")
app_salt = config.get("auth","FN_FLASK_SECRET_KEY") app_salt = config.get("auth", "FN_FLASK_SECRET_KEY")
login_redirect = "/" login_redirect = "/"
app = flask.Blueprint('app_auth', __name__) app = flask.Blueprint('app_auth', __name__)
app.debug = True app.debug = True
def no_cache(view): def no_cache(view):
@functools.wraps(view) @functools.wraps(view)
def no_cache_impl(*args, **kwargs): def no_cache_impl(*args, **kwargs):
response = flask.make_response(view(*args, **kwargs)) response = flask.make_response(view(*args, **kwargs))
response.headers['Cache-Control'] = 'no-store, no-cache, must-revalidate, max-age=0' response.headers['Cache-Control'] = 'no-store, no-cache, must-revalidate, max-age=0'
response.headers['Pragma'] = 'no-cache' response.headers['Pragma'] = 'no-cache'
response.headers['Expires'] = '-1' response.headers['Expires'] = '-1'
@ -31,27 +33,35 @@ def no_cache(view):
return functools.update_wrapper(no_cache_impl, view) return functools.update_wrapper(no_cache_impl, view)
def is_logged_in(): def is_logged_in():
return True if 'loginid' in flask.session else False if flask.session.get('loginid'):
if flask.session.loggedin:
pprint.pprint('Flask session loginhash:') pprint.pprint('Flask session loginhash:')
pprint.pprint(flask.session.loggedin) pprint.pprint(flask.session.get('loginid'))
return True if appdb.verify_id(
flask.session.get('email'),
flask.session.get('loginid')) is True:
return True
return False
return False return False
def verify_login(email, password): def verify_login(email, password):
result = appdb.verify_login(email, password) result = appdb.verify_login(email, password)
if result: if result:
return True return True
return False return False
@app.route('/auth/login', methods=['POST']) @app.route('/auth/login', methods=['POST'])
@no_cache @no_cache
def auth_login(): def auth_login():
"""Login using provided credentials""" """Login using provided credentials"""
#pprint.pprint('Got credentials offff') # pprint.pprint('Got credentials offff')
#pprint.pprint(request.form) # pprint.pprint(request.form)
if appdb.verify_login(request.form['email'],hash_password(request.form['passwd'].encode('ascii'))): if appdb.verify_login(
request.form['email'],
hash_password(request.form['passwd'].encode('ascii'))):
pprint.pprint("got variables") pprint.pprint("got variables")
uniqueID = appdb.generate_id(request.form['email']) uniqueID = appdb.generate_id(request.form['email'])
flask.session['loggedin'] = True flask.session['loggedin'] = True
@ -61,50 +71,58 @@ def auth_login():
flask.session['password'] = request.form['passwd'] flask.session['password'] = request.form['passwd']
return "/" return "/"
return "error" return "error"
#return login_redirect # return login_redirect
@app.route('/auth/register', methods=['POST']) @app.route('/auth/register', methods=['POST'])
@no_cache @no_cache
def auth_register_login(): def auth_register_login():
"""Create a login using the supplied credentials in request.form""" """Create a login using the supplied credentials in request.form"""
#pprint.pprint('Got credentials offff') # pprint.pprint('Got credentials offff')
#pprint.pprint(request.form) # pprint.pprint(request.form)
return "DISABLED" return "DISABLED"
@app.route('/auth/updatepw', methods=['POST']) @app.route('/auth/updatepw', methods=['POST'])
@no_cache @no_cache
def auth_updatepw(): def auth_updatepw():
'''This takes three post variables to match the old password then match two passwords '''This takes three post variables to match the old password then match two
forms then update password if it all checks out.''' passwords forms then update password if it all checks out.'''
if not is_logged_in(): if not is_logged_in():
return "error" return "error"
if flask.session['loginid']: if flask.session['loginid']:
user_info = appdb.getUserInfo(flask.session['email'],flask.session['loginid']) user_info = appdb.getUserInfo(
flask.session['email'], flask.session['loginid'])
passzero = request.form['passwdzero'] passzero = request.form['passwdzero']
passone = request.form['passwdone'] passone = request.form['passwdone']
orighash = hash_password(passzero.encode('ascii')) orighash = hash_password(passzero.encode('ascii'))
newhash = hash_password(passone.encode('ascii')) newhash = hash_password(passone.encode('ascii'))
if (appdb.updatePass(user_info[0],orighash,newhash)): if (appdb.updatePass(user_info[0], orighash, newhash)):
return '200' return '200'
return "error" return "error"
@app.route('/auth/logout') @app.route('/auth/logout')
@no_cache @no_cache
def auth_logout(): def auth_logout():
flask.session.clear() flask.session.clear()
return redirect('/') return redirect('/')
def hash_password(password): def hash_password(password):
"""Hash a password for storing.""" """Hash a password for storing."""
pwdhash = hashlib.pbkdf2_hmac('sha512', password, app_salt.encode('ascii'), 100000) pwdhash = hashlib.pbkdf2_hmac(
#hash = pbkdf2_sha256.encrypt(password, rounds=200000, salt_size=16) 'sha512', password, app_salt.encode('ascii'), 100000)
# hash = pbkdf2_sha256.encrypt(password, rounds=200000, salt_size=16)
pwdhash = binascii.hexlify(pwdhash).decode('ascii') pwdhash = binascii.hexlify(pwdhash).decode('ascii')
return pwdhash return pwdhash
def verify_password(stored_password, provided_password): def verify_password(stored_password, provided_password):
"""Verify a stored password against one provided by user""" """Verify a stored password against one provided by user"""
pwdhash = hashlib.pbkdf2_hmac('sha512', provided_password, pwdhash = hashlib.pbkdf2_hmac(
app_salt.encode('ascii'), 100000) 'sha512', provided_password, app_salt.encode('ascii'), 100000)
pwdhash = binascii.hexlify(pwdhash).decode('ascii') pwdhash = binascii.hexlify(pwdhash).decode('ascii')
return pwdhash == stored_password return pwdhash == stored_password

@ -1,18 +1,19 @@
#!/usr/bin/env python3 #!/usr/bin/env python3
#app_settings.py # app_settings.py
import functools # import functools
import os # import os
import pprint import pprint
import configparser import configparser
import json import json
import flask import flask
import appdb, app_auth import appdb
import app_auth
config = configparser.ConfigParser() config = configparser.ConfigParser()
config.read('config.ini') config.read('config.ini')
app_debug = config.get("app","debug") app_debug = config.get("app", "debug")
app = flask.Blueprint('app_settings', __name__) app = flask.Blueprint('app_settings', __name__)
if app_debug == '1': if app_debug == '1':
@ -20,29 +21,34 @@ if app_debug == '1':
else: else:
app.debug = False app.debug = False
@app.route('/settings') @app.route('/settings')
def appsettings(): def appsettings():
'''This function pulls some information and then renders the settings or error template''' '''This function pulls some information and then renders the settings or
error template'''
if flask.session.get('loginid'): if flask.session.get('loginid'):
user_info = appdb.getUserInfo(flask.session['email'],flask.session['loginid']) user_info = appdb.getUserInfo(flask.session['email'],
flask.session['loginid'])
loggedin = True loggedin = True
else: else:
user_info = False user_info = False
indbRes = False # indbRes = False
return flask.render_template('deny.html', denymsg = "I don't know who you are so I can't help you with your user settings. :(", loggedin = False) return flask.render_template('deny.html',
denymsg="I don't know who you are so I can't help you with your user settings. :(", loggedin = False)
if user_info: if user_info:
rows = appdb.getDIDsbyAccount(user_info[0]) rows = appdb.getDIDsbyAccount(user_info[0])
pprint.pprint(user_info) pprint.pprint(user_info)
accountInfo = appdb.getInfobyEmail(user_info[2]) accountInfo = appdb.getInfobyEmail(user_info[2])
pprint.pprint(accountInfo) pprint.pprint(accountInfo)
else: else:
return 'error'; # loggedin = False
return flask.render_template('homepage.html', loggedin=False)
return flask.render_template('settings.html', return flask.render_template('settings.html',
user_info = user_info, user_info=user_info,
account_info = accountInfo, account_info=accountInfo,
dids = rows, dids=rows,
loggedin = True) loggedin=True)
@app.route('/checkUsername/<username>', methods=['GET']) @app.route('/checkUsername/<username>', methods=['GET'])
def checkUsername(username): def checkUsername(username):
@ -53,13 +59,14 @@ def checkUsername(username):
return json.dumps({'error': 'Username already exists, please choose another.'}) return json.dumps({'error': 'Username already exists, please choose another.'})
return json.dumps({'name': 'Available'}) return json.dumps({'name': 'Available'})
@app.route('/createUser', methods=['POST']) @app.route('/createUser', methods=['POST'])
def createUser(): def createUser():
'''Create the rest of the db entry for the user''' '''Create the rest of the db entry for the user'''
username = flask.request.form['username'] username = flask.request.form['username']
password = flask.request.form['password'] password = flask.request.form['password']
email = flask.request.form['email'] email = flask.request.form['email']
#user_info = google_auth.get_user_info() # user_info = google_auth.get_user_info()
if appdb.isUserExist(username): if appdb.isUserExist(username):
return json.dumps({'error': 'Username already exists. Please choose another.'}) return json.dumps({'error': 'Username already exists. Please choose another.'})
@ -72,7 +79,7 @@ def createUser():
passwd = app_auth.hash_password(password.encode('ascii')) passwd = app_auth.hash_password(password.encode('ascii'))
res = appdb.finalizeNewUser(email, username, passwd) res = appdb.finalizeNewUser(email, username, passwd)
if app_debug == '1': if app_debug == '1':
pprint.pprint('Updating email, username, passwd' + email ) pprint.pprint('Updating email, username, passwd' + email)
if res: if res:
return json.dumps({'msg': 'Success!'}) return json.dumps({'msg': 'Success!'})
return json.dumps({'error': 'There is an error in processing the request.'}) return json.dumps({'error': 'There is an error in processing the request.'})

@ -1,39 +1,43 @@
#!/usr/bin/env python3 #!/usr/bin/env python3
#appdb.py # appdb.py
#We connect to our database and any database calls are put into functions here. # We connect to our database and any database calls are put here.
import pymysql import pymysql
import pymysql.cursors import pymysql.cursors
import pprint import pprint
import uuid import uuid
#import time # import time
import configparser import configparser
config = configparser.ConfigParser() config = configparser.ConfigParser()
config.read('config.ini') config.read('config.ini')
sqlhost = config.get("sql","sqlhost") sqlhost = config.get("sql", "sqlhost")
sqlport = int(config.get("sql","sqlport")) sqlport = int(config.get("sql", "sqlport"))
sqluser = config.get("sql","sqluser") sqluser = config.get("sql", "sqluser")
sqlpass = config.get("sql","sqlpass") sqlpass = config.get("sql", "sqlpass")
sqldb = config.get("sql","sqldb") sqldb = config.get("sql", "sqldb")
app_debug = config.get("app","debug") app_debug = config.get("app", "debug")
def logsms_db(msg_id, msg_ts, direction, to_did, from_did, cost, status, msg, account_id): def logsms_db(msg_id, msg_ts, direction, to_did, from_did, cost, status, msg,
#This statement logs a SMS to the smslog table. account_id):
db = pymysql.connect(host=sqlhost, port=sqlport, user=sqluser, passwd=sqlpass, db=sqldb) '''This statement logs a SMS to the smslog table.'''
db = pymysql.connect(host=sqlhost, port=sqlport,
user=sqluser, passwd=sqlpass, db=sqldb)
cur = db.cursor() cur = db.cursor()
cur.execute("INSERT INTO messages (`timestamp`, `provider_timestamp`,`direction`, `source_number`, `dest_number`, `cost`,`pid`,`status`, `body`, `account_id`)VALUES \ cur.execute("INSERT INTO messages (`timestamp`, `provider_timestamp`, `direction`, `source_number`, `dest_number`, `cost`, `pid`, `status`, `body`, `account_id`) VALUES (now(), %s, %s, %s, %s, %s, %s, %s, %s, %s)",
(now(), %s, %s, %s, %s, %s, %s, %s, %s, %s)",(msg_ts, direction, from_did, to_did, cost, msg_id, status, msg, account_id)) (msg_ts, direction, from_did, to_did, cost, msg_id, status, msg, account_id))
db.commit() db.commit()
db.close() db.close()
return True return True
def getUserInfobyID(id): def getUserInfobyID(id):
'''This pulls * from 'account' and returns it if it matches an email.''' '''This pulls * from 'account' and returns it if it matches an email.'''
db = pymysql.connect(host=sqlhost, port=sqlport, user=sqluser, passwd=sqlpass, db=sqldb) db = pymysql.connect(host=sqlhost, port=sqlport,
user=sqluser, passwd=sqlpass, db=sqldb)
cur = db.cursor() cur = db.cursor()
cur.execute("SELECT * FROM account WHERE id=%s LIMIT 1",(id)) cur.execute("SELECT * FROM account WHERE id=%s LIMIT 1", (id))
data = cur.fetchone() data = cur.fetchone()
db.close() db.close()
if app_debug == '1': if app_debug == '1':
@ -43,72 +47,76 @@ def getUserInfobyID(id):
return False return False
return data return data
def isGUserinDB(google_id):
db = pymysql.connect(host=sqlhost, port=sqlport, user=sqluser, passwd=sqlpass, db=sqldb) def isUserinDB(id):
db = pymysql.connect(host=sqlhost, port=sqlport,
user=sqluser, passwd=sqlpass, db=sqldb)
cur = db.cursor() cur = db.cursor()
cur.execute("SELECT * FROM account WHERE google_id=%s LIMIT 1" % google_id) cur.execute("SELECT * FROM account WHERE id=%s LIMIT 1" % id)
data = cur.fetchone() data = cur.fetchone()
db.close() db.close()
if data: if data:
pprint.pprint(data) pprint.pprint(data)
return True return data
else: else:
return False return False
def isUserinDB(id):
db = pymysql.connect(host=sqlhost, port=sqlport, user=sqluser, passwd=sqlpass, db=sqldb)
cur = db.cursor()
cur.execute("SELECT * FROM account WHERE id=%s LIMIT 1" % id)
data = cur.fetchone()
db.close()
if data:
pprint.pprint(data)
return data
else:
return False
def isUserExist(email): def isUserExist(email):
db = pymysql.connect(host=sqlhost, port=sqlport, user=sqluser, passwd=sqlpass, db=sqldb) db = pymysql.connect(host=sqlhost, port=sqlport,
user=sqluser, passwd=sqlpass, db=sqldb)
cur = db.cursor() cur = db.cursor()
cur.execute("SELECT email FROM account WHERE email=%s",(name)) cur.execute("SELECT email FROM account WHERE email=%s", (email))
data = cur.fetchone() data = cur.fetchone()
db.close() db.close()
if data: if data:
return True return True
return False return False
def generate_id(email): def generate_id(email):
db = pymysql.connect(host=sqlhost, port=sqlport, user=sqluser, passwd=sqlpass, db=sqldb) db = pymysql.connect(host=sqlhost, port=sqlport,
user=sqluser, passwd=sqlpass, db=sqldb)
cur = db.cursor() cur = db.cursor()
newID = uuid.uuid4().hex newID = uuid.uuid4().hex
cur.execute("UPDATE account SET loginid=%s WHERE email=%s LIMIT 1",(newID, email)) cur.execute("UPDATE account SET loginid=%s WHERE email=%s LIMIT 1",
(newID, email))
db.commit() db.commit()
db.close() db.close()
return newID return newID
def verify_id(email, uniqueid): def verify_id(email, uniqueid):
db = pymysql.connect(host=sqlhost, port=sqlport, user=sqluser, passwd=sqlpass, db=sqldb) db = pymysql.connect(host=sqlhost, port=sqlport,
user=sqluser, passwd=sqlpass, db=sqldb)
cur = db.cursor() cur = db.cursor()
cur.execute("SELECT email FROM account WHERE email=%s AND loginid=%s LIMIT 1",(email, uniqueid)) cur.execute(
"SELECT email FROM account WHERE email=%s AND loginid=%s LIMIT 1",
(email, uniqueid))
data = cur.fetchone() data = cur.fetchone()
db.close() db.close()
if data: if data:
return True return True
return False return False
def verify_login(email, password): def verify_login(email, password):
db = pymysql.connect(host=sqlhost, port=sqlport, user=sqluser, passwd=sqlpass, db=sqldb) db = pymysql.connect(host=sqlhost, port=sqlport,
user=sqluser, passwd=sqlpass, db=sqldb)
cur = db.cursor() cur = db.cursor()
cur.execute("SELECT email FROM account WHERE email=%s AND passwd=%s LIMIT 1",(email, password)) cur.execute(
"SELECT email FROM account WHERE email=%s AND passwd=%s LIMIT 1",
(email, password))
data = cur.fetchone() data = cur.fetchone()
db.close() db.close()
if data: if data:
return data return data
return False return False
def getUserInfo(email, uniqueid): def getUserInfo(email, uniqueid):
db = pymysql.connect(host=sqlhost, port=sqlport, user=sqluser, passwd=sqlpass, db=sqldb) db = pymysql.connect(host=sqlhost, port=sqlport,
user=sqluser, passwd=sqlpass, db=sqldb)
cur = db.cursor() cur = db.cursor()
cur.execute("SELECT * FROM account WHERE email=%s AND loginid=%s LIMIT 1",(email, uniqueid)) cur.execute("SELECT * FROM account WHERE email=%s AND loginid=%s LIMIT 1",(email, uniqueid))
data = cur.fetchone() data = cur.fetchone()
@ -119,9 +127,12 @@ def getUserInfo(email, uniqueid):
if data: if data:
return data return data
return False return False
def isUserVerfied(google_id): def isUserVerfied(google_id):
#This checks to see if the account is set to verified true '''This checks to see if the account is set to verified true'''
db = pymysql.connect(host=sqlhost, port=sqlport, user=sqluser, passwd=sqlpass, db=sqldb) db = pymysql.connect(host=sqlhost, port=sqlport,
user=sqluser, passwd=sqlpass, db=sqldb)
cur = db.cursor() cur = db.cursor()
cur.execute("SELECT verified_email FROM account WHERE google_id=%s",(google_id)) cur.execute("SELECT verified_email FROM account WHERE google_id=%s",(google_id))
data = cur.fetchone() data = cur.fetchone()
@ -131,18 +142,22 @@ def isUserVerfied(google_id):
else: else:
return False return False
def registerUser(email, password): def registerUser(email, password):
db = pymysql.connect(host=sqlhost, port=sqlport, user=sqluser, passwd=sqlpass, db=sqldb) db = pymysql.connect(host=sqlhost, port=sqlport,
user=sqluser, passwd=sqlpass, db=sqldb)
cur = db.cursor() cur = db.cursor()
#cur.execute("INSERT INTO account (`name`, `email`, `refresh_token`, `user_id`, `verified_email`, `created`, `last_modified`) VALUES \ # cur.execute("INSERT INTO account (`name`, `email`, `refresh_token`, `user_id`, `verified_email`, `created`, `last_modified`) VALUES \
# (%s, %s, %s, %s, %s, NOW(), NOW())",(name, email, refresh_token, user_id, verified)) # (%s, %s, %s, %s, %s, NOW(), NOW())",(name, email, refresh_token, user_id, verified))
db.commit() db.commit()
db.close() db.close()
return "Success!" return "Success!"
def setNewUser(user_id, refresh_token, name, email, verified): def setNewUser(user_id, refresh_token, name, email, verified):
#This statement is for creating a user into the account table. '''This statement is for creating a user into the account table.'''
db = pymysql.connect(host=sqlhost, port=sqlport, user=sqluser, passwd=sqlpass, db=sqldb) db = pymysql.connect(host=sqlhost, port=sqlport,
user=sqluser, passwd=sqlpass, db=sqldb)
cur = db.cursor() cur = db.cursor()
cur.execute("INSERT INTO account (`name`, `email`, `refresh_token`, `user_id`, `verified_email`, `created`, `last_modified`) VALUES \ cur.execute("INSERT INTO account (`name`, `email`, `refresh_token`, `user_id`, `verified_email`, `created`, `last_modified`) VALUES \
(%s, %s, %s, %s, %s, NOW(), NOW())",(name, email, refresh_token, user_id, verified)) (%s, %s, %s, %s, %s, NOW(), NOW())",(name, email, refresh_token, user_id, verified))
@ -150,10 +165,13 @@ def setNewUser(user_id, refresh_token, name, email, verified):
db.close() db.close()
return True return True
def finalizeNewUser(email, username, passwd): def finalizeNewUser(email, username, passwd):
'''Finalizes a user creation after calling setNewUser(), this requires a password already converted to a '''Finalizes a user creation after calling setNewUser(), this requires a
safe hash of the password. Don't store plaintext passwords in this please''' password already converted to a safe hash of the password. Don't store
db = pymysql.connect(host=sqlhost, port=sqlport, user=sqluser, passwd=sqlpass, db=sqldb) plaintext passwords in this please'''
db = pymysql.connect(host=sqlhost, port=sqlport,
user=sqluser, passwd=sqlpass, db=sqldb)
cur = db.cursor() cur = db.cursor()
rows = cur.execute("UPDATE account SET username=%s, passwd=%s, verified_email=%s, last_modified=NOW() WHERE email=%s LIMIT 1",(username, passwd,2, email)) rows = cur.execute("UPDATE account SET username=%s, passwd=%s, verified_email=%s, last_modified=NOW() WHERE email=%s LIMIT 1",(username, passwd,2, email))
db.commit() db.commit()
@ -163,11 +181,13 @@ def finalizeNewUser(email, username, passwd):
else: else:
return False return False
def getInfobyEmail(email): def getInfobyEmail(email):
'''This pulls * from 'account' and returns it if it matches an email.''' '''This pulls * from 'account' and returns it if it matches an email.'''
db = pymysql.connect(host=sqlhost, port=sqlport, user=sqluser, passwd=sqlpass, db=sqldb) db = pymysql.connect(host=sqlhost, port=sqlport,
user=sqluser, passwd=sqlpass, db=sqldb)
cur = db.cursor() cur = db.cursor()
cur.execute("SELECT * FROM account WHERE email=%s LIMIT 1",(email)) cur.execute("SELECT * FROM account WHERE email=%s LIMIT 1", (email))
data = cur.fetchone() data = cur.fetchone()
db.close() db.close()
if app_debug == '1': if app_debug == '1':
@ -177,53 +197,49 @@ def getInfobyEmail(email):
return False return False
return data return data
def getUserIdFromRT(refreshtoken): def getUserIdFromRT(refreshtoken):
#This pulls an UserID from a Refresh Token '''This pulls an UserID from a Refresh Token'''
db = pymysql.connect(host=sqlhost, port=sqlport, user=sqluser, passwd=sqlpass, db=sqldb) db = pymysql.connect(host=sqlhost, port=sqlport,
user=sqluser, passwd=sqlpass, db=sqldb)
cur = db.cursor() cur = db.cursor()
cur.execute("SELECT id FROM account WHERE refresh_token=%s",(refreshtoken)) cur.execute("SELECT id FROM account WHERE loginid=%s", (refreshtoken))
data = cur.fetchone() data = cur.fetchone()
db.close() db.close()
if not data: if not data:
return False return False
return data[0] return data[0]
def getUserIDfromGoogleID(google_id):
#This pulls an UserID from a Google ID
db = pymysql.connect(host=sqlhost, port=sqlport, user=sqluser, passwd=sqlpass, db=sqldb)
cur = db.cursor()
cur.execute("SELECT id FROM account WHERE google_id=%s",(google_id))
data = cur.fetchone()
db.close()
if not data:
return False
return data[0]
def getAccountbyDID(did): def getAccountbyDID(did):
#This function pulls the account id for the DID in the query. '''This function pulls the account id for the DID in the query.'''
db = pymysql.connect(host=sqlhost, port=sqlport, user=sqluser, passwd=sqlpass, db=sqldb) db = pymysql.connect(host=sqlhost, port=sqlport,
user=sqluser, passwd=sqlpass, db=sqldb)
cur = db.cursor() cur = db.cursor()
cur.execute("SELECT account_id FROM dids WHERE number=%s LIMIT 1",(did)) cur.execute("SELECT account_id FROM dids WHERE number=%s LIMIT 1", (did))
data = cur.fetchone() data = cur.fetchone()
db.close() db.close()
if not data: if not data:
return False return False
return data[0] return data[0]
def getDIDsbyAccount(account_id): def getDIDsbyAccount(account_id):
#DIDs that are assigned to an account. '''DIDs that are assigned to an account.'''
db = pymysql.connect(host=sqlhost, port=sqlport, user=sqluser, passwd=sqlpass, db=sqldb) db = pymysql.connect(host=sqlhost, port=sqlport,
user=sqluser, passwd=sqlpass, db=sqldb)
cur = db.cursor() cur = db.cursor()
cur.execute("SELECT number,provider FROM dids WHERE account_id=%s",(account_id)) cur.execute("SELECT number,provider FROM dids WHERE account_id=%s", (account_id))
rows = cur.fetchall() rows = cur.fetchall()
db.close() db.close()
return rows return rows
def authIdforDID(account_id,did): def authIdforDID(account_id, did):
db = pymysql.connect(host=sqlhost, port=sqlport, user=sqluser, passwd=sqlpass, db=sqldb) db = pymysql.connect(host=sqlhost, port=sqlport,
user=sqluser, passwd=sqlpass, db=sqldb)
cur = db.cursor() cur = db.cursor()
cur.execute("SELECT account.id FROM dids,account WHERE dids.account_id=account.id AND account.id=%s AND dids.number=%s LIMIT 1",(account_id,did)) cur.execute("SELECT account.id FROM dids,account WHERE dids.account_id=account.id AND account.id=%s AND dids.number=%s LIMIT 1", (account_id,did))
data = cur.fetchone() data = cur.fetchone()
db.close() db.close()
pprint.pprint("Printing AUTH ID") pprint.pprint("Printing AUTH ID")
@ -232,17 +248,22 @@ def authIdforDID(account_id,did):
else: else:
return False return False
def setRefreshToken(refresh_token, google_id): def setRefreshToken(refresh_token, google_id):
db = pymysql.connect(host=sqlhost, port=sqlport, user=sqluser, passwd=sqlpass, db=sqldb) db = pymysql.connect(host=sqlhost, port=sqlport,
user=sqluser, passwd=sqlpass, db=sqldb)
cur = db.cursor() cur = db.cursor()
pprint.pprint("Setting new refresh token of " + google_id + " and " + refresh_token) pprint.pprint("Setting new refresh token of " + google_id + " and " + refresh_token)
cur.execute("UPDATE account SET refresh_token=%s WHERE google_id=%s",(refresh_token, google_id)) cur.execute("UPDATE account SET refresh_token=%s WHERE google_id=%s",
(refresh_token, google_id))
db.commit() db.commit()
db.close() db.close()
return True return True
def getAccountId(uniqueID): def getAccountId(uniqueID):
db = pymysql.connect(host=sqlhost, port=sqlport, user=sqluser, passwd=sqlpass, db=sqldb) db = pymysql.connect(host=sqlhost, port=sqlport,
user=sqluser, passwd=sqlpass, db=sqldb)
cur = db.cursor() cur = db.cursor()
cur.execute("SELECT id FROM account WHERE loginid=%s LIMIT 1", uniqueID) cur.execute("SELECT id FROM account WHERE loginid=%s LIMIT 1", uniqueID)
rows = cur.fetchall() rows = cur.fetchall()
@ -251,72 +272,122 @@ def getAccountId(uniqueID):
return rows return rows
return False return False
def getAllSMSLog(limit=5, order='desc'): def getAllSMSLog(limit=5, order='desc'):
#This gets the last X amount of logs from all numbers. '''This gets the last X amount of logs from all numbers.'''
db = pymysql.connect(host=sqlhost, port=sqlport, user=sqluser, passwd=sqlpass, db=sqldb) db = pymysql.connect(host=sqlhost, port=sqlport,
user=sqluser, passwd=sqlpass, db=sqldb)
cur = db.cursor() cur = db.cursor()
cur.execute("SELECT * FROM messages ORDER BY timestamp DESC LIMIT %s",(limit)) cur.execute("SELECT * FROM messages ORDER BY timestamp DESC LIMIT %s",
(limit))
rows = cur.fetchall() rows = cur.fetchall()
db.close() db.close()
return rows return rows
def getSMSbyAccount(account_id, limit=5): def getSMSbyAccount(account_id, limit=5):
db = pymysql.connect(host=sqlhost, port=sqlport, user=sqluser, passwd=sqlpass, db=sqldb) db = pymysql.connect(host=sqlhost, port=sqlport,
user=sqluser, passwd=sqlpass, db=sqldb)
cur = db.cursor() cur = db.cursor()
cur.execute("SELECT messages.* FROM messages, account WHERE messages.account_id=account.id AND ACCOUNT.loginid=%s ORDER BY id DESC LIMIT %s",(account_id, limit)) cur.execute("SELECT messages.* FROM messages, account WHERE messages.account_id=account.id AND ACCOUNT.loginid=%s ORDER BY id DESC LIMIT %s",(account_id, limit))
rows = cur.fetchall() rows = cur.fetchall()
db.close() db.close()
return rows return rows
def getNumSMSLog(did, limit=5): def getNumSMSLog(did, limit=5):
#This gets the last X amount of logs from all numbers. '''This gets the last X amount of logs from all numbers.'''
db = pymysql.connect(host=sqlhost, port=sqlport, user=sqluser, passwd=sqlpass, db=sqldb) db = pymysql.connect(host=sqlhost, port=sqlport,
user=sqluser, passwd=sqlpass, db=sqldb)
cur = db.cursor() cur = db.cursor()
cur.execute("SELECT * FROM messages WHERE source_number=%s OR dest_number=%s ORDER BY timestamp DESC LIMIT %s",(did,did,limit)) cur.execute("SELECT * FROM messages WHERE source_number=%s OR dest_number=%s ORDER BY timestamp DESC LIMIT %s",(did,did,limit))
rows = cur.fetchall() rows = cur.fetchall()
#for row in rows: # for row in rows:
#pprint.pprint(row) # pprint.pprint(row)
db.close() db.close()
return rows return rows
def updateReadStatus(msg_id, isread):
db = pymysql.connect(host=sqlhost, port=sqlport,
user=sqluser, passwd=sqlpass, db=sqldb)
cur = db.cursor()
affected_count = cur.execute("UPDATE messages SET is_read=%s WHERE id=%s",
(isread, msg_id))
db.commit()
db.close()
return affected_count
def updateMarkAllRead(account_id):
db = pymysql.connect(host=sqlhost, port=sqlport,
user=sqluser, passwd=sqlpass, db=sqldb)
cur = db.cursor()
affected_count = cur.execute("UPDATE messages SET is_read=1 WHERE account_id=%s",
(account_id))
db.commit()
db.close()
return affected_count
def updateMarkAllUnread(account_id):
db = pymysql.connect(host=sqlhost, port=sqlport,
user=sqluser, passwd=sqlpass, db=sqldb)
cur = db.cursor()
affected_count = cur.execute("UPDATE messages SET is_read=0 WHERE account_id=%s",
(account_id))
db.commit()
db.close()
return affected_count
def updateMsgStatus(msg_id, status, msg_timestamp): def updateMsgStatus(msg_id, status, msg_timestamp):
#Update the delivered field in the database based on delivery reports. '''Update the delivered field in the database based on delivery reports.'''
#UPDATE messages SET delivered='success' WHERE pid='mdr2-46999f9ce19e11e99074722a1f1f4bb4'
db = pymysql.connect(host=sqlhost, port=sqlport, user=sqluser, passwd=sqlpass, db=sqldb) db = pymysql.connect(host=sqlhost, port=sqlport,
user=sqluser, passwd=sqlpass, db=sqldb)
cur = db.cursor() cur = db.cursor()
affected_count = cur.execute("UPDATE `messages` SET status=%s, `provider_timestamp`=%s WHERE `pid`=%s",(status, msg_timestamp ,msg_id)) affected_count = cur.execute("UPDATE `messages` SET status=%s, `provider_timestamp`=%s WHERE `pid`=%s",
(status, msg_timestamp, msg_id))
db.commit() db.commit()
db.close() db.close()
return affected_count return affected_count
def updateMsgTimestamp(msg_id, timestamp): def updateMsgTimestamp(msg_id, timestamp):
#This changes the timestamp of the msg_id to the timestamp provided by the provider. '''This changes the timestamp of the msg_id to the timestamp provided by
db = pymysql.connect(host=sqlhost, port=sqlport, user=sqluser, passwd=sqlpass, db=sqldb) the provider.'''
db = pymysql.connect(host=sqlhost, port=sqlport,
user=sqluser, passwd=sqlpass, db=sqldb)
cur = db.cursor() cur = db.cursor()
affected_count = cur.execute("UPDATE `messages` SET `provider_timestamp`=%s WHERE `pid`=%s",(timestamp,msg_id)) affected_count = cur.execute("UPDATE `messages` SET `provider_timestamp`=%s WHERE `pid`=%s",(timestamp,msg_id))
db.commit() db.commit()
db.close() db.close()
return affected_count return affected_count
def updatePass(account_id, origpass,newpass):
'''updatePass(origpass, newpass) this assumes newpass has been verified twice def updatePass(account_id, origpass, newpass):
and origpass equals the current password. Returns the amount of rows updated which should '''updatePass(origpass, newpass) this assumes newpass has been verified
always be 1 or 0.''' twice and origpass equals the current password. Returns the amount of rows
db = pymysql.connect(host=sqlhost, port=sqlport, user=sqluser, passwd=sqlpass, db=sqldb) updated which should always be 1 or 0.'''
db = pymysql.connect(host=sqlhost, port=sqlport,
user=sqluser, passwd=sqlpass, db=sqldb)
cur = db.cursor() cur = db.cursor()
#pprint.pprint("Updating the following %s with old pass %s",(account_id, origpass)) # pprint.pprint("Updating the following %s with old pass %s",(account_id, origpass))
affected_count = cur.execute("UPDATE account SET passwd=%s WHERE id=%s AND passwd=%s LIMIT 1",(newpass,account_id,origpass)) affected_count = cur.execute("UPDATE account SET passwd=%s WHERE id=%s AND passwd=%s LIMIT 1",(newpass,account_id,origpass))
db.commit() db.commit()
db.close() db.close()
return affected_count return affected_count
def validateFrom(did): def validateFrom(did):
# Looks up in the DB to see if a DID is a valid DID '''Looks up in the DB to see if a DID is a valid DID'''
db = pymysql.connect(host=sqlhost, port=sqlport, user=sqluser, passwd=sqlpass, db=sqldb) db = pymysql.connect(host=sqlhost, port=sqlport,
user=sqluser, passwd=sqlpass, db=sqldb)
cur = db.cursor() cur = db.cursor()
cur.execute("SELECT number FROM dids WHERE number=%s LIMIT 1" % did) cur.execute("SELECT number FROM dids WHERE number=%s LIMIT 1" % did)
data = cur.fetchone() data = cur.fetchone()
db.close() db.close()
if data != None and int(data[0]) == int(did): if data is not None and int(data[0]) == int(did):
return True return True
return False return False

@ -1,6 +1,6 @@
#!/usr/bin/env python3 #!/usr/bin/env python3
#appsms.py # appsms.py
import time # import time
import re import re
import configparser import configparser
from flowroutenumbersandmessaging.flowroutenumbersandmessaging_client import FlowroutenumbersandmessagingClient from flowroutenumbersandmessaging.flowroutenumbersandmessaging_client import FlowroutenumbersandmessagingClient
@ -9,22 +9,31 @@ fr_api_url = "https://api.flowroute.com/v2.2/messages"
config = configparser.ConfigParser() config = configparser.ConfigParser()
config.read('config.ini') config.read('config.ini')
basic_auth_user_name = config.get("flowroute","fr_access_key") basic_auth_user_name = config.get("flowroute", "fr_access_key")
basic_auth_password = config.get("flowroute","fr_secret_key") basic_auth_password = config.get("flowroute", "fr_secret_key")
client = FlowroutenumbersandmessagingClient(basic_auth_user_name, basic_auth_password) client = FlowroutenumbersandmessagingClient(
basic_auth_user_name, basic_auth_password)
messages_controller = client.messages messages_controller = client.messages
def prettyPhone(phonenumber): def prettyPhone(phonenumber):
result = re.search('1?\W*([2-9][0-8][0-9])\W*([2-9][0-9]{2})\W*([0-9]{4})?',str(phonenumber)) result = re.search(
prettystr = "(" + result.group(1) + ") " + result.group(2) + "-" + result.group(3) '1?\W*([2-9][0-8][0-9])\W*([2-9][0-9]{2})\W*([0-9]{4})?',
str(phonenumber))
prettystr = "(" + result.group(1) + ") " +\
result.group(2) + "-" + result.group(3)
return prettystr return prettystr
def uglyPhone(phonenumber): def uglyPhone(phonenumber):
result = re.search('1?\W*([2-9][0-8][0-9])\W*([2-9][0-9]{2})\W*([0-9]{4})?',str(phonenumber)) result = re.search(
'1?\W*([2-9][0-8][0-9])\W*([2-9][0-9]{2})\W*([0-9]{4})?',
str(phonenumber))
uglystr = "1" + result.group(1) + result.group(2) + result.group(3) uglystr = "1" + result.group(1) + result.group(2) + result.group(3)
return uglystr return uglystr
def sendsms(reply_to, reply_from, msg): def sendsms(reply_to, reply_from, msg):
request_body = '{ \ request_body = '{ \
"data": { "type": "message", \ "data": { "type": "message", \
@ -35,7 +44,7 @@ def sendsms(reply_to, reply_from, msg):
} } }' } } }'
result = messages_controller.send_a_message(request_body) result = messages_controller.send_a_message(request_body)
if result: if result:
msg_id = result['data']['id'] msg_id = result['data']['id']
return msg_id return msg_id
return False return False

@ -1,13 +1,14 @@
#!/usr/bin/env python3 #!/usr/bin/env python3
import os # import os
import urllib # import urllib
import json # import json
import pprint import pprint
import time # import time
import datetime # import datetime
import string # import string
import appdb, appsms import appdb
# import appsms
import configparser import configparser
import flask import flask
@ -18,10 +19,11 @@ app = flask.Blueprint('callback-sms', __name__)
# This is so bare I don't need a config right now. # This is so bare I don't need a config right now.
config = configparser.ConfigParser() config = configparser.ConfigParser()
config.read('config.ini') config.read('config.ini')
app_debug = config.get("app","debug") app_debug = config.get("app", "debug")
############################# #############################
## Callback defs go here # Callback defs go here
def prettyStatus(status): def prettyStatus(status):
if status == 'message buffered': if status == 'message buffered':
return 'Delivered' return 'Delivered'
@ -36,9 +38,11 @@ def prettyStatus(status):
elif status == 'smsc intermediate notifications': elif status == 'smsc intermediate notifications':
return 'Sent' return 'Sent'
@app.route('/sms-inbound', methods=['POST']) @app.route('/sms-inbound', methods=['POST'])
def smsinbound(): def smsinbound():
#extract attributes from POSTed JSON of inbound SMS '''extract attributes from POSTed JSON of inbound SMS'''
json_content = flask.request.json json_content = flask.request.json
reply_to = json_content['data']['attributes']['from'] reply_to = json_content['data']['attributes']['from']
reply_from = json_content['data']['attributes']['to'] reply_from = json_content['data']['attributes']['to']
@ -48,12 +52,14 @@ def smsinbound():
smsRate = json_content['data']['attributes']['amount_display'].replace('$','') smsRate = json_content['data']['attributes']['amount_display'].replace('$','')
status = 'Delivered' status = 'Delivered'
account_id = appdb.getAccountbyDID(reply_from) account_id = appdb.getAccountbyDID(reply_from)
appdb.logsms_db(msg_id, msg_timestamp, 'inbound', reply_from, reply_to, smsRate, status, body, account_id) # Lets log to our silly db. appdb.logsms_db(msg_id, msg_timestamp, 'inbound', reply_from, reply_to,
smsRate, status, body, account_id) # Lets log to our silly db.
return "200" return "200"
@app.route('/dlr', methods=['POST','GET'])
@app.route('/dlr', methods=['POST', 'GET'])
def deliveryReport(): def deliveryReport():
#This is the delivery report callback function. '''This is the delivery report callback function.'''
json_content = flask.request.json json_content = flask.request.json
pprint.pprint(json_content) pprint.pprint(json_content)
msg_id = json_content['data']['id'] msg_id = json_content['data']['id']

@ -119,3 +119,4 @@ ALTER TABLE account DROP COLUMN `username`;
ALTER TABLE account DROP COLUMN `refresh_token`; ALTER TABLE account DROP COLUMN `refresh_token`;
ALTER TABLE account ADD COLUMN `loginid` VARCHAR(255) NULL UNIQUE; ALTER TABLE account ADD COLUMN `loginid` VARCHAR(255) NULL UNIQUE;
ALTER TABLE account ADD COLUMN `picture_url` VARCHAR(255) NULL; ALTER TABLE account ADD COLUMN `picture_url` VARCHAR(255) NULL;
ALTER TABLE messages ADD COLUMN `is_read` BOOL NOT NULL DEFAULT '0';

@ -1,26 +1,27 @@
#!/usr/bin/env python3 #!/usr/bin/env python3
# This program runs a flask daemon to provide communications with flowroute n stuff. # This program runs a flask daemon to provide communications with flowroute n
# stuff.
import time import time
import datetime # import datetime
import pprint import pprint
import configparser import configparser
import json import json
import re # import re
import flask 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 appdb
import appsms
import app_settings
import app_auth
import callback_sms import callback_sms
config = configparser.ConfigParser() config = configparser.ConfigParser()
config.read('config.ini') config.read('config.ini')
app_debug = config.get("app","debug") app_debug = config.get("app", "debug")
app = flask.Flask(__name__) app = flask.Flask(__name__)
app.secret_key = config.get("auth","FN_FLASK_SECRET_KEY") app.secret_key = config.get("auth", "FN_FLASK_SECRET_KEY")
app.register_blueprint(callback_sms.app) app.register_blueprint(callback_sms.app)
app.register_blueprint(app_settings.app) app.register_blueprint(app_settings.app)
@ -33,64 +34,83 @@ if app_debug == '1':
else: else:
app.debug = False app.debug = False
@app.route('/') @app.route('/')
def index(): def index():
'''This is the root index. If not logged in it displays homepage.html''' '''This is the root index. If not logged in it displays homepage.html'''
if flask.session.get('loginid'): if flask.session.get('loginid'):
user_info = appdb.getUserInfo(flask.session['email'],flask.session['loginid']) user_info = appdb.getUserInfo(
loggedin = True flask.session['email'], flask.session['loginid'])
if not user_info: if not user_info:
return flask.render_template('homepage.html', loggedin = False) return flask.render_template('homepage.html', loggedin=False)
rows = appdb.getDIDsbyAccount(flask.session['account_id']) rows = appdb.getDIDsbyAccount(flask.session['account_id'])
return flask.render_template('index.html', return flask.render_template('index.html',
name = user_info[2], name=user_info[2],
picture = user_info[8], picture=user_info[8],
dids = rows, dids=rows,
loggedin = True) loggedin=True)
else: else:
return flask.render_template('homepage.html', loggedin = False) return flask.render_template('homepage.html', loggedin=False)
@app.route('/landing') @app.route('/landing')
def landingPage(): def landingPage():
'''This renders the landing page''' '''This renders the landing page'''
#user_info = google_auth.get_user_info() # user_info = google_auth.get_user_info()
if flask.session['loginid']: if flask.session['loginid']:
user_info = appdb.getUserInfo(flask.session['email'],flask.session['loginid']) user_info = appdb.getUserInfo(
#Going to replace google_auth with a local authentication. flask.session['email'], flask.session['loginid'])
# Going to replace google_auth with a local authentication.
if app_auth.is_logged_in(): if app_auth.is_logged_in():
loggedin = True loggedin = True
else: else:
loggedin = False loggedin = False
return flask.render_template('landing.html', user_info = user_info, loggedin = loggedin) return flask.render_template(
'landing.html', user_info=user_info, loggedin=loggedin)
@app.route('/single/<int:number>', methods=['GET']) @app.route('/single/<int:number>', methods=['GET'])
def manageSingleSMS(number): def manageSingleSMS(number):
'''This renders a view for a single SMS number and its associated messages''' '''This renders a view for a single SMS number and its
associated messages'''
if not app_auth.is_logged_in(): if not app_auth.is_logged_in():
return flask.render_template('deny.html',denymsg = loginMsg, loggedin = False) return flask.render_template(
'deny.html', denymsg=loginMsg, loggedin=False)
if flask.session['loginid']: if flask.session['loginid']:
user_info = appdb.getUserInfo(flask.session['email'],flask.session['loginid']) user_info = appdb.getUserInfo(
flask.session['email'], flask.session['loginid'])
result = appdb.authIdforDID(user_info[0],number) result = appdb.authIdforDID(user_info[0], number)
prettynum = appsms.prettyPhone(number) prettynum = appsms.prettyPhone(number)
if appdb.validateFrom(int(number)) and result: if appdb.validateFrom(int(number)) and result:
return flask.render_template('single.html',srcnumber = number, prettynum = prettynum, loggedin = True) return flask.render_template(
'single.html',
srcnumber=number,
prettynum=prettynum,
loggedin=True)
else: else:
return flask.render_template('notvalid.html', srcnumber = number, prettynum = prettynum, loggedin = True) return flask.render_template(
'notvalid.html',
srcnumber=number,
prettynum=prettynum,
loggedin=True)
#Gotta redo this logic #Gotta redo this logic
@app.route('/getNumber/<int:number>',methods=['GET']) @app.route('/getNumber/<int:number>',methods=['GET'])
def getNumMessages(number): def getNumMessages(number):
'''Return the messages from a single DID in json form''' '''Return the messages from a single DID in json form'''
#This gets the mssages based on the provided from or two DID # This gets the mssages based on the provided from or two DID
if not app_auth.is_logged_in(): if not app_auth.is_logged_in():
return json.dumps({'error': 'Unable to send SMS, you are not logged in'}) return json.dumps({'error': 'Unable to send SMS, you are not logged in'})
# We need to take and compare the authIDforDID, gotta add use id
# to getNumSMSLog and pull the id from result.
userid = flask.session['account_id'] userid = flask.session['account_id']
result = appdb.authIdforDID(userid,number) result = appdb.authIdforDID(userid, number)
smslog = appdb.getNumSMSLog(number,10) smslog = appdb.getNumSMSLog(number, 10)
i = 0 i = 0
msgjson = "" msgjson = ""
@ -98,14 +118,61 @@ def getNumMessages(number):
prettyto = appsms.prettyPhone(line[7]) prettyto = appsms.prettyPhone(line[7])
prettyfrom = appsms.prettyPhone(line[6]) prettyfrom = appsms.prettyPhone(line[6])
if i >= 1: if i >= 1:
msgjson = msgjson + ',' + json.dumps({'to':prettyto,'from':prettyfrom,'body':line[9],'timestamp': line[4],'status': line[10]}) msgjson = msgjson + ',' + json.dumps({'to': prettyto,
'from': prettyfrom,
'body': line[9],
'timestamp': line[4],
'status': line[10]})
else: else:
msgjson = json.dumps({'to':prettyto,'from':prettyfrom,'body':line[9],'timestamp': line[4], 'status': line[10]}) msgjson = json.dumps({'to': prettyto,
'from': prettyfrom,
'body': line[9],
'timestamp': line[4],
'status': line[10]})
i += 1 i += 1
msgArrayJson = '[' + msgjson + ']'
return msgArrayJson
msgArrayJson = '['+msgjson +']' @app.route('/markread/<int:number>', methods=['GET'])
return msgArrayJson def markread(msg_id):
'''This will mark the id for the message as read.'''
if not app_auth.is_logged_in():
return json.dumps({'error': 'Unable to send SMS, you are not logged in'})
if appdb.updateReadStatus(msg_id, 1) == 0:
return json.dumps({'error': 'Unable to update the read status.'})
else:
return json.dumps({'status': 'success'})
@app.route('/markallread', methods=['GET'])
def markallread():
'''This will mark every EVERY I said, message for the user id which should
be pulled from session info.'''
if not app_auth.is_logged_in():
return json.dumps({'error': 'Unable to send SMS, you are not logged in'})
userid = flask.session['account_id']
if appdb.updateMarkAllRead(userid) == 0:
return json.dumps({'error':
'Nothing to update or error updating the read status.'})
else:
return json.dumps({'status': 'success'})
return False
@app.route('/markallunread', methods=['GET'])
def markallunread():
'''This will mark every EVERY I said, message for the user id which should
be pulled from session info.'''
if not app_auth.is_logged_in():
return json.dumps({'error': 'Unable to send SMS, you are not logged in'})
userid = flask.session['account_id']
if appdb.updateMarkAllUnread(userid) == 0:
return json.dumps({'error': 'Nothing to update or error updating the read status.'})
else:
return json.dumps({'status':'success'})
return False
@app.route('/submitMessage', methods=['POST']) @app.route('/submitMessage', methods=['POST'])
def submitMessage(): def submitMessage():
@ -118,31 +185,40 @@ def submitMessage():
fromDid = flask.request.form['fromdid'] fromDid = flask.request.form['fromdid']
targetDid = flask.request.form['targetdid'] targetDid = flask.request.form['targetdid']
user_info = appdb.getUserInfo(flask.session['email'],flask.session['loginid']) # user_info = appdb.getUserInfo(
# flask.session['email'],
# flask.session['loginid'])
userid = flask.session['account_id'] userid = flask.session['account_id']
result = appdb.authIdforDID(userid,fromDid) result = appdb.authIdforDID(userid, fromDid)
if userid != result: if userid != result:
return json.dumps({'error': 'Unauthorized UserID of ' + str(userid) + " and DID id of " + str(result) + " and fromDID " + str(fromDid)}) return json.dumps({'error': 'Unauthorized UserID of ' + str(userid)
+ " and DID id of " + str(result) + " and fromDID "
+ str(fromDid)})
if appdb.validateFrom(fromDid) == False: if appdb.validateFrom(fromDid) is False:
return json.dumps({'error': 'Unauthorized source phone number.'}) return json.dumps({'error': 'Unauthorized source phone number.'})
uglyphone = appsms.uglyPhone(targetDid) uglyphone = appsms.uglyPhone(targetDid)
#pprint.pprint('Got ' + message + ',' + fromDid) # pprint.pprint('Got ' + message + ',' + fromDid)
msg_id = appsms.sendsms(uglyphone,fromDid,message) msg_id = appsms.sendsms(uglyphone, fromDid, message)
if msg_id == False: #This sends the sms! if msg_id is False: # This sends the sms!
returndata = json.dumps({'error': 'Unable to send SMS'}) returndata = json.dumps({'error': 'Unable to send SMS'})
else: else:
msgTS = time.strftime("%Y-%m-%dT%H:%m:%SZ") msgTS = time.strftime("%Y-%m-%dT%H:%m:%S+00:00")
appdb.logsms_db(msg_id, msgTS, 'outbound', uglyphone, fromDid, 0.0040, 'pending', message, result) appdb.logsms_db(msg_id, msgTS, 'outbound', uglyphone, fromDid,
returndata = json.dumps({"msg" : message, "fromdid" : fromDid, 'targetdid' : targetDid}) 0.0040, 'pending', message, result)
returndata = json.dumps({"msg": message,
"fromdid": fromDid,
"targetdid": targetDid})
return returndata return returndata
@app.route('/testAjax') @app.route('/testAjax')
def testAjax(): def testAjax():
return json.dumps({"msg" : 'Success!'}) return json.dumps({"msg": 'Success!'})
@app.route('/inbox') @app.route('/inbox')
def inbox(): def inbox():
@ -150,21 +226,49 @@ def inbox():
loggedin = True loggedin = True
else: else:
loggedin = False loggedin = False
loginId = flask.session['loginid']
results = appdb.getSMSbyAccount(loginId,10)
pprint.pprint(results)
return flask.render_template('inbox.html', loggedin=loggedin) return flask.render_template('inbox.html', loggedin=loggedin)
@app.route('/getInbox')
def returnInbox():
if not app_auth.is_logged_in():
return json.dumps({'error':
'Unable to send SMS, you are not logged in'})
# userid = flask.session['account_id']
loginId = flask.session['loginid']
results = appdb.getSMSbyAccount(loginId, 20)
# pprint.pprint(results)
jsonresult = ''
i = 0
for x in results:
if i >= 1:
jsonresult += ',' + json.dumps({"msg": x[9],
"fromdid": x[6],
"targetdid": x[7]})
else:
jsonresult = json.dumps({"msg": x[9],
"fromdid": x[6],
"targetdid": x[7]})
i += 1
return jsonresult
@app.route('/launch') @app.route('/launch')
def launchPage(): def launchPage():
if app_debug == '1':
pprint.pprint(flask.session)
if app_auth.is_logged_in(): if app_auth.is_logged_in():
loggedin = True loggedin = True
else: else:
loggedin = False loggedin = False
if app_debug == '1': if app_debug == '1':
pprint.pprint(loggedin) pprint.pprint(loggedin)
pprint.pprint("loggedin") pprint.pprint("loggedin")
return flask.render_template('launch.html',loggedin=loggedin)
return flask.render_template('launch.html', loggedin=loggedin)
@app.route('/pp') @app.route('/pp')
def PrivacyPolicy(): def PrivacyPolicy():
@ -174,7 +278,8 @@ def PrivacyPolicy():
loggedin = True loggedin = True
else: else:
loggedin = False loggedin = False
return flask.render_template('pp.html',loggedin=loggedin) return flask.render_template('pp.html', loggedin=loggedin)
@app.route('/tos') @app.route('/tos')
def tos(): def tos():
@ -182,7 +287,8 @@ def tos():
loggedin = True loggedin = True
else: else:
loggedin = False loggedin = False
return flask.render_template('tos.html',loggedin=loggedin) return flask.render_template('tos.html', loggedin=loggedin)
@app.route('/about') @app.route('/about')
def about(): def about():
@ -190,7 +296,8 @@ def about():
loggedin = True loggedin = True
else: else:
loggedin = False loggedin = False
return flask.render_template('about.html',loggedin=loggedin) return flask.render_template('about.html', loggedin=loggedin)
if __name__ == '__main__': if __name__ == '__main__':
app.run( app.run(

@ -1,12 +1,83 @@
{% include 'include/header.html' %} {% include 'include/header.html' %}
<title>Login Methods</title> <title>Inbox</title>
<script type="text/javascript"> <script type="text/javascript">
$("document").ready(function() { $("document").ready(function() {
$("#markallread").click(function() {
$.get("/markallread",function(data){
if (data == 'error') {
$('#status').text("Error updating.");
return false;
} else {
$('#status').text("All messages marked read.");
return true;
}
});
});
$("#markallunread").click(function() {
$.get("/markallunread",function(data){
if (data == 'error') {
$('#status').text("Error updating.");
return false;
} else {
$('#status').text("All messages marked unread.");
return true;
}
});
});
$("#refreshinbox").click(function() {
msgResults = "";
/* $.getJSON("/getInbox").done(function(result) {
alert("YAR");
}).fail(function(error ) {
alert( "Request Failed: " + error);
});*/
alert ("start");
$.ajax({
method: "GET",
url: "/getInbox",
data: ''
}).done(function(msg) {
//msgParsed = $.parseJSON(msg);
$.each(msgParsed, function(field) {
chatHtml = chatHtml + " GOT SOME " + field.msg;
});
alert(chatHtml);
if (msgParsed.error) {
alert('Error! Got "' + msgParsed.error + '"');
}
});
$("#chat-body").html(msgResults);
});
}); });
</script> </script>
</head> </head>
{% include 'include/navblock.html' %} {% include 'include/navblock.html' %}
<div id='inboxpage'> <div id='inboxpage'>
<h1>UNDER CONSTRUCTION</h1> <h1>UNDER CONSTRUCTION</h1>
<div id='status'></div>
<div id='chatlogcontroller'>
<input type='button' id='markallread' value='Mark all Read' /> |
<input type='button' id='markallunread' value='Mark all Unread' /> |
<input type='button' id='refreshinbox' value='Refresh Inbox' />
</div>
<div id='chat-body'>
</div>
{% for n in results %}
<div class='smschatlog'>
<div class='ts'>
{{n[2]}} Status: {{n[5]}} {{n[0]}}
</div>
<span class='from'>{{n[6]}}</span> to <span class='to'>{{n[7]}}</span>
{% if n[11] == 1 %}
Read
{% else %}
Unread
{% endif %}
<div class='smsbody'>{{n[9]}}</div>
</div>
{% endfor %}
</div> </div>
{% include 'include/footer.html' %} {% include 'include/footer.html' %}

Loading…
Cancel
Save