<s> from . api import MatrixHttpApi , MatrixRequestError , MatrixUnexpectedResponse <EOL> from threading import Thread <EOL> import sys <EOL> class MatrixClient ( object ) : <EOL> """<STR_LIT>""" <EOL> def __init__ ( self , base_url , token = None , valid_cert_check = True ) : <EOL> self . api = MatrixHttpApi ( base_url , token ) <EOL> self . api . validate_certificate ( valid_cert_check ) <EOL> self . listeners = [ ] <EOL> self . rooms = { <EOL> } <EOL> if token : <EOL> self . _sync ( ) <EOL> def register_with_password ( self , username , password , limit = <NUM_LIT:1> ) : <EOL> response = self . api . register ( <EOL> "<STR_LIT>" , user = username , password = password <EOL> ) <EOL> self . user_id = response [ "<STR_LIT>" ] <EOL> self . token = response [ "<STR_LIT>" ] <EOL> self . hs = response [ "<STR_LIT>" ] <EOL> self . api . token = self . token <EOL> self . _sync ( limit ) <EOL> return self . token <EOL> def login_with_password ( self , username , password , limit = <NUM_LIT:1> ) : <EOL> response = self . api . login ( <EOL> "<STR_LIT>" , user = username , password = password <EOL> ) <EOL> self . user_id = response [ "<STR_LIT>" ] <EOL> self . token = response [ "<STR_LIT>" ] <EOL> self . hs = response [ "<STR_LIT>" ] <EOL> self . api . token = self . token <EOL> self . _sync ( limit ) <EOL> return self . token <EOL> def create_room ( self , alias = None , is_public = False , invitees = ( ) ) : <EOL> response = self . api . create_room ( alias , is_public , invitees ) <EOL> return self . _mkroom ( response [ "<STR_LIT>" ] ) <EOL> def join_room ( self , room_id_or_alias ) : <EOL> response = self . api . join_room ( room_id_or_alias ) <EOL> room_id = ( <EOL> response [ "<STR_LIT>" ] if "<STR_LIT>" in response else room_id_or_alias <EOL> ) <EOL> return self . _mkroom ( room_id ) <EOL> def get_rooms ( self ) : <EOL> return self . rooms <EOL> def add_listener ( self , callback ) : <EOL> self . listeners . append ( callback ) <EOL> def listen_for_events ( self , timeout = <NUM_LIT> ) : <EOL> response = self . api . event_stream ( self . end , timeout ) <EOL> self . end = response [ "<STR_LIT:end>" ] <EOL> for chunk in response [ "<STR_LIT>" ] : <EOL> for listener in self . listeners : <EOL> listener ( chunk ) <EOL> if "<STR_LIT>" in chunk : <EOL> if chunk [ "<STR_LIT>" ] not in self . rooms : <EOL> self . _mkroom ( chunk [ "<STR_LIT>" ] ) <EOL> self . rooms [ chunk [ "<STR_LIT>" ] ] . events . append ( chunk ) <EOL> for listener in self . rooms [ chunk [ "<STR_LIT>" ] ] . listeners : <EOL> listener ( chunk ) <EOL> def listen_forever ( self , timeout = <NUM_LIT> ) : <EOL> while ( True ) : <EOL> self . listen_for_events ( timeout ) <EOL> def start_listener_thread ( self , timeout = <NUM_LIT> ) : <EOL> try : <EOL> thread = Thread ( target = self . listen_forever , args = ( timeout , ) ) <EOL> thread . daemon = True <EOL> thread . start ( ) <EOL> except : <EOL> e = sys . exc_info ( ) [ <NUM_LIT:0> ] <EOL> print ( "<STR_LIT>" + str ( e ) ) <EOL> def upload ( self , content , content_type ) : <EOL> try : <EOL> response = self . api . media_upload ( content , content_type ) <EOL> if "<STR_LIT>" in response : <EOL> return response [ "<STR_LIT>" ] <EOL> else : <EOL> raise MatrixUnexpectedResponse ( <EOL> "<STR_LIT>" <EOL> ) <EOL> except MatrixRequestError as e : <EOL> raise MatrixRequestError ( <EOL> code = e . code , <EOL> content = "<STR_LIT>" % e <EOL> ) <EOL> def _mkroom ( self , room_id ) : <EOL> self . rooms [ room_id ] = Room ( self , room_id ) <EOL> return self . rooms [ room_id ] <EOL> def _process_state_event ( self , state_event , current_room ) : <EOL> if "<STR_LIT:type>" not in state_event : <EOL> return <EOL> etype = state_event [ "<STR_LIT:type>" ] <EOL> if etype == "<STR_LIT>" : <EOL> current_room . name = state_event [ "<STR_LIT:content>" ] [ "<STR_LIT:name>" ] <EOL> elif etype == "<STR_LIT>" : <EOL> current_room . topic = state_event [ "<STR_LIT:content>" ] [ "<STR_LIT>" ] <EOL> elif etype == "<STR_LIT>" : <EOL> current_room . aliases = state_event [ "<STR_LIT:content>" ] [ "<STR_LIT>" ] <EOL> def _sync ( self , limit = <NUM_LIT:1> ) : <EOL> response = self . api . initial_sync ( limit ) <EOL> try : <EOL> self . end = response [ "<STR_LIT:end>" ] <EOL> for room in response [ "<STR_LIT>" ] : <EOL> self . _mkroom ( room [ "<STR_LIT>" ] ) <EOL> current_room = self . get_rooms ( ) [ room [ "<STR_LIT>" ] ] <EOL> for chunk in room [ "<STR_LIT>" ] [ "<STR_LIT>" ] : <EOL> current_room . events . append ( chunk ) <EOL> for state_event in room [ "<STR_LIT:state>" ] : <EOL> self . _process_state_event ( state_event , current_room ) <EOL> except KeyError : <EOL> pass <EOL> def get_user ( self , user_id ) : <EOL> return User ( self . api , user_id ) <EOL> class Room ( object ) : <EOL> def __init__ ( self , client , room_id ) : <EOL> self . room_id = room_id <EOL> self . client = client <EOL> self . listeners = [ ] <EOL> self . events = [ ] <EOL> self . name = None <EOL> self . aliases = [ ] <EOL> self . topic = None <EOL> def send_text ( self , text ) : <EOL> return self . client . api . send_message ( self . room_id , text ) <EOL> def send_emote ( self , text ) : <EOL> return self . client . api . send_emote ( self . room_id , text ) <EOL> def send_image ( self , url , name , ** imageinfo ) : <EOL> return self . client . api . send_content ( <EOL> self . room_id , url , name , "<STR_LIT>" , <EOL> extra_information = imageinfo <EOL> ) <EOL> def add_listener ( self , callback ) : <EOL> self . listeners . append ( callback ) <EOL> def get_events ( self ) : <EOL> return self . events <EOL> def invite_user ( self , user_id ) : <EOL> """<STR_LIT>""" <EOL> try : <EOL> self . client . api . invite_user ( self . room_id , user_id ) <EOL> return True <EOL> except MatrixRequestError : <EOL> return False <EOL> def kick_user ( self , user_id , reason = "<STR_LIT>" ) : <EOL> try : <EOL> self . client . api . kick_user ( self . room_id , user_id ) <EOL> return True <EOL> except MatrixRequestError : <EOL> return False <EOL> def ban_user ( self , user_id , reason ) : <EOL> try : <EOL> self . client . api . ban_user ( self . room_id , user_id , reason ) <EOL> return True <EOL> except MatrixRequestError : <EOL> return False <EOL> def leave ( self , user_id ) : <EOL> try : <EOL> self . client . api . leave_room ( self . room_id ) <EOL> self . client . rooms . remove ( self . room_id ) <EOL> return True <EOL> except MatrixRequestError : <EOL> return False <EOL> def update_room_name ( self ) : <EOL> """<STR_LIT>""" <EOL> try : <EOL> response = self . client . api . get_room_name ( self . room_id ) <EOL> if "<STR_LIT:name>" in response and response [ "<STR_LIT:name>" ] != self . name : <EOL> self . name = response [ "<STR_LIT:name>" ] <EOL> return True <EOL> else : <EOL> return False <EOL> except MatrixRequestError : <EOL> return False <EOL> def update_room_topic ( self ) : <EOL> """<STR_LIT>""" <EOL> try : <EOL> response = self . client . api . get_room_topic ( self . room_id ) <EOL> if "<STR_LIT>" in response and response [ "<STR_LIT>" ] != self . topic : <EOL> self . topic = response [ "<STR_LIT>" ] <EOL> return True <EOL> else : <EOL> return False <EOL> except MatrixRequestError : <EOL> return False <EOL> def update_aliases ( self ) : <EOL> """<STR_LIT>""" <EOL> try : <EOL> response = self . client . api . get_room_state ( self . room_id ) <EOL> for chunk in response : <EOL> if "<STR_LIT:content>" in chunk and "<STR_LIT>" in chunk [ "<STR_LIT:content>" ] : <EOL> if chunk [ "<STR_LIT:content>" ] [ "<STR_LIT>" ] != self . aliases : <EOL> self . aliases = chunk [ "<STR_LIT:content>" ] [ "<STR_LIT>" ] <EOL> return True <EOL> else : <EOL> return False <EOL> except MatrixRequestError : <EOL> return False <EOL> class User ( object ) : <EOL> def __init__ ( self , api , user_id ) : <EOL> self . user_id = user_id <EOL> self . api = api <EOL> def get_display_name ( self ) : <EOL> return self . api . get_display_name ( self . user_id ) <EOL> def set_display_name ( self , display_name ) : <EOL> return self . api . set_display_name ( self . user_id , display_name ) <EOL> def get_avatar_url ( self ) : <EOL> mxcurl = self . api . get_avatar_url ( self . user_id ) <EOL> url = self . api . get_download_url ( mxcurl ) <EOL> return url <EOL> def set_avatar_url ( self , avatar_url ) : <EOL> return self . api . set_avatar_url ( self . user_id , avatar_url ) </s>
