KakaoTalk ID management from Matrix

Add commands to set ID and make it searchable/hidden.
Also don't print stack traces of ProtocolErrors.
This commit is contained in:
Andrew Ferrazzutti 2022-05-06 02:45:00 -04:00
parent b994ca65ee
commit dfdd98da96
5 changed files with 154 additions and 6 deletions

View File

@ -87,6 +87,9 @@
* [x] Remove friend * [x] Remove friend
* [ ] Manage favourite friends * [ ] Manage favourite friends
* [ ] Manage hidden friends * [ ] Manage hidden friends
* [x] KakaoTalk ID management
* [x] Set/Change ID
* [x] Make ID searchable/hidden
<sup>[1]</sup> Sometimes fails with "Invalid body" error <sup>[1]</sup> Sometimes fails with "Invalid body" error
<sup>[2]</sup> Only recently-sent KakaoTalk messages can be deleted <sup>[2]</sup> Only recently-sent KakaoTalk messages can be deleted

View File

@ -15,7 +15,7 @@
# along with this program. If not, see <https://www.gnu.org/licenses/>. # along with this program. If not, see <https://www.gnu.org/licenses/>.
from __future__ import annotations from __future__ import annotations
from typing import TYPE_CHECKING from typing import TYPE_CHECKING, Awaitable
import asyncio import asyncio
from mautrix.bridge.commands import HelpSection, command_handler from mautrix.bridge.commands import HelpSection, command_handler
@ -36,6 +36,7 @@ from .typehint import CommandEvent
from ..kt.client.errors import CommandException from ..kt.client.errors import CommandException
SECTION_ACCOUNT = HelpSection("Account management", 35, "")
SECTION_FRIENDS = HelpSection("Friends management", 40, "") SECTION_FRIENDS = HelpSection("Friends management", 40, "")
SECTION_CHANNELS = HelpSection("Channel management", 45, "") SECTION_CHANNELS = HelpSection("Channel management", 45, "")
@ -46,6 +47,84 @@ if TYPE_CHECKING:
from ..kt.types.bson import Long from ..kt.types.bson import Long
_CMD_CONFIRM_CHANGE_ID = "confirm-change-id"
@command_handler(
needs_auth=True,
management_only=False,
help_section=SECTION_ACCOUNT,
help_text="Set or change your KakaoTalk ID",
help_args="<_KakaoTalk ID_>",
aliases=["set-id"],
)
async def change_id(evt: CommandEvent) -> None:
if len(evt.args) != 1:
await evt.reply(f"**Usage:** `$cmdprefix+sp {evt.command} <KakaoTalk ID>`")
return
new_id = evt.args[0]
if len(new_id) > 20:
await evt.reply("ID must not exceed 20 characters. Please choose a shorter ID.")
return
await evt.mark_read()
if await evt.sender.client.can_change_uuid(new_id):
await evt.reply(
"Once set, your KakaoTalk ID can be changed only once! "
f"If you are sure that you want to change it, type `$cmdprefix+sp {_CMD_CONFIRM_CHANGE_ID}`. "
"Otherwise, type `$cmdprefix+sp cancel`."
)
evt.sender.command_status = {
"action": "ID change",
"room_id": evt.room_id,
"next": _confirm_change_id,
"new_id": new_id,
}
else:
await evt.reply(
f"Cannot change KakaoTalk ID to `{new_id}`. "
"That ID might already be in use or have restricted characters. "
"Either try a different ID, or try again later."
)
async def _confirm_change_id(evt: CommandEvent) -> None:
assert evt.sender.command_status
new_id = evt.sender.command_status.pop("new_id")
evt.sender.command_status = None
await evt.mark_read()
try:
await evt.sender.client.change_uuid(new_id)
except CommandException:
await evt.reply(f"Failed to change KakaoTalk ID to `{new_id}`. Try again later.")
else:
await evt.reply(f"Successfully changed ID to `{new_id}`")
@command_handler(
needs_auth=True,
management_only=False,
help_section=SECTION_ACCOUNT,
help_text="Allow others to search by your KakaoTalk ID",
)
def make_id_searchable(evt: CommandEvent) -> Awaitable[None]:
return _set_id_searchable(evt, True)
@command_handler(
needs_auth=True,
management_only=False,
help_section=SECTION_ACCOUNT,
help_text="Prevent others from searching by your KakaoTalk ID",
)
def make_id_hidden(evt: CommandEvent) -> Awaitable[None]:
return _set_id_searchable(evt, False)
async def _set_id_searchable(evt: CommandEvent, searchable: bool) -> None:
await evt.mark_read()
try:
await evt.sender.client.set_uuid_searchable(searchable)
except RPCError as e:
await evt.reply(str(e))
else:
await evt.reply(f"Successfully made KakaoTalk ID {'searchable' if searchable else 'hidden'}")
async def _get_search_result_puppet(source: u.User, friend_struct: FriendStruct) -> pu.Puppet: async def _get_search_result_puppet(source: u.User, friend_struct: FriendStruct) -> pu.Puppet:
puppet = await pu.Puppet.get_by_ktid(friend_struct.userId) puppet = await pu.Puppet.get_by_ktid(friend_struct.userId)
if not puppet.name_set: if not puppet.name_set:

