diff --git a/commands/everyone.py b/commands/everyone.py index 6ccac0f..ec23bb7 100755 --- a/commands/everyone.py +++ b/commands/everyone.py @@ -501,7 +501,11 @@ class CmdRead(Command): buf = myfile.read() session = reader.sessions.get()[0] width = self.client_width() - tidied = md_to_evennia(buf, reader.utf(), width) + + if reader.is_webclient(): + tidied = md_to_html(buf) + else: + tidied = md_to_evennia(buf, reader.is_utf(), width) if prefix: tidied = prefix + "|/" + tidied EvMore(reader, tidied, session=session, @@ -509,9 +513,19 @@ class CmdRead(Command): def md_to_evennia(text, utf, width): brk = '─' if utf else '-' - no_data = sub(r">.*[\n\r]+", "", text, MULTILINE) - return sub(r"#.*", '|W' + (brk * width) + '|n', - no_data, MULTILINE) + line_brk = '|W' + (brk * width) + '|n' + breaks = [line_brk if line.startswith("#") else line + for line in md_preprocessor(text)] + return "\n".join(breaks) + +def md_to_html(text): + breaks = ["
" if line.startswith("#") else line + for line in md_preprocessor(text)] + return "\n".join(breaks) + +def md_preprocessor(text): + lines = text.splitlines() + return [line for line in lines if not line.startswith(">")] class CmdTake(CmdGet, NumberedTargetCommand): """ diff --git a/typeclasses/characters.py b/typeclasses/characters.py index 127c221..6595672 100644 --- a/typeclasses/characters.py +++ b/typeclasses/characters.py @@ -22,6 +22,7 @@ from evennia.utils.search import (search_object, search_account, search_objects_by_typeclass, search_channel) +from utils.user_info import location from utils.word_list import routput, choices, fix_msg from typeclasses.objects import Object, ObjectParent from typeclasses.tutorial import TutorBird, TutorialState @@ -76,7 +77,7 @@ class Character(Object, GenderCharacter, ContribRPCharacter): self.create_ticket() self.create_pouch() - def utf(self): + def is_utf(self): """ Return True if character's user encoding is UTF-8. """ @@ -86,6 +87,16 @@ class Character(Object, GenderCharacter, ContribRPCharacter): return flags.get("ENCODING") == 'utf-8' return False + def is_webclient(self): + """ + Return True if using the 'web' protocol. + """ + session = self.sessions.get()[0] + if session: + prot = session.protocol_key + return prot == 'web' + return False + def delete_inv(self, typeclass): """ Delete items from a character's inventory of typeclass. @@ -152,21 +163,6 @@ class Character(Object, GenderCharacter, ContribRPCharacter): self.delete_inv("typeclasses.drinkables.Cocktail") self.move_to(search_object("mp05").first(), quiet=True, use_destination=True) - def location_info(self): - """ - Return string containing location information from IP address. - """ - ip_address = self.sessions.get()[0].address - if ip_address == "127.0.0.1": - return "(from localhost)" - - response = requests.get(f'https://ipapi.co/{ip_address}/json/', - timeout=1).json() - city = response.get("city") or "Unknown City" - region = response.get("region") or "Unknown Region" - country = response.get("country_name") or "Unknown Country" - return f"(from {city}, {region}, {country})" - def at_post_puppet(self, **kwargs): """ Setup the character based for _this world_. @@ -185,7 +181,11 @@ class Character(Object, GenderCharacter, ContribRPCharacter): name = self.name desc = self.db._sdesc - where = self.location_info() + (city, region, country, _) = location(self) + if country: + where = f"(from {city}, {region}, {country})" + else: + where = "(from localhost)" msg = f"{name}, the {desc}, played by `{player}`, connected {where}." logger.info(msg) diff --git a/typeclasses/readables.py b/typeclasses/readables.py index 836cd0e..9b7311c 100755 --- a/typeclasses/readables.py +++ b/typeclasses/readables.py @@ -9,6 +9,7 @@ from evennia.prototypes.spawner import spawn from typeclasses.objects import Object from typeclasses.consumables import Producer +from utils.user_info import location from utils.word_list import routput from commands.misc import CmdSetWrite @@ -117,7 +118,13 @@ class WriteableBook(Book): myfile.write(message) myfile.write("\n\n") myfile.write(f"> User: {writer.account.name}\n") - myfile.write(f"> Addr: {session.protocol_key}:{session.address}\n") + myfile.write(f"> Desc: {writer.db._sdesc}\n") + + (city, region, country, _) = location(self) + if country: + myfile.write(f"> From: {city}, {region}, {country}") + else: + myfile.write("> From: localhost") myfile.write(f"> Cmds: {session.cmd_total}\n\n") def do_write_end(self, writer):