Beispiel #1
0
 def from_xml(self,node):
     """Initialize Feature from XML node."""
     if node.type!="element":
         raise ValueError,"XML node is not a feature element (not en element)"
     ns=get_node_ns_uri(node)
     if ns and ns!=FEATURE_NEG_NS or node.name!="feature":
         raise ValueError,"XML node is not a feature element"
     possible_streams = []
     self.selected_stream = None
     n=node.children
     while n:
         if n.type!="element":
             n=n.next
             continue
         ns=get_node_ns_uri(n)
         if ns and ns!=DATAFORM_NS or n.name!="x":
             n=n.next
             continue
         form=dataforms.Form(n, strict=False)
         if not form.type:
             form.type = "form"
         try:
             field = form['stream-method']
         except KeyError:
             n=n.next
             continue
         else: #if new_streams:
             self.selected_stream = field.value
             possible_streams.extend(field.options)
         n=n.next
     self.possible_streams = [v for o in possible_streams for v in o.values] or None
Beispiel #2
0
 def from_xml(self, node):
     """Initialize Feature from XML node."""
     if node.type != "element":
         raise ValueError, "XML node is not a feature element (not en element)"
     ns = get_node_ns_uri(node)
     if ns and ns != FEATURE_NEG_NS or node.name != "feature":
         raise ValueError, "XML node is not a feature element"
     possible_streams = []
     self.selected_stream = None
     n = node.children
     while n:
         if n.type != "element":
             n = n.next
             continue
         ns = get_node_ns_uri(n)
         if ns and ns != DATAFORM_NS or n.name != "x":
             n = n.next
             continue
         form = dataforms.Form(n, strict=False)
         if not form.type:
             form.type = "form"
         try:
             field = form['stream-method']
         except KeyError:
             n = n.next
             continue
         else:  #if new_streams:
             self.selected_stream = field.value
             possible_streams.extend(field.options)
         n = n.next
     self.possible_streams = [
         v for o in possible_streams for v in o.values
     ] or None
Beispiel #3
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)
Beispiel #4
0
    def from_xml(self,node,strict=True):
        """
        Initialize Roster object from XML node.

        If `strict` is False, than invalid items in the XML will be ignored.
        """
        self.items_dict={}
        if node.type!="element":
            raise ValueError,"XML node is not a roster (not en element)"
        ns=get_node_ns_uri(node)
        if ns and ns!=ROSTER_NS or node.name!="query":
            raise ValueError,"XML node is not a roster"
        n=node.children
        while n:
            if n.type!="element":
                n=n.next
                continue
            ns=get_node_ns_uri(n)
            if ns and ns!=ROSTER_NS or n.name!="item":
                n=n.next
                continue
            try:
                item=RosterItem(n)
                self.items_dict[item.jid]=item
            except ValueError:
                if strict:
                    raise
            n=n.next
Beispiel #5
0
 def from_xml(self, node):
     """Initialize RosterItem from XML node."""
     if node.type != "element":
         raise ValueError, "XML node is not a roster item (not en element)"
     ns = get_node_ns_uri(node)
     if ns and ns != ROSTER_NS or node.name != "item":
         raise ValueError, "XML node is not a roster item"
     jid = JID(node.prop("jid").decode("utf-8"))
     subscription = node.prop("subscription")
     if subscription not in ("none", "from", "to", "both", "remove"):
         subscription = "none"
     ask = node.prop("ask")
     if ask not in ("subscribe", None):
         ask = None
     name = from_utf8(node.prop("name"))
     groups = []
     n = node.children
     while n:
         if n.type != "element":
             n = n.next
             continue
         ns = get_node_ns_uri(n)
         if ns and ns != ROSTER_NS or n.name != "group":
             n = n.next
             continue
         group = n.getContent()
         if group:
             groups.append(from_utf8(group))
         n = n.next
     self.jid = jid
     self.name = name
     self.groups = groups
     self.subscription = subscription
     self.ask = ask
Beispiel #6
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)
Beispiel #7
0
 def from_xml(self,node):
     """Initialize RosterItem from XML node."""
     if node.type!="element":
         raise ValueError,"XML node is not a roster item (not en element)"
     ns=get_node_ns_uri(node)
     if ns and ns!=ROSTER_NS or node.name!="item":
         raise ValueError,"XML node is not a roster item"
     jid=JID(node.prop("jid").decode("utf-8"))
     subscription=node.prop("subscription")
     if subscription not in ("none","from","to","both","remove"):
         subscription="none"
     ask=node.prop("ask")
     if ask not in ("subscribe",None):
         ask=None
     name=from_utf8(node.prop("name"))
     groups=[]
     n=node.children
     while n:
         if n.type!="element":
             n=n.next
             continue
         ns=get_node_ns_uri(n)
         if ns and ns!=ROSTER_NS or n.name!="group":
             n=n.next
             continue
         group=n.getContent()
         if group:
             groups.append(from_utf8(group))
         n=n.next
     self.jid=jid
     self.name=name
     self.groups=groups
     self.subscription=subscription
     self.ask=ask
