Skip to content
This repository was archived by the owner on Aug 16, 2024. It is now read-only.
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 0 additions & 8 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -4,12 +4,6 @@ project (mirai-cpp VERSION 0.4.4)

set(CMAKE_CXX_STANDARD 11)

if(CMAKE_TOOLCHAIN_FILE)
include(${CMAKE_TOOLCHAIN_FILE})
endif(CMAKE_TOOLCHAIN_FILE)

find_package(CURL CONFIG REQUIRED)


if(MSVC)
add_compile_options("$<$<C_COMPILER_ID:MSVC>:/utf-8>")
Expand All @@ -18,7 +12,6 @@ if(MSVC)
endif(MSVC)



option(BUILD_SHARED_LIBS "Build ${PROJECT_NAME} as a shared library." OFF)
if(BUILD_SHARED_LIBS)
set(LIBRARY_TYPE SHARED)
Expand All @@ -37,7 +30,6 @@ target_include_directories(${PROJECT_NAME} PUBLIC
$<BUILD_INTERFACE:${CMAKE_CURRENT_SOURCE_DIR}/third-party>
$<INSTALL_INTERFACE:third-party>
)
target_link_libraries(${PROJECT_NAME} CURL::libcurl)

if(BUILD_SHARED_LIBS)
if(WIN32)
Expand Down
2 changes: 1 addition & 1 deletion examples/RichMessage.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@ int main()
{
try
{
gm.Reply(MessageChain().FlashImage(img));
gm.Reply(MessageChain().FlashImage(gImg));
}
catch (const std::exception& ex)
{
Expand Down
14 changes: 13 additions & 1 deletion include/events/member_mute_event.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -24,13 +24,15 @@ namespace Cyan
DurationSeconds = gm.DurationSeconds;
Member = gm.Member;
Operator = gm.Operator;
operator_is_null_ = gm.operator_is_null_;
}
MemberMuteEvent& operator=(const MemberMuteEvent& t)
{
MemberMuteEvent tmp(t);
std::swap(this->DurationSeconds, tmp.DurationSeconds);
std::swap(this->Member, tmp.Member);
std::swap(this->Operator, tmp.Operator);
std::swap(this->operator_is_null_, tmp.operator_is_null_);
return *this;
}

Expand All @@ -39,12 +41,21 @@ namespace Cyan
this->bot_ = bot;
}

bool OperatorIsBot() const
{
return operator_is_null_;
}

virtual ~MemberMuteEvent() = default;
virtual bool Set(const json& j) override
{
this->DurationSeconds = j["durationSeconds"].get<int>();
this->Member.Set(j["member"]);
this->Operator.Set(j["operator"]);
if (!j["operator"].is_null())
{
this->Operator.Set(j["operator"]);
this->operator_is_null_ = false;
}
return true;
}
virtual json ToJson() const override
Expand All @@ -59,6 +70,7 @@ namespace Cyan

private:
MiraiBot* bot_;
bool operator_is_null_ = true;
};

}
Expand Down
14 changes: 13 additions & 1 deletion include/events/member_unmute_event.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -22,12 +22,14 @@ namespace Cyan
{
Member = gm.Member;
Operator = gm.Operator;
operator_is_null_ = gm.operator_is_null_;
}
MemberUnmuteEvent& operator=(const MemberUnmuteEvent& t)
{
MemberUnmuteEvent tmp(t);
std::swap(this->Member, tmp.Member);
std::swap(this->Operator, tmp.Operator);
std::swap(this->operator_is_null_, tmp.operator_is_null_);
return *this;
}

Expand All @@ -36,11 +38,20 @@ namespace Cyan
this->bot_ = bot;
}

bool OperatorIsBot() const
{
return operator_is_null_;
}

virtual ~MemberUnmuteEvent() = default;
virtual bool Set(const json& j) override
{
this->Member.Set(j["member"]);
this->Operator.Set(j["operator"]);
if (!j["operator"].is_null())
{
this->Operator.Set(j["operator"]);
this->operator_is_null_ = false;
}
return true;
}
virtual json ToJson() const override
Expand All @@ -54,6 +65,7 @@ namespace Cyan

private:
MiraiBot* bot_;
bool operator_is_null_ = true;
};

}
Expand Down
46 changes: 30 additions & 16 deletions include/mirai_bot.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,14 @@

#include "ThreadPool.h"
#include "nlohmann/json.hpp"
#include "CURLWrapper.h"

// fuck windows.h
#ifdef max
#undef max
#endif
#include "httplib.h"


