Mercurial > traipse_dev
diff orpg/networking/mplay_server.py @ 122:36919b8a3ef9 alpha
Traipse Alpha 'OpenRPG' {091031-00}
Traipse is a distribution of OpenRPG that is designed to be easy to setup and go.
Traipse also makes it easy for developers to work on code without fear of
sacrifice. 'Ornery-Orc' continues the trend of 'Grumpy' and adds fixes to the
code. 'Ornery-Orc's main goal is to offer more advanced features and enhance the
productivity of the user.
Update Summary (Cleaning up for Beta)
Added Bookmarks
Fix to Remote Admin Commands
Minor fix to text based Server
Fix to Pretty Print, from Core
Fix to Splitter Nodes not being created
Fix to massive amounts of images loading, from Core
Added 'boot' command to remote admin
Added confirmation window for sent nodes
Minor changes to allow for portability to an OpenSUSE linux OS
Miniatures Layer pop up box allows users to turn off Mini labels, from FlexiRPG
Zoom Mouse plugin added
Images added to Plugin UI
Switching to Element Tree
Map efficiency, from FlexiRPG
Added Status Bar to Update Manager
default_manifest.xml renamed to default_upmana.xml
Cleaner clode for saved repositories
New TrueDebug Class in orpg_log
Mercurial's hgweb folder is ported to upmana
Happy Halloween!
author | sirebral |
---|---|
date | Sat, 31 Oct 2009 22:07:55 -0500 |
parents | 9314d63c0941 |
children | 8827271fbe1b |
line wrap: on
line diff
--- a/orpg/networking/mplay_server.py Fri Oct 30 22:21:40 2009 -0500 +++ b/orpg/networking/mplay_server.py Sat Oct 31 22:07:55 2009 -0500 @@ -31,6 +31,8 @@ # 04-15-2005 [Snowdog]: Added patch from Brandan Yares (xeriar). Reference: patch tracker id #1182076 +from __future__ import with_statement + __version__ = "$Id: mplay_server.py,v 1.155 2008/01/24 03:52:03 digitalxero Exp $" #!/usr/bin/env python @@ -43,14 +45,7 @@ <role action='set,get,display' player='' group_id='' boot_pwd='' role=''/> """ -import re -import gc -import cgi -import sys -import string -import time -import urllib -import traceback +import re, gc, cgi, sys, string, time, urllib, traceback from mplay_client import * from mplay_client import MPLAY_LENSIZE @@ -62,8 +57,11 @@ from struct import pack, unpack, calcsize from meta_server_lib import * -# Import the minidom XML module -from xml.dom import minidom +from xml.etree.ElementTree import ElementTree, Element, iselement +from xml.etree.ElementTree import fromstring, tostring, parse, XML + +from orpg.tools.orpg_log import logger, crash, debug +from orpg.tools.decorators import debugging # Snag the version number from orpg.orpg_version import VERSION, PROTOCOL_VERSION, CLIENT_STRING, SERVER_MIN_CLIENT_VERSION @@ -77,6 +75,7 @@ class game_group(object): + debug() def __init__(self, id, name, pwd, desc="", boot_pwd="", minVersion="", mapFile=None, messageFile=None, persist=0): self.id = id @@ -93,55 +92,52 @@ self.voice = {} self.persistant = persist self.mapFile = None - + ### Needs to use Element Tree closer if mapFile != None: - """ - try: - f = open(filename, "rb") - tree = parse(f) - self.xml_root = tree.getroot() - except: - f.close() - self.xml_root = None - """ - self.mapFile = mapFile f = open( mapFile ) tree = f.read() f.close() else: - f = open(dir_struct["template"] + "default_map.xml") + f = open(orpg.dirpath.dir_struct["template"] + "default_map.xml") tree = f.read() f.close() - self.game_map.init_from_xml(tree) + self.game_map.init_from_xml(fromstring(tree)) def save_map(self): + debug() if self.mapFile is not None and self.persistant == 1 and self.mapFile.find("default_map.xml") == -1: f = open(self.mapFile, "w") f.write(self.game_map.get_all_xml()) f.close() def add_player(self,id): + debug() self.players.append(id) def remove_player(self,id): + debug() if self.voice.has_key(id): del self.voice[id] self.players.remove(id) def get_num_players(self): + debug() num = len(self.players) return num def get_player_ids(self): + debug() tmp = self.players return tmp def check_pwd(self,pwd): + debug() return (pwd==self.pwd) def check_boot_pwd(self,pwd): return (pwd==self.boot_pwd) def check_version(self,ver): + debug() if (self.minVersion == ""): return 1 minVersion=self.minVersion.split('.') version=ver.split('.') @@ -157,6 +153,7 @@ #depreciated - see send_group_list() def toxml(self, act="new"): + debug(self.name) # Please don't add the boot_pwd to the xml, as this will give it away to players watching their console el = Element('group') el.set('id', self.id) @@ -165,15 +162,9 @@ el.set('players', str(self.get_num_players())) el.set('action', act) return tostring(el) - #xml_data = "<group id=\"" + self.id - #xml_data += "\" name=\"" + self.name - #xml_data += "\" pwd=\"" + str(self.pwd!="") - #xml_data += "\" players=\"" + str(self.get_num_players()) - #xml_data += "\" action=\"" + act + "\" />" - #return xml_data - class client_stub(client_base): + debug() def __init__(self,inbox,sock,props,log): client_base.__init__(self) self.ip = props['ip'] @@ -205,6 +196,7 @@ else: return 0 def send(self, msg, player, group): + debug() if self.get_status() == MPLAY_CONNECTED: #el = Element('msg') #el.set('to', player) @@ -213,7 +205,8 @@ #el.text(msg) self.outbox.put("<msg to='" + player + "' from='0' group_id='" + group + "' />" + msg) - def change_group(self,group_id,groups): + def change_group(self, group_id, groups): + debug() old_group_id = str(self.group_id) groups[group_id].add_player(self.id) groups[old_group_id].remove_player(self.id) @@ -224,11 +217,11 @@ return old_group_id def self_message(self,act): - self.send(act,self.id,self.group_id) + self.send(act, self.id, self.group_id) def take_dom(self,xml_dom): - self.name = xml_dom.getAttribute("name") - self.text_status = xml_dom.getAttribute("status") + self.name = xml_dom.get("name") + self.text_status = xml_dom.get("status") """ ###################################################################### @@ -243,7 +236,10 @@ """ class mplay_server: + debug() def __init__(self, log_console=None, name=None): + logger._set_log_level = 16 + logger._set_log_to_console(True) self.log_to_console = 1 self.log_console = log_console self.alive = 1 @@ -278,6 +274,7 @@ self.sendLobbySound = False self.lobbySound = 'http://www.digitalxero.net/music/mus_tavern1.bmu' ##used? + @debugging def initServer(self, **kwargs): for atter, value in kwargs.iteritems(): setattr(self, atter, value) validate.config_file( self.lobbyMapFile, "default_Lobby_map.xml" ) @@ -335,13 +332,13 @@ # try to use it. try: - self.banDom = minidom.parse(self.userPath + 'ban_list.xml') - self.banDom.normalize() - self.banDoc = self.banDom.documentElement + self.banDom = parse(self.userPath + 'ban_list.xml') + #self.banDom.normalize() + self.banDoc = self.banDom.getroot() - for element in self.banDom.getElementsByTagName('banned'): - playerName = element.getAttribute( 'name' ).replace("&", "&").replace("<", "<").replace('"', """).replace(">", ">") - playerIP = element.getAttribute('ip') + for element in self.banDom.findall('banned'): + playerName = element.get('name').replace("&", "&").replace("<", "<").replace('"', """).replace(">", ">") + playerIP = element.get('ip') self.ban_list[playerIP] = {} self.ban_list[playerIP]['ip'] = playerIP self.ban_list[playerIP]['name'] = playerName @@ -356,13 +353,14 @@ # try to use it. try: - data = [] - data.append("<server>\n") + etreeEl = Element('server') for ip in self.ban_list: - data.append(' <banned name="' + str(self.ban_list[ip]['name'].replace("&", "&").replace("<", "<").replace(""", '"').replace(">", ">")) + '" ip="' + str(self.ban_list[ip]['ip']) + '" />' + "\n") - data.append("</server>") + el = Element('banned') + el.set('name', str(self.ban_list[ip]['name'].replace("&", "&").replace("<", "<").replace(""", '"').replace(">", ">"))) + el.set('ip', str(self.ban_list[ip]['ip'])) + etreeEl.append(el) file = open(self.userPath + self.banFile ,"w") - file.write("".join(data)) + file.write(tostring(etreeEl)) file.close() except Exception, e: self.log_msg("Exception in saveBanList() " + str(e)) @@ -375,31 +373,27 @@ validate.config_file( "server_ini.xml", "default_server_ini.xml" ) # try to use it. try: - self.configDom = minidom.parse(self.userPath + 'server_ini.xml') - self.configDom.normalize() - self.configDoc = self.configDom.documentElement - + self.configDom = parse(self.userPath + 'server_ini.xml') + #self.configDom.normalize() + self.configDoc = self.configDom.getroot() if hasattr(self, 'bootPassword'): self.boot_pwd = self.bootPassword - else: - if self.configDoc.hasAttribute("admin"): self.boot_pwd = self.configDoc.getAttribute("admin") - elif self.configDoc.hasAttribute("boot"): self.boot_pwd = self.configDoc.getAttribute("boot") - + if self.configDoc.get("admin"): self.boot_pwd = self.configDoc.get("admin") + elif self.configDoc.get("boot"): self.boot_pwd = self.configDoc.get("boot") if len(self.boot_pwd) < 1: self.boot_pwd = raw_input("Enter admin password: ") - - if not hasattr(self, 'reg') and self.configDoc.hasAttribute("register"): - self.reg = self.configDoc.getAttribute("register") + if not hasattr(self, 'reg') and self.configDoc.get("register"): + self.reg = self.configDoc.get("register") if not len(self.reg) > 0 or self.reg[0].upper() not in ("Y", "N"): opt = raw_input("Do you want to post your server to the OpenRPG Meta Server list? (y,n) ") if len(opt) and (opt[0].upper() == 'Y'): self.reg = 'Y' else: self.reg = 'N' LobbyName = 'Lobby' - if self.configDoc.hasAttribute("lobbyname"): LobbyName = self.configDoc.getAttribute("lobbyname") - map_node = service_node = self.configDoc.getElementsByTagName("map")[0] - msg_node = service_node = self.configDoc.getElementsByTagName("message")[0] - mapFile = map_node.getAttribute('file') - msgFile = msg_node.getAttribute('file') + if self.configDoc.get("lobbyname"): LobbyName = self.configDoc.get("lobbyname") + map_node = service_node = self.configDoc.findall("map")[0] + msg_node = service_node = self.configDoc.findall("message")[0] + mapFile = map_node.get('file') + msgFile = msg_node.get('file') if mapFile == '': mapFile = 'Lobby_map.xml' if msgFile == '': msgFile = 'LobbyMessage.html' # Update the lobby with the passwords if they've been specified @@ -411,15 +405,15 @@ } # set ip or dns name to send to meta server - service_node = self.configDoc.getElementsByTagName("service")[0] - address = service_node.getAttribute("address") + service_node = self.configDoc.findall("service")[0] + address = service_node.get("address") address = address.lower() if address == "" or address == "hostname/address" or address == "localhost": self.server_address = None else: self.server_address = address self.server_port = OPENRPG_PORT - if service_node.hasAttribute("port"): self.server_port = int(service_node.getAttribute("port")) - if self.configDoc.hasAttribute("name") and len(self.configDoc.getAttribute("name")) > 0 : - self.name = self.configDoc.getAttribute("name") + if service_node.get("port"): self.server_port = int(service_node.get("port")) + if self.configDoc.get("name") and len(self.configDoc.get("name")) > 0 : + self.name = self.configDoc.get("name") else: if self.reg[0].upper() == "Y": if self.name == None: self.name = raw_input("Server Name? ") @@ -433,8 +427,8 @@ """ try: - mver = self.configDoc.getElementsByTagName("version")[0] - self.minClientVersion = mver.getAttribute("min") + mver = self.configDoc.findall("version")[0] + self.minClientVersion = mver.get("min") except: self.minClientVersion = SERVER_MIN_CLIENT_VERSION #from orpg/orpg_version.py self.defaultMessageFile = "" # This try/except bit is to allow older versions of python to continue without a list error. @@ -450,13 +444,13 @@ """ try: - ak = self.configDoc.getElementsByTagName("autokick")[0] - if ak.hasAttribute("silent"): - if ((ak.getAttribute("silent")).lower() == "yes"): self.silent_auto_kick = 1 + ak = self.configDoc.findall("autokick")[0] + if ak.get("silent"): + if ((ak.get("silent")).lower() == "yes"): self.silent_auto_kick = 1 else: self.silent_auto_kick = 0 - if ak.hasAttribute("delay"): + if ak.get("delay"): try: - delay = int(ak.getAttribute("delay")) + delay = int(ak.get("delay")) self.zombie_time = delay except: #delay value cannot be converted into an int use defaut @@ -493,28 +487,28 @@ #pull information from config file DOM try: - roomdefaults = self.configDom.getElementsByTagName("room_defaults")[0] + roomdefaults = self.configDom.findall("room_defaults")[0] #rd.normalize() #roomdefaults = self.rd.documentElement try: - setting = roomdefaults.getElementsByTagName('passwords')[0] - rpw = setting.getAttribute('allow') + setting = roomdefaults.findall('passwords')[0] + rpw = setting.get('allow') if rpw == "no" or rpw == "0": roomdefault_pass = 0 self.log_msg("Room Defaults: Disallowing Passworded Rooms") else: self.log_msg("Room Defaults: Allowing Passworded Rooms") except: self.log_msg("Room Defaults: [Warning] Allowing Passworded Rooms") try: - setting = roomdefaults.getElementsByTagName('map')[0] - map = setting.getAttribute('file') + setting = roomdefaults.findall('map')[0] + map = setting.get('file') if map != "": roomdefault_map = self.userPath + map.replace("myfiles/", "") self.log_msg("Room Defaults: Using " + str(map) + " for room map") except: self.log_msg("Room Defaults: [Warning] Using Default Map") try: - setting = roomdefaults.getElementsByTagName('message')[0] - msg = setting.getAttribute('file') + setting = roomdefaults.findall('message')[0] + msg = setting.get('file') if msg != "": if msg[:4].lower() == 'http': roomdefault_msg = msg else: roomdefault_msg = self.userPath + msg.replace("myfiles/", "") @@ -539,16 +533,16 @@ ###Server Cheat message try: - cheat_node = self.configDoc.getElementsByTagName("cheat")[0] - self.cheat_msg = cheat_node.getAttribute("text") + cheat_node = self.configDoc.findall("cheat")[0] + self.cheat_msg = cheat_node.get("text") except: self.cheat_msg = "**FAKE ROLL**" self.log_msg("**WARNING** <cheat txt=\"\"> tag missing from server configuration file. Using empty string.") # should validate protocal - validate_protocol_node = self.configDom.getElementsByTagName("validate_protocol ") + validate_protocol_node = self.configDom.findall("validate_protocol") self.validate_protocol = 1 - if(validate_protocol_node): self.validate_protocol = (validate_protocol_node[0].getAttribute("value") == "True") + if(validate_protocol_node): self.validate_protocol = (validate_protocol_node[0].get("value") == "True") if(self.validate_protocol != 1): self.log_msg("Protocol Validation: OFF") self.makePersistentRooms() self.log_msg("Server Configuration File: Processing Completed.") @@ -558,28 +552,28 @@ def makePersistentRooms(self): 'Creates rooms on the server as defined in the server config file.' - for element in self.configDom.getElementsByTagName('room'): - roomName = element.getAttribute('name') - roomPassword = element.getAttribute('password') - bootPassword = element.getAttribute('boot') + for element in self.configDom.findall('room'): + roomName = element.get('name') + roomPassword = element.get('password') + bootPassword = element.get('boot') # Conditionally check for minVersion attribute - if element.hasAttribute('minVersion'): minVersion = element.getAttribute('minVersion') + if element.get('minVersion'): minVersion = element.get('minVersion') else: minVersion = "" # Extract the map filename attribute from the map node # we only care about the first map element found -- others are ignored - mapElement = element.getElementsByTagName('map')[0] - mapFile = self.userPath + mapElement.getAttribute('file').replace("myfiles/", "") - messageElement = element.getElementsByTagName('message')[0] - messageFile = messageElement.getAttribute('file') + mapElement = element.findall('map')[0] + mapFile = self.userPath + mapElement.get('file').replace("myfiles/", "") + messageElement = element.findall('message')[0] + messageFile = messageElement.get('file') if messageFile[:4] != 'http': messageFile = self.userPath + messageFile.replace("myfiles/", "") # Make sure we have a message to even mess with if(len(messageFile) == 0): messageFile = self.defaultMessageFile if(len(mapFile) == 0): mapFile = self.defaultMapFile moderated = 0 - if element.hasAttribute('moderated') and element.getAttribute('moderated').lower() == "true": moderated = 1 + if element.get('moderated') and element.get('moderated').lower() == "true": moderated = 1 #create the new persistant group self.new_group(roomName, roomPassword, @@ -639,7 +633,7 @@ elif self.log_network_messages == 2: return "Network Traffic Log: Logging (inbound/outbound files)" else: self.log_msg("Network Traffic Log: [Unknown]") - def register_callback(instance, xml_dom = None,source=None): + def register_callback(instance, xml_dom = None, source=None): if xml_dom: # if we get something if source == getMetaServerBaseURL(): # if the source of this DOM is the authoritative meta try: @@ -776,21 +770,21 @@ data = ServerPlugins.preParseOutgoing() for msg in data: try: - xml_dom = parseXml(msg) - xml_dom = xml_dom._get_documentElement() - if xml_dom.hasAttribute('from') and int(xml_dom.getAttribute('from')) > -1: - xml_dom.setAttribute('from', '-1') - xml_dom.setAttribute('to', 'all') + #xml_dom = parseXml(msg) + xml_dom = fromstring(msg).getroot() + if xml_dom.get('from') and int(xml_dom.get('from')) > -1: + xml_dom.set('from', '-1') + xml_dom.set('to', 'all') self.incoming_msg_handler(xml_dom, msg) - xml_dom.unlink() + #xml_dom.unlink() except: pass self.p_lock.release() time.sleep(0.250) def sendMsg( self, sock, msg, useCompression=False, cmpType=None): - """Very simple function that will properly encode and send a message to te + """Very simple function that will properly encode and send a message to the remote on the specified socket.""" - if useCompression and cmpType != None: + if not useCompression and cmpType != None: mpacket = cmpType.compress(msg) lpacket = pack('!i', len(mpacket)) sock.send(lpacket) @@ -1074,13 +1068,13 @@ self.log_msg(str(e)) self.p_lock.release() - def update_request(self,newsock,xml_dom): + def update_request(self,newsock, xml_dom): # handle reconnects self.log_msg( "update_request() has been called." ) # get player id - id = xml_dom.getAttribute("id") - group_id = xml_dom.getAttribute("group_id") + id = xml_dom.get("id") + group_id = xml_dom.get("group_id") self.p_lock.acquire() if self.players.has_key(id): self.sendMsg(newsock, self.players[id].toxml("update"), @@ -1091,33 +1085,31 @@ need_new = 0 else: need_new = 1 self.p_lock.release() - if need_new: self.new_request(newsock,xml_dom) + if need_new: self.new_request(newsock, xml_dom) else: msg = self.groups[group_id].game_map.get_all_xml() self.send(msg,id,group_id) - def new_request(self,newsock,xml_dom,LOBBY_ID='0'): + def new_request(self,newsock, xml_dom, LOBBY_ID='0'): #build client stub props = {} # Don't trust what the client tells us...trust what they connected as! props['ip'] = socket.gethostbyname( newsock.getpeername()[0] ) - try: props['role'] = xml_dom.getAttribute("role") - except: props['role'] = "GM" - - props['name'] = xml_dom.getAttribute("name") + props['role'] = xml_dom.get("role") or 'GM' + props['name'] = xml_dom.get("name") props['group_id'] = LOBBY_ID props['id'] = str(self.next_player_id) - props['version'] = xml_dom.getAttribute("version") - props['protocol_version'] = xml_dom.getAttribute("protocol_version") - props['client_string'] = xml_dom.getAttribute("client_string") + props['version'] = xml_dom.get("version") + props['protocol_version'] = xml_dom.get("protocol_version") + props['client_string'] = xml_dom.get("client_string") self.next_player_id += 1 - new_stub = client_stub(self.incoming,newsock,props,self.log_console) - if xml_dom.hasAttribute('useCompression'): + new_stub = client_stub(self.incoming, newsock, props, self.log_console) + if xml_dom.get('useCompression'): new_stub.useCompression = True - if xml_dom.hasAttribute('cmpType'): - cmpType = xml_dom.getAttribute('cmpType') + if xml_dom.get('cmpType'): + cmpType = xml_dom.get('cmpType') if cmpBZ2 and cmpType == 'bz2': new_stub.compressionType = bz2 elif cmpZLIB and cmpType == 'zlib': new_stub.compressionType = zlib else: new_stub.compressionType = None @@ -1129,14 +1121,14 @@ self.sendMsg(newsock, new_stub.toxml("new"), False, None) # try to remove circular refs - if xml_dom: - xml_dom.unlink() + #if xml_dom: + # xml_dom.unlink() # send confirmation data = self.recvMsg(newsock, new_stub.useCompression, new_stub.compressionType) try: - xml_dom = parseXml(data) - xml_dom = xml_dom._get_documentElement() + xml_dom = XML(data) + #xml_dom = xml_dom._get_documentElement() except Exception, e: print e (remote_host,remote_port) = newsock.getpeername() @@ -1151,7 +1143,7 @@ print "Error in parse found from " + str(remote_host) + ". Disconnected." print " Offending data(" + str(len(data)) + "bytes)=" + data print "Exception=" + str(e) - if xml_dom: xml_dom.unlink() + #if xml_dom: xml_dom.unlink() return #start threads and store player @@ -1180,8 +1172,7 @@ time.sleep(1) self.log_msg("Connection terminating due to version incompatibility with client (ver: " + props['version'] + " protocol: " + props['protocol_version'] + ")" ) newsock.close() - if xml_dom: - xml_dom.unlink() + #if xml_dom: xml_dom.unlink() return None ip = props['ip'] @@ -1194,8 +1185,7 @@ # Give messages time to flow time.sleep(1) newsock.close() - if xml_dom: - xml_dom.unlink() + #if xml_dom: xml_dom.unlink() return None """ @@ -1213,7 +1203,7 @@ """ try: - if xml_dom.getAttribute("id") == props['id']: + if xml_dom.get("id") == props['id']: new_stub.initialize_threads() self.p_lock.acquire() self.players[props['id']] = new_stub @@ -1222,12 +1212,12 @@ self.send_player_list(props['id'],LOBBY_ID) self.p_lock.release() msg = self.groups[LOBBY_ID].game_map.get_all_xml() - self.send(msg,props['id'],LOBBY_ID) - self.send_to_group(props['id'],LOBBY_ID,self.players[props['id']].toxml('new')) - self.return_room_roles(props['id'],LOBBY_ID) + self.send(msg, props['id'], LOBBY_ID) + self.send_to_group(props['id'], LOBBY_ID, self.players[props['id']].toxml('new')) + self.return_room_roles(props['id'], LOBBY_ID) # Re-initialize the role for this player incase they came from a different server - self.handle_role("set",props['id'], "GM",self.groups[LOBBY_ID].boot_pwd, LOBBY_ID) + self.handle_role("set", props['id'], "GM", self.groups[LOBBY_ID].boot_pwd, LOBBY_ID) cmsg = "Client Connect: (" + str(props['id']) + ") " + str(props['name']) + " [" + str(props['ip']) + "]" self.log_msg(cmsg) cmsg = ("connect", props) ################################################# @@ -1242,7 +1232,7 @@ #something didn't go right. Notify client and drop the connection err_string = "<center>" - err_string += "<hr><b>The server has encountered an error while processing your connection request.</b><hr>" + err_string += "<hr><b>The server has encountered an error while processing your connection request.</b><hr>" err_string += "<br /><i>You are being disconnected from the server.</i><br />" err_string += "This error may represent a problem with the server. If you continue to get this message " err_string += "please contact the servers administrator to correct the issue.</center> " @@ -1252,8 +1242,7 @@ # Display the lobby message self.SendLobbyMessage(newsock,props['id']) - if xml_dom: - xml_dom.unlink() + #if xml_dom: xml_dom.unlink() def checkClientVersion(self, clientversion): minv = self.minClientVersion.split('.') @@ -1268,6 +1257,7 @@ return 1 def SendLobbyMessage(self, socket, player_id): + debug() """ # Display the lobby message # prepend this server's version string to the the lobby message @@ -1277,10 +1267,8 @@ lobbyMsg += "server, version '" + VERSION + "'" # See if we have a server name to report! - if len(self.serverName): - lobbyMsg += ", named '" + self.serverName + "'." - else: - lobbyMsg += "." + if len(self.serverName): lobbyMsg += ", named '" + self.serverName + "'." + else: lobbyMsg += "." # Add extra line spacing lobbyMsg += "\n\n" @@ -1341,6 +1329,7 @@ self.listen_event.set() def acceptedNewConnectionThread( self, newsock, addr ): + debug() """Once a new connection comes in and is accepted, this thread starts up to handle it.""" # Initialize xml_dom xml_dom = None @@ -1362,8 +1351,7 @@ except: pass return #returning causes connection thread instance to terminate # Clear out the xml_dom in preparation for new stuff, if necessary - try: - if xml_dom: xml_dom.unlink() + """try: if xml_dom: xml_dom.unlink() except: self.log_msg( "The following exception caught unlinking xml_dom:") @@ -1371,10 +1359,10 @@ try: newsock.close() except: pass return #returning causes connection thread instance to terminate - # Parse the XML received from the connecting client + # Parse the XML received from the connecting client""" try: - xml_dom = parseXml(data) - xml_dom = xml_dom._get_documentElement() + xml_dom = XML(data) + #xml_dom = xml_dom._get_documentElement() except: try: newsock.close() @@ -1388,11 +1376,11 @@ # Determine the correct action and execute it try: # get action - action = xml_dom.getAttribute("action") + action = xml_dom.get("action") # Figure out what type of connection we have going on now - if action == "new": self.new_request(newsock,xml_dom) - elif action == "update": self.update_request(newsock,xml_dom) + if action == "new": self.new_request(newsock, xml_dom) + elif action == "update": self.update_request(newsock, xml_dom) else: self.log_msg("Unknown Join Request!") except Exception, e: @@ -1421,7 +1409,8 @@ # and player load capacity -- Snowdog 3/04 """ - def message_handler(self,arg): + def message_handler(self, arg): + debug() xml_dom = None self.log_msg( "message handler thread running..." ) while self.alive: @@ -1441,40 +1430,42 @@ data = None except Exception, e: self.log_msg(str(e)) - if xml_dom: xml_dom.unlink() - if xml_dom: xml_dom.unlink() + #if xml_dom: xml_dom.unlink() + #if xml_dom: xml_dom.unlink() self.log_msg("message handler thread exiting...") self.incoming_event.set() def parse_incoming_dom(self,data): + debug(data) end = data.find(">") #locate end of first element of message head = data[:end+1] #self.log_msg(head) xml_dom = None try: - xml_dom = parseXml(head) - xml_dom = xml_dom._get_documentElement() - self.message_action(xml_dom,data) + xml_dom = XML(head) + #xml_dom = xml_dom._get_documentElement() + self.message_action(xml_dom, data) except Exception, e: print "Error in parse of inbound message. Ignoring message." print " Offending data(" + str(len(data)) + "bytes)=" + data print "Exception=" + str(e) - if xml_dom: xml_dom.unlink() - + def message_action(self, xml_dom, data): - tag_name = xml_dom._get_tagName() - if self.svrcmds.has_key(tag_name): self.svrcmds[tag_name]['function'](xml_dom,data) + debug() + tag_name = xml_dom.tag; print 'message_action tag_name', tag_name + if self.svrcmds.has_key(tag_name): self.svrcmds[tag_name]['function'](xml_dom, data) else: raise Exception, "Not a valid header!" #Message Action thread expires and closes here. return def do_alter(self, xml_dom, data): - target = xml_dom.getAttribute("key") - value = xml_dom.getAttribute("val") - player = xml_dom.getAttribute("plr") - group_id = xml_dom.getAttribute("gid") - boot_pwd = xml_dom.getAttribute("bpw") + debug() + target = xml_dom.get("key") + value = xml_dom.get("val") + player = xml_dom.get("plr") + group_id = xml_dom.get("gid") + boot_pwd = xml_dom.get("bpw") actual_boot_pwd = self.groups[group_id].boot_pwd if self.allow_room_passwords == 0: @@ -1501,41 +1492,44 @@ self.players[player].outbox.put(msg) def do_role(self, xml_dom, data): + debug() role = "" boot_pwd = "" - act = xml_dom.getAttribute("action") - player = xml_dom.getAttribute("player") - group_id = xml_dom.getAttribute("group_id") + act = xml_dom.get("action") + player = xml_dom.get("player") + group_id = xml_dom.get("group_id") if act == "set": - role = xml_dom.getAttribute("role") - boot_pwd = xml_dom.getAttribute("boot_pwd") - xml_dom.unlink() + role = xml_dom.get("role") + boot_pwd = xml_dom.get("boot_pwd") + #xml_dom.unlink() if group_id != "0": self.handle_role(act, player, role, boot_pwd, group_id) self.log_msg(("role", (player, role))) def do_ping(self, xml_dom, data): - player = xml_dom.getAttribute("player") - group_id = xml_dom.getAttribute("group_id") + debug() + player = xml_dom.get("player") + group_id = xml_dom.get("group_id") sent_time = "" msg = "" - try: sent_time = xml_dom.getAttribute("time") + try: sent_time = xml_dom.get("time") except: pass if sent_time != "": msg ="<ping time='" + str(sent_time) + "' />" #because a time was sent return a ping response else: msg ="<msg to='" + player + "' from='" + player + "' group_id='" + group_id + "'>" msg += "<font color='#FF0000'>PONG!?!</font>" self.players[player].outbox.put(msg) - xml_dom.unlink() + #xml_dom.unlink() def do_system(self, xml_dom, data): + debug() pass def moderate_group(self,xml_dom,data): try: - action = xml_dom.getAttribute("action") - from_id = xml_dom.getAttribute("from") - if xml_dom.hasAttribute("pwd"): pwd=xml_dom.getAttribute("pwd") + action = xml_dom.get("action") + from_id = xml_dom.get("from") + if xml_dom.get("pwd"): pwd=xml_dom.get("pwd") else: pwd="" group_id=self.players[from_id].group_id if action == "list": @@ -1561,10 +1555,10 @@ self.groups[group_id].moderated = 0 self.players[from_id].self_message("This channel is now unmoderated") if action == 'addvoice': - users = xml_dom.getAttribute("users").split(',') + users = xml_dom.get("users").split(',') for i in users: self.groups[group_id].voice[i.strip()]=1 if action == 'delvoice': - users = xml_dom.getAttribute("users").split(',') + users = xml_dom.get("users").split(',') for i in users: if self.groups[group_id].voice.has_key(i.strip()): del self.groups[group_id].voice[i.strip()] else: @@ -1572,11 +1566,11 @@ except Exception,e: self.log_msg(str(e)) - def join_group(self,xml_dom,data): + def join_group(self, xml_dom, data): try: - from_id = xml_dom.getAttribute("from") - pwd = xml_dom.getAttribute("pwd") - group_id = xml_dom.getAttribute("group_id") + from_id = xml_dom.get("from") + pwd = xml_dom.get("pwd") + group_id = xml_dom.get("group_id") ver = self.players[from_id].version allowed = 1 @@ -1588,7 +1582,7 @@ allowed = 0 #tell the clients password manager the password failed -- SD 8/03 - pm = "<password signal=\"fail\" type=\"room\" id=\"" + group_id + "\" data=\"\"/>" + pm = "<password signal='fail' type='room' id='" + group_id + "' data=''/>" self.players[from_id].outbox.put(pm) msg = 'failed - incorrect room password' @@ -1597,7 +1591,7 @@ #the following line makes sure that their role is reset to normal, #since it is briefly set to lurker when they even TRY to change #rooms - msg = "<role action=\"update\" id=\"" + from_id + "\" role=\"" + self.players[from_id].role + "\" />" + msg = "<role action='update' id='" + from_id + "' role='" + self.players[from_id].role + "' />" self.players[from_id].outbox.put(msg) return @@ -1618,10 +1612,8 @@ "move a player from one group to another" try: try: - if group_id == "0": - self.players[from_id].role = "GM" - else: - self.players[from_id].role = "Lurker" + if group_id == "0": self.players[from_id].role = "GM" + else: self.players[from_id].role = "Lurker" except Exception, e: print "exception in move_player() " traceback.print_exc() @@ -1669,10 +1661,12 @@ self.log_msg(str(e)) thread.start_new_thread(self.registerRooms,(0,)) - def return_room_roles(self,from_id,group_id): + def return_room_roles(self, from_id, group_id): for m in self.players.keys(): if self.players[m].group_id == group_id: - msg = "<role action=\"update\" id=\"" + self.players[m].id + "\" role=\"" + self.players[m].role + "\" />" + print 'return_room_roles', self.players[m].id, self.players[m].role, self.players[m] + try: msg = "<role action='update' id='" + self.players[m].id + "' role='" + self.players[m].role + "' />" + except: exit() self.players[from_id].outbox.put(msg) """ @@ -1734,78 +1728,79 @@ except: return "An error occured during rename of room!" thread.start_new_thread(self.registerRooms,(0,)) - def create_group(self,xml_dom,data): - try: - from_id = xml_dom.getAttribute("from") - pwd = xml_dom.getAttribute("pwd") - name = xml_dom.getAttribute("name") - boot_pwd = xml_dom.getAttribute("boot_pwd") - minVersion = xml_dom.getAttribute("min_version") - #added var reassign -- akoman - messageFile = self.defaultMessageFile + def create_group(self, xml_dom, data): + debug((tostring(xml_dom), data)) + #try: + from_id = xml_dom.get("from") + pwd = xml_dom.get("pwd") + name = xml_dom.get("name") + boot_pwd = xml_dom.get("boot_pwd") + minVersion = xml_dom.get("min_version") + #added var reassign -- akoman + messageFile = self.defaultMessageFile - # see if passwords are allowed on this server and null password if not - if self.allow_room_passwords != 1: pwd = "" + # see if passwords are allowed on this server and null password if not + if self.allow_room_passwords != 1: pwd = "" - # Check for & in name. We want to allow this because of its common - # use in d&d games. + # Check for & in name. We want to allow this because of its common + # use in d&d games. - loc = name.find("&") - oldloc = 0 - while loc > -1: - loc = name.find("&",oldloc) - if loc > -1: - b = name[:loc] - e = name[loc+1:] - name = b + "&" + e - oldloc = loc+1 - loc = name.find("'") - oldloc = 0 - while loc > -1: - loc = name.find("'",oldloc) - if loc > -1: - b = name[:loc] - e = name[loc+1:] - name = b + "'" + e - oldloc = loc+1 - loc = name.find('"') - oldloc = 0 - while loc > -1: - loc = name.find('"',oldloc) - if loc > -1: - b = name[:loc] - e = name[loc+1:] - name = b + """ + e - oldloc = loc+1 - group_id = str(self.next_group_id) - self.next_group_id += 1 - self.groups[group_id] = game_group(group_id,name,pwd,"",boot_pwd, minVersion, None, messageFile ) - self.groups[group_id].voice[from_id]=1 - self.players[from_id].outbox.put(self.groups[group_id].toxml('new')) - old_group_id = self.players[from_id].change_group(group_id,self.groups) - self.send_to_group(from_id,old_group_id,self.players[from_id].toxml('del')) - self.check_group(from_id, old_group_id) - self.send_to_all(from_id,self.groups[group_id].toxml('new')) - self.send_to_all('0',self.groups[group_id].toxml('update')) - self.handle_role("set",from_id,"GM",boot_pwd, group_id) - lmsg = "Creating Group... (" + str(group_id) + ") " + str(name) - self.log_msg( lmsg ) - jmsg = "moving to room " + str(group_id) + "." - self.log_msg( jmsg ) - self.log_msg(("create_group", (str(name), group_id, from_id))) - #even creators of the room should see the HTML --akoman - #edit: jan10/03 - was placed in the except statement. Silly me. - if self.defaultMessageFile != None: - if self.defaultMessageFile[:4] == 'http': - data = urllib.urlretrieve(self.defaultMessageFile) - open_msg = open(data[0]) - urllib.urlcleanup() - else: open_msg = open( self.defaultMessageFile, "r" ) - roomMsg = open_msg.read() - open_msg.close() - # Send the rooms message to the client no matter what - self.players[from_id].outbox.put( "<msg to='" + from_id + "' from='0' group_id='" + group_id + "' />" + roomMsg ) - except Exception, e: self.log_msg( "Exception: create_group(): " + str(e)) + loc = name.find("&") + oldloc = 0 + while loc > -1: + loc = name.find("&",oldloc) + if loc > -1: + b = name[:loc] + e = name[loc+1:] + name = b + "&" + e + oldloc = loc+1 + loc = name.find("'") + oldloc = 0 + while loc > -1: + loc = name.find("'",oldloc) + if loc > -1: + b = name[:loc] + e = name[loc+1:] + name = b + "'" + e + oldloc = loc+1 + loc = name.find('"') + oldloc = 0 + while loc > -1: + loc = name.find('"',oldloc) + if loc > -1: + b = name[:loc] + e = name[loc+1:] + name = b + """ + e + oldloc = loc+1 + group_id = str(self.next_group_id) + self.next_group_id += 1 + self.groups[group_id] = game_group(group_id, name, pwd, "", boot_pwd, minVersion, None, messageFile) + self.groups[group_id].voice[from_id]=1 + self.players[from_id].outbox.put(self.groups[group_id].toxml('new')) + old_group_id = self.players[from_id].change_group(group_id,self.groups) + self.send_to_group(from_id,old_group_id,self.players[from_id].toxml('del')) + self.check_group(from_id, old_group_id) + self.send_to_all(from_id,self.groups[group_id].toxml('new')) + self.send_to_all('0',self.groups[group_id].toxml('update')) + self.handle_role("set",from_id,"GM",boot_pwd, group_id) + lmsg = "Creating Group... (" + str(group_id) + ") " + str(name) + self.log_msg( lmsg ) + jmsg = "moving to room " + str(group_id) + "." + self.log_msg( jmsg ) + self.log_msg(("create_group", (str(name), group_id, from_id))) + #even creators of the room should see the HTML --akoman + #edit: jan10/03 - was placed in the except statement. Silly me. + if self.defaultMessageFile != None: + if self.defaultMessageFile[:4] == 'http': + data = urllib.urlretrieve(self.defaultMessageFile) + open_msg = open(data[0]) + urllib.urlcleanup() + else: open_msg = open( self.defaultMessageFile, "r" ) + roomMsg = open_msg.read() + open_msg.close() + # Send the rooms message to the client no matter what + self.players[from_id].outbox.put( "<msg to='" + from_id + "' from='0' group_id='" + group_id + "' />" + roomMsg ) + #except Exception, e: self.log_msg( "Exception: create_group(): " + str(e)) thread.start_new_thread(self.registerRooms,(0,)) def check_group(self, from_id, group_id): @@ -1825,7 +1820,7 @@ except Exception, e: self.log_msg(str(e)) - def del_player(self,id,group_id): + def del_player(self, id, group_id): try: dmsg = "Client Disconnect: (" + str(id) + ") " + str(self.players[id].name) self.players[id].disconnect() @@ -1844,10 +1839,11 @@ except Exception, e: self.log_msg(str(e)) self.log_msg("Explicit garbage collection shows %s undeletable items." % str(gc.collect())) - def incoming_player_handler(self,xml_dom,data): - id = xml_dom.getAttribute("id") - act = xml_dom.getAttribute("action") - #group_id = xml_dom.getAttribute("group_id") + def incoming_player_handler(self, xml_dom, data): + debug() + id = xml_dom.get("id") + act = xml_dom.get("action") + #group_id = xml_dom.get("group_id") group_id = self.players[id].group_id ip = self.players[id].ip self.log_msg("Player with IP: " + str(ip) + " joined.") @@ -1859,21 +1855,22 @@ self.send_group_list(id) except Exception, e: traceback.print_exc() elif act=="del": - #print "del player" self.del_player(id,group_id) self.check_group(id, group_id) elif act=="update": + debug(xml_dom) + print xml_dom.get('name') self.players[id].take_dom(xml_dom) self.log_msg(("update", {"id": id, - "name": xml_dom.getAttribute("name"), - "status": xml_dom.getAttribute("status"), - "role": xml_dom.getAttribute("role"), + "name": xml_dom.get("name"), + "status": xml_dom.get("status"), + "role": xml_dom.get("role"), "ip": str(ip), - "group": xml_dom.getAttribute("group_id"), - "room": xml_dom.getAttribute("name"), - "boot": xml_dom.getAttribute("rm_boot"), - "version": xml_dom.getAttribute("version"), - "ping": xml_dom.getAttribute("time") \ + "group": xml_dom.get("group_id"), + "room": xml_dom.get("name"), + "boot": xml_dom.get("rm_boot"), + "version": xml_dom.get("version"), + "ping": xml_dom.get("time") \ })) def strip_cheat_roll(self, string): @@ -1895,10 +1892,12 @@ return False def incoming_msg_handler(self,xml_dom,data): + debug() xml_dom, data = ServerPlugins.preParseIncoming(xml_dom, data) - to_id = xml_dom.getAttribute("to") - from_id = xml_dom.getAttribute("from") - group_id = xml_dom.getAttribute("group_id") + ########################################################### + to_id = xml_dom.get("to") + from_id = xml_dom.get("from") + group_id = xml_dom.get("group_id") end = data.find(">") msg = data[end+1:] @@ -1943,14 +1942,15 @@ return def sound_msg_handler(self, xml_dom, data): - from_id = xml_dom.getAttribute("from") - group_id = xml_dom.getAttribute("group_id") + debug() + from_id = xml_dom.get("from") + group_id = xml_dom.get("group_id") if group_id != 0: self.send_to_group(from_id, group_id, data) def plugin_msg_handler(self,xml_dom,data): - to_id = xml_dom.getAttribute("to") - from_id = xml_dom.getAttribute("from") - group_id = xml_dom.getAttribute("group_id") + to_id = xml_dom.get("to") + from_id = xml_dom.get("from") + group_id = xml_dom.get("group_id") end = data.find(">") msg = data[end+1:] @@ -2002,9 +2002,9 @@ try: given_boot_pwd = None try: - xml_dom = parseXml(msg) - xml_dom = xml_dom._get_documentElement() - given_boot_pwd = xml_dom.getAttribute("boot_pwd") + xml_dom = XML(msg) + #xml_dom = xml_dom._get_documentElement() + given_boot_pwd = xml_dom.get("boot_pwd") except: print "Error in parse of boot message, Ignoring." @@ -2102,14 +2102,14 @@ ### Alpha ### Addition added to assist in Un Banning users. def admin_build_banlist(self): validate.config_file("ban_list.xml", "default_ban_list.xml" ) - configDom = minidom.parse(dir_struct["user"] + 'ban_list.xml') + configDom = parse(dir_struct["user"] + 'ban_list.xml') self.ban_list = {} - for element in configDom.getElementsByTagName('banned'): - player = element.getAttribute('name').replace("&", "&").replace("<", "<").replace('"', """).replace(">", ">") - ip = element.getAttribute('ip') + for element in configDom.findall('banned'): + player = element.get('name').replace("&", "&").replace("<", "<").replace('"', """).replace(">", ">") + ip = element.get('ip') self.ban_list[ip] = {} self.ban_list[ip]['ip'] = ip - self.ban_list[ip]['name'] = element.getAttribute('name') + self.ban_list[ip]['name'] = element.get('name') ################ def admin_banip(self, ip, name="", silent = 0): @@ -2245,8 +2245,10 @@ def send_group_list(self, to_id, action="new"): try: + print self.groups for key in self.groups: xml = self.groups[key].toxml(action) + print xml, key self.players[to_id].outbox.put(xml) except Exception, e: self.log_msg("Exception: send_group_list(): (client #"+to_id+") : " + str(e)) @@ -2303,10 +2305,10 @@ """ if not self.allowRemoteAdmin: return try: - pid = xml_dom.getAttribute("id") + pid = xml_dom.get("id") gid = "" - given_pwd = xml_dom.getAttribute("pwd") - cmd = xml_dom.getAttribute("cmd") + given_pwd = xml_dom.get("pwd") + cmd = xml_dom.get("cmd") server_admin_pwd = self.groups["0"].boot_pwd p_id = "" p_name= "" @@ -2345,24 +2347,24 @@ msg ="<msg to='" + pid + "' from='0' group_id='" + gid + "'>" + self.player_list_remote() self.players[pid].outbox.put(msg) elif cmd == "banip": - ip = xml_dom.getAttribute("bip") - name = xml_dom.getAttribute("bname") + ip = xml_dom.get("bip") + name = xml_dom.get("bname") msg = "<msg to='" + pid + "' from='0' group_id='" + gid + "'> Banned: " + str(ip) self.admin_banip(ip, name) elif cmd == "ban": - id = xml_dom.getAttribute("bid") + id = xml_dom.get("bid") msg = "<msg to='" + id + "' from='0' group_id='" + gid + "'> Banned!" self.players[pid].outbox.put(msg) self.admin_ban(id, "") ### Alpha ### and untested elif cmd == "boot": - id = xml_dom.getAttribute("bid") + id = xml_dom.get("bid") msg = "<msg to='" + id + "' from='0' group_id='" + gid + "'> Booted!" self.players[pid].outbox.put(msg) self.admin_kick(id, "") ############# elif cmd == "unban": - ip = xml_dom.getAttribute("ip") + ip = xml_dom.get("ip") self.admin_unban(ip) msg = "<msg to='" + pid + "' from='0' group_id='" + gid + "'> Unbaned: " + str(ip) self.players[pid].outbox.put(msg) @@ -2370,7 +2372,7 @@ msg = "<msg to='" + pid + "' from='0' group_id='" + gid + "'>" + self.admin_banlist() self.players[pid].outbox.put(msg) elif cmd == "killgroup": - ugid = xml_dom.getAttribute("gid") + ugid = xml_dom.get("gid") if ugid == "0": m = "<msg to='" + pid + "' from='0' group_id='" + gid + "'>" m += "Cannot Remove Lobby! Remote administrator request denied!" @@ -2381,15 +2383,15 @@ self.players[pid].outbox.put(msg) elif cmd == "message": - tuid = xml_dom.getAttribute("to_id") - msg = xml_dom.getAttribute("msg") + tuid = xml_dom.get("to_id") + msg = xml_dom.get("msg") pmsg = "<msg to='" + tuid + "' from='0' group_id='" + self.players[tuid].group_id + "' >" + msg try: self.players[tuid].outbox.put(pmsg) except: msg = "<msg to='" + pid + "' from='0' group_id='" + gid + ">Unknown Player ID: No message sent." self.players[pid].outbox.put(msg) elif cmd == "broadcast": - bmsg = xml_dom.getAttribute("msg") + bmsg = xml_dom.get("msg") self.broadcast(bmsg) elif cmd == "killserver" and self.allowRemoteKill: #dangerous command..once server stopped it must be restarted manually @@ -2407,21 +2409,21 @@ msg += self.RoomPasswords() self.players[pid].outbox.put( msg) elif cmd == "createroom": - rm_name = xml_dom.getAttribute("name") - rm_pass = xml_dom.getAttribute("pass") - rm_boot = xml_dom.getAttribute("boot") + rm_name = xml_dom.get("name") + rm_pass = xml_dom.get("pass") + rm_boot = xml_dom.get("boot") result = self.create_temporary_persistant_room(rm_name, rm_boot, rm_pass) msg = "<msg to='" + pid + "' from='0' group_id='" + gid + "'>" + result self.players[pid].outbox.put(msg) elif cmd == "nameroom": - rm_id = xml_dom.getAttribute("rmid") - rm_name = xml_dom.getAttribute("name") + rm_id = xml_dom.get("rmid") + rm_name = xml_dom.get("name") result = self.change_group_name(rm_id,rm_name,pid) msg ="<msg to='" + pid + "' from='0' group_id='" + gid + "'/>" + result self.players[pid].outbox.put(msg) elif cmd == "passwd": - tgid = xml_dom.getAttribute("gid") - npwd = xml_dom.getAttribute("pass") + tgid = xml_dom.get("gid") + npwd = xml_dom.get("pass") if tgid == "0": msg ="<msg to='" + pid + "' from='0' group_id='" + gid + "'>" msg += "Server password may not be changed remotely!"