Exemplo n.º 1
0
Arquivo: bind.go Projeto: kpmy/xippo
func (b *Bind) Act() func(s stream.Stream) error {
	return func(s stream.Stream) (err error) {
		bind := &entity.Bind{}
		*bind = entity.BindPrototype
		bind.Resource = b.Rsrc
		iq := entity.IQ(entity.SET, bind)
		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:
							stream.Bind(s, e.Inner.(*entity.Bind).Jid)
							done = true
						}
					default:
						log.Println(reflect.TypeOf(e))
					}
				}
				return
			}, 0)
		}
		return
	}
}
Exemplo n.º 2
0
Arquivo: first.go Projeto: kpmy/xippo
func (n *Negotiation) Act() func(stream.Stream) error {
	return func(s stream.Stream) (err error) {
		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.Features:
					n.AuthMechanisms = e.Mechanisms
					done = true
				default:
					log.Println(reflect.TypeOf(e))
				}
			}
			return
		}, 0)
		return
	}
}
Exemplo n.º 3
0
Arquivo: bind.go Projeto: kpmy/xippo
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
}
Exemplo n.º 4
0
func conv(fn func(entity.Entity)) func(*bytes.Buffer) bool {
	delayed := func(msg dom.Element) bool {
		for _, _e := range msg.Children() {
			if e, ok := _e.(dom.Element); ok && e.Name() == "delay" {
				return true
			}
		}
		return false
	}

	return func(in *bytes.Buffer) (done bool) {
		done = true
		//log.Println("IN")
		//log.Println(string(in.Bytes()))
		//log.Println()
		if _, err := xmlpath.Parse(bytes.NewBuffer(in.Bytes())); err == nil {

		} else {
			log.Println(err)
		}
		if _e, err := entity.Decode(bytes.NewBuffer(in.Bytes())); err == nil {
			e := _e.Model()
			switch e.Name() {
			case dyn.MESSAGE:
				if !delayed(e) {
					if ent, err := entity.ConsumeStatic(in); err == nil {
						fn(ent)
					} else {
						log.Println(err)
					}
				}
			case dyn.PRESENCE:
				fn(_e)
			}
		} else {
			log.Println(err)
		}
		return
	}
}
Exemplo n.º 5
0
Arquivo: auth.go Projeto: kpmy/xippo
func (p *PlainAuth) Act() func(stream.Stream) error {
	return func(s stream.Stream) (err error) {
		auth := &entity.PlainAuth{}
		*auth = entity.PlainAuthPrototype
		auth.Init(p.Client.Name, p.Pwd)
		if err = s.Write(entity.ProduceStatic(auth)); 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.Success:
						done = true
					default:
						log.Println(reflect.TypeOf(e))
					}
				}
				return
			}, 0)
		}
		return
	}
}
Exemplo n.º 6
0
Arquivo: first.go Projeto: kpmy/xippo
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
}