refactor(util): extract common functions from message_sender to util
This commit is contained in:
parent
ccc8dcbbcd
commit
2dc1f25b75
@ -268,15 +268,17 @@ std::unique_ptr<WxString> new_wx_string(const wchar_t *wstr)
|
|||||||
|
|
||||||
std::unique_ptr<WxString> new_wx_string(const std::wstring &wstr) { return std::make_unique<WxString>(wstr); }
|
std::unique_ptr<WxString> new_wx_string(const std::wstring &wstr) { return std::make_unique<WxString>(wstr); }
|
||||||
|
|
||||||
std::vector<WxString> parse_wxids(const std::string &wxids)
|
AtWxidSplitResult<> parse_wxids(const std::string &atWxids)
|
||||||
{
|
{
|
||||||
std::vector<WxString> wx_members;
|
AtWxidSplitResult<> result;
|
||||||
std::wstringstream wss(s2w(wxids));
|
if (!atWxids.empty()) {
|
||||||
std::wstring wstr;
|
std::wstringstream wss(util::s2w(atWxids));
|
||||||
while (getline(wss, wstr, L',')) {
|
for (std::wstring wxid; std::getline(wss, wxid, L',');) {
|
||||||
wx_members.emplace_back(wstr);
|
result.wxids.push_back(wxid);
|
||||||
|
result.wxWxids.emplace_back(result.wxids.back());
|
||||||
}
|
}
|
||||||
return wx_members;
|
}
|
||||||
|
return result;
|
||||||
}
|
}
|
||||||
|
|
||||||
WxString *CreateWxString(const std::string &s)
|
WxString *CreateWxString(const std::string &s)
|
||||||
|
@ -76,14 +76,24 @@ template <typename T> static T *AllocBuffer(size_t count)
|
|||||||
return reinterpret_cast<T *>(HeapAlloc(GetProcessHeap(), 8, sizeof(T) * count));
|
return reinterpret_cast<T *>(HeapAlloc(GetProcessHeap(), 8, sizeof(T) * count));
|
||||||
}
|
}
|
||||||
|
|
||||||
|
template <typename T> struct WxStringHolder {
|
||||||
|
std::wstring ws;
|
||||||
|
WxString wx;
|
||||||
|
explicit WxStringHolder(const T &str) : ws(util::s2w(str)), wx(ws) { }
|
||||||
|
};
|
||||||
|
|
||||||
|
template <typename StringT = std::wstring> struct AtWxidSplitResult {
|
||||||
|
std::vector<StringT> wxids;
|
||||||
|
std::vector<WxString> wxWxids;
|
||||||
|
};
|
||||||
|
|
||||||
WxString *CreateWxString(const std::string &s);
|
WxString *CreateWxString(const std::string &s);
|
||||||
void FreeWxString(WxString *wxStr);
|
void FreeWxString(WxString *wxStr);
|
||||||
|
AtWxidSplitResult<> parse_wxids(const std::string &atWxids);
|
||||||
|
|
||||||
std::unique_ptr<WxString> new_wx_string(const char *str);
|
std::unique_ptr<WxString> new_wx_string(const char *str);
|
||||||
std::unique_ptr<WxString> new_wx_string(const wchar_t *wstr);
|
std::unique_ptr<WxString> new_wx_string(const wchar_t *wstr);
|
||||||
std::unique_ptr<WxString> new_wx_string(const std::string &str);
|
std::unique_ptr<WxString> new_wx_string(const std::string &str);
|
||||||
std::unique_ptr<WxString> new_wx_string(const std::wstring &wstr);
|
std::unique_ptr<WxString> new_wx_string(const std::wstring &wstr);
|
||||||
|
|
||||||
std::vector<WxString> parse_wxids(const std::string &wxids);
|
|
||||||
|
|
||||||
} // namespace util
|
} // namespace util
|
||||||
|
@ -44,45 +44,12 @@ Sender::Sender()
|
|||||||
func_xml_buf_sign = reinterpret_cast<XmlBufSign_t>(g_WeChatWinDllAddr + OsSend::XML_BUF_SIGN);
|
func_xml_buf_sign = reinterpret_cast<XmlBufSign_t>(g_WeChatWinDllAddr + OsSend::XML_BUF_SIGN);
|
||||||
}
|
}
|
||||||
|
|
||||||
std::unique_ptr<WxString> Sender::new_wx_string(const char *str)
|
|
||||||
{
|
|
||||||
return new_wx_string(str ? std::string(str) : std::string());
|
|
||||||
}
|
|
||||||
std::unique_ptr<WxString> Sender::new_wx_string(const std::string &str)
|
|
||||||
{
|
|
||||||
return std::make_unique<WxString>(util::s2w(str));
|
|
||||||
}
|
|
||||||
|
|
||||||
template <typename T> struct WxStringHolder {
|
|
||||||
std::wstring ws;
|
|
||||||
WxString wx;
|
|
||||||
explicit WxStringHolder(const T &str) : ws(util::s2w(str)), wx(ws) { }
|
|
||||||
};
|
|
||||||
|
|
||||||
template <typename StringT = std::wstring> struct AtWxidSplitResult {
|
|
||||||
std::vector<StringT> wxids;
|
|
||||||
std::vector<WxString> wxWxids;
|
|
||||||
};
|
|
||||||
|
|
||||||
AtWxidSplitResult<> parse_wxids(const std::string &atWxids)
|
|
||||||
{
|
|
||||||
AtWxidSplitResult<> result;
|
|
||||||
if (!atWxids.empty()) {
|
|
||||||
std::wstringstream wss(util::s2w(atWxids));
|
|
||||||
for (std::wstring wxid; std::getline(wss, wxid, L',');) {
|
|
||||||
result.wxids.push_back(wxid);
|
|
||||||
result.wxWxids.emplace_back(result.wxids.back());
|
|
||||||
}
|
|
||||||
}
|
|
||||||
return result;
|
|
||||||
}
|
|
||||||
|
|
||||||
void Sender::send_text(const std::string &wxid, const std::string &msg, const std::string &at_wxids)
|
void Sender::send_text(const std::string &wxid, const std::string &msg, const std::string &at_wxids)
|
||||||
{
|
{
|
||||||
WxStringHolder<std::string> holderMsg(msg);
|
util::WxStringHolder<std::string> holderMsg(msg);
|
||||||
WxStringHolder<std::string> holderWxid(wxid);
|
util::WxStringHolder<std::string> holderWxid(wxid);
|
||||||
|
|
||||||
auto wxAtWxids = parse_wxids(at_wxids).wxWxids;
|
auto wxAtWxids = util::parse_wxids(at_wxids).wxWxids;
|
||||||
QWORD pWxAtWxids = wxAtWxids.empty() ? 0 : reinterpret_cast<QWORD>(&wxAtWxids);
|
QWORD pWxAtWxids = wxAtWxids.empty() ? 0 : reinterpret_cast<QWORD>(&wxAtWxids);
|
||||||
|
|
||||||
char buffer[1104] = { 0 };
|
char buffer[1104] = { 0 };
|
||||||
@ -93,8 +60,8 @@ void Sender::send_text(const std::string &wxid, const std::string &msg, const st
|
|||||||
|
|
||||||
void Sender::send_image(const std::string &wxid, const std::string &path)
|
void Sender::send_image(const std::string &wxid, const std::string &path)
|
||||||
{
|
{
|
||||||
WxStringHolder<std::string> holderWxid(wxid);
|
util::WxStringHolder<std::string> holderWxid(wxid);
|
||||||
WxStringHolder<std::string> holderPath(path);
|
util::WxStringHolder<std::string> holderPath(path);
|
||||||
|
|
||||||
char msg[1192] = { 0 };
|
char msg[1192] = { 0 };
|
||||||
char msgTmp[1192] = { 0 };
|
char msgTmp[1192] = { 0 };
|
||||||
@ -258,8 +225,8 @@ int Sender::send_pat(const std::string &roomid, const std::string &wxid)
|
|||||||
{
|
{
|
||||||
QWORD status = -1;
|
QWORD status = -1;
|
||||||
|
|
||||||
WxStringHolder<std::string> holderRoom(roomid);
|
util::WxStringHolder<std::string> holderRoom(roomid);
|
||||||
WxStringHolder<std::string> holderWxid(wxid);
|
util::WxStringHolder<std::string> holderWxid(wxid);
|
||||||
|
|
||||||
status = func_send_pat(&holderRoom.wx, &holderWxid.wx);
|
status = func_send_pat(&holderRoom.wx, &holderWxid.wx);
|
||||||
|
|
||||||
|
Loading…
Reference in New Issue
Block a user