12
12
import miniirc , requests , traceback # type: ignore
13
13
14
14
15
- ver = (0 , 0 , 2 )
15
+ ver = (0 , 0 , 3 )
16
16
__version__ = '.' .join (map (str , ver ))
17
17
18
18
@@ -388,24 +388,44 @@ def __init__(self, ip: str, port: int = 0, nick: str = '', *args,
388
388
self .__session .headers ['Authorization' ] = f'Bearer { token } '
389
389
if auto_connect :
390
390
self .connect ()
391
+ else :
392
+ self ._update_baseurl ()
393
+
394
+ def _update_baseurl (self ) -> None :
395
+ hostname = f'{ self .ip } :{ self .port } '
396
+ if (not self .ssl and self .ssl is not None and
397
+ self .ip in ('localhost' , '127.0.0.1' , '::1' , '0::1' )):
398
+ # Non-SSL localhost connections are probably to
399
+ # https://github.com/matrix-org/pantalaimon which doesn't support
400
+ # the "v3" URLs yet.
401
+ protocol = 'http'
402
+ api_version = 'r0'
403
+ else :
404
+ protocol = 'https'
405
+ api_version = 'v3'
406
+
407
+ # Use a shorter hostname if possible
408
+ if self .port == 443 :
409
+ hostname = self .ip
391
410
392
- def _url_for (self , endpoint : str ) -> str :
393
- return f'https://{ self .ip } :{ self .port } /_matrix/client/v3/{ endpoint } '
411
+ matrix_url = f'{ protocol } ://{ hostname } /_matrix'
412
+ self ._baseurl = f'{ matrix_url } /client/{ api_version } '
413
+ self ._media_baseurl = f'{ matrix_url } /media/{ api_version } '
394
414
395
415
def __get (self , endpoint : str , timeout : int = 5 , / ,
396
416
** params : Optional [str | int ]) -> Any :
397
417
self .debug ('GET' , endpoint , params )
398
- return self .__session .get (self ._url_for ( endpoint ) , params = params ,
418
+ return self .__session .get (f' { self ._baseurl } / { endpoint } ' , params = params ,
399
419
timeout = timeout ).json ()
400
420
401
421
def __post (self , endpoint : str , / , ** params : Any ) -> Any :
402
422
self .debug ('POST' , endpoint , params )
403
- return self .__session .post (self ._url_for ( endpoint ) , json = params ,
423
+ return self .__session .post (f' { self ._baseurl } / { endpoint } ' , json = params ,
404
424
timeout = 5 ).json ()
405
425
406
426
def __put (self , endpoint : str , / , ** params : Any ) -> Any :
407
427
self .debug ('PUT' , endpoint , params )
408
- return self .__session .put (self ._url_for ( endpoint ) , json = params ,
428
+ return self .__session .put (f' { self ._baseurl } / { endpoint } ' , json = params ,
409
429
timeout = 5 ).json ()
410
430
411
431
def _get_room_url_no_cache (self , room_id : str ) -> str :
@@ -427,6 +447,7 @@ def connect(self) -> None:
427
447
if self .connected is not None :
428
448
return
429
449
with self ._send_lock :
450
+ self ._update_baseurl ()
430
451
self .active_caps = self .ircv3_caps & {
431
452
'account-tag' , 'echo-message' , 'message-tags' ,
432
453
}
@@ -610,8 +631,7 @@ def _message_event(self, room_id: str, event: _Event) -> None:
610
631
if 'url' in content :
611
632
msg = content .url [str ]
612
633
if msg .startswith ('mxc://' ):
613
- msg = (f'https://{ self .ip } :{ self .port } /_matrix/media/v3/'
614
- f'download/{ msg [6 :]} ' )
634
+ msg = f'{ self ._media_baseurl } /download/{ msg [6 :]} '
615
635
else :
616
636
msg , html_parsed_ok = _matrix_html_to_irc (content )
617
637
@@ -697,7 +717,7 @@ def _logout(cls, homeserver: str, token: str) -> None:
697
717
def _logout_all (cls , homeserver : str , username : str ,
698
718
password : str ) -> None :
699
719
""" Logs out everywhere / voids all tokens for the user. """
700
- token = cls .login (homeserver , username , password )
720
+ token = cls ._login (homeserver , username , password )
701
721
matrix = Matrix (homeserver , token = token , auto_connect = False )
702
722
res = matrix .__post ('logout/all' )
703
723
assert 'error' not in res , res
0 commit comments