Пример #1
0
    def process_data(self, data):
        # Parse incoming data

        data = self.replace_non_character(data)

        if self._client.is_websocket:
            stanza = Node(node=data)
            if is_websocket_stream_error(stanza):
                for tag in stanza.getChildren():
                    name = tag.getName()
                    if (name != 'text'
                            and tag.getNamespace() == Namespace.XMPP_STREAMS):
                        self._websocket_stream_error = name

            elif is_websocket_close(stanza):
                self._log.info('Stream <close> received')
                self.notify('stream-end', self._websocket_stream_error)
                return

            self.dispatch(stanza)
            return

        try:
            self._parser.Parse(data)
        except (ExpatError, ValueError) as error:
            self._log.error('XML parsing error: %s', error)
            self.notify('parsing-error', error)
            return

        # end stream:stream tag received
        if self._parser.has_received_endtag():
            self._log.info('End of stream: %s', self._parser.streamError)
            self.notify('stream-end', self._parser.streamError)
            return
Пример #2
0
 def to_node(self):
     node = Node(self.name)
     if self.parameters:
         node.addChild(node=make_parameters(self.parameters))
     add_children(node, 'surname', self.surname)
     add_children(node, 'given', self.given)
     add_children(node, 'additional', self.additional)
     add_children(node, 'prefix', self.prefix)
     add_children(node, 'suffix', self.suffix)
     return node
Пример #3
0
 def to_node(self):
     vcard = Node(f'{Namespace.VCARD4} vcard')
     for group, props in self._properties:
         if group is None:
             vcard.addChild(node=props.to_node())
         else:
             group = Node(group)
             for prop in props:
                 group.addChild(node=prop.to_node())
             vcard.addChild(node=group)
     return vcard
Пример #4
0
 def to_node(self):
     node = Node(self.name)
     if self.parameters:
         node.addChild(node=make_parameters(self.parameters))
     add_children(node, 'pobox', self.pobox)
     add_children(node, 'ext', self.ext)
     add_children(node, 'street', self.street)
     add_children(node, 'locality', self.locality)
     add_children(node, 'region', self.region)
     add_children(node, 'code', self.code)
     add_children(node, 'country', self.country)
     return node
Пример #5
0
    def __init__(self, device_list):
        id_ = gajim.get_an_id()
        attrs = {'id': id_}
        Iq.__init__(self, typ='set', attrs=attrs)

        list_node = Node('list')
        for device in device_list:
            list_node.addChild('device').setAttr('id', device)

        publish = PublishNode(NS_DEVICE_LIST, list_node)
        pubsub = PubsubNode(publish)

        self.addChild(node=pubsub)
Пример #6
0
    def __init__(self, device_list):
        id_ = gajim.get_an_id()
        attrs = {'id': id_}
        Iq.__init__(self, typ='set', attrs=attrs)

        list_node = Node('list')
        for device in device_list:
            list_node.addChild('device').setAttr('id', device)

        publish = PublishNode(NS_DEVICE_LIST, list_node)
        pubsub = PubsubNode(publish)

        self.addChild(node=pubsub)
Пример #7
0
 def to_node(self):
     node = Node(self.name)
     if self.parameters:
         node.addChild(node=make_parameters(self.parameters))
     node.addChild('sourceid', payload=self.sourceid)
     node.addChild('uri', payload=self.uri)
     return node
Пример #8
0
    def __init__(self, device_list):
        assert isinstance(device_list, list)
        assert len(device_list) > 0
        id_ = gajim.get_an_id()
        attrs = {'id': id_}
        Iq.__init__(self, typ='set', attrs=attrs)

        list_node = Node('list', attrs={'xmlns': NS_DEVICE_LIST})
        for device in device_list:
            list_node.addChild('device').setAttr('id', device)

        publish = PublishNode(NS_DEVICE_LIST, list_node)
        pubsub = PubsubNode(publish)

        self.addChild(node=pubsub)
