mirror of
https://github.com/Xevion/tcp-chat.git
synced 2026-01-31 02:26:04 -06:00
new sqlite3 database for recording messages by clients and server
This commit is contained in:
@@ -1,9 +1,13 @@
|
|||||||
from typing import List
|
from typing import List
|
||||||
|
|
||||||
|
import os
|
||||||
import webcolors
|
import webcolors
|
||||||
|
|
||||||
|
__BASE_DIR = os.path.dirname(os.path.abspath(__file__))
|
||||||
|
|
||||||
HEADER_LENGTH = 10
|
HEADER_LENGTH = 10
|
||||||
MINIMUM_CONTRAST = 4.65
|
MINIMUM_CONTRAST = 4.65
|
||||||
|
DATABASE = os.path.join(__BASE_DIR, 'messages.db')
|
||||||
|
|
||||||
|
|
||||||
class Types:
|
class Types:
|
||||||
|
|||||||
@@ -0,0 +1,34 @@
|
|||||||
|
import sqlite3
|
||||||
|
from typing import List
|
||||||
|
import logging
|
||||||
|
|
||||||
|
import constants
|
||||||
|
|
||||||
|
logger = logging.getLogger('database')
|
||||||
|
|
||||||
|
conn = sqlite3.connect(constants.DATABASE)
|
||||||
|
logger.debug(f"Connected to '{constants.DATABASE}'")
|
||||||
|
|
||||||
|
logger.debug("Constructing 'message' table.")
|
||||||
|
cur = conn.cursor()
|
||||||
|
conn.execute('''CREATE TABLE IF NOT EXISTS message
|
||||||
|
(id INTEGER PRIMARY KEY,
|
||||||
|
nickname TEXT NOT NULL,
|
||||||
|
connection_hash TEXT NOT NULL,
|
||||||
|
color TEXT DEFAULT '#000000',
|
||||||
|
message TEXT DEFAULT '',
|
||||||
|
timestamp INTEGER NOT NULL)''')
|
||||||
|
conn.commit()
|
||||||
|
|
||||||
|
|
||||||
|
def add_message(nickname: str, hash: str, color: str, message: str, timestamp: int):
|
||||||
|
cur.execute('''INSERT INTO message (nickname, connection_hash, color, message, timestamp)
|
||||||
|
VALUES (?, ?, ?, ?, ?)''', [nickname, hash, color, message, timestamp])
|
||||||
|
conn.commit()
|
||||||
|
|
||||||
|
|
||||||
|
def get_messages(columns: List[str] = None):
|
||||||
|
if columns is None:
|
||||||
|
cur.execute('''SELECT * FROM message''')
|
||||||
|
return cur.fetchall()
|
||||||
|
|
||||||
@@ -8,6 +8,8 @@ from typing import Any, List
|
|||||||
|
|
||||||
import constants
|
import constants
|
||||||
import helpers
|
import helpers
|
||||||
|
# noinspection PyUnresolvedReferences
|
||||||
|
from server import db
|
||||||
from server.commands import CommandHandler
|
from server.commands import CommandHandler
|
||||||
|
|
||||||
logger = logging.getLogger('handler')
|
logger = logging.getLogger('handler')
|
||||||
@@ -113,6 +115,10 @@ class Client(BaseClient):
|
|||||||
color=self.color.hex
|
color=self.color.hex
|
||||||
))
|
))
|
||||||
|
|
||||||
|
# Record the message in the DB.
|
||||||
|
db.add_message(self.nickname, self.id, self.color.hex, data['content'], int(time.time()))
|
||||||
|
|
||||||
|
# Process commands
|
||||||
command = data['content'].strip()
|
command = data['content'].strip()
|
||||||
if command.startswith('/'):
|
if command.startswith('/'):
|
||||||
args = data['content'][1:].strip().split()
|
args = data['content'][1:].strip().split()
|
||||||
|
|||||||
+3
-2
@@ -11,8 +11,6 @@ server = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
|
|||||||
server.bind((host, port))
|
server.bind((host, port))
|
||||||
server.listen()
|
server.listen()
|
||||||
|
|
||||||
logging.basicConfig(level=logging.DEBUG,
|
|
||||||
format='[%(asctime)s] [%(levelname)s] [%(threadName)s] %(message)s')
|
|
||||||
logger = logging.getLogger('server')
|
logger = logging.getLogger('server')
|
||||||
|
|
||||||
clients = []
|
clients = []
|
||||||
@@ -40,4 +38,7 @@ def receive():
|
|||||||
|
|
||||||
|
|
||||||
if __name__ == '__main__':
|
if __name__ == '__main__':
|
||||||
|
from server import db
|
||||||
|
|
||||||
receive()
|
receive()
|
||||||
|
db.conn.close()
|
||||||
|
|||||||
Reference in New Issue
Block a user