commands: Add invite support.

This commit is contained in:
Damir Jelić 2018-08-22 17:13:25 +02:00
parent c3ab43b02e
commit 53d2152a21
3 changed files with 46 additions and 22 deletions

View file

@ -44,7 +44,7 @@ from matrix.utf import utf8_decode
# file, import the callbacks here so weechat can find them. # file, import the callbacks here so weechat can find them.
from matrix.commands import (hook_commands, hook_page_up, matrix_command_cb, from matrix.commands import (hook_commands, hook_page_up, matrix_command_cb,
matrix_topic_command_cb, matrix_command_join_cb, matrix_topic_command_cb, matrix_command_join_cb,
matrix_command_part_cb, matrix_command_invite_cb, matrix_command_part_cb, matrix_invite_command_cb,
matrix_command_pgup_cb, matrix_redact_command_cb, matrix_command_pgup_cb, matrix_redact_command_cb,
matrix_command_buf_clear_cb, matrix_me_command_cb, matrix_command_buf_clear_cb, matrix_me_command_cb,
matrix_kick_command_cb) matrix_kick_command_cb)

View file

@ -72,6 +72,13 @@ class WeechatCommandParser(object):
return WeechatCommandParser._run_parser(parser, args) return WeechatCommandParser._run_parser(parser, args)
@staticmethod
def invite(args):
parser = WeechatArgParse(prog="invite")
parser.add_argument("user_id")
return WeechatCommandParser._run_parser(parser, args)
def hook_commands(): def hook_commands():
W.hook_command( W.hook_command(
@ -164,10 +171,23 @@ def hook_commands():
"matrix_kick_command_cb", "matrix_kick_command_cb",
"") "")
W.hook_command(
# Command name and short description
"invite",
"invite a user to the current room",
# Synopsis
("<user-id>"),
# Description
("user-id: user-id to invite"),
# Completions
("%(matrix_users)"),
# Callback
"matrix_invite_command_cb",
"")
# TODO those should be hook_command() calls # TODO those should be hook_command() calls
# W.hook_command_run('/join', 'matrix_command_join_cb', '') # W.hook_command_run('/join', 'matrix_command_join_cb', '')
# W.hook_command_run('/part', 'matrix_command_part_cb', '') # W.hook_command_run('/part', 'matrix_command_part_cb', '')
# W.hook_command_run('/invite', 'matrix_command_invite_cb', '')
W.hook_command_run('/buffer clear', 'matrix_command_buf_clear_cb', '') W.hook_command_run('/buffer clear', 'matrix_command_buf_clear_cb', '')
@ -363,28 +383,26 @@ def matrix_command_part_cb(data, buffer, command):
@utf8_decode @utf8_decode
def matrix_command_invite_cb(data, buffer, command): def matrix_invite_command_cb(data, buffer, args):
parsed_args = WeechatCommandParser.invite(args)
def invite(server, buf, args): if not parsed_args:
split_args = args.split(" ", 1) return W.WEECHAT_RC_OK
# TODO handle join for non public rooms
if len(split_args) != 2:
message = (
"{prefix}Error with command \"/invite\" (help on "
"command: /help invite)").format(prefix=W.prefix("error"))
W.prnt("", message)
return
_, invitee = split_args
room_id = key_from_value(server.buffers, buf)
raise NotImplementedError
for server in SERVERS.values(): for server in SERVERS.values():
if buffer in server.buffers.values(): if buffer == server.server_buffer:
invite(server, buffer, command) server.error("command \"invite\" must be "
return W.WEECHAT_RC_OK_EAT "executed on a Matrix room buffer")
return W.WEECHAT_RC_OK
room = server.find_room_from_ptr(buffer)
if not room:
continue
user_id = parsed_args.user_id
user_id = user_id if user_id.startswith("@") else "@" + user_id
server.room_invite(room, user_id)
break
return W.WEECHAT_RC_OK return W.WEECHAT_RC_OK

View file

@ -522,6 +522,12 @@ class MatrixServer(object):
reason) reason)
self.send_or_queue(request) self.send_or_queue(request)
def room_invite(self, room_buffer, user_id):
_, request = self.client.room_invite(
room_buffer.room.room_id,
user_id)
self.send_or_queue(request)
def room_send_message(self, room_buffer, formatted, msgtype="m.text"): def room_send_message(self, room_buffer, formatted, msgtype="m.text"):
# type: (RoomBuffer, Formatted, str) -> None # type: (RoomBuffer, Formatted, str) -> None
if room_buffer.room.encrypted: if room_buffer.room.encrypted: