Example #1
0
    def route_account(self):
        create_success = False
        if self.method == "POST":
            # Parse body.
            body = self.parse_body()
            username = body.get("username", "")
            password = body.get("password", "")
            # Check if this username is used.
            if not (check_account_exist(username,
                                        password)) and (username != ""):
                add_account(username, password)
                # Add this user to account_list.
                log("username and password", username, password)
                create_success = True

        header = "HTTP/1.1 200 OK\r\nContent-Type: text/html\r\n\r\n"
        body = template("create_success.html") if create_success else template(
            "account.html")

        if not create_success:
            # Decide if user create account or not.
            if (self.method == "POST"):
                body = body.replace('{{message}}',
                                    "This username cannot be used !")
            else:
                body = body.replace('{{message}}', "")

        reply = header + body
        return reply.encode(encoding="utf-8")
Example #2
0
    def route_login(self):
        # Check if user logged in before.
        if (check_cookie(self.cookie)):
            header = "HTTP/1.1 200 OK\r\nContent-Type: text/html\r\n\r\n"
            body = template("login_success.html")
            reply = header + body
            return reply.encode(encoding="utf-8")

        login_success = False
        # If body is empty means that it is jump from somewhere.
        if self.method == "POST":
            # Parse body.
            body = self.parse_body()
            username = body.get("username", "")
            password = body.get("password", "")
            # Add this user to account_list.
            login_success = check_account(username, password)

        header = "HTTP/1.1 200 OK\r\nContent-Type: text/html\r\n\r\n"
        body = template("login_success.html") if login_success else template(
            "login.html")
        if not login_success:
            # Decide if user enter account or not.
            if (self.method == "POST"):
                body = body.replace('{{message}}',
                                    "Wrong username or password !")
            else:
                body = body.replace('{{message}}', "")
        else:
            add_cookie(self.cookie, username)

        reply = header + body
        return reply.encode(encoding="utf-8")
Example #3
0
    def route_starburst(self):
        if not (check_cookie(self.cookie)):
            header = "HTTP/1.1 200 OK\r\nContent-Type: text/html\r\n\r\n"
            body = template("login.html")
            body = body.replace('{{message}}', "")
            reply = header + body
            return reply.encode(encoding="utf-8")

        header = "HTTP/1.1 200 OK\r\nContent-Type: text/html\r\n\r\n"
        body = template("starburst.html")

        reply = header + body
        return reply.encode(encoding="utf-8")
Example #4
0
    def route_message(self):
        msgs = ""
        msg = Message()
        if self.method == "POST":
            # Parse body.
            body = self.parse_body()
            msg.author = body.get("author", "")
            msg.message = body.get("message", "")
            msg.save_txt()

        # Load previous message from file.
        try:
            msgs = msg.load_txt()
        except:
            pass
        header = "HTTP/1.1 200 OK\r\nContent-Type: text/html\r\n\r\n"
        body = template("message.html")

        log("msgs ", msgs)

        body = body.replace('{{message}}', msgs)
        reply = header + body
        return reply.encode(encoding="utf-8")
Example #5
0
 def route_index(self):
     header = "HTTP/1.1 200 OK\r\nContent-Type: text/html\r\n\r\n"
     body = template("index.html")
     reply = header + body
     return reply.encode(encoding="utf-8")
Example #6
0
 def route_logout(self):
     delete_cookie(self.cookie)
     header = "HTTP/1.1 200 OK\r\nContent-Type: text/html\r\n\r\n"
     body = template("logout.html")
     reply = header + body
     return reply.encode(encoding="utf-8")