Ejemplo n.º 1
0
    def set_room(self, room_id):
        try:
            from matrix_client.room import Room
        except ModuleNotFoundError:
            raise ModuleNotFoundError("No matrix client module found, please run j.clients.matrix.install() first")

        self.room = Room(client=self.client, room_id=room_id)
Ejemplo n.º 2
0
def make_message(convert_to_hex: bool = False, overwrite_data=None):
    from matrix_client.room import Room
    room = Room(None, '!roomID:server')
    if not overwrite_data:
        message = SecretRequest(
            message_identifier=random.randint(0, UINT64_MAX),
            payment_identifier=1,
            secrethash=UNIT_SECRETHASH,
            amount=1,
            expiration=10,
        )
        message.sign(HOP1_KEY)
        data = message.encode()
        if convert_to_hex:
            data = '0x' + data.hex()
        else:
            data = json.dumps(message.to_dict())
    else:
        data = overwrite_data

    event = dict(
        type='m.room.message',
        sender=USERID1,
        content={
            'msgtype': 'm.text',
            'body': data,
        },
    )
    return room, event
Ejemplo n.º 3
0
 def is_room_member(self, room_id, user_id):
     try:
         r = Room(self.client, room_id)
         return user_id in list(r.get_joined_members().keys())
     except Exception as e:
         return False
     return False
Ejemplo n.º 4
0
 def is_room_member(self, room_id, user_id):
     try:
         r = Room(self.client, room_id)
         room_members = [m.user_id for m in r.get_joined_members()]
         return user_id in room_members
     except Exception as e:
         self.logger.error("Error when fetching room members: %s" % e)
         return False
     return False
Ejemplo n.º 5
0
 def eventCallback(self, event):
     if 'type' in event and 'room_id' in event and 'content' in event and event[
             'type'] == 'm.room.message':
         room = Room(self.client, event['room_id'])
         self.messageReceived.emit(room, event['sender'], event['content'],
                                   time.time() - event['unsigned']['age'])
     if 'type' in event and 'room_id' in event and 'content' in event and event[
             'type'] == 'm.room.canonical_alias':
         self.roomUpdated.emit(event['room_id'], 'canonical_alias',
                               event['content']['alias'])
Ejemplo n.º 6
0
 def __init__(self):
     self.BOTUSERNAME = "******"
     self.BOTPASSWORD = "******"
     self.BOTSERVO = "matrix.org"
     self.RID = "!RnpiUpFIsfzZfHdHQf:matrix.org"
     self.realRID = "!obQcCWaLRAUgiGBvMg:postmarketos.org"
     self.MainClient = MatrixClient("https://" + self.BOTSERVO)
     self.token = self.MainClient.login_with_password(
         username=self.BOTUSERNAME, password=self.BOTPASSWORD)
     self.APIWrapper = MatrixHttpApi("https://" + self.BOTSERVO,
                                     token=self.token)
     self.target_room = Room(self.MainClient, self.RID)
     print("ready")
Ejemplo n.º 7
0
    def test_megolm_outbound_persistence(self, device):
        session = MegolmOutboundSession(max_messages=2, max_age=100000)
        session.message_count = 1
        session.add_device(self.device_id)
        sessions = {}

        self.store.load_outbound_sessions(sessions)
        assert not sessions
        assert not self.store.get_outbound_session(self.room_id)

        self.store.save_outbound_session(self.room_id, session)
        self.store.save_megolm_outbound_devices(self.room_id, {self.device_id})
        self.store.load_outbound_sessions(sessions)
        assert sessions[self.room_id].id == session.id
        assert sessions[self.room_id].devices == session.devices
        assert sessions[self.room_id].creation_time == session.creation_time
        assert sessions[self.room_id].max_messages == session.max_messages
        assert sessions[self.room_id].message_count == session.message_count
        assert sessions[self.room_id].max_age == session.max_age

        saved_session = self.store.get_outbound_session(self.room_id)
        assert saved_session.id == session.id
        assert saved_session.devices == session.devices
        assert saved_session.creation_time == session.creation_time
        assert saved_session.max_messages == session.max_messages
        assert saved_session.message_count == session.message_count
        assert saved_session.max_age == session.max_age

        sessions.clear()
        saved_session = self.store.get_outbound_session(self.room_id, sessions)
        assert sessions[self.room_id].id == session.id

        self.store.remove_outbound_session(self.room_id)
        assert not self.store.get_outbound_session(self.room_id)

        self.store.save_outbound_session(self.room_id, session)
        saved_session = self.store.get_outbound_session(self.room_id)
        # Verify the saved devices have been erased with the session
        assert not saved_session.devices

        room = Room(None, self.room_id)
        with pytest.raises(AttributeError):
            device.megolm_build_encrypted_event(room, {})
        assert device.megolm_outbound_sessions[self.room_id].id == session.id

        self.store.remove_olm_account()
        assert not self.store.get_outbound_session(self.room_id)