Beispiel #8
0
    def from_xml(self, node, strict=True):
        """
        Initialize Roster object from XML node.

        If `strict` is False, than invalid items in the XML will be ignored.
        """
        self.items_dict = {}
        if node.type != "element":
            raise ValueError, "XML node is not a roster (not en element)"
        ns = get_node_ns_uri(node)
        if ns and ns != ROSTER_NS or node.name != "query":
            raise ValueError, "XML node is not a roster"
        n = node.children
        while n:
            if n.type != "element":
                n = n.next
                continue
            ns = get_node_ns_uri(n)
            if ns and ns != ROSTER_NS or n.name != "item":
                n = n.next
                continue
            try:
                item = RosterItem(n)
                self.items_dict[item.jid] = item
            except ValueError:
                if strict:
                    raise
            n = n.next
Beispiel #9
0
    def __from_xml(self, node):
        self.tstamp = None
        AbstractBlob.set_data(self, None)
        if node.type!="element":
            raise ValueError,"XML node is not a %s (not en element)" % self.xml_element_namespace
        ns=get_node_ns_uri(node)
        if ns and ns!=self.xml_element_namespace or node.name!=self.xml_element_name:
            raise ValueError,"XML node is not a %s" % self.xml_element_namespace

        n=node.children
        while n:
            if n.type!="element":
                n=n.next
                continue
            ns=get_node_ns_uri(n)
            if ns and ns!=self.xml_element_namespace:
                n=n.next
                continue
            if n.name=="data":
                AbstractBlob.set_data(self, base64.decodestring(n.getContent()))
            elif n.name=="time":
                self.tstamp=n.getContent()
            elif n.name=="update-needed":
                self.update_needed = True
            n=n.next
Beispiel #10
0
 def __from_xml(self, xmlnode):
     if xmlnode.type != "element":
         raise ValueError, "XML node is not a Privacy (not en element)"
     ns = get_node_ns_uri(xmlnode)
     if ns and ns != PRIVACY_NS or xmlnode.name != "query":
         raise ValueError, "XML node is not a query"
     n = xmlnode.children
     while n:
         if n.type != "element":
             n = n.next
             continue
         ns = get_node_ns_uri(n)
         if ns and ns != PRIVACY_NS or n.name not in ("active", "default",
                                                      "list"):
             n = n.next
             continue
         if n.name == "active":
             self.active = n.prop("name") if libxml2.xmlNode.hasProp(
                 "name") else None
         elif n.name == "default":
             self.default = n.prop("default") if libxml2.xmlNode.hasProp(
                 "default") else None
         else:
             self.append(List(n))
         n = n.next
Beispiel #11
0
    def from_xml(self, xmlnode):
        """Initialize Delay object from an XML node.

        :Parameters:
            - `xmlnode`: the jabber:x:delay XML element.
        :Types:
            - `xmlnode`: `libxml2.xmlNode`"""
        if xmlnode.type != "element":
            raise ValueError, "XML node is not a jabber:x:delay element (not an element)"
        ns = get_node_ns_uri(xmlnode)
        if ns and ns != DELAY_NS or xmlnode.name != "x":
            raise ValueError, "XML node is not a jabber:x:delay element"
        stamp = xmlnode.prop("stamp")
        if stamp.endswith("Z"):
            stamp = stamp[:-1]
        if "-" in stamp:
            stamp = stamp.split("-", 1)[0]
        try:
            tm = time.strptime(stamp, "%Y%m%dT%H:%M:%S")
        except ValueError:
            raise BadRequestProtocolError, "Bad timestamp"
        tm = tm[0:8] + (0,)
        self.timestamp = datetime.datetime.fromtimestamp(time.mktime(tm))
        delay_from = from_utf8(xmlnode.prop("from"))
        if delay_from:
            try:
                self.delay_from = JID(delay_from)
            except JIDError:
                raise JIDMalformedProtocolError, "Bad JID in the jabber:x:delay 'from' attribute"
        else:
            self.delay_from = None
        self.reason = from_utf8(xmlnode.getContent())
Beispiel #12
0
    def from_xml(self,node):
        #need jid, host, port, zeroconf
        """Initialize StreamHost from XML node."""
        if node.type!="element":
            raise ValueError,"XML node is not a streamhost (not en element)"
        ns=get_node_ns_uri(node)
        if ns and ns!=self.xml_element_namespace or node.name!=self.xml_element_name:
            raise ValueError,"XML node is not a %s descriptor" % self.xml_element_name
        jid=JID(node.prop("jid").decode("utf-8"))
        self.jid=jid

        host=node.prop("host").decode("utf-8")
        self.host=host

        port=node.prop("port")
        #py2.5:
        self.port = int(port.decode("utf-8")) if port else None
        #py2.4:
#        if port:
#            self.port = int(port.decode("utf-8"))
#        else:
#            self.port = None
        zeroconf=node.prop("zeroconf")
        #py2.5:
        self.zeroconf = zeroconf.decode("utf-8") if zeroconf else None
