Initial commit

This commit is contained in:
mogad0n 2021-01-20 07:20:30 +05:30
commit 50c2710925
No known key found for this signature in database
GPG Key ID: 13851DB523F8CE74
9 changed files with 240 additions and 0 deletions

1
.gitignore vendored Normal file
View File

@ -0,0 +1 @@
venv/

1
README.md Normal file
View File

@ -0,0 +1 @@
TODO: Update Readme

66
flaskapp.py Normal file
View File

@ -0,0 +1,66 @@
from flask import Flask, render_template, url_for, request, redirect, flash
from forms import RegistrationForm, VerificationForm
from irc_register import ircregister
from irc_verify import ircverify
app = Flask(__name__)
app.config['SECRET_KEY'] = '189aee7e774aaedce1269c5a35966db7' #replace this with yours
# Placeholder for the home page. You can place the home page
# in the `template` directory.
#
# @app.route('/')
# def hello():
# return render_template('home.html')
# Example for a kiwi instance.
#
# @app.route('/kiwi')
# def kiwi():
# return redirect("<YOUR KIWI URL>")
# For now we will assume that email verification
# is required.
@app.route('/register', methods=['GET', 'POST'])
def register():
form = RegistrationForm()
if request.method == 'POST':
username = request.form.get('username')
password = request.form.get('password')
email = request.form.get('email')
response = ircregister(username, password, email)
if response == "server failure":
flash("Server Unavailable")
elif response == "433":
flash("Username already taken. Please select a different username")
elif response == "success":
return redirect(url_for('verify'))
elif response == "failure":
flash("Failure! Please try after some time or use NickServ.")
return render_template('register.html', title='Register', form=form)
@app.route('/verify', methods=['GET', 'POST'])
def verify():
form = VerificationForm()
if request.method == 'POST':
username = request.form.get('username')
verif_code = request.form.get('verif_code')
response = ircverify(username, verif_code)
if response == "server failure":
flash("Server Unavailable")
elif response == "433":
flash("Username under use. Please check your username or visit us for help")
elif response == "success":
return redirect(url_for('<Either `hello` or `kiwi`>'))
elif response == "failure":
flash("Failure! Please try after some time or use NickServ.")
return render_template('verify.html', title='Verify', form=form)
if __name__ == '__main__':
app.run(debug=True)

16
forms.py Normal file
View File

@ -0,0 +1,16 @@
from flask_wtf import FlaskForm
from wtforms import StringField, PasswordField, SubmitField
from wtforms.validators import DataRequired, Length, EqualTo, Email
class RegistrationForm(FlaskForm):
username = StringField('Username', validators=[DataRequired(), Length(min=1, max=32)])
email = StringField('Email', validators=[DataRequired(), Email()])
password = PasswordField('Password', validators=[DataRequired()])
confirm_password = PasswordField('Confirm Password', validators=[DataRequired(), EqualTo('password')])
submit = SubmitField('Register')
class VerificationForm(FlaskForm):
username = StringField('Username', validators=[DataRequired(), Length(min=1, max=32)])
verif_code = StringField('Verification Code', validators=[DataRequired()])
submit = SubmitField('Verify')

45
irc_register.py Normal file
View File

@ -0,0 +1,45 @@
import socket, irctokens
def ircregister(username, password, email):
# define the variables
d = irctokens.StatefulDecoder()
e = irctokens.StatefulEncoder()
s = socket.socket()
#connecting to the server
s.connect(("127.0.0.1", 6667))
#defining the send function with proper formatting
def _send(line):
print(f"> {line.format()}")
e.push(line)
while e.pending():
e.pop(s.send(e.pending()))
# registering the connection to the server
_send(irctokens.build("USER", [username, "0", "*", username]))
_send(irctokens.build("NICK", [username]))
# go through the cases
while True:
lines = d.push(s.recv(1024))
if lines == None: #if nothing is received from server
return "server error"
break
for line in lines:
print(f"< {line.format()}")
if line.command == "433": # if nickname already in use
return "433"
elif line.command == "005": # when 005 is received pass the nickserv register command command
_send(irctokens.build("PRIVMSG", ["NickServ", f"REGISTER {password} {email}"]))
if line.command == "NOTICE" and line.params == [username, f"Account created, pending verification; verification code has been sent to {email}"]: # if Services respond with appropriate notice NOTICE
_send(irctokens.build("QUIT"))
return "success"

44
irc_verify.py Normal file
View File

@ -0,0 +1,44 @@
import socket, irctokens
def ircverify(username, verif_code):
# define the variables
d = irctokens.StatefulDecoder()
e = irctokens.StatefulEncoder()
s = socket.socket()
#connecting to the server
s.connect(("127.0.0.1", 6667))
#defining the send function with proper formatting
def _send(line):
print(f"> {line.format()}")
e.push(line)
while e.pending():
e.pop(s.send(e.pending()))
# registering the connection to the server
_send(irctokens.build("USER", [username, "0", "*", username]))
_send(irctokens.build("NICK", [username]))
# go through the cases
while True:
lines = d.push(s.recv(1024))
if lines == None: #if nothing is received from server
return "server error"
break
for line in lines:
print(f"< {line.format()}")
if line.command == "433": # if nickname already in use
return "433"
elif line.command == "005": # when 005 is received pass the nickserv register command command
_send(irctokens.build("PRIVMSG", ["NickServ", f"VERIFY {username} {verif_code}"]))
if line.command == "NOTICE" and line.params == [username, "Account created"]: # if Services respond with appropriate notice NOTICE
_send(irctokens.build("QUIT"))
return "success"

9
requirements.txt Normal file
View File

@ -0,0 +1,9 @@
click==7.1.2
Flask==1.1.2
Flask-WTF==0.14.3
irctokens==2.0.0
itsdangerous==1.1.0
Jinja2==2.11.2
MarkupSafe==1.1.1
Werkzeug==1.0.1
WTForms==2.3.3

30
templates/register.html Normal file
View File

@ -0,0 +1,30 @@
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>Register</title>
</head>
<body>
{% for message in get_flashed_messages() %}
{{ message }}
{% endfor %}
<pre>
<form method="POST" action="/register">
{{ form.hidden_tag() }}
<fieldset>
<legend>Sign Up</legend>
{{ form.username.label }}
{{ form.username }}
{{ form.email.label }}
{{ form.email }}
{{ form.password.label }}
{{ form.password }}
{{ form.confirm_password.label }}
{{ form.confirm_password }}
</fieldset>
{{ form.submit }}
</form>

28
templates/verify.html Normal file
View File

@ -0,0 +1,28 @@
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>Verify</title>
</head>
<body>
{% for message in get_flashed_messages() %}
{{ message }}
{% endfor %}
<pre>
Please check your the registered email for the verification code (check in spam!)
<form method="POST" action="/verify">
{{ form.hidden_tag() }}
<fieldset>
<legend>Verify Your Account</legend>
{{ form.username.label }}
{{ form.username }}
{{ form.verif_code.label }}
{{ form.verif_code }}
</fieldset>
{{ form.submit }}
</form>