#include "defs/defs.hpp"
#include "events/events.hpp"
#include "exported.h"
Expand Down Expand Up @@ -40,14 +47,8 @@ namespace Cyan
class EXPORTED MiraiBot
{
public:
MiraiBot() :pool_(4), qq_(0) {}
MiraiBot(const string& host, int port) : pool_(4), qq_(0)
{
stringstream ss;
ss << "http://" << host << ":" << port;
api_url_prefix_ = ss.str();
}
MiraiBot(const string& url_prefix) :api_url_prefix_(url_prefix), pool_(4), qq_(0) {}
MiraiBot() :qq_(0), pool_(4), http_client_("localhost", 8080) {}
MiraiBot(const string& host, int port) : qq_(0), pool_(4), http_client_(host, port) {}
~MiraiBot()
{
Release();
Expand All @@ -56,9 +57,10 @@ namespace Cyan
{
return sessionKey_;
}
string GetApiUrlPrefix() const

httplib::Client* GetHttpClient()
{
return api_url_prefix_;
return &(this->http_client_);
}

bool Auth(const string& authKey, QQ_t qq);
Expand Down Expand Up @@ -110,10 +112,9 @@ namespace Cyan
void EventLoop();

private:
bool SessionVerify() const;
bool SessionRelease() const;
bool SessionVerify();
bool SessionRelease();
unsigned int FetchMessagesAndEvents(unsigned int count = 10);

template<typename T>
inline WeakEvent MakeWeakEvent(const json& json_)
{
Expand All @@ -122,7 +123,6 @@ namespace Cyan
e->Set(json_);
return std::dynamic_pointer_cast<Serializable>(e);
}

WeakEvent CreateEvent(MiraiEvent mirai_event, const json& json_);
bool Release() noexcept
{
Expand All @@ -136,10 +136,24 @@ namespace Cyan
}

}

// 因为 httplib 使用 string 来保存文件内容,这里适配一下
inline string ReadFile(const string& filename)
{
std::ifstream ifs(filename, std::ifstream::binary);
std::filebuf* pbuf = ifs.rdbuf();
std::size_t size = pbuf->pubseekoff(0, ifs.end, ifs.in);
pbuf->pubseekpos(0, ifs.in);
string result(size, '\0');
pbuf->sgetn(&result[0], size);
ifs.close();
return result;
}

string authKey_;
QQ_t qq_;
string sessionKey_;
string api_url_prefix_ = "http://127.0.0.1:8080";
httplib::Client http_client_;
unordered_map<MiraiEvent, function<void(WeakEvent)> > processors_;
ThreadPool pool_;
};
Expand Down
64 changes: 33 additions & 31 deletions src/event_func.cpp
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
#include "mirai_bot.hpp"
#include "events/events.hpp"
#include "httplib.h"

namespace Cyan
{
Expand Down Expand Up @@ -41,51 +42,52 @@ namespace Cyan

bool NewFriendRequestEvent::Respose(int operate, const string& message)
{
static const string api_url = bot_->GetApiUrlPrefix() + "/resp/newFriendRequestEvent";

json j;
j["sessionKey"] = bot_->GetSessionKey();
j["eventId"] = this->EventId;
j["fromId"] = (int64_t)this->FromId;
j["groupId"] = (int64_t)this->GroupId;
j["operate"] = operate;
j["message"] = message;

string pData = j.dump();
HTTP http; http.SetContentType("application/json;charset=UTF-8");
auto res = http.Post(api_url, pData);

if (res.Ready)
json data =
{
{ "sessionKey", bot_->GetSessionKey() },
{ "eventId", this->EventId},
{ "fromId", (int64_t)this->FromId},
{ "groupId", (int64_t)this->GroupId},
{ "operate", operate},
{ "message", message},
};

httplib::Client& http_client = *(bot_->GetHttpClient());
auto res = http_client.Post("/resp/newFriendRequestEvent", data.dump(), "application/json;charset=UTF-8");
if (res)
{
if (res->status != 200)
throw std::runtime_error("[mirai-api-http error]: " + res->body);
return true;
}
else
throw runtime_error(res.ErrorMsg);
throw runtime_error("网络错误");

}

bool MemberJoinRequestEvent::Respose(int operate, const string& message)
{
static const string api_url = bot_->GetApiUrlPrefix() + "/resp/memberJoinRequestEvent";

json j;
j["sessionKey"] = bot_->GetSessionKey();
j["eventId"] = this->EventId;
j["fromId"] = (int64_t)this->FromId;
j["groupId"] = (int64_t)this->GroupId;
j["operate"] = operate;
j["message"] = message;

string pData = j.dump();
HTTP http; http.SetContentType("application/json;charset=UTF-8");
auto res = http.Post(api_url, pData);

if (res.Ready)
json data =
{
{ "sessionKey", bot_->GetSessionKey() },
{ "eventId", this->EventId},
{ "fromId", (int64_t)this->FromId},
{ "groupId", (int64_t)this->GroupId},
{ "operate", operate},
{ "message", message},
};

httplib::Client& http_client = *(bot_->GetHttpClient());
auto res = http_client.Post("/resp/memberJoinRequestEvent", data.dump(), "application/json;charset=UTF-8");
if (res)
{
if (res->status != 200)
throw std::runtime_error("[mirai-api-http error]: " + res->body);
return true;
}
else
throw runtime_error(res.ErrorMsg);
throw runtime_error("网络错误");

}

Expand Down
Loading