Guest User

Untitled

a guest
Dec 15th, 2017
91
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.88 KB | None | 0 0
  1. from builtins import object
  2.  
  3. import os
  4. import socket
  5. import weakref
  6. import time
  7. from django.utils import timezone
  8. from django.conf import settings
  9. from evennia import settings as ev_settings
  10. from evennia.comms.models import ChannelDB
  11. from evennia.utils import logger
  12. from evennia.utils.utils import make_iter, lazy_property
  13. from evennia.commands.cmdsethandler import CmdSetHandler
  14. from evennia.server.session import Session
  15. from evennia.scripts.monitorhandler import MONITOR_HANDLER
  16.  
  17. # -------------------------------------------------------------
  18. # Server Session
  19. # -------------------------------------------------------------
  20.  
  21. class ServerSession(Session):
  22. """
  23. This class represents an account's session and is a template for
  24. individual protocols to communicate with Evennia.
  25.  
  26. Each account gets a session assigned to them whenever they connect
  27. to the game server. All communication between game and account goes
  28. through their session.
  29.  
  30. """
  31. def __init__(self):
  32. """Initiate to avoid AttributeErrors down the line"""
  33. self.puppet = None
  34. self.account = None
  35. self.cmdset_storage_string = ""
  36. self.cmdset = CmdSetHandler(self, True)
  37.  
  38. # Attributes governing auditing of commands and where to send logs
  39. self.audit_method = getattr(ev_settings, 'AUDIT_METHOD', None)
  40. self.audit_in = getattr(ev_settings, 'AUDIT_IN', False)
  41. self.audit_out = getattr(ev_settings, 'AUDIT_OUT', False)
  42.  
  43. def audit(self, **kwargs):
  44. """
  45. Creates a log entry from the given session.
  46. """
  47. # Sanitize user input
  48. session = self
  49. src = kwargs.get('src', '?')
  50.  
  51. if src == 'client':
  52. try:
  53. data = kwargs['text'][0][0].strip()
  54. except IndexError:
  55. logger.log_err("Failed to log %s" % kwargs['text'])
  56. return False
  57.  
  58. elif src == 'server':
  59. try:
  60. data = kwargs['text'].strip()
  61. except:
  62. data = str(kwargs)
  63.  
  64. # Do not log empty lines
  65. if not data: return False
  66.  
  67. # Get current session's IP address
  68. ip_token = session.address
  69.  
  70. # Capture Account name and dbref together
  71. account = None
  72. account = session.get_account()
  73. account_token = 'null'
  74. if account:
  75. account_token = '%s%s' % (account.key, account.dbref)
  76.  
  77. # Capture Character name and dbref together
  78. char = None
  79. char = session.get_puppet()
  80. char_token = 'null'
  81. if char:
  82. char_token = '%s%s' % (char.key, char.dbref)
  83.  
  84. # Capture Room name and dbref together
  85. room = None
  86. room_token = 'null'
  87. if char:
  88. room = char.location
  89. room_token = '%s%s' % (room.key, room.dbref)
  90.  
  91. # Compile the IP, Account, Character, Room, and the message.
  92. mapping = {
  93. 'time': str(timezone.now()),
  94. 'hostname': socket.getfqdn(),
  95. 'application': '%s_server' % ev_settings.SERVERNAME,
  96. 'pid': os.getpid(),
  97. 'messageid': '-',
  98. 'structured-data': {
  99. 'src': src,
  100. 'ip': ip_token,
  101. 'account': account_token,
  102. 'character': char_token,
  103. 'room': room_token
  104. },
  105. 'msg': '(%s)' % data,
  106. 'objects': {
  107. 'session': self,
  108. 'account': account,
  109. 'character': char,
  110. 'room': room,
  111. }
  112. }
  113.  
  114. return mapping
  115.  
  116. def data_out(self, **kwargs):
  117. """
  118. Sending data from Evennia->Client
  119.  
  120. Kwargs:
  121. text (str or tuple)
  122. any (str or tuple): Send-commands identified
  123. by their keys. Or "options", carrying options
  124. for the protocol(s).
  125.  
  126. """
  127. if self.audit_method and self.audit_out:
  128. self.audit_method(self.audit(src='server', **kwargs))
  129.  
  130. self.sessionhandler.data_out(self, **kwargs)
  131.  
  132. def data_in(self, **kwargs):
  133. """
  134. Receiving data from the client, sending it off to
  135. the respective inputfuncs.
  136.  
  137. Kwargs:
  138. kwargs (any): Incoming data from protocol on
  139. the form `{"commandname": ((args), {kwargs}),...}`
  140. Notes:
  141. This method is here in order to give the user
  142. a single place to catch and possibly process all incoming data from
  143. the client. It should usually always end by sending
  144. this data off to `self.sessionhandler.call_inputfuncs(self, **kwargs)`.
  145. """
  146. if self.audit_method and self.audit_in:
  147. self.audit_method(self.audit(src='client', **kwargs))
  148.  
  149. self.sessionhandler.call_inputfuncs(self, **kwargs)
Add Comment
Please, Sign In to add comment