Skip to content

Commit

Permalink
🔖 Update to 1.1.2 beta
Browse files Browse the repository at this point in the history
  • Loading branch information
kanrichan committed Dec 13, 2020
1 parent ed4d891 commit e1d2b2d
Show file tree
Hide file tree
Showing 10 changed files with 397 additions and 28 deletions.
2 changes: 1 addition & 1 deletion app.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ type AppInfo struct {
func newAppInfo() *AppInfo {
return &AppInfo{
Name: "OneBot-YaYa",
Pver: "1.1.1 beta",
Pver: "1.1.2 beta",
Sver: 3,
Author: "kanri",
Desc: "OneBot标准的先驱实现 项目地址: http://github.com/Yiwen-Chan/OneBot-YaYa",
Expand Down
4 changes: 2 additions & 2 deletions core/event.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ package core
import "C"

var Create func(version string) string
var Event func(selfID int64, mseeageType int64, subType int64, groupID int64, userID int64, noticID int64, message string, messageNum int64, messageID int64, rawMessage string, time int64, ret int64) int64
var Event func(selfID int64, mseeageType int64, subType int64, groupID int64, userID int64, noticID int64, message string, messageNum int64, messageID int64, rawMessage []byte, time int64, ret int64) int64
var DestroyPlugin func() int64
var SetUp func() int64

Expand All @@ -23,7 +23,7 @@ func GO_Event(selfID *C.char, mseeageType C.int, subType C.int, groupID *C.char,
GoString(message),
CStr2GoInt(messageNum),
CStr2GoInt(messageID),
GoString(rawMessage),
[]byte(GoString(rawMessage)),
CStr2GoInt(time),
CStr2GoInt(ret),
))
Expand Down
2 changes: 2 additions & 0 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,9 @@ module yaya
go 1.14

require (
github.com/cockroachdb/pebble v0.0.0-20201212194246-100e1a4acbe0
github.com/gorilla/websocket v1.4.2
github.com/mattn/go-sqlite3 v1.14.5
github.com/tidwall/gjson v1.6.3
golang.org/x/text v0.3.4
gopkg.in/yaml.v3 v3.0.0-20200615113413-eeeca48fe776
Expand Down
276 changes: 276 additions & 0 deletions go.sum

Large diffs are not rendered by default.

2 changes: 2 additions & 0 deletions onebot/cqhttpConfig.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import (

"yaya/core"

"database/sql"
"github.com/gorilla/websocket"
)

Expand Down Expand Up @@ -38,6 +39,7 @@ type HeratBeatYaml struct {

type BotYaml struct {
Bot int64 `yaml:"bot"`
DB *sql.DB `yaml:"-"`
WSSConf []*WSSYaml `yaml:"websocket"`
WSCConf []*WSCYaml `yaml:"websocket_reverse"`
HTTPConf []*HTTPYaml `yaml:"http"`
Expand Down
8 changes: 3 additions & 5 deletions onebot/cqhttpWSS.go
Original file line number Diff line number Diff line change
Expand Up @@ -28,11 +28,9 @@ func (s *WSSYaml) ServeHTTP(w http.ResponseWriter, r *http.Request) {
if err != nil {
panic(err)
}
if r.URL.Path != "/api" {
s.Conn = append(s.Conn, conn)
s.Status = 1
s.handShake()
}
s.Conn = append(s.Conn, conn)
s.Status = 1
s.handShake()
} else {
ERROR("[连接][正向WS][%v] BOT X Token X %v:%v", s.BotID, s.Host, s.Port)
}
Expand Down
4 changes: 2 additions & 2 deletions onebot/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@ var FirstStart bool = true

var XQPath = PathExecute()
var AppPath = XQPath + "OneBot/"
var DatabasePath = XQPath + "OneBot/database/"
var ImagePath = XQPath + "OneBot/image/"
var RecordPath = XQPath + "OneBot/record/"
var VideoPath = XQPath + "OneBot/video/"
Expand All @@ -18,16 +17,17 @@ func Main() {
func onStart() {
if FirstStart {
CreatePath(AppPath)
CreatePath(DatabasePath)
CreatePath(ImagePath)
CreatePath(RecordPath)
CreatePath(VideoPath)

INFO("夜夜は世界一かわいい")
Conf = Load(AppPath + "config.yml")
if Conf == nil {
ERROR("晚安~")
return
}
go Conf.runDB()
go Conf.runOnebot()
}
FirstStart = false
Expand Down
39 changes: 39 additions & 0 deletions onebot/media.go
Original file line number Diff line number Diff line change
Expand Up @@ -130,3 +130,42 @@ func groupHonor(groupID int64, honorType int64, cookie string) []byte {
}
return data
}

func Base642ImageBytes(res string) []byte {
data, err := base64.StdEncoding.DecodeString(res)
if err != nil {
ERROR("base64编码解码失败")
}
return data
}

func Url2ImageBytes(url string) []byte {
client := &http.Client{}
reqest, err := http.NewRequest("GET", url, nil)
reqest.Header.Add("User-Agent", "QQ/8.2.0.1296 CFNetwork/1126")
reqest.Header.Add("Net-Type", "Wifi")
if err != nil {
ERROR("[CQ码解析] 从TX服务器图片%s下载失败", url)
return nil
}
resp, err := client.Do(reqest)
if err != nil {
ERROR("[CQ码解析] 从TX服务器图片%s下载失败", url)
return nil
}
defer resp.Body.Close()
data, err := ioutil.ReadAll(resp.Body)
if err != nil {
ERROR("[CQ码解析] 从TX服务器图片%s下载失败", url)
return nil
}
return data
}

func Path2ImageBytes(path string) []byte {
data, err := ioutil.ReadFile(path)
if err != nil {
return nil
}
return data
}
8 changes: 5 additions & 3 deletions onebot/utils.go
Original file line number Diff line number Diff line change
Expand Up @@ -49,9 +49,11 @@ func PathExecute() string {
}

func CreatePath(path string) {
err := os.MkdirAll(path, 0644)
if err != nil {
ERROR("生成应用目录失败")
if !PathExists(path) {
err := os.MkdirAll(path, 0644)
if err != nil {
ERROR("生成应用目录失败")
}
}
}

Expand Down
80 changes: 65 additions & 15 deletions onebot/xq2cqEvent.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@ package onebot
import (
"encoding/json"
"fmt"
//"strings"

"yaya/core"
)
Expand Down Expand Up @@ -35,16 +34,17 @@ type XEvent struct {
message string
messageNum int64
messageID int64
rawMessage string
rawMessage []byte
time int64
ret int64
cqID int64
}

func XQCreate(version string) string {
return AppInfoJson
}

func XQEvent(selfID int64, mseeageType int64, subType int64, groupID int64, userID int64, noticID int64, message string, messageNum int64, messageID int64, rawMessage string, time int64, ret int64) int64 {
func XQEvent(selfID int64, mseeageType int64, subType int64, groupID int64, userID int64, noticID int64, message string, messageNum int64, messageID int64, rawMessage []byte, time int64, ret int64) int64 {
xe := XEvent{
selfID: selfID,
mseeageType: mseeageType,
Expand All @@ -58,7 +58,9 @@ func XQEvent(selfID int64, mseeageType int64, subType int64, groupID int64, user
rawMessage: rawMessage,
time: time,
ret: ret,
cqID: 0,
}

switch mseeageType {
case 12001:
go ProtectRun(func() { onStart() }, "onStart()")
Expand All @@ -67,17 +69,33 @@ func XQEvent(selfID int64, mseeageType int64, subType int64, groupID int64, user
// 消息事件
// 0:临时会话 1:好友会话 4:群临时会话 7:好友验证会话
case 0, 1, 4, 5, 7:
for i, _ := range Conf.BotConfs {
if selfID == Conf.BotConfs[i].Bot && selfID != 0 {
if Conf.BotConfs[i].DB != nil {
xe.event2DB(Conf.BotConfs[i].DB)
}
}
}
go ProtectRun(func() { onPrivateMessage(xe) }, "onPrivateMessage()")
/*if strings.Contains(message, "/") {
go ProtectRun(func() { commandHandle(xe) }, "commandHandle()")
}*/
// 2:群聊信息
case 2, 3:
for i, _ := range Conf.BotConfs {
if selfID == Conf.BotConfs[i].Bot && selfID != 0 {
if Conf.BotConfs[i].DB != nil {
xe.event2DB(Conf.BotConfs[i].DB)
}
}
}
go ProtectRun(func() { onGroupMessage(xe) }, "onGroupMessage()")
/*if strings.Contains(message, "/") {
go ProtectRun(func() { commandHandle(xe) }, "commandHandle()")
}*/

// 10:回音信息
case 10:
for i, _ := range Conf.BotConfs {
if selfID == Conf.BotConfs[i].Bot && selfID != 0 {
if Conf.BotConfs[i].DB != nil {
xe.event2DB(Conf.BotConfs[i].DB)
}
}
}
// 通知事件
// 群文件接收
case 218:
Expand Down Expand Up @@ -107,6 +125,13 @@ func XQEvent(selfID int64, mseeageType int64, subType int64, groupID int64, user
// 群消息撤回 subType 2
// 好友消息撤回 subType 1
case 9:
for i, _ := range Conf.BotConfs {
if selfID == Conf.BotConfs[i].Bot && selfID != 0 {
if Conf.BotConfs[i].DB != nil {
xe.xq2cqid(Conf.BotConfs[i].DB)
}
}
}
if xe.subType == 2 {
go ProtectRun(func() { noticGroupMsgDelete(xe) }, "noticGroupMsgDelete()")
} else {
Expand Down Expand Up @@ -148,21 +173,35 @@ func WSCPush(bot int64, e Event, c *Yaml) {
}
}()

send, _ := json.Marshal(e)
for i, _ := range c.BotConfs {
if bot == c.BotConfs[i].Bot {
for j, _ := range c.BotConfs[i].WSSConf {
if c.BotConfs[i].WSSConf[j].Status == 1 && c.BotConfs[i].WSSConf[j].Enable == true && c.BotConfs[i].WSSConf[j].Host != "" {
ce := e
if c.BotConfs[i].WSSConf[j].PostMessageFormat == "array" {
ce["message"] = cqCode2Array(e["message"].(string))
}
send, _ := json.Marshal(ce)
c.BotConfs[i].WSSConf[j].Event <- send
}
}
for k, _ := range c.BotConfs[i].WSCConf {
if c.BotConfs[i].WSCConf[k].Status == 1 && c.BotConfs[i].WSCConf[k].Enable == true && c.BotConfs[i].WSCConf[k].Url != "" {
ce := e
if c.BotConfs[i].WSCConf[k].PostMessageFormat == "array" {
ce["message"] = cqCode2Array(e["message"].(string))
}
send, _ := json.Marshal(ce)
c.BotConfs[i].WSCConf[k].Event <- send
}
}
for l, _ := range c.BotConfs[i].HTTPConf {
ce := e
if c.BotConfs[i].HTTPConf[l].Status == 1 && c.BotConfs[i].HTTPConf[l].Enable == true && c.BotConfs[i].HTTPConf[l].Host != "" {
if c.BotConfs[i].HTTPConf[l].PostMessageFormat == "array" {
ce["message"] = cqCode2Array(e["message"].(string))
}
send, _ := json.Marshal(ce)
c.BotConfs[i].HTTPConf[l].Event <- send
}
}
Expand All @@ -171,6 +210,17 @@ func WSCPush(bot int64, e Event, c *Yaml) {

}

func xq2cqMsgID(xqid int64, xqnum int64) int64 {
return core.Str2Int(fmt.Sprintf("%01d%02d%06d%010d", len(core.Int2Str(xqid)), len(core.Int2Str(xqnum)), xqid, xqnum))
}

func cq2xqMsgID(cqid int64) (int64, int64) {
idLen := core.Str2Int(core.Int2Str(cqid)[0:1])
numLen := core.Str2Int(core.Int2Str(cqid)[1:3])
return core.Str2Int(core.Int2Str(cqid)[(9 - idLen):9]),
core.Str2Int(core.Int2Str(cqid)[(19 - numLen):19])
}

func onPrivateMessage(xe XEvent) {
Tsubtype := "error"
switch xe.mseeageType {
Expand All @@ -193,7 +243,7 @@ func onPrivateMessage(xe XEvent) {
"post_type": "message",
"message_type": "private",
"sub_type": Tsubtype,
"message_id": xe.messageID,
"message_id": xe.cqID,
"user_id": xe.userID,
"message": xq2cqCode(xe.message),
"raw_message": xq2cqCode(xe.message),
Expand Down Expand Up @@ -224,7 +274,7 @@ func onGroupMessage(xe XEvent) {
"post_type": "message",
"message_type": Tmessagetype,
"sub_type": "normal",
"message_id": xe.messageID,
"message_id": xe.cqID,
"group_id": xe.groupID,
"user_id": xe.userID,
"anonymous": nil,
Expand Down Expand Up @@ -358,7 +408,7 @@ func noticGroupMsgDelete(xe XEvent) {
"group_id": xe.groupID,
"user_id": xe.noticID,
"operator_id": xe.userID,
"message_id": 0,
"message_id": xe.cqID,
}
WSCPush(xe.selfID, e, Conf)
}
Expand All @@ -371,7 +421,7 @@ func noticFriendMsgDelete(xe XEvent) {
"post_type": "notice",
"notice_type": "friend_recall",
"user_id": xe.noticID,
"message_id": 0,
"message_id": xe.cqID,
}
WSCPush(xe.selfID, e, Conf)
}
Expand Down

0 comments on commit e1d2b2d

Please sign in to comment.