Beispiel #13
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"))
Beispiel #14
0
    def from_xml(self, xmlnode):
        """Initialize Delay object from an XML node.

        :Parameters:
            - `xmlnode`: the jabber:x:delay XML element.
        :Types:
            - `xmlnode`: `libxml2.xmlNode`"""
        if xmlnode.type != "element":
            raise ValueError, "XML node is not a jabber:x:delay element (not an element)"
        ns = get_node_ns_uri(xmlnode)
        if ns and (ns != self.xml_element_namespace
                   or xmlnode.name != self.xml_element_name):
            raise ValueError, "XML node is not a " + self.xml_element_namespace + " element"
        stamp = xmlnode.prop("stamp")
        tm = _parse_ts(stamp)
        tm = tm[0:8] + (0, )
        self.timestamp = datetime.datetime.fromtimestamp(time.mktime(tm))
        delay_from = from_utf8(xmlnode.prop("from"))
        if delay_from:
            try:
                self.delay_from = JID(delay_from)
            except JIDError:
                raise JIDMalformedProtocolError, "Bad JID in the " + self.xml_element_namespace + " 'from' attribute"
        else:
            self.delay_from = None
        self.reason = from_utf8(xmlnode.getContent())
Beispiel #15
0
    def from_xml(self, xmlnode):
        """Initialize Delay object from an XML node.

        :Parameters:
            - `xmlnode`: the jabber:x:delay XML element.
        :Types:
            - `xmlnode`: `libxml2.xmlNode`"""
        if xmlnode.type != "element":
            raise ValueError, "XML node is not a jabber:x:delay element (not an element)"
        ns = get_node_ns_uri(xmlnode)
        if ns and ns != DELAY_NS or xmlnode.name != "x":
            raise ValueError, "XML node is not a jabber:x:delay element"
        stamp = xmlnode.prop("stamp")
        if stamp.endswith("Z"):
            stamp = stamp[:-1]
        if "-" in stamp:
            stamp = stamp.split("-", 1)[0]
        try:
            tm = time.strptime(stamp, "%Y%m%dT%H:%M:%S")
        except ValueError:
            raise BadRequestProtocolError, "Bad timestamp"
        tm = tm[0:8] + (0, )
        self.timestamp = datetime.datetime.fromtimestamp(time.mktime(tm))
        delay_from = from_utf8(xmlnode.prop("from"))
        if delay_from:
            try:
                self.delay_from = JID(delay_from)
            except JIDError:
                raise JIDMalformedProtocolError, "Bad JID in the jabber:x:delay 'from' attribute"
        else:
            self.delay_from = None
        self.reason = from_utf8(xmlnode.getContent())
Beispiel #16
0
 def from_xml(self,node):
     if node.type!="element":
         raise ValueError,"XML node is not a ip (not en element)"
     ns=get_node_ns_uri(node)
     if ns and ns!=self.xml_element_namespace or node.name!=self.xml_element_name:
         raise ValueError,"XML node is not a %s descriptor" % self.xml_element_name
     self.ip = from_utf8(node.getContent())
Beispiel #17
0
 def from_xml(self, node):
     if node.type != "element":
         raise ValueError, "XML node is not a ip (not en element)"
     ns = get_node_ns_uri(node)
     if ns and ns != self.xml_element_namespace or node.name != self.xml_element_name:
         raise ValueError, "XML node is not a %s descriptor" % self.xml_element_name
     self.ip = from_utf8(node.getContent())
Beispiel #18
0
    def from_xml(self,xmlnode):
        """Initialize Delay object from an XML node.

        :Parameters:
            - `xmlnode`: the jabber:x:delay XML element.
        :Types:
            - `xmlnode`: `libxml2.xmlNode`"""
        if xmlnode.type!="element":
            raise ValueError,"XML node is not a jabber:x:delay element (not an element)"
        ns=get_node_ns_uri(xmlnode)
        if ns and (ns != self.xml_element_namespace
                or xmlnode.name != self.xml_element_name):
            raise ValueError,"XML node is not a " + self.xml_element_namespace + " element"
        stamp=xmlnode.prop("stamp")
        tm = _parse_ts(stamp)
        tm=tm[0:8]+(0,)
        self.timestamp=datetime.datetime.fromtimestamp(time.mktime(tm))
        delay_from=from_utf8(xmlnode.prop("from"))
        if delay_from:
            try:
                self.delay_from = JID(delay_from)
            except JIDError:
                raise JIDMalformedProtocolError, "Bad JID in the " + self.xml_element_namespace + " 'from' attribute"
        else:
            self.delay_from = None
        self.reason = from_utf8(xmlnode.getContent())
