Пример #1
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)))
	}
}
Пример #2
0
func bot(st stream.Stream) error {
	actors.With().Do(actors.C(steps.PresenceTo(units.Bare2Full(ROOM, ME), entity.CHAT, "ПЩ сюды: https://github.com/kpmy/xep"))).Run(st)
	executor = luaexecutor.NewExecutor(st)
	executor.Start()
	jsexec = jsexecutor.NewExecutor(st)
	jsexec.Start()
	hookExec = hookexecutor.NewExecutor(st)
	hookExec.Start()
	for {
		st.Ring(conv(func(_e entity.Entity) {
			switch e := _e.(type) {
			case *entity.Message:
				if strings.HasPrefix(e.From, ROOM+"/") {
					sender := strings.TrimPrefix(e.From, ROOM+"/")
					um := muc.UserMapping()
					user := sender
					if u, ok := um[sender]; ok {
						user, _ = u.(string)
					}
					if e.Type == entity.GROUPCHAT {
						posts.Lock()
						posts.data = append(posts.data, Post{Nick: sender, User: user, Msg: e.Body})
						IncStat(user)
						posts.Unlock()
					}
					if sender != ME {
						executor.NewEvent(luaexecutor.IncomingEvent{"message",
							map[string]string{"sender": sender, "body": e.Body}})
						jsexec.NewEvent(jsexecutor.IncomingEvent{"message",
							map[string]string{"sender": sender, "body": e.Body}})
						hookExec.NewEvent(hookexecutor.IncomingEvent{"message",
							map[string]string{"sender": sender, "body": e.Body}})
						switch {
						case strings.HasPrefix(e.Body, "lua>"):
							go func(script string) {
								actors.With().Do(actors.C(doLua(script))).Run(st)
							}(strings.TrimPrefix(e.Body, "lua>"))
						case strings.HasPrefix(e.Body, "js>"):
							go func(script string) {
								actors.With().Do(actors.C(doJS(script))).Run(st)
							}(strings.TrimPrefix(e.Body, "js>"))
						case strings.HasPrefix(e.Body, "say"):
							go func(script string) {
								actors.With().Do(actors.C(doLuaAndPrint(script))).Run(st)
							}(strings.TrimSpace(strings.TrimPrefix(e.Body, "say")))
						}
					}
				}
			case dyn.Entity:
				switch e.Type() {
				case dyn.PRESENCE:
					if from := e.Model().Attr("from"); from != "" && strings.HasPrefix(from, ROOM+"/") {
						sender := strings.TrimPrefix(from, ROOM+"/")
						um := muc.UserMapping()
						user := sender
						if u, ok := um[sender]; ok {
							user, _ = u.(string)
						}
						if show := firstByName(e.Model(), "show"); e.Model().Attr("type") == "" && (show == nil || show.ChildrenCount() == 0) { //онлаен тип
							//go func() { actors.With().Do(actors.C(doLuaAndPrint(`"` + user + `, насяльника..."`))).Run(st) }()
							executor.NewEvent(luaexecutor.IncomingEvent{"presence",
								map[string]string{"sender": sender, "user": user}})
							log.Println("ONLINE", user)
						}
					}
				}
			default:
				log.Println(reflect.TypeOf(e))
			}
		}), 0)
	}
}