Move more commands out of the main file.
This commit is contained in:
parent
3f2559adeb
commit
5686d1f6c1
2 changed files with 570 additions and 568 deletions
570
matrix.py
570
matrix.py
|
@ -45,9 +45,11 @@ from matrix.socket import disconnect, send_or_queue, send
|
|||
from matrix.commands import (
|
||||
hook_commands,
|
||||
hook_page_up,
|
||||
matrix_command_cb,
|
||||
matrix_command_join_cb,
|
||||
matrix_command_part_cb,
|
||||
matrix_command_invite_cb,
|
||||
matrix_command_topic_cb,
|
||||
matrix_command_pgup_cb,
|
||||
matrix_redact_command_cb,
|
||||
matrix_command_buf_clear_cb,
|
||||
|
@ -1424,506 +1426,6 @@ def check_server_existence(server_name, servers):
|
|||
return True
|
||||
|
||||
|
||||
def matrix_command_debug(args):
|
||||
if not args:
|
||||
message = ("{prefix}matrix: Too few arguments for command "
|
||||
"\"/matrix debug\" (see the help for the command: "
|
||||
"/matrix help debug").format(prefix=W.prefix("error"))
|
||||
W.prnt("", message)
|
||||
return
|
||||
|
||||
def toggle_debug(debug_type):
|
||||
if debug_type in GLOBAL_OPTIONS.debug:
|
||||
message = ("{prefix}matrix: Disabling matrix {t} "
|
||||
"debugging.").format(
|
||||
prefix=W.prefix("error"),
|
||||
t=debug_type)
|
||||
W.prnt("", message)
|
||||
GLOBAL_OPTIONS.debug.remove(debug_type)
|
||||
else:
|
||||
message = ("{prefix}matrix: Enabling matrix {t} "
|
||||
"debugging.").format(
|
||||
prefix=W.prefix("error"),
|
||||
t=debug_type)
|
||||
W.prnt("", message)
|
||||
GLOBAL_OPTIONS.debug.append(debug_type)
|
||||
|
||||
for command in args:
|
||||
if command == "network":
|
||||
toggle_debug(DebugType.NETWORK)
|
||||
elif command == "messaging":
|
||||
toggle_debug(DebugType.MESSAGING)
|
||||
elif command == "timing":
|
||||
toggle_debug(DebugType.TIMING)
|
||||
else:
|
||||
message = ("{prefix}matrix: Unknown matrix debug "
|
||||
"type \"{t}\".").format(
|
||||
prefix=W.prefix("error"),
|
||||
t=command)
|
||||
W.prnt("", message)
|
||||
|
||||
|
||||
def matrix_command_help(args):
|
||||
if not args:
|
||||
message = ("{prefix}matrix: Too few arguments for command "
|
||||
"\"/matrix help\" (see the help for the command: "
|
||||
"/matrix help help").format(prefix=W.prefix("error"))
|
||||
W.prnt("", message)
|
||||
return
|
||||
|
||||
for command in args:
|
||||
message = ""
|
||||
|
||||
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"))
|
||||
|
||||
elif command == "debug":
|
||||
message = ("{delimiter_color}[{ncolor}matrix{delimiter_color}] "
|
||||
"{ncolor}{cmd_color}/debug{ncolor} "
|
||||
"<debug-type> [<debug-type>...]"
|
||||
"\n\n"
|
||||
"enable/disable degugging for a Matrix subsystem"
|
||||
"\n\n"
|
||||
"debug-type: a Matrix debug type, one of messaging, "
|
||||
"timing, network").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)
|
||||
|
||||
return
|
||||
|
||||
|
||||
def matrix_server_command_listfull(args):
|
||||
def get_value_string(value, default_value):
|
||||
if value == default_value:
|
||||
if not value:
|
||||
value = "''"
|
||||
value_string = " ({value})".format(value=value)
|
||||
else:
|
||||
value_string = "{color}{value}{ncolor}".format(
|
||||
color=W.color("chat_value"),
|
||||
value=value,
|
||||
ncolor=W.color("reset"))
|
||||
|
||||
return value_string
|
||||
|
||||
for server_name in args:
|
||||
if server_name not in SERVERS:
|
||||
continue
|
||||
|
||||
server = SERVERS[server_name]
|
||||
connected = ""
|
||||
|
||||
W.prnt("", "")
|
||||
|
||||
if server.connected:
|
||||
connected = "connected"
|
||||
else:
|
||||
connected = "not connected"
|
||||
|
||||
message = ("Server: {server_color}{server}{delimiter_color}"
|
||||
" [{ncolor}{connected}{delimiter_color}]"
|
||||
"{ncolor}").format(
|
||||
server_color=W.color("chat_server"),
|
||||
server=server.name,
|
||||
delimiter_color=W.color("chat_delimiters"),
|
||||
connected=connected,
|
||||
ncolor=W.color("reset"))
|
||||
|
||||
W.prnt("", message)
|
||||
|
||||
option = server.options["autoconnect"]
|
||||
default_value = W.config_string_default(option)
|
||||
value = W.config_string(option)
|
||||
|
||||
value_string = get_value_string(value, default_value)
|
||||
message = " autoconnect. : {value}".format(value=value_string)
|
||||
|
||||
W.prnt("", message)
|
||||
|
||||
option = server.options["address"]
|
||||
default_value = W.config_string_default(option)
|
||||
value = W.config_string(option)
|
||||
|
||||
value_string = get_value_string(value, default_value)
|
||||
message = " address. . . : {value}".format(value=value_string)
|
||||
|
||||
W.prnt("", message)
|
||||
|
||||
option = server.options["port"]
|
||||
default_value = str(W.config_integer_default(option))
|
||||
value = str(W.config_integer(option))
|
||||
|
||||
value_string = get_value_string(value, default_value)
|
||||
message = " port . . . . : {value}".format(value=value_string)
|
||||
|
||||
W.prnt("", message)
|
||||
|
||||
option = server.options["username"]
|
||||
default_value = W.config_string_default(option)
|
||||
value = W.config_string(option)
|
||||
|
||||
value_string = get_value_string(value, default_value)
|
||||
message = " username . . : {value}".format(value=value_string)
|
||||
|
||||
W.prnt("", message)
|
||||
|
||||
option = server.options["password"]
|
||||
value = W.config_string(option)
|
||||
|
||||
if value:
|
||||
value = "(hidden)"
|
||||
|
||||
value_string = get_value_string(value, '')
|
||||
message = " password . . : {value}".format(value=value_string)
|
||||
|
||||
W.prnt("", message)
|
||||
|
||||
|
||||
def matrix_server_command_delete(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 "
|
||||
"{color}{server}{ncolor} because you are "
|
||||
"connected to it. Try \"/matrix disconnect "
|
||||
"{color}{server}{ncolor}\" before.").format(
|
||||
prefix=W.prefix("error"),
|
||||
color=W.color("chat_server"),
|
||||
ncolor=W.color("reset"),
|
||||
server=server.name)
|
||||
W.prnt("", message)
|
||||
return
|
||||
|
||||
for buf in server.buffers.values():
|
||||
W.buffer_close(buf)
|
||||
|
||||
if server.server_buffer:
|
||||
W.buffer_close(server.server_buffer)
|
||||
|
||||
for option in server.options.values():
|
||||
W.config_option_free(option)
|
||||
|
||||
message = ("matrix: server {color}{server}{ncolor} has been "
|
||||
"deleted").format(
|
||||
server=server.name,
|
||||
color=W.color("chat_server"),
|
||||
ncolor=W.color("reset"))
|
||||
|
||||
del SERVERS[server.name]
|
||||
server = None
|
||||
|
||||
W.prnt("", message)
|
||||
|
||||
|
||||
def matrix_server_command_add(args):
|
||||
if len(args) < 2:
|
||||
message = ("{prefix}matrix: Too few arguments for command "
|
||||
"\"/matrix server add\" (see the help for the command: "
|
||||
"/matrix help server").format(prefix=W.prefix("error"))
|
||||
W.prnt("", message)
|
||||
return
|
||||
elif len(args) > 4:
|
||||
message = ("{prefix}matrix: Too many arguments for command "
|
||||
"\"/matrix server add\" (see the help for the command: "
|
||||
"/matrix help server").format(prefix=W.prefix("error"))
|
||||
W.prnt("", message)
|
||||
return
|
||||
|
||||
def remove_server(server):
|
||||
for option in server.options.values():
|
||||
W.config_option_free(option)
|
||||
del SERVERS[server.name]
|
||||
|
||||
server_name = args[0]
|
||||
|
||||
if server_name in SERVERS:
|
||||
message = ("{prefix}matrix: server {color}{server}{ncolor} "
|
||||
"already exists, can't add it").format(
|
||||
prefix=W.prefix("error"),
|
||||
color=W.color("chat_server"),
|
||||
server=server_name,
|
||||
ncolor=W.color("reset"))
|
||||
W.prnt("", message)
|
||||
return
|
||||
|
||||
server = MatrixServer(args[0], W, CONFIG)
|
||||
SERVERS[server.name] = server
|
||||
|
||||
if len(args) >= 2:
|
||||
try:
|
||||
host, port = args[1].split(":", 1)
|
||||
except ValueError:
|
||||
host, port = args[1], None
|
||||
|
||||
return_code = W.config_option_set(
|
||||
server.options["address"],
|
||||
host,
|
||||
1
|
||||
)
|
||||
|
||||
if return_code == W.WEECHAT_CONFIG_OPTION_SET_ERROR:
|
||||
remove_server(server)
|
||||
message = ("{prefix}Failed to set address for server "
|
||||
"{color}{server}{ncolor}, failed to add "
|
||||
"server.").format(
|
||||
prefix=W.prefix("error"),
|
||||
color=W.color("chat_server"),
|
||||
server=server.name,
|
||||
ncolor=W.color("reset"))
|
||||
|
||||
W.prnt("", message)
|
||||
server = None
|
||||
return
|
||||
|
||||
if port:
|
||||
return_code = W.config_option_set(
|
||||
server.options["port"],
|
||||
port,
|
||||
1
|
||||
)
|
||||
if return_code == W.WEECHAT_CONFIG_OPTION_SET_ERROR:
|
||||
remove_server(server)
|
||||
message = ("{prefix}Failed to set port for server "
|
||||
"{color}{server}{ncolor}, failed to add "
|
||||
"server.").format(
|
||||
prefix=W.prefix("error"),
|
||||
color=W.color("chat_server"),
|
||||
server=server.name,
|
||||
ncolor=W.color("reset"))
|
||||
|
||||
W.prnt("", message)
|
||||
server = None
|
||||
return
|
||||
|
||||
if len(args) >= 3:
|
||||
user = args[2]
|
||||
return_code = W.config_option_set(
|
||||
server.options["username"],
|
||||
user,
|
||||
1
|
||||
)
|
||||
|
||||
if return_code == W.WEECHAT_CONFIG_OPTION_SET_ERROR:
|
||||
remove_server(server)
|
||||
message = ("{prefix}Failed to set user for server "
|
||||
"{color}{server}{ncolor}, failed to add "
|
||||
"server.").format(
|
||||
prefix=W.prefix("error"),
|
||||
color=W.color("chat_server"),
|
||||
server=server.name,
|
||||
ncolor=W.color("reset"))
|
||||
|
||||
W.prnt("", message)
|
||||
server = None
|
||||
return
|
||||
|
||||
if len(args) == 4:
|
||||
password = args[3]
|
||||
|
||||
return_code = W.config_option_set(
|
||||
server.options["password"],
|
||||
password,
|
||||
1
|
||||
)
|
||||
if return_code == W.WEECHAT_CONFIG_OPTION_SET_ERROR:
|
||||
remove_server(server)
|
||||
message = ("{prefix}Failed to set password for server "
|
||||
"{color}{server}{ncolor}, failed to add "
|
||||
"server.").format(
|
||||
prefix=W.prefix("error"),
|
||||
color=W.color("chat_server"),
|
||||
server=server.name,
|
||||
ncolor=W.color("reset"))
|
||||
W.prnt("", message)
|
||||
server = None
|
||||
return
|
||||
|
||||
message = ("matrix: server {color}{server}{ncolor} "
|
||||
"has been added").format(
|
||||
server=server.name,
|
||||
color=W.color("chat_server"),
|
||||
ncolor=W.color("reset"))
|
||||
W.prnt("", message)
|
||||
|
||||
|
||||
def matrix_server_command(command, args):
|
||||
def list_servers(_):
|
||||
if SERVERS:
|
||||
W.prnt("", "\nAll matrix servers:")
|
||||
for server in SERVERS:
|
||||
W.prnt("", " {color}{server}".format(
|
||||
color=W.color("chat_server"),
|
||||
server=server
|
||||
))
|
||||
|
||||
# TODO the argument for list and listfull is used as a match word to
|
||||
# find/filter servers, we're currently match exactly to the whole name
|
||||
if command == 'list':
|
||||
list_servers(args)
|
||||
elif command == 'listfull':
|
||||
matrix_server_command_listfull(args)
|
||||
elif command == 'add':
|
||||
matrix_server_command_add(args)
|
||||
elif command == 'delete':
|
||||
matrix_server_command_delete(args)
|
||||
else:
|
||||
message = ("{prefix}matrix: Error: unknown matrix server command, "
|
||||
"\"{command}\" (type /matrix help server for help)").format(
|
||||
prefix=W.prefix("error"),
|
||||
command=command)
|
||||
W.prnt("", message)
|
||||
|
||||
|
||||
@utf8_decode
|
||||
def matrix_command_cb(data, buffer, args):
|
||||
def connect_server(args):
|
||||
for server_name in args:
|
||||
if check_server_existence(server_name, SERVERS):
|
||||
server = SERVERS[server_name]
|
||||
connect(server)
|
||||
|
||||
def disconnect_server(args):
|
||||
for server_name in args:
|
||||
if check_server_existence(server_name, SERVERS):
|
||||
server = SERVERS[server_name]
|
||||
if server.connected:
|
||||
W.unhook(server.timer_hook)
|
||||
server.timer_hook = None
|
||||
server.access_token = ""
|
||||
disconnect(server)
|
||||
|
||||
split_args = list(filter(bool, args.split(' ')))
|
||||
|
||||
if len(split_args) < 1:
|
||||
message = ("{prefix}matrix: Too few arguments for command "
|
||||
"\"/matrix\" (see the help for the command: "
|
||||
"/help matrix").format(prefix=W.prefix("error"))
|
||||
W.prnt("", message)
|
||||
return W.WEECHAT_RC_ERROR
|
||||
|
||||
command, args = split_args[0], split_args[1:]
|
||||
|
||||
if command == 'connect':
|
||||
connect_server(args)
|
||||
|
||||
elif command == 'disconnect':
|
||||
disconnect_server(args)
|
||||
|
||||
elif command == 'reconnect':
|
||||
disconnect_server(args)
|
||||
connect_server(args)
|
||||
|
||||
elif command == 'server':
|
||||
if len(args) >= 1:
|
||||
subcommand, args = args[0], args[1:]
|
||||
matrix_server_command(subcommand, args)
|
||||
else:
|
||||
matrix_server_command("list", "")
|
||||
|
||||
elif command == 'help':
|
||||
matrix_command_help(args)
|
||||
|
||||
elif command == 'debug':
|
||||
matrix_command_debug(args)
|
||||
|
||||
else:
|
||||
message = ("{prefix}matrix: Error: unknown matrix command, "
|
||||
"\"{command}\" (type /help matrix for help)").format(
|
||||
prefix=W.prefix("error"),
|
||||
command=command)
|
||||
W.prnt("", message)
|
||||
|
||||
return W.WEECHAT_RC_OK
|
||||
|
||||
|
||||
def add_servers_to_completion(completion):
|
||||
for server_name in SERVERS:
|
||||
W.hook_completion_list_add(
|
||||
|
@ -2001,74 +1503,6 @@ def create_default_server(config_file):
|
|||
return True
|
||||
|
||||
|
||||
@utf8_decode
|
||||
def matrix_command_topic_cb(data, buffer, command):
|
||||
for server in SERVERS.values():
|
||||
if buffer in server.buffers.values():
|
||||
topic = None
|
||||
room_id = key_from_value(server.buffers, buffer)
|
||||
split_command = command.split(' ', 1)
|
||||
|
||||
if len(split_command) == 2:
|
||||
topic = split_command[1]
|
||||
|
||||
if not topic:
|
||||
room = server.rooms[room_id]
|
||||
if not room.topic:
|
||||
return W.WEECHAT_RC_OK
|
||||
|
||||
message = ("{prefix}Topic for {color}{room}{ncolor} is "
|
||||
"\"{topic}\"").format(
|
||||
prefix=W.prefix("network"),
|
||||
color=W.color("chat_buffer"),
|
||||
ncolor=W.color("reset"),
|
||||
room=room.alias,
|
||||
topic=room.topic)
|
||||
|
||||
date = int(time.time())
|
||||
topic_date = room.topic_date.strftime("%a, %d %b %Y "
|
||||
"%H:%M:%S")
|
||||
|
||||
tags = "matrix_topic,log1"
|
||||
W.prnt_date_tags(buffer, date, tags, message)
|
||||
|
||||
# TODO the nick should be colored
|
||||
|
||||
# TODO we should use the display name as well as
|
||||
# the user name here
|
||||
message = ("{prefix}Topic set by {author} on "
|
||||
"{date}").format(
|
||||
prefix=W.prefix("network"),
|
||||
author=room.topic_author,
|
||||
date=topic_date)
|
||||
W.prnt_date_tags(buffer, date, tags, message)
|
||||
|
||||
return W.WEECHAT_RC_OK_EAT
|
||||
|
||||
body = {"topic": topic}
|
||||
|
||||
message = MatrixMessage(
|
||||
server,
|
||||
GLOBAL_OPTIONS,
|
||||
MessageType.STATE,
|
||||
data=body,
|
||||
room_id=room_id,
|
||||
extra_id="m.room.topic"
|
||||
)
|
||||
send_or_queue(server, message)
|
||||
|
||||
return W.WEECHAT_RC_OK_EAT
|
||||
|
||||
elif buffer == server.server_buffer:
|
||||
message = ("{prefix}matrix: command \"topic\" must be "
|
||||
"executed on a Matrix channel buffer").format(
|
||||
prefix=W.prefix("error"))
|
||||
W.prnt(buffer, message)
|
||||
return W.WEECHAT_RC_OK_EAT
|
||||
|
||||
return W.WEECHAT_RC_OK
|
||||
|
||||
|
||||
@utf8_decode
|
||||
def matrix_bar_item_plugin(data, item, window, buffer, extra_info):
|
||||
# pylint: disable=unused-argument
|
||||
|
|
|
@ -457,3 +457,571 @@ def matrix_message_completion_cb(data, completion_item, buffer, completion):
|
|||
line = W.hdata_move(W.hdata_get('line'), line, -1)
|
||||
|
||||
return W.WEECHAT_RC_OK
|
||||
|
||||
|
||||
def matrix_command_debug(args):
|
||||
if not args:
|
||||
message = ("{prefix}matrix: Too few arguments for command "
|
||||
"\"/matrix debug\" (see the help for the command: "
|
||||
"/matrix help debug").format(prefix=W.prefix("error"))
|
||||
W.prnt("", message)
|
||||
return
|
||||
|
||||
def toggle_debug(debug_type):
|
||||
if debug_type in GLOBAL_OPTIONS.debug:
|
||||
message = ("{prefix}matrix: Disabling matrix {t} "
|
||||
"debugging.").format(
|
||||
prefix=W.prefix("error"),
|
||||
t=debug_type)
|
||||
W.prnt("", message)
|
||||
GLOBAL_OPTIONS.debug.remove(debug_type)
|
||||
else:
|
||||
message = ("{prefix}matrix: Enabling matrix {t} "
|
||||
"debugging.").format(
|
||||
prefix=W.prefix("error"),
|
||||
t=debug_type)
|
||||
W.prnt("", message)
|
||||
GLOBAL_OPTIONS.debug.append(debug_type)
|
||||
|
||||
for command in args:
|
||||
if command == "network":
|
||||
toggle_debug(DebugType.NETWORK)
|
||||
elif command == "messaging":
|
||||
toggle_debug(DebugType.MESSAGING)
|
||||
elif command == "timing":
|
||||
toggle_debug(DebugType.TIMING)
|
||||
else:
|
||||
message = ("{prefix}matrix: Unknown matrix debug "
|
||||
"type \"{t}\".").format(
|
||||
prefix=W.prefix("error"),
|
||||
t=command)
|
||||
W.prnt("", message)
|
||||
|
||||
|
||||
def matrix_command_help(args):
|
||||
if not args:
|
||||
message = ("{prefix}matrix: Too few arguments for command "
|
||||
"\"/matrix help\" (see the help for the command: "
|
||||
"/matrix help help").format(prefix=W.prefix("error"))
|
||||
W.prnt("", message)
|
||||
return
|
||||
|
||||
for command in args:
|
||||
message = ""
|
||||
|
||||
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"))
|
||||
|
||||
elif command == "debug":
|
||||
message = ("{delimiter_color}[{ncolor}matrix{delimiter_color}] "
|
||||
"{ncolor}{cmd_color}/debug{ncolor} "
|
||||
"<debug-type> [<debug-type>...]"
|
||||
"\n\n"
|
||||
"enable/disable degugging for a Matrix subsystem"
|
||||
"\n\n"
|
||||
"debug-type: a Matrix debug type, one of messaging, "
|
||||
"timing, network").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)
|
||||
|
||||
return
|
||||
|
||||
|
||||
def matrix_server_command_listfull(args):
|
||||
def get_value_string(value, default_value):
|
||||
if value == default_value:
|
||||
if not value:
|
||||
value = "''"
|
||||
value_string = " ({value})".format(value=value)
|
||||
else:
|
||||
value_string = "{color}{value}{ncolor}".format(
|
||||
color=W.color("chat_value"),
|
||||
value=value,
|
||||
ncolor=W.color("reset"))
|
||||
|
||||
return value_string
|
||||
|
||||
for server_name in args:
|
||||
if server_name not in SERVERS:
|
||||
continue
|
||||
|
||||
server = SERVERS[server_name]
|
||||
connected = ""
|
||||
|
||||
W.prnt("", "")
|
||||
|
||||
if server.connected:
|
||||
connected = "connected"
|
||||
else:
|
||||
connected = "not connected"
|
||||
|
||||
message = ("Server: {server_color}{server}{delimiter_color}"
|
||||
" [{ncolor}{connected}{delimiter_color}]"
|
||||
"{ncolor}").format(
|
||||
server_color=W.color("chat_server"),
|
||||
server=server.name,
|
||||
delimiter_color=W.color("chat_delimiters"),
|
||||
connected=connected,
|
||||
ncolor=W.color("reset"))
|
||||
|
||||
W.prnt("", message)
|
||||
|
||||
option = server.options["autoconnect"]
|
||||
default_value = W.config_string_default(option)
|
||||
value = W.config_string(option)
|
||||
|
||||
value_string = get_value_string(value, default_value)
|
||||
message = " autoconnect. : {value}".format(value=value_string)
|
||||
|
||||
W.prnt("", message)
|
||||
|
||||
option = server.options["address"]
|
||||
default_value = W.config_string_default(option)
|
||||
value = W.config_string(option)
|
||||
|
||||
value_string = get_value_string(value, default_value)
|
||||
message = " address. . . : {value}".format(value=value_string)
|
||||
|
||||
W.prnt("", message)
|
||||
|
||||
option = server.options["port"]
|
||||
default_value = str(W.config_integer_default(option))
|
||||
value = str(W.config_integer(option))
|
||||
|
||||
value_string = get_value_string(value, default_value)
|
||||
message = " port . . . . : {value}".format(value=value_string)
|
||||
|
||||
W.prnt("", message)
|
||||
|
||||
option = server.options["username"]
|
||||
default_value = W.config_string_default(option)
|
||||
value = W.config_string(option)
|
||||
|
||||
value_string = get_value_string(value, default_value)
|
||||
message = " username . . : {value}".format(value=value_string)
|
||||
|
||||
W.prnt("", message)
|
||||
|
||||
option = server.options["password"]
|
||||
value = W.config_string(option)
|
||||
|
||||
if value:
|
||||
value = "(hidden)"
|
||||
|
||||
value_string = get_value_string(value, '')
|
||||
message = " password . . : {value}".format(value=value_string)
|
||||
|
||||
W.prnt("", message)
|
||||
|
||||
|
||||
def matrix_server_command_delete(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 "
|
||||
"{color}{server}{ncolor} because you are "
|
||||
"connected to it. Try \"/matrix disconnect "
|
||||
"{color}{server}{ncolor}\" before.").format(
|
||||
prefix=W.prefix("error"),
|
||||
color=W.color("chat_server"),
|
||||
ncolor=W.color("reset"),
|
||||
server=server.name)
|
||||
W.prnt("", message)
|
||||
return
|
||||
|
||||
for buf in server.buffers.values():
|
||||
W.buffer_close(buf)
|
||||
|
||||
if server.server_buffer:
|
||||
W.buffer_close(server.server_buffer)
|
||||
|
||||
for option in server.options.values():
|
||||
W.config_option_free(option)
|
||||
|
||||
message = ("matrix: server {color}{server}{ncolor} has been "
|
||||
"deleted").format(
|
||||
server=server.name,
|
||||
color=W.color("chat_server"),
|
||||
ncolor=W.color("reset"))
|
||||
|
||||
del SERVERS[server.name]
|
||||
server = None
|
||||
|
||||
W.prnt("", message)
|
||||
|
||||
|
||||
def matrix_server_command_add(args):
|
||||
if len(args) < 2:
|
||||
message = ("{prefix}matrix: Too few arguments for command "
|
||||
"\"/matrix server add\" (see the help for the command: "
|
||||
"/matrix help server").format(prefix=W.prefix("error"))
|
||||
W.prnt("", message)
|
||||
return
|
||||
elif len(args) > 4:
|
||||
message = ("{prefix}matrix: Too many arguments for command "
|
||||
"\"/matrix server add\" (see the help for the command: "
|
||||
"/matrix help server").format(prefix=W.prefix("error"))
|
||||
W.prnt("", message)
|
||||
return
|
||||
|
||||
def remove_server(server):
|
||||
for option in server.options.values():
|
||||
W.config_option_free(option)
|
||||
del SERVERS[server.name]
|
||||
|
||||
server_name = args[0]
|
||||
|
||||
if server_name in SERVERS:
|
||||
message = ("{prefix}matrix: server {color}{server}{ncolor} "
|
||||
"already exists, can't add it").format(
|
||||
prefix=W.prefix("error"),
|
||||
color=W.color("chat_server"),
|
||||
server=server_name,
|
||||
ncolor=W.color("reset"))
|
||||
W.prnt("", message)
|
||||
return
|
||||
|
||||
server = MatrixServer(args[0], W, CONFIG)
|
||||
SERVERS[server.name] = server
|
||||
|
||||
if len(args) >= 2:
|
||||
try:
|
||||
host, port = args[1].split(":", 1)
|
||||
except ValueError:
|
||||
host, port = args[1], None
|
||||
|
||||
return_code = W.config_option_set(
|
||||
server.options["address"],
|
||||
host,
|
||||
1
|
||||
)
|
||||
|
||||
if return_code == W.WEECHAT_CONFIG_OPTION_SET_ERROR:
|
||||
remove_server(server)
|
||||
message = ("{prefix}Failed to set address for server "
|
||||
"{color}{server}{ncolor}, failed to add "
|
||||
"server.").format(
|
||||
prefix=W.prefix("error"),
|
||||
color=W.color("chat_server"),
|
||||
server=server.name,
|
||||
ncolor=W.color("reset"))
|
||||
|
||||
W.prnt("", message)
|
||||
server = None
|
||||
return
|
||||
|
||||
if port:
|
||||
return_code = W.config_option_set(
|
||||
server.options["port"],
|
||||
port,
|
||||
1
|
||||
)
|
||||
if return_code == W.WEECHAT_CONFIG_OPTION_SET_ERROR:
|
||||
remove_server(server)
|
||||
message = ("{prefix}Failed to set port for server "
|
||||
"{color}{server}{ncolor}, failed to add "
|
||||
"server.").format(
|
||||
prefix=W.prefix("error"),
|
||||
color=W.color("chat_server"),
|
||||
server=server.name,
|
||||
ncolor=W.color("reset"))
|
||||
|
||||
W.prnt("", message)
|
||||
server = None
|
||||
return
|
||||
|
||||
if len(args) >= 3:
|
||||
user = args[2]
|
||||
return_code = W.config_option_set(
|
||||
server.options["username"],
|
||||
user,
|
||||
1
|
||||
)
|
||||
|
||||
if return_code == W.WEECHAT_CONFIG_OPTION_SET_ERROR:
|
||||
remove_server(server)
|
||||
message = ("{prefix}Failed to set user for server "
|
||||
"{color}{server}{ncolor}, failed to add "
|
||||
"server.").format(
|
||||
prefix=W.prefix("error"),
|
||||
color=W.color("chat_server"),
|
||||
server=server.name,
|
||||
ncolor=W.color("reset"))
|
||||
|
||||
W.prnt("", message)
|
||||
server = None
|
||||
return
|
||||
|
||||
if len(args) == 4:
|
||||
password = args[3]
|
||||
|
||||
return_code = W.config_option_set(
|
||||
server.options["password"],
|
||||
password,
|
||||
1
|
||||
)
|
||||
if return_code == W.WEECHAT_CONFIG_OPTION_SET_ERROR:
|
||||
remove_server(server)
|
||||
message = ("{prefix}Failed to set password for server "
|
||||
"{color}{server}{ncolor}, failed to add "
|
||||
"server.").format(
|
||||
prefix=W.prefix("error"),
|
||||
color=W.color("chat_server"),
|
||||
server=server.name,
|
||||
ncolor=W.color("reset"))
|
||||
W.prnt("", message)
|
||||
server = None
|
||||
return
|
||||
|
||||
message = ("matrix: server {color}{server}{ncolor} "
|
||||
"has been added").format(
|
||||
server=server.name,
|
||||
color=W.color("chat_server"),
|
||||
ncolor=W.color("reset"))
|
||||
W.prnt("", message)
|
||||
|
||||
|
||||
def matrix_server_command(command, args):
|
||||
def list_servers(_):
|
||||
if SERVERS:
|
||||
W.prnt("", "\nAll matrix servers:")
|
||||
for server in SERVERS:
|
||||
W.prnt("", " {color}{server}".format(
|
||||
color=W.color("chat_server"),
|
||||
server=server
|
||||
))
|
||||
|
||||
# TODO the argument for list and listfull is used as a match word to
|
||||
# find/filter servers, we're currently match exactly to the whole name
|
||||
if command == 'list':
|
||||
list_servers(args)
|
||||
elif command == 'listfull':
|
||||
matrix_server_command_listfull(args)
|
||||
elif command == 'add':
|
||||
matrix_server_command_add(args)
|
||||
elif command == 'delete':
|
||||
matrix_server_command_delete(args)
|
||||
else:
|
||||
message = ("{prefix}matrix: Error: unknown matrix server command, "
|
||||
"\"{command}\" (type /matrix help server for help)").format(
|
||||
prefix=W.prefix("error"),
|
||||
command=command)
|
||||
W.prnt("", message)
|
||||
|
||||
|
||||
@utf8_decode
|
||||
def matrix_command_cb(data, buffer, args):
|
||||
def connect_server(args):
|
||||
for server_name in args:
|
||||
if check_server_existence(server_name, SERVERS):
|
||||
server = SERVERS[server_name]
|
||||
connect(server)
|
||||
|
||||
def disconnect_server(args):
|
||||
for server_name in args:
|
||||
if check_server_existence(server_name, SERVERS):
|
||||
server = SERVERS[server_name]
|
||||
if server.connected:
|
||||
W.unhook(server.timer_hook)
|
||||
server.timer_hook = None
|
||||
server.access_token = ""
|
||||
disconnect(server)
|
||||
|
||||
split_args = list(filter(bool, args.split(' ')))
|
||||
|
||||
if len(split_args) < 1:
|
||||
message = ("{prefix}matrix: Too few arguments for command "
|
||||
"\"/matrix\" (see the help for the command: "
|
||||
"/help matrix").format(prefix=W.prefix("error"))
|
||||
W.prnt("", message)
|
||||
return W.WEECHAT_RC_ERROR
|
||||
|
||||
command, args = split_args[0], split_args[1:]
|
||||
|
||||
if command == 'connect':
|
||||
connect_server(args)
|
||||
|
||||
elif command == 'disconnect':
|
||||
disconnect_server(args)
|
||||
|
||||
elif command == 'reconnect':
|
||||
disconnect_server(args)
|
||||
connect_server(args)
|
||||
|
||||
elif command == 'server':
|
||||
if len(args) >= 1:
|
||||
subcommand, args = args[0], args[1:]
|
||||
matrix_server_command(subcommand, args)
|
||||
else:
|
||||
matrix_server_command("list", "")
|
||||
|
||||
elif command == 'help':
|
||||
matrix_command_help(args)
|
||||
|
||||
elif command == 'debug':
|
||||
matrix_command_debug(args)
|
||||
|
||||
else:
|
||||
message = ("{prefix}matrix: Error: unknown matrix command, "
|
||||
"\"{command}\" (type /help matrix for help)").format(
|
||||
prefix=W.prefix("error"),
|
||||
command=command)
|
||||
W.prnt("", message)
|
||||
|
||||
return W.WEECHAT_RC_OK
|
||||
|
||||
|
||||
@utf8_decode
|
||||
def matrix_command_topic_cb(data, buffer, command):
|
||||
for server in SERVERS.values():
|
||||
if buffer in server.buffers.values():
|
||||
topic = None
|
||||
room_id = key_from_value(server.buffers, buffer)
|
||||
split_command = command.split(' ', 1)
|
||||
|
||||
if len(split_command) == 2:
|
||||
topic = split_command[1]
|
||||
|
||||
if not topic:
|
||||
room = server.rooms[room_id]
|
||||
if not room.topic:
|
||||
return W.WEECHAT_RC_OK
|
||||
|
||||
message = ("{prefix}Topic for {color}{room}{ncolor} is "
|
||||
"\"{topic}\"").format(
|
||||
prefix=W.prefix("network"),
|
||||
color=W.color("chat_buffer"),
|
||||
ncolor=W.color("reset"),
|
||||
room=room.alias,
|
||||
topic=room.topic)
|
||||
|
||||
date = int(time.time())
|
||||
topic_date = room.topic_date.strftime("%a, %d %b %Y "
|
||||
"%H:%M:%S")
|
||||
|
||||
tags = "matrix_topic,log1"
|
||||
W.prnt_date_tags(buffer, date, tags, message)
|
||||
|
||||
# TODO the nick should be colored
|
||||
|
||||
# TODO we should use the display name as well as
|
||||
# the user name here
|
||||
message = ("{prefix}Topic set by {author} on "
|
||||
"{date}").format(
|
||||
prefix=W.prefix("network"),
|
||||
author=room.topic_author,
|
||||
date=topic_date)
|
||||
W.prnt_date_tags(buffer, date, tags, message)
|
||||
|
||||
return W.WEECHAT_RC_OK_EAT
|
||||
|
||||
body = {"topic": topic}
|
||||
|
||||
message = MatrixMessage(
|
||||
server,
|
||||
GLOBAL_OPTIONS,
|
||||
MessageType.STATE,
|
||||
data=body,
|
||||
room_id=room_id,
|
||||
extra_id="m.room.topic"
|
||||
)
|
||||
send_or_queue(server, message)
|
||||
|
||||
return W.WEECHAT_RC_OK_EAT
|
||||
|
||||
elif buffer == server.server_buffer:
|
||||
message = ("{prefix}matrix: command \"topic\" must be "
|
||||
"executed on a Matrix channel buffer").format(
|
||||
prefix=W.prefix("error"))
|
||||
W.prnt(buffer, message)
|
||||
return W.WEECHAT_RC_OK_EAT
|
||||
|
||||
return W.WEECHAT_RC_OK
|
||||
|
|
Loading…
Add table
Reference in a new issue