Beispiel #19
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"))
Beispiel #20
0
    def __from_xml(self, node):
        if node.type!="element":
            raise ValueError,"XML node is not a %s element (not en element)" % self.xml_element_name
        ns=get_node_ns_uri(node)
        if ns and ns!=self.xml_element_namespace or node.name!=self.xml_element_name:
            raise ValueError,"XML node is not an %s element" % self.xml_element_name

        labelss = xpath_eval(node, 'g:labels',{'g':GOOGLE_MAIL_NOTIFY_NS})
        labels = labelss[0].getContent() if labelss else None
        self.labels = from_utf8(labels).split('|') if labels else []

        senderss = xpath_eval(node, 'g:senders',{'g':GOOGLE_MAIL_NOTIFY_NS})
        self.senders = Senders(senderss[0]) if senderss else []

        subjects = xpath_eval(node, 'g:subject',{'g':GOOGLE_MAIL_NOTIFY_NS})
        self.subject = from_utf8(subjects[0].getContent()) if subjects else None

        snippets = xpath_eval(node, 'g:snippet',{'g':GOOGLE_MAIL_NOTIFY_NS})
        self.snippet = from_utf8(snippets[0].getContent()) if snippets else None

        self.tid = int(from_utf8(node.prop("tid")))
        self.participation = int(from_utf8(node.prop("participation")))
        self.messages = int(from_utf8(node.prop("messages")))
        self.date = int(from_utf8(node.prop("date")))
        self.url = from_utf8(node.prop("date"))
Beispiel #21
0
    def from_xml(self, node):
        #need jid, host, port, zeroconf
        """Initialize StreamHost from XML node."""
        if node.type != "element":
            raise ValueError, "XML node is not a streamhost (not en element)"
        ns = get_node_ns_uri(node)
        if ns and ns != self.xml_element_namespace or node.name != self.xml_element_name:
            raise ValueError, "XML node is not a %s descriptor" % self.xml_element_name
        jid = JID(node.prop("jid").decode("utf-8"))
        self.jid = jid

        host = node.prop("host").decode("utf-8")
        self.host = host

        port = node.prop("port")
        #py2.5:
        self.port = int(port.decode("utf-8")) if port else None
        #py2.4:
        #        if port:
        #            self.port = int(port.decode("utf-8"))
        #        else:
        #            self.port = None
        zeroconf = node.prop("zeroconf")
        #py2.5:
        self.zeroconf = zeroconf.decode("utf-8") if zeroconf else None
Beispiel #22
0
 def __from_xml(self, node):
     if node.type != "element":
         raise ValueError, "XML node is not an Widgets element (not en element)"
     ns = get_node_ns_uri(node)
     if ns and ns != DIGSBY_WIDGETS_NS or node.name != "query":
         raise ValueError, "XML node is not an Widgets element"
     widgets = xpath_eval(node, 'w:widget', {'w': DIGSBY_WIDGETS_NS})
     self.extend(Widget(widget) for widget in widgets)
Beispiel #23
0
 def __from_xml(self, node):
     if node.type!="element":
         raise ValueError,"XML node is not a %s element (not en element)" % self.xml_element_name
     ns=get_node_ns_uri(node)
     if ns and ns!=self.xml_element_namespace or node.name!=self.xml_element_name:
         raise ValueError,"XML node is not an %s element" % self.xml_element_name
     senders = xpath_eval(node, 's:sender',{'s':GOOGLE_MAIL_NOTIFY_NS})
     self.extend(Sender(sender) for sender in senders)
Beispiel #24
0
 def __from_xml(self, node):
     if node.type != "element":
         raise ValueError, "XML node is not a %s element (not en element)" % self.xml_element_name
     ns = get_node_ns_uri(node)
     if ns and ns != self.xml_element_namespace or node.name != self.xml_element_name:
         raise ValueError, "XML node is not an %s element" % self.xml_element_name
     senders = xpath_eval(node, 's:sender', {'s': GOOGLE_MAIL_NOTIFY_NS})
     self.extend(Sender(sender) for sender in senders)
Beispiel #25
0
 def __from_xml(self, node):
     if node.type!="element":
         raise ValueError,"XML node is not a Stats Counter element (not en element)"
     ns=get_node_ns_uri(node)
     if ns and ns!=DIGSBY_STATS_COUNTER_NS or node.name!="query":
         raise ValueError,"XML node is not a Stats Counter element"
     actions = xpath_eval(node, 'sc:action',{'sc':DIGSBY_STATS_COUNTER_NS})
     self.extend(Action(action) for action in actions)
Beispiel #26
0
 def __from_xml(self, node):
     if node.type!="element":
         raise ValueError,"XML node is not an Widgets element (not en element)"
     ns=get_node_ns_uri(node)
     if ns and ns!=DIGSBY_WIDGETS_NS or node.name!="query":
         raise ValueError,"XML node is not an Widgets element"
     widgets = xpath_eval(node, 'w:widget',{'w':DIGSBY_WIDGETS_NS})
     self.extend(Widget(widget) for widget in widgets)
Beispiel #27
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
Beispiel #28
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
Beispiel #29
0
 def __from_xml(self, node):
     if node.type != "element":
         raise ValueError, "XML node is not a Stats Counter element (not en element)"
     ns = get_node_ns_uri(node)
     if ns and ns != DIGSBY_STATS_COUNTER_NS or node.name != "query":
         raise ValueError, "XML node is not a Stats Counter element"
     actions = xpath_eval(node, 'sc:action',
                          {'sc': DIGSBY_STATS_COUNTER_NS})
     self.extend(Action(action) for action in actions)
