Refactoring

This commit is contained in:
Changhua 2025-01-31 11:06:26 +08:00
parent 065db1d236
commit da4dcec8bc
3 changed files with 94 additions and 83 deletions

View File

@ -1,113 +1,109 @@
#include "framework.h" #pragma execution_character_set("utf-8")
#include <sstream>
#include <vector>
#include "chatroom_mgmt.h" #include "chatroom_mgmt.h"
#include "fill_response.h"
#include "log.hpp" #include "log.hpp"
#include "pb_util.h"
#include "util.h" #include "util.h"
using namespace std; using namespace std;
extern QWORD g_WeChatWinDllAddr; extern QWORD g_WeChatWinDllAddr;
namespace chatroom_mgmt
{
#define OS_GET_CHATROOM_MGR 0x1B83BD0 #define OS_GET_CHATROOM_MGR 0x1B83BD0
#define OS_ADD_MEMBERS 0x2155100 #define OS_ADD_MEMBERS 0x2155100
#define OS_DELETE_MEMBERS 0x2155740 #define OS_DELETE_MEMBERS 0x2155740
#define OS_INVITE_MEMBERS 0x2154AE0 #define OS_INVITE_MEMBERS 0x2154AE0
typedef QWORD (*GetChatRoomMgr_t)(); using get_chatroom_mgr_t = QWORD (*)();
typedef QWORD (*AddMemberToChatRoom_t)(QWORD, QWORD, QWORD, QWORD); using add_member_to_chatroom_t = QWORD (*)(QWORD, QWORD, QWORD, QWORD);
typedef QWORD (*DelMemberFromChatRoom_t)(QWORD, QWORD, QWORD); using del_member_from_chatroom_t = QWORD (*)(QWORD, QWORD, QWORD);
typedef QWORD (*InviteMemberToChatRoom_t)(QWORD, QWORD, QWORD, QWORD); using invite_member_to_chatroom_t = QWORD (*)(QWORD, QWORD, QWORD, QWORD);
int AddChatroomMember(string roomid, string wxids) static vector<WxString> parse_wxids(const string &wxids)
{ {
int status = -1; vector<WxString> wx_members;
if (roomid.empty() || wxids.empty()) {
LOG_ERROR("Empty roomid or wxids.");
return status;
}
GetChatRoomMgr_t GetChatRoomMgr = (GetChatRoomMgr_t)(g_WeChatWinDllAddr + OS_GET_CHATROOM_MGR);
AddMemberToChatRoom_t AddMembers = (AddMemberToChatRoom_t)(g_WeChatWinDllAddr + OS_ADD_MEMBERS);
vector<wstring> vMembers;
vector<WxString> vWxMembers;
wstringstream wss(String2Wstring(wxids)); wstringstream wss(String2Wstring(wxids));
while (wss.good()) { wstring wstr;
wstring wstr; while (getline(wss, wstr, L',')) {
getline(wss, wstr, L','); wx_members.emplace_back(wstr);
vMembers.push_back(wstr);
WxString wxMember(vMembers.back());
vWxMembers.push_back(wxMember);
} }
return wx_members;
QWORD temp[2] = { 0 };
WxString *pWxRoomid = NewWxStringFromStr(roomid);
QWORD pMembers = (QWORD) & ((RawVector_t *)&vWxMembers)->start;
QWORD mgr = GetChatRoomMgr();
status = (int)AddMembers(mgr, pMembers, (QWORD)pWxRoomid, (QWORD)temp);
return status;
} }
int DelChatroomMember(string roomid, string wxids) int add_chatroom_member(const string &roomid, const string &wxids)
{ {
int status = -1;
if (roomid.empty() || wxids.empty()) { if (roomid.empty() || wxids.empty()) {
LOG_ERROR("Empty roomid or wxids."); LOG_ERROR("Empty roomid or wxids.");
return status; return -1;
} }
GetChatRoomMgr_t GetChatRoomMgr = (GetChatRoomMgr_t)(g_WeChatWinDllAddr + OS_GET_CHATROOM_MGR); get_chatroom_mgr_t get_chatroom_mgr
DelMemberFromChatRoom_t DelMembers = (DelMemberFromChatRoom_t)(g_WeChatWinDllAddr + OS_DELETE_MEMBERS); = reinterpret_cast<get_chatroom_mgr_t>(g_WeChatWinDllAddr + OS_GET_CHATROOM_MGR);
add_member_to_chatroom_t add_members
= reinterpret_cast<add_member_to_chatroom_t>(g_WeChatWinDllAddr + OS_ADD_MEMBERS);
vector<wstring> vMembers; vector<WxString> wx_members = parse_wxids(wxids);
vector<WxString> vWxMembers; WxString *p_wx_roomid = NewWxStringFromStr(roomid);
wstringstream wss(String2Wstring(wxids)); QWORD p_members = reinterpret_cast<QWORD>(&wx_members.front());
while (wss.good()) {
wstring wstr;
getline(wss, wstr, L',');
vMembers.push_back(wstr);
WxString wxMember(vMembers.back());
vWxMembers.push_back(wxMember);
}
WxString *pWxRoomid = NewWxStringFromStr(roomid); return static_cast<int>(add_members(get_chatroom_mgr(), p_members, reinterpret_cast<QWORD>(p_wx_roomid), 0));
QWORD pMembers = (QWORD) & ((RawVector_t *)&vWxMembers)->start;
QWORD mgr = GetChatRoomMgr();
status = (int)DelMembers(mgr, pMembers, (QWORD)pWxRoomid);
return status;
} }
int InviteChatroomMember(string roomid, string wxids) int del_chatroom_member(const string &roomid, const string &wxids)
{ {
int status = -1;
if (roomid.empty() || wxids.empty()) { if (roomid.empty() || wxids.empty()) {
LOG_ERROR("Empty roomid or wxids."); LOG_ERROR("Empty roomid or wxids.");
return status; return -1;
} }
InviteMemberToChatRoom_t InviteMembers = (InviteMemberToChatRoom_t)(g_WeChatWinDllAddr + OS_INVITE_MEMBERS); get_chatroom_mgr_t get_chatroom_mgr
= reinterpret_cast<get_chatroom_mgr_t>(g_WeChatWinDllAddr + OS_GET_CHATROOM_MGR);
del_member_from_chatroom_t del_members
= reinterpret_cast<del_member_from_chatroom_t>(g_WeChatWinDllAddr + OS_DELETE_MEMBERS);
vector<wstring> vMembers; vector<WxString> wx_members = parse_wxids(wxids);
vector<WxString> vWxMembers; WxString *p_wx_roomid = NewWxStringFromStr(roomid);
wstringstream wss(String2Wstring(wxids)); QWORD p_members = reinterpret_cast<QWORD>(&wx_members.front());
while (wss.good()) {
wstring wstr;
getline(wss, wstr, L',');
vMembers.push_back(wstr);
WxString wxMember(vMembers.back());
vWxMembers.push_back(wxMember);
}
QWORD temp[2] = { 0 };
wstring wsRoomid = String2Wstring(roomid);
WxString *pWxRoomid = NewWxStringFromWstr(wsRoomid);
QWORD pMembers = (QWORD) & ((RawVector_t *)&vWxMembers)->start;
status = (int)InviteMembers((QWORD)wsRoomid.c_str(), pMembers, (QWORD)pWxRoomid, (QWORD)temp); return static_cast<int>(del_members(get_chatroom_mgr(), p_members, reinterpret_cast<QWORD>(p_wx_roomid)));
return status;
} }
int invite_chatroom_member(const string &roomid, const string &wxids)
{
if (roomid.empty() || wxids.empty()) {
LOG_ERROR("Empty roomid or wxids.");
return -1;
}
invite_member_to_chatroom_t invite_members
= reinterpret_cast<invite_member_to_chatroom_t>(g_WeChatWinDllAddr + OS_INVITE_MEMBERS);
vector<WxString> wx_members = parse_wxids(wxids);
WxString *p_wx_roomid = NewWxStringFromStr(roomid);
QWORD p_members = reinterpret_cast<QWORD>(&wx_members.front());
return static_cast<int>(invite_members(reinterpret_cast<QWORD>(p_wx_roomid->c_str()), p_members,
reinterpret_cast<QWORD>(p_wx_roomid), 0));
}
bool rpc_add_chatroom_member(const string &roomid, const string &wxids, uint8_t *out, size_t *len)
{
return fill_response<Functions_FUNC_ADD_ROOM_MEMBERS>(
out, len, [&](Response &rsp) { rsp.msg.status = add_chatroom_member(roomid, wxids); });
}
bool rpc_del_chatroom_member(const string &roomid, const string &wxids, uint8_t *out, size_t *len)
{
return fill_response<Functions_FUNC_DEL_ROOM_MEMBERS>(
out, len, [&](Response &rsp) { rsp.msg.status = del_chatroom_member(roomid, wxids); });
}
bool rpc_invite_chatroom_member(const string &roomid, const string &wxids, uint8_t *out, size_t *len)
{
return fill_response<Functions_FUNC_INV_ROOM_MEMBERS>(
out, len, [&](Response &rsp) { rsp.msg.status = invite_chatroom_member(roomid, wxids); });
}
} // namespace chatroom_mgmt

