예제 #1
0
// NewMessagesFromBody creates a new ygor message from a plain string.
func (srv *Server) NewMessagesFromBody(body string, recursion int) ([]*Message, error) {
	var msgs []*Message

	sentences, err := lexer.Split(body)
	if err != nil {
		return nil, err
	}

	for i := 0; i < 3; i++ {
		sentences, err = srv.Aliases.ExpandSentences(sentences, recursion)
		if err != nil {
			return nil, err
		}
	}

	for _, words := range sentences {
		if len(words) == 0 {
			continue
		}

		msg := NewMessage()

		msg.Recursion = recursion
		msg.Body = strings.Join(words, " ")
		msg.Command = words[0]

		if len(words) > 1 {
			msg.Args = append(msg.Args, words[1:]...)
		}

		msgs = append(msgs, msg)
	}

	return msgs, nil
}
예제 #2
0
파일: file.go 프로젝트: SalmonMode/ygor
func (file *File) ExpandSentence(words []string, recursion int) ([][]string, error) {
	sentences := make([][]string, len(words))

	// Resolve any alias found as first word.
	expanded, err := file.Resolve(words[0], recursion)
	if err != nil {
		return nil, err
	}

	sentences, err = lexer.Split(expanded)
	if err != nil {
		return nil, err
	}

	last := len(sentences) - 1
	sentences[last] = append(sentences[last], words[1:]...)

	return sentences, nil
}