Beispiel #30
0
 def from_xml(self, xmlnode):
     self.xml_element_name = xmlnode.name
     if xmlnode.type!="element":
         raise ValueError,"XML node is not a chat state (not en element)"
     ns=get_node_ns_uri(xmlnode)
     if ns and ns!=self.xml_element_namespace:
         raise ValueError,"XML node is not a chat state descriptor"
     if not self.valid_state():
         import warnings
         warnings.warn("XML node with name: %r is not a valid chat state" % self.xml_element_name)
Beispiel #31
0
 def __from_xml(self, xmlnode):
     """Initialize List from XML node."""
     if xmlnode.type!="element":
         raise ValueError,"XML node is not a list (not en element)"
     ns=get_node_ns_uri(xmlnode)
     if ns and ns!=PRIVACY_NS or xmlnode.name!="list":
         raise ValueError,"XML node is not a list"
     self.name = xmlnode.prop("name")
     n=xmlnode.children
     while n:
         if n.type!="element":
             n=n.next
             continue
         ns=get_node_ns_uri(n)
         if ns and ns!=PRIVACY_NS or n.name != "item":
             n=n.next
             continue
         self.append(ListItem(n))
         n=n.next
Beispiel #32
0
    def from_xml(self,node,strict=True):
        """
        Initialize Roster object from XML node.

        If `strict` is False, than invalid items in the XML will be ignored.
        """
        node_=node.prop("node")
        sid=node.prop("sid")
        self.sid = from_utf8(sid) if sid else None
        mode=node.prop("mode")
        self.mode = from_utf8(mode) if mode else None
        self.mode = self.mode if self.mode != 'tcp' else None
        if node_:
            self.node = node_.decode("utf-8")
        else:
            self.node = None
        if node.type!="element":
            raise ValueError,"XML node is not a bytestreams (not en element)"
        ns=get_node_ns_uri(node)
        if ns and ns!=BYTESTREAMS_NS or node.name!="query":
            raise ValueError,"XML node is not a bytestreams query"
        n=node.children
        while n:
            if n.type!="element":
                n=n.next
                continue
            ns=get_node_ns_uri(n)
            if ns and ns!=BYTESTREAMS_NS:
                n=n.next
                continue
            if n.name=="streamhost":
                try:
                    self.add_host(n)
                except ValueError:
                    if strict:
                        raise
            elif n.name=="streamhost-used":
                host_used=JID(n.prop("jid").decode("utf-8"))
                self.host_used=host_used
            elif n.name=="activate":
                activate=JID(n.getContent())
                self.activate=activate
            n=n.next
Beispiel #33
0
 def __from_xml(self, xmlnode):
     """Initialize List from XML node."""
     if xmlnode.type != "element":
         raise ValueError, "XML node is not a list (not en element)"
     ns = get_node_ns_uri(xmlnode)
     if ns and ns != PRIVACY_NS or xmlnode.name != "list":
         raise ValueError, "XML node is not a list"
     self.name = xmlnode.prop("name")
     n = xmlnode.children
     while n:
         if n.type != "element":
             n = n.next
             continue
         ns = get_node_ns_uri(n)
         if ns and ns != PRIVACY_NS or n.name != "item":
             n = n.next
             continue
         self.append(ListItem(n))
         n = n.next
Beispiel #34
0
    def from_xml(self, node, strict=True):
        """
        Initialize Roster object from XML node.

        If `strict` is False, than invalid items in the XML will be ignored.
        """
        node_ = node.prop("node")
        sid = node.prop("sid")
        self.sid = from_utf8(sid) if sid else None
        mode = node.prop("mode")
        self.mode = from_utf8(mode) if mode else None
        self.mode = self.mode if self.mode != 'tcp' else None
        if node_:
            self.node = node_.decode("utf-8")
        else:
            self.node = None
        if node.type != "element":
            raise ValueError, "XML node is not a bytestreams (not en element)"
        ns = get_node_ns_uri(node)
        if ns and ns != BYTESTREAMS_NS or node.name != "query":
            raise ValueError, "XML node is not a bytestreams query"
        n = node.children
        while n:
            if n.type != "element":
                n = n.next
                continue
            ns = get_node_ns_uri(n)
            if ns and ns != BYTESTREAMS_NS:
                n = n.next
                continue
            if n.name == "streamhost":
                try:
                    self.add_host(n)
                except ValueError:
                    if strict:
                        raise
            elif n.name == "streamhost-used":
                host_used = JID(n.prop("jid").decode("utf-8"))
                self.host_used = host_used
            elif n.name == "activate":
                activate = JID(n.getContent())
                self.activate = activate
            n = n.next
Beispiel #35
0
 def __from_xml(self, node):
     if node.type!="element":
         raise ValueError,"XML node is not an Accounts element (not en element)"
     ns=get_node_ns_uri(node)
     if ns and ns!=DIGSBY_ACCOUNTS_NS or node.name!="query":
         raise ValueError,"XML node is not an Accounts element"
     accts = xpath_eval(node, 'a:account',{'a':DIGSBY_ACCOUNTS_NS})
     orders = xpath_eval(node, 'a:order',{'a':DIGSBY_ACCOUNTS_NS})
     self.order = [ord(c) for c in
              base64.decodestring(orders[0].getContent())] if orders else []
     self.extend(Account(acct) for acct in accts)
