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

Commit

Permalink
Add enhanced CQ code support
Browse files Browse the repository at this point in the history
  • Loading branch information
stdrc committed Feb 11, 2017
1 parent 9c0f5d8 commit a0b8569
Show file tree
Hide file tree
Showing 13 changed files with 686 additions and 50 deletions.
4 changes: 4 additions & 0 deletions CoolQHttpApi.vcxproj
Original file line number Diff line number Diff line change
Expand Up @@ -66,19 +66,23 @@
<ItemGroup>
<ClCompile Include="src\appmain.cpp" />
<ClCompile Include="src\base64.cpp" />
<ClCompile Include="src\cqcode.cpp" />
<ClCompile Include="src\dllmain.cpp" />
<ClCompile Include="src\encoding.cpp" />
<ClCompile Include="src\ini.c" />
<ClCompile Include="src\md5.cpp" />
<ClCompile Include="src\misc_functions.cpp" />
<ClCompile Include="src\request.cpp" />
<ClCompile Include="src\request_handlers.cpp" />
</ItemGroup>
<ItemGroup>
<ClInclude Include="src\app.h" />
<ClInclude Include="src\base64.h" />
<ClInclude Include="src\cqcode.h" />
<ClInclude Include="src\encoding.h" />
<ClInclude Include="src\ini.h" />
<ClInclude Include="src\log.h" />
<ClInclude Include="src\md5.h" />
<ClInclude Include="src\misc_functions.h" />
<ClInclude Include="src\request.h" />
<ClInclude Include="src\request_handlers.h" />
Expand Down
12 changes: 12 additions & 0 deletions CoolQHttpApi.vcxproj.filters
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,12 @@
<ClCompile Include="src\ini.c">
<Filter>src\ini</Filter>
</ClCompile>
<ClCompile Include="src\cqcode.cpp">
<Filter>src</Filter>
</ClCompile>
<ClCompile Include="src\md5.cpp">
<Filter>src\encoding</Filter>
</ClCompile>
</ItemGroup>
<ItemGroup>
<ClInclude Include="src\log.h">
Expand Down Expand Up @@ -91,5 +97,11 @@
<ClInclude Include="src\ini.h">
<Filter>src\ini</Filter>
</ClInclude>
<ClInclude Include="src\cqcode.h">
<Filter>src</Filter>
</ClInclude>
<ClInclude Include="src\md5.h">
<Filter>src\encoding</Filter>
</ClInclude>
</ItemGroup>
</Project>
2 changes: 2 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,8 @@

停用插件将会关闭 HTTP 线程,再次启用将重新读取配置文件。

