From 52892f88e2cae8c6f47795c038f08405fc04e783 Mon Sep 17 00:00:00 2001 From: Tanner Collin Date: Fri, 20 Aug 2021 20:23:43 +0000 Subject: [PATCH] Add CAPTCHA --- t0txt.py | 35 +++++++++++++++++++++++++++++------ 1 file changed, 29 insertions(+), 6 deletions(-) diff --git a/t0txt.py b/t0txt.py index 023259f..0713790 100644 --- a/t0txt.py +++ b/t0txt.py @@ -16,10 +16,13 @@ def help(): form = ( '
' '' - '' + '' + '
' + '
CAPTCHA: Who owns this site? ' '
'.format(URL, POST) ) return """ +
                         txt.t0.vc
 NAME
@@ -28,7 +31,6 @@ NAME
 USAGE
     <command> | curl -F '{0}=<-' {1}
     or upload from the web:
-
 {2}
 
 DESCRIPTION
@@ -68,21 +70,42 @@ def index():
 
 @flask_app.route('/', methods=['POST'])
 def new():
+    print()
     try:
+        is_web = 'web' in request.form
+
+        name = request.form.get('name', None)
+        if name:
+            print('Name filled out:', name)
+
+        captcha = request.form.get('captcha', None)
+        if is_web and captcha.lower() not in ['tanner', 'tanner collin', 'tannercollin', 'collin']:
+            print('Captcha failed:', captcha)
+            return redirect('https://txt.t0.vc/LPPZ')
+        else:
+            print('Captcha passed:', captcha)
+
         with shelve.open(DB) as db:
             nid = new_id()
             while nid in db:
                 nid = new_id()
 
             txt = request.form['txt']
-            if not txt: raise
-            if len(txt) > 250000: raise
+
+            if not txt:
+                print('Note is empty.')
+                raise
+            if len(txt) > 250000:
+                print('Note too large.')
+                raise
+
             db[nid] = txt
 
-            print('Adding note {}:\n{}'.format(nid, txt))
+        print('Adding note {} via {}:\n{}'.format(nid, 'web' if is_web else 'api', txt))
 
         url = '{}/{}'.format(URL, nid)
-        if 'web' in request.form:
+
+        if is_web:
             return redirect(url)
         else:
             return url + '\n'