Exemple #1
0
func (exc *Executor) SendMessageToBot(msg *Message) {
	m := entity.MSG(entity.GROUPCHAT)
	m.To = "*****@*****.**"
	m.Body = msg.IncomingEvent.Data["body"]
	err := exc.xmppStream.Write(entity.ProduceStatic(m))
	if err != nil {
		exc.logger.Printf("failed to write message to xmpp stream: %v", err)
	}
}
Exemple #2
0
func (e *Executor) sendingRoutine() {
	for msg := range e.outgoingMsgs {
		m := entity.MSG(entity.GROUPCHAT)
		m.To = "*****@*****.**"
		m.Body = msg
		err := e.xmppStream.Write(entity.ProduceStatic(m))
		if err != nil {
			fmt.Printf("send error: %s", err)
		}
		time.Sleep(sleepDuration)
	}
}
Exemple #3
0
func doReply(sender string, typ entity.MessageType, body string) func(stream.Stream) error {
	return func(s stream.Stream) error {
		m := entity.MSG(typ)
		if typ != entity.GROUPCHAT {
			m.To = units.Bare2Full(ROOM, sender)
		} else {
			m.To = ROOM
		}
		m.Body = body
		return s.Write(entity.Encode(dyn.NewMessage(m.Type, m.To, m.Body)))
	}
}
Exemple #4
0
func (e *Executor) execute() {
	for script := range e.incomingScripts {
		e.stateMutex.Lock()
		err := lua.DoString(e.state, script)
		if err != nil {
			fmt.Printf("lua f*****g shit error: %s\n", err)
			m := entity.MSG(entity.GROUPCHAT)
			m.To = "*****@*****.**"
			m.Body = err.Error()
			e.xmppStream.Write(entity.ProduceStatic(m))
		}
		e.stateMutex.Unlock()
	}
}
Exemple #5
0
func (e *Executor) processIncomingEvents() {
	for evt := range e.incomingEvents {
		e.stateMutex.Lock()
		obj, _ := e.vm.Object("({})")
		for key, value := range evt.Data {
			obj.Set(key, value)
		}
		for _, handler := range e.eventHandlers[evt.Type] {
			_, err := handler.Call(obj.Value(), obj.Value())
			if err != nil {
				fmt.Printf("js f*****g shit error: %s\n", err)
				m := entity.MSG(entity.GROUPCHAT)
				m.To = "*****@*****.**"
				m.Body = err.Error()
				e.xmppStream.Write(entity.ProduceStatic(m))
			}
		}
		e.stateMutex.Unlock()
	}
}
Exemple #6
0
func (e *Executor) processIncomingEvents() {
	for evt := range e.incomingEvents {
		e.stateMutex.Lock()
		// get events table
		e.state.PushString(callbacksLocation)
		e.state.Table(lua.RegistryIndex)
		// get callbacks table for the specific event
		e.state.PushString(evt.Type)
		e.state.Table(-2)
		// loop over callbacks
		if !e.state.IsNil(-1) {
			e.state.PushNil()
			for e.state.Next(-2) {
				if e.state.IsFunction(-1) {
					// create the table which will be passed to the handler
					e.state.NewTable()
					// loop over the event data, populating the table
					for k, v := range evt.Data {
						e.state.PushString(k)
						e.state.PushString(v)
						e.state.SetTable(-3)
					}
					err := e.state.ProtectedCall(1, 0, 0)
					if err != nil {
						m := entity.MSG(entity.GROUPCHAT)
						m.To = "*****@*****.**"
						m.Body, _ = e.state.ToString(-1)
						e.xmppStream.Write(entity.ProduceStatic(m))
						e.state.Pop(1)
					}
				} else {
					e.state.Pop(1)
				}
			}
		}
		// pop callbacks table or nil value
		e.state.Pop(1)
		e.stateMutex.Unlock()
	}
}