some nntp client things

nntp
magical 2022-08-07 01:31:18 +00:00
parent 23c8abfae2
commit 6d15940943
1 changed files with 26 additions and 39 deletions

View File

@ -1,7 +1,8 @@
from urllib.error import URLError from urllib.error import URLError
import urllib.request as url import urllib.request as url
import nntplib
from hashlib import sha256 from hashlib import sha256
from time import time import time
import json import json
@ -49,7 +50,7 @@ class BBJ(object):
See the offical API error documentation for more details. See the offical API error documentation for more details.
""" """
def __init__(self, host="127.0.0.1", port=7099, https=False): def __init__(self, host="127.0.0.1", port=119, https=False):
""" """
Optionally takes port and host as kwargs. It will immediately Optionally takes port and host as kwargs. It will immediately
try to resolve a connection to the server, if its down, it try to resolve a connection to the server, if its down, it
@ -193,6 +194,7 @@ class BBJ(object):
self.instance_info = response["data"] self.instance_info = response["data"]
# unsupported
def validate(self, key, value, exception=AssertionError): def validate(self, key, value, exception=AssertionError):
""" """
Uses the server's db_validate method to verify the validty Uses the server's db_validate method to verify the validty
@ -212,21 +214,6 @@ class BBJ(object):
# or you can handle it as a boolean like this: # or you can handle it as a boolean like this:
is_okay = bbj.validate("title", "teacups and roses <3", exception=None) is_okay = bbj.validate("title", "teacups and roses <3", exception=None)
""" """
response = self(
"db_validate",
no_auth=True,
key=key,
value=value
)
if not response["data"]["bool"]:
if not exception:
return False
description = response["data"]["description"]
error = exception(description)
error.description = description
raise error
return True return True
@ -313,6 +300,7 @@ class BBJ(object):
return True return True
# AUTHINFO?
def validate_credentials(self, user_name, user_auth, exception=True): def validate_credentials(self, user_name, user_auth, exception=True):
""" """
Pings the server to check that user_name can be authenticated with Pings the server to check that user_name can be authenticated with
@ -352,6 +340,7 @@ class BBJ(object):
return False return False
# unsupported
def user_is_registered(self, user_name): def user_is_registered(self, user_name):
""" """
Returns True or False whether user_name is registered Returns True or False whether user_name is registered
@ -366,6 +355,7 @@ class BBJ(object):
return response["data"] return response["data"]
# unsupported
def user_register(self, user_name, user_auth, hash_auth=True, set_as_user=True): def user_register(self, user_name, user_auth, hash_auth=True, set_as_user=True):
""" """
Register user_name into the system with user_auth. Unless hash_auth Register user_name into the system with user_auth. Unless hash_auth
@ -414,15 +404,15 @@ class BBJ(object):
`color`. On success, the newly updated user object is `color`. On success, the newly updated user object is
returned and is also internalized as self.user. returned and is also internalized as self.user.
""" """
response = self("user_update", **params)
if params.get("user_name"): if params.get("user_name"):
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 = self("get_me")["data"] self.user = dict(params)
return response["data"] return self.user
# unused
def user_get(self, user_id_or_name): def user_get(self, user_id_or_name):
""" """
Return a full user object by their id or username. Return a full user object by their id or username.
@ -500,13 +490,14 @@ class BBJ(object):
"body": self.format_message(body, format), "body": self.format_message(body, format),
"author": author or self.user["user_id"], "author": author or self.user["user_id"],
"post_id": post_id, "post_id": post_id,
"created": time(), "created": time.time(),
"edited": False, "edited": False,
"send_raw": False, "send_raw": False,
"thread_id": "gibberish" "thread_id": "gibberish"
} }
# unused
def format_message(self, body, format="sequential"): def format_message(self, body, format="sequential"):
""" """
Send `body` to the server to be formatted according to `format`, Send `body` to the server to be formatted according to `format`,
@ -515,7 +506,7 @@ class BBJ(object):
response = self("format_message", body=body, format=format) response = self("format_message", body=body, format=format)
return response["data"] return response["data"]
# unsupported
def message_delete(self, thread_id, post_id): def message_delete(self, thread_id, post_id):
""" """
Delete message `post_id` from `thread_id`. The same rules apply Delete message `post_id` from `thread_id`. The same rules apply
@ -526,7 +517,7 @@ class BBJ(object):
response = self("delete_post", thread_id=thread_id, post_id=post_id) response = self("delete_post", thread_id=thread_id, post_id=post_id)
return response["data"] return response["data"]
# done
def edit_query(self, thread_id, post_id): def edit_query(self, thread_id, post_id):
""" """
Queries ther server database to see if a post can Queries ther server database to see if a post can
@ -536,22 +527,19 @@ class BBJ(object):
Returns a message object on success, or raises Returns a message object on success, or raises
a UserWarning describing why it failed. a UserWarning describing why it failed.
""" """
response = self("edit_query", thread_id=thread_id, post_id=int(post_id)) raise UserWarning("NNTP posts cannot be edited")
return response["data"]
# done
def can_edit(self, thread_id, post_id): def can_edit(self, thread_id, post_id):
""" """
Return bool True/False that the post at thread_id | post_id Return bool True/False that the post at thread_id | post_id
can be edited by the logged in user. Will not raise UserWarning. can be edited by the logged in user. Will not raise UserWarning.
""" """
try: return False
result = bool(self.edit_query(thread_id, post_id))
except UserWarning:
result = False
return result
# done
def edit_message(self, thread_id, post_id, new_body): def edit_message(self, thread_id, post_id, new_body):
""" """
Requires the thread_id and post_id. The edit flag is then Requires the thread_id and post_id. The edit flag is then
@ -561,12 +549,9 @@ class BBJ(object):
Will raise UserWarning if server editing rules are violated. Will raise UserWarning if server editing rules are violated.
See also `can_edit` and `edit_query` See also `can_edit` and `edit_query`
""" """
response = self( raise UserWarning("NNTP posts cannot be edited")
"edit_post", thread_id=thread_id,
post_id=post_id, body=new_body)
return response["data"]
# unused
def set_post_raw(self, thread_id, post_id, value): def set_post_raw(self, thread_id, post_id, value):
""" """
This is a subset of `edit_message` that retains the old This is a subset of `edit_message` that retains the old
@ -582,16 +567,17 @@ class BBJ(object):
return response["data"] return response["data"]
# done
def user_is_admin(self, user_name_or_id): def user_is_admin(self, user_name_or_id):
""" """
Return boolean True or False whether the given user identifier Return boolean True or False whether the given user identifier
is an admin on the server. Will raise ValueError if this user is an admin on the server. Will raise ValueError if this user
is not registered. is not registered.
""" """
response = self("is_admin", target_user=user_name_or_id) return False
return response["data"]
# unsupported
def thread_set_pin(self, thread_id, new_status): def thread_set_pin(self, thread_id, new_status):
""" """
Set whether a thread should be pinned or not. new_status Set whether a thread should be pinned or not. new_status
@ -599,10 +585,11 @@ class BBJ(object):
user is an admin, the thread is set to this status on user is an admin, the thread is set to this status on
the server, and the boolean is returned. the server, and the boolean is returned.
""" """
response = self("thread_set_pin", thread_id=thread_id, value=new_status) return None
return response["data"] #raise NotImplementedError
# unused
def message_feed(self, time, format=None): def message_feed(self, time, format=None):
""" """
Returns a special object representing all activity on the board since Returns a special object representing all activity on the board since