This repository has been archived by the owner on Jan 7, 2020. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathapi.h
334 lines (266 loc) · 13 KB
/
api.h
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
#pragma once
#include "./common.h"
#include "./app.h"
#include "./enums.h"
#include "./target.h"
#include "./types.h"
#include "./utils/string.h"
namespace cq::exception {
struct ApiError : RuntimeError {
int code;
ApiError(const int code) : RuntimeError("failed to call coolq api") { this->code = code; }
static const auto INVALID_DATA = 100;
static const auto INVALID_TARGET = 101;
};
} // namespace cq::exception
namespace cq::api {
/**
* Init all API functions.
* This is internally called in the Initialize exported function.
*/
void __init();
/**
* Provide ways to access the raw CoolQ API functions.
*/
namespace raw {
#include "./api_funcs.h"
}
inline void __throw_if_needed(const int32_t ret) noexcept(false) {
if (ret < 0) {
throw exception::ApiError(ret);
}
}
inline void __throw_if_needed(const void *const ret_ptr) noexcept(false) {
if (!ret_ptr) {
throw exception::ApiError(exception::ApiError::INVALID_DATA);
}
}
#pragma region Message
inline int64_t send_private_msg(const int64_t user_id, const std::string &msg) noexcept(false) {
const auto ret = raw::CQ_sendPrivateMsg(app::auth_code, user_id, utils::string_to_coolq(msg).c_str());
__throw_if_needed(ret);
return ret;
}
inline int64_t send_group_msg(const int64_t group_id, const std::string &msg) noexcept(false) {
const auto ret = raw::CQ_sendGroupMsg(app::auth_code, group_id, utils::string_to_coolq(msg).c_str());
__throw_if_needed(ret);
return ret;
}
inline int64_t send_discuss_msg(const int64_t discuss_id, const std::string &msg) noexcept(false) {
const auto ret = raw::CQ_sendDiscussMsg(app::auth_code, discuss_id, utils::string_to_coolq(msg).c_str());
__throw_if_needed(ret);
return ret;
}
inline void delete_msg(const int64_t msg_id) noexcept(false) {
__throw_if_needed(raw::CQ_deleteMsg(app::auth_code, msg_id));
}
#pragma endregion
#pragma region Send Like
inline void send_like(const int64_t user_id) noexcept(false) {
__throw_if_needed(raw::CQ_sendLike(app::auth_code, user_id));
}
inline void send_like(const int64_t user_id, const int32_t times) noexcept(false) {
__throw_if_needed(raw::CQ_sendLikeV2(app::auth_code, user_id, times));
}
#pragma endregion
#pragma region Group &Discuss Operation
inline void set_group_kick(const int64_t group_id, const int64_t user_id,
const bool reject_add_request) noexcept(false) {
__throw_if_needed(raw::CQ_setGroupKick(app::auth_code, group_id, user_id, reject_add_request));
}
inline void set_group_ban(const int64_t group_id, const int64_t user_id, const int64_t duration) noexcept(false) {
__throw_if_needed(raw::CQ_setGroupBan(app::auth_code, group_id, user_id, duration));
}
inline void set_group_anonymous_ban(const int64_t group_id, const std::string &flag,
const int64_t duration) noexcept(false) {
__throw_if_needed(
raw::CQ_setGroupAnonymousBan(app::auth_code, group_id, utils::string_to_coolq(flag).c_str(), duration));
}
inline void set_group_whole_ban(const int64_t group_id, const bool enable) noexcept(false) {
__throw_if_needed(raw::CQ_setGroupWholeBan(app::auth_code, group_id, enable));
}
inline void set_group_admin(const int64_t group_id, const int64_t user_id, const bool enable) noexcept(false) {
__throw_if_needed(raw::CQ_setGroupAdmin(app::auth_code, group_id, user_id, enable));
}
inline void set_group_anonymous(const int64_t group_id, const bool enable) noexcept(false) {
__throw_if_needed(raw::CQ_setGroupAnonymous(app::auth_code, group_id, enable));
}
inline void set_group_card(const int64_t group_id, const int64_t user_id, const std::string &card) noexcept(false) {
__throw_if_needed(
raw::CQ_setGroupCard(app::auth_code, group_id, user_id, utils::string_to_coolq(card).c_str()));
}
inline void set_group_leave(const int64_t group_id, const bool is_dismiss) noexcept(false) {
__throw_if_needed(raw::CQ_setGroupLeave(app::auth_code, group_id, is_dismiss));
}
inline void set_group_special_title(const int64_t group_id, const int64_t user_id, const std::string &special_title,
const int64_t duration) noexcept(false) {
__throw_if_needed(raw::CQ_setGroupSpecialTitle(
app::auth_code, group_id, user_id, utils::string_to_coolq(special_title).c_str(), duration));
}
inline void set_discuss_leave(const int64_t discuss_id) noexcept(false) {
__throw_if_needed(raw::CQ_setDiscussLeave(app::auth_code, discuss_id));
}
#pragma endregion
#pragma region Request Operation
inline void set_friend_add_request(const std::string &flag, const request::Operation operation,
const std::string &remark) noexcept(false) {
__throw_if_needed(raw::CQ_setFriendAddRequest(
app::auth_code, utils::string_to_coolq(flag).c_str(), operation, utils::string_to_coolq(remark).c_str()));
}
inline void set_group_add_request(const std::string &flag, const request::SubType type,
const request::Operation operation) noexcept(false) {
__throw_if_needed(
raw::CQ_setGroupAddRequest(app::auth_code, utils::string_to_coolq(flag).c_str(), type, operation));
}
inline void set_group_add_request(const std::string &flag, const request::SubType type,
const request::Operation operation, const std::string &reason) noexcept(false) {
__throw_if_needed(raw::CQ_setGroupAddRequestV2(app::auth_code,
utils::string_to_coolq(flag).c_str(),
type,
operation,
utils::string_to_coolq(reason).c_str()));
}
#pragma endregion
#pragma region Get QQ Information
inline int64_t get_login_user_id() noexcept { return raw::CQ_getLoginQQ(app::auth_code); }
inline std::string get_login_nickname() noexcept(false) {
const auto ret = raw::CQ_getLoginNick(app::auth_code);
__throw_if_needed(ret);
return utils::string_from_coolq(ret);
}
inline std::string get_stranger_info_base64(const int64_t user_id, const bool no_cache = false) noexcept(false) {
const auto ret = raw::CQ_getStrangerInfo(app::auth_code, user_id, no_cache);
__throw_if_needed(ret);
return utils::string_from_coolq(ret);
}
inline std::string get_friend_list_base64() noexcept(false) {
const auto ret = raw::CQ_getFriendList(app::auth_code, false);
__throw_if_needed(ret);
return utils::string_from_coolq(ret);
}
inline std::string get_group_list_base64() noexcept(false) {
const auto ret = raw::CQ_getGroupList(app::auth_code);
__throw_if_needed(ret);
return utils::string_from_coolq(ret);
}
inline std::string get_group_info_base64(const int64_t group_id, const bool no_cache = false) noexcept(false) {
const auto ret = raw::CQ_getGroupInfo(app::auth_code, group_id, no_cache);
__throw_if_needed(ret);
return utils::string_from_coolq(ret);
}
inline std::string get_group_member_list_base64(const int64_t group_id) noexcept(false) {
const auto ret = raw::CQ_getGroupMemberList(app::auth_code, group_id);
__throw_if_needed(ret);
return utils::string_from_coolq(ret);
}
inline std::string get_group_member_info_base64(const int64_t group_id, const int64_t user_id,
const bool no_cache = false) noexcept(false) {
const auto ret = raw::CQ_getGroupMemberInfoV2(app::auth_code, group_id, user_id, no_cache);
__throw_if_needed(ret);
return utils::string_from_coolq(ret);
}
#pragma endregion
#pragma region Get CoolQ Information
inline std::string get_cookies() noexcept(false) {
const auto ret = raw::CQ_getCookies(app::auth_code);
__throw_if_needed(ret);
return utils::string_from_coolq(ret);
}
inline std::string get_cookies(const std::string &domain) noexcept(false) {
const auto ret = raw::CQ_getCookiesV2(app::auth_code, utils::string_to_coolq(domain).c_str());
__throw_if_needed(ret);
return utils::string_from_coolq(ret);
}
inline int32_t get_csrf_token() noexcept { return raw::CQ_getCsrfToken(app::auth_code); }
inline std::string get_app_directory() noexcept(false) {
const auto ret = raw::CQ_getAppDirectory(app::auth_code);
__throw_if_needed(ret);
return utils::string_from_coolq(ret);
}
inline std::string get_record(const std::string &file, const std::string &out_format,
const bool full_path = false) noexcept(false) {
const auto ret =
full_path
? raw::CQ_getRecordV2(
app::auth_code, utils::string_to_coolq(file).c_str(), utils::string_to_coolq(out_format).c_str())
: raw::CQ_getRecord(
app::auth_code, utils::string_to_coolq(file).c_str(), utils::string_to_coolq(out_format).c_str());
__throw_if_needed(ret);
return utils::string_from_coolq(ret);
}
inline std::string get_image(const std::string &file) noexcept(false) {
const auto ret = raw::CQ_getImage(app::auth_code, utils::string_to_coolq(file).c_str());
__throw_if_needed(ret);
return utils::string_from_coolq(ret);
}
inline bool can_send_image() noexcept(false) { return static_cast<bool>(raw::CQ_canSendImage(app::auth_code)); }
inline bool can_send_record() noexcept(false) { return static_cast<bool>(raw::CQ_canSendRecord(app::auth_code)); }
#pragma endregion
#pragma region CoolQ Self - operation
// int32_t set_fatal(const char *error_info) {
// return raw::CQ_setFatal(app::auth_code, error_info);
//}
//
// int32_t set_restart() {
// return raw::CQ_setRestart(app::auth_code);
//}
#pragma endregion
#pragma region CQSDK Bonus
inline int64_t send_msg(const Target &target, const std::string &msg) noexcept(false) {
if (target.group_id.has_value()) {
return send_group_msg(target.group_id.value(), msg);
}
if (target.discuss_id.has_value()) {
return send_discuss_msg(target.discuss_id.value(), msg);
}
if (target.user_id.has_value()) {
return send_private_msg(target.user_id.value(), msg);
}
throw exception::ApiError(exception::ApiError::INVALID_TARGET);
}
inline User get_stranger_info(const int64_t user_id, const bool no_cache = false) noexcept(false) {
try {
return ObjectHelper::from_base64<User>(get_stranger_info_base64(user_id, no_cache));
} catch (exception::ParseError &) {
throw exception::ApiError(exception::ApiError::INVALID_DATA);
}
}
inline std::vector<Friend> get_friend_list() noexcept(false) {
try {
return ObjectHelper::multi_from_base64<std::vector<Friend>>(get_friend_list_base64());
} catch (exception::ParseError &) {
throw exception::ApiError(exception::ApiError::INVALID_DATA);
}
}
inline std::vector<Group> get_group_list() noexcept(false) {
try {
return ObjectHelper::multi_from_base64<std::vector<Group>>(get_group_list_base64());
} catch (exception::ParseError &) {
throw exception::ApiError(exception::ApiError::INVALID_DATA);
}
}
inline Group get_group_info(const int64_t group_id, const bool no_cache = false) noexcept(false) {
try {
return ObjectHelper::from_base64<Group>(get_group_info_base64(group_id, no_cache));
} catch (exception::ParseError &) {
throw exception::ApiError(exception::ApiError::INVALID_DATA);
}
}
inline std::vector<GroupMember> get_group_member_list(const int64_t group_id) noexcept(false) {
try {
return ObjectHelper::multi_from_base64<std::vector<GroupMember>>(get_group_member_list_base64(group_id));
} catch (exception::ParseError &) {
throw exception::ApiError(exception::ApiError::INVALID_DATA);
}
}
inline GroupMember get_group_member_info(const int64_t group_id, const int64_t user_id,
const bool no_cache = false) noexcept(false) {
try {
return ObjectHelper::from_base64<GroupMember>(get_group_member_info_base64(group_id, user_id, no_cache));
} catch (exception::ParseError &) {
throw exception::ApiError(exception::ApiError::INVALID_DATA);
}
}
inline User get_login_info() noexcept(false) { return get_stranger_info(get_login_user_id()); }
#pragma endregion
} // namespace cq::api