Пример #9
0
    def __init__(self, device_list):
        assert isinstance(device_list, list)
        assert len(device_list) > 0
        id_ = gajim.get_an_id()
        attrs = {'id': id_}
        Iq.__init__(self, typ='set', attrs=attrs)

        list_node = Node('list', attrs={'xmlns': NS_OMEMO})
        for device in device_list:
            list_node.addChild('device').setAttr('id', device)

        publish = PublishNode(NS_DEVICE_LIST, list_node)
        pubsub = PubsubNode(publish)

        self.addChild(node=pubsub)
Пример #10
0
def _make_delete(jid):
    return Iq('set',
              Namespace.ROSTER,
              payload=[Node('item', {
                  'jid': jid,
                  'subscription': 'remove'
              })])
Пример #11
0
def make_cancel_config_request(room_jid):
    cancel = Node(tag='x', attrs={'xmlns': Namespace.DATA, 'type': 'cancel'})
    iq = Iq(typ='set',
            queryNS=Namespace.MUC_OWNER,
            payload=cancel,
            to=room_jid)
    return iq
Пример #12
0
 def __init__(self, contact_jid, device_id):
     id_ = gajim.get_an_id()
     attrs = {'id': id_}
     Iq.__init__(self, typ='get', attrs=attrs, to=contact_jid)
     items = Node('items', attrs={'node': NS_BUNDLES + str(device_id)})
     pubsub = PubsubNode(items)
     self.addChild(node=pubsub)
Пример #13
0
 def __init__(self, contact_jid,):
     id_ = gajim.get_an_id()
     attrs = {'id': id_}
     Iq.__init__(self, typ='get', attrs=attrs, to=contact_jid)
     items = Node('items', attrs={'node': NS_DEVICE_LIST, 'max_items': 1})
     pubsub = PubsubNode(items)
     self.addChild(node=pubsub)
Пример #14
0
    def handle_outgoing_stanza(self, event):
        try:
            if not event.msg_iq.getTag('body'):
                return

            account = event.conn.name
            state = self.get_omemo_state(account)
            full_jid = str(event.msg_iq.getAttr('to'))
            to_jid = gajim.get_jid_without_resource(full_jid)
            if not state.encryption.is_active(to_jid):
                return

            # Delete previous Message out of Correction Message Stanza
            if event.msg_iq.getTag('replace', namespace=NS_CORRECT):
                event.msg_iq.delChild('encrypted')

            plaintext = event.msg_iq.getBody().encode('utf8')

            msg_dict = state.create_msg(gajim.get_jid_from_account(account),
                                        to_jid, plaintext)
            if not msg_dict:
                return True

            encrypted_node = OmemoMessage(msg_dict)
            event.msg_iq.delChild('body')
            event.msg_iq.addChild(node=encrypted_node)
            store = Node('store', attrs={'xmlns': NS_HINTS})
            event.msg_iq.addChild(node=store)
            self.print_msg_to_log(event.msg_iq)
        except Exception as e:
            log.debug(e)
            return True
Пример #15
0
 def to_node(self):
     node = Node(self.name)
     if self.parameters:
         node.addChild(node=make_parameters(self.parameters))
     if self.sex:
         node.addChild('sex', payload=self.sex)
     if self.identity:
         node.addChild('identity', payload=self.identity)
     return node
Пример #16
0
    def __init__(self, msg_dict):
        # , contact_jid, key, iv, payload, dev_id, my_dev_id):
        Node.__init__(self, 'encrypted', attrs={'xmlns': NS_OMEMO})
        header = Node('header', attrs={'sid': msg_dict['sid']})
        for rid, key in msg_dict['keys'].items():
            header.addChild('key', attrs={'rid': rid}).addData(b64encode(key))

        header.addChild('iv').addData(b64encode(msg_dict['iv']))
        self.addChild(node=header)
        self.addChild('payload').addData(b64encode(msg_dict['payload']))