Beispiel #36
0
    def get_pubsub_ns(self):
        """Get a namespace of the stanza payload.

        :return: XML namespace URI of the payload or None if there is no
            payload.
        :returntype: `str`"""
        q=self.get_query()
        if q:
            return get_node_ns_uri(q)
        else:
            return None
Beispiel #37
0
 def from_xml(self, xmlnode):
     self.xml_element_name = xmlnode.name
     if xmlnode.type != "element":
         raise ValueError, "XML node is not a chat state (not en element)"
     ns = get_node_ns_uri(xmlnode)
     if ns and ns != self.xml_element_namespace:
         raise ValueError, "XML node is not a chat state descriptor"
     if not self.valid_state():
         import warnings
         warnings.warn("XML node with name: %r is not a valid chat state" %
                       self.xml_element_name)
Beispiel #38
0
    def get_query_ns(self):
        """Get a namespace of the stanza payload.

        :return: XML namespace URI of the payload or None if there is no
            payload.
        :returntype: `str`"""
        q = self.get_query()
        if q:
            return get_node_ns_uri(q)
        else:
            return None
Beispiel #39
0
 def __from_xml(self, node):
     if node.type!="element":
         raise ValueError,"XML node is not an si (not en element)"
     ns=get_node_ns_uri(node)
     if ns and ns!=SI_NS or node.name!=self.xml_element_name:
         raise ValueError,"XML node is not an si"
     sid       = node.prop("id")
     self.sid  = to_utf8(sid) if sid else None
     mime_type = node.prop("mime-type")
     self.mime_type  = to_utf8(mime_type) if mime_type else None
     profile_ns   = node.prop("profile")
     self.profile_ns  = to_utf8(profile_ns) if profile_ns else None
Beispiel #40
0
def get_delays(stanza):
    """Get jabber:x:delay elements from the stanza.

    :Parameters:
        - `stanza`: a, probably delayed, stanza.
    :Types:
        - `stanza`: `pyxmpp.stanza.Stanza`

    :return: list of delay tags sorted by the timestamp.
    :returntype: `list` of `Delay`"""
    delays = []
    n = stanza.xmlnode.children
    while n:
        if n.type == "element":
            if get_node_ns_uri(n) == DELAY_NS and n.name == "delay":
                delays.append(Delay(n))
            elif get_node_ns_uri(n) == LEGACY_DELAY_NS and n.name == "x":
                delays.append(LegacyDelay(n))
        n = n.next
    delays.sort()
    return delays
Beispiel #41
0
 def __from_xml(self, node):
     if node.type != "element":
         raise ValueError, "XML node is not an si (not en element)"
     ns = get_node_ns_uri(node)
     if ns and ns != SI_NS or node.name != self.xml_element_name:
         raise ValueError, "XML node is not an si"
     sid = node.prop("id")
     self.sid = to_utf8(sid) if sid else None
     mime_type = node.prop("mime-type")
     self.mime_type = to_utf8(mime_type) if mime_type else None
     profile_ns = node.prop("profile")
     self.profile_ns = to_utf8(profile_ns) if profile_ns else None
Beispiel #42
0
def get_delays(stanza):
    """Get jabber:x:delay elements from the stanza.

    :Parameters:
        - `stanza`: a, probably delayed, stanza.
    :Types:
        - `stanza`: `pyxmpp.stanza.Stanza`

    :return: list of delay tags sorted by the timestamp.
    :returntype: `list` of `Delay`"""
    delays=[]
    n=stanza.xmlnode.children
    while n:
        if n.type=="element":
            if get_node_ns_uri(n) == DELAY_NS and n.name == "delay":
                delays.append(Delay(n))
            elif get_node_ns_uri(n) == LEGACY_DELAY_NS and n.name == "x":
                delays.append(LegacyDelay(n))
        n=n.next
    delays.sort()
    return delays
Beispiel #43
0
    def __from_xml(self, xmlnode):
        """Initialize ListItem from XML node."""
        if xmlnode.type != "element":
            raise ValueError, "XML node is not a list item (not en element)"
        ns = get_node_ns_uri(xmlnode)
        if ns and ns != PRIVACY_NS or xmlnode.name != "item":
            raise ValueError, "XML node is not a list item"

        [setattr(self, x, xmlnode.prop(x)) for x in PRIVACY_ATTRS]
        self.order = int(self.order) if self.order else 0

        n = xmlnode.children
        while n:
            if n.type != "element":
                n = n.next
                continue
            ns = get_node_ns_uri(n)
            if ns and ns != PRIVACY_NS or n.name not in PRIVACY_TYPES:
                n = n.next
                continue
            setattr(self, util.pythonize(n.name), True)
            n = n.next
