plugin_parse.go 7.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268
  1. package core
  2. import (
  3. "fmt"
  4. "regexp"
  5. "strings"
  6. "time"
  7. "github.com/cdle/sillyplus/core/common"
  8. "github.com/cdle/sillyplus/utils"
  9. )
  10. func pluginParse(script string, uuid string) (*common.Function, []func()) {
  11. var cbs = []func(){}
  12. var rules []string
  13. var imType *common.Filter
  14. var userId *common.Filter
  15. var groupId *common.Filter
  16. var cron = map[string]string{}
  17. var admin bool
  18. var disable bool = plugin_disable.GetString(uuid) == "b:true"
  19. var priority int
  20. var title string
  21. var public bool
  22. var description string
  23. var icon string
  24. var version string = "v1.0.0"
  25. var author string
  26. var create_at string
  27. var module bool
  28. var encrypt bool
  29. var onStart bool
  30. var origin = "自定义"
  31. var https = []*common.Http{}
  32. var message *common.Reply
  33. var FindAll bool
  34. var hasForm bool
  35. var carry bool
  36. var classes = []string{}
  37. ks := map[string]bool{}
  38. ress := regexp.MustCompile(
  39. `\*\s?@([\d\w+-]+)\s+([^\n]+?)\n`,
  40. ).FindAllStringSubmatch(script, -1)
  41. for _, res := range ress {
  42. switch res[1] {
  43. case "rule", "match", "regex", "pattern":
  44. rule := strings.TrimSpace(res[2])
  45. rule = parseReply3(rule, func(s1, s2 string) {
  46. k := s1 + "." + s2
  47. if _, ok := ks[k]; !ok { //已改成表单提交触发
  48. // cbs = append(cbs, func() {
  49. // storage.Watch(MakeBucket(s1), s2, func(old, new, key string) *storage.Final {
  50. // return &storage.Final{
  51. // EndFunc: func() {
  52. // plugins.Set(uuid, "reload")
  53. // },
  54. // }
  55. // }, uuid)
  56. // })
  57. ks[k] = true
  58. }
  59. })
  60. _rs := []string{}
  61. FR:
  62. ress := regexp.MustCompile(`\[([^\s\[\]]+)\]`).FindAllStringSubmatch(rule, -1)
  63. if len(ress) != 0 {
  64. res := ress[len(ress)-1]
  65. var inner = res[1]
  66. slice := strings.SplitN(inner, ":", 2)
  67. name := slice[0]
  68. ps := ""
  69. if len(slice) == 2 {
  70. ps = slice[1]
  71. }
  72. if strings.HasSuffix(name, "?") {
  73. name = strings.TrimRight(name, "?")
  74. rep := ""
  75. if ps == "" {
  76. rep = fmt.Sprintf("[%s]", name)
  77. } else {
  78. rep = fmt.Sprintf("[%s:%s]", name, ps)
  79. }
  80. for l := range _rs {
  81. _rs[l] = strings.Replace(_rs[l], res[0], rep, 1)
  82. }
  83. rule1 := strings.Replace(rule, res[0], rep, 1)
  84. if len(_rs) == 0 {
  85. _rs = append(_rs, rule1)
  86. }
  87. rule = strings.Replace(rule, res[0], "", 1)
  88. rule = regexp.MustCompile("\x20{2,}").ReplaceAllString(rule, " ")
  89. rule = strings.TrimSpace(rule)
  90. _rs = append(_rs, rule)
  91. goto FR
  92. }
  93. }
  94. if len(_rs) != 0 {
  95. rules = append(rules, _rs...)
  96. } else {
  97. rules = append(rules, rule)
  98. }
  99. case "class":
  100. classes = append(classes, regexp.MustCompile(`[\S]+`).FindAllString(res[2], -1)...)
  101. classes = utils.Unique(classes)
  102. case "platform", "imType", "platform+", "imType+":
  103. var item []string
  104. for _, i := range regexp.MustCompile(`[\d\w-]+`).FindAllString(res[2], -1) {
  105. item = append(item, strings.TrimSpace(i))
  106. }
  107. imType = &common.Filter{
  108. BlackMode: false,
  109. Items: item,
  110. }
  111. case "platform-", "imType-":
  112. var item []string
  113. for _, i := range regexp.MustCompile(`[\d\w-]+`).FindAllString(res[2], -1) {
  114. item = append(item, strings.TrimSpace(i))
  115. }
  116. imType = &common.Filter{
  117. BlackMode: true,
  118. Items: item,
  119. }
  120. case "userId", "userID", "uid", "userId+", "userID+", "uid+":
  121. var item []string
  122. for _, i := range regexp.MustCompile(`[\d\w-]+`).FindAllString(res[2], -1) {
  123. item = append(item, strings.TrimSpace(i))
  124. }
  125. userId = &common.Filter{
  126. BlackMode: false,
  127. Items: item,
  128. }
  129. case "userId-", "userID-", "uid-":
  130. var item []string
  131. for _, i := range regexp.MustCompile(`[\d\w-]+`).FindAllString(res[2], -1) {
  132. item = append(item, strings.TrimSpace(i))
  133. }
  134. userId = &common.Filter{
  135. BlackMode: true,
  136. Items: item,
  137. }
  138. case "groupId", "groupID", "groupCode", "chat_id", "chat_id+", "chatId", "chatID", "gid", "groupId+", "groupID+", "groupCode+", "chatId+", "chatID+", "gid+":
  139. var item []string
  140. for _, i := range regexp.MustCompile(`[\d\w-]+`).FindAllString(res[2], -1) {
  141. item = append(item, strings.TrimSpace(i))
  142. }
  143. groupId = &common.Filter{
  144. BlackMode: false,
  145. Items: item,
  146. }
  147. case "groupId-", "groupID-", "groupCode-", "chatId-", "chat_id-", "chatID-", "gid-":
  148. var item []string
  149. for _, i := range regexp.MustCompile(`[\d\w-]+`).FindAllString(res[2], -1) {
  150. item = append(item, strings.TrimSpace(i))
  151. }
  152. groupId = &common.Filter{
  153. BlackMode: true,
  154. Items: item,
  155. }
  156. case "admin":
  157. admin = strings.TrimSpace(res[2]) == "true"
  158. // case "disable":
  159. // disable = strings.TrimSpace(res[2]) == "true"
  160. case "findall":
  161. FindAll = strings.TrimSpace(res[2]) == "true"
  162. case "priority":
  163. priority = utils.Int(strings.TrimSpace(res[2]))
  164. case "title", "name", "show":
  165. title = strings.TrimSpace(res[2])
  166. case "public":
  167. public = strings.TrimSpace(res[2]) == "true"
  168. case "description":
  169. description = strings.TrimSpace(res[2])
  170. case "icon":
  171. icon = strings.TrimSpace(res[2])
  172. case "version":
  173. version = strings.TrimSpace(res[2])
  174. case "author":
  175. author = strings.TrimSpace(res[2])
  176. case "http":
  177. ss := regexp.MustCompile(`[\S]+`).FindAllString(strings.TrimSpace(res[2]), -1)
  178. if len(ss) == 2 {
  179. https = append(https, &common.Http{
  180. Path: ss[1],
  181. Method: strings.ToUpper(ss[0]),
  182. })
  183. } else {
  184. console.Warn("http param is not 2")
  185. }
  186. case "message":
  187. ss := regexp.MustCompile(`[\S]+`).FindAllString(strings.TrimSpace(res[2]), -1)
  188. if len(ss) > 1 {
  189. if len(ss) == 2 && ss[1] == "*" {
  190. message = &common.Reply{
  191. Platform: ss[0],
  192. BotsID: []string{},
  193. }
  194. } else {
  195. message = &common.Reply{
  196. Platform: ss[0],
  197. BotsID: ss[1:],
  198. }
  199. }
  200. } else {
  201. console.Warn("message param is 0")
  202. }
  203. case "create_at":
  204. create_at = strings.TrimSpace(res[2])
  205. case "origin":
  206. origin = strings.TrimSpace(res[2])
  207. case "module":
  208. module = strings.TrimSpace(res[2]) == "true"
  209. case "carry":
  210. carry = strings.TrimSpace(res[2]) == "true"
  211. case "encrypt":
  212. encrypt = strings.TrimSpace(res[2]) == "true"
  213. case "on_start", "service":
  214. onStart = strings.TrimSpace(res[2]) == "true"
  215. case "form":
  216. hasForm = true
  217. case "paterner":
  218. paterner := strings.TrimSpace(res[2])
  219. go func() {
  220. time.Sleep(time.Second * 2)
  221. getPaterner(uuid, strings.TrimSpace(paterner))
  222. }()
  223. default:
  224. cron_ := strings.TrimSpace(res[2])
  225. cron_ = strings.ReplaceAll(cron_, `\/`, "/")
  226. if strings.HasPrefix(res[1], "cron") {
  227. cron[res[1]] = cron_
  228. }
  229. }
  230. }
  231. if !hasForm {
  232. hasForm = strings.Contains(script, "Form(")
  233. }
  234. return &common.Function{
  235. Rules: rules,
  236. ImType: imType,
  237. UserId: userId,
  238. GroupId: groupId,
  239. Cron: cron,
  240. Admin: admin,
  241. Priority: priority,
  242. Disable: disable,
  243. UUID: uuid,
  244. Title: title,
  245. Public: public,
  246. Description: description,
  247. Icon: icon,
  248. Version: version,
  249. Author: author,
  250. CreateAt: create_at,
  251. Module: module,
  252. Encrypt: encrypt,
  253. OnStart: onStart,
  254. Origin: origin,
  255. Running: onStart,
  256. Reply: message,
  257. Https: https,
  258. FindAll: FindAll,
  259. HasForm: hasForm,
  260. Carry: carry,
  261. Classes: classes,
  262. }, cbs
  263. }