Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- from builtins import object
- import os
- import socket
- import weakref
- import time
- from django.utils import timezone
- from django.conf import settings
- from evennia import settings as ev_settings
- from evennia.comms.models import ChannelDB
- from evennia.utils import logger
- from evennia.utils.utils import make_iter, lazy_property
- from evennia.commands.cmdsethandler import CmdSetHandler
- from evennia.server.session import Session
- from evennia.scripts.monitorhandler import MONITOR_HANDLER
- # -------------------------------------------------------------
- # Server Session
- # -------------------------------------------------------------
- class ServerSession(Session):
- """
- This class represents an account's session and is a template for
- individual protocols to communicate with Evennia.
- Each account gets a session assigned to them whenever they connect
- to the game server. All communication between game and account goes
- through their session.
- """
- def __init__(self):
- """Initiate to avoid AttributeErrors down the line"""
- self.puppet = None
- self.account = None
- self.cmdset_storage_string = ""
- self.cmdset = CmdSetHandler(self, True)
- # Attributes governing auditing of commands and where to send logs
- self.audit_method = getattr(ev_settings, 'AUDIT_METHOD', None)
- self.audit_in = getattr(ev_settings, 'AUDIT_IN', False)
- self.audit_out = getattr(ev_settings, 'AUDIT_OUT', False)
- def audit(self, **kwargs):
- """
- Creates a log entry from the given session.
- """
- # Sanitize user input
- session = self
- src = kwargs.get('src', '?')
- if src == 'client':
- try:
- data = kwargs['text'][0][0].strip()
- except IndexError:
- logger.log_err("Failed to log %s" % kwargs['text'])
- return False
- elif src == 'server':
- try:
- data = kwargs['text'].strip()
- except:
- data = str(kwargs)
- # Do not log empty lines
- if not data: return False
- # Get current session's IP address
- ip_token = session.address
- # Capture Account name and dbref together
- account = None
- account = session.get_account()
- account_token = 'null'
- if account:
- account_token = '%s%s' % (account.key, account.dbref)
- # Capture Character name and dbref together
- char = None
- char = session.get_puppet()
- char_token = 'null'
- if char:
- char_token = '%s%s' % (char.key, char.dbref)
- # Capture Room name and dbref together
- room = None
- room_token = 'null'
- if char:
- room = char.location
- room_token = '%s%s' % (room.key, room.dbref)
- # Compile the IP, Account, Character, Room, and the message.
- mapping = {
- 'time': str(timezone.now()),
- 'hostname': socket.getfqdn(),
- 'application': '%s_server' % ev_settings.SERVERNAME,
- 'pid': os.getpid(),
- 'messageid': '-',
- 'structured-data': {
- 'src': src,
- 'ip': ip_token,
- 'account': account_token,
- 'character': char_token,
- 'room': room_token
- },
- 'msg': '(%s)' % data,
- 'objects': {
- 'session': self,
- 'account': account,
- 'character': char,
- 'room': room,
- }
- }
- return mapping
- def data_out(self, **kwargs):
- """
- Sending data from Evennia->Client
- Kwargs:
- text (str or tuple)
- any (str or tuple): Send-commands identified
- by their keys. Or "options", carrying options
- for the protocol(s).
- """
- if self.audit_method and self.audit_out:
- self.audit_method(self.audit(src='server', **kwargs))
- self.sessionhandler.data_out(self, **kwargs)
- def data_in(self, **kwargs):
- """
- Receiving data from the client, sending it off to
- the respective inputfuncs.
- Kwargs:
- kwargs (any): Incoming data from protocol on
- the form `{"commandname": ((args), {kwargs}),...}`
- Notes:
- This method is here in order to give the user
- a single place to catch and possibly process all incoming data from
- the client. It should usually always end by sending
- this data off to `self.sessionhandler.call_inputfuncs(self, **kwargs)`.
- """
- if self.audit_method and self.audit_in:
- self.audit_method(self.audit(src='client', **kwargs))
- self.sessionhandler.call_inputfuncs(self, **kwargs)
Add Comment
Please, Sign In to add comment