Beispiel #44
0
    def __from_xml(self, xmlnode):
        """Initialize ListItem from XML node."""
        if xmlnode.type!="element":
            raise ValueError,"XML node is not a list item (not en element)"
        ns=get_node_ns_uri(xmlnode)
        if ns and ns!=PRIVACY_NS or xmlnode.name!="item":
            raise ValueError,"XML node is not a list item"

        [setattr(self, x, xmlnode.prop(x)) for x in PRIVACY_ATTRS]
        self.order = int(self.order) if self.order else 0

        n=xmlnode.children
        while n:
            if n.type!="element":
                n=n.next
                continue
            ns=get_node_ns_uri(n)
            if ns and ns!=PRIVACY_NS or n.name not in PRIVACY_TYPES:
                n=n.next
                continue
            setattr(self, util.pythonize(n.name), True)
            n=n.next
Beispiel #45
0
 def __from_xml(self, xmlnode):
     if xmlnode.type!="element":
         raise ValueError,"XML node is not a Privacy (not en element)"
     ns=get_node_ns_uri(xmlnode)
     if ns and ns!=PRIVACY_NS or xmlnode.name!="query":
         raise ValueError,"XML node is not a query"
     n=xmlnode.children
     while n:
         if n.type!="element":
             n=n.next
             continue
         ns=get_node_ns_uri(n)
         if ns and ns!=PRIVACY_NS or n.name not in ("active", "default", "list"):
             n=n.next
             continue
         if n.name == "active":
             self.active = n.prop("name")if libxml2.xmlNode.hasProp("name") else None
         elif n.name == "default":
             self.default = n.prop("default")if libxml2.xmlNode.hasProp("default") else None
         else:
             self.append(List(n))
         n = n.next
Beispiel #46
0
    def __from_xml(self, node):
        '''A libxml2 node to a digsby.account'''

        if node.type!="element":
            raise ValueError,"XML node is not an account element (not en element)"
        ns = get_node_ns_uri(node)
        if ns and ns != DIGSBY_ACCOUNTS_NS or node.name != "account":
            raise ValueError,"XML node is not an account element"
        id = node.prop("id")
        self.id = int(from_utf8(id)) if id else None

        username = node.prop("username")
        self.username = from_utf8(username) if username else None

        protocol = node.prop("protocol")
        self.protocol = from_utf8(protocol) if protocol else None

        self.protocol = fix_truncated(self.protocol)

        password = node.prop("password")
        self.password = base64.b64decode(password) if password else None

        action = node.prop("action")
        self.action = from_utf8(action) if action else None

        self.data = None
        n=node.children
        while n:
            if n.type!="element":
                n = n.next
                continue
            ns = get_node_ns_uri(n)
            if ns and ns!=DIGSBY_ACCOUNTS_NS:
                n=n.next
                continue
            if n.name=="data":
                self.data=base64.decodestring(n.getContent())
            n = n.next
Beispiel #47
0
    def __from_xml(self, node):
        if node.type != "element":
            raise ValueError, "XML node is not a %s element (not en element)" % self.xml_element_name
        ns = get_node_ns_uri(node)
        if ns and ns != self.xml_element_namespace or node.name != self.xml_element_name:
            raise ValueError, "XML node is not an %s element" % self.xml_element_name

        self.name = from_utf8(node.prop("name"))
        self.address = from_utf8(node.prop("address"))

        originator = node.prop("originator")
        self.originator = int(from_utf8(originator)) if originator else 0

        unread = node.prop("unread")
        self.unread = int(from_utf8(unread)) if unread else 0
Beispiel #48
0
    def from_xml(self, xmlnode):
        if xmlnode.type != "element":
            raise ValueError, "XML node is not a photo (not en element)"
        ns = get_node_ns_uri(xmlnode)
        if ns and ns != self.xml_element_namespace or xmlnode.name != self.xml_element_name:
            raise ValueError, "XML node is not a %s descriptor" % self.xml_element_name

        from_ = xmlnode.prop("from")
        self.from_ = to_utf8(from_) if from_ else None

        timestamp = xmlnode.prop("stamp")
        try:
            self.timestamp = datetime.strptime(timestamp, DELAY_TIME_FORMAT)
        except Exception:
            self.timestamp = None
Beispiel #49
0
 def from_xml(self,node):
     """Initialize File from XML node."""
     if node.type!="element":
         raise ValueError,"XML node is not a file element (not en element)"
     ns=get_node_ns_uri(node)
     if ns and ns!=SI_FILETRANSFER_NS or node.name!="file":
         raise ValueError,"XML node is not a file element"
     name = node.prop("name")
     self.name = from_utf8(name) if name else None
     size = node.prop("size")
     self.size = int(from_utf8(size)) if size else None
     hash = node.prop("hash")
     date = node.prop("date")
     self.hash = from_utf8(hash) if hash else None
     self.date = from_utf8(date) if date else None
     desc = None
     length = None
     offset = None
     n=node.children
     while n:
         if n.type!="element":
             n=n.next
             continue
         ns=get_node_ns_uri(n)
         if ns and ns!=SI_FILETRANSFER_NS:
             n=n.next
             continue
         if n.name =="desc":
             desc = n.getContent()
         elif n.name == "range":
             offset = n.prop("offset")
             length = n.prop("length")
         n=n.next
     self.desc   = from_utf8(desc)        if desc   else None
     self.offset = int(from_utf8(offset)) if offset else None
     self.length = int(from_utf8(length)) if length else None
