def sendBuddyPermissionResponse(self, allowBuddy, targetBuddy): if allowBuddy: msgXML = XMLObj.build_from_str("<n res='g'>" + targetBuddy + "</n>") else: msgXML = XMLObj.build_from_str("<n res='r'>" + targetBuddy + "</n>") self.send(self.MESSAGE_HEADER_SYSTEM, "bPrm", -1, msgXML) return
def setRoomVariables(self, vars, roomId = None, setOwnership = True): if roomId is None: roomId = self.activeRoomId if self.checkRoomList() and self.checkJoin(): if setOwnership: xmlMsg = XMLObj.build_from_str("<vars></vars>") else: xmlMsg = XMLObj.build_from_str("<vars so='0'></vars>") for varName, varValue in vars.items(): xmlMsg += self.getXmlRoomVariable(varName, varValue) self.send(self.MESSAGE_HEADER_SYSTEM, "setRvars", roomId, xmlMsg) return
def setRoomVariables(self, vars, roomId=None, setOwnership=True): if roomId is None: roomId = self.activeRoomId if self.checkRoomList() and self.checkJoin(): if setOwnership: xmlMsg = XMLObj.build_from_str("<vars></vars>") else: xmlMsg = XMLObj.build_from_str("<vars so='0'></vars>") for varName, varValue in vars.items(): xmlMsg += self.getXmlRoomVariable(varName, varValue) self.send(self.MESSAGE_HEADER_SYSTEM, "setRvars", roomId, xmlMsg) return
def setBuddyBlockStatus(self, buddyName, status): b = self.getBuddyByName(buddyName) if b: if not b.isBlocked == status: b.setBlocked(status) if status: xmlMsg = XMLObj.build_from_str("<n x='1'>" + buddyName + "</n>") else: xmlMsg = XMLObj.build_from_str("<n x='0'>" + buddyName + "</n>") self.send(self.MESSAGE_HEADER_SYSTEM, "setB", -1, xmlMsg) evt = SFSEvent(SFSEvent.onBuddyListUpdate, {"buddy":b}) self.dispatchEvent(evt) return
def setBuddyBlockStatus(self, buddyName, status): b = self.getBuddyByName(buddyName) if b: if not b.isBlocked == status: b.setBlocked(status) if status: xmlMsg = XMLObj.build_from_str("<n x='1'>" + buddyName + "</n>") else: xmlMsg = XMLObj.build_from_str("<n x='0'>" + buddyName + "</n>") self.send(self.MESSAGE_HEADER_SYSTEM, "setB", -1, xmlMsg) evt = SFSEvent(SFSEvent.onBuddyListUpdate, {"buddy": b}) self.dispatchEvent(evt) return
def sendXtMessage(self, xtName, cmd, paramsObj, roomId=None, sendType="xml"): if roomId is None: roomId = self.activeRoomId if self.checkRoomList() is False: return if sendType == "xml": xtReq = {} xtReq["name"] = xtName xtReq["cmd"] = cmd xtReq["param"] = paramsObj xmlmsg = XMLObj.build_from_str( "<![CDATA[" + SFSObjectSerializer.serialize(xtReq) + "]]>") self.send(self.MESSAGE_HEADER_EXTENSION, "xtReq", roomId, xmlmsg) elif sendType == "json": jobj = {} jobj["x"] = xtName jobj["c"] = cmd jobj["r"] = roomId jobj["p"] = paramsObj self.sendJson(json.dumps({"t": "xt", "b": jobj})) elif sendType == "str": hdr = self.MSG_STR.join([ str(d) for d in ["xt", xtName, cmd, roomId] + paramsObj ]) + self.MSG_STR self.sendString(hdr) return
def setBuddyVariables(self, varList): xmlMsg = XMLObj.build_from_str("<vars></vars>") for vName, vValue in varList.items(): self.myBuddyVars[vName] = vValue xmlMsg += "<var n='" + vName + "'><![CDATA[" + vValue + "]]></var>" self.send(self.MESSAGE_HEADER_SYSTEM, "setBvars", -1, xmlMsg) return
def joinRoom(self, newRoom, pword = "", isSpectator = False, dontLeave = False, oldRoom = -1): if isinstance(newRoom, basestring): nr = self.getRoomByName(newRoom) if not nr: return newRoom = nr.getId() if not self.checkRoomList(): return if isSpectator: isSpec = 1 else: isSpec = 0 if self.changingRoom is False: if dontLeave: leaveCurrRoom = "0" else: leaveCurrRoom = "1" if oldRoom > -1: roomToLeave = oldRoom else: self.activeRoomId if self.activeRoomId == -1: leaveCurrRoom = "0" roomToLeave = -1 roomXML = XMLObj.build_from_str("<room/>") roomXML.set_attribute({ "id":newRoom, "pwd":pword, "spec":isSpec, "leave":leaveCurrRoom, "old":roomToLeave, }) self.send(self.MESSAGE_HEADER_SYSTEM, "joinRoom", self.activeRoomId, roomXML) self.changingRoom = True return
def sendPublicMessage(self, message, roomId = None): if roomId is None: roomId = self.activeRoomId if self.checkRoomList() and self.checkJoin(): xmlMsg = XMLObj.build_from_str("<txt><![CDATA[" + message + "]]></txt>") self.send(self.MESSAGE_HEADER_SYSTEM, "pubMsg", roomId, xmlMsg) return
def sendPrivateMessage(self, message, recipientId, roomId = None): if roomId is None: roomId = self.activeRoomId if self.checkRoomList() and self.checkJoin(): xmlMsg = XMLObj.build_from_str("<txt rcp='" + recipientId + "'><![CDATA[" + message + "]]></txt>") self.send(self.MESSAGE_HEADER_SYSTEM, "prvMsg", roomId, xmlMsg) return
def sendPublicMessage(self, message, roomId=None): if roomId is None: roomId = self.activeRoomId if self.checkRoomList() and self.checkJoin(): xmlMsg = XMLObj.build_from_str("<txt><![CDATA[" + message + "]]></txt>") self.send(self.MESSAGE_HEADER_SYSTEM, "pubMsg", roomId, xmlMsg) return
def sendModeratorMessage(self, message, mtype, id): if self.checkRoomList() and self.checkJoin(): xmlMsg = XMLObj.build_from_str("<txt t='" + mtype + "' id='" + id + "'><![CDATA[" + message + "]]></txt>") self.send(self.MESSAGE_HEADER_SYSTEM, "modMsg", self.activeRoomId, xmlMsg) return
def sendPrivateMessage(self, message, recipientId, roomId=None): if roomId is None: roomId = self.activeRoomId if self.checkRoomList() and self.checkJoin(): xmlMsg = XMLObj.build_from_str("<txt rcp='" + recipientId + "'><![CDATA[" + message + "]]></txt>") self.send(self.MESSAGE_HEADER_SYSTEM, "prvMsg", roomId, xmlMsg) return
def xmlReceived(self, xml_str): try: xml_obj = XMLObj.build_from_str(xml_str) except ExpatError: self.print_debug("[ERROR] XML Error \n\t" + xml_str) return header_id = xml_obj.xml_attr.get("t") if header_id and self.messageHandlers.has_key(header_id): handler = self.messageHandlers[header_id] handler.handleMessage(xml_obj) return
def removeBuddy(self, buddyName): found = False for index, buddy in enumerate(self.buddyList): if buddy.getName() == buddyName: del self.buddyList[index] found = True break if found: self.send(self.MESSAGE_HEADER_SYSTEM, "remB", -1, XMLObj.build_from_str("<n>" + buddyName + "</n>")) evt = SFSEvent(SFSEvent.onBuddyList, {"list":self.buddyList}) self.dispatchEvent(evt) return
def removeBuddy(self, buddyName): found = False for index, buddy in enumerate(self.buddyList): if buddy.getName() == buddyName: del self.buddyList[index] found = True break if found: self.send(self.MESSAGE_HEADER_SYSTEM, "remB", -1, XMLObj.build_from_str("<n>" + buddyName + "</n>")) evt = SFSEvent(SFSEvent.onBuddyList, {"list": self.buddyList}) self.dispatchEvent(evt) return
def handle_dataObj(self, xml_obj): body = xml_obj.body roomId = int(body.xml_attr.get("r", -1)) xmlStr = body.dataObj.get_cdata() user = body.user userId = int(user.xml_attr.get("id", -1)) sender = self.sfc.getRoom(roomId).getUser(userId) xmlObj = XMLObj.build_from_str(xmlStr) params = {} params["sender"] = sender params["obj"] = xmlObj evt = SFSEvent(SFSEvent.onObjectReceived, params) self.sfc.dispatchEvent(evt) return
def createRoom(self, name, maxUsers, roomProperties, roomId = -1): if self.checkRoomList() is False or self.checkJoin() is False: return try: isGame = "0" exitCurrentRoom = "1" joinAsSpectator = "0" if roomProperties.get("isGame", False): isGame = "1" if roomProperties.get("exitCurrentRoom"): exitCurrentRoom = "1" if roomProperties.get("joinAsSpectator"): joinAsSpectator = "1" maxSpectators = roomProperties.get("maxSpectators", "0") password = roomProperties.get("password","null") uCount = "0" if roomProperties.get("uCount"): uCount = "1" extensionName = roomProperties.get("extensionName") extensionScript = roomProperties.get("extensionScript") xmlMsg = XMLObj.build_from_str("<room></room>") xmlMsg.set_attribute({ "tmp":"1", "gam":isGame, "spec":maxSpectators, "exit":exitCurrentRoom, "jas":joinAsSpectator }) xmlMsg += "<name><![CDATA[" + name + "]]></name>" xmlMsg += "<pwd><![CDATA[" + password + "]]></pwd>" xmlMsg += "<max>" + maxUsers + "</max>" xmlMsg += "<uCnt>" + uCount + "</uCnt>" if extensionName and extensionScript: xmlMsg += "<xt n='" + extensionName xmlMsg += "' s='" + extensionScript + "' />" vars = roomProperties.get("vars") xmlMsg += "<vars></vars>" if vars: for varName, varValue in vars.items(): xmlMsg += self.getXmlRoomVariable(varName, varValue) self.send(self.MESSAGE_HEADER_SYSTEM, "createRoom", roomId, xmlMsg) except: self.print_debug("[Error] createRoom error") return
def createRoom(self, name, maxUsers, roomProperties, roomId=-1): if self.checkRoomList() is False or self.checkJoin() is False: return try: isGame = "0" exitCurrentRoom = "1" joinAsSpectator = "0" if roomProperties.get("isGame", False): isGame = "1" if roomProperties.get("exitCurrentRoom"): exitCurrentRoom = "1" if roomProperties.get("joinAsSpectator"): joinAsSpectator = "1" maxSpectators = roomProperties.get("maxSpectators", "0") password = roomProperties.get("password", "null") uCount = "0" if roomProperties.get("uCount"): uCount = "1" extensionName = roomProperties.get("extensionName") extensionScript = roomProperties.get("extensionScript") xmlMsg = XMLObj.build_from_str("<room></room>") xmlMsg.set_attribute({ "tmp": "1", "gam": isGame, "spec": maxSpectators, "exit": exitCurrentRoom, "jas": joinAsSpectator }) xmlMsg += "<name><![CDATA[" + name + "]]></name>" xmlMsg += "<pwd><![CDATA[" + password + "]]></pwd>" xmlMsg += "<max>" + maxUsers + "</max>" xmlMsg += "<uCnt>" + uCount + "</uCnt>" if extensionName and extensionScript: xmlMsg += "<xt n='" + extensionName xmlMsg += "' s='" + extensionScript + "' />" vars = roomProperties.get("vars") xmlMsg += "<vars></vars>" if vars: for varName, varValue in vars.items(): xmlMsg += self.getXmlRoomVariable(varName, varValue) self.send(self.MESSAGE_HEADER_SYSTEM, "createRoom", roomId, xmlMsg) except: self.print_debug("[Error] createRoom error") return
def joinRoom(self, newRoom, pword="", isSpectator=False, dontLeave=False, oldRoom=-1): if isinstance(newRoom, basestring): nr = self.getRoomByName(newRoom) if not nr: return newRoom = nr.getId() if not self.checkRoomList(): return if isSpectator: isSpec = 1 else: isSpec = 0 if self.changingRoom is False: if dontLeave: leaveCurrRoom = "0" else: leaveCurrRoom = "1" if oldRoom > -1: roomToLeave = oldRoom else: self.activeRoomId if self.activeRoomId == -1: leaveCurrRoom = "0" roomToLeave = -1 roomXML = XMLObj.build_from_str("<room/>") roomXML.set_attribute({ "id": newRoom, "pwd": pword, "spec": isSpec, "leave": leaveCurrRoom, "old": roomToLeave, }) self.send(self.MESSAGE_HEADER_SYSTEM, "joinRoom", self.activeRoomId, roomXML) self.changingRoom = True return
def handleMessage(self, msg_obj, obj_type = None): from it.gotoandplay.smartfoxclient import SmartFoxClient params = {} if obj_type == SmartFoxClient.XTMSG_TYPE_XML: body = msg_obj.body action = body.xml_attr.get("action") if action == "xtRes": params["dataObj"] = XMLObj.build_from_str(body.get_data()) params["type"] = obj_type evt = SFSEvent(SFSEvent.onExtensionResponse, params) self.sfc.dispatchEvent(evt) elif obj_type == SmartFoxClient.XTMSG_TYPE_JSON: params["dataObj"] = msg_obj.get("o") params["type"] = obj_type evt = SFSEvent(SFSEvent.onExtensionResponse, params) self.sfc.dispatchEvent(evt) elif obj_type == SmartFoxClient.XTMSG_TYPE_STR: params["dataObj"] = msg_obj params["type"] = obj_type evt = SFSEvent(SFSEvent.onExtensionResponse, params) self.sfc.dispatchEvent(evt)
def handleMessage(self, msg_obj, obj_type=None): from it.gotoandplay.smartfoxclient import SmartFoxClient params = {} if obj_type == SmartFoxClient.XTMSG_TYPE_XML: body = msg_obj.body action = body.xml_attr.get("action") if action == "xtRes": params["dataObj"] = XMLObj.build_from_str(body.get_data()) params["type"] = obj_type evt = SFSEvent(SFSEvent.onExtensionResponse, params) self.sfc.dispatchEvent(evt) elif obj_type == SmartFoxClient.XTMSG_TYPE_JSON: params["dataObj"] = msg_obj.get("o") params["type"] = obj_type evt = SFSEvent(SFSEvent.onExtensionResponse, params) self.sfc.dispatchEvent(evt) elif obj_type == SmartFoxClient.XTMSG_TYPE_STR: params["dataObj"] = msg_obj params["type"] = obj_type evt = SFSEvent(SFSEvent.onExtensionResponse, params) self.sfc.dispatchEvent(evt)
def sendXtMessage(self, xtName, cmd, paramsObj, roomId = None, sendType = "xml"): if roomId is None: roomId = self.activeRoomId if self.checkRoomList() is False: return if sendType == "xml": xtReq = {} xtReq["name"] = xtName xtReq["cmd"] = cmd xtReq["param"] = paramsObj xmlmsg = XMLObj.build_from_str("<![CDATA[" + SFSObjectSerializer.serialize(xtReq) + "]]>") self.send(self.MESSAGE_HEADER_EXTENSION, "xtReq", roomId, xmlmsg) elif sendType == "json": jobj = {} jobj["x"] = xtName jobj["c"] = cmd jobj["r"] = roomId jobj["p"] = paramsObj self.sendJson(json.dumps({"t":"xt","b":jobj})) elif sendType == "str": hdr = self.MSG_STR.join([str(d) for d in ["xt", xtName, cmd, roomId]+paramsObj]) + self.MSG_STR self.sendString(hdr) return
def serialize(sfsobj, need_obj=False): if need_obj: return XMLObj.build_from_str( SFSObjectSerializer.obj2xml(sfsobj, 0, "", None)) else: return SFSObjectSerializer.obj2xml(sfsobj, 0, "", None)
def login(self, zone, name, passwd): message = "<login z='" + zone + "'><nick><![CDATA[" + name + "]]></nick><pword><![CDATA[" + passwd + "]]></pword></login>" self.send(self.MESSAGE_HEADER_SYSTEM, "login", 0, XMLObj.build_from_str(message)) return
def makeXmlHeader(self, header): xml_head = "<msg></msg>" xml_msg = XMLObj.build_from_str(xml_head) xml_msg.set_attribute({"t": header}) return xml_msg
def onConnection(self): self.print_debug("onConnection") xml_msg = XMLObj.build_from_str("<ver v='" + self.VER + "'/>") self.send(self.MESSAGE_HEADER_SYSTEM, "verChk", -1, xml_msg) return
def addBuddy(self, buddyName): if buddyName != self.myUserName and self.checkBuddyDuplicates( buddyName) == False: self.send(self.MESSAGE_HEADER_SYSTEM, "addB", -1, XMLObj.build_from_str("<n>" + buddyName + "</n>")) return
def leaveRoom(self, roomId): if self.checkRoomList() and self.checkJoin(): self.send(self.MESSAGE_HEADER_SYSTEM, "leaveRoom", roomId, XMLObj.build_from_str("<rm id='" + roomId + "' />")) return
def getBuddyRoom(self, buddy): if buddy.getId() != -1: self.send(self.MESSAGE_HEADER_SYSTEM, "roomB", -1, XMLObj.build_from_str("<b id='" + buddy.getId() + "' />")) return
def getBuddyRoom(self, buddy): if buddy.getId() != -1: self.send( self.MESSAGE_HEADER_SYSTEM, "roomB", -1, XMLObj.build_from_str("<b id='" + buddy.getId() + "' />")) return
def addBuddy(self, buddyName): if buddyName != self.myUserName and self.checkBuddyDuplicates(buddyName) == False: self.send(self.MESSAGE_HEADER_SYSTEM, "addB", -1, XMLObj.build_from_str("<n>" + buddyName + "</n>")) return
def onConnection(self): self.print_debug("onConnection") xml_msg = XMLObj.build_from_str("<ver v='"+self.VER+"'/>") self.send(self.MESSAGE_HEADER_SYSTEM, "verChk", -1, xml_msg) return
def serialize(sfsobj, need_obj = False): if need_obj: return XMLObj.build_from_str(SFSObjectSerializer.obj2xml(sfsobj, 0, "", None)) else: return SFSObjectSerializer.obj2xml(sfsobj, 0, "", None)
def makeXmlHeader(self, header): xml_head = "<msg></msg>" xml_msg = XMLObj.build_from_str(xml_head) xml_msg.set_attribute({"t":header}) return xml_msg