diff --git a/bbs.py b/bbs.py index 4f0d058..3660b5e 100644 --- a/bbs.py +++ b/bbs.py @@ -1,14 +1,27 @@ +import mysql.connector import socket import time import os from _thread import * from funct import * +#DEFINE LISTENING HOST AND PORT BBS = socket.socket(socket.AF_INET, socket.SOCK_STREAM) host = '' # leave empty for any IP port = 8818 # you can change the incoming port to any desired UsersCount = 0 +# Conect to mysql (to make it work: pip install mysql-connector) +mydb = mysql.connector.connect( + host="localhost", #mysql server address + user="bbs", #bbs user you created on mysql + password="bbspass", + database="cbmbbs" +) + +# Define a database cursor to make the queries +mycursor = mydb.cursor(dictionary=True) + try: BBS.bind((host, port)) BBS.setsockopt(socket.IPPROTO_TCP,socket.TCP_NODELAY,True) @@ -17,7 +30,7 @@ print('SERVER > Waiting for a Connection..\r') -BBS.listen(8) +BBS.listen() def do_welcome(connection): @@ -27,7 +40,8 @@ def do_welcome(connection): #SEND HEADER FILE (.SEQ) # print ("STATUS > will send seq") - send_file(connection, "headbbs.seq") + connection.send(cbmencode("\n\n")) + send_file(connection, "welcome.seq") #THIS SECTION SENDS RANDOM THINGS (FOR YOU TO LEARN HOW) connection.send(cbmcursor("red")) #cbmcursor sends color red code @@ -43,34 +57,159 @@ def do_welcome(connection): connection.send(cbmencode("i am at 1,1")) cursorxy(connection,1,25) connection.send(cbmencode("i am at 1,25")) - time.sleep(2) + time.sleep(8) def do_login(connection): - #CLEAR SCREEN - connection.send(cbmcursor("clear")) - connection.send(cbmcursor("home")) - cursorxy(connection,1,3) + #CLEAR SCREEN + connection.send(cbmcursor("clear")) + connection.send(cbmcursor("home")) + cursorxy(connection,1,3) #positions cursor on third line + imf=0 #sets a flag for an infinite loop in while + maxtries=3 #max tries + attempts=0 + while True: #Login (not finished) connection.send(cbmencode("Username (new): ")) namex=input_line(connection) #input_line function reads a line print("decoded name: ",cbmdecode(namex)) # this is displayed on system side + uname=cbmdecode(namex) #receives the name of the user + + if (uname=="new"): + uname,realid = do_newuser(connection) + print("new user created") + break connection.send(cbmencode("Password: ")) pword=input_pass(connection) #input_pass reads a line but shows '*' print("decoded pass: ",cbmdecode(pword)) # this is displayed on system side + upass=cbmdecode(pword) #receives the password of the user - uname=cbmdecode(namex) #receives the name of the user + # Define QUERY + query="SELECT id,username,password FROM accounts WHERE username='%s'" + query=query % (uname) + + # Execute QUERY + mycursor.execute(query) + myresult = mycursor.fetchone() + + if (myresult == None): + connection.send(cbmencode("\n\nIncorrect username or password.\n\n")) + attempts=attempts+1 + else: + realpass = myresult['password'] + realid = myresult['id'] + + # Check if there is a match + if(realpass==upass): + print("match!!") + break + else: + connection.send(cbmencode("\n\nIncorrect username or password.\n\n")) + attempts=attempts+1 + if (attempts>=maxtries): + connection.send(cbmencode("\n\nSorry maximum number of attempts reached..\n\n")) + connection.close() + print("ok") + print("uname->",uname,"realid->",realid) + return uname,realid; + +def do_newuser(connection): + #CLEAR SCREEN + connection.send(cbmcursor("clear")) + connection.send(cbmcursor("home")) + cursorxy(connection,1,3) #positions cursor on third line + connection.send(cbmencode("Welcome new user!\nLet's create a new username.\n\n")) - return uname + #DEFINE Variables to control loop + maxtries=3 #max tries + attempts=0 -def do_bucle(connection,namex): + while True: + connection.send(cbmencode("Type your username: ")) + namex=input_line(connection) #input_line function reads a line + uname=cbmdecode(namex) + + # Define QUERY + query="SELECT id,username,password FROM accounts WHERE username='%s'" + query=query % (uname) + + # Execute QUERY + mycursor.execute(query) + myresult = mycursor.fetchone() + + if(myresult == None): + connection.send(cbmencode("Your username is: "+uname+"\nAre you sure? (y/n): ")) + letter=cbmdecode(get_char(connection)) + print("letra: ",letter) + connection.send(cbmencode("\n\n")) + attempts=attempts+1 + if(letter=="y" or letter=="Y"): + print("YES!") + break + else: + connection.send(cbmencode("\nError: User already exists.\n")) + attempts=attempts+1 + + if attempts >=maxtries: + connection.send(cbmencode("Sorry, maximum number of attempts.\nConnection closed. ")) + connection.close() + + attempts=0 + + #Asks for password + while True: + connection.send(cbmencode("\nType your password: ")) + pass1=input_pass(connection) #input_line function reads a line + pass1=cbmdecode(pass1) + connection.send(cbmencode("\nRepeat your password: ")) + pass2=input_pass(connection) #input_line function reads a line + pass2=cbmdecode(pass2) + if(pass2==pass1): + print("->"+pass1+"<-") + break + attempts=attempts+1 + if attempts >= maxtries: + connection.send(cbmencode("Sorry, maximum number of attempts.\nConnection closed. ")) + connection.close() + connection.send(cbmencode("\nError: Passwords don't match.\nTry Again.\n\n")) + + attempts=0 + + #Asks for email + while True: + connection.send(cbmencode("\nType your email: ")) + mail1=input_line(connection) #input_line function reads a line + mail1=cbmdecode(mail1) + connection.send(cbmencode("\nRepeat your email: ")) + mail2=input_line(connection) #input_line function reads a line + mail2=cbmdecode(mail2) + if(mail2==mail1): + print("email matches!") + break + attempts=attempts+1 + if attempts >= maxtries: + connection.send(cbmencode("Sorry, maximum number of attempts.\nConnection closed. ")) + connection.close() + connection.send(cbmencode("\nError: Emails don't match.\nTry Again.\n\n")) + + print("Creating account:",uname) + ## Creates user here + query="INSERT INTO accounts (username, password, email, active, level) VALUES ('%s','%s','%s',1,1)" + query=query % (uname,pass1,mail1) + mycursor.execute(query) + mydb.commit() #imperative + realid=mycursor.lastrowid + print("new id:",realid) + return uname,realid; + +def do_bucle(connection,namex,idx): while True: - print("Hola, ",namex) - connection.send(cbmencode("\n\nTYPE CHAR: ")) - #bucl=input_line(connection) + #Borra Pantalla y ENVIA MENU + print("Hola, ",namex,"->",idx) + connection.send(cbmencode("\n\nSelecciona Opcion: ")) bucl=get_char(connection) print(bucl) print("decoded: ",cbmdecode(bucl)) @@ -81,16 +220,17 @@ def do_bucle(connection,namex): ########################################### def user_session(connection): - time.sleep(3) + while True: #initializes terminal do_welcome(connection) + time.sleep(2) - userx = do_login(connection) + userx,idx = do_login(connection) - do_bucle(connection,userx) + do_bucle(connection,userx,idx) ############################################################################# diff --git a/funct.py b/funct.py index b6eae63..9d37d80 100644 --- a/funct.py +++ b/funct.py @@ -126,21 +126,23 @@ def input_line(connection): print("no data - closed connection") connection.close() break - delchar=0 if (data==b'\xff' or data==b'\xfb' or data==b'\x00' or data==b'\x01' or data==b'\xfd'): data=b'' if (data==b'\xff\xfb\x01\xff\xfb\x00\xff\xfd\x00'): data=b'' if (data==b'\x14'): - delchar=1 - tline=tline+data - if (delchar==1): - tline=tline[0:len(tline)-2] - connection.send(data) + if (len(tline)==0): + data=b'' + tline=tline[0:len(tline)-1] if (tline==b'\xff\xfb\x01\xff\xfb\x00\xff\xfd\x00'): tline=b'' + connection.send(data) if (data==b'\r' or data==b'\r\n' or data==b'\n'): break + if (data==b'\x14'): + tline=tline + else: + tline=tline+data return tline #reads a password (and when typing it shows '*' to the user) @@ -158,20 +160,22 @@ def input_pass(connection): data=b'' if (data==b'\xff\xfb\x01\xff\xfb\x00\xff\xfd\x00'): data=b'' - if (data==b'\r' or data==b'\r\n' or data==b'\n' or data==b'\n\r'): - data=b'' - bbb=999 if (data==b'\x14'): - delchar=1 - tline=tline+data - if (delchar==1): - tline=tline[0:len(tline)-2] + if (len(tline)==0): + data=b'' + tline=tline[0:len(tline)-1] if (tline==b'\xff\xfb\x01\xff\xfb\x00\xff\xfd\x00'): tline=b'' - if (bbb==999): + if (data==b'\x14' or data==b'\r' or data==b'\r\n' or data==b'\n' or data==b''): + connection.send(data) + else: + connection.send(b'*') + if (data==b'\r' or data==b'\r\n' or data==b'\n'): break + if (data==b'\x14'): + tline=tline else: - connection.send(b'*') + tline=tline+data return tline #similar to commodore basic get command (it just wait to type one char) diff --git a/menu.seq b/menu.seq new file mode 100644 index 0000000..7bdf28e --- /dev/null +++ b/menu.seq @@ -0,0 +1 @@ +Ÿ’°``````````````````````````````````````®} }}mAIN mENU }«``````````````````````````````````````³} Ÿ}}š1] bOARDSŸ }}š2] gOOD BYEŸ Ÿ}}š Ÿ}}š Ÿ}}š Ÿ}­``````````````````````````````````````½ \ No newline at end of file diff --git a/welcome.seq b/welcome.seq new file mode 100644 index 0000000..4db02c0 --- /dev/null +++ b/welcome.seq @@ -0,0 +1 @@ +’¬š ¬ž¬¢¬š ¢ ˜¢š ™¬¬š —¢»š ¡’¬¡ž¡’¢¡š’ ¡’ ˜¡š’ ˜¡™¡¡¡—¼’»š ŸcœOžMM•A–N—D™E›Rš ¼¿¿ž¡’¢¡’¢š¼¢˜¼¢¾™¡š ™¡—¼’»š x-Ÿ16 \ No newline at end of file