예제 #1
0
			ulang = utils.getLang(el)
			ptype = el.getAttribute("type")
			if to.find('@') < 0:
				# If the presence packet is to the transport (not a user) and there isn't already a session
				if not ptype: # Don't create a session unless they're sending available presence
					if self.serviceplugins['Statistics'].stats["OnlineSessions"] < 1000:
						LogEvent(INFO, msg="Attempting to create a new session")
						s = session.makeSession(self, froj.userhost(), ulang, toj)
						if s:
							self.sessions[froj.userhost()] = s
							LogEvent(INFO, msg="New session created")
							# Send the first presence
							s.onPresence(el)
						else:
							LogEvent(INFO, msg="Failed to create session")
							jabw.sendMessage(self, to=froj.userhost(), fro=config.jid, body=lang.get("notregistered", ulang))
					else:
						LogEvent(INFO, msg="Max sessions limit reached")
						jabw.sendMessage(self, to=froj.userhost(), fro=config.jid, body=lang.get("sessionslimit", ulang))
				
				elif ptype != "error":
					LogEvent(INFO, msg="Sending unavailable presence to non-logged in user")
					pres = Element((None, "presence"))
					pres.attributes["from"] = to
					pres.attributes["to"] = fro
					pres.attributes["type"] = "unavailable"
					self.send(pres)
					return
			
			elif ptype and (ptype.startswith("subscribe") or ptype.startswith("unsubscribe")):
				# They haven't logged in, and are trying to change subscription to a user
예제 #2
0
class RegisterManager:
    def __init__(self, pytrans):
        self.pytrans = pytrans
        if config.allowRegister:
            self.pytrans.discovery.addFeature(disco.IQREGISTER,
                                              self.incomingRegisterIq,
                                              config.jid)
        LogEvent(INFO)

    def removeRegInfo(self, jabberID):
        LogEvent(INFO)
        try:
            # If the session is active then send offline presences
            session = self.pytrans.sessions[jabberID]
            session.removeMe()
        except KeyError:
            pass

        self.pytrans.xdb.remove(jabberID)
        LogEvent(INFO, "", "done")

    def setRegInfo(self, jabberID, username, password):
        LogEvent(INFO)
        if (len(password) == 0):
            (blah1, password, blah3) = self.getRegInfo(jabberID)

        reginfo = legacy.formRegEntry(username, password)
        self.pytrans.xdb.set(
            internJID(jabberID).userhost(), legacy.namespace, reginfo)

    def getRegInfo(self, jabberID):
        LogEvent(INFO)
        result = self.pytrans.xdb.request(
            internJID(jabberID).userhost(), legacy.namespace)
        if (result == None):
            LogEvent(INFO, "", "Not registered!")
            return None

        username, password = legacy.getAttributes(result)

        if (username and password and len(username) > 0 and len(password) > 0):
            LogEvent(INFO, "", "Returning reg info.")
            return (username, password)
        else:
            LogEvent(WARN, "", "Registration data corrupted!")
            return None

    def incomingRegisterIq(self, incoming):
        # Check what type the Iq is..
        itype = incoming.getAttribute("type")
        LogEvent(INFO)
        if (itype == "get"):
            self.sendRegistrationFields(incoming)
        elif (itype == "set"):
            self.updateRegistration(incoming)

    def sendRegistrationFields(self, incoming):
        # Construct a reply with the fields they must fill out
        ID = incoming.getAttribute("id")
        fro = incoming.getAttribute("from")
        LogEvent(INFO)
        reply = Element((None, "iq"))
        reply.attributes["from"] = config.jid
        reply.attributes["to"] = fro
        if ID:
            reply.attributes["id"] = ID
        reply.attributes["type"] = "result"
        query = reply.addElement("query")
        query.attributes["xmlns"] = "jabber:iq:register"
        instructions = query.addElement("instructions")
        ulang = utils.getLang(incoming)
        instructions.addContent(lang.get(ulang).registerText)
        userEl = query.addElement("username")
        passEl = query.addElement("password")

        # Check to see if they're registered
        result = self.getRegInfo(incoming.getAttribute("from"))
        if (result):
            username, password = result
            userEl.addContent(username)
            query.addElement("registered")

        self.pytrans.send(reply)

    def updateRegistration(self, incoming):
        # Grab the username, password
        ID = incoming.getAttribute("id")
        fro = incoming.getAttribute("from")
        LogEvent(INFO)
        source = internJID(fro).userhost()
        ulang = utils.getLang(incoming)
        username = None
        password = None

        for queryFind in incoming.elements():
            if (queryFind.name == "query"):
                for child in queryFind.elements():
                    try:
                        if (child.name == "username"):
                            username = child.__str__()
                        elif (child.name == "password"):
                            password = child.__str__()
                        elif (child.name == "remove"):
                            # The user wants to unregister the transport! Gasp!
                            LogEvent(INFO, "", "Unregistering.")
                            try:
                                self.removeRegInfo(source)
                                self.successReply(incoming)
                            except:
                                self.xdbErrorReply(incoming)
                                return
                            LogEvent(INFO, "", "Unregistered!")
                            return
                    except AttributeError, TypeError:
                        continue  # Ignore any errors, we'll check everything below

        if (self.isValid(username) and password and len(username) > 0
                and len(password) > 0):
            # Valid registration data
            LogEvent(INFO, "", "Updating XDB")
            try:
                self.setRegInfo(source, username, password)
                LogEvent(INFO, "", "Updated XDB")
                self.successReply(incoming)
                LogEvent(INFO, "", "Sent a result Iq")
                to = internJID(incoming.getAttribute("from")).userhost()
                jabw.sendPresence(self.pytrans,
                                  to=to,
                                  fro=config.jid,
                                  ptype="subscribe")
                if (config.registerMessage):
                    jabw.sendMessage(self.pytrans,
                                     to=incoming.getAttribute("from"),
                                     fro=config.jid,
                                     body=config.registerMessage)
            except:
                self.xdbErrorReply(incoming)
                raise

        else:
            self.badRequestReply(incoming)
