func Session(s stream.Stream) (err error) { iq := entity.IQ(entity.SET, &entity.SessionPrototype) if err = s.Write(entity.ProduceStatic(iq)); err == nil { s.Ring(func(b *bytes.Buffer) (done bool) { var _e entity.Entity if _e, err = entity.ConsumeStatic(b); err == nil { switch e := _e.(type) { case *entity.Iq: switch { case e.Id == iq.Id && e.Type == entity.RESULT: done = true } default: log.Println(reflect.TypeOf(e)) } } return }, 0) } return }
func Starter(s stream.Stream) (err error) { if err = s.Write(entity.Open(s.Server()).Produce()); err == nil { s.Ring(func(b *bytes.Buffer) (done bool) { _e, e := entity.ConsumeStatic(b) if _e != nil { switch e := _e.(type) { case *entity.Stream: s.Id(e.Id) done = true default: log.Println(reflect.TypeOf(e)) } } else if e == nil { err = errors.New(fmt.Sprint("unknown entity ", string(b.Bytes()))) } else { err = e } return }, 0) } return }
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) } }