rafctor(chatroom): refactoring

This commit is contained in:
Changhua 2025-03-06 06:54:05 +08:00
parent aefea53bdd
commit e00958cf33

View File

@ -7,22 +7,33 @@
#include "rpc_helper.h"
#include "util.h"
using namespace std;
extern QWORD g_WeChatWinDllAddr;
namespace chatroom
{
namespace OsRoom = Offsets::Chatroom;
using get_chatroom_mgr_t = QWORD (*)();
using add_member_to_chatroom_t = QWORD (*)(QWORD, QWORD, WxString *, QWORD);
using del_member_from_chatroom_t = QWORD (*)(QWORD, QWORD, WxString *);
using invite_members_t = QWORD (*)(const wchar_t *, QWORD, WxString *, QWORD);
using get_mgr_t = QWORD (*)();
using add_member_t = QWORD (*)(QWORD, QWORD, WxString *, QWORD);
using delete_member_t = QWORD (*)(QWORD, QWORD, WxString *);
using invite_members_t = QWORD (*)(const wchar_t *, QWORD, WxString *, QWORD);
template <auto FillFunc, typename Func>
bool rpc_chatroom_common(const MemberMgmt &m, uint8_t *out, size_t *len, Func func)
{
int status = -1;
if (m.wxids && m.roomid) {
status = func(m.roomid, m.wxids);
} else {
LOG_ERROR("wxid 和 roomid 不能为空");
}
return fill_response<FillFunc>(out, len, [&](Response &rsp) { rsp.msg.status = status; });
}
int add_chatroom_member(const string &roomid, const string &wxids)
{
get_chatroom_mgr_t get_chatroom_mgr = reinterpret_cast<get_chatroom_mgr_t>(g_WeChatWinDllAddr + OsRoom::MGR);
add_member_to_chatroom_t add_members = reinterpret_cast<add_member_to_chatroom_t>(g_WeChatWinDllAddr + OsRoom::ADD);
auto get_chatroom_mgr = reinterpret_cast<get_mgr_t>(g_WeChatWinDllAddr + OsRoom::MGR);
auto add_members = reinterpret_cast<add_member_t>(g_WeChatWinDllAddr + OsRoom::ADD);
WxString *wx_roomid = util::CreateWxString(roomid);
@ -35,9 +46,8 @@ int add_chatroom_member(const string &roomid, const string &wxids)
int del_chatroom_member(const string &roomid, const string &wxids)
{
get_chatroom_mgr_t get_chatroom_mgr = reinterpret_cast<get_chatroom_mgr_t>(g_WeChatWinDllAddr + OsRoom::MGR);
del_member_from_chatroom_t del_members
= reinterpret_cast<del_member_from_chatroom_t>(g_WeChatWinDllAddr + OsRoom::DEL);
auto get_chatroom_mgr = reinterpret_cast<get_mgr_t>(g_WeChatWinDllAddr + OsRoom::MGR);
auto del_members = reinterpret_cast<delete_member_t>(g_WeChatWinDllAddr + OsRoom::DEL);
WxString *wx_roomid = util::CreateWxString(roomid);
auto wx_members = util::parse_wxids(wxids).wxWxids;
@ -48,7 +58,7 @@ int del_chatroom_member(const string &roomid, const string &wxids)
int invite_chatroom_member(const string &roomid, const string &wxids)
{
invite_members_t invite_members = reinterpret_cast<invite_members_t>(g_WeChatWinDllAddr + OsRoom::INV);
auto invite_members = reinterpret_cast<invite_members_t>(g_WeChatWinDllAddr + OsRoom::INV);
wstring ws_roomid = util::s2w(roomid);
WxString *wx_roomid = util::CreateWxString(roomid);
@ -62,47 +72,17 @@ int invite_chatroom_member(const string &roomid, const string &wxids)
bool rpc_add_chatroom_member(const MemberMgmt &m, uint8_t *out, size_t *len)
{
int status = -1;
if (m.wxids && m.roomid) {
const std::string wxids = m.wxids;
const std::string roomid = m.roomid;
status = add_chatroom_member(roomid, wxids);
} else {
LOG_ERROR("wxid 和 roomid 不能为空");
}
return fill_response<Functions_FUNC_ADD_ROOM_MEMBERS>(out, len, [&](Response &rsp) { rsp.msg.status = status; });
return rpc_chatroom_common<Functions_FUNC_ADD_ROOM_MEMBERS>(m, out, len, add_chatroom_member);
}
bool rpc_delete_chatroom_member(const MemberMgmt &m, uint8_t *out, size_t *len)
{
int status = -1;
if (m.wxids && m.roomid) {
const std::string wxids = m.wxids;
const std::string roomid = m.roomid;
status = del_chatroom_member(roomid, wxids);
} else {
LOG_ERROR("wxid 和 roomid 不能为空");
}
return fill_response<Functions_FUNC_DEL_ROOM_MEMBERS>(out, len, [&](Response &rsp) { rsp.msg.status = status; });
return rpc_chatroom_common<Functions_FUNC_DEL_ROOM_MEMBERS>(m, out, len, del_chatroom_member);
}
bool rpc_invite_chatroom_member(const MemberMgmt &m, uint8_t *out, size_t *len)
{
int status = -1;
if (m.wxids && m.roomid) {
const std::string wxids = m.wxids;
const std::string roomid = m.roomid;
status = invite_chatroom_member(roomid, wxids);
} else {
LOG_ERROR("wxid 和 roomid 不能为空");
}
return fill_response<Functions_FUNC_INV_ROOM_MEMBERS>(out, len, [&](Response &rsp) { rsp.msg.status = status; });
return rpc_chatroom_common<Functions_FUNC_INV_ROOM_MEMBERS>(m, out, len, invite_chatroom_member);
}
} // namespace chatroom