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);
        }
Пример #2
0
        private IQ GetUserDisco(XmppStream stream, IQ iq, XmppHandlerContext context)
        {
            if (iq.To.HasResource)
            {
                var session = context.SessionManager.GetSession(iq.To);

                if (session != null && iq.Query is DiscoInfo)
                {
                    var discoInfo = session.ClientInfo.GetDiscoInfo(((DiscoInfo)iq.Query).Node);
                    if (discoInfo != null)
                    {
                        iq.Query = discoInfo;
                        return(ToResult(iq));
                    }
                }

                if (session == null)
                {
                    return(XmppStanzaError.ToRecipientUnavailable(iq));
                }
                context.Sender.SendTo(session, iq);
                IdleWatcher.StartWatch(iq.Id, TimeSpan.FromSeconds(4.5f), IQLost, iq);
            }
            else
            {
                if (iq.Query is DiscoInfo && context.UserManager.IsUserExists(iq.To))
                {
                    ((DiscoInfo)iq.Query).AddIdentity(new DiscoIdentity("registered", "account"));
                    return(ToResult(iq));
                }
                else if (iq.Query is DiscoItems)
                {
                    foreach (var s in context.SessionManager.GetBareJidSessions(iq.To))
                    {
                        ((DiscoItems)iq.Query).AddDiscoItem(new DiscoItem()
                        {
                            Jid = s.Jid
                        });
                    }
                    return(ToResult(iq));
                }
                return(XmppStanzaError.ToServiceUnavailable(iq));
            }
            return(null);
        }
Пример #3
0
        public override IQ HandleIQ(XmppStream stream, IQ iq, XmppHandlerContext context)
        {
            if (iq.Type != IqType.get || !iq.HasTo)
            {
                return(XmppStanzaError.ToNotAcceptable(iq));
            }

            var currSession = context.SessionManager.GetSession(iq.From);

            if (currSession == null || !currSession.Available)
            {
                return(XmppStanzaError.ToForbidden(iq));
            }

            double seconds = 0;//available

            if (iq.To.IsServer)
            {
                seconds = (DateTime.UtcNow - startedTime).TotalSeconds;
            }
            else
            {
                var session = context.SessionManager.GetSession(iq.To);
                if (session == null || !session.Available)
                {
                    var lastActivity = context.StorageManager.OfflineStorage.GetLastActivity(iq.To);
                    if (lastActivity != null)
                    {
                        seconds        = (DateTime.UtcNow - lastActivity.LogoutDateTime).TotalSeconds;
                        iq.Query.Value = lastActivity.Status;
                    }
                    else
                    {
                        return(XmppStanzaError.ToRecipientUnavailable(iq));
                    }
                }
            }

            ((Last)(iq.Query)).Seconds = (int)seconds;
            iq.Type = IqType.result;
            iq.SwitchDirection();
            return(iq);
        }
Пример #4
0
        public override IQ HandleIQ(XmppStream stream, IQ iq, XmppHandlerContext context)
        {
            if (!iq.HasTo || !iq.To.HasUser)
            {
                return(XmppStanzaError.ToServiceUnavailable(iq));
            }

            var session = context.SessionManager.GetSession(iq.To);

            if (session != null)
            {
                context.Sender.SendTo(session, iq);
                return(null);
            }
            else
            {
                return(XmppStanzaError.ToRecipientUnavailable(iq));
            }
        }
Пример #5
0
        public override IQ HandleIQ(XmppStream stream, IQ iq, XmppHandlerContext context)
        {
            var answer = new IQ(IqType.result)
            {
                Id   = iq.Id,
                To   = iq.From,
                From = iq.To,
            };

            //iq sended to server
            if (iq.Type == IqType.get && (!iq.HasTo || iq.To.IsServer || iq.To == iq.From))
            {
                if (iq.Query is Version)
                {
                    answer.Query = new Version()
                    {
                        Name = "TeamLab Jabber Server",
                        Os   = System.Environment.OSVersion.ToString(),
                        Ver  = "1.0",
                    };
                    return(answer);
                }
                else if (iq.Query is Ping)
                {
                    return(answer);
                }
                return(XmppStanzaError.ToServiceUnavailable(iq));
            }

            if (iq.Type == IqType.get && iq.HasTo)
            {
                //resend iq
                var sessionTo   = context.SessionManager.GetSession(iq.To);
                var sessionFrom = context.SessionManager.GetSession(iq.From);
                if (sessionTo != null && sessionFrom != null)
                {
                    if (string.IsNullOrEmpty(iq.Id))
                    {
                        iq.Id = System.Guid.NewGuid().ToString("N");
                    }

                    IdleWatcher.StartWatch(
                        iq.Id + iq.From,
                        System.TimeSpan.FromSeconds(3),
                        (s, e) => { context.Sender.SendTo(sessionFrom, XmppStanzaError.ToServiceUnavailable(iq)); });
                    context.Sender.SendTo(sessionTo, iq);
                }
                else
                {
                    return(XmppStanzaError.ToRecipientUnavailable(iq));
                }
            }
            if (iq.Type == IqType.error || iq.Type == IqType.result)
            {
                if (!iq.HasTo)
                {
                    return(XmppStanzaError.ToBadRequest(iq));
                }

                IdleWatcher.StopWatch(iq.Id + iq.To);
                var session = context.SessionManager.GetSession(iq.To);
                if (session != null)
                {
                    context.Sender.SendTo(session, iq);
                }
            }
            return(null);
        }