moar nntp, working post list

nntp
magical 2022-08-08 08:23:08 +00:00
parent 19269a1ade
commit d43b3f9979
2 changed files with 45 additions and 17 deletions

View File

@ -8,6 +8,8 @@ import ssl
import email.utils import email.utils
from collections import namedtuple from collections import namedtuple
import os
class BBJNews(object): class BBJNews(object):
# this module isnt exactly complete. The below description claims # this module isnt exactly complete. The below description claims
# `all of its endpoints are mapped to native methods` though this # `all of its endpoints are mapped to native methods` though this
@ -74,22 +76,23 @@ class BBJNews(object):
If you set this to False, anonymous network usage is If you set this to False, anonymous network usage is
guaranteed. guaranteed.
""" """
host = "news.tildeverse.org"
self.host = host self.host = host
self.port = port self.port = port
self.tls = https self.tls = https
self.group = "tilde.team" self.group = "local.test"
self.user_name = None self.user_name = None
self.user_auth = None self.user_auth = None
self.send_auth = True self.send_auth = True
self.user = { self.user = {
"user_id": "", "user_id": "",
"user_name": "", "user_name": "anonymous",
"is_admin": False, "is_admin": False,
"color": 0, "color": 0,
} }
self.instance_info = { self.instance_info = {
"instance_name": "", "instance_name": "TEST nntp TEST",
"allow_anon": True, "allow_anon": True,
"admins": [], "admins": [],
} }
@ -120,12 +123,11 @@ class BBJNews(object):
def connect(self): def connect(self):
self.conn = nntplib.NNTP(self.host, self.port) self.conn = nntplib.NNTP(self.host, self.port, usenetrc=False)
if self.tls: if self.tls:
context = ssl.create_default_context() context = ssl.create_default_context()
self.conn.starttls(context) self.conn.starttls(context)
# TODO: reconnect automatically # TODO: reconnect automatically
@ -147,6 +149,19 @@ class BBJNews(object):
See raise_exception() for details on how this function reacts See raise_exception() for details on how this function reacts
to various failure conditions. to various failure conditions.
""" """
if endpoint == "get_me":
return {"error": False, "data": self.user.copy(), "usermap": {}}
return {
"error": {
"code": 0,
"description": "unsupported endpoint",
},
"data": None,
"usermap": {},
}
headers = {"Content-Type": "application/json"} headers = {"Content-Type": "application/json"}
if params.get("no_auth"): if params.get("no_auth"):
params.pop("no_auth") params.pop("no_auth")
@ -222,8 +237,9 @@ class BBJNews(object):
"admins": (list) // usernames of those who have admin rights on the server "admins": (list) // usernames of those who have admin rights on the server
} }
""" """
response = self("instance_info") # TODO
self.instance_info = response["data"] #response = self("instance_info")
#self.instance_info = response["data"]
# unsupported # unsupported
@ -321,14 +337,12 @@ class BBJNews(object):
) )
""" """
self.conn.login(user_name, user_auth, usernetrc=False) self.conn.login(user_name, user_auth, usernetrc=False)
# TODO: catch
if check_validity and not self.validate_credentials(user_name, user_auth):
self.user_auth = self.user_name = None
raise ConnectionRefusedError("Auth and User do not match")
self.user_auth = user_auth self.user_auth = user_auth
self.user_name = user_name self.user_name = user_name
self.user = self("get_me")["data"] #self.user = self("get_me")["data"]
self.user['user_name'] = user_name
return True return True
@ -440,7 +454,7 @@ class BBJNews(object):
self.user_name = params["user_name"] self.user_name = params["user_name"]
if params.get("auth_hash"): if params.get("auth_hash"):
self.user_auth = params["auth_hash"] self.user_auth = params["auth_hash"]
self.user = dict(params) self.user.update(**params)
return self.user return self.user
@ -486,6 +500,8 @@ class BBJNews(object):
# :lines - the number of lines in the body (deprecated) # :lines - the number of lines in the body (deprecated)
response, overviews = self.conn.over((first, None)) response, overviews = self.conn.over((first, None))
with open("overview_cache.bbj.json", "wt") as f:
json.dump(overviews, f)
if False: if False:
# build up a map of message references # build up a map of message references
@ -546,11 +562,14 @@ class BBJNews(object):
print(usermap[author_id]["user_name"]) print(usermap[author_id]["user_name"])
print(message["body"]) print(message["body"])
""" """
return {}, {}
response = self("thread_load", response = self("thread_load",
format=format, thread_id=thread_id, op_only=op_only) format=format, thread_id=thread_id, op_only=op_only)
return response["data"], response["usermap"] return response["data"], response["usermap"]
# POST
def thread_create(self, title, body): def thread_create(self, title, body):
""" """
Submit a new thread, and return its new object. Requires the Submit a new thread, and return its new object. Requires the
@ -558,15 +577,20 @@ class BBJNews(object):
120 chars in length, else UserWarning is raised. Body must 120 chars in length, else UserWarning is raised. Body must
also not be empty. also not be empty.
""" """
raise NotImplementedError
response = self("thread_create", title=title, body=body) response = self("thread_create", title=title, body=body)
return response["data"] return response["data"]
# POST
def thread_reply(self, thread_id, body): def thread_reply(self, thread_id, body):
""" """
Submits a new reply to a thread and returns the new object. Submits a new reply to a thread and returns the new object.
Requires the thread's id and a non-empty body string. Requires the thread's id and a non-empty body string.
""" """
raise NotImplementedError
response = self("thread_reply", thread_id=thread_id, body=body) response = self("thread_reply", thread_id=thread_id, body=body)
return response["data"] return response["data"]

View File

@ -20,7 +20,10 @@ Please mail me (~desvox) for feedback and for any of your
"OH MY GOD WHY WOULD YOU DO THIS"'s or "PEP8 IS A THING"'s. "OH MY GOD WHY WOULD YOU DO THIS"'s or "PEP8 IS A THING"'s.
""" """
from network import BBJ, URLError import sys, os; sys.path.append(os.path.dirname(os.path.dirname(__file__)))
#from network import BBJ, URLError
from nntp_client import BBJNews as BBJ, URLError
from string import punctuation from string import punctuation
from datetime import datetime from datetime import datetime
from sys import argv, version from sys import argv, version
@ -2599,9 +2602,10 @@ def main():
exit() exit()
try: try:
network = BBJ(get_arg("host", "127.0.0.1"), #network = BBJ(get_arg("host", "127.0.0.1"),
get_arg("port", 7099), # get_arg("port", 7099),
get_arg("https", False, False)) # get_arg("https", False, False))
network = BBJ(https=get_arg("https", False, False))
except URLError as e: except URLError as e:
# print the connection error in red # print the connection error in red
exit("\033[0;31m%s\033[0m" % repr(e)) exit("\033[0;31m%s\033[0m" % repr(e))