コード例 #1
0
ファイル: executor.go プロジェクト: postman0/xep
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)
	}
}
コード例 #2
0
ファイル: executor.go プロジェクト: postman0/xep
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)
	}
}
コード例 #3
0
ファイル: xmpp.go プロジェクト: golang-cjr/xep
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)))
	}
}
コード例 #4
0
ファイル: executor.go プロジェクト: postman0/xep
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()
	}
}
コード例 #5
0
ファイル: executor.go プロジェクト: postman0/xep
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()
	}
}
コード例 #6
0
ファイル: executor.go プロジェクト: postman0/xep
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()
	}
}