Esempio n. 1
0
func (s *Service) AnnotateReply(msg sarif.Message) sarif.Message {
	mu := sync.Mutex{}
	fin := make(chan bool)
	go func() {
		for name, a := range s.Cfg.Annotators {
			if a.Enabled && (time.Now().Sub(a.LastSeen) < s.ParserKeepAlive) {
				reply, ok := <-s.Request(sarif.Message{
					Action:      "natural/annotate/" + msg.Action,
					Destination: name,
					Text:        msg.Text,
					Payload:     msg.Payload,
				})
				if !ok || reply.IsAction("err") {
					continue
				}
				mu.Lock()
				if reply.Text != "" {
					msg.Text = reply.Text
				}
				if len(reply.Payload.Raw) > 0 {
					msg.Payload = reply.Payload
				}
				mu.Unlock()
			}
		}
		fin <- true
	}()

	select {
	case <-fin:
	case <-time.After(time.Second):
	}
	mu.Lock()
	defer mu.Unlock()

	natural.FormatMessage(&msg)
	msg.Text = s.TransformReply(msg.Text)
	return msg
}