Пример #17
0
    def make_bundle_node(self, state_bundle):
        result = Node('bundle', attrs={'xmlns': NS_OMEMO})
        prekey_pub_node = result.addChild(
            'signedPreKeyPublic',
            attrs={'signedPreKeyId': state_bundle['signedPreKeyId']})
        prekey_pub_node.addData(state_bundle['signedPreKeyPublic'])

        prekey_sig_node = result.addChild('signedPreKeySignature')
        prekey_sig_node.addData(state_bundle['signedPreKeySignature'])

        identity_key_node = result.addChild('identityKey')
        identity_key_node.addData(state_bundle['identityKey'])
        prekeys = result.addChild('prekeys')

        for key in state_bundle['prekeys']:
            prekeys.addChild('preKeyPublic',
                             attrs={'preKeyId': key[0]}).addData(key[1])
        return result
Пример #18
0
    def to_node(self):
        displaymarking = Node(tag='displaymarking')
        if self.fgcolor and self.fgcolor != '#000':
            displaymarking.setAttr('fgcolor', self.fgcolor)

        if self.bgcolor and self.bgcolor != '#FFF':
            displaymarking.setAttr('bgcolor', self.bgcolor)

        if self.name:
            displaymarking.setData(self.name)

        return displaymarking
Пример #19
0
    def send_enable(self):
        if not self.sm_supported:
            return

        if self._client.sm_disabled:
            return

        enable = Node(Namespace.STREAM_MGMT + ' enable',
                      attrs={'resume': 'true'})
        self._client.send_nonza(enable, now=False)
        self._log.debug('Send enable')
        self._enable_sent = True
Пример #20
0
    def __init__(self, msg_dict):
        # , contact_jid, key, iv, payload, dev_id, my_dev_id):
        Node.__init__(self, 'encrypted', attrs={'xmlns': NS_OMEMO})
        header = Node('header', attrs={'sid': msg_dict['sid']})
        for rid, key in msg_dict['keys'].items():
            header.addChild('key', attrs={'rid': rid}).addData(b64encode(key))

        header.addChild('iv').addData(b64encode(msg_dict['iv']))
        self.addChild(node=header)
        self.addChild('payload').addData(b64encode(msg_dict['payload']))
Пример #21
0
 def to_node(self):
     security = Node(tag='securitylabel',
                     attrs={'xmlns': Namespace.SECLABEL})
     if self.displaymarking is not None:
         security.addChild(node=self.displaymarking.to_node())
     security.addChild(node=self.label)
     return security
Пример #22
0
    def resume_request(self):
        if self._session_id is None:
            self._log.error('Attempted to resume without a valid session id')
            return

        # Save old messages in an extra "queue" to avoid race conditions
        # and to make it possible to replay stanzas even when resuming fails
        # Add messages here (instead of overwriting) so that repeated
        # connection errors don't delete unacked stanzas
        # (uqueue should be empty in this case anyways)
        self._old_uqueue += self._uqueue
        self._uqueue = []

        resume = Node(Namespace.STREAM_MGMT + ' resume',
                      attrs={
                          'h': self._in_h,
                          'previd': self._session_id
                      })

        self._acked_h = self._in_h
        self.resume_in_progress = True
        self._client.send_nonza(resume, now=False)
Пример #23
0
 def to_node(self):
     vcard = Node(tag='vCard', attrs={'xmlns': Namespace.VCARD})
     for i in self.data:
         if i == 'jid':
             continue
         if isinstance(self.data[i], dict):
             child = vcard.addChild(i)
             for j in self.data[i]:
                 child.addChild(j).setData(self.data[i][j])
         elif isinstance(self.data[i], list):
             for j in self.data[i]:
                 child = vcard.addChild(i)
                 for k in j:
                     child.addChild(k).setData(j[k])
         else:
             vcard.addChild(i).setData(self.data[i])
     return vcard
