Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

特殊字符需要做转义处理 #9

Closed
rmuchan opened this issue Aug 3, 2020 · 16 comments
Closed

特殊字符需要做转义处理 #9

rmuchan opened this issue Aug 3, 2020 · 16 comments
Labels
bug Something isn't working

Comments

@rmuchan
Copy link
Contributor

rmuchan commented Aug 3, 2020

特殊字符需要转义,以避免处理时发生混淆。(图为QQ中发送的实际消息和go-cqhttp上报事件的解析结果)
image
image
根据nonebot源码推断,原cqhttp会将&、[、]分别转义为&、[、],并将出现在CQ码参数中的英文逗号转义为,。

@Mrs4s Mrs4s added the bug Something isn't working label Aug 3, 2020
@Mrs4s
Copy link
Owner

Mrs4s commented Aug 3, 2020

下个版本处理

@Mrs4s
Copy link
Owner

Mrs4s commented Aug 3, 2020

草 有没有文档 CQ的文档站死了

@undefined-moe
Copy link
Contributor

@shigma
Copy link
Contributor

shigma commented Aug 3, 2020

@Mrs4s

  1. 对于原文中的 &[] 进行转义
  2. 对于 CQ 码内部字符串中的 &[],= 进行转义
  3. 其他字符不处理

举例:

image

]&[
[CQ:at,qq=123]
[CQ:face,id=187]

@takayama-lily
Copy link
Contributor

可以支持cqhttp中post_message_format的配置,以字符串或数组上报数据,数组不需要转义

Tsuk1ko added a commit to Tsuk1ko/cq-picsearcher-bot that referenced this issue Aug 7, 2020
@shigma
Copy link
Contributor

shigma commented Aug 9, 2020

可以等一下 CQHTTP 那边补完 spec 再进行实现:https://github.com/richardchien/cqhttp-protocol/issues/1

@yyuueexxiinngg
Copy link
Contributor

CQ文档原文:

规范

  • CQ码是大小写敏感的,使用时请注意大小写。
  • 对于不在CQ码内的消息(即文本消息),为了防止解析混淆,需要进行转义。转义规则如下:
& -> &
[ -> [
] -> ]
  • CQ码中的 function(功能名)与 key(参数名),仅支持大小写字母、数字、短横线(-)、下划线(_)及点号(.)。

  • 对于CQ码中的 value(参数值),为了防止解析混淆,需要进行转义。转义规则如下:

& -> &
[ -> [
] -> ]
, -> ,

Mrs4s added a commit that referenced this issue Aug 9, 2020
@Tsuk1ko
Copy link

Tsuk1ko commented Aug 10, 2020

4941f0c 经测试CQ码内的链接不会被转义,例如 CQ:share 的 url 或者 image 参数,CQ:image 的 file 参数

@Mrs4s
Copy link
Owner

Mrs4s commented Aug 10, 2020

4941f0c 经测试CQ码内的链接不会被转义,例如 CQ:share 的 url 或者 image 参数,CQ:image 的 file 参数

后续版本会处理的

@yyuueexxiinngg
Copy link
Contributor

4941f0c 经测试CQ码内的链接不会被转义,例如 CQ:share 的 url 或者 image 参数,CQ:image 的 file 参数

我在写cqhttp-mirai的时候同样提到了这个Issue, 未用过酷Q, 请教一下, 原CQHTTP是否会对这些参数进行转义, 我尝试参考CQHTTP的源码:
https://github.com/richardchien/coolq-http-api/blob/25b93a7b6269311abb653d9f645bd7e10f88ce1f/src/cqhttp/plugins/message_enhancer/message_enhancer.cpp#L249

auto url = pt.get_optional<string>("image.url");
if (url && !url->empty()) {
    segment.data["url"] = url.value();
 }

似乎只是添加了url字段, 并未对url值进行转义, 而此方法被注册在消息事件中, 据CQ官方文档推测, 传递给CQHTTP的消息应已被转义过, 所以没能弄明白此hook完成后是否对消息再次进行转义

谢谢

@Tsuk1ko
Copy link

Tsuk1ko commented Aug 11, 2020

@yyuueexxiinngg 根据我的使用经历原 CQHTTP 确实会对用户提供的 url 参数进行反转义后再处理图片发送等,我平时会发送一些 url 中带校验参数的图片,其中&我都自行转义成&amp;了,如果 CQHTTP 没有反转义那么就会发送失败,结果是原 CQHTTP 一直是能正常发送的(我到现在还开着酷Q没关),而 go-cqhttp 发送失败了,因此我才发现了这个问题

按理来说应该要转义,因为这是 CQ 码内的参数,而且由于[]&,在 URL 内也是特殊字符,使用 URL encode 会改变其意义,因此还是只能靠 CQ 码转义来解决

@Tsuk1ko
Copy link

Tsuk1ko commented Aug 11, 2020

4941f0c 经测试CQ码内的链接不会被转义,例如 CQ:share 的 url 或者 image 参数,CQ:image 的 file 参数

@Mrs4s @yyuueexxiinngg 我的表述有误,应该是“经测试CQ码内的链接没有被 go-cqhttp 反转义”

即如果我发送[CQ:image,url=https://abc.com/a=1&amp;b=2],那么 go-cqhttp 应当发送https://abc.com/a=1&b=2,而目前发送的却是https://abc.com/a=1&amp;b=2

@yyuueexxiinngg
Copy link
Contributor

用户提供的 url 参数进行反转义

其中&我都自行转义成&amp;

对于用户提供的CQ码我已进行反转义, 您取消转义之前测试cqpf已无问题, 现在的疑惑点在于CQHTTP发给用户的的url是否进行了转义

@Tsuk1ko
Copy link

Tsuk1ko commented Aug 11, 2020

用户提供的 url 参数进行反转义

其中&我都自行转义成&amp;

对于用户提供的CQ码我已进行反转义, 您取消转义之前测试cqpf已无问题, 现在的疑惑点在于CQHTTP发给用户的的url是否进行了转义

这还真没注意……印象中 QQ 提供的图片 URL 至少没有[],,至于&我没有印象有没有

但像我上面所说的,这是 CQ 码内部,因此我认为需要转义,否则用户侧要解析也会出现歧义


至于你提到的 https://github.com/richardchien/coolq-http-api/blob/25b93a7b6269311abb653d9f645bd7e10f88ce1f/src/cqhttp/plugins/message_enhancer/message_enhancer.cpp#L249

可能是因为 QQ 提供的图片 URL 不含有特殊字符因此没特地加上转义,或者是最初开发时没有考虑到这点,但是一直没出过问题所以没有意识到漏了转义?

@stdrc
Copy link
Contributor

stdrc commented Aug 11, 2020

CQ 码转义这块,可以参考下 https://cqhttp.cc/specs/message/string.html ,这边描述应该符合原先 酷Q 的

Mrs4s added a commit that referenced this issue Aug 12, 2020
@Mrs4s
Copy link
Owner

Mrs4s commented Aug 12, 2020

理论上已对转义有了完整的支持, 有问题请reopen

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working
Projects
None yet
Development

No branches or pull requests

8 participants