Ejemplo n.º 8
0
 def send_message(self,
                  room_id: str,
                  text: str,
                  name: str = None,
                  avatar_url: str = None,
                  file_url: str = None,
                  file_name: str = None,
                  file_mimetype: str = None,
                  file_authorization: str = None):
     room = Room(self._client, room_id)
     current_avatar_url = None
     current_name = None
     avatar_uri = None
     if room_id in self._cache['rooms']:
         current_name = self._cache['rooms'][room_id]['name']
         current_avatar_url = self._cache['rooms'][room_id]['avatar_url']
     else:
         self._cache['rooms'][room_id] = {}
     if avatar_url is not None and avatar_url != current_avatar_url:
         if avatar_url in self._cache['uploaded_avatars']:
             avatar_uri = self._cache['uploaded_avatars'][avatar_url]
             print("Use cache avatar for an user " + avatar_uri + " (" +
                   avatar_url + ")")
         else:
             avatar_content = request.urlopen(avatar_url).read()
             avatar_uri = self._client.upload(avatar_content, 'image/png')
             self._cache['uploaded_avatars'][avatar_url] = avatar_uri
             print("Uploaded a new avatar for an user " + avatar_uri +
                   " (" + avatar_url + ")")
     if (name is not None
             and name is not current_name) or avatar_uri is not None:
         room.set_user_profile(displayname=name, avatar_url=avatar_uri)
         self._cache['rooms'][room_id]['name'] = name
         self._cache['rooms'][room_id]['avatar_url'] = avatar_url
         self.__save_cache()
     if file_url is not None and file_mimetype is not None and file_name is not None:
         rq = Request(file_url)
         rq.add_header('Authorization', file_authorization)
         file_content = urlopen(rq).read()
         file_uri = self._client.upload(file_content, file_mimetype)
         if file_mimetype in ['image/png', 'image/jpeg']:
             room.send_image(file_uri, file_name)
         else:
             room.send_file(file_uri, file_name)
     if text is not None:
         room.send_text(text)
Ejemplo n.º 9
0
 def create_room(alias, is_public=False, invitees=None):  # pylint: disable=unused-argument
     room = Room(client, f"!room_id:ownserver.com")
     room.canonical_alias = alias
     return room