Пример #24
0
    def make_bundle_node(self, state_bundle):
        result = Node('bundle', attrs={'xmlns': NS_OMEMO})
        prekey_pub_node = result.addChild(
            'signedPreKeyPublic',
            attrs={'signedPreKeyId': state_bundle['signedPreKeyId']})
        prekey_pub_node.addData(state_bundle['signedPreKeyPublic'])

        prekey_sig_node = result.addChild('signedPreKeySignature')
        prekey_sig_node.addData(state_bundle['signedPreKeySignature'])

        identity_key_node = result.addChild('identityKey')
        identity_key_node.addData(state_bundle['identityKey'])
        prekeys = result.addChild('prekeys')

        for key in state_bundle['prekeys']:
            prekeys.addChild('preKeyPublic',
                             attrs={'preKeyId': key[0]}).addData(key[1])
        return result
Пример #25
0
    def handle_outgoing_gc_stanza(self, event):
        """ Manipulates the outgoing groupchat stanza

            The body is getting encrypted

            Parameters
            ----------
            event : StanzaMessageOutgoingEvent

            Returns
            -------
            Return if encryption is not activated or any other
            exception or error occurs
        """
        account = event.conn.name
        if account in self.disabled_accounts:
            return
        try:
            # If we send a correction msg, the stanza is saved
            # in correction_msg
            if event.correction_msg:
                event.msg_iq = event.correction_msg
            if not event.msg_iq.getTag('body'):
                return
            state = self.get_omemo_state(account)
            full_jid = str(event.msg_iq.getAttr('to'))
            to_jid = gajim.get_jid_without_resource(full_jid)
            if to_jid not in self.groupchat:
                return
            if not state.encryption.is_active(to_jid):
                return
            # Delete previous Message out of Correction Message Stanza
            if event.msg_iq.getTag('replace', namespace=NS_CORRECT):
                event.msg_iq.delChild('encrypted', attrs={'xmlns': NS_OMEMO})

            plaintext = event.msg_iq.getBody()
            msg_dict = state.create_gc_msg(
                gajim.get_jid_from_account(account),
                to_jid,
                plaintext.encode('utf8'))
            if not msg_dict:
                return True

            self.gc_message[msg_dict['payload']] = plaintext
            encrypted_node = OmemoMessage(msg_dict)
            event.msg_iq.delChild('body')
            event.msg_iq.addChild(node=encrypted_node)

            # XEP-xxxx: Explicit Message Encryption
            if not event.msg_iq.getTag('encrypted', attrs={'xmlns': NS_EME}):
                eme_node = Node('encrypted', attrs={'xmlns': NS_EME,
                                                    'name': 'OMEMO',
                                                    'namespace': NS_OMEMO})
                event.msg_iq.addChild(node=eme_node)

            # Add Message for devices that dont support OMEMO
            support_msg = 'You received a message encrypted with ' \
                          'OMEMO but your client doesnt support OMEMO.'
            event.msg_iq.setBody(support_msg)

            # Store Hint for MAM
            store = Node('store', attrs={'xmlns': NS_HINTS})
            event.msg_iq.addChild(node=store)
            if event.correction_msg:
                event.correction_msg = event.msg_iq
                event.msg_iq = None
                self.print_msg_to_log(event.correction_msg)
            else:
                self.print_msg_to_log(event.msg_iq)
        except Exception as e:
            log.debug(e)
            return True
Пример #26
0
 def __init__(self, data):
     assert data is Node
     Node.__init__(self, tag='pubsub', attrs={'xmlns': NS_PUBSUB})
     self.addChild(node=data)
Пример #27
0
 def __init__(self, data):
     assert isinstance(data, Node)
     Node.__init__(self, tag='pubsub', attrs={'xmlns': NS_PUBSUB})
     self.addChild(node=data)
