Esempio n. 1
0
    def __from_xml(self, xmlnode):
        """Initialize `Register` from an XML node.

        :Parameters:
            - `xmlnode`: the jabber:x:register XML element.
        :Types:
            - `xmlnode`: `libxml2.xmlNode`"""

        self.__logger.debug("Converting jabber:iq:register element from XML")
        if xmlnode.type != "element":
            raise ValueError, "XML node is not a jabber:iq:register element (not an element)"
        ns = get_node_ns_uri(xmlnode)
        if ns and ns != REGISTER_NS or xmlnode.name != "query":
            raise ValueError, "XML node is not a jabber:iq:register element"

        for element in xml_element_iter(xmlnode.children):
            ns = get_node_ns_uri(element)
            if ns == DATAFORM_NS and element.name == "x" and not self.form:
                self.form = Form(element)
            elif ns != REGISTER_NS:
                continue
            name = element.name
            if name == "instructions" and not self.instructions:
                self.instructions = from_utf8(element.getContent())
            elif name == "registered":
                self.registered = True
            elif name == "remove":
                self.remove = True
            elif name in legacy_fields and not getattr(self, name):
                value = from_utf8(element.getContent())
                if value is None:
                    value = u""
                self.__logger.debug(u"Setting legacy field %r to %r" %
                                    (name, value))
                setattr(self, name, value)
Esempio n. 2
0
    def set_history(self, parameters):
        """
        Set history parameters.

        Types:
            - `parameters`: `HistoryParameters`
        """
        for child in xml_element_iter(self.xmlnode.children):
            if get_node_ns_uri(child) == MUC_NS and child.name == "history":
                child.unlinkNode()
                child.freeNode()
                break

        if parameters.maxchars and parameters.maxchars < 0:
            raise ValueError, "History parameter maxchars must be positive"
        if parameters.maxstanzas and parameters.maxstanzas < 0:
            raise ValueError, "History parameter maxstanzas must be positive"
        if parameters.maxseconds and parameters.maxseconds < 0:
            raise ValueError, "History parameter maxseconds must be positive"

        hnode=self.xmlnode.newChild(self.xmlnode.ns(), "history", None)

        if parameters.maxchars is not None:
            hnode.setProp("maxchars", str(parameters.maxchars))
        if parameters.maxstanzas is not None:
            hnode.setProp("maxstanzas", str(parameters.maxstanzas))
        if parameters.maxseconds is not None:
            hnode.setProp("maxseconds", str(parameters.maxseconds))
        if parameters.since is not None:
            hnode.setProp("since", parameters.since.strftime("%Y-%m-%dT%H:%M:%SZ"))
Esempio n. 3
0
    def set_history(self, parameters):
        """
        Set history parameters.

        Types:
            - `parameters`: `HistoryParameters`
        """
        for child in xml_element_iter(self.xmlnode.children):
            if get_node_ns_uri(child) == MUC_NS and child.name == "history":
                child.unlinkNode()
                child.freeNode()
                break

        if parameters.maxchars and parameters.maxchars < 0:
            raise ValueError, "History parameter maxchars must be positive"
        if parameters.maxstanzas and parameters.maxstanzas < 0:
            raise ValueError, "History parameter maxstanzas must be positive"
        if parameters.maxseconds and parameters.maxseconds < 0:
            raise ValueError, "History parameter maxseconds must be positive"

        hnode = self.xmlnode.newChild(self.xmlnode.ns(), "history", None)

        if parameters.maxchars is not None:
            hnode.setProp("maxchars", str(parameters.maxchars))
        if parameters.maxstanzas is not None:
            hnode.setProp("maxstanzas", str(parameters.maxstanzas))
        if parameters.maxseconds is not None:
            hnode.setProp("maxseconds", str(parameters.maxseconds))
        if parameters.since is not None:
            hnode.setProp("since",
                          parameters.since.strftime("%Y-%m-%dT%H:%M:%SZ"))
