Added some Signals.

This commit is contained in:
Andrew Bastien 2019-04-12 20:16:30 -04:00
parent cd5e38fe14
commit 11dc2ee561
4 changed files with 31 additions and 2 deletions

View file

@ -29,6 +29,8 @@ from evennia.utils import class_from_module, create, logger
from evennia.utils.utils import (lazy_property, to_str,
make_iter, is_iter,
variable_from_module)
from evennia.utils.signals import (ACCOUNT_CREATE, OBJECT_PUPPET,
OBJECT_UNPUPPET, ACCOUNT_LOGOUT)
from evennia.typeclasses.attributes import NickHandler
from evennia.scripts.scripthandler import ScriptHandler
from evennia.commands.cmdsethandler import CmdSetHandler
@ -51,6 +53,7 @@ _CONNECT_CHANNEL = None
CREATION_THROTTLE = Throttle(limit=2, timeout=10 * 60)
LOGIN_THROTTLE = Throttle(limit=5, timeout=5 * 60)
class AccountSessionHandler(object):
"""
Manages the session(s) attached to an account.
@ -227,6 +230,8 @@ class DefaultAccount(with_metaclass(TypeclassBase, AccountDB)):
if not _SESSIONS:
from evennia.server.sessionhandler import SESSIONS as _SESSIONS
_SESSIONS.disconnect(session, reason)
if not self.sessions.all():
ACCOUNT_LOGOUT.send(sender=self)
# puppeting operations
@ -301,6 +306,7 @@ class DefaultAccount(with_metaclass(TypeclassBase, AccountDB)):
# re-cache locks to make sure superuser bypass is updated
obj.locks.cache_lock_bypass(obj)
# final hook
OBJECT_PUPPET.send(sender=obj, account=self, session=session)
obj.at_post_puppet()
def unpuppet_object(self, session):
@ -323,6 +329,7 @@ class DefaultAccount(with_metaclass(TypeclassBase, AccountDB)):
obj.sessions.remove(session)
if not obj.sessions.count():
del obj.account
OBJECT_UNPUPPET.send(sender=obj, session=session, account=self)
obj.at_post_unpuppet(self, session=session)
# Just to be sure we're always clear.
session.puppet = None
@ -736,7 +743,9 @@ class DefaultAccount(with_metaclass(TypeclassBase, AccountDB)):
logger.log_trace()
# Update the throttle to indicate a new account was created from this IP
if ip and not guest: CREATION_THROTTLE.update(ip, 'Too many accounts being created.')
if ip and not guest:
CREATION_THROTTLE.update(ip, 'Too many accounts being created.')
ACCOUNT_CREATE.send(sender=account)
return account, errors
def delete(self, *args, **kwargs):

View file

@ -25,6 +25,7 @@ from django.utils.encoding import smart_str
from evennia.accounts.manager import AccountDBManager
from evennia.typeclasses.models import TypedObject
from evennia.utils.utils import make_iter
from evennia.utils.signals import ACCOUNT_RENAME
__all__ = ("AccountDB",)
@ -146,8 +147,10 @@ class AccountDB(TypedObject, AbstractUser):
return self.username
def __username_set(self, value):
old_name = self.username
self.username = value
self.save(update_fields=["username"])
ACCOUNT_RENAME.send(self, old_name=old_name, new_name=value)
def __username_del(self):
del self.username

View file

@ -21,6 +21,7 @@ from evennia.commands.cmdhandler import CMD_LOGINSTART
from evennia.utils.logger import log_trace
from evennia.utils.utils import (variable_from_module, is_iter,
to_str, make_iter, delay, callables_from_module)
from evennia.utils.signals import ACCOUNT_LOGIN
from evennia.utils.inlinefuncs import parse_inlinefunc
from codecs import decode as codecs_decode
@ -483,7 +484,6 @@ class ServerSessionHandler(SessionHandler):
faking login without any AMP being actually active.
"""
if session.logged_in and not force:
# don't log in a session that is already logged in.
return
@ -518,6 +518,8 @@ class ServerSessionHandler(SessionHandler):
operation=SLOGIN,
sessiondata={"logged_in": True,
"uid": session.uid})
if nsess < 2:
ACCOUNT_LOGIN.send(sender=account, session=session)
account.at_post_login(session=session)
def disconnect(self, session, reason="", sync_portal=True):

15
evennia/utils/signals.py Normal file
View file

@ -0,0 +1,15 @@
from django.dispatch import Signal
ACCOUNT_CREATE = Signal(providing_args=['session', ])
ACCOUNT_RENAME = Signal(providing_args=['old_name', 'new_name'])
ACCOUNT_LOGIN = Signal(providing_args=['session', ])
ACCOUNT_LOGOUT = Signal()
OBJECT_PUPPET = Signal(providing_args=['session', 'account'])
OBJECT_UNPUPPET = Signal(providing_args=['session', 'account'])