Skip to content

Commit

Permalink
Check .well-known/matrix/client
Browse files Browse the repository at this point in the history
  • Loading branch information
luk3yx committed May 22, 2022
1 parent 1d34f87 commit 0b4d14b
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 11 deletions.
35 changes: 25 additions & 10 deletions miniirc_matrix.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,12 +7,12 @@
from __future__ import annotations
from collections.abc import Callable
from typing import Any, Optional, TypeVar, overload
from urllib.parse import quote as _url_quote
from urllib.parse import quote as _url_quote, urlparse as _urlparse
import functools, html.parser, itertools, json, math, re, threading, time, uuid
import miniirc, requests, traceback # type: ignore


ver = (0, 0, 4)
ver = (0, 0, 5)
__version__ = '.'.join(map(str, ver))


Expand Down Expand Up @@ -398,19 +398,34 @@ def _update_baseurl(self) -> None:
# Non-SSL localhost connections are probably to
# https://github.com/matrix-org/pantalaimon which doesn't support
# the "v3" URLs yet.
protocol = 'http'
matrix_url = f'http://{hostname}'
api_version = 'r0'
else:
protocol = 'https'
api_version = 'v3'

# Use a shorter hostname if possible
if self.port == 443:
hostname = self.ip
# Check for a .well-known/matrix/client
res = requests.get(f'https://{hostname}/.well-known/matrix/client',
timeout=10)
if res.status_code == 200:
e = _Event(res.json())
baseurl = e.m_homeserver.base_url[str].rstrip('/')

# Ensure the baseurl is valid
parsed_url = _urlparse(baseurl)
if (parsed_url.scheme != 'https' or parsed_url.query or
parsed_url.fragment):
raise ValueError(f'Invalid URL {baseurl!r}')
elif res.status_code == 404:
# Use a shorter hostname if possible
if self.port == 443:
hostname = self.ip
baseurl = f'https://{hostname}'
else:
res.raise_for_status()
raise ValueError(f'Status code {res.status_code} returned')

matrix_url = f'{protocol}://{hostname}/_matrix'
self._baseurl = f'{matrix_url}/client/{api_version}'
self._media_baseurl = f'{matrix_url}/media/{api_version}'
self._baseurl = f'{baseurl}/_matrix/client/{api_version}'
self._media_baseurl = f'{baseurl}/_matrix/media/{api_version}'

def __get(self, endpoint: str, timeout: int = 5, /,
**params: Optional[str | int]) -> Any:
Expand Down
2 changes: 1 addition & 1 deletion setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@

setup(
name='miniirc_matrix',
version='0.0.4',
version='0.0.5',
py_modules=['miniirc_matrix'],
author='luk3yx',
description='A Matrix wrapper for miniirc.',
Expand Down

0 comments on commit 0b4d14b

Please sign in to comment.