xenu_nntp/lib/nntp/tiny/server.py

45 lines
1.1 KiB
Python
Raw Normal View History

2024-11-20 21:17:03 -05:00
import enum
2024-11-26 16:55:44 -05:00
import socket
import threading
2024-11-20 21:17:03 -05:00
2024-11-26 16:24:32 -05:00
from typing import Callable
2024-11-22 23:57:14 -05:00
from nntp.tiny.newsgroup import Newsgroup
2024-11-26 16:55:44 -05:00
from nntp.tiny.session import Session
2024-11-22 23:57:14 -05:00
2024-11-20 21:17:03 -05:00
class ServerCapability(enum.Flag):
NONE = 0
AUTH = enum.auto()
POST = enum.auto()
class Server():
2024-11-26 16:24:32 -05:00
def __init__(self, connect_to_db: Callable):
self.connect_to_db = connect_to_db
self.capabilities = ServerCapability.NONE
self.newsgroups = dict()
2024-11-22 23:57:14 -05:00
2024-11-25 00:16:15 -05:00
self._init_newsgroups()
2024-11-22 23:57:14 -05:00
def _init_newsgroups(self):
2024-11-26 16:24:32 -05:00
db = self.connect_to_db()
for newsgroup in db.query(Newsgroup).each():
2024-11-22 23:57:14 -05:00
self.newsgroups[newsgroup.name.casefold()] = newsgroup
2024-11-26 16:55:44 -05:00
def run(self):
listener = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
listener.bind(('localhost', 1190))
listener.listen()
while True:
sock, addr = listener.accept()
2024-11-26 17:01:49 -05:00
def spawn():
session = Session(self, sock)
session.handle()
thread = threading.Thread(target=spawn)
2024-11-26 16:55:44 -05:00
thread.start()
listener.close()