Skip to content
This repository was archived by the owner on Aug 2, 2020. It is now read-only.

Commit 1f71578

Browse files
committed
Merge branch 'Arnie97-group_notice_patch'
2 parents 79f685d + 0feaa78 commit 1f71578

File tree

3 files changed

+81
-0
lines changed

3 files changed

+81
-0
lines changed

src/cqhttp/plugins/experimental_actions/experimental_actions.cpp

Lines changed: 70 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -312,13 +312,83 @@ namespace cqhttp::plugins {
312312
result.data = nullptr;
313313
}
314314

315+
static void action_group_notice(ActionContext &ctx, bool post_new_notice) {
316+
auto &result = ctx.result;
317+
result.code = Codes::OK;
318+
result.data = nullptr;
319+
320+
const auto group_id = ctx.params.get_integer("group_id");
321+
if (group_id <= 0) {
322+
result.code = Codes::DEFAULT_ERROR;
323+
return;
324+
}
325+
326+
string params;
327+
string cookies;
328+
try {
329+
const int csrf_token = api::get_csrf_token();
330+
params = "bkn=" + to_string(csrf_token) + "&qid=" + to_string(group_id);
331+
cookies = api::get_cookies();
332+
} catch (exception &) {
333+
result.code = Codes::CREDENTIAL_INVALID;
334+
return;
335+
}
336+
337+
// list the current group notices and get the gsi
338+
string gsi;
339+
try {
340+
const auto url =
341+
"https://web.qun.qq.com/cgi-bin/announce/get_t_list?" + params + "&ft=23&s=-1&n=10&ni=1&i=1";
342+
const auto res = utils::http::get_json(url, true, cookies).value_or(nullptr);
343+
result.data = res.at("feeds").is_array() ? res.at("feeds") : json::array();
344+
gsi = res.at("gsi");
345+
} catch (exception &) {
346+
result.code = Codes::CREDENTIAL_INVALID;
347+
return;
348+
}
349+
if (!post_new_notice) {
350+
return; // get_group_notice finishes here
351+
}
352+
353+
result.data = nullptr;
354+
355+
const auto title = ctx.params.get_string("title");
356+
const auto content = ctx.params.get_string("content");
357+
// well it's theoretically possible to create a empty group notice...
358+
// but that's not useful anyway, hence just let it go
359+
if (title.empty() || content.empty()) {
360+
result.code = Codes::DEFAULT_ERROR;
361+
return;
362+
}
363+
364+
// reuse the bkn & gsi parameters from above to post a new group notice
365+
try {
366+
const auto url = "https://web.qun.qq.com/cgi-bin/announce/add_qun_notice";
367+
const auto body = params + "&gsi=" + gsi + "&text=" + utils::http::url_encode(content)
368+
+ "&title=" + utils::http::url_encode(title);
369+
const auto post_response = utils::http::post(url, body, {{"Cookie", cookies}});
370+
const auto res = post_response.get_json();
371+
if (res.at("ec").get<int>()) { // error code
372+
result.code = Codes::CREDENTIAL_INVALID;
373+
} else if (res.at("id").get<int>() == 0) { // insufficient user permission for posting a new notice
374+
result.code = Codes::OPERATION_FAILED;
375+
}
376+
} catch (exception &) {
377+
result.code = Codes::INVALID_DATA;
378+
}
379+
}
380+
315381
void ExperimentalActions::hook_missed_action(ActionContext &ctx) {
316382
if (ctx.action == "_get_friend_list") {
317383
action_get_friend_list(ctx);
318384
} else if (ctx.action == "_get_group_info") {
319385
action_get_group_info(ctx);
320386
} else if (ctx.action == "_get_vip_info") {
321387
action_get_vip_info(ctx);
388+
} else if (ctx.action == "_get_group_notice") {
389+
action_group_notice(ctx, false);
390+
} else if (ctx.action == "_send_group_notice") {
391+
action_group_notice(ctx, true);
322392
} else {
323393
ctx.next();
324394
}

src/cqhttp/utils/http.cpp

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -223,4 +223,13 @@ namespace cqhttp::utils::http {
223223
const auto res = request.post();
224224
return static_cast<Response>(res);
225225
}
226+
227+
string url_encode(const string &text) {
228+
const auto curl = curl_easy_init();
229+
char *escaped_text = curl_easy_escape(curl, text.c_str(), 0);
230+
const string escaped_string = escaped_text;
231+
curl_free(escaped_text);
232+
curl_easy_cleanup(curl);
233+
return escaped_string;
234+
}
226235
} // namespace cqhttp::utils::http

src/cqhttp/utils/http.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -49,4 +49,6 @@ namespace cqhttp::utils::http {
4949
Response post(const std::string &url, const std::string &body = "", const std::string &content_type = "text/plain",
5050
long timeout = 0);
5151
Response post(const std::string &url, const std::string &body, Headers headers, long timeout = 0);
52+
53+
std::string url_encode(const std::string &text);
5254
} // namespace cqhttp::utils::http

0 commit comments

Comments
 (0)