Ejemplo n.º 10
0
def main():
    module = AnsibleModule(
        argument_spec={
            "username": {
                "required": True,
                "type": "str"
            },
            "password": {
                "required": True,
                "type": "str",
                "no_log": True
            },
            "domain": {
                "required": False,
                "type": "str",
                "default": "http://localhost:8008"
            },
            "join_rule": {
                "required": False,
                "type": "str",
                "choices": ["public", "invite"]
            },
            "history_visibility": {
                "required": False,
                "type": "str",
                "choices": ["invited", "joined", "shared", "world_readable"]
            },
            "invites": {
                "required": False,
                "type": "list",
                "default": []
            },
            "power_levels": {
                "required": False,
                "type": "list",
                "default": []
            },
            "room_alias": {
                "required": False,
                "type": "str"
            },
        })

    client = MatrixClient(module.params['domain'])
    client.login(module.params["username"], module.params["password"])
    room = None
    changed = False
    try:
        room = client.create_room(module.params["room_alias"],
                                  invitees=module.params["invites"])
        changed = True
    except MatrixRequestError as e:
        if json.loads(e.content)["errcode"] == "M_ROOM_IN_USE":
            room_id = client.api.get_room_id("#" +
                                             module.params["room_alias"] +
                                             ":" + client.hs)
            room = Room(client, room_id)
            for user_id in module.params["invites"]:
                # Failure can be due to many reasons, one of which is that the user is
                # already in the room. We interpret failure as lack of changing.
                changed |= room.invite_user(user_id)

        else:
            raise

    if module.params["power_levels"] != []:
        content = client.api.get_power_levels(room.room_id)
        content_changed = False
        for [user, level] in module.params["power_levels"]:
            if content["users"].get(user, -1) != level:
                content["users"][user] = level
                content_changed = True

        if content_changed:
            changed = True
            client.api.set_power_levels(room.room_id, content)

    if module.params["join_rule"] is not None:
        current = get_state(room, "m.room.join_rules")
        if current is None or current["join_rule"] != module.params[
                "join_rule"]:
            client.api.set_join_rule(room.room_id, module.params["join_rule"])

    if module.params["history_visibility"] is not None:
        current = get_state(room, "m.room.history_visibility")
        if current is None or current["history_visibility"] != module.params[
                "history_visibility"]:
            changed = True
            room.send_state_event(
                "m.room.history_visibility", {
                    "history_visibility": module.params["history_visibility"],
                })

    module.exit_json(changed=changed, meta={"room_id": room.room_id})
Ejemplo n.º 11
0
 def create_room(alias, is_public=False, invitees=None):
     room = Room(client, f'!room_id:ownserver.com')
     room.canonical_alias = alias
     return room
Ejemplo n.º 12
0
    service=args.e,
    host=args.l,
    host_display=args.n,
    host_output=args.o,
)

if args.ip4:
    message += 'IPv4: {ip4}; '.format(ip4=args.ip4)
if args.ip6:
    message += 'IPv6: {ip6}; '.format(ip6=args.ip6)
if args.c:
    message += 'Comment: {c}; '.format(c=args.c)
if args.b:
    message += 'Comment by: {b}; '.format(b=args.b)
if args.so:
    message += 'Service Output: {so}; '.format(so=args.so)

print(message)

# MESSAGE SENDING
if PASSWORD:
    client = MatrixClient(SERVER)
    token = client.login_with_password(USERNAME, PASSWORD)
elif TOKEN:
    client = MatrixClient(SERVER, token=TOKEN, user_id=USERNAME)
else:
    sys.exit(0)

if ROOMID:
    room = Room(client, ROOMID)
    room.send_text(message)