예제 #3
0
파일: register.py 프로젝트: Ashaman-/pyaimt
							return
					except AttributeError, TypeError:
						continue # Ignore any errors, we'll check everything below
		
		if username and password and len(username) > 0 and len(password) > 0:
			# Valid registration data
			LogEvent(INFO, msg="Updating XDB")
			try:
				self.pytrans.xdb.setRegistration(source, username, password)
				LogEvent(INFO, msg="Updated XDB")
				self.successReply(incoming)
				LogEvent(INFO, msg="Sent a result Iq")
				to = internJID(incoming.getAttribute("from")).userhost()
				jabw.sendPresence(self.pytrans, to=to, fro=config.jid, ptype="subscribe")
				if config.registerMessage:
					jabw.sendMessage(self.pytrans, to=incoming.getAttribute("from"), fro=config.jid, body=config.registerMessage)
			except:
				self.xdbErrorReply(incoming)
				raise
		
		else:
			self.badRequestReply(incoming)
	
	def badRequestReply(self, incoming):
		LogEvent(INFO)
		# Send an error Iq
		reply = incoming
		reply.swapAttributeValues("to", "from")
		reply.attributes["type"] = "error"
		error = reply.addElement("error")
		error.attributes["type"] = "modify"
예제 #4
0
파일: main.py 프로젝트: zfortier/yakalope
            ulang = utils.getLang(el)
            ptype = el.getAttribute("type")
            if to.find('@') < 0:
                # If the presence packet is to the transport (not a user) and there isn't already a session
                if not ptype:  # Don't create a session unless they're sending available presence
                    LogEvent(INFO, msg="Attempting to create a new session")
                    s = session.makeSession(self, froj.userhost(), ulang, toj)
                    if s:
                        self.sessions[froj.userhost()] = s
                        LogEvent(INFO, msg="New session created")
                        # Send the first presence
                        s.onPresence(el)
                    else:
                        LogEvent(INFO, msg="Failed to create session")
                        jabw.sendMessage(self,
                                         to=froj.userhost(),
                                         fro=config.jid,
                                         body=lang.get("notregistered", ulang))

                elif ptype != "error":
                    LogEvent(
                        INFO,
                        msg="Sending unavailable presence to non-logged in user"
                    )
                    pres = Element((None, "presence"))
                    pres.attributes["from"] = to
                    pres.attributes["to"] = fro
                    pres.attributes["type"] = "unavailable"
                    self.send(pres)
                    return

            elif ptype and (ptype.startswith("subscribe")
예제 #5
0
                # If the presence packet is to the transport (not a user) and there isn't already a session
                if not el.getAttribute(
                        "type"
                ):  # Don't create a session unless they're sending available presence
                    LogEvent(INFO, "", "Attempting to create a new session.")
                    s = session.makeSession(self, froj.userhost(), ulang)
                    if s:
                        self.statistics.stats["TotalUsers"] += 1
                        self.sessions[froj.userhost()] = s
                        LogEvent(INFO, "", "New session created.")
                        # Send the first presence
                        s.onPresence(el)
                    else:
                        LogEvent(INFO, "", "Failed to create session")
                        jabw.sendMessage(self,
                                         to=froj.userhost(),
                                         fro=config.jid,
                                         body=lang.get(ulang).notRegistered)

                elif el.getAttribute("type") != "error":
                    LogEvent(
                        INFO, "",
                        "Sending unavailable presence to non-logged in user.")
                    jabw.sendPresence(self, fro, to, ptype="unavailable")
                    return

            elif ptype and (ptype.startswith("subscribe")
                            or ptype.startswith("unsubscribe")):
                # They haven't logged in, and are trying to change subscription to a user
                # No, lets not log them in. Lets send an error :)
                jabw.sendPresence(self, fro, to, ptype="error")
예제 #6
0
        if username and password and len(username) > 0 and len(password) > 0:
            # Valid registration data
            LogEvent(INFO, msg="Updating XDB")
            try:
                self.pytrans.xdb.setRegistration(source, username, password)
                LogEvent(INFO, msg="Updated XDB")
                self.successReply(incoming)
                LogEvent(INFO, msg="Sent a result Iq")
                to = internJID(incoming.getAttribute("from")).userhost()
                jabw.sendPresence(self.pytrans,
                                  to=to,
                                  fro=config.jid,
                                  ptype="subscribe")
                if config.registerMessage:
                    jabw.sendMessage(self.pytrans,
                                     to=incoming.getAttribute("from"),
                                     fro=config.jid,
                                     body=config.registerMessage)
            except:
                self.xdbErrorReply(incoming)
                raise

        else:
            self.badRequestReply(incoming)

    def badRequestReply(self, incoming):
        LogEvent(INFO)
        # Send an error Iq
        reply = incoming
        reply.swapAttributeValues("to", "from")
        reply.attributes["type"] = "error"
        error = reply.addElement("error")