Пример #28
0
    def handle_outgoing_stanza(self, event):
        """ Manipulates the outgoing stanza

            The body is getting encrypted

            Parameters
            ----------
            event : StanzaMessageOutgoingEvent

            Returns
            -------
            Return if encryption is not activated or any other
            exception or error occurs
        """
        account = event.conn.name
        if account in self.disabled_accounts:
            return
        try:
            if not event.msg_iq.getTag('body'):
                return

            state = self.get_omemo_state(account)
            full_jid = str(event.msg_iq.getAttr('to'))
            to_jid = gajim.get_jid_without_resource(full_jid)
            if not state.encryption.is_active(to_jid):
                return

            # Delete previous Message out of Correction Message Stanza
            if event.msg_iq.getTag('replace', namespace=NS_CORRECT):
                event.msg_iq.delChild('encrypted', attrs={'xmlns': NS_OMEMO})

            plaintext = event.msg_iq.getBody().encode('utf8')

            msg_dict = state.create_msg(
                gajim.get_jid_from_account(account), to_jid, plaintext)
            if not msg_dict:
                return True

            encrypted_node = OmemoMessage(msg_dict)

            # Check if non-OMEMO resource is online
            contacts = gajim.contacts.get_contacts(account, to_jid)
            non_omemo_resource_online = False
            for contact in contacts:
                if contact.show == 'offline':
                    continue
                if not contact.supports(NS_NOTIFY):
                    log.debug(contact.get_full_jid() +
                              ' => Contact doesnt support OMEMO, '
                              'adding Info Message to Body')
                    support_msg = 'You received a message encrypted with ' \
                                  'OMEMO but your client doesnt support OMEMO.'
                    event.msg_iq.setBody(support_msg)
                    non_omemo_resource_online = True
            if not non_omemo_resource_online:
                event.msg_iq.delChild('body')

            event.msg_iq.addChild(node=encrypted_node)

            # XEP-xxxx: Explicit Message Encryption
            if not event.msg_iq.getTag('encrypted', attrs={'xmlns': NS_EME}):
                eme_node = Node('encrypted', attrs={'xmlns': NS_EME,
                                                    'name': 'OMEMO',
                                                    'namespace': NS_OMEMO})
                event.msg_iq.addChild(node=eme_node)

            # Store Hint for MAM
            store = Node('store', attrs={'xmlns': NS_HINTS})
            event.msg_iq.addChild(node=store)
            self.print_msg_to_log(event.msg_iq)
        except Exception as e:
            log.debug(e)
            return True
Пример #29
0
 def to_node(self):
     node = Node(self.name)
     if self.parameters:
         node.addChild(node=make_parameters(self.parameters))
     node.addChild('language-tag', payload=self.value)
     return node
Пример #30
0
 def __init__(self, node_str, data):
     assert node_str is not None and isinstance(data, Node)
     Node.__init__(self, tag='publish', attrs={'node': node_str})
     self.addChild('item').addChild(node=data)
Пример #31
0
 def _send_ack(self, *args):
     ack = Node(Namespace.STREAM_MGMT + ' a', attrs={'h': self._in_h})
     self._acked_h = self._in_h
     self._log.debug('Send ack, h: %s', self._in_h)
     self._client.send_nonza(ack, now=False)
Пример #32
0
 def _request_ack(self):
     request = Node(Namespace.STREAM_MGMT + ' r')
     self._log.debug('Request ack')
     self._client.send_nonza(request, now=False)
Пример #33
0
 def __init__(self, data):
     assert isinstance(data, Node)
     Node.__init__(self, tag='pubsub', attrs={'xmlns': NS_PUBSUB})
     self.addChild(node=data)
Пример #34
0
 def to_node(self):
     node = Node(self.name)
     if self.parameters:
         node.addChild(node=make_parameters(self.parameters))
     add_children(node, 'text', self.values)
     return node
Пример #35
0
 def __init__(self, data):
     assert data is Node
     Node.__init__(self, tag='pubsub', attrs={'xmlns': NS_PUBSUB})
     self.addChild(node=data)
Пример #36
0
 def __init__(self, node_str, data):
     assert node_str is not None and data is Node
     Node.__init__(self, tag='publish', attrs={'node': node_str})
     self.addChild('item').addChild(node=data)
Пример #37
0
 def to_node(self):
     node = Node(self.name)
     if self.parameters:
         node.addChild(node=make_parameters(self.parameters))
     node.addChild('timestamp', payload=self.value)
     return node