From 458e3e75992fd4239d4eab9ca890063f8e0f1d4a Mon Sep 17 00:00:00 2001 From: Griatch Date: Sun, 17 Feb 2013 20:21:23 +0100 Subject: [PATCH] Added @sessions command for viewing active sessions connected to a given account. --- src/commands/default/cmdset_default.py | 1 + src/commands/default/general.py | 40 ++++++++++++++++++++++++++ src/objects/models.py | 11 +++++-- src/objects/objects.py | 6 ++-- 4 files changed, 53 insertions(+), 5 deletions(-) diff --git a/src/commands/default/cmdset_default.py b/src/commands/default/cmdset_default.py index 51eb06d752..ff819c9577 100644 --- a/src/commands/default/cmdset_default.py +++ b/src/commands/default/cmdset_default.py @@ -31,6 +31,7 @@ class DefaultCmdSet(CmdSet): self.add(general.CmdSay()) self.add(general.CmdAccess()) self.add(general.CmdColorTest()) + self.add(general.CmdSessions()) # The help system self.add(help.CmdHelp()) diff --git a/src/commands/default/general.py b/src/commands/default/general.py index ab453a4829..988caff413 100644 --- a/src/commands/default/general.py +++ b/src/commands/default/general.py @@ -530,6 +530,46 @@ class CmdSay(MuxCommand): speech) caller.location.msg_contents(emit_string, exclude=caller) +class CmdSessions(MuxCommand): + """ + check connected session(s) + + Usage: + @sessions + + Lists the sessions currently connected to your account. + + """ + key = "@sessions" + locks = "cmd:all()" + help_category = "General" + + def func(self): + "Implement function" + + # make sure we work on the player, not on the character + player = self.caller + if hasattr(player, "player"): + player = player.player + + sessions = player.get_all_sessions() + + table = [["sessid"], ["host"], ["character"], ["location"]] + for sess in sorted(sessions, key=lambda x:x.sessid): + sessid = sess.sessid + char = player.get_character(sessid) + table[0].append(str(sess.sessid)) + table[1].append(str(sess.address[0])) + table[2].append(char and str(char) or "None") + table[3].append(char and str(char.location) or "N/A") + ftable = utils.format_table(table, 5) + string = "" + for ir, row in enumerate(ftable): + if ir == 0: + string += "\n" + "{w%s{n" % ("".join(row)) + else: + string += "\n" + "".join(row) + self.msg(string) class CmdPose(MuxCommand): diff --git a/src/objects/models.py b/src/objects/models.py index e169f55ae3..36955602a0 100644 --- a/src/objects/models.py +++ b/src/objects/models.py @@ -678,13 +678,18 @@ class ObjectDB(TypedObject): data (object): an optional data object that may or may not be used by the protocol. sessid (int): sessid to relay to, if any. - If set to 0 (default), use self.sessid automatically + If set to 0 (default), use either from_obj.sessid (if set) or self.sessid automatically If None, echo to all connected sessions """ if _GA(self, 'player'): # note that we must call the player *typeclass'* msg(), otherwise one couldn't overload it. - _GA(_GA(self, 'player'), "typeclass").msg(msg, from_obj=from_obj, data=data, - sessid=(sessid==0 and _GA(self, "sessid") or sessid or None)) + if sessid == 0: + sessid = None + if from_obj and hasattr(from_obj, "sessid"): + sessid = from_obj.sessid + elif hasattr(self, "sessid"): + sessid = self.sessid + _GA(_GA(self, 'player'), "typeclass").msg(msg, from_obj=from_obj, data=data, sessid=sessid) def emit_to(self, message, from_obj=None, data=None): "Deprecated. Alias for msg" diff --git a/src/objects/objects.py b/src/objects/objects.py index ababa69496..48034ce787 100644 --- a/src/objects/objects.py +++ b/src/objects/objects.py @@ -216,7 +216,7 @@ class Object(TypeClass): """ return self.dbobj.execute_cmd(raw_string, sessid=sessid) - def msg(self, message, from_obj=None, data=None): + def msg(self, message, from_obj=None, data=None, sessid=0): """ Emits something to any sessions attached to the object. @@ -224,8 +224,10 @@ class Object(TypeClass): from_obj (obj): object that is sending. data (object): an optional data object that may or may not be used by the protocol. + sessid: optional session target. If sessid=0, the session will + default to self.sessid or from_obj.sessid. """ - self.dbobj.msg(message, from_obj=from_obj, data=data) + self.dbobj.msg(message, from_obj=from_obj, data=data, sessid=0) def msg_contents(self, message, exclude=None, from_obj=None, data=None): """