remove global network value

master
magical 2022-08-06 06:01:25 +00:00
parent ceb4937c60
commit 2e54939345
1 changed files with 40 additions and 39 deletions

View File

@ -309,7 +309,8 @@ markpath = os.path.join(os.getenv("HOME"), ".bbjmarks")
pinpath = os.path.join(os.getenv("HOME"), ".bbjpins") pinpath = os.path.join(os.getenv("HOME"), ".bbjpins")
class App(object): class App(object):
def __init__(self): def __init__(self, network):
self.network = network
self.prefs = bbjrc("load") self.prefs = bbjrc("load")
self.client_pinned_threads = load_client_pins() self.client_pinned_threads = load_client_pins()
self.usermap = {} self.usermap = {}
@ -368,7 +369,7 @@ class App(object):
bar formatting to it. bar formatting to it.
""" """
header = ("{}@bbj | " + text).format( header = ("{}@bbj | " + text).format(
(network.user_name or "anonymous"), (self.network.user_name or "anonymous"),
*format_specs *format_specs
) )
self.loop.widget.header = urwid.AttrMap(urwid.Text(header), "bar") self.loop.widget.header = urwid.AttrMap(urwid.Text(header), "bar")
@ -574,7 +575,7 @@ class App(object):
# first we need to get the server's version of the message # first we need to get the server's version of the message
# instead of our formatted one # instead of our formatted one
try: try:
message = network.edit_query(thread_id, post_id) message = self.network.edit_query(thread_id, post_id)
except UserWarning as e: except UserWarning as e:
self.remove_overlays() self.remove_overlays()
return self.temp_footer_message(e.description) return self.temp_footer_message(e.description)
@ -597,7 +598,7 @@ class App(object):
urwid.Text(("bold", "Delete this %s?" % ("whole thread" if op else "post"))), urwid.Text(("bold", "Delete this %s?" % ("whole thread" if op else "post"))),
urwid.Divider(), urwid.Divider(),
cute_button(("10" , ">> Yes"), lambda _: [ cute_button(("10" , ">> Yes"), lambda _: [
network.message_delete(message["thread_id"], message["post_id"]), self.network.message_delete(message["thread_id"], message["post_id"]),
self.remove_overlays(), self.remove_overlays(),
self.index() if op else self.refresh() self.index() if op else self.refresh()
]), ]),
@ -619,7 +620,7 @@ class App(object):
def toggle_formatting(self, button, message): def toggle_formatting(self, button, message):
self.remove_overlays() self.remove_overlays()
raw = not message["send_raw"] raw = not message["send_raw"]
network.set_post_raw(message["thread_id"], message["post_id"], raw) self.network.set_post_raw(message["thread_id"], message["post_id"], raw)
return self.refresh() return self.refresh()
@ -636,7 +637,7 @@ class App(object):
"View %sQuote" % ("a " if len(quotes) != 1 else ""), "View %sQuote" % ("a " if len(quotes) != 1 else ""),
self.quote_view_menu, quotes)) self.quote_view_menu, quotes))
if network.can_edit(message["thread_id"], message["post_id"]) \ if self.network.can_edit(message["thread_id"], message["post_id"]) \
and not self.window_split: and not self.window_split:
if message["post_id"] == 0: if message["post_id"] == 0:
@ -649,7 +650,7 @@ class App(object):
"Enable Formatting" if raw else "Disable Formatting", "Enable Formatting" if raw else "Disable Formatting",
self.toggle_formatting, message)) self.toggle_formatting, message))
buttons.insert(0, urwid.Button("Edit Post", self.edit_post, message)) buttons.insert(0, urwid.Button("Edit Post", self.edit_post, message))
if network.user["is_admin"]: if self.network.user["is_admin"]:
buttons.insert(0, urwid.Text(("20", "Reminder: You're an admin!"))) buttons.insert(0, urwid.Text(("20", "Reminder: You're an admin!")))
if not buttons: if not buttons:
@ -802,7 +803,7 @@ class App(object):
# narrowed selection of content, so we dont want to resume last_index_pos # narrowed selection of content, so we dont want to resume last_index_pos
self.last_index_pos = False self.last_index_pos = False
else: else:
threads, usermap = network.thread_index() threads, usermap = self.network.thread_index()
self.usermap.update(usermap) self.usermap.update(usermap)
self.walker.clear() self.walker.clear()
@ -848,7 +849,7 @@ class App(object):
self.body.attr_map = {None: "default"} self.body.attr_map = {None: "default"}
self.mode = "thread" self.mode = "thread"
thread, usermap = network.thread_load(thread_id, format="sequential") thread, usermap = self.network.thread_load(thread_id, format="sequential")
self.usermap.update(usermap) self.usermap.update(usermap)
self.thread = thread self.thread = thread
self.match_data["matches"].clear() self.match_data["matches"].clear()
@ -869,16 +870,16 @@ class App(object):
def toggle_server_pin(self): def toggle_server_pin(self):
if self.mode != "index" or not network.user["is_admin"]: if self.mode != "index" or not self.network.user["is_admin"]:
return return
thread = self.walker.get_focus()[0].thread thread = self.walker.get_focus()[0].thread
network.thread_set_pin(thread["thread_id"], not thread["pinned"]) self.network.thread_set_pin(thread["thread_id"], not thread["pinned"])
self.index() self.index()
def search_index_callback(self, query): def search_index_callback(self, query):
simple_query = query.lower().strip() simple_query = query.lower().strip()
threads, usermap = network.thread_index() threads, usermap = self.network.thread_index()
self.usermap.update(usermap) self.usermap.update(usermap)
results = [ results = [
thread for thread in threads thread for thread in threads
@ -1156,7 +1157,7 @@ class App(object):
call("clear", shell=True) call("clear", shell=True)
print(welcome) print(welcome)
try: try:
log_in() log_in(self.network)
except (KeyboardInterrupt, InterruptedError): except (KeyboardInterrupt, InterruptedError):
pass pass
self.loop.start() self.loop.start()
@ -1169,8 +1170,9 @@ class App(object):
Options menu callback to anonymize the user and Options menu callback to anonymize the user and
then redisplay the options menu. then redisplay the options menu.
""" """
network.user_name = network.user_auth = None self.network.user_name = None
network.user = network("get_me")["data"] self.network.user_auth = None
self.network.user = self.network("get_me")["data"]
self.loop.widget = self.loop.widget[0] self.loop.widget = self.loop.widget[0]
self.set_default_header() self.set_default_header()
self.options_menu() self.options_menu()
@ -1212,7 +1214,7 @@ class App(object):
""" """
# we can "recycle" the server's formatting abilities to # we can "recycle" the server's formatting abilities to
# use the same syntax for the help text itself # use the same syntax for the help text itself
message = network.fake_message( message = self.network.fake_message(
"\n\n".join(format_help), format="sequential") "\n\n".join(format_help), format="sequential")
widget = OptionsMenu( widget = OptionsMenu(
@ -1234,7 +1236,7 @@ class App(object):
def set_color(self, button, value, color): def set_color(self, button, value, color):
if value == False: if value == False:
return return
network.user_update(color=color) self.network.user_update(color=color)
def toggle_exit(self, button, value): def toggle_exit(self, button, value):
@ -1263,8 +1265,8 @@ class App(object):
self.loop.stop() self.loop.stop()
call("clear", shell=True) call("clear", shell=True)
try: try:
name = nameloop("Choose a new username", True) name = nameloop(self.network, "Choose a new username", True)
network.user_update(user_name=name) self.network.user_update(user_name=name)
motherfucking_rainbows("~~hello there %s~~" % name) motherfucking_rainbows("~~hello there %s~~" % name)
sleep(0.8) sleep(0.8)
self.loop.start() self.loop.start()
@ -1280,7 +1282,7 @@ class App(object):
call("clear", shell=True) call("clear", shell=True)
try: try:
password = password_loop("Choose a new password. Can be empty", True) password = password_loop("Choose a new password. Can be empty", True)
network.user_update(auth_hash=network._hash(password)) self.network.user_update(auth_hash=self.network._hash(password))
motherfucking_rainbows("SET NEW PASSWORD") motherfucking_rainbows("SET NEW PASSWORD")
sleep(0.8) sleep(0.8)
self.loop.start() self.loop.start()
@ -1376,8 +1378,8 @@ class App(object):
editor_buttons = [] editor_buttons = []
edit_mode = [] edit_mode = []
if network.user_auth: if self.network.user_auth:
account_message = "Logged in as %s." % network.user_name account_message = "Logged in as %s." % self.network.user_name
account_stuff = [ account_stuff = [
urwid.Button("Relog", on_press=self.relog), urwid.Button("Relog", on_press=self.relog),
urwid.Button("Go anonymous", on_press=self.unlog), urwid.Button("Go anonymous", on_press=self.unlog),
@ -1394,7 +1396,7 @@ class App(object):
for index, color in enumerate(colornames): for index, color in enumerate(colornames):
urwid.RadioButton( urwid.RadioButton(
user_colors, color.title(), user_colors, color.title(),
network.user["color"] == index, self.network.user["color"] == index,
self.set_color, index) self.set_color, index)
for item in user_colors: for item in user_colors:
@ -1616,7 +1618,7 @@ class App(object):
return self.footer_prompt("Title", self.compose) return self.footer_prompt("Title", self.compose)
elif title: elif title:
try: network.validate("title", title) try: self.network.validate("title", title)
except AssertionError as e: except AssertionError as e:
return self.footer_prompt( return self.footer_prompt(
"Title", self.compose, extra_text=e.description) "Title", self.compose, extra_text=e.description)
@ -1642,7 +1644,7 @@ class App(object):
endpoint = "thread_create" endpoint = "thread_create"
params.update({"title": title}) params.update({"title": title})
network.request(endpoint, **params) self.network.request(endpoint, **params)
self.refresh() self.refresh()
if edit: if edit:
self.goto_post(edit["post_id"]) self.goto_post(edit["post_id"])
@ -1751,7 +1753,7 @@ class MessageBody(urwid.Text):
if _c != 0: if _c != 0:
color = str(_c) color = str(_c)
if user != "anonymous" and user["user_name"] == network.user_name: if user != "anonymous" and user["user_name"] == app.network.user_name:
display = "[You]" display = "[You]"
# bold it # bold it
color += "0" color += "0"
@ -1978,7 +1980,7 @@ class ExternalEditor(urwid.Terminal):
def exterminate(self, *_, anon_confirmed=False): def exterminate(self, *_, anon_confirmed=False):
if app.prefs["confirm_anon"] \ if app.prefs["confirm_anon"] \
and not anon_confirmed \ and not anon_confirmed \
and network.user["user_name"] == "anonymous": and app.network.user["user_name"] == "anonymous":
# TODO fixoverlay: urwid terminal widgets have been mucking # TODO fixoverlay: urwid terminal widgets have been mucking
# up overlay dialogs since the wee days of bbj, i really # up overlay dialogs since the wee days of bbj, i really
# need to find a real solution instead of dodging the issue # need to find a real solution instead of dodging the issue
@ -2021,7 +2023,7 @@ class ExternalEditor(urwid.Terminal):
if body and not re.search("^>>[0-9]+$", body): if body and not re.search("^>>[0-9]+$", body):
self.params.update({"body": body}) self.params.update({"body": body})
network.request(self.endpoint, **self.params) app.network.request(self.endpoint, **self.params)
if self.endpoint == "edit_post": if self.endpoint == "edit_post":
app.refresh() app.refresh()
app.goto_post(self.params["post_id"]) app.goto_post(self.params["post_id"])
@ -2366,13 +2368,13 @@ def paren_prompt(text, positive=True, choices=[], function=input, default=None):
return "" return ""
def sane_value(key, prompt, positive=True, return_empty=False): def sane_value(network, key, prompt, positive=True, return_empty=False):
response = paren_prompt(prompt, positive) response = paren_prompt(prompt, positive)
if return_empty and response == "": if return_empty and response == "":
return response return response
try: network.validate(key, response) try: network.validate(key, response)
except AssertionError as e: except AssertionError as e:
return sane_value(key, e.description, False) return sane_value(network, key, e.description, False)
return response return response
@ -2388,14 +2390,14 @@ def password_loop(prompt, positive=True):
return response1 return response1
def nameloop(prompt, positive): def nameloop(network, prompt, positive):
name = sane_value("user_name", prompt, positive) name = sane_value(network, "user_name", prompt, positive)
if network.user_is_registered(name): if network.user_is_registered(name):
return nameloop("%s is already registered" % name, False) return nameloop(network, "%s is already registered" % name, False)
return name return name
def log_in(name="", password=""): def log_in(network, name="", password=""):
""" """
Handles login or registration. If name and/or password are not Handles login or registration. If name and/or password are not
provided, the user is prompted for them using an oldschool provided, the user is prompted for them using an oldschool
@ -2403,7 +2405,7 @@ def log_in(name="", password=""):
curses app. curses app.
""" """
if not name: if not name:
name = sane_value("user_name", "Username", return_empty=True) name = sane_value(network, "user_name", "Username", return_empty=True)
if name == "": if name == "":
motherfucking_rainbows("~~W3 4R3 4n0nYm0u5~~") motherfucking_rainbows("~~W3 4R3 4n0nYm0u5~~")
else: else:
@ -2436,7 +2438,7 @@ def log_in(name="", password=""):
) )
if response == "c": if response == "c":
name = nameloop("Pick a new name", True) name = nameloop(network, "Pick a new name", True)
elif response == "n": elif response == "n":
raise InterruptedError raise InterruptedError
@ -2565,7 +2567,6 @@ def main():
print(help_text) print(help_text)
exit() exit()
global network
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),
@ -2575,14 +2576,14 @@ def main():
exit("\033[0;31m%s\033[0m" % repr(e)) exit("\033[0;31m%s\033[0m" % repr(e))
global app global app
app = App() app = App(network)
call("clear", shell=True) call("clear", shell=True)
motherfucking_rainbows(obnoxious_logo) motherfucking_rainbows(obnoxious_logo)
print(welcome) print(welcome)
try: try:
name = get_arg("user") or os.getenv("BBJ_USER") name = get_arg("user") or os.getenv("BBJ_USER")
password = os.getenv("BBJ_PASSWORD", default="") password = os.getenv("BBJ_PASSWORD", default="")
log_in(name, password) log_in(network, name, password)
app.index() app.index()
app.loop.run() app.loop.run()
except (InterruptedError, KeyboardInterrupt): except (InterruptedError, KeyboardInterrupt):