4 Commits

Author SHA1 Message Date
luk3yx
1a0d644b60 Try again in 15s if error returned from /v3/sync 2022-11-18 09:35:17 +13:00
luk3yx
c184810355 Fix bug when room['timeline'] doesn't exist 2022-11-16 13:46:07 +13:00
luk3yx
af6a763c14 Bugfix 2022-05-22 17:50:30 +12:00
luk3yx
0b4d14b8fd Check .well-known/matrix/client 2022-05-22 17:40:16 +12:00
2 changed files with 37 additions and 13 deletions

View File

@@ -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, 8)
__version__ = '.'.join(map(str, ver))
@@ -398,19 +398,34 @@ class Matrix(miniirc.IRC):
# Non-SSL localhost connections are probably to
# https://github.com/matrix-org/pantalaimon which doesn't support
# the "v3" URLs yet.
protocol = 'http'
baseurl = 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('/')
matrix_url = f'{protocol}://{hostname}/_matrix'
self._baseurl = f'{matrix_url}/client/{api_version}'
self._media_baseurl = f'{matrix_url}/media/{api_version}'
# 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')
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:
@@ -480,6 +495,14 @@ class Matrix(miniirc.IRC):
if self.debug_file:
self.debug(json.dumps(res, indent=4))
if 'error' in res:
# TODO: Use self.debug or something
print(f'[miniirc_matrix] Error returned when trying to '
f'fetch /sync: {res["error"]!r}')
if self.persist:
self.debug('Trying again in 15 seconds...')
time.sleep(15)
continue
break
next_batch = res['next_batch']
if 'rooms' in res:
@@ -505,8 +528,9 @@ class Matrix(miniirc.IRC):
@_room_processor('join', 'leave')
def __process_join(self, room_id: str, room: dict[str, Any]) -> None:
# Joined rooms
for raw_event in room['timeline']['events']:
self.__fire_event(room_id, _Event(raw_event))
if 'timeline' in room:
for raw_event in room['timeline']['events']:
self.__fire_event(room_id, _Event(raw_event))
@_room_processor('invite')
def __process_invite(self, room_id: str, room: dict[str, Any]) -> None:

View File

@@ -5,7 +5,7 @@ from setuptools import setup
setup(
name='miniirc_matrix',
version='0.0.4',
version='0.0.8',
py_modules=['miniirc_matrix'],
author='luk3yx',
description='A Matrix wrapper for miniirc.',