View File

@ -324,6 +324,20 @@ class Client:
unread_chat_ids=[c.serialize() for c in unread_chat_ids], unread_chat_ids=[c.serialize() for c in unread_chat_ids],
) )
async def can_change_uuid(self, uuid: str) -> bool:
try:
await self._api_user_request_void("can_change_uuid", uuid=uuid)
except CommandException:
return False
else:
return True
def change_uuid(self, uuid: str) -> Awaitable[None]:
return self._api_user_request_void("change_uuid", uuid=uuid)
def set_uuid_searchable(self, searchable: bool) -> Awaitable[None]:
return self._api_user_request_void("set_uuid_searchable", searchable=searchable)
def list_friends(self) -> Awaitable[FriendListStruct]: def list_friends(self) -> Awaitable[FriendListStruct]:
return self._api_user_request_result( return self._api_user_request_result(
FriendListStruct, FriendListStruct,

View File

@ -278,8 +278,8 @@ class User(DBUser, BaseUser):
self.log.warning(f"UUID mismatch: expected {self.uuid}, got {oauth_credential.deviceUUID}") self.log.warning(f"UUID mismatch: expected {self.uuid}, got {oauth_credential.deviceUUID}")
self.uuid = oauth_credential.deviceUUID self.uuid = oauth_credential.deviceUUID
async def get_own_info(self) -> SettingsStruct | None: async def get_own_info(self, *, force: bool = False) -> SettingsStruct | None:
if self._client and (not self._logged_in_info or self._logged_in_info_time + 60 * 60 < time.monotonic()): if force or self._client and (not self._logged_in_info or self._logged_in_info_time + 60 * 60 < time.monotonic()):
self._logged_in_info = await self._client.get_settings() self._logged_in_info = await self._client.get_settings()
self._logged_in_info_time = time.monotonic() self._logged_in_info_time = time.monotonic()
return self._logged_in_info return self._logged_in_info

View File

@ -834,6 +834,51 @@ export default class PeerClient {
return makeCommandResult(receipts) return makeCommandResult(receipts)
} }
/**
* @param {Object} req
* @param {string} req.mxid
* @param {string} req.uuid
*/
canChangeUUID = async (req) => {
return await this.#getUser(req.mxid).serviceClient.canChangeUUID(req.uuid)
}
/**
* @param {Object} req
* @param {string} req.mxid
* @param {string} req.uuid
*/
changeUUID = async (req) => {
const serviceClient = this.#getUser(req.mxid).serviceClient
const checkRes = await serviceClient.canChangeUUID(req.uuid)
if (!checkRes.success) return checkRes
return await serviceClient.changeUUID(req.uuid)
}
/**
* @param {Object} req
* @param {string} req.mxid
* @param {boolean} req.searchable
*/
setUUIDSearchable = async (req) => {
const serviceClient = this.#getUser(req.mxid).serviceClient
const moreRes = await serviceClient.requestMoreSettings()
if (!moreRes.success) {
throw new ProtocolError("Error checking status of KakaoTalk ID")
}
if (!moreRes.result.uuid) {
throw new ProtocolError("You do not yet have a KakaoTalk ID")
}
if (req.searchable == moreRes.result.uuidSearchable) {
throw new ProtocolError(`Your KakaoTalk ID is already ${req.searchable ? "searchable" : "hidden"}`)
}
return await serviceClient.updateSettings({
uuid_searchable: req.searchable,
})
}
/** /**
* @param {Object} req * @param {Object} req
* @param {string} req.mxid * @param {string} req.mxid
@ -1174,6 +1219,9 @@ export default class PeerClient {
get_participants: this.getParticipants, get_participants: this.getParticipants,
get_chats: this.getChats, get_chats: this.getChats,
get_read_receipts: this.getReadReceipts, get_read_receipts: this.getReadReceipts,
can_change_uuid: this.canChangeUUID,
change_uuid: this.changeUUID,
set_uuid_searchable: this.setUUIDSearchable,
list_friends: this.listFriends, list_friends: this.listFriends,
edit_friend: this.editFriend, edit_friend: this.editFriend,
edit_friend_by_uuid: this.editFriendByUUID, edit_friend_by_uuid: this.editFriendByUUID,
@ -1205,11 +1253,15 @@ export default class PeerClient {
} }
} else { } else {
resp.command = "error" resp.command = "error"
resp.error = err instanceof ProtocolError ? err.message : err.toString() if (err instanceof ProtocolError) {
resp.error = err.message
} else {
resp.error = err.toString()
this.log(`Error handling request ${resp.id} ${err.stack}`) this.log(`Error handling request ${resp.id} ${err.stack}`)
}
}
}
// TODO Check if session is broken. If it is, close the PeerClient // TODO Check if session is broken. If it is, close the PeerClient
}
}
await this.write(resp) await this.write(resp)
} }