from app import app, db
from flask import request
from api import Teli
from models import User, DID
import re

teli = Teli(app.config['TELI_TOKEN'], app.config['TELI_DID'])


@app.route('/', methods=['POST'])
def main():
    # subscribe <nick>
    message = request.form.get('message').split(' ')
    if message[0].lower() == 'subscribe' and len(message) >= 2:
        subscribe_user(message, src)


def subscribe_user(message, src):
    nick = re.sub('[^A-Za-z0-9_-]+', '', message[1])
    if len(nick >= 2):
        # Grab the first available DID
        did = DID.query.filter_by(user_id=None).first()
        if did:
            new_user = User(nick, src, did)
            db.session.add(new_user)
            db.session.commit()
            teli.send_sms(int(src=new_user.did.number),
                    int(new_user.user_phone),
                    '''Welcome to Mojave SMS IRC for DEFCON 24!\n
                    Written by @__tux for the Mojave!''')

@@ -42,16 +42,22 @@
  request
+, json
 %0Afrom ap
@@ -368,16 +368,23 @@
         
+return 
 subscrib
@@ -403,13 +403,86 @@
 ge, 
-src)%0A
+request.form.get('source'))%0A    return json.dumps(%7B'status': 'Call received'%7D)
 %0A%0Ade
@@ -585,14 +585,14 @@
 nick
+)
  %3E= 2
-)
 :%0A  
@@ -843,37 +843,8 @@
 sms(
-int(src=new_user.did.number),
 %0A   
@@ -906,19 +906,17 @@
         
-'''
+%22
 Welcome 
@@ -953,63 +953,141 @@
 4!%5Cn
-%0A                    Written by @__tux for the Mojave!'''
+Written by @__tux for the Mojave!%22,%0A                    src=int(new_user.did.number))%0A    return json.dumps(%7B'status': 'Call received'%7D
 )%0A

Fix errythang