Beispiel #50
0
    def __from_xml(self,node):
        if node.type!="element":
            raise ValueError,"XML node is not a %s element (not en element)" % self.xml_element_name
        ns=get_node_ns_uri(node)
        if ns and ns!=self.xml_element_namespace or node.name!=self.xml_element_name:
            raise ValueError,"XML node is not an %s element" % self.xml_element_name

        self.name = from_utf8(node.prop("name"))
        self.address = from_utf8(node.prop("address"))

        originator = node.prop("originator")
        self.originator = int(from_utf8(originator)) if originator else 0

        unread = node.prop("unread")
        self.unread = int(from_utf8(unread)) if unread else 0
Beispiel #51
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))
Beispiel #52
0
 def __from_xml(self, node):
     '''A libxml2 node to a digsby.action'''
     if node.type!="element":
         raise ValueError,"XML node is not an action element (not en element)"
     ns = get_node_ns_uri(node)
     if ns and ns != DIGSBY_STATS_COUNTER_NS or node.name != "action":
         raise ValueError,"XML node is not an action element"
     type = node.prop("type")
     self.type = from_utf8(type) if type is not None else None
     initial = node.prop("initial")
     self.initial = int(from_utf8(initial)) if initial is not None else None
     value = node.prop("value")
     self.value = int(from_utf8(value)) if value is not None else None
     result = node.prop("result")
     self.result = int(from_utf8(result)) if result is not None else None
Beispiel #53
0
    def from_xml(self, xmlnode):
        if xmlnode.type!="element":
            raise ValueError,"XML node is not a photo (not en element)"
        ns=get_node_ns_uri(xmlnode)
        if ns and ns!=self.xml_element_namespace or xmlnode.name!=self.xml_element_name:
            raise ValueError,"XML node is not a %s descriptor" % self.xml_element_name

        from_       = xmlnode.prop("from")
        self.from_  = to_utf8(from_) if from_ else None

        timestamp   = xmlnode.prop("stamp")
        try:
            self.timestamp = datetime.strptime(timestamp, DELAY_TIME_FORMAT)
        except Exception:
            self.timestamp = None
Beispiel #54
0
 def from_xml(self, node):
     """Initialize File from XML node."""
     if node.type != "element":
         raise ValueError, "XML node is not a file element (not en element)"
     ns = get_node_ns_uri(node)
     if ns and ns != SI_FILETRANSFER_NS or node.name != "file":
         raise ValueError, "XML node is not a file element"
     name = node.prop("name")
     self.name = from_utf8(name) if name else None
     size = node.prop("size")
     self.size = int(from_utf8(size)) if size else None
     hash = node.prop("hash")
     date = node.prop("date")
     self.hash = from_utf8(hash) if hash else None
     self.date = from_utf8(date) if date else None
     desc = None
     length = None
     offset = None
     n = node.children
     while n:
         if n.type != "element":
             n = n.next
             continue
         ns = get_node_ns_uri(n)
         if ns and ns != SI_FILETRANSFER_NS:
             n = n.next
             continue
         if n.name == "desc":
             desc = n.getContent()
         elif n.name == "range":
             offset = n.prop("offset")
             length = n.prop("length")
         n = n.next
     self.desc = from_utf8(desc) if desc else None
     self.offset = int(from_utf8(offset)) if offset else None
     self.length = int(from_utf8(length)) if length else None
Beispiel #55
0
    def __from_xml(self, node):
        if node.type!="element":
            raise ValueError,"XML node is not a %s element (not en element)" % self.xml_element_name
        ns=get_node_ns_uri(node)
        if ns and ns!=self.xml_element_namespace or node.name!=self.xml_element_name:
            raise ValueError,"XML node is not an %s element" % self.xml_element_name

        self.result_time = int(from_utf8(node.prop("result-time")))
        self.total_matched = int(from_utf8(node.prop("total-matched")))
        self.url = from_utf8(node.prop("url"))

        total_estimate = node.prop("messages")
        self.total_estimate = int(from_utf8(total_estimate)) if total_estimate else 0

        threads = xpath_eval(node, 'g:mail-thread-info',{'g':GOOGLE_MAIL_NOTIFY_NS})
        self.extend(MailThreadInfo(thread) for thread in threads)
Beispiel #56
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))
Beispiel #57
0
    def __from_xml(self, node):
        if node.type != "element":
            raise ValueError, "XML node is not a geoip (not en element)"
        ns = get_node_ns_uri(node)
        if ns and ns != DIGSBY_GEOIP_NS or node.name != self.xml_element_name:
            raise ValueError, "XML node is not a geoip"

        for fields, convert in [(self.strfields, from_utf8),
                                (self.decfields, Decimal)]:
            for field in fields:
                val = None
                try:
                    val2 = node.prop(field)
                    val = convert(val2) if val2 else None
                except Exception:
                    traceback.print_exc()
                setattr(self, field, val)