Delete server command.
This commit is contained in:
parent
d3c18c9e57
commit
402b9e9463
1 changed files with 86 additions and 30 deletions
|
@ -33,8 +33,10 @@ MATRIX_API_PATH = "/_matrix/client/r0" # type: unicode
|
||||||
SERVERS = dict() # type: Dict[unicode, MatrixServer]
|
SERVERS = dict() # type: Dict[unicode, MatrixServer]
|
||||||
CONFIG = None # type: weechat.config
|
CONFIG = None # type: weechat.config
|
||||||
|
|
||||||
# Unicode handling
|
NICK_GROUP_HERE = "0|Here"
|
||||||
|
|
||||||
|
|
||||||
|
# Unicode handling
|
||||||
def encode_to_utf8(data):
|
def encode_to_utf8(data):
|
||||||
if isinstance(data, unicode):
|
if isinstance(data, unicode):
|
||||||
return data.encode('utf-8')
|
return data.encode('utf-8')
|
||||||
|
@ -260,7 +262,6 @@ class MatrixServer:
|
||||||
self.access_token = None # type: unicode
|
self.access_token = None # type: unicode
|
||||||
self.next_batch = None # type: unicode
|
self.next_batch = None # type: unicode
|
||||||
|
|
||||||
|
|
||||||
# TODO this should be made stateless
|
# TODO this should be made stateless
|
||||||
host_string = ':'.join([self.address,
|
host_string = ':'.join([self.address,
|
||||||
str(self.port)]) # type: unicode
|
str(self.port)]) # type: unicode
|
||||||
|
@ -385,8 +386,6 @@ def handle_http_response(server, message):
|
||||||
|
|
||||||
return
|
return
|
||||||
|
|
||||||
NICK_GROUP_HERE = "0|Here"
|
|
||||||
NICK_GROUP_AWAY = "1|Away"
|
|
||||||
|
|
||||||
def handle_room_info(server, room_info):
|
def handle_room_info(server, room_info):
|
||||||
# type: (MatrixServer, Dict) -> None
|
# type: (MatrixServer, Dict) -> None
|
||||||
|
@ -624,6 +623,7 @@ def send(server, message):
|
||||||
server_buffer_prnt(server, str(error))
|
server_buffer_prnt(server, str(error))
|
||||||
return False
|
return False
|
||||||
|
|
||||||
|
|
||||||
@utf8_decode
|
@utf8_decode
|
||||||
def receive_cb(server_name, file_descriptor):
|
def receive_cb(server_name, file_descriptor):
|
||||||
server = SERVERS[server_name]
|
server = SERVERS[server_name]
|
||||||
|
@ -746,7 +746,7 @@ def connect_cb(data, status, gnutls_rc, sock, error, ip_address):
|
||||||
server.fd_hook = hook
|
server.fd_hook = hook
|
||||||
server.connected = True
|
server.connected = True
|
||||||
server.connecting = False
|
server.connecting = False
|
||||||
server.reconnectCount = 0
|
server.reconnect_count = 0
|
||||||
|
|
||||||
server_buffer_prnt(server, "Connected")
|
server_buffer_prnt(server, "Connected")
|
||||||
|
|
||||||
|
@ -792,6 +792,8 @@ def connect_cb(data, status, gnutls_rc, sock, error, ip_address):
|
||||||
|
|
||||||
def reconnect(server):
|
def reconnect(server):
|
||||||
# type: (MatrixServer) -> None
|
# type: (MatrixServer) -> None
|
||||||
|
# TODO this needs some more work, do we want a reconnecting flag?
|
||||||
|
server.connecting = True
|
||||||
timeout = server.reconnect_count * 5 * 1000
|
timeout = server.reconnect_count * 5 * 1000
|
||||||
|
|
||||||
if timeout > 0:
|
if timeout > 0:
|
||||||
|
@ -834,6 +836,7 @@ def connect(server):
|
||||||
if not server.server_buffer:
|
if not server.server_buffer:
|
||||||
create_server_buffer(server)
|
create_server_buffer(server)
|
||||||
|
|
||||||
|
if not server.timer_hook:
|
||||||
server.timer_hook = W.hook_timer(
|
server.timer_hook = W.hook_timer(
|
||||||
1 * 1000,
|
1 * 1000,
|
||||||
0,
|
0,
|
||||||
|
@ -845,7 +848,6 @@ def connect(server):
|
||||||
W.hook_connect("", server.address, server.port, 1, 0, "",
|
W.hook_connect("", server.address, server.port, 1, 0, "",
|
||||||
"connect_cb", server.name)
|
"connect_cb", server.name)
|
||||||
|
|
||||||
server.connecting = True
|
|
||||||
return W.WEECHAT_RC_OK
|
return W.WEECHAT_RC_OK
|
||||||
|
|
||||||
|
|
||||||
|
@ -853,6 +855,13 @@ def connect(server):
|
||||||
def room_input_cb(server_name, buffer, input_data):
|
def room_input_cb(server_name, buffer, input_data):
|
||||||
server = SERVERS[server_name]
|
server = SERVERS[server_name]
|
||||||
|
|
||||||
|
if not server.connected:
|
||||||
|
message = "{prefix}you are not connected to the server".format(
|
||||||
|
prefix=W.prefix("error"))
|
||||||
|
W.prnt(buffer, message)
|
||||||
|
return W.WEECHAT_RC_ERROR
|
||||||
|
|
||||||
|
# TODO put this in a function
|
||||||
room_id = list(server.buffers.keys())[list(server.buffers.values()).index(buffer)]
|
room_id = list(server.buffers.keys())[list(server.buffers.values()).index(buffer)]
|
||||||
body = {"msgtype": "m.text", "body": input_data}
|
body = {"msgtype": "m.text", "body": input_data}
|
||||||
message = generate_matrix_request(server, MessageType.POST_MSG,
|
message = generate_matrix_request(server, MessageType.POST_MSG,
|
||||||
|
@ -1001,18 +1010,30 @@ def matrix_unload_cb():
|
||||||
return W.WEECHAT_RC_OK
|
return W.WEECHAT_RC_OK
|
||||||
|
|
||||||
|
|
||||||
|
def check_server_existence(server_name, servers):
|
||||||
|
if not server_name in servers:
|
||||||
|
message = "{prefix}matrix: No such server: {server} found".format(
|
||||||
|
prefix=W.prefix("error"), server=server_name)
|
||||||
|
W.prnt("", message)
|
||||||
|
return False
|
||||||
|
else:
|
||||||
|
return True
|
||||||
|
|
||||||
|
|
||||||
@utf8_decode
|
@utf8_decode
|
||||||
def matrix_server_command_cb(data, buffer, args):
|
def matrix_server_command_cb(data, buffer, args):
|
||||||
def connect_server(servers):
|
def connect_server(args):
|
||||||
# TODO check if the server exists
|
for server_name in args:
|
||||||
for server_name in servers:
|
if check_server_existence(server_name, SERVERS):
|
||||||
server = SERVERS[server_name]
|
server = SERVERS[server_name]
|
||||||
connect(server)
|
connect(server)
|
||||||
|
|
||||||
def disconnect_server(servers):
|
def disconnect_server(args):
|
||||||
# TODO check if the server exists
|
for server_name in args:
|
||||||
for server_name in servers:
|
if check_server_existence(server_name, SERVERS):
|
||||||
server = SERVERS[server_name]
|
server = SERVERS[server_name]
|
||||||
|
W.unhook(server.timer_hook)
|
||||||
|
server.timer_hook = None
|
||||||
disconnect(server)
|
disconnect(server)
|
||||||
|
|
||||||
def list_servers():
|
def list_servers():
|
||||||
|
@ -1028,9 +1049,39 @@ def matrix_server_command_cb(data, buffer, args):
|
||||||
def list_full_servers(servers):
|
def list_full_servers(servers):
|
||||||
W.prnt("", "\nCommand not implemented")
|
W.prnt("", "\nCommand not implemented")
|
||||||
|
|
||||||
|
def delete_server(args):
|
||||||
|
for server_name in args:
|
||||||
|
if check_server_existence(server_name, SERVERS):
|
||||||
|
server = SERVERS[server_name]
|
||||||
|
|
||||||
|
if server.connected:
|
||||||
|
message = ("{prefix}matrix: you can not delete server "
|
||||||
|
"\"{server}\" because you are connected to it. "
|
||||||
|
"Try \"/matrix disconnect {server}\" "
|
||||||
|
"before.").format(prefix=W.prefix("error"),
|
||||||
|
server=server.name)
|
||||||
|
W.prnt("", message)
|
||||||
|
return
|
||||||
|
|
||||||
|
for buf in server.buffers.values():
|
||||||
|
W.buffer_close(buf)
|
||||||
|
|
||||||
|
W.buffer_close(server.server_buffer)
|
||||||
|
|
||||||
|
for option in server.options.values():
|
||||||
|
W.config_option_free(option)
|
||||||
|
|
||||||
|
message = "matrix: server {server} has been deleted".format(
|
||||||
|
server=server.name)
|
||||||
|
|
||||||
|
del SERVERS[server.name]
|
||||||
|
server = None
|
||||||
|
|
||||||
|
W.prnt("", message)
|
||||||
|
|
||||||
# TODO
|
# TODO
|
||||||
def delete_server(servers):
|
def add_server(args):
|
||||||
W.prnt("", "\nCommand not implemented")
|
pass
|
||||||
|
|
||||||
split_args = args.split(' ', 2)
|
split_args = args.split(' ', 2)
|
||||||
|
|
||||||
|
@ -1051,11 +1102,17 @@ def matrix_server_command_cb(data, buffer, args):
|
||||||
|
|
||||||
if subcommand == 'list':
|
if subcommand == 'list':
|
||||||
list_servers()
|
list_servers()
|
||||||
|
|
||||||
if subcommand == 'listfull':
|
if subcommand == 'listfull':
|
||||||
list_full_servers(args)
|
list_full_servers(args)
|
||||||
|
|
||||||
elif subcommand == 'add':
|
elif subcommand == 'add':
|
||||||
# TODO allow setting the address and port
|
# TODO allow setting the address and port
|
||||||
SERVERS[args[0]] = MatrixServer(args[0], CONFIG)
|
SERVERS[args[0]] = MatrixServer(args[0], CONFIG)
|
||||||
|
|
||||||
|
elif subcommand == 'delete':
|
||||||
|
delete_server(args)
|
||||||
|
|
||||||
else:
|
else:
|
||||||
print("Unknown subcommand")
|
print("Unknown subcommand")
|
||||||
else:
|
else:
|
||||||
|
@ -1179,7 +1236,6 @@ if __name__ == "__main__":
|
||||||
|
|
||||||
init_hooks()
|
init_hooks()
|
||||||
|
|
||||||
# TODO this can't be here
|
|
||||||
if not SERVERS:
|
if not SERVERS:
|
||||||
create_default_server(CONFIG)
|
create_default_server(CONFIG)
|
||||||
|
|
||||||
|
|
Loading…
Reference in a new issue