另外,本插件所支持的 CQ 码在原生的基础上进行了一些增强,见 [CQ 码](https://richardchien.github.io/coolq-http-api/#/CQCode)

## 文档

更多文档,见 [CoolQ HTTP API 插件文档](https://richardchien.github.io/coolq-http-api/)
Expand Down
75 changes: 39 additions & 36 deletions docs/API.md
Original file line number Diff line number Diff line change
Expand Up @@ -60,10 +60,11 @@ Authorization: token kSLuTF2GC2Q4q4ugm3

#### 参数

| 字段名 | 数据类型 | 默认值 | 说明 |
| --------- | ------ | ---- | ------- |
| `user_id` | number | - | 对方 QQ 号 |
| `message` | string | - | 要发送的内容 |
| 字段名 | 数据类型 | 默认值 | 说明 |
| --------- | ------ | ----- | ------------------------ |
| `user_id` | number | - | 对方 QQ 号 |
| `message` | string | - | 要发送的内容 |
| `is_raw` | bool | false | 消息内容是否作为纯文本发送(即不解析 CQ 码) |

#### 响应数据

Expand All @@ -73,10 +74,11 @@ Authorization: token kSLuTF2GC2Q4q4ugm3

#### 参数

| 字段名 | 数据类型 | 默认值 | 说明 |
| ---------- | ------ | ---- | ------ |
| `group_id` | number | - | 群号 |
| `message` | string | - | 要发送的内容 |
| 字段名 | 数据类型 | 默认值 | 说明 |
| ---------- | ------ | ----- | ------------------------ |
| `group_id` | number | - | 群号 |
| `message` | string | - | 要发送的内容 |
| `is_raw` | bool | false | 消息内容是否作为纯文本发送(即不解析 CQ 码) |

#### 响应数据

Expand All @@ -86,10 +88,11 @@ Authorization: token kSLuTF2GC2Q4q4ugm3

#### 参数

| 字段名 | 数据类型 | 默认值 | 说明 |
| ------------ | ------ | ---- | --------------------------------- |
| `discuss_id` | number | - | 讨论组 ID(正常情况下看不到,需要从讨论组消息上报的数据中获得) |
| `message` | string | - | 要发送的内容 |
| 字段名 | 数据类型 | 默认值 | 说明 |
| ------------ | ------ | ----- | --------------------------------- |
| `discuss_id` | number | - | 讨论组 ID(正常情况下看不到,需要从讨论组消息上报的数据中获得) |
| `message` | string | - | 要发送的内容 |
| `is_raw` | bool | false | 消息内容是否作为纯文本发送(即不解析 CQ 码) |

#### 响应数据

Expand Down Expand Up @@ -280,39 +283,39 @@ Authorization: token kSLuTF2GC2Q4q4ugm3

#### 参数

| 字段名 | 数据类型 | 默认值 | 说明 |
| ---------- | ------ | ------- | ------------------------- |
| `group_id` | number | - | 群号 |
| `user_id` | number | - | QQ 号(不可以是登录号) |
| 字段名 | 数据类型 | 默认值 | 说明 |
| ---------- | ------ | ------- | -------------------------- |
| `group_id` | number | - | 群号 |
| `user_id` | number | - | QQ 号(不可以是登录号) |
| `no_cache` | bool | `false` | 是否不使用缓存(使用缓存可能更新不及时,但响应更快) |

#### 响应数据

| 字段名 | 数据类型 | 说明 |
| ------------------- | ------- | -------------------------------- |
| `group_id` | number | 群号 |
| `user_id` | number | QQ 号 |
| `nickname` | string | 昵称 |
| `card` | string | 群名片/备注 |
| `sex` | string | 性别,`male``female``unknown` |
| `age` | number | 年龄 |
| `area` | string | 地区 |
| `join_time` | number | 加群时间戳 |
| `last_sent_time` | number | 最后发言时间戳 |
| `level` | string | 成员等级 |
| `role` | string | 角色,`owner``admin``member` |
| `unfriendly` | bool | 是否不良记录成员(不确定) |
| `title` | string | 专属头衔 |
| `title_expire_time` | number | 专属头衔过期时间戳 |
| `card_changeable` | bool | 是否允许修改群名片(不确定) |
| 字段名 | 数据类型 | 说明 |
| ------------------- | ------ | -------------------------------- |
| `group_id` | number | 群号 |
| `user_id` | number | QQ 号 |
| `nickname` | string | 昵称 |
| `card` | string | 群名片/备注 |
| `sex` | string | 性别,`male``female``unknown` |
| `age` | number | 年龄 |
| `area` | string | 地区 |
| `join_time` | number | 加群时间戳 |
| `last_sent_time` | number | 最后发言时间戳 |
| `level` | string | 成员等级 |
| `role` | string | 角色,`owner``admin``member` |
| `unfriendly` | bool | 是否不良记录成员(不确定) |
| `title` | string | 专属头衔 |
| `title_expire_time` | number | 专属头衔过期时间戳 |
| `card_changeable` | bool | 是否允许修改群名片(不确定) |

### `/get_stranger_info` 获取陌生人信息

#### 参数

| 字段名 | 数据类型 | 默认值 | 说明 |
| ---------- | ------ | ------- | ------------------------- |
| `user_id` | number | - | QQ 号(不可以是登录号) |
| 字段名 | 数据类型 | 默认值 | 说明 |
| ---------- | ------ | ------- | -------------------------- |
| `user_id` | number | - | QQ 号(不可以是登录号) |
| `no_cache` | bool | `false` | 是否不使用缓存(使用缓存可能更新不及时,但响应更快) |

#### 响应数据
Expand Down
23 changes: 23 additions & 0 deletions docs/CQCode.md
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]
```
4 changes: 2 additions & 2 deletions io.github.richardchien.coolqhttpapi.json
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,8 @@
"ret": 1, // 返回码,固定为1
"apiver": 9, // Api版本,本SDK为9
"name": "HTTP API", // 应用名称
"version": "1.0.1", // 应用版本
"version_id": 3, // 应用顺序版本(每次发布时至少+1)
"version": "1.0.2", // 应用版本
"version_id": 4, // 应用顺序版本(每次发布时至少+1)
"author": "Richard Chien", // 应用作者
"description": "使酷 Q 支持通过 HTTP 上报事件信息及调用接口",
"event": [ // 事件列表,同一事件类型可重复定义(发布前请删除无用事件)
Expand Down
98 changes: 98 additions & 0 deletions src/cqcode.cpp
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, "&", "&amp;");
string_replace(tmp, "[", "&#91;");
string_replace(tmp, "]", "&#93;");
string_replace(tmp, ",", "&#44;");
return tmp;
}

string message_unescape(const string& msg)
{
string tmp = msg;
string_replace(tmp, "&#91;", "[");
string_replace(tmp, "&#93;", "]");
string_replace(tmp, "&#44;", ",");
string_replace(tmp, "&amp;", "&");
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;
}
9 changes: 9 additions & 0 deletions src/cqcode.h
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);
Loading

0 comments on commit a0b8569

Please sign in to comment.