View File

@ -2,6 +2,21 @@
#include <string> #include <string>
int AddChatroomMember(std::string roomid, std::string wxids); namespace chatroom_mgmt
int DelChatroomMember(std::string roomid, std::string wxids); {
int InviteChatroomMember(std::string roomid, std::string wxids);
// 添加成员到群聊
int add_chatroom_member(const std::string &roomid, const std::string &wxids);
// 从群聊中移除成员
int del_chatroom_member(const std::string &roomid, const std::string &wxids);
// 邀请成员加入群聊
int invite_chatroom_member(const std::string &roomid, const std::string &wxids);
// RPC 方法
bool rpc_add_chatroom_member(const std::string &roomid, const std::string &wxids, uint8_t *out, size_t *len);
bool rpc_del_chatroom_member(const std::string &roomid, const std::string &wxids, uint8_t *out, size_t *len);
bool rpc_invite_chatroom_member(const std::string &roomid, const std::string &wxids, uint8_t *out, size_t *len);
} // namespace chatroom_mgmt

View File

@ -465,7 +465,7 @@ static bool func_add_room_members(char *roomid, char *wxids, uint8_t *out, size_
LOG_ERROR("Empty roomid or wxids."); LOG_ERROR("Empty roomid or wxids.");
rsp.msg.status = -1; rsp.msg.status = -1;
} else { } else {
rsp.msg.status = AddChatroomMember(roomid, wxids); rsp.msg.status = chatroom_mgmt::add_chatroom_member(roomid, wxids);
} }
}); });
} }
@ -477,7 +477,7 @@ static bool func_del_room_members(char *roomid, char *wxids, uint8_t *out, size_
LOG_ERROR("Empty roomid or wxids."); LOG_ERROR("Empty roomid or wxids.");
rsp.msg.status = -1; rsp.msg.status = -1;
} else { } else {
rsp.msg.status = DelChatroomMember(roomid, wxids); rsp.msg.status = chatroom_mgmt::del_chatroom_member(roomid, wxids);
} }
}); });
} }
@ -489,7 +489,7 @@ static bool func_invite_room_members(char *roomid, char *wxids, uint8_t *out, si
LOG_ERROR("Empty roomid or wxids."); LOG_ERROR("Empty roomid or wxids.");
rsp.msg.status = -1; rsp.msg.status = -1;
} else { } else {
rsp.msg.status = InviteChatroomMember(roomid, wxids); rsp.msg.status = chatroom_mgmt::invite_chatroom_member(roomid, wxids);
} }
}); });
} }