func LoadStrings(file *os.File, set *map[string]struct{}, logger *logs.Logger) error { bytes, err := ReadAll(file) if err != nil { return err } lines := split([]rune(string(bytes)), []rune{'\n', ' '}) newSet := map[string]struct{}{} oldSet := map[string]struct{}{} for k, v := range *set { oldSet[k] = v //复制一遍 } var added []string for _, _line := range lines { var line string if line = strings.TrimSpace(string(_line)); line == "" { continue } newSet[line] = struct{}{} if _, exist := oldSet[line]; !exist { added = append(added, line) } delete(oldSet, line) } *set = newSet var updateInfo string = fmt.Sprintf("更新关键词(%s):", file.Name()) if len(added) > 0 { updateInfo = updateInfo + "\n[+]" for _, str := range added { updateInfo = updateInfo + str + " " } updateInfo = strings.TrimRight(updateInfo, " ") } if len(oldSet) > 0 { updateInfo = updateInfo + "\n[-]" for str, _ := range oldSet { updateInfo = updateInfo + str + " " } updateInfo = strings.TrimRight(updateInfo, " ") } logger.Info(updateInfo) return nil }
func newStrKwManager(fileName string, logger *logs.Logger) *kw_manager.StringKeywordManager { var m *kw_manager.StringKeywordManager var err error if fileName != "" { m, err = kw_manager.NewStringKeywordManagerBidingWithFile( fileName, time.Second, logger) if err != nil { logger.Error("无法创建string关键词管理.", err) panic(err) } return m } else { logger.Warn("未设置string关键词文件") return kw_manager.NewStringKeywordManager(logger) } }
func NewMessageMonitor(logger *logs.Logger, interval time.Duration, acc *postbar.Account, lastFoundPid uint64) *MessageMonitor { var monitor MessageMonitor monitor.Interval = interval monitor.MessageChan = make(chan message.ReplyMessage) monitor.actChan = make(chan action.Action) go func() { ticker := time.NewTicker(monitor.Interval) for { msgs, _lastFoundPid, err, pberr := checkReply(acc, lastFoundPid) if len(msgs) > 0 { if err != nil { logger.Error("无法获取消息提醒", err) continue } else if pberr != nil && pberr.ErrorCode != 0 { logger.Error("无法获取消息提醒", pberr) continue } lastFoundPid = _lastFoundPid for _, msg := range msgs { monitor.MessageChan <- msg } } select { case <-ticker.C: case act := <-monitor.actChan: switch act.Pattern { case Stop: ticker.Stop() //logs.Debug("喵") close(monitor.MessageChan) //logs.Debug("喵喵") close(monitor.actChan) //logs.Debug("将解引赋值:", lastFoundPid) *(act.Param.(*chan uint64)) <- lastFoundPid return } } } }() return &monitor }
func NewRegexpKeywordManagerBidingWithFile(keyWordFileFlieName string, checkInterval time.Duration, logger *logs.Logger) (*RegexpKeywordManager, error) { var m RegexpKeywordManager m.FileName = keyWordFileFlieName file, err := os.Open(m.FileName) if os.IsNotExist(err) { var err error file, err = os.Create(m.FileName) if err != nil { return nil, err } } else if err != nil { return nil, err } else { err := LoadExps(file, &m.KewWordExps, logger) if err != nil { return nil, err } } if fi, err := file.Stat(); err != nil { return nil, err } else { m.LastModTime = fi.ModTime() } file.Close() m.CheckInterval = checkInterval m.actChan = make(chan action.Action) go func() { ticker := time.NewTicker(m.CheckInterval) for { select { case <-ticker.C: case act := <-m.actChan: switch act.Pattern { case ChangeInterval: ticker.Stop() ticker = time.NewTicker(act.Param.(time.Duration)) case ChangeFile: } continue } file, err1 := os.Open(m.FileName) if err1 != nil { logger.Error("无法打开关键词文件,跳过本次.", err1, ".") continue } func() { defer func() { file.Close() }() fi, err2 := file.Stat() if err2 != nil { logger.Error("无法获取文件信息,跳过本次.", err2, ".") return } if modTime := fi.ModTime(); modTime != m.LastModTime { err := LoadExps(file, &m.KewWordExps, logger) if err != nil { logger.Error("无法更新关键词,下次修改前将不尝试读取.", err, ".") } m.LastModTime = modTime } }() } }() return &m, nil }
func LoadExps(file *os.File, exps *[]RegexpKeyword, logger *logs.Logger) error { bytes, err := ReadAll(file) if err != nil { return err } lines := strings.Split(string(bytes), "\n") oldExps := make(map[string]RegexpKeyword) for _, exp := range *exps { if exp.BanFlag { oldExps["$ban "+exp.Rx.String()] = exp } else { oldExps[exp.Rx.String()] = exp } } newExps := make(map[string]RegexpKeyword) var addedExps []string for lineNo, line := range lines { line = strings.TrimRightFunc(line, func(r rune) bool { return r == '\n' || r == '\r' }) if line == "" { continue } if exp, exist := oldExps[line]; exist { newExps[line] = exp delete(oldExps, line) } else { var banFlag bool var newExp *regexp.Regexp var err error if banFlag = strings.HasPrefix(line, "$ban "); banFlag { newExp, err = regexp.Compile(strings.TrimLeft(line, "$ban ")) } else { newExp, err = regexp.Compile(line) } if err != nil { logs.Error(fmt.Sprintf("不正确的关键词(第%d行),跳过.", lineNo), err) } else { newExps[line] = RegexpKeyword{banFlag, newExp} addedExps = append(addedExps, line) } } } newExpSlice := make([]RegexpKeyword, 0, len(newExps)) for _, exp := range newExps { newExpSlice = append(newExpSlice, exp) } *exps = newExpSlice var updateInfo string = fmt.Sprintf("更新关键词(%s):\n", file.Name()) for _, exp := range addedExps { updateInfo = updateInfo + "[+] " + exp + "\n" } for _, exp := range oldExps { if exp.BanFlag { updateInfo = updateInfo + "[-] $ban" + exp.Rx.String() + "\n" } else { updateInfo = updateInfo + "[-] " + exp.Rx.String() + "\n" } } updateInfo = strings.TrimSuffix(updateInfo, "\n") logger.Info(updateInfo) //logger.Debug("现在的关键词:", newExpSlice, ".") return nil }