@@ -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 }
0 commit comments