Esempio n. 4
0
    def __from_xml(self, xmlnode):
        """Initialize `Register` from an XML node.

        :Parameters:
            - `xmlnode`: the jabber:x:register XML element.
        :Types:
            - `xmlnode`: `libxml2.xmlNode`"""

        self.__logger.debug("Converting jabber:iq:register element from XML")
        if xmlnode.type != "element":
            raise ValueError, "XML node is not a jabber:iq:register element (not an element)"
        ns = get_node_ns_uri(xmlnode)
        if ns and ns != REGISTER_NS or xmlnode.name != "query":
            raise ValueError, "XML node is not a jabber:iq:register element"

        for element in xml_element_iter(xmlnode.children):
            ns = get_node_ns_uri(element)
            if ns == DATAFORM_NS and element.name == "x" and not self.form:
                self.form = Form(element)
            elif ns != REGISTER_NS:
                continue
            name = element.name
            if name == "instructions" and not self.instructions:
                self.instructions = from_utf8(element.getContent())
            elif name == "registered":
                self.registered = True
            elif name == "remove":
                self.remove = True
            elif name in legacy_fields and not getattr(self, name):
                value = from_utf8(element.getContent())
                if value is None:
                    value = u""
                self.__logger.debug(u"Setting legacy field %r to %r" % (name, value))
                setattr(self, name, value)
Esempio n. 5
0
    def get_password(self):
        """Get password from the MUC request.

        :returntype: `unicode`
        """
        for child in xml_element_iter(self.xmlnode.children):
            if get_node_ns_uri(child) == MUC_NS and child.name == "password":
                return from_utf8(child.getContent())
        return None
Esempio n. 6
0
    def get_password(self):
        """Get password from the MUC request.

        :returntype: `unicode`
        """
        for child in xml_element_iter(self.xmlnode.children):
            if get_node_ns_uri(child) == MUC_NS and child.name == "password":
                return from_utf8(child.getContent())
        return None
Esempio n. 7
0
    def set_password(self, password):
        """Set password for the MUC request.

        :Parameters:
            - `password`: password
        :Types:
            - `password`: `unicode`"""
        for child in xml_element_iter(self.xmlnode.children):
            if get_node_ns_uri(child) == MUC_NS and child.name == "password":
                child.unlinkNode()
                child.freeNode()
                break

        if password is not None:
            self.xmlnode.newTextChild(self.xmlnode.ns(), "password", to_utf8(password))
Esempio n. 8
0
    def set_password(self, password):
        """Set password for the MUC request.

        :Parameters:
            - `password`: password
        :Types:
            - `password`: `unicode`"""
        for child in xml_element_iter(self.xmlnode.children):
            if get_node_ns_uri(child) == MUC_NS and child.name == "password":
                child.unlinkNode()
                child.freeNode()
                break

        if password is not None:
            self.xmlnode.newTextChild(self.xmlnode.ns(), "password",
                                      to_utf8(password))
Esempio n. 9
0
    def get_history(self):
        """Return history parameters carried by the stanza.

        :returntype: `HistoryParameters`"""
        for child in xml_element_iter(self.xmlnode.children):
            if get_node_ns_uri(child) == MUC_NS and child.name == "history":
                maxchars = from_utf8(child.prop("maxchars"))
                if maxchars is not None:
                    maxchars = int(maxchars)
                maxstanzas = from_utf8(child.prop("maxstanzas"))
                if maxstanzas is not None:
                    maxstanzas = int(maxstanzas)
                maxseconds = from_utf8(child.prop("maxseconds"))
                if maxseconds is not None:
                    maxseconds = int(maxseconds)
                # TODO: since -- requires parsing of Jabber dateTime profile
                since = None
                return HistoryParameters(maxchars, maxstanzas, maxseconds, since)
Esempio n. 10
0
    def get_history(self):
        """Return history parameters carried by the stanza.

        :returntype: `HistoryParameters`"""
        for child in xml_element_iter(self.xmlnode.children):
            if get_node_ns_uri(child) == MUC_NS and child.name == "history":
                maxchars = from_utf8(child.prop("maxchars"))
                if maxchars is not None:
                    maxchars = int(maxchars)
                maxstanzas = from_utf8(child.prop("maxstanzas"))
                if maxstanzas is not None:
                    maxstanzas = int(maxstanzas)
                maxseconds = from_utf8(child.prop("maxseconds"))
                if maxseconds is not None:
                    maxseconds = int(maxseconds)
                # TODO: since -- requires parsing of Jabber dateTime profile
                since = None
                return HistoryParameters(maxchars, maxstanzas, maxseconds,
                                         since)