2024-07-19 20:02:18 +08:00
|
|
|
package parsers
|
|
|
|
|
|
|
|
|
|
import (
|
|
|
|
|
"caatsm/internal/config"
|
|
|
|
|
"regexp"
|
|
|
|
|
)
|
|
|
|
|
|
2024-07-20 00:01:04 +08:00
|
|
|
var (
|
|
|
|
|
bodyTypePattern = regexp.MustCompile(`^\(([A-Z]{3})(.*\n?)+\)$`)
|
|
|
|
|
)
|
2024-07-19 20:02:18 +08:00
|
|
|
|
2024-07-20 00:01:04 +08:00
|
|
|
func FindPatterns(messageBody string) *config.BodyConfig {
|
2024-07-19 20:02:18 +08:00
|
|
|
if match := bodyTypePattern.FindStringSubmatch(messageBody); len(match) > 1 {
|
|
|
|
|
name := match[1]
|
2024-07-20 00:01:04 +08:00
|
|
|
patters := config.GetBodyPatterns()
|
|
|
|
|
if body, found := patters[name]; found {
|
|
|
|
|
return &body
|
2024-07-19 20:02:18 +08:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
return nil
|
|
|
|
|
}
|
|
|
|
|
|
2024-07-20 00:01:04 +08:00
|
|
|
func ParseBody(messageBody string) map[string]string {
|
|
|
|
|
if body := FindPatterns(messageBody); body != nil {
|
2024-07-19 20:02:18 +08:00
|
|
|
for _, pattern := range body.Patterns {
|
|
|
|
|
if matches := pattern.Expression.FindStringSubmatch(messageBody); matches != nil {
|
|
|
|
|
result := make(map[string]string)
|
|
|
|
|
for i, name := range pattern.Expression.SubexpNames() {
|
|
|
|
|
if i != 0 && name != "" {
|
|
|
|
|
result[name] = matches[i]
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
return result
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
return nil
|
|
|
|
|
}
|