This repository has been archived by the owner on Aug 2, 2020. It is now read-only.
-
-
Notifications
You must be signed in to change notification settings - Fork 306
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
13 changed files
with
686 additions
and
50 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,23 @@ | ||
# CQ 码 | ||
|
||
CQ 码的使用方式和原生的酷 Q 相同,在要发送的消息中插入相应的代码即可,例如: | ||
|
||
``` | ||
[CQ:face,id=14] [CQ:image,file=1.jpg] | ||
``` | ||
|
||
使用方式和原生 CQ 码(关于原生 CQ 码的使用,请看 [Pro/CQ码](https://d.cqp.me/Pro/CQ%E7%A0%81))完全相同意味着需要在某些情况下对字符进行转义,由于很多时候我们不需要使用 CQ 码,只需要发送文字消息就行了,这种情况下可以在请求 API 时加入 `is_raw` 参数,这将会自动对整个消息的特殊字符进行转义,具体请看 [API 描述](https://richardchien.github.io/coolq-http-api/#/API)。 | ||
|
||
除了原生的 CQ 码,CoolQ HTTP API 还提供了一些实用的增强功能,后面称之为「增强 CQ 码」。 | ||
|
||
## 增强功能列表 | ||
|
||
目前只有一个增强功能。 | ||
|
||
### 发送网络图片 | ||
|
||
酷 Q 原生的 CQ 码只能发送 `data\image` 文件夹里的图片,增强 CQ 码支持设置 `file` 为图片链接,内部会首先把图片下载到 `data\image` 文件夹,然后把 `file` 替换成下载好的本地文件。例如: | ||
|
||
``` | ||
[CQ:image,file=http://i1.piimg.com/567571/fdd6e7b6d93f1ef0.jpg] | ||
``` |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,98 @@ | ||
#include "cqcode.h" | ||
|
||
#include <regex> | ||
#include <curl/curl.h> | ||
|
||
#include "md5.h" | ||
#include "misc_functions.h" | ||
#include <event2/event.h> | ||
|
||
using namespace std; | ||
|
||
string message_escape(const string& msg) | ||
{ | ||
string tmp = msg; | ||
string_replace(tmp, "&", "&"); | ||
string_replace(tmp, "[", "["); | ||
string_replace(tmp, "]", "]"); | ||
string_replace(tmp, ",", ","); | ||
return tmp; | ||
} | ||
|
||
string message_unescape(const string& msg) | ||
{ | ||
string tmp = msg; | ||
string_replace(tmp, "[", "["); | ||
string_replace(tmp, "]", "]"); | ||
string_replace(tmp, ",", ","); | ||
string_replace(tmp, "&", "&"); | ||
return tmp; | ||
} | ||
|
||
string enhance_cq_code_function_image(smatch& match); | ||
|
||
string enhance_cq_code(const string& msg) | ||
{ | ||
string result; | ||
|
||
// 0: full CQ code function message, 1: function name, 2: params string | ||
regex exp("\\[CQ:([\\._\\-0-9A-Za-z]+?)(?:\\s*\\]|\\s*,\\s*(.*?)\\])"); | ||
|
||
smatch match; | ||
string::const_iterator search_iter(msg.cbegin()); | ||
while (regex_search(search_iter, msg.cend(), match, exp)) | ||
{ | ||
result += string(search_iter, search_iter + match.position()); // normal message before this current CQ code | ||
|
||
string function = match.str(1); | ||
if (function == "image") | ||
result += enhance_cq_code_function_image(match); | ||
|
||
search_iter += match.position() + match.length(); | ||
} | ||
return result; | ||
} | ||
|
||
string enhance_cq_code_function_image(smatch& match) | ||
{ | ||
// enhance image function to support file from the internet | ||
string cqcode_call = match.str(0); // full CQ code function message | ||
string params = match.str(2); | ||
smatch m; | ||
if (regex_search(params, m, regex("file=(https?:\\/\\/[^,\\?]+(\\.[^\\s,\\?]+)\\??[^,\\?]*)"))) | ||
{ | ||
string raw_url = m.str(1); | ||
string url = message_unescape(raw_url); | ||
string ext = m.str(2); | ||
MD5 md5(url); | ||
string hash = md5.toStr(); | ||
string filename = hash + ext; | ||
|
||
string filepath = get_cq_root_path() + "data\\image\\" + filename; | ||
FILE* fp = NULL; | ||
fopen_s(&fp, filepath.c_str(), "wb"); | ||
if (fp) | ||
{ | ||
CURL* curl = curl_easy_init(); | ||
curl_easy_setopt(curl, CURLOPT_URL, url); | ||
|
||
curl_easy_setopt(curl, CURLOPT_WRITEFUNCTION, curl_write_file_callback); | ||
curl_easy_setopt(curl, CURLOPT_WRITEDATA, fp); | ||
|
||
struct curl_slist* chunk = NULL; | ||
chunk = curl_slist_append(chunk, | ||
"User-Agent: Mozilla/5.0 (Windows NT 10.0; Win64; x64) " | ||
"AppleWebKit/537.36 (KHTML, like Gecko) " | ||
"Chrome/56.0.2924.87 Safari/537.36"); | ||
curl_easy_setopt(curl, CURLOPT_HTTPHEADER, chunk); | ||
|
||
curl_easy_perform(curl); // download image file | ||
|
||
fclose(fp); | ||
curl_easy_cleanup(curl); | ||
curl_slist_free_all(chunk); | ||
} | ||
string_replace(cqcode_call, raw_url, filename); | ||
} | ||
return cqcode_call; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
#pragma once | ||
|
||
#include <string> | ||
|
||
std::string message_escape(const std::string& msg); | ||
|
||
std::string message_unescape(const std::string& msg); | ||
|
||
std::string enhance_cq_code(const std::string& msg); |
Oops, something went wrong.