2024-12-04 18:28:59 -05:00
|
|
|
import re
|
|
|
|
import socket
|
|
|
|
import ssl
|
2024-12-04 23:07:30 -05:00
|
|
|
import datetime
|
2024-12-04 18:28:59 -05:00
|
|
|
|
2024-12-04 23:52:29 -05:00
|
|
|
from typing import Optional
|
|
|
|
|
2024-12-04 18:28:59 -05:00
|
|
|
from nntp.tiny.socket import Connection
|
|
|
|
from nntp.tiny.host import Host
|
|
|
|
from nntp.tiny.response import Response, ResponseCode
|
2024-12-04 23:07:30 -05:00
|
|
|
from nntp.tiny.remote import RemoteNewsgroup
|
2024-12-04 23:52:29 -05:00
|
|
|
from nntp.tiny.message import MessageRange
|
2024-12-04 18:28:59 -05:00
|
|
|
|
2024-12-04 21:43:39 -05:00
|
|
|
class ClientException(Exception):
|
|
|
|
pass
|
|
|
|
|
2024-12-04 22:35:49 -05:00
|
|
|
class ClientEOFException(ClientException):
|
|
|
|
def __str__(self):
|
|
|
|
return 'Unexpected client EOF'
|
|
|
|
|
2024-12-04 18:28:59 -05:00
|
|
|
class Client(Connection):
|
2024-12-04 21:43:39 -05:00
|
|
|
RE_SPLIT = re.compile(r'\s+')
|
|
|
|
|
|
|
|
def _read_response(self):
|
|
|
|
line = self.readline()
|
|
|
|
|
|
|
|
if line == '':
|
2024-12-04 23:07:30 -05:00
|
|
|
raise ClientEOFException()
|
2024-12-04 21:43:39 -05:00
|
|
|
|
|
|
|
parts = self.RE_SPLIT.split(line.rstrip(), 1)
|
|
|
|
|
|
|
|
if len(parts) == 0:
|
|
|
|
return
|
|
|
|
elif len(parts) == 1:
|
|
|
|
return Response(ResponseCode(int(parts[0])))
|
|
|
|
else:
|
|
|
|
return Response(ResponseCode(int(parts[0])), parts[1])
|
|
|
|
|
2024-12-04 18:28:59 -05:00
|
|
|
def __init__(self, host: str, port: int, tls: bool=False):
|
|
|
|
sock = socket.create_connection((host, port))
|
|
|
|
|
|
|
|
if tls:
|
|
|
|
sslctx = ssl.create_default_context()
|
|
|
|
sslctx.check_hostname = False
|
|
|
|
sslctx.verify_mode = ssl.CERT_NONE
|
|
|
|
|
|
|
|
if Host.is_hostname(host):
|
|
|
|
sock = sslctx.wrap_socket(sock, server_hostname=host)
|
|
|
|
else:
|
|
|
|
sock = sslctx.wrap_socket(sock)
|
|
|
|
|
|
|
|
self.host = host
|
|
|
|
self.port = port
|
|
|
|
|
|
|
|
super().__init__(sock)
|
|
|
|
|
2024-12-04 21:43:39 -05:00
|
|
|
response = self._read_response()
|
|
|
|
|
|
|
|
if response is None:
|
|
|
|
raise ClientException('Server not ready')
|
2024-12-04 18:28:59 -05:00
|
|
|
|
|
|
|
def request(self, *args):
|
|
|
|
self.print(' '.join(args))
|
|
|
|
|
2024-12-04 21:43:39 -05:00
|
|
|
return self._read_response()
|
2024-12-04 22:35:49 -05:00
|
|
|
|
|
|
|
def each_response_line(self):
|
|
|
|
while True:
|
|
|
|
line = self.readline()
|
|
|
|
|
|
|
|
if line == '':
|
|
|
|
raise ClientEOFException()
|
|
|
|
|
|
|
|
line = line.rstrip()
|
|
|
|
|
|
|
|
if line == '.':
|
|
|
|
break
|
|
|
|
|
|
|
|
yield line
|
2024-12-04 23:07:30 -05:00
|
|
|
|
2024-12-05 08:39:17 -05:00
|
|
|
def each_newsgroup_since(self, timestamp: datetime.datetime):
|
2024-12-04 23:07:30 -05:00
|
|
|
date = timestamp.strftime('%Y%m%d')
|
|
|
|
time = timestamp.strftime('%H%M%S')
|
|
|
|
|
2024-12-05 08:39:17 -05:00
|
|
|
response = self.request('NEWGROUPS', date, time)
|
2024-12-04 23:07:30 -05:00
|
|
|
|
|
|
|
for line in self.each_response_line():
|
|
|
|
parts = self.RE_SPLIT.split(line)
|
|
|
|
|
|
|
|
if len(parts) != 4:
|
|
|
|
raise ClientException('Unexpected result from NEWGROUPS')
|
|
|
|
|
|
|
|
yield RemoteNewsgroup(parts[0],
|
|
|
|
int(parts[1]),
|
|
|
|
int(parts[2]),
|
|
|
|
parts[3] == 'y')
|