mirror of
https://github.com/evennia/evennia.git
synced 2026-03-28 18:47:16 +01:00
Evennia now runs on its own Twisted webserver (no need for testserver or Apache if you don't want to). Evennia now also has an ajax long-polling web client running from Twisted. The web client requires no extra dependencies beyond jQuery which is included. The src/server structure has been r
cleaned up and rewritten to make it easier to add new protocols in the future - all new protocols need to inherit from server.session.Session, whi ch implements a set of hooks that Evennia uses to communicate. The current web client protocol is functional but does not implement any of rcaskey 's suggestions as of yet - it uses a separate data object passed through msg() to communicate between the server and the various protocols. Also the client itself could probably need cleanup and 'prettification'. The fact that the system runs a hybrid of Django and Twisted, getting the best of both worlds should allow for many possibilities in the future. /Griatch
This commit is contained in:
parent
ecefbfac01
commit
251f94aa7a
118 changed files with 9049 additions and 593 deletions
|
|
@ -9,15 +9,9 @@ Everything starts at handle_setup()
|
|||
from django.contrib.auth.models import User
|
||||
from django.core import management
|
||||
from django.conf import settings
|
||||
|
||||
from src.config.models import ConfigValue, ConnectScreen
|
||||
from src.objects.models import ObjectDB
|
||||
from src.comms.models import Channel, ChannelConnection
|
||||
from src.players.models import PlayerDB
|
||||
from src.help.models import HelpEntry
|
||||
from src.scripts import scripts
|
||||
from src.utils import create
|
||||
from src.utils import gametime
|
||||
|
||||
def create_config_values():
|
||||
"""
|
||||
|
|
@ -34,14 +28,14 @@ def create_connect_screens():
|
|||
|
||||
print " Creating startup screen(s) ..."
|
||||
|
||||
text = "%ch%cb==================================================================%cn"
|
||||
text += "\r\n Welcome to %chEvennia%cn! Please type one of the following to begin:\r\n"
|
||||
text = "{b=================================================================={n"
|
||||
text += "\r\n Welcome to {wEvennia{n! Please type one of the following to begin:\r\n"
|
||||
text += "\r\n If you have an existing account, connect to it by typing:\r\n "
|
||||
text += "%chconnect <email> <password>%cn\r\n If you need to create an account, "
|
||||
text += "{wconnect <email> <password>{n\r\n If you need to create an account, "
|
||||
text += "type (without the <>'s):\r\n "
|
||||
text += "%chcreate \"<username>\" <email> <password>%cn\r\n"
|
||||
text += "\r\n Enter %chhelp%cn for more info. %chlook%cn will re-show this screen.\r\n"
|
||||
text += "%ch%cb==================================================================%cn\r\n"
|
||||
text += "{wcreate \"<username>\" <email> <password>{n\r\n"
|
||||
text += "\r\n Enter {whelp{n for more info. {wlook{n will re-show this screen.\r\n"
|
||||
text += "{b=================================================================={n\r\n"
|
||||
ConnectScreen(db_key="Default", db_text=text, db_is_active=True).save()
|
||||
|
||||
def get_god_user():
|
||||
|
|
@ -116,6 +110,7 @@ def create_channels():
|
|||
|
||||
# connect the god user to all these channels by default.
|
||||
goduser = get_god_user()
|
||||
from src.comms.models import ChannelConnection
|
||||
ChannelConnection.objects.create_connection(goduser, pchan)
|
||||
ChannelConnection.objects.create_connection(goduser, ichan)
|
||||
ChannelConnection.objects.create_connection(goduser, cchan)
|
||||
|
|
@ -164,9 +159,10 @@ def create_system_scripts():
|
|||
Setup the system repeat scripts. They are automatically started
|
||||
by the create_script function.
|
||||
"""
|
||||
from src.scripts import scripts
|
||||
|
||||
print " Creating and starting global scripts ..."
|
||||
|
||||
|
||||
# check so that all sessions are alive.
|
||||
script1 = create.create_script(scripts.CheckSessions)
|
||||
# validate all scripts in script table.
|
||||
|
|
@ -184,7 +180,7 @@ def start_game_time():
|
|||
(the uptime can also be found directly from the server though).
|
||||
"""
|
||||
print " Starting in-game time ..."
|
||||
|
||||
from src.utils import gametime
|
||||
gametime.init_gametime()
|
||||
|
||||
def handle_setup(last_step):
|
||||
|
|
@ -230,11 +226,16 @@ def handle_setup(last_step):
|
|||
setup_func()
|
||||
except Exception:
|
||||
if last_step + num == 2:
|
||||
from src.players.models import PlayerDB
|
||||
from src.objects.models import ObjectDB
|
||||
|
||||
for obj in ObjectDB.objects.all():
|
||||
obj.delete()
|
||||
for profile in PlayerDB.objects.all():
|
||||
profile.delete()
|
||||
elif last_step + num == 3:
|
||||
from src.comms.models import Channel, ChannelConnection
|
||||
|
||||
for chan in Channel.objects.all():
|
||||
chan.delete()
|
||||
for conn in ChannelConnection.objects.all():
|
||||
|
|
|
|||
|
|
@ -1,6 +1,11 @@
|
|||
"""
|
||||
This module implements the main Evennia
|
||||
server process, the core of the game engine.
|
||||
This module implements the main Evennia server process, the core of
|
||||
the game engine. Only import this once!
|
||||
|
||||
This module should be started with the 'twistd' executable since it
|
||||
sets up all the networking features. (this is done by
|
||||
game/evennia.py).
|
||||
|
||||
"""
|
||||
import time
|
||||
import sys
|
||||
|
|
@ -12,75 +17,82 @@ if os.name == 'nt':
|
|||
|
||||
from twisted.application import internet, service
|
||||
from twisted.internet import protocol, reactor
|
||||
from twisted.web import server, static
|
||||
from django.db import connection
|
||||
from django.conf import settings
|
||||
|
||||
from src.utils import reloads
|
||||
from src.config.models import ConfigValue
|
||||
from src.server.session import SessionProtocol
|
||||
from src.server import sessionhandler
|
||||
from src.server import initial_setup
|
||||
from src.utils import reloads
|
||||
|
||||
from src.utils.utils import get_evennia_version
|
||||
from src.comms import channelhandler
|
||||
|
||||
class EvenniaService(service.Service):
|
||||
|
||||
#------------------------------------------------------------
|
||||
# Evennia Server settings
|
||||
#------------------------------------------------------------
|
||||
|
||||
SERVERNAME = settings.SERVERNAME
|
||||
VERSION = get_evennia_version()
|
||||
|
||||
TELNET_PORTS = settings.TELNET_PORTS
|
||||
WEBSERVER_PORTS = settings.WEBSERVER_PORTS
|
||||
|
||||
TELNET_ENABLED = settings.TELNET_ENABLED and TELNET_PORTS
|
||||
WEBSERVER_ENABLED = settings.WEBSERVER_ENABLED and WEBSERVER_PORTS
|
||||
WEBCLIENT_ENABLED = settings.WEBCLIENT_ENABLED
|
||||
IMC2_ENABLED = settings.IMC2_ENABLED
|
||||
IRC_ENABLED = settings.IRC_ENABLED
|
||||
|
||||
#------------------------------------------------------------
|
||||
# Evennia Main Server object
|
||||
#------------------------------------------------------------
|
||||
class Evennia(object):
|
||||
|
||||
"""
|
||||
The main server service task.
|
||||
The main Evennia server handler. This object sets up the database and
|
||||
tracks and interlinks all the twisted network services that make up
|
||||
evennia.
|
||||
"""
|
||||
def __init__(self):
|
||||
# Holds the TCP services.
|
||||
self.service_collection = None
|
||||
self.game_running = True
|
||||
|
||||
def __init__(self, application):
|
||||
"""
|
||||
Setup the server.
|
||||
|
||||
application - an instantiated Twisted application
|
||||
|
||||
"""
|
||||
sys.path.append('.')
|
||||
|
||||
# create a store of services
|
||||
self.services = service.IServiceCollection(application)
|
||||
|
||||
print '\n' + '-'*50
|
||||
|
||||
# Database-specific startup optimizations.
|
||||
if (settings.DATABASE_ENGINE == "sqlite3"
|
||||
or hasattr(settings, 'DATABASE')
|
||||
and settings.DATABASE.get('ENGINE', None) == 'django.db.backends.sqlite3'):
|
||||
# run sqlite3 preps
|
||||
self.sqlite3_prep()
|
||||
|
||||
# Begin startup debug output.
|
||||
print '\n' + '-'*50
|
||||
|
||||
last_initial_setup_step = \
|
||||
ConfigValue.objects.conf('last_initial_setup_step')
|
||||
|
||||
if not last_initial_setup_step:
|
||||
# None is only returned if the config does not exist,
|
||||
# i.e. this is an empty DB that needs populating.
|
||||
print ' Server started for the first time. Setting defaults.'
|
||||
initial_setup.handle_setup(0)
|
||||
print '-'*50
|
||||
|
||||
elif int(last_initial_setup_step) >= 0:
|
||||
# last_setup_step >= 0 means the setup crashed
|
||||
# on one of its modules and setup will resume, retrying
|
||||
# the last failed module. When all are finished, the step
|
||||
# is set to -1 to show it does not need to be run again.
|
||||
print ' Resuming initial setup from step %s.' % \
|
||||
last_initial_setup_step
|
||||
initial_setup.handle_setup(int(last_initial_setup_step))
|
||||
print '-'*50
|
||||
self.sqlite3_prep()
|
||||
|
||||
# Run the initial setup if needed
|
||||
self.run_initial_setup()
|
||||
|
||||
# we have to null this here.
|
||||
sessionhandler.change_session_count(0)
|
||||
sessionhandler.SESSIONS.session_count(0)
|
||||
|
||||
self.start_time = time.time()
|
||||
|
||||
# initialize channelhandler
|
||||
channelhandler.CHANNELHANDLER.update()
|
||||
|
||||
# init all global scripts
|
||||
reloads.reload_scripts(init_mode=True)
|
||||
|
||||
# Make output to the terminal.
|
||||
print ' %s (%s) started on port(s):' % \
|
||||
(settings.SERVERNAME, get_evennia_version())
|
||||
for port in settings.GAMEPORTS:
|
||||
print ' * %s' % (port)
|
||||
print '-'*50
|
||||
# Make info output to the terminal.
|
||||
self.terminal_output()
|
||||
|
||||
|
||||
print '-'*50
|
||||
|
||||
self.game_running = True
|
||||
|
||||
# Server startup methods
|
||||
|
||||
|
|
@ -89,14 +101,50 @@ class EvenniaService(service.Service):
|
|||
Optimize some SQLite stuff at startup since we
|
||||
can't save it to the database.
|
||||
"""
|
||||
cursor = connection.cursor()
|
||||
cursor.execute("PRAGMA cache_size=10000")
|
||||
cursor.execute("PRAGMA synchronous=OFF")
|
||||
cursor.execute("PRAGMA count_changes=OFF")
|
||||
cursor.execute("PRAGMA temp_store=2")
|
||||
|
||||
if (settings.DATABASE_ENGINE == "sqlite3"
|
||||
or hasattr(settings, 'DATABASE')
|
||||
and settings.DATABASE.get('ENGINE', None)
|
||||
== 'django.db.backends.sqlite3'):
|
||||
cursor = connection.cursor()
|
||||
cursor.execute("PRAGMA cache_size=10000")
|
||||
cursor.execute("PRAGMA synchronous=OFF")
|
||||
cursor.execute("PRAGMA count_changes=OFF")
|
||||
cursor.execute("PRAGMA temp_store=2")
|
||||
|
||||
# General methods
|
||||
def run_initial_setup(self):
|
||||
"""
|
||||
This attempts to run the initial_setup script of the server.
|
||||
It returns if this is not the first time the server starts.
|
||||
"""
|
||||
last_initial_setup_step = ConfigValue.objects.conf('last_initial_setup_step')
|
||||
if not last_initial_setup_step:
|
||||
# None is only returned if the config does not exist,
|
||||
# i.e. this is an empty DB that needs populating.
|
||||
print ' Server started for the first time. Setting defaults.'
|
||||
initial_setup.handle_setup(0)
|
||||
print '-'*50
|
||||
elif int(last_initial_setup_step) >= 0:
|
||||
# a positive value means the setup crashed on one of its
|
||||
# modules and setup will resume from this step, retrying
|
||||
# the last failed module. When all are finished, the step
|
||||
# is set to -1 to show it does not need to be run again.
|
||||
print ' Resuming initial setup from step %s.' % \
|
||||
last_initial_setup_step
|
||||
initial_setup.handle_setup(int(last_initial_setup_step))
|
||||
print '-'*50
|
||||
|
||||
def terminal_output(self):
|
||||
"""
|
||||
Outputs server startup info to the terminal.
|
||||
"""
|
||||
print ' %s (%s) started on port(s):' % (SERVERNAME, VERSION)
|
||||
if TELNET_ENABLED:
|
||||
print " telnet: " + ", ".join([str(port) for port in TELNET_PORTS])
|
||||
if WEBSERVER_ENABLED:
|
||||
clientstring = ""
|
||||
if WEBCLIENT_ENABLED:
|
||||
clientstring = '/client'
|
||||
print " webserver%s: " % clientstring + ", ".join([str(port) for port in WEBSERVER_PORTS])
|
||||
|
||||
def shutdown(self, message=None):
|
||||
"""
|
||||
|
|
@ -104,52 +152,88 @@ class EvenniaService(service.Service):
|
|||
"""
|
||||
if not message:
|
||||
message = 'The server has been shutdown. Please check back soon.'
|
||||
sessionhandler.announce_all(message)
|
||||
sessionhandler.disconnect_all_sessions()
|
||||
sessionhandler.SESSIONS.disconnect_all_sessions(reason=message)
|
||||
reactor.callLater(0, reactor.stop)
|
||||
|
||||
def getEvenniaServiceFactory(self):
|
||||
"Retrieve instances of the server"
|
||||
|
||||
#------------------------------------------------------------
|
||||
#
|
||||
# Start the Evennia game server and add all active services
|
||||
#
|
||||
#------------------------------------------------------------
|
||||
|
||||
# twistd requires us to define the variable 'application' so it knows
|
||||
# what to execute from.
|
||||
application = service.Application('Evennia')
|
||||
|
||||
# The main evennia server program. This sets up the database
|
||||
# and is where we store all the other services.
|
||||
EVENNIA = Evennia(application)
|
||||
|
||||
# We group all the various services under the same twisted app.
|
||||
# These will gradually be started as they are initialized below.
|
||||
|
||||
if TELNET_ENABLED:
|
||||
|
||||
# start telnet game connections
|
||||
|
||||
from src.server import telnet
|
||||
|
||||
for port in TELNET_PORTS:
|
||||
factory = protocol.ServerFactory()
|
||||
factory.protocol = SessionProtocol
|
||||
factory.server = self
|
||||
return factory
|
||||
factory.protocol = telnet.TelnetProtocol
|
||||
telnet_service = internet.TCPServer(port, factory)
|
||||
telnet_service.setName('Evennia%s' % port)
|
||||
EVENNIA.services.addService(telnet_service)
|
||||
|
||||
def start_services(self, application):
|
||||
"""
|
||||
Starts all of the TCP services.
|
||||
"""
|
||||
self.service_collection = service.IServiceCollection(application)
|
||||
for port in settings.GAMEPORTS:
|
||||
evennia_server = \
|
||||
internet.TCPServer(port, self.getEvenniaServiceFactory())
|
||||
evennia_server.setName('Evennia%s' %port)
|
||||
evennia_server.setServiceParent(self.service_collection)
|
||||
|
||||
if settings.IMC2_ENABLED:
|
||||
from src.imc2.connection import IMC2ClientFactory
|
||||
from src.imc2 import events as imc2_events
|
||||
imc2_factory = IMC2ClientFactory()
|
||||
svc = internet.TCPClient(settings.IMC2_SERVER_ADDRESS,
|
||||
settings.IMC2_SERVER_PORT,
|
||||
imc2_factory)
|
||||
svc.setName('IMC2')
|
||||
svc.setServiceParent(self.service_collection)
|
||||
imc2_events.add_events()
|
||||
if WEBSERVER_ENABLED:
|
||||
|
||||
if settings.IRC_ENABLED:
|
||||
from src.irc.connection import IRC_BotFactory
|
||||
irc = internet.TCPClient(settings.IRC_NETWORK,
|
||||
settings.IRC_PORT,
|
||||
IRC_BotFactory(settings.IRC_CHANNEL,
|
||||
settings.IRC_NETWORK,
|
||||
settings.IRC_NICKNAME))
|
||||
irc.setName("%s:%s" % ("IRC", settings.IRC_CHANNEL))
|
||||
irc.setServiceParent(self.service_collection)
|
||||
# a django-compatible webserver.
|
||||
|
||||
from src.server.webserver import DjangoWebRoot
|
||||
|
||||
# define the root url (/) as a wsgi resource recognized by Django
|
||||
web_root = DjangoWebRoot()
|
||||
# point our media resources to url /media
|
||||
media_dir = os.path.join(settings.SRC_DIR, 'web', settings.MEDIA_URL.lstrip('/')) #TODO: Could be made cleaner?
|
||||
web_root.putChild("media", static.File(media_dir))
|
||||
|
||||
if WEBCLIENT_ENABLED:
|
||||
# create ajax client processes at /webclientdata
|
||||
from src.server.webclient import WebClient
|
||||
web_root.putChild("webclientdata", WebClient())
|
||||
|
||||
web_site = server.Site(web_root, logPath=settings.HTTP_LOG_FILE)
|
||||
for port in WEBSERVER_PORTS:
|
||||
# create the webserver
|
||||
webserver = internet.TCPServer(port, web_site)
|
||||
webserver.setName('EvenniaWebServer%s' % port)
|
||||
EVENNIA.services.addService(webserver)
|
||||
|
||||
|
||||
# Twisted requires us to define an 'application' attribute.
|
||||
application = service.Application('Evennia')
|
||||
# The main mud service. Import this for access to the server methods.
|
||||
mud_service = EvenniaService()
|
||||
mud_service.start_services(application)
|
||||
if IMC2_ENABLED:
|
||||
|
||||
# IMC2 channel connections
|
||||
|
||||
from src.imc2.connection import IMC2ClientFactory
|
||||
from src.imc2 import events as imc2_events
|
||||
imc2_factory = IMC2ClientFactory()
|
||||
svc = internet.TCPClient(settings.IMC2_SERVER_ADDRESS,
|
||||
settings.IMC2_SERVER_PORT,
|
||||
imc2_factory)
|
||||
svc.setName('IMC2')
|
||||
EVENNIA.services.addService(svc)
|
||||
imc2_events.add_events()
|
||||
|
||||
if IRC_ENABLED:
|
||||
|
||||
# IRC channel connections
|
||||
|
||||
from src.irc.connection import IRC_BotFactory
|
||||
irc = internet.TCPClient(settings.IRC_NETWORK,
|
||||
settings.IRC_PORT,
|
||||
IRC_BotFactory(settings.IRC_CHANNEL,
|
||||
settings.IRC_NETWORK,
|
||||
settings.IRC_NICKNAME))
|
||||
irc.setName("%s:%s" % ("IRC", settings.IRC_CHANNEL))
|
||||
EVENNIA.services.addService(irc)
|
||||
|
|
|
|||
|
|
@ -1,214 +1,183 @@
|
|||
"""
|
||||
This module contains classes related to Sessions. sessionhandler has the things
|
||||
needed to manage them.
|
||||
This defines a generic session class.
|
||||
|
||||
All protocols should implement this class and its hook methods.
|
||||
"""
|
||||
import time
|
||||
|
||||
import time
|
||||
from datetime import datetime
|
||||
from twisted.conch.telnet import StatefulTelnetProtocol
|
||||
#from django.contrib.auth.models import User
|
||||
from django.conf import settings
|
||||
from src.server import sessionhandler
|
||||
from src.objects.models import ObjectDB
|
||||
#from src.objects.models import ObjectDB
|
||||
from src.comms.models import Channel
|
||||
from src.config.models import ConnectScreen
|
||||
from src.utils import logger, reloads
|
||||
from src.commands import cmdhandler
|
||||
from src.utils import ansi
|
||||
from src.utils import reloads
|
||||
from src.utils import logger
|
||||
from src.utils import utils
|
||||
from src.server import sessionhandler
|
||||
|
||||
ENCODINGS = settings.ENCODINGS
|
||||
IDLE_TIMEOUT = settings.IDLE_TIMEOUT
|
||||
IDLE_COMMAND = settings.IDLE_COMMAND
|
||||
|
||||
class SessionProtocol(StatefulTelnetProtocol):
|
||||
|
||||
|
||||
class IOdata(object):
|
||||
"""
|
||||
This class represents a player's session. Each player
|
||||
gets a session assigned to them whenever
|
||||
they connect to the game server. All communication
|
||||
between game and player goes through here.
|
||||
A simple storage object that allows for storing
|
||||
new attributes on it at creation.
|
||||
"""
|
||||
def __init__(self, **kwargs):
|
||||
"Give keyword arguments to store as new arguments on the object."
|
||||
self.__dict__.update(**kwargs)
|
||||
|
||||
|
||||
#------------------------------------------------------------
|
||||
# SessionBase class
|
||||
#------------------------------------------------------------
|
||||
|
||||
class SessionBase(object):
|
||||
"""
|
||||
This class represents a player's session and is a template for
|
||||
individual protocols to communicate with Evennia.
|
||||
|
||||
Each player gets a session assigned to them whenever they connect
|
||||
to the game server. All communication between game and player goes
|
||||
through their session.
|
||||
|
||||
"""
|
||||
|
||||
def __str__(self):
|
||||
"""
|
||||
String representation of the user session class. We use
|
||||
this a lot in the server logs and stuff.
|
||||
"""
|
||||
if self.logged_in:
|
||||
symbol = '#'
|
||||
else:
|
||||
symbol = '?'
|
||||
return "<%s> %s@%s" % (symbol, self.name, self.address,)
|
||||
# use this to uniquely identify the protocol name, e.g. "telnet" or "comet"
|
||||
protocol_key = "BaseProtocol"
|
||||
|
||||
def connectionMade(self):
|
||||
def session_connect(self, address, suid=None):
|
||||
"""
|
||||
What to do when we get a connection.
|
||||
"""
|
||||
# setup the parameters
|
||||
self.prep_session()
|
||||
# send info
|
||||
logger.log_infomsg('New connection: %s' % self)
|
||||
# add this new session to handler
|
||||
sessionhandler.add_session(self)
|
||||
# show a connect screen
|
||||
self.game_connect_screen()
|
||||
The setup of the session. An address (usually an IP address) on any form is required.
|
||||
|
||||
def getClientAddress(self):
|
||||
"""
|
||||
Returns the client's address and port in a tuple. For example
|
||||
('127.0.0.1', 41917)
|
||||
"""
|
||||
return self.transport.client
|
||||
This should be called by the protocol at connection time.
|
||||
|
||||
def prep_session(self):
|
||||
suid = this is a session id. Needed by some transport protocols.
|
||||
"""
|
||||
This sets up the main parameters of
|
||||
the session. The game will poll these
|
||||
properties to check the status of the
|
||||
connection and to be able to contact
|
||||
the connected player.
|
||||
"""
|
||||
# main server properties
|
||||
self.server = self.factory.server
|
||||
self.address = self.getClientAddress()
|
||||
self.address = address
|
||||
|
||||
# player setup
|
||||
# user setup
|
||||
self.name = None
|
||||
self.uid = None
|
||||
self.suid = suid
|
||||
self.logged_in = False
|
||||
self.encoding = "utf-8"
|
||||
|
||||
current_time = time.time()
|
||||
|
||||
# The time the user last issued a command.
|
||||
self.cmd_last = time.time()
|
||||
self.cmd_last = current_time
|
||||
# Player-visible idle time, excluding the IDLE command.
|
||||
self.cmd_last_visible = time.time()
|
||||
self.cmd_last_visible = current_time
|
||||
# The time when the user connected.
|
||||
self.conn_time = current_time
|
||||
# Total number of commands issued.
|
||||
self.cmd_total = 0
|
||||
# The time when the user connected.
|
||||
self.conn_time = time.time()
|
||||
#self.channels_subscribed = {}
|
||||
sessionhandler.SESSIONS.add_unloggedin_session(self)
|
||||
# call hook method
|
||||
self.at_connect()
|
||||
|
||||
def disconnectClient(self):
|
||||
def session_login(self, player):
|
||||
"""
|
||||
Manually disconnect the client.
|
||||
"""
|
||||
self.transport.loseConnection()
|
||||
Private startup mechanisms that need to run at login
|
||||
|
||||
def connectionLost(self, reason):
|
||||
player - the connected player
|
||||
"""
|
||||
Execute this when a client abruplty loses their connection.
|
||||
"""
|
||||
logger.log_infomsg('Disconnected: %s' % self)
|
||||
self.cemit_info('Disconnected: %s.' % self)
|
||||
self.handle_close()
|
||||
self.player = player
|
||||
self.user = player.user
|
||||
self.uid = self.user.id
|
||||
self.name = self.user.username
|
||||
self.logged_in = True
|
||||
self.conn_time = time.time()
|
||||
|
||||
def lineReceived(self, raw_string):
|
||||
"""
|
||||
Communication Player -> Evennia
|
||||
Any line return indicates a command for the purpose of the MUD.
|
||||
So we take the user input and pass it to the Player and their currently
|
||||
connected character.
|
||||
"""
|
||||
# Update account's last login time.
|
||||
self.user.last_login = datetime.now()
|
||||
self.user.save()
|
||||
self.log('Logged in: %s' % self)
|
||||
|
||||
if self.encoding:
|
||||
try:
|
||||
raw_string = utils.to_unicode(raw_string, encoding=self.encoding)
|
||||
self.execute_cmd(raw_string)
|
||||
return
|
||||
except Exception, e:
|
||||
err = str(e)
|
||||
print err
|
||||
pass
|
||||
# start (persistent) scripts on this object
|
||||
reloads.reload_scripts(obj=self.player.character, init_mode=True)
|
||||
|
||||
#add session to connected list
|
||||
sessionhandler.SESSIONS.add_loggedin_session(self)
|
||||
|
||||
# malformed/wrong encoding defined on player-try some defaults
|
||||
for encoding in ENCODINGS:
|
||||
try:
|
||||
raw_string = utils.to_unicode(raw_string, encoding=encoding)
|
||||
err = None
|
||||
break
|
||||
except Exception, e:
|
||||
err = str(e)
|
||||
continue
|
||||
if err:
|
||||
self.sendLine(err)
|
||||
#call hook
|
||||
self.at_login()
|
||||
|
||||
def session_disconnect(self, reason=None):
|
||||
"""
|
||||
Clean up the session, removing it from the game and doing some
|
||||
accounting. This method is used also for non-loggedin
|
||||
accounts.
|
||||
|
||||
Note that this methods does not close the connection - this is protocol-dependent
|
||||
and have to be done right after this function!
|
||||
"""
|
||||
if self.logged_in:
|
||||
character = self.get_character()
|
||||
if character:
|
||||
character.player.at_disconnect(reason)
|
||||
uaccount = character.player.user
|
||||
uaccount.last_login = datetime.now()
|
||||
uaccount.save()
|
||||
self.logged_in = False
|
||||
sessionhandler.SESSIONS.remove_session(self)
|
||||
|
||||
def session_validate(self):
|
||||
"""
|
||||
Validate the session to make sure they have not been idle for too long
|
||||
"""
|
||||
if IDLE_TIMEOUT > 0 and (time.time() - self.cmd_last) > IDLE_TIMEOUT:
|
||||
self.msg("Idle timeout exceeded, disconnecting.")
|
||||
self.session_disconnect()
|
||||
|
||||
def get_player(self):
|
||||
"""
|
||||
Get the player associated with this session
|
||||
"""
|
||||
if self.logged_in:
|
||||
return self.player
|
||||
else:
|
||||
self.execute_cmd(raw_string)
|
||||
|
||||
def msg(self, message, markup=True):
|
||||
"""
|
||||
Communication Evennia -> Player
|
||||
Sends a message to the session.
|
||||
|
||||
markup - determines if formatting markup should be
|
||||
parsed or not. Currently this means ANSI
|
||||
colors, but could also be html tags for
|
||||
web connections etc.
|
||||
"""
|
||||
if self.encoding:
|
||||
try:
|
||||
message = utils.to_str(message, encoding=self.encoding)
|
||||
self.sendLine(ansi.parse_ansi(message, strip_ansi=not markup))
|
||||
return
|
||||
except Exception:
|
||||
pass
|
||||
|
||||
# malformed/wrong encoding defined on player - try some defaults
|
||||
for encoding in ENCODINGS:
|
||||
try:
|
||||
message = utils.to_str(message, encoding=encoding)
|
||||
err = None
|
||||
break
|
||||
except Exception, e:
|
||||
err = str(e)
|
||||
continue
|
||||
if err:
|
||||
self.sendLine(err)
|
||||
else:
|
||||
self.sendLine(ansi.parse_ansi(message, strip_ansi=not markup))
|
||||
return None
|
||||
|
||||
# if self.logged_in:
|
||||
# character = ObjectDB.objects.get_object_with_user(self.uid)
|
||||
# if not character:
|
||||
# string = "No player match for session uid: %s" % self.uid
|
||||
# logger.log_errmsg(string)
|
||||
# return None
|
||||
# return character.player
|
||||
# return None
|
||||
|
||||
def get_character(self):
|
||||
"""
|
||||
Returns the in-game character associated with a session.
|
||||
This returns the typeclass of the object.
|
||||
"""
|
||||
if self.logged_in:
|
||||
character = ObjectDB.objects.get_object_with_user(self.uid)
|
||||
if not character:
|
||||
string = "No character match for session uid: %s" % self.uid
|
||||
logger.log_errmsg(string)
|
||||
else:
|
||||
return character
|
||||
player = self.get_player()
|
||||
if player:
|
||||
return player.character
|
||||
return None
|
||||
|
||||
def execute_cmd(self, raw_string):
|
||||
def log(self, message, channel=True):
|
||||
"""
|
||||
Sends a command to this session's
|
||||
character for processing.
|
||||
Emits session info to the appropriate outputs and info channels.
|
||||
"""
|
||||
if channel:
|
||||
try:
|
||||
cchan = settings.CHANNEL_CONNECTINFO
|
||||
cchan = Channel.objects.get_channel(cchan[0])
|
||||
cchan.msg("[%s]: %s" % (cchan.key, message))
|
||||
except Exception:
|
||||
pass
|
||||
logger.log_infomsg(message)
|
||||
|
||||
'idle' is a special command that is
|
||||
interrupted already here. It doesn't do
|
||||
anything except silently updates the
|
||||
last-active timer to avoid getting kicked
|
||||
off for idleness.
|
||||
"""
|
||||
# handle the 'idle' command
|
||||
if str(raw_string).strip() == 'idle':
|
||||
self.update_counters(idle=True)
|
||||
return
|
||||
|
||||
# all other inputs, including empty inputs
|
||||
character = self.get_character()
|
||||
if character:
|
||||
# normal operation.
|
||||
character.execute_cmd(raw_string)
|
||||
else:
|
||||
# we are not logged in yet
|
||||
cmdhandler.cmdhandler(self, raw_string, unloggedin=True)
|
||||
# update our command counters and idle times.
|
||||
self.update_counters()
|
||||
|
||||
def update_counters(self, idle=False):
|
||||
def update_session_counters(self, idle=False):
|
||||
"""
|
||||
Hit this when the user enters a command in order to update idle timers
|
||||
and command counters. If silently is True, the public-facing idle time
|
||||
is not updated.
|
||||
and command counters.
|
||||
"""
|
||||
# Store the timestamp of the user's last command.
|
||||
self.cmd_last = time.time()
|
||||
|
|
@ -217,80 +186,125 @@ class SessionProtocol(StatefulTelnetProtocol):
|
|||
self.cmd_total += 1
|
||||
# Player-visible idle time, not used in idle timeout calcs.
|
||||
self.cmd_last_visible = time.time()
|
||||
|
||||
def handle_close(self):
|
||||
|
||||
def execute_cmd(self, command_string):
|
||||
"""
|
||||
Break the connection and do some accounting.
|
||||
Execute a command string.
|
||||
"""
|
||||
|
||||
# handle the 'idle' command
|
||||
if str(command_string).strip() == IDLE_COMMAND:
|
||||
self.update_session_counters(idle=True)
|
||||
return
|
||||
|
||||
# all other inputs, including empty inputs
|
||||
character = self.get_character()
|
||||
|
||||
if character:
|
||||
#call hook functions
|
||||
character.at_disconnect()
|
||||
character.player.at_disconnect()
|
||||
uaccount = character.player.user
|
||||
uaccount.last_login = datetime.now()
|
||||
uaccount.save()
|
||||
self.disconnectClient()
|
||||
self.logged_in = False
|
||||
sessionhandler.remove_session(self)
|
||||
|
||||
def game_connect_screen(self):
|
||||
#print "loggedin _execute_cmd: '%s' __ %s" % (command_string, character)
|
||||
# normal operation.
|
||||
character.execute_cmd(command_string)
|
||||
else:
|
||||
#print "unloggedin _execute_cmd: '%s' __ %s" % (command_string, character)
|
||||
# we are not logged in yet; call cmdhandler directly
|
||||
cmdhandler.cmdhandler(self, command_string, unloggedin=True)
|
||||
|
||||
def get_data_obj(self, **kwargs):
|
||||
"""
|
||||
Show the banner screen. Grab from the 'connect_screen'
|
||||
config directive. If more than one connect screen is
|
||||
defined in the ConnectScreen attribute, it will be
|
||||
random which screen is used.
|
||||
Create a data object, storing keyword arguments on itself as arguments.
|
||||
"""
|
||||
screen = ConnectScreen.objects.get_random_connect_screen()
|
||||
string = ansi.parse_ansi(screen.text)
|
||||
self.msg(string)
|
||||
|
||||
return IOdata(**kwargs)
|
||||
|
||||
def __eq__(self, other):
|
||||
return self.address == other.address
|
||||
|
||||
def __str__(self):
|
||||
"""
|
||||
String representation of the user session class. We use
|
||||
this a lot in the server logs.
|
||||
"""
|
||||
if self.logged_in:
|
||||
symbol = '#'
|
||||
else:
|
||||
symbol = '?'
|
||||
return "<%s> %s@%s" % (symbol, self.name, self.address,)
|
||||
|
||||
def __unicode__(self):
|
||||
"""
|
||||
Unicode representation
|
||||
"""
|
||||
return u"%s" % str(self)
|
||||
|
||||
|
||||
|
||||
#------------------------------------------------------------
|
||||
# Session class - inherit from this
|
||||
#------------------------------------------------------------
|
||||
|
||||
class Session(SessionBase):
|
||||
"""
|
||||
The main class to inherit from. Overload the methods here.
|
||||
"""
|
||||
|
||||
# exchange this for a unique name you can use to identify the
|
||||
# protocol type this session uses
|
||||
protocol_key = "TemplateProtocol"
|
||||
|
||||
#
|
||||
# Hook methods
|
||||
#
|
||||
|
||||
def at_connect(self):
|
||||
"""
|
||||
This method is called by the connection mechanic after
|
||||
connection has been made. The session is added to the
|
||||
sessionhandler and basic accounting has been made at this
|
||||
point.
|
||||
|
||||
This is the place to put e.g. welcome screens specific to the
|
||||
protocol.
|
||||
"""
|
||||
pass
|
||||
|
||||
def at_login(self, player):
|
||||
"""
|
||||
This method is called by the login mechanic whenever the user
|
||||
has finished authenticating. The user has been moved to the
|
||||
right sessionhandler list and basic book keeping has been
|
||||
done at this point (so logged_in=True).
|
||||
"""
|
||||
pass
|
||||
|
||||
def at_disconnect(self, reason=None):
|
||||
"""
|
||||
This method is called just before cleaning up the session
|
||||
(so still logged_in=True at this point).
|
||||
"""
|
||||
pass
|
||||
|
||||
def at_data_in(self, string="", data=None):
|
||||
"""
|
||||
Player -> Evennia
|
||||
"""
|
||||
pass
|
||||
|
||||
def at_data_out(self, string="", data=None):
|
||||
"""
|
||||
Evennia -> Player
|
||||
|
||||
string - an string of any form to send to the player
|
||||
data - a data structure of any form
|
||||
|
||||
"""
|
||||
pass
|
||||
|
||||
# easy-access functions
|
||||
def login(self, player):
|
||||
"""
|
||||
After the user has authenticated, this actually
|
||||
logs them in. At this point the session has
|
||||
a User account tied to it. User is an django
|
||||
object that handles stuff like permissions and
|
||||
access, it has no visible precense in the game.
|
||||
This User object is in turn tied to a game
|
||||
Object, which represents whatever existence
|
||||
the player has in the game world. This is the
|
||||
'character' referred to in this module.
|
||||
"""
|
||||
# set the session properties
|
||||
|
||||
user = player.user
|
||||
self.uid = user.id
|
||||
self.name = user.username
|
||||
self.logged_in = True
|
||||
self.conn_time = time.time()
|
||||
if player.db.encoding:
|
||||
self.encoding = player.db.encoding
|
||||
|
||||
if not settings.ALLOW_MULTISESSION:
|
||||
# disconnect previous sessions.
|
||||
sessionhandler.disconnect_duplicate_session(self)
|
||||
|
||||
# start (persistent) scripts on this object
|
||||
reloads.reload_scripts(obj=self.get_character(), init_mode=True)
|
||||
|
||||
logger.log_infomsg("Logged in: %s" % self)
|
||||
self.cemit_info('Logged in: %s' % self)
|
||||
|
||||
# Update their account's last login time.
|
||||
user.last_login = datetime.now()
|
||||
user.save()
|
||||
|
||||
def cemit_info(self, message):
|
||||
"""
|
||||
Channel emits info to the appropriate info channel. By default, this
|
||||
is MUDConnections.
|
||||
"""
|
||||
try:
|
||||
cchan = settings.CHANNEL_CONNECTINFO
|
||||
cchan = Channel.objects.get_channel(cchan[0])
|
||||
cchan.msg("[%s]: %s" % (cchan.key, message))
|
||||
except Exception:
|
||||
logger.log_infomsg(message)
|
||||
|
||||
|
||||
"alias for at_login"
|
||||
self.at_login(player)
|
||||
def logout(self):
|
||||
"alias for at_logout"
|
||||
self.at_disconnect()
|
||||
def msg(self, string='', data=None):
|
||||
"alias for at_data_out"
|
||||
self.at_data_out(string, data)
|
||||
|
|
|
|||
|
|
@ -1,137 +1,184 @@
|
|||
"""
|
||||
Sessionhandler, stores and handles
|
||||
a list of all player connections (sessions).
|
||||
This module handles sessions of users connecting
|
||||
to the server.
|
||||
|
||||
Since Evennia supports several different connection
|
||||
protocols, it is important to have a joint place
|
||||
to store session info. It also makes it easier
|
||||
to dispatch data.
|
||||
|
||||
Whereas server.py handles all setup of the server
|
||||
and database itself, this file handles all that
|
||||
comes after initial startup.
|
||||
|
||||
All new sessions (of whatever protocol) are responsible for
|
||||
registering themselves with this module.
|
||||
|
||||
"""
|
||||
import time
|
||||
from django.conf import settings
|
||||
from django.contrib.auth.models import User
|
||||
from src.config.models import ConfigValue
|
||||
from src.utils import logger
|
||||
|
||||
# Our list of connected sessions.
|
||||
SESSIONS = []
|
||||
ALLOW_MULTISESSION = settings.ALLOW_MULTISESSION
|
||||
|
||||
def add_session(session):
|
||||
"""
|
||||
Adds a session to the session list.
|
||||
"""
|
||||
SESSIONS.insert(0, session)
|
||||
change_session_count(1)
|
||||
logger.log_infomsg('Sessions active: %d' % (len(get_sessions(return_unlogged=True),)))
|
||||
|
||||
def get_sessions(return_unlogged=False):
|
||||
"""
|
||||
Lists the connected session objects.
|
||||
"""
|
||||
if return_unlogged:
|
||||
return SESSIONS
|
||||
else:
|
||||
return [sess for sess in SESSIONS if sess.logged_in]
|
||||
|
||||
def get_session_id_list(return_unlogged=False):
|
||||
"""
|
||||
Lists the connected session object ids.
|
||||
"""
|
||||
if return_unlogged:
|
||||
return SESSIONS
|
||||
else:
|
||||
return [sess.uid for sess in SESSIONS if sess.logged_in]
|
||||
#------------------------------------------------------------
|
||||
# SessionHandler class
|
||||
#------------------------------------------------------------
|
||||
|
||||
def disconnect_all_sessions():
|
||||
class SessionHandler(object):
|
||||
"""
|
||||
Cleanly disconnect all of the connected sessions.
|
||||
"""
|
||||
for sess in get_sessions():
|
||||
sess.handle_close()
|
||||
This object holds the stack of sessions active in the game at
|
||||
any time.
|
||||
|
||||
def disconnect_duplicate_session(session):
|
||||
"""
|
||||
Disconnects any existing session under the same object. This is used in
|
||||
connection recovery to help with record-keeping.
|
||||
"""
|
||||
SESSIONS = get_sessions()
|
||||
session_pobj = session.get_character()
|
||||
for other_session in SESSIONS:
|
||||
other_pobject = other_session.get_character()
|
||||
if session_pobj == other_pobject and other_session != session:
|
||||
other_session.msg("Your account has been logged in from elsewhere, disconnecting.")
|
||||
other_session.disconnectClient()
|
||||
return True
|
||||
return False
|
||||
A session register with the handler in two steps, first by
|
||||
registering itself with the connect() method. This indicates an
|
||||
non-authenticated session. Whenever the session is authenticated
|
||||
the session together with the related player is sent to the login()
|
||||
method.
|
||||
|
||||
def check_all_sessions():
|
||||
"""
|
||||
Check all currently connected sessions and see if any are dead.
|
||||
"""
|
||||
idle_timeout = int(ConfigValue.objects.conf('idle_timeout'))
|
||||
|
||||
if len(SESSIONS) <= 0:
|
||||
return
|
||||
|
||||
if idle_timeout <= 0:
|
||||
return
|
||||
|
||||
for sess in get_sessions(return_unlogged=True):
|
||||
if (time.time() - sess.cmd_last) > idle_timeout:
|
||||
sess.msg("Idle timeout exceeded, disconnecting.")
|
||||
sess.handle_close()
|
||||
|
||||
def change_session_count(num):
|
||||
"""
|
||||
Count number of connected users by use of a config value
|
||||
|
||||
num can be a positive or negative value. If 0, the counter
|
||||
will be reset to 0.
|
||||
"""
|
||||
|
||||
if num == 0:
|
||||
# reset
|
||||
ConfigValue.objects.conf('nr_sessions', 0)
|
||||
|
||||
nr = ConfigValue.objects.conf('nr_sessions')
|
||||
if nr == None:
|
||||
nr = 0
|
||||
else:
|
||||
nr = int(nr)
|
||||
nr += num
|
||||
ConfigValue.objects.conf('nr_sessions', str(nr))
|
||||
def __init__(self):
|
||||
"""
|
||||
Init the handler. We track two types of sessions, those
|
||||
who have just connected (unloggedin) and those who have
|
||||
logged in (authenticated).
|
||||
"""
|
||||
self.unloggedin = []
|
||||
self.loggedin = []
|
||||
|
||||
|
||||
def remove_session(session):
|
||||
"""
|
||||
Removes a session from the session list.
|
||||
"""
|
||||
try:
|
||||
SESSIONS.remove(session)
|
||||
change_session_count(-1)
|
||||
logger.log_infomsg('Sessions active: %d' % (len(get_sessions()),))
|
||||
except ValueError:
|
||||
# the session was already removed.
|
||||
logger.log_errmsg("Unable to remove session: %s" % (session,))
|
||||
return
|
||||
|
||||
def find_sessions_from_username(username):
|
||||
"""
|
||||
Given a username, return any matching sessions.
|
||||
"""
|
||||
try:
|
||||
uobj = User.objects.get(username=username)
|
||||
uid = uobj.id
|
||||
return [session for session in SESSIONS if session.uid == uid]
|
||||
except User.DoesNotExist:
|
||||
return None
|
||||
|
||||
def sessions_from_object(targ_object):
|
||||
"""
|
||||
Returns a list of matching session objects, or None if there are no matches.
|
||||
|
||||
targobject: (Object) The object to match.
|
||||
"""
|
||||
return [session for session in SESSIONS
|
||||
if session.get_character() == targ_object]
|
||||
def add_unloggedin_session(self, session):
|
||||
"""
|
||||
Call at first connect. This adds a not-yet authenticated session.
|
||||
"""
|
||||
self.unloggedin.insert(0, session)
|
||||
|
||||
def announce_all(message):
|
||||
"""
|
||||
Announces something to all connected players.
|
||||
"""
|
||||
for session in get_sessions():
|
||||
session.msg('%s' % message)
|
||||
def add_loggedin_session(self, session):
|
||||
"""
|
||||
Log in the previously unloggedin session and the player we by
|
||||
now should know is connected to it. After this point we
|
||||
assume the session to be logged in one way or another.
|
||||
"""
|
||||
# prep the session with player/user info
|
||||
|
||||
|
||||
if not ALLOW_MULTISESSION:
|
||||
# disconnect previous sessions.
|
||||
self.disconnect_duplicate_sessions(session)
|
||||
|
||||
# store/move the session to the right list
|
||||
try:
|
||||
self.unloggedin.remove(session)
|
||||
except ValueError:
|
||||
pass
|
||||
self.loggedin.insert(0, session)
|
||||
self.session_count(1)
|
||||
|
||||
def remove_session(self, session):
|
||||
"""
|
||||
Remove session from the handler
|
||||
"""
|
||||
removed = False
|
||||
try:
|
||||
self.unloggedin.remove(session)
|
||||
except Exception:
|
||||
try:
|
||||
self.loggedin.remove(session)
|
||||
except Exception:
|
||||
return
|
||||
self.session_count(-1)
|
||||
|
||||
def get_sessions(self, include_unloggedin=False):
|
||||
"""
|
||||
Returns the connected session objects.
|
||||
"""
|
||||
if include_unloggedin:
|
||||
return self.loggedin + self.unloggedin
|
||||
else:
|
||||
return self.loggedin
|
||||
|
||||
def disconnect_all_sessions(self, reason=None):
|
||||
"""
|
||||
Cleanly disconnect all of the connected sessions.
|
||||
"""
|
||||
sessions = self.get_sessions(include_unloggedin=True)
|
||||
for session in sessions:
|
||||
session.session_disconnect(reason)
|
||||
self.session_count(0)
|
||||
|
||||
def disconnect_duplicate_sessions(self, session):
|
||||
"""
|
||||
Disconnects any existing sessions with the same game object. This is used in
|
||||
connection recovery to help with record-keeping.
|
||||
"""
|
||||
reason = "Your account has been logged in from elsewhere. Disconnecting."
|
||||
sessions = self.get_sessions()
|
||||
session_character = self.get_character(session)
|
||||
logged_out = 0
|
||||
for other_session in sessions:
|
||||
other_character = self.get_character(other_session)
|
||||
if session_character == other_character and other_session != session:
|
||||
self.remove_session(other_session, reason=reason)
|
||||
logged_out += 1
|
||||
self.session_count(-logged_out)
|
||||
return logged_out
|
||||
|
||||
def validate_sessions(self):
|
||||
"""
|
||||
Check all currently connected sessions (logged in and not)
|
||||
and see if any are dead.
|
||||
"""
|
||||
for session in self.get_sessions(include_unloggedin=True):
|
||||
session.session_validate()
|
||||
|
||||
def session_count(self, num=None):
|
||||
"""
|
||||
Count up/down the number of connected, authenticated users.
|
||||
If num is None, the current number of sessions is returned.
|
||||
|
||||
num can be a positive or negative value to be added to the current count.
|
||||
If 0, the counter will be reset to 0.
|
||||
"""
|
||||
if num == None:
|
||||
# show the current value. This also syncs it.
|
||||
return int(ConfigValue.objects.conf('nr_sessions', default=0))
|
||||
elif num == 0:
|
||||
# reset value to 0
|
||||
ConfigValue.objects.conf('nr_sessions', 0)
|
||||
else:
|
||||
# add/remove session count from value
|
||||
add = int(ConfigValue.objects.conf('nr_sessions', default=0))
|
||||
num = max(0, num + add)
|
||||
ConfigValue.objects.conf('nr_sessions', str(num))
|
||||
|
||||
def sessions_from_player(self, player):
|
||||
"""
|
||||
Given a player, return any matching sessions.
|
||||
"""
|
||||
username = player.user.username
|
||||
try:
|
||||
uobj = User.objects.get(username=username)
|
||||
except User.DoesNotExist:
|
||||
return None
|
||||
uid = uobj.id
|
||||
return [session for session in self.loggedin if session.uid == uid]
|
||||
|
||||
def sessions_from_character(self, character):
|
||||
"""
|
||||
Given a game character, return any matching sessions.
|
||||
"""
|
||||
player = character.player
|
||||
if player:
|
||||
return self.sessions_from_player(player)
|
||||
return None
|
||||
|
||||
def session_from_suid(self, suid):
|
||||
"""
|
||||
Given a session id, retrieve the session (this is primarily
|
||||
intended to be called by web clients)
|
||||
"""
|
||||
return [sess for sess in self.get_sessions(include_unloggedin=True) if sess.suid and sess.suid == suid]
|
||||
|
||||
SESSIONS = SessionHandler()
|
||||
|
||||
|
||||
|
|
|
|||
153
src/server/telnet.py
Normal file
153
src/server/telnet.py
Normal file
|
|
@ -0,0 +1,153 @@
|
|||
"""
|
||||
This module implements the telnet protocol.
|
||||
|
||||
This depends on a generic session module that implements
|
||||
the actual login procedure of the game, tracks
|
||||
sessions etc.
|
||||
|
||||
"""
|
||||
|
||||
from twisted.conch.telnet import StatefulTelnetProtocol
|
||||
from django.conf import settings
|
||||
from src.config.models import ConnectScreen
|
||||
from src.server import session
|
||||
from src.utils import ansi, utils
|
||||
|
||||
ENCODINGS = settings.ENCODINGS
|
||||
|
||||
class TelnetProtocol(StatefulTelnetProtocol, session.Session):
|
||||
"""
|
||||
Each player connecting over telnet (ie using most traditional mud
|
||||
clients) gets a telnet protocol instance assigned to them. All
|
||||
communication between game and player goes through here.
|
||||
"""
|
||||
|
||||
# identifier in case one needs to easily separate protocols at run-time.
|
||||
protocol_key = "telnet"
|
||||
|
||||
# telnet-specific hooks
|
||||
|
||||
def connectionMade(self):
|
||||
"""
|
||||
This is called when the connection is first
|
||||
established.
|
||||
"""
|
||||
# initialize the session
|
||||
self.session_connect(self.getClientAddress())
|
||||
|
||||
def connectionLost(self, reason="Disconnecting. Goodbye for now."):
|
||||
"""
|
||||
This is executed when the connection is lost for
|
||||
whatever reason. It should also be called from
|
||||
self.at_disconnect() so one can close the connection
|
||||
manually without having to know the name of this specific
|
||||
method.
|
||||
"""
|
||||
self.session_disconnect(reason)
|
||||
self.transport.loseConnection()
|
||||
|
||||
def getClientAddress(self):
|
||||
"""
|
||||
Returns the client's address and port in a tuple. For example
|
||||
('127.0.0.1', 41917)
|
||||
"""
|
||||
return self.transport.client
|
||||
|
||||
def lineReceived(self, string):
|
||||
"""
|
||||
Communication Player -> Evennia. Any line return indicates a
|
||||
command for the purpose of the MUD. So we take the user input
|
||||
and pass it on to the game engine.
|
||||
"""
|
||||
self.at_data_in(string)
|
||||
|
||||
def lineSend(self, string):
|
||||
"""
|
||||
Communication Evennia -> Player
|
||||
Any string sent should already have been
|
||||
properly formatted and processed
|
||||
before reaching this point.
|
||||
|
||||
"""
|
||||
self.sendLine(string) #this is the telnet-specific method for sending
|
||||
|
||||
# session-general method hooks
|
||||
|
||||
def at_connect(self):
|
||||
"""
|
||||
Show the banner screen. Grab from the 'connect_screen'
|
||||
config directive. If more than one connect screen is
|
||||
defined in the ConnectScreen attribute, it will be
|
||||
random which screen is used.
|
||||
"""
|
||||
self.telnet_markup = True
|
||||
# show screen
|
||||
screen = ConnectScreen.objects.get_random_connect_screen()
|
||||
string = ansi.parse_ansi(screen.text)
|
||||
self.lineSend(string)
|
||||
|
||||
def at_login(self):
|
||||
"""
|
||||
Called after authentication. self.logged_in=True at this point.
|
||||
"""
|
||||
if self.player.has_attribute('telnet_markup'):
|
||||
self.telnet_markup = self.player.get_attribute("telnet_markup")
|
||||
else:
|
||||
self.telnet_markup = True
|
||||
|
||||
def at_disconnect(self, reason="Connection closed. Goodbye for now."):
|
||||
"""
|
||||
Disconnect from server
|
||||
"""
|
||||
if reason:
|
||||
self.lineSend(reason)
|
||||
self.connectionLost(reasoon)
|
||||
|
||||
def at_data_out(self, string, data=None):
|
||||
"""
|
||||
Data Evennia -> Player access hook. 'data' argument is ignored.
|
||||
"""
|
||||
if self.encoding:
|
||||
try:
|
||||
string = utils.to_str(string, encoding=self.encoding)
|
||||
self.lineSend(ansi.parse_ansi(string, strip_ansi=not self.telnet_markup))
|
||||
return
|
||||
except Exception:
|
||||
pass
|
||||
# malformed/wrong encoding defined on player - try some defaults
|
||||
for encoding in ENCODINGS:
|
||||
try:
|
||||
string = utils.to_str(string, encoding=encoding)
|
||||
err = None
|
||||
break
|
||||
except Exception, e:
|
||||
err = str(e)
|
||||
continue
|
||||
if err:
|
||||
self.lineSend(err)
|
||||
else:
|
||||
self.lineSend(ansi.parse_ansi(string, strip_ansi=not self.telnet_markup))
|
||||
|
||||
def at_data_in(self, string, data=None):
|
||||
"""
|
||||
Line from Player -> Evennia. 'data' argument is not used.
|
||||
|
||||
"""
|
||||
if self.encoding:
|
||||
try:
|
||||
string = utils.to_unicode(string, encoding=self.encoding)
|
||||
self.execute_cmd(string)
|
||||
return
|
||||
except Exception, e:
|
||||
err = str(e)
|
||||
print err
|
||||
# malformed/wrong encoding defined on player - try some defaults
|
||||
for encoding in ENCODINGS:
|
||||
try:
|
||||
string = utils.to_unicode(string, encoding=encoding)
|
||||
err = None
|
||||
break
|
||||
except Exception, e:
|
||||
err = str(e)
|
||||
continue
|
||||
self.execute_cmd(self, string)
|
||||
286
src/server/webclient.py
Normal file
286
src/server/webclient.py
Normal file
|
|
@ -0,0 +1,286 @@
|
|||
"""
|
||||
Web client server resource.
|
||||
|
||||
The Evennia web client consists of two components running
|
||||
on twisted and django. They are both a part of the Evennia
|
||||
website url tree (so the testing website might be located
|
||||
on http://localhost:8020/, whereas the webclient can be
|
||||
found on http://localhost:8020/webclient.)
|
||||
|
||||
/webclient - this url is handled through django's template
|
||||
system and serves the html page for the client
|
||||
itself along with its javascript chat program.
|
||||
/webclientdata - this url is called by the ajax chat using
|
||||
POST requests (long-polling when necessary)
|
||||
The WebClient resource in this module will
|
||||
handle these requests and act as a gateway
|
||||
to sessions connected over the webclient.
|
||||
"""
|
||||
|
||||
from twisted.web import server, resource
|
||||
from twisted.internet import defer
|
||||
|
||||
from django.utils import simplejson
|
||||
from django.utils.functional import Promise
|
||||
from django.utils.encoding import force_unicode
|
||||
from django.conf import settings
|
||||
from src.utils import utils
|
||||
from src.utils.ansi2html import parse_html
|
||||
from src.config.models import ConnectScreen
|
||||
from src.server import session, sessionhandler
|
||||
|
||||
SERVERNAME = settings.SERVERNAME
|
||||
ENCODINGS = settings.ENCODINGS
|
||||
|
||||
# defining a simple json encoder for returning
|
||||
# django data to the client. Might need to
|
||||
# extend this if one wants to send more
|
||||
# complex database objects too.
|
||||
|
||||
class LazyEncoder(simplejson.JSONEncoder):
|
||||
def default(self, obj):
|
||||
if isinstance(obj, Promise):
|
||||
return force_unicode(obj)
|
||||
return super(LazyEncoder, self).default(obj)
|
||||
def jsonify(obj):
|
||||
return simplejson.dumps(obj, ensure_ascii=False, cls=LazyEncoder)
|
||||
|
||||
|
||||
#
|
||||
# WebClient resource - this is called by the ajax client
|
||||
# using POST requests to /webclientdata.
|
||||
#
|
||||
|
||||
class WebClient(resource.Resource):
|
||||
"""
|
||||
An ajax/comet long-polling transport protocol for
|
||||
"""
|
||||
isLeaf = True
|
||||
allowedMethods = ('POST',)
|
||||
|
||||
def __init__(self):
|
||||
self.requests = {}
|
||||
self.databuffer = {}
|
||||
|
||||
def getChild(self, path, request):
|
||||
"""
|
||||
This is the place to put dynamic content.
|
||||
"""
|
||||
return self
|
||||
|
||||
def _responseFailed(self, failure, suid, request):
|
||||
"callback if a request is lost/timed out"
|
||||
try:
|
||||
self.requests.get(suid, []).remove(request)
|
||||
except ValueError:
|
||||
pass
|
||||
|
||||
def lineSend(self, suid, string, data=None):
|
||||
"""
|
||||
This adds the data to the buffer and/or sends it to
|
||||
the client as soon as possible.
|
||||
"""
|
||||
requests = self.requests.get(suid, None)
|
||||
if requests:
|
||||
request = requests.pop(0)
|
||||
# we have a request waiting. Return immediately.
|
||||
request.write(jsonify({'msg':string, 'data':data}))
|
||||
request.finish()
|
||||
self.requests[suid] = requests
|
||||
else:
|
||||
# no waiting request. Store data in buffer
|
||||
dataentries = self.databuffer.get(suid, [])
|
||||
dataentries.append(jsonify({'msg':string, 'data':data}))
|
||||
self.databuffer[suid] = dataentries
|
||||
|
||||
def disconnect(self, suid):
|
||||
"Disconnect session"
|
||||
sess = sessionhandler.SESSIONS.session_from_suid(suid)
|
||||
if sess:
|
||||
sess[0].session_disconnect()
|
||||
if self.requests.has_key(suid):
|
||||
for request in self.requests.get(suid, []):
|
||||
request.finish()
|
||||
del self.requests[suid]
|
||||
if self.databuffer.has_key(suid):
|
||||
del self.databuffer[suid]
|
||||
|
||||
def mode_init(self, request):
|
||||
"""
|
||||
This is called by render_POST when the client
|
||||
requests an init mode operation (at startup)
|
||||
"""
|
||||
csess = request.getSession() # obs, this is a cookie, not an evennia session!
|
||||
#csees.expireCallbacks.append(lambda : )
|
||||
suid = csess.uid
|
||||
remote_addr = request.getClientIP()
|
||||
host_string = "%s (%s:%s)" % (SERVERNAME, request.getHost().host, request.getHost().port)
|
||||
self.requests[suid] = []
|
||||
self.databuffer[suid] = []
|
||||
|
||||
sess = sessionhandler.SESSIONS.session_from_suid(suid)
|
||||
if not sess:
|
||||
sess = WebClientSession()
|
||||
sess.client = self
|
||||
sess.session_connect(remote_addr, suid)
|
||||
sessionhandler.SESSIONS.add_unloggedin_session(sess)
|
||||
return jsonify({'msg':host_string})
|
||||
|
||||
def mode_input(self, request):
|
||||
"""
|
||||
This is called by render_POST when the client
|
||||
is sending data to the server.
|
||||
"""
|
||||
string = request.args.get('msg', [''])[0]
|
||||
data = request.args.get('data', [None])[0]
|
||||
suid = request.getSession().uid
|
||||
sess = sessionhandler.SESSIONS.session_from_suid(suid)
|
||||
if sess:
|
||||
sess[0].at_data_in(string, data)
|
||||
return ''
|
||||
|
||||
def mode_receive(self, request):
|
||||
"""
|
||||
This is called by render_POST when the client is telling us
|
||||
that it is ready to receive data as soon as it is
|
||||
available. This is the basis of a long-polling (comet)
|
||||
mechanism: the server will wait to reply until data is
|
||||
available.
|
||||
"""
|
||||
suid = request.getSession().uid
|
||||
dataentries = self.databuffer.get(suid, [])
|
||||
if dataentries:
|
||||
return dataentries.pop(0)
|
||||
reqlist = self.requests.get(suid, [])
|
||||
request.notifyFinish().addErrback(self._responseFailed, suid, request)
|
||||
reqlist.append(request)
|
||||
self.requests[suid] = reqlist
|
||||
return server.NOT_DONE_YET
|
||||
|
||||
def render_POST(self, request):
|
||||
"""
|
||||
This function is what Twisted calls with POST requests coming
|
||||
in from the ajax client. The requests should be tagged with
|
||||
different modes depending on what needs to be done, such as
|
||||
initializing or sending/receving data through the request. It
|
||||
uses a long-polling mechanism to avoid sending data unless
|
||||
there is actual data available.
|
||||
"""
|
||||
dmode = request.args.get('mode', [None])[0]
|
||||
if dmode == 'init':
|
||||
# startup. Setup the server.
|
||||
return self.mode_init(request)
|
||||
elif dmode == 'input':
|
||||
# input from the client to the server
|
||||
return self.mode_input(request)
|
||||
elif dmode == 'receive':
|
||||
# the client is waiting to receive data.
|
||||
return self.mode_receive(request)
|
||||
else:
|
||||
# this should not happen if client sends valid data.
|
||||
return ''
|
||||
|
||||
#
|
||||
# A session type handling communication over the
|
||||
# web client interface.
|
||||
#
|
||||
|
||||
class WebClientSession(session.Session):
|
||||
"""
|
||||
This represents a session running in a webclient.
|
||||
"""
|
||||
|
||||
def at_connect(self):
|
||||
"""
|
||||
Show the banner screen. Grab from the 'connect_screen'
|
||||
config directive. If more than one connect screen is
|
||||
defined in the ConnectScreen attribute, it will be
|
||||
random which screen is used.
|
||||
"""
|
||||
# show screen
|
||||
screen = ConnectScreen.objects.get_random_connect_screen()
|
||||
string = parse_html(screen.text)
|
||||
self.client.lineSend(self.suid, string)
|
||||
|
||||
def at_login(self):
|
||||
"""
|
||||
Called after authentication. self.logged_in=True at this point.
|
||||
"""
|
||||
if self.player.has_attribute('telnet_markup'):
|
||||
self.telnet_markup = self.player.get_attribute("telnet_markup")
|
||||
else:
|
||||
self.telnet_markup = True
|
||||
|
||||
def at_disconnect(self, reason=None):
|
||||
"""
|
||||
Disconnect from server
|
||||
"""
|
||||
if reason:
|
||||
self.lineSend(self.suid, reason)
|
||||
self.client.disconnect(self.suid)
|
||||
|
||||
def at_data_out(self, string='', data=None):
|
||||
"""
|
||||
Data Evennia -> Player access hook.
|
||||
|
||||
data argument may be used depending on
|
||||
the client-server implementation.
|
||||
"""
|
||||
|
||||
if data:
|
||||
# treat data?
|
||||
pass
|
||||
|
||||
# string handling is similar to telnet
|
||||
if self.encoding:
|
||||
try:
|
||||
string = utils.to_str(string, encoding=self.encoding)
|
||||
#self.client.lineSend(self.suid, ansi.parse_ansi(string, strip_ansi=True))
|
||||
self.client.lineSend(self.suid, parse_html(string))
|
||||
return
|
||||
except Exception:
|
||||
pass
|
||||
# malformed/wrong encoding defined on player - try some defaults
|
||||
for encoding in ENCODINGS:
|
||||
try:
|
||||
string = utils.to_str(string, encoding=encoding)
|
||||
err = None
|
||||
break
|
||||
except Exception, e:
|
||||
err = str(e)
|
||||
continue
|
||||
if err:
|
||||
self.client.lineSend(self.suid, err)
|
||||
else:
|
||||
#self.client.lineSend(self.suid, ansi.parse_ansi(string, strip_ansi=True))
|
||||
self.client.lineSend(self.suid, parse_html(string))
|
||||
def at_data_in(self, string, data=None):
|
||||
"""
|
||||
Input from Player -> Evennia (called by client).
|
||||
Use of 'data' is up to the client - server implementation.
|
||||
"""
|
||||
|
||||
# treat data?
|
||||
if data:
|
||||
pass
|
||||
|
||||
# the string part is identical to telnet
|
||||
if self.encoding:
|
||||
try:
|
||||
string = utils.to_unicode(string, encoding=self.encoding)
|
||||
self.execute_cmd(string)
|
||||
return
|
||||
except Exception, e:
|
||||
err = str(e)
|
||||
print err
|
||||
# malformed/wrong encoding defined on player - try some defaults
|
||||
for encoding in ENCODINGS:
|
||||
try:
|
||||
string = utils.to_unicode(string, encoding=encoding)
|
||||
err = None
|
||||
break
|
||||
except Exception, e:
|
||||
err = str(e)
|
||||
continue
|
||||
self.execute_cmd(string)
|
||||
|
||||
60
src/server/webserver.py
Normal file
60
src/server/webserver.py
Normal file
|
|
@ -0,0 +1,60 @@
|
|||
"""
|
||||
This implements resources for twisted webservers using the wsgi
|
||||
interface of django. This alleviates the need of running e.g. an
|
||||
apache server to serve Evennia's web presence (although you could do
|
||||
that too if desired).
|
||||
|
||||
The actual servers are started inside server.py as part of the Evennia
|
||||
application.
|
||||
|
||||
(Lots of thanks to http://githup.com/clemensha/twisted-wsgi-django for
|
||||
a great example/aid on how to do this.)
|
||||
|
||||
"""
|
||||
from twisted.web import resource
|
||||
from twisted.python import threadpool
|
||||
from twisted.internet import reactor
|
||||
|
||||
from twisted.web.wsgi import WSGIResource
|
||||
from django.core.handlers.wsgi import WSGIHandler
|
||||
|
||||
#
|
||||
# Website server resource
|
||||
#
|
||||
|
||||
class DjangoWebRoot(resource.Resource):
|
||||
"""
|
||||
This creates a web root (/) that Django
|
||||
understands by tweaking the way the
|
||||
child instancee are recognized.
|
||||
"""
|
||||
|
||||
def __init__(self):
|
||||
"""
|
||||
Setup the django+twisted resource
|
||||
"""
|
||||
resource.Resource.__init__(self)
|
||||
self.wsgi_resource = self._wsgi_resource()
|
||||
|
||||
def _wsgi_resource(self):
|
||||
"""
|
||||
Sets up a threaded webserver resource by tying
|
||||
django and twisted together.
|
||||
"""
|
||||
# Start the threading
|
||||
pool = threadpool.ThreadPool()
|
||||
pool.start()
|
||||
# Set it up so the pool stops after e.g. Ctrl-C kills the server
|
||||
reactor.addSystemEventTrigger('after', 'shutdown', pool.stop)
|
||||
# combine twisted's wsgi resource with django's wsgi handler
|
||||
wsgi_resource = WSGIResource(reactor, pool, WSGIHandler())
|
||||
return wsgi_resource
|
||||
|
||||
def getChild(self, path, request):
|
||||
"""
|
||||
To make things work we nudge the
|
||||
url tree to make this the root.
|
||||
"""
|
||||
path0 = request.prepath.pop(0)
|
||||
request.postpath.insert(0, path0)
|
||||
return self.wsgi_resource
|
||||
Loading…
Add table
Add a link
Reference in a new issue