fix: 修复权限系统qq提取问题,新增权限问题

This commit is contained in:
lixiangwuxian 2024-10-20 03:20:37 +08:00
parent 049b5774bf
commit 6f78e3e30d
7 changed files with 148 additions and 14 deletions

View File

@ -1,6 +1,8 @@
package auth package auth
import ( import (
"strconv"
"git.lxtend.com/qqbot/constants" "git.lxtend.com/qqbot/constants"
"git.lxtend.com/qqbot/model" "git.lxtend.com/qqbot/model"
"git.lxtend.com/qqbot/sqlite3" "git.lxtend.com/qqbot/sqlite3"
@ -25,6 +27,14 @@ func HasPermission(qqID int64, level constants.PermissionLevel) bool {
return auth.Role <= level return auth.Role <= level
} }
func SetPermission(qqID int64, level constants.PermissionLevel) error {
auth := Auth{
Qqid: strconv.Itoa(int(qqID)),
Role: level,
}
return setAuth(auth)
}
func TryExecHandler(msg model.Message, level constants.PermissionLevel, handler model.Handler) (reply model.Reply) { func TryExecHandler(msg model.Message, level constants.PermissionLevel, handler model.Handler) (reply model.Reply) {
if HasPermission(msg.UserId, level) { if HasPermission(msg.UserId, level) {
return handler(msg) return handler(msg)

View File

@ -3,6 +3,7 @@ package auth
import "git.lxtend.com/qqbot/constants" import "git.lxtend.com/qqbot/constants"
type Auth struct { type Auth struct {
ID int64 `json:"id" db:"id"`
Qqid string `json:"qqid" db:"qqid"` Qqid string `json:"qqid" db:"qqid"`
Role constants.PermissionLevel `json:"role" db:"role"` Role constants.PermissionLevel `json:"role" db:"role"`
} }

View File

@ -22,3 +22,17 @@ func getAuth(qqID int64) (auth Auth, err error) {
} }
return auth, nil return auth, nil
} }
func setAuth(auth Auth) error {
tx, err := sqlite3.GetTran()
if err != nil {
return err
}
defer tx.Rollback()
_, err = tx.Exec("UPDATE auth SET role = ? WHERE qqid = ?", auth.Role, auth.Qqid)
if err != nil {
return err
}
err = tx.Commit()
return err
}

71
handler/auth/auth.go Normal file
View File

@ -0,0 +1,71 @@
package auth
import (
"log"
"regexp"
"strconv"
"git.lxtend.com/qqbot/auth"
"git.lxtend.com/qqbot/constants"
"git.lxtend.com/qqbot/handler"
"git.lxtend.com/qqbot/model"
)
func init() {
handler.RegisterFrontMatchHandler("用户权限", setUserLevel, constants.LEVEL_ROOT)
handler.RegisterHelpInform("用户权限", "用户权限 [用户] [权限] 设置用户权限。允许的权限有:根用户、管理员、信任、用户、拉黑")
}
var userLevelMap = map[string]constants.PermissionLevel{
"根用户": constants.LEVEL_ROOT,
"管理员": constants.LEVEL_ADMIN,
"信任": constants.LEVEL_TRUSTED,
"用户": constants.LEVEL_USER,
"拉黑": constants.LEVEL_BANNED,
}
func setUserLevel(msg model.Message) (reply model.Reply) {
re := regexp.MustCompile(`\s+`)
tokens := re.Split(msg.RawMsg, -1)
if len(tokens) < 3 {
return model.Reply{
ReplyMsg: "参数不足",
ReferOriginMsg: true,
FromMsg: msg,
}
}
userText := tokens[1]
if userId, err := model.ParseAtMessage(userText); err == nil {
userText = userId.Data.QQ
}
log.Println(userText)
user, err := strconv.Atoi(userText)
if err != nil {
return model.Reply{
ReplyMsg: "用户解析失败",
ReferOriginMsg: true,
FromMsg: msg,
}
}
levelText := tokens[2]
level, ok := userLevelMap[levelText]
if !ok {
return model.Reply{
ReplyMsg: "权限不存在",
ReferOriginMsg: true,
FromMsg: msg,
}
}
if err := auth.SetPermission(int64(user), level); err != nil {
return model.Reply{
ReplyMsg: "权限设置失败",
ReferOriginMsg: true,
FromMsg: msg,
}
}
return model.Reply{
ReplyMsg: "权限设置成功",
ReferOriginMsg: true,
FromMsg: msg,
}
}

View File

@ -19,6 +19,27 @@ type AtData struct {
QQ string `json:"qq"` QQ string `json:"qq"`
} }
func (msg *AtMessage) ToCQString() string {
return fmt.Sprintf(`\[CQ:at,qq=%s\]`, msg.Data.QQ)
}
func ParseAtMessage(data string) (*AtMessage, error) {
// 使用正则表达式提取QQ号
re := regexp.MustCompile(`\[CQ:at,qq=(\d+)\]`)
matches := re.FindStringSubmatch(data)
if len(matches) < 2 {
return nil, fmt.Errorf("数据格式不正确")
}
// 返回解析后的结构体
return &AtMessage{
Type: "at",
Data: AtData{
QQ: matches[1],
},
}, nil
}
// 回复消息结构体 // 回复消息结构体
type ReplyMessage struct { type ReplyMessage struct {
Type string `json:"type"` Type string `json:"type"`
@ -30,7 +51,12 @@ type ReplyData struct {
ID string `json:"id"` ID string `json:"id"`
} }
type CQImageMessage struct { type ImageMessage struct {
Type string `json:"type"`
Data ImageMessageData `json:"data"`
}
type ImageMessageData struct {
File string File string
SubType string SubType string
FileID string FileID string
@ -39,20 +65,20 @@ type CQImageMessage struct {
FileUnique string FileUnique string
} }
func (msg *CQImageMessage) ToCQString() (string, error) { func (msg *ImageMessage) ToCQString() (string, error) {
// URL 转义 // URL 转义
escapedURL := url.QueryEscape(msg.URL) escapedURL := url.QueryEscape(msg.Data.URL)
// 构造 CQ:image 字符串 // 构造 CQ:image 字符串
cqString := fmt.Sprintf("[CQ:image,file=%s,sub_type=%s,file_id=%s,url=%s,file_size=%d,file_unique=%s]", cqString := fmt.Sprintf("[CQ:image,file=%s,sub_type=%s,file_id=%s,url=%s,file_size=%d,file_unique=%s]",
msg.File, msg.SubType, msg.FileID, escapedURL, msg.FileSize, msg.FileUnique) msg.Data.File, msg.Data.SubType, msg.Data.FileID, escapedURL, msg.Data.FileSize, msg.Data.FileUnique)
return cqString, nil return cqString, nil
} }
func ParseCQImageMessage(data string) (*CQImageMessage, error) { func ParseCQImageMessage(data string) (*ImageMessage, error) {
// 使用正则表达式提取各个字段 // 使用正则表达式提取各个字段
re := regexp.MustCompile(`CQ:image,file=(.*?),sub_type=(.*?),file_id=(.*?),url=(.*?),file_size=(\d+),file_unique=(.*?)\]`) re := regexp.MustCompile(`\[CQ:image,file=(.*?),sub_type=(.*?),file_id=(.*?),url=(.*?),file_size=(\d+),file_unique=(.*?)\]`)
matches := re.FindStringSubmatch(data) matches := re.FindStringSubmatch(data)
if len(matches) < 7 { if len(matches) < 7 {
return nil, fmt.Errorf("数据格式不正确") return nil, fmt.Errorf("数据格式不正确")
@ -76,12 +102,23 @@ func ParseCQImageMessage(data string) (*CQImageMessage, error) {
} }
// 返回解析后的结构体 // 返回解析后的结构体
return &CQImageMessage{ // return &ImageMessageData{
// File: matches[1],
// SubType: matches[2],
// FileID: matches[3],
// URL: decodedURL,
// FileSize: fileSize,
// FileUnique: matches[6],
// }, nil
return &ImageMessage{
Type: "image",
Data: ImageMessageData{
File: matches[1], File: matches[1],
SubType: matches[2], SubType: matches[2],
FileID: matches[3], FileID: matches[3],
URL: decodedURL, URL: decodedURL,
FileSize: fileSize, FileSize: fileSize,
FileUnique: matches[6], FileUnique: matches[6],
},
}, nil }, nil
} }

View File

@ -1,6 +1,7 @@
package main package main
import ( import (
_ "git.lxtend.com/qqbot/handler/auth"
_ "git.lxtend.com/qqbot/handler/beatleader" _ "git.lxtend.com/qqbot/handler/beatleader"
_ "git.lxtend.com/qqbot/handler/echo" _ "git.lxtend.com/qqbot/handler/echo"
_ "git.lxtend.com/qqbot/handler/getweb" _ "git.lxtend.com/qqbot/handler/getweb"

View File

@ -124,7 +124,7 @@ func GenerateCongratulationImage(text string, inputFile, outputFile string, isGo
func isImageCQ(text string) (string, bool) { func isImageCQ(text string) (string, bool) {
if img, err := model.ParseCQImageMessage(text); err == nil { if img, err := model.ParseCQImageMessage(text); err == nil {
return img.URL, true return img.Data.URL, true
} }
return "", false return "", false
} }