Ejemplo n.º 13
0
class TestCryptoStore(object):

    # Initialise a store and test some init code
    device_id = 'AUIETSRN'
    user_id = '@user:matrix.org'
    room_id = '!test:example.com'
    room = Room(None, room_id)
    user = User(None, user_id, '')
    room._members[user_id] = user
    db_name = 'test.db'
    db_path = mkdtemp()
    store_conf = {
        'db_name': db_name,
        'db_path': db_path
    }
    store = CryptoStore(
        user_id, device_id=device_id, db_path=db_path, db_name=db_name)
    db_filepath = os.path.join(db_path, db_name)
    assert os.path.exists(db_filepath)
    store.close()
    store = CryptoStore(
        user_id, device_id=device_id, db_path=db_path, db_name=db_name)

    @pytest.fixture(autouse=True, scope='class')
    def cleanup(self):
        yield
        os.remove(self.db_filepath)

    @pytest.fixture()
    def account(self):
        account = self.store.get_olm_account()
        if account is None:
            account = olm.Account()
            self.store.save_olm_account(account)
        return account

    @pytest.fixture()
    def curve_key(self, account):
        return account.identity_keys['curve25519']

    @pytest.fixture()
    def ed_key(self, account):
        return account.identity_keys['ed25519']

    @pytest.fixture()
    def device(self):
        return OlmDevice(None, self.user_id, self.device_id, store_conf=self.store_conf)

    def test_olm_account_persistence(self):
        account = olm.Account()
        identity_keys = account.identity_keys
        self.store.remove_olm_account()

        # Try to load inexisting account
        saved_account = self.store.get_olm_account()
        assert saved_account is None

        # Try to load inexisting account without device_id
        self.store.device_id = None
        with pytest.raises(ValueError):
            self.store.get_olm_account()
        self.store.device_id = self.device_id

        # Save and load
        self.store.save_olm_account(account)
        saved_account = self.store.get_olm_account()
        assert saved_account.identity_keys == identity_keys

        # Save and load without device_id
        self.store.save_olm_account(account)
        self.store.device_id = None
        saved_account = self.store.get_olm_account()
        assert saved_account.identity_keys == identity_keys
        assert self.store.device_id == self.device_id

        # Replace the account, causing foreign keys to be deleted
        self.store.save_sync_token('test')
        self.store.replace_olm_account(account)
        assert self.store.get_sync_token() is None

        # Load the account from an OlmDevice
        device = OlmDevice(None, self.user_id, self.device_id, store_conf=self.store_conf)
        assert device.olm_account.identity_keys == account.identity_keys

        # Load the account from an OlmDevice, without device_id
        device = OlmDevice(None, self.user_id, store_conf=self.store_conf)
        assert device.device_id == self.device_id

    def test_olm_sessions_persistence(self, account, curve_key, device):
        session = olm.OutboundSession(account, curve_key, curve_key)
        sessions = defaultdict(list)

        self.store.load_olm_sessions(sessions)
        assert not sessions
        assert not self.store.get_olm_sessions(curve_key)

        self.store.save_olm_session(curve_key, session)
        self.store.load_olm_sessions(sessions)
        assert sessions[curve_key][0].id == session.id

        saved_sessions = self.store.get_olm_sessions(curve_key)
        assert saved_sessions[0].id == session.id

        sessions.clear()
        saved_sessions = self.store.get_olm_sessions(curve_key, sessions)
        assert sessions[curve_key][0].id == session.id

        # Replace the session when its internal state has changed
        pickle = session.pickle()
        session.encrypt('test')
        self.store.save_olm_session(curve_key, session)
        saved_sessions = self.store.get_olm_sessions(curve_key)
        assert saved_sessions[0].pickle != pickle

        # Load sessions dynamically
        assert not device.olm_sessions
        with pytest.raises(AttributeError):
            device._olm_decrypt(None, curve_key)
        assert device.olm_sessions[curve_key][0].id == session.id

        device.olm_sessions.clear()
        device.device_keys[self.user_id][self.device_id] = device
        device.olm_ensure_sessions({self.user_id: [self.device_id]})
        assert device.olm_sessions[curve_key][0].id == session.id

        # Test cascade deletion
        self.store.remove_olm_account()
        assert not self.store.get_olm_sessions(curve_key)

    def test_megolm_inbound_persistence(self, curve_key, ed_key, device):
        out_session = olm.OutboundGroupSession()
        session = MegolmInboundSession(out_session.session_key, ed_key)
        session.forwarding_chain.append(curve_key)
        sessions = defaultdict(lambda: defaultdict(dict))

        self.store.load_inbound_sessions(sessions)
        assert not sessions
        assert not self.store.get_inbound_session(self.room_id, curve_key, session.id)

        self.store.save_inbound_session(self.room_id, curve_key, session)
        self.store.load_inbound_sessions(sessions)
        assert sessions[self.room_id][curve_key][session.id].id == session.id

        saved_session = self.store.get_inbound_session(self.room_id, curve_key,
                                                       session.id)
        assert saved_session.id == session.id
        assert saved_session.forwarding_chain == [curve_key]

        sessions = {}
        saved_session = self.store.get_inbound_session(self.room_id, curve_key,
                                                       session.id, sessions)
        assert sessions[session.id].id == session.id

        assert not device.megolm_inbound_sessions
        created = device.megolm_add_inbound_session(
            self.room_id, curve_key, ed_key, session.id, out_session.session_key)
        assert not created
        assert device.megolm_inbound_sessions[self.room_id][curve_key][session.id].id == \
            session.id

        device.megolm_inbound_sessions.clear()
        content = {
            'sender_key': curve_key,
            'session_id': session.id,
            'algorithm': device._megolm_algorithm,
            'device_id': ''
        }
        event = {
            'sender': '',
            'room_id': self.room_id,
            'content': content
        }
        with pytest.raises(KeyError):
            device.megolm_decrypt_event(event)
        assert device.megolm_inbound_sessions[self.room_id][curve_key][session.id].id == \
            session.id

        self.store.remove_olm_account()
        assert not self.store.get_inbound_session(self.room_id, curve_key, session.id)

    @pytest.mark.usefixtures('account')
    def test_megolm_outbound_persistence(self, device):
        session = MegolmOutboundSession(max_messages=2, max_age=100000)
        session.message_count = 1
        session.add_device(self.device_id)
        sessions = {}

        self.store.load_outbound_sessions(sessions)
        assert not sessions
        assert not self.store.get_outbound_session(self.room_id)

        self.store.save_outbound_session(self.room_id, session)
        self.store.save_megolm_outbound_devices(self.room_id, {self.device_id})
        self.store.load_outbound_sessions(sessions)
        assert sessions[self.room_id].id == session.id
        assert sessions[self.room_id].devices == session.devices
        assert sessions[self.room_id].creation_time == session.creation_time
        assert sessions[self.room_id].max_messages == session.max_messages
        assert sessions[self.room_id].message_count == session.message_count
        assert sessions[self.room_id].max_age == session.max_age

        saved_session = self.store.get_outbound_session(self.room_id)
        assert saved_session.id == session.id
        assert saved_session.devices == session.devices
        assert saved_session.creation_time == session.creation_time
        assert saved_session.max_messages == session.max_messages
        assert saved_session.message_count == session.message_count
        assert saved_session.max_age == session.max_age

        sessions.clear()
        saved_session = self.store.get_outbound_session(self.room_id, sessions)
        assert sessions[self.room_id].id == session.id

        self.store.remove_outbound_session(self.room_id)
        assert not self.store.get_outbound_session(self.room_id)

        self.store.save_outbound_session(self.room_id, session)
        saved_session = self.store.get_outbound_session(self.room_id)
        # Verify the saved devices have been erased with the session
        assert not saved_session.devices

        room = Room(None, self.room_id)
        with pytest.raises(AttributeError):
            device.megolm_build_encrypted_event(room, {})
        assert device.megolm_outbound_sessions[self.room_id].id == session.id

        self.store.remove_olm_account()
        assert not self.store.get_outbound_session(self.room_id)

    @pytest.mark.usefixtures('account')
    def test_device_keys_persistence(self, device):
        user_devices = {self.user_id: [self.device_id]}
        device_keys = defaultdict(dict)
        device._verified = True

        self.store.load_device_keys(None, device_keys)
        assert not device_keys
        assert not self.store.get_device_keys(None, user_devices, device_keys)
        assert not device_keys

        device_keys_to_save = {self.user_id: {self.device_id: device}}
        self.store.save_device_keys(device_keys_to_save)
        self.store.load_device_keys(None, device_keys)
        assert device_keys[self.user_id][self.device_id].curve25519 == \
            device.curve25519
        assert device_keys[self.user_id][self.device_id].verified

        device_keys.clear()
        devices = self.store.get_device_keys(None, user_devices)[self.user_id]
        assert devices[self.device_id].curve25519 == device.curve25519
        assert self.store.get_device_keys(None, user_devices, device_keys)
        assert device_keys[self.user_id][self.device_id].curve25519 == \
            device.curve25519
        assert device_keys[self.user_id][self.device_id].verified

        # Test device verification persistence
        device.verified = False
        device.ignored = True
        devices = self.store.get_device_keys(None, user_devices)[self.user_id]
        assert not devices[self.device_id].verified
        assert devices[self.device_id].ignored

        # Test [] wildcard
        devices = self.store.get_device_keys(None, {self.user_id: []})[self.user_id]
        assert devices[self.device_id].curve25519 == device.curve25519

        device.device_list.tracked_user_ids = {self.user_id}
        device.device_list.get_room_device_keys(self.room)
        assert device_keys[self.user_id][self.device_id].curve25519 == \
            device.curve25519

        # Test multiples []
        device_keys.clear()
        user_id = 'test'
        device_id = 'test'
        device_keys_to_save[user_id] = {device_id: device}
        self.store.save_device_keys(device_keys_to_save)
        user_devices[user_id] = []
        user_devices[self.user_id] = []
        device_keys = self.store.get_device_keys(None, user_devices)
        assert device_keys[self.user_id][self.device_id].curve25519 == device.curve25519
        assert device_keys[user_id][device_id].curve25519 == device.curve25519

        # Try to verify a device that has no keys
        device._ed25519 = None
        with pytest.raises(ValueError):
            device.verified = False

        self.store.remove_olm_account()
        assert not self.store.get_device_keys(None, user_devices)

    @pytest.mark.usefixtures('account')
    def test_tracked_users_persistence(self):
        tracked_user_ids = set()
        tracked_user_ids_to_save = {self.user_id}

        self.store.load_tracked_users(tracked_user_ids)
        assert not tracked_user_ids

        self.store.save_tracked_users(tracked_user_ids_to_save)
        self.store.load_tracked_users(tracked_user_ids)
        assert tracked_user_ids == tracked_user_ids_to_save

        self.store.remove_tracked_users({self.user_id})
        tracked_user_ids.clear()
        self.store.load_tracked_users(tracked_user_ids)
        assert not tracked_user_ids

    @pytest.mark.usefixtures('account')
    def test_sync_token_persistence(self):
        sync_token = 'test'

        assert not self.store.get_sync_token()

        self.store.save_sync_token(sync_token)
        assert self.store.get_sync_token() == sync_token

        sync_token = 'new'
        self.store.save_sync_token(sync_token)
        assert self.store.get_sync_token() == sync_token

    @pytest.mark.usefixtures('account')
    def test_key_requests(self):
        session_id = 'test'
        session_ids = set()

        self.store.load_outgoing_key_requests(session_ids)
        assert not session_ids

        self.store.add_outgoing_key_request(session_id)
        self.store.load_outgoing_key_requests(session_ids)
        assert session_id in session_ids

        session_ids.clear()
        self.store.remove_outgoing_key_request(session_id)
        self.store.load_outgoing_key_requests(session_ids)
        assert not session_ids

    def test_load_all(self, account, curve_key, ed_key, device):
        curve_key = account.identity_keys['curve25519']
        session = olm.OutboundSession(account, curve_key, curve_key)
        out_session = MegolmOutboundSession()
        out_session.add_device(self.device_id)
        in_session = MegolmInboundSession(out_session.session_key, ed_key)
        device_keys_to_save = {self.user_id: {self.device_id: device}}

        self.store.save_inbound_session(self.room_id, curve_key, in_session)
        self.store.save_olm_session(curve_key, session)
        self.store.save_outbound_session(self.room_id, out_session)
        self.store.save_megolm_outbound_devices(self.room_id, {self.device_id})
        self.store.save_device_keys(device_keys_to_save)

        device = OlmDevice(
            None, self.user_id, self.device_id, store_conf=self.store_conf, load_all=True)

        assert session.id in {s.id for s in device.olm_sessions[curve_key]}
        saved_in_session = \
            device.megolm_inbound_sessions[self.room_id][curve_key][in_session.id]
        assert saved_in_session.id == in_session.id
        saved_out_session = device.megolm_outbound_sessions[self.room_id]
        assert saved_out_session.id == out_session.id
        assert saved_out_session.devices == out_session.devices
        assert device.device_keys[self.user_id][self.device_id].curve25519 == \
            device.curve25519