Esempio n. 1
0
        private IQ ProcessBind(XmppStream stream, IQ iq, XmppHandlerContext context)
        {
            if (iq.Type != IqType.set)
            {
                return(XmppStanzaError.ToBadRequest(iq));
            }

            var answer = new IQ(IqType.result);

            answer.Id = iq.Id;

            var bind     = (Bind)iq.Bind;
            var resource = !string.IsNullOrEmpty(bind.Resource) ? bind.Resource : stream.User;

            if (bind.TagName.Equals("bind", StringComparison.OrdinalIgnoreCase))
            {
                var jid = new Jid(stream.User, stream.Domain, resource);

                var session = context.SessionManager.GetSession(jid);
                if (session != null)
                {
                    if (session.Stream.Id != stream.Id)
                    {
                        context.Sender.SendToAndClose(session.Stream, XmppStreamError.Conflict);
                        answer.AddTag("newTalkWindow", "true");
                    }
                    else
                    {
                        return(XmppStanzaError.ToConflict(iq));
                    }
                }

                stream.BindResource(resource);
                context.SessionManager.AddSession(new XmppSession(jid, stream));
                answer.Bind = new Bind(jid);
            }
            else if (bind.TagName.Equals("unbind", StringComparison.OrdinalIgnoreCase))
            {
                if (!stream.Resources.Contains(resource))
                {
                    return(XmppStanzaError.ToNotFound(iq));
                }

                context.SessionManager.CloseSession(iq.From);
                stream.UnbindResource(resource);
                if (stream.Resources.Count == 0)
                {
                    context.Sender.CloseStream(stream);
                }
            }
            else
            {
                return(XmppStanzaError.ToBadRequest(iq));
            }
            if (stream.MultipleResources)
            {
                answer.To = iq.From;
            }
            return(answer);
        }
        private Element ValidateClientStanza(Stanza stanza, XmppStream stream)
        {
            if (!stream.Authenticated)
            {
                if (!(stanza is AuthIq) && (stanza is IQ && !(((IQ)stanza).Query is Register)))
                {
                    return(XmppStanzaError.ToNotAuthorized(stanza));
                }
            }

            //remove empty jids
            if (stanza.HasFrom && string.IsNullOrEmpty(stanza.From.ToString()))
            {
                stanza.From = null;
            }
            if (stanza.HasTo && string.IsNullOrEmpty(stanza.To.ToString()))
            {
                stanza.To = null;
            }

            //prep strings
            stanza.From = NodePrep(stanza.From);
            stanza.To   = NodePrep(stanza.To);

            if (!ValidateJid(stanza.From) || !ValidateJid(stanza.To))
            {
                return(XmppStanzaError.ToBadRequest(stanza));
            }

            if (stanza.HasFrom)
            {
                if (!stream.JidBinded(stanza.From))
                {
                    // return null if we have from in bind iq (for qutIM 0.3 client)
                    if (!(stanza is IQ) || ((IQ)stanza).Bind == null || ((IQ)stanza).Bind.Resource != stanza.From.Resource)
                    {
                        return(XmppStreamError.InvalidFrom);
                    }
                }
            }
            else
            {
                if (stream.MultipleResources)
                {
                    return(XmppStanzaError.ToConflict(stanza));
                }
                stanza.From = new Jid(string.Format("{0}@{1}/{2}", stream.User, stream.Domain, 0 < stream.Resources.Count ? stream.Resources[0] : null));
            }

            if (stanza is Message)
            {
                var message = (Message)stanza;
                if (message.Type == MessageType.chat && message.To == null)
                {
                    return(XmppStanzaError.ToRecipientUnavailable(stanza));
                }
            }
            return(null);
        }
Esempio n. 3
0
        public override void ElementHandle(XmppStream stream, Element element, XmppHandlerContext context)
        {
            var iq = (AuthIq)element;

            if (stream.Authenticated)
            {
                context.Sender.SendTo(stream, XmppStanzaError.ToConflict(iq));
                return;
            }

            if (iq.Type == IqType.get)
            {
                ProcessAuthIQGet(stream, iq, context);
            }
            else if (iq.Type == IqType.set)
            {
                ProcessAuthIQSet(stream, iq, context);
            }
            else
            {
                context.Sender.SendTo(stream, XmppStanzaError.ToNotAcceptable(iq));
            }
        }
        private IQ SetRegister(XmppStream stream, IQ iq, XmppHandlerContext context)
        {
            var register = (Register)iq.Query;

            iq.Type = IqType.result;

            if (register.RemoveAccount)
            {
                if (!stream.Authenticated || !iq.From.HasUser)
                {
                    context.Sender.SendToAndClose(stream, XmppStreamError.NotAuthorized);
                }

                context.UserManager.RemoveUser(iq.From);
                foreach (var s in context.SessionManager.GetBareJidSessions(iq.From))
                {
                    if (s.Stream.Id == stream.Id)
                    {
                        continue;
                    }
                    context.Sender.SendToAndClose(s.Stream, XmppStreamError.Conflict);
                }
                //TODO: remove roster subscriptions
                register.RemoveAllChildNodes();
                iq.SwitchDirection();
                return(iq);
            }

            if (string.IsNullOrEmpty(register.Username) ||
                string.IsNullOrEmpty(register.Password) ||
                Stringprep.NamePrep(register.Username) != register.Username)
            {
                var error = XmppStanzaError.ToNotAcceptable(iq);
                if (string.IsNullOrEmpty(register.Username))
                {
                    error.Error.Message = "Empty required field Username.";
                }
                else if (string.IsNullOrEmpty(register.Password))
                {
                    error.Error.Message = "Empty required field Password.";
                }
                else if (Stringprep.NamePrep(register.Username) != register.Username)
                {
                    error.Error.Message = "Invalid character.";
                }
                return(error);
            }

            var userJid = new Jid(register.Username, stream.Domain, null);

            if (context.UserManager.IsUserExists(userJid))
            {
                return(XmppStanzaError.ToConflict(iq));
            }

            var user = new User(userJid, register.Password);

            context.UserManager.SaveUser(user);

            register.RemoveAllChildNodes();
            if (stream.Authenticated)
            {
                iq.SwitchDirection();
            }
            else
            {
                iq.To = null;
            }
            iq.From = null;
            return(iq);
        }