xenu_nntp/lib/nntp/tiny/server.py
2024-12-03 10:19:00 -05:00

51 lines
1.3 KiB
Python

import enum
import socket
import threading
from configparser import ConfigParser
from nntp.tiny.db import Database
from nntp.tiny.newsgroup import Newsgroup
from nntp.tiny.session import Session
class ServerCapability(enum.Flag):
NONE = 0
AUTH = enum.auto()
POST = enum.auto()
class Server():
def __init__(self, config: ConfigParser):
self.config = config
self.capabilities = ServerCapability.NONE
self.newsgroups = dict()
self._init_newsgroups()
def connect_to_db(self):
return Database.connect(self.config['database']['path'])
def _init_newsgroups(self):
db = self.connect_to_db()
for newsgroup in db.query(Newsgroup).each():
self.newsgroups[newsgroup.name.casefold()] = newsgroup
def run(self):
host = self.config['listen']['host']
port = int(self.config['listen']['port'])
listener = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
listener.bind((host, port))
listener.listen()
while True:
sock, addr = listener.accept()
def spawn():
session = Session(self, sock)
session.handle()
thread = threading.Thread(target=spawn)
thread.start()
listener.close()