diff --git a/main.py b/main.py index d1a6236..cc6753b 100644 --- a/main.py +++ b/main.py @@ -44,7 +44,7 @@ from matrix.utf import utf8_decode # file, import the callbacks here so weechat can find them. from matrix.commands import (hook_commands, hook_page_up, matrix_command_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_buf_clear_cb, matrix_me_command_cb, matrix_kick_command_cb) diff --git a/matrix/commands.py b/matrix/commands.py index edf2a00..021f827 100644 --- a/matrix/commands.py +++ b/matrix/commands.py @@ -72,6 +72,13 @@ class WeechatCommandParser(object): 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(): W.hook_command( @@ -164,10 +171,23 @@ def hook_commands(): "matrix_kick_command_cb", "") + W.hook_command( + # Command name and short description + "invite", + "invite a user to the current room", + # Synopsis + (""), + # Description + ("user-id: user-id to invite"), + # Completions + ("%(matrix_users)"), + # Callback + "matrix_invite_command_cb", + "") + # TODO those should be hook_command() calls # W.hook_command_run('/join', 'matrix_command_join_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', '') @@ -363,28 +383,26 @@ def matrix_command_part_cb(data, buffer, command): @utf8_decode -def matrix_command_invite_cb(data, buffer, command): - - def invite(server, buf, args): - split_args = args.split(" ", 1) - - # 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 +def matrix_invite_command_cb(data, buffer, args): + parsed_args = WeechatCommandParser.invite(args) + if not parsed_args: + return W.WEECHAT_RC_OK for server in SERVERS.values(): - if buffer in server.buffers.values(): - invite(server, buffer, command) - return W.WEECHAT_RC_OK_EAT + if buffer == server.server_buffer: + server.error("command \"invite\" must be " + "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 diff --git a/matrix/server.py b/matrix/server.py index 1a1ea7b..0ca08c0 100644 --- a/matrix/server.py +++ b/matrix/server.py @@ -522,6 +522,12 @@ class MatrixServer(object): reason) 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"): # type: (RoomBuffer, Formatted, str) -> None if room_buffer.room.encrypted: