Exemple #1
0
    async def join_via(self, room_id: str) -> Union[JoinResponse, JoinError]:
        via = room_id.split(':')[1]
        print(f"joining {room_id} via {via}")

        path = Api._build_path(['join', room_id],
                               dict(access_token=self.client.access_token,
                                    server_name=via))
        response = await self.client._send(JoinResponse, 'POST', path,
                                           Api.to_json({}))
        print("join via response: ", response)
        return response
Exemple #2
0
async def community_invite(client, group, sender):
    if not group:
        return
    path = "groups/{}/admin/users/invite/{}".format(group, sender)
    data = {"user_id": sender}
    query_parameters = {"access_token": client.access_token}
    path = Api._build_path(path, query_parameters)
    logger.debug("community_invite path: %r", path)
    await client.send("PUT",
                      path,
                      Api.to_json(data),
                      headers={"Content-Type": "application/json"})
    return
Exemple #3
0
    def render(self):
        # type: () -> str
        assert self.content_uri

        if self.encrypt:
            http_url = Api.encrypted_mxc_to_plumb(
                self.content_uri, self.file_keys["key"]["k"],
                self.file_keys["hashes"]["sha256"], self.file_keys["iv"])
            url = http_url if http_url else self.content_uri

            description = "{}".format(self.file_name)
            return ("{del_color}<{ncolor}{desc}{del_color}>{ncolor} "
                    "{del_color}[{ncolor}{url}{del_color}]{ncolor}").format(
                        del_color=W.color("chat_delimiters"),
                        ncolor=W.color("reset"),
                        desc=description,
                        url=url)

        http_url = Api.mxc_to_http(self.content_uri)
        description = ("/{}".format(self.file_name) if self.file_name else "")
        return "{url}{desc}".format(url=http_url, desc=description)
Exemple #4
0
 def encrypted_media(mxc, body, key, hash,  iv, homeserver=None):
     """Render a mxc media URI of an encrypted file."""
     http_url = Api.encrypted_mxc_to_plumb(
         mxc,
         key,
         hash,
         iv,
         homeserver
     )
     url = http_url if http_url else mxc
     description = "{}".format(body) if body else "file"
     return Render._media(url, description)
Exemple #5
0
async def callback_room_message_text(room, event):
    logger.info("Message received for room {} | {}: {}".format(
            room.display_name, room.user_name(event.sender), event.body
        ))
    
    # so apparently this API isn't implemented yet in the async client :(
    r = Api.room_read_markers(client.access_token, room.room_id, event.event_id, event.event_id)
    await client.send(*r)
    # but this works!

    ## Plug in game modules here
    if room.is_group:
        # ad-hoc, direct chat
        pass
    else:
        # group chat
        pass
Exemple #6
0
    async def purge_worker(room: str):
        async with semaphore:
            start_time = time.time_ns()
            wait_time = purge_config[config.PURGE_WAIT_SECONDS]
            return_string = "OK"
            content = {"purge_up_to_ts": ts_to_purge}
            if purge_config[config.PURGE_DELETE_LOCAL_EVENTS]:
                content["delete_local_events"] = True

            headers = {
                "Content-type": "application/json",
                "Authorization": "Bearer {}".format(client.access_token)
            }

            history_response = await client.send("POST",
                                                 _PURGE_HISTORY.format(room),
                                                 Api.to_json(content),
                                                 headers=headers)
            status_code = history_response.status
            if status_code != 200:
                return_string = "Error: {} {}".format(status_code,
                                                      history_response.reason)
            else:
                purge_id = (await history_response.json())['purge_id']

                # Wait for the purge request to finish
                while True:
                    history_status_response = await client.send(
                        "GET",
                        _PURGE_HISTORY_STATUS.format(purge_id),
                        headers=headers)
                    history_status_dict = await history_status_response.json()
                    history_status_purge = history_status_dict["status"]
                    if history_status_purge == "active":
                        await asyncio.sleep(wait_time)
                        continue
                    elif history_status_purge == "failed":
                        return_string = "Failed!"
                    elif history_status_purge != "complete":
                        return_string = "Unknown status: {}".format(
                            history_status_purge)
                    break

            end_time = time.time_ns()
            print("Room: {} finished: {} ({} seconds)".format(
                room, return_string, (end_time - start_time) / 1e9))
Exemple #7
0
async def matrixMediaListener(room, event):
    urbitClient.post_message(
        urbitHost, urbitBridgeChat, {
            "text":
            f"{room.user_name(event.sender)} in {room.display_name} sent media:"
        })

    mxcSplit = event.url.split('/')
    imageDownloadRequest = requests.get(
        matrixHomeServer + Api.download(mxcSplit[2], mxcSplit[3])[1])
    with open('storage/' + event.body, 'wb') as f:
        f.write(imageDownloadRequest.content)
    s3Client.Bucket(s3Bucket).upload_file(Filename='storage/' + event.body,
                                          Key=event.body)
    s3AttachmentUrl = s3BucketUrl + '/' + event.body
    urbitClient.post_message(urbitHost, urbitBridgeChat,
                             {"url": f"{s3AttachmentUrl}"})
Exemple #8
0
    async def _find_client(self, access_token):
        client_info = self.client_info.get(access_token, None)

        if not client_info:
            async with aiohttp.ClientSession() as session:
                try:
                    method, path = Api.whoami(access_token)
                    resp = await session.request(
                        method,
                        self.homeserver_url + path,
                        proxy=self.proxy,
                        ssl=self.ssl,
                    )
                except ClientConnectionError:
                    return None

                if resp.status != 200:
                    return None

                try:
                    body = await resp.json()
                except (JSONDecodeError, ContentTypeError):
                    return None

                try:
                    user_id = body["user_id"]
                except KeyError:
                    return None

                if user_id not in self.pan_clients:
                    logger.warn(
                        f"User {user_id} doesn't have a matching pan " f"client."
                    )
                    return None

                logger.info(
                    f"Homeserver confirmed valid access token "
                    f"for user {user_id}, caching info."
                )

                client_info = ClientInfo(user_id, access_token)
                self.client_info[access_token] = client_info

        client = self.pan_clients.get(client_info.user_id, None)

        return client
Exemple #9
0
def upload_process(args):
    file_path = os.path.expanduser(args.file)
    thumbnail = None

    try:
        if args.encrypt:
            upload = EncryptedUpload(file_path)

            if upload.source_mimetype.startswith("image"):
                # TODO create a thumbnail
                thumbnail = None
        else:
            upload = Upload(file_path)

    except (FileNotFoundError, OSError, IOError) as e:
        error(e)

    try:
        url = urlparse(args.homeserver)
    except ValueError as e:
        error(e)

    upload_url = ("https://{}".format(args.homeserver)
                  if not url.scheme else args.homeserver)
    _, api_path, _ = Api.upload(args.access_token, upload.filename)
    upload_url += api_path

    headers = {
        "Content-type": upload.mimetype,
    }

    proxies = {}

    if args.proxy_address:
        user = args.proxy_user or ""

        if args.proxy_password:
            user += ":{}".format(args.proxy_password)

        if user:
            user += "@"

        proxies = {
            "https": "{}://{}{}:{}/".format(
                args.proxy_type,
                user,
                args.proxy_address,
                args.proxy_port
            )
        }

    message = {
        "type": "status",
        "status": "started",
        "total": upload.totalsize,
        "file_name": upload.filename,
    }

    if isinstance(upload, EncryptedUpload):
        message["mimetype"] = upload.source_mimetype
    else:
        message["mimetype"] = upload.mimetype

    to_stdout(message)

    session = requests.Session()
    session.trust_env = False

    try:
        r = session.post(
            url=upload_url,
            auth=None,
            headers=headers,
            data=IterableToFileAdapter(upload),
            verify=(not args.insecure),
            proxies=proxies
        )
    except (requests.exceptions.RequestException, OSError) as e:
        error(e)

    try:
        json_response = json.loads(r.content)
    except JSONDecodeError:
        error(r.content)

    response = UploadResponse.from_dict(json_response)

    if isinstance(response, UploadError):
        error(str(response))

    message = {
        "type": "status",
        "status": "done",
        "url": response.content_uri
    }

    if isinstance(upload, EncryptedUpload):
        message["file_keys"] = upload.file_keys

    to_stdout(message)

    return 0
Exemple #10
0
 def msgtype(self):
     # type: () -> str
     assert self.mimetype
     return Api.mimetype_to_msgtype(self.mimetype)
Exemple #11
0
 def media(mxc, body, homeserver=None):
     """Render a mxc media URI."""
     url = Api.mxc_to_http(mxc, homeserver)
     description = "{}".format(body) if body else "file"
     return Render._media(url, description)
Exemple #12
0
 def request_spaces(access_token: str, room_id: str) -> Tuple[str, str]:
     path = ["org.matrix.msc2946", "rooms", room_id, "spaces"]
     return "GET", Api._build_path(path,
                                   dict(access_token=access_token),
                                   base_path="/_matrix/client/unstable")