Implement matrix help command.
This commit is contained in:
parent
c4fcbe5e84
commit
2145496bf8
1 changed files with 147 additions and 40 deletions
|
@ -46,8 +46,7 @@ def encode_to_utf8(data):
|
||||||
return type(data)(map(encode_to_utf8, data.iteritems()))
|
return type(data)(map(encode_to_utf8, data.iteritems()))
|
||||||
elif isinstance(data, Iterable):
|
elif isinstance(data, Iterable):
|
||||||
return type(data)(map(encode_to_utf8, data))
|
return type(data)(map(encode_to_utf8, data))
|
||||||
else:
|
return data
|
||||||
return data
|
|
||||||
|
|
||||||
|
|
||||||
def decode_from_utf8(data):
|
def decode_from_utf8(data):
|
||||||
|
@ -59,18 +58,17 @@ def decode_from_utf8(data):
|
||||||
return type(data)(map(decode_from_utf8, data.iteritems()))
|
return type(data)(map(decode_from_utf8, data.iteritems()))
|
||||||
elif isinstance(data, Iterable):
|
elif isinstance(data, Iterable):
|
||||||
return type(data)(map(decode_from_utf8, data))
|
return type(data)(map(decode_from_utf8, data))
|
||||||
else:
|
return data
|
||||||
return data
|
|
||||||
|
|
||||||
|
|
||||||
def utf8_decode(f):
|
def utf8_decode(function):
|
||||||
"""
|
"""
|
||||||
Decode all arguments from byte strings to unicode strings. Use this for
|
Decode all arguments from byte strings to unicode strings. Use this for
|
||||||
functions called from outside of this script, e.g. callbacks from weechat.
|
functions called from outside of this script, e.g. callbacks from weechat.
|
||||||
"""
|
"""
|
||||||
@wraps(f)
|
@wraps(function)
|
||||||
def wrapper(*args, **kwargs):
|
def wrapper(*args, **kwargs):
|
||||||
return f(*decode_from_utf8(args), **decode_from_utf8(kwargs))
|
return function(*decode_from_utf8(args), **decode_from_utf8(kwargs))
|
||||||
return wrapper
|
return wrapper
|
||||||
|
|
||||||
|
|
||||||
|
@ -94,8 +92,7 @@ class WeechatWrapper(object):
|
||||||
orig_attr = self.wrapped_class.__getattribute__(attr)
|
orig_attr = self.wrapped_class.__getattribute__(attr)
|
||||||
if callable(orig_attr):
|
if callable(orig_attr):
|
||||||
return self.wrap_for_utf8(orig_attr)
|
return self.wrap_for_utf8(orig_attr)
|
||||||
else:
|
return decode_from_utf8(orig_attr)
|
||||||
return decode_from_utf8(orig_attr)
|
|
||||||
|
|
||||||
# Ensure all lines sent to weechat specify a prefix. For lines after the
|
# Ensure all lines sent to weechat specify a prefix. For lines after the
|
||||||
# first, we want to disable the prefix, which is done by specifying a space.
|
# first, we want to disable the prefix, which is done by specifying a space.
|
||||||
|
@ -739,7 +736,7 @@ def create_server_buffer(server):
|
||||||
@utf8_decode
|
@utf8_decode
|
||||||
def connect_cb(data, status, gnutls_rc, sock, error, ip_address):
|
def connect_cb(data, status, gnutls_rc, sock, error, ip_address):
|
||||||
# pylint: disable=too-many-arguments,too-many-branches
|
# pylint: disable=too-many-arguments,too-many-branches
|
||||||
status_value = int(status) # type: long
|
status_value = int(status) # type: int
|
||||||
server = SERVERS[data]
|
server = SERVERS[data]
|
||||||
|
|
||||||
if status_value == W.WEECHAT_HOOK_CONNECT_OK:
|
if status_value == W.WEECHAT_HOOK_CONNECT_OK:
|
||||||
|
@ -1031,12 +1028,100 @@ def check_server_existence(server_name, servers):
|
||||||
return True
|
return True
|
||||||
|
|
||||||
|
|
||||||
def matrix_server_command(command, args):
|
def matrix_command_help(args):
|
||||||
def weechat_server_color():
|
for command in args:
|
||||||
option = weechat.config_get("weechat.color.chat_server")
|
message = ""
|
||||||
color = W.config_color(option)
|
|
||||||
return W.color(color)
|
|
||||||
|
|
||||||
|
if command == "connect":
|
||||||
|
message = ("{delimiter_color}[{ncolor}matrix{delimiter_color}] "
|
||||||
|
"{ncolor}{cmd_color}/connect{ncolor} "
|
||||||
|
"<server-name> [<server-name>...]"
|
||||||
|
"\n\n"
|
||||||
|
"connect to Matrix server(s)"
|
||||||
|
"\n\n"
|
||||||
|
"server-name: server to connect to"
|
||||||
|
"(internal name)").format(
|
||||||
|
delimiter_color=W.color("chat_delimiters"),
|
||||||
|
cmd_color=W.color("chat_buffer"),
|
||||||
|
ncolor=W.color("reset"))
|
||||||
|
|
||||||
|
elif command == "disconnect":
|
||||||
|
message = ("{delimiter_color}[{ncolor}matrix{delimiter_color}] "
|
||||||
|
"{ncolor}{cmd_color}/disconnect{ncolor} "
|
||||||
|
"<server-name> [<server-name>...]"
|
||||||
|
"\n\n"
|
||||||
|
"disconnect from Matrix server(s)"
|
||||||
|
"\n\n"
|
||||||
|
"server-name: server to disconnect"
|
||||||
|
"(internal name)").format(
|
||||||
|
delimiter_color=W.color("chat_delimiters"),
|
||||||
|
cmd_color=W.color("chat_buffer"),
|
||||||
|
ncolor=W.color("reset"))
|
||||||
|
|
||||||
|
elif command == "reconnect":
|
||||||
|
message = ("{delimiter_color}[{ncolor}matrix{delimiter_color}] "
|
||||||
|
"{ncolor}{cmd_color}/reconnect{ncolor} "
|
||||||
|
"<server-name> [<server-name>...]"
|
||||||
|
"\n\n"
|
||||||
|
"reconnect to Matrix server(s)"
|
||||||
|
"\n\n"
|
||||||
|
"server-name: server to reconnect"
|
||||||
|
"(internal name)").format(
|
||||||
|
delimiter_color=W.color("chat_delimiters"),
|
||||||
|
cmd_color=W.color("chat_buffer"),
|
||||||
|
ncolor=W.color("reset"))
|
||||||
|
|
||||||
|
elif command == "server":
|
||||||
|
message = ("{delimiter_color}[{ncolor}matrix{delimiter_color}] "
|
||||||
|
"{ncolor}{cmd_color}/server{ncolor} "
|
||||||
|
"add <server-name> <hostname>[:<port>]"
|
||||||
|
"\n "
|
||||||
|
"delete|list|listfull <server-name>"
|
||||||
|
"\n\n"
|
||||||
|
"list, add, or remove Matrix servers"
|
||||||
|
"\n\n"
|
||||||
|
" list: list servers (without argument, this "
|
||||||
|
"list is displayed)\n"
|
||||||
|
" listfull: list servers with detailed info for each "
|
||||||
|
"server\n"
|
||||||
|
" add: add a new server\n"
|
||||||
|
" delete: delete a server\n"
|
||||||
|
"server-name: server to reconnect (internal name)\n"
|
||||||
|
" hostname: name or IP address of server\n"
|
||||||
|
" port: port of server (default: 8448)\n"
|
||||||
|
"\n"
|
||||||
|
"Examples:"
|
||||||
|
"\n /server listfull"
|
||||||
|
"\n /server add matrix matrix.org:80"
|
||||||
|
"\n /server del matrix").format(
|
||||||
|
delimiter_color=W.color("chat_delimiters"),
|
||||||
|
cmd_color=W.color("chat_buffer"),
|
||||||
|
ncolor=W.color("reset"))
|
||||||
|
|
||||||
|
elif command == "help":
|
||||||
|
message = ("{delimiter_color}[{ncolor}matrix{delimiter_color}] "
|
||||||
|
"{ncolor}{cmd_color}/help{ncolor} "
|
||||||
|
"<matrix-command> [<matrix-command>...]"
|
||||||
|
"\n\n"
|
||||||
|
"display help about Matrix commands"
|
||||||
|
"\n\n"
|
||||||
|
"matrix-command: a Matrix command name"
|
||||||
|
"(internal name)").format(
|
||||||
|
delimiter_color=W.color("chat_delimiters"),
|
||||||
|
cmd_color=W.color("chat_buffer"),
|
||||||
|
ncolor=W.color("reset"))
|
||||||
|
|
||||||
|
else:
|
||||||
|
message = ("{prefix}matrix: No help available, \"{command}\" "
|
||||||
|
"is not a matrix command").format(
|
||||||
|
prefix=W.prefix("error"),
|
||||||
|
command=command)
|
||||||
|
|
||||||
|
W.prnt("", "")
|
||||||
|
W.prnt("", message)
|
||||||
|
|
||||||
|
|
||||||
|
def matrix_server_command(command, args):
|
||||||
def get_value_string(value, default_value):
|
def get_value_string(value, default_value):
|
||||||
if value == default_value:
|
if value == default_value:
|
||||||
if not value:
|
if not value:
|
||||||
|
@ -1055,7 +1140,7 @@ def matrix_server_command(command, args):
|
||||||
W.prnt("", "\nAll matrix servers:")
|
W.prnt("", "\nAll matrix servers:")
|
||||||
for server in SERVERS:
|
for server in SERVERS:
|
||||||
W.prnt("", " {color}{server}".format(
|
W.prnt("", " {color}{server}".format(
|
||||||
color=weechat_server_color(),
|
color=W.color("chat_server"),
|
||||||
server=server
|
server=server
|
||||||
))
|
))
|
||||||
|
|
||||||
|
@ -1069,9 +1154,6 @@ def matrix_server_command(command, args):
|
||||||
|
|
||||||
W.prnt("", "")
|
W.prnt("", "")
|
||||||
|
|
||||||
option = weechat.config_get("weechat.color.chat_delimiters")
|
|
||||||
delimiter_color = W.color(W.config_color(option))
|
|
||||||
|
|
||||||
if server.connected:
|
if server.connected:
|
||||||
connected = "connected"
|
connected = "connected"
|
||||||
else:
|
else:
|
||||||
|
@ -1080,11 +1162,11 @@ def matrix_server_command(command, args):
|
||||||
message = ("Server: {server_color}{server}{delimiter_color}"
|
message = ("Server: {server_color}{server}{delimiter_color}"
|
||||||
" [{ncolor}{connected}{delimiter_color}]"
|
" [{ncolor}{connected}{delimiter_color}]"
|
||||||
"{ncolor}").format(
|
"{ncolor}").format(
|
||||||
server_color=weechat_server_color(),
|
server_color=W.color("chat_server"),
|
||||||
server=server.name,
|
server=server.name,
|
||||||
delimiter_color=delimiter_color,
|
delimiter_color=W.color("chat_delimiters"),
|
||||||
connected=connected,
|
connected=connected,
|
||||||
ncolor=W.color("chat"))
|
ncolor=W.color("reset"))
|
||||||
|
|
||||||
W.prnt("", message)
|
W.prnt("", message)
|
||||||
|
|
||||||
|
@ -1146,8 +1228,8 @@ def matrix_server_command(command, args):
|
||||||
"connected to it. Try \"/matrix disconnect "
|
"connected to it. Try \"/matrix disconnect "
|
||||||
"{color}{server}{ncolor}\" before.").format(
|
"{color}{server}{ncolor}\" before.").format(
|
||||||
prefix=W.prefix("error"),
|
prefix=W.prefix("error"),
|
||||||
color=weechat_server_color(),
|
color=W.color("chat_server"),
|
||||||
ncolor=W.color("chat"),
|
ncolor=W.color("reset"),
|
||||||
server=server.name)
|
server=server.name)
|
||||||
W.prnt("", message)
|
W.prnt("", message)
|
||||||
return
|
return
|
||||||
|
@ -1164,8 +1246,8 @@ def matrix_server_command(command, args):
|
||||||
message = ("matrix: server {color}{server}{ncolor} has been "
|
message = ("matrix: server {color}{server}{ncolor} has been "
|
||||||
"deleted").format(
|
"deleted").format(
|
||||||
server=server.name,
|
server=server.name,
|
||||||
color=weechat_server_color(),
|
color=W.color("chat_server"),
|
||||||
ncolor=W.color("chat"))
|
ncolor=W.color("reset"))
|
||||||
|
|
||||||
del SERVERS[server.name]
|
del SERVERS[server.name]
|
||||||
server = None
|
server = None
|
||||||
|
@ -1197,9 +1279,9 @@ def matrix_server_command(command, args):
|
||||||
message = ("{prefix}matrix: server {color}{server}{ncolor} "
|
message = ("{prefix}matrix: server {color}{server}{ncolor} "
|
||||||
"already exists, can't add it").format(
|
"already exists, can't add it").format(
|
||||||
prefix=W.prefix("error"),
|
prefix=W.prefix("error"),
|
||||||
color=weechat_server_color(),
|
color=W.color("chat_server"),
|
||||||
server=server_name,
|
server=server_name,
|
||||||
ncolor=W.color("chat"))
|
ncolor=W.color("reset"))
|
||||||
W.prnt("", message)
|
W.prnt("", message)
|
||||||
return
|
return
|
||||||
|
|
||||||
|
@ -1224,9 +1306,9 @@ def matrix_server_command(command, args):
|
||||||
"{color}{server}{ncolor}, failed to add "
|
"{color}{server}{ncolor}, failed to add "
|
||||||
"server.").format(
|
"server.").format(
|
||||||
prefix=W.prefix("error"),
|
prefix=W.prefix("error"),
|
||||||
color=weechat_server_color(),
|
color=W.color("chat_server"),
|
||||||
server=server.name,
|
server=server.name,
|
||||||
ncolor=W.color("chat"))
|
ncolor=W.color("reset"))
|
||||||
|
|
||||||
W.prnt("", message)
|
W.prnt("", message)
|
||||||
server = None
|
server = None
|
||||||
|
@ -1244,9 +1326,9 @@ def matrix_server_command(command, args):
|
||||||
"{color}{server}{ncolor}, failed to add "
|
"{color}{server}{ncolor}, failed to add "
|
||||||
"server.").format(
|
"server.").format(
|
||||||
prefix=W.prefix("error"),
|
prefix=W.prefix("error"),
|
||||||
color=weechat_server_color(),
|
color=W.color("chat_server"),
|
||||||
server=server.name,
|
server=server.name,
|
||||||
ncolor=W.color("chat"))
|
ncolor=W.color("reset"))
|
||||||
|
|
||||||
W.prnt("", message)
|
W.prnt("", message)
|
||||||
server = None
|
server = None
|
||||||
|
@ -1266,9 +1348,9 @@ def matrix_server_command(command, args):
|
||||||
"{color}{server}{ncolor}, failed to add "
|
"{color}{server}{ncolor}, failed to add "
|
||||||
"server.").format(
|
"server.").format(
|
||||||
prefix=W.prefix("error"),
|
prefix=W.prefix("error"),
|
||||||
color=weechat_server_color(),
|
color=W.color("chat_server"),
|
||||||
server=server.name,
|
server=server.name,
|
||||||
ncolor=W.color("chat"))
|
ncolor=W.color("reset"))
|
||||||
|
|
||||||
W.prnt("", message)
|
W.prnt("", message)
|
||||||
server = None
|
server = None
|
||||||
|
@ -1288,9 +1370,9 @@ def matrix_server_command(command, args):
|
||||||
"{color}{server}{ncolor}, failed to add "
|
"{color}{server}{ncolor}, failed to add "
|
||||||
"server.").format(
|
"server.").format(
|
||||||
prefix=W.prefix("error"),
|
prefix=W.prefix("error"),
|
||||||
color=weechat_server_color(),
|
color=W.color("chat_server"),
|
||||||
server=server.name,
|
server=server.name,
|
||||||
ncolor=W.color("chat"))
|
ncolor=W.color("reset"))
|
||||||
W.prnt("", message)
|
W.prnt("", message)
|
||||||
server = None
|
server = None
|
||||||
return
|
return
|
||||||
|
@ -1298,8 +1380,8 @@ def matrix_server_command(command, args):
|
||||||
message = ("matrix: server {color}{server}{ncolor} "
|
message = ("matrix: server {color}{server}{ncolor} "
|
||||||
"has been added").format(
|
"has been added").format(
|
||||||
server=server.name,
|
server=server.name,
|
||||||
color=weechat_server_color(),
|
color=W.color("chat_server"),
|
||||||
ncolor=W.color("chat"))
|
ncolor=W.color("reset"))
|
||||||
W.prnt("", message)
|
W.prnt("", message)
|
||||||
|
|
||||||
# TODO the argument for list and listfull is used as a match word to
|
# TODO the argument for list and listfull is used as a match word to
|
||||||
|
@ -1361,6 +1443,9 @@ def matrix_command_cb(data, buffer, args):
|
||||||
else:
|
else:
|
||||||
matrix_server_command("list", "")
|
matrix_server_command("list", "")
|
||||||
|
|
||||||
|
elif command == 'help':
|
||||||
|
matrix_command_help(args)
|
||||||
|
|
||||||
else:
|
else:
|
||||||
print("Unknown command")
|
print("Unknown command")
|
||||||
|
|
||||||
|
@ -1411,11 +1496,23 @@ def server_command_completion_cb(data, completion_item, buffer, completion):
|
||||||
return W.WEECHAT_RC_OK
|
return W.WEECHAT_RC_OK
|
||||||
|
|
||||||
|
|
||||||
|
@utf8_decode
|
||||||
def matrix_server_completion_cb(data, completion_item, buffer, completion):
|
def matrix_server_completion_cb(data, completion_item, buffer, completion):
|
||||||
add_servers_to_completion(completion)
|
add_servers_to_completion(completion)
|
||||||
return W.WEECHAT_RC_OK
|
return W.WEECHAT_RC_OK
|
||||||
|
|
||||||
|
|
||||||
|
@utf8_decode
|
||||||
|
def matrix_command_completion_cb(data, completion_item, buffer, completion):
|
||||||
|
for command in ["connect", "disconnect", "reconnect", "server", "help"]:
|
||||||
|
W.hook_completion_list_add(
|
||||||
|
completion,
|
||||||
|
command,
|
||||||
|
0,
|
||||||
|
weechat.WEECHAT_LIST_POS_SORT)
|
||||||
|
return W.WEECHAT_RC_OK
|
||||||
|
|
||||||
|
|
||||||
def create_default_server(config_file):
|
def create_default_server(config_file):
|
||||||
server = MatrixServer('matrix.org', config_file)
|
server = MatrixServer('matrix.org', config_file)
|
||||||
SERVERS[server.name] = server
|
SERVERS[server.name] = server
|
||||||
|
@ -1441,6 +1538,13 @@ def init_hooks():
|
||||||
""
|
""
|
||||||
)
|
)
|
||||||
|
|
||||||
|
W.hook_completion(
|
||||||
|
"matrix_commands",
|
||||||
|
"Matrix command completion",
|
||||||
|
"matrix_command_completion_cb",
|
||||||
|
""
|
||||||
|
)
|
||||||
|
|
||||||
W.hook_command(
|
W.hook_command(
|
||||||
# Command name and short description
|
# Command name and short description
|
||||||
'matrix', 'Matrix chat protocol command',
|
'matrix', 'Matrix chat protocol command',
|
||||||
|
@ -1450,7 +1554,8 @@ def init_hooks():
|
||||||
'server delete|list|listfull <server-name> ||'
|
'server delete|list|listfull <server-name> ||'
|
||||||
'connect <server-name> ||'
|
'connect <server-name> ||'
|
||||||
'disconnect <server-name> ||'
|
'disconnect <server-name> ||'
|
||||||
'reconnect <server-name>'
|
'reconnect <server-name> ||'
|
||||||
|
'help <matrix-command>'
|
||||||
),
|
),
|
||||||
# Description
|
# Description
|
||||||
(
|
(
|
||||||
|
@ -1458,6 +1563,7 @@ def init_hooks():
|
||||||
' connect: connect to Matrix servers\n'
|
' connect: connect to Matrix servers\n'
|
||||||
'disconnect: disconnect from one or all Matrix servers\n'
|
'disconnect: disconnect from one or all Matrix servers\n'
|
||||||
' reconnect: reconnect to server(s)\n\n'
|
' reconnect: reconnect to server(s)\n\n'
|
||||||
|
' help: show detailed command help\n\n'
|
||||||
'Use /matrix help [command] to find out more\n'
|
'Use /matrix help [command] to find out more\n'
|
||||||
),
|
),
|
||||||
# Completions
|
# Completions
|
||||||
|
@ -1465,7 +1571,8 @@ def init_hooks():
|
||||||
'server %(matrix_server_commands)|%* ||'
|
'server %(matrix_server_commands)|%* ||'
|
||||||
'connect %(matrix_servers) ||'
|
'connect %(matrix_servers) ||'
|
||||||
'disconnect %(matrix_servers) ||'
|
'disconnect %(matrix_servers) ||'
|
||||||
'reconnect %(matrix_servers)'
|
'reconnect %(matrix_servers) ||'
|
||||||
|
'help %(matrix_commands)'
|
||||||
),
|
),
|
||||||
# Function name
|
# Function name
|
||||||
'matrix_command_cb', '')
|
'matrix_command_cb', '')
|
||||||
|
|
Loading…
Reference in a new issue