Exemplo n.º 1
0
func (c *Client) handleChatMessage(chat *xmpp.ClientMessage) {
	c.Log.Debugln("[xmpp] chat: ", chat)
	if chat.Type != "chat" {
		return
	}
	if chat.Body == "" {
		return
	}

	cv, ok := c.conversations[xmpp.RemoveResourceFromJid(chat.From)]
	if !ok {
		cv = c.newConversation(chat.From)
	}

	if strings.HasPrefix(chat.Body, ".subscribe ") {
		action := strings.TrimPrefix(chat.Body, ".subscribe ")
		if action != "" {
			if err := cv.Proto.Subscribe(action, "", cv.handleProtoMessage); err != nil {
				c.Log.Errorln("[xmpp] subscribe:", err)
			}
		}
		return
	}

	cv.Proto.Publish(sarif.Message{
		Action: "natural/handle",
		Text:   chat.Body,
	})
}
Exemplo n.º 2
0
func (c *Client) newConversation(remote string) *conversation {
	user := xmpp.RemoveResourceFromJid(remote)
	client := sarif.NewClient("xmpp/" + user)
	client.Connect(c.Broker.NewLocalConn())
	cv := &conversation{
		Remote: remote,
		Proto:  client,
		Xmpp:   c,
	}
	if err := client.Subscribe("", "self", cv.handleProtoMessage); err != nil {
		c.Log.Errorln("[xmpp] new:", err)
	}
	c.conversations[user] = cv
	return cv
}