Ejemplo n.º 1
0
		public override void HandleMessage(XmppStream stream, Message message, XmppHandlerContext context)
		{
			if (!message.HasTo || message.To.IsServer)
			{
				context.Sender.SendTo(stream, XmppStanzaError.ToServiceUnavailable(message));
				return;
			}

			var sessions = context.SessionManager.GetBareJidSessions(message.To);
            if (0 < sessions.Count)
            {
                foreach(var s in sessions)
                {
                    try
                    {
                        context.Sender.SendTo(s, message);
                    }
                    catch
                    {
                        context.Sender.SendToAndClose(s.Stream, message);
                    }
                }
            }
            else
            {
                StoreOffline(message, context.StorageManager.OfflineStorage);
            }
		}
        public override void HandleMessage(XmppStream stream, Message message, XmppHandlerContext context)
        {
            if (!message.HasTo) return;

            if (archiveStore.GetMessageLogging(message.From, message.To))
            {
                if (!string.IsNullOrEmpty(message.Body) ||
                    !string.IsNullOrEmpty(message.Subject) ||
                    !string.IsNullOrEmpty(message.Thread) ||
                    message.Html != null)
                {
                    var flush = false;
                    lock (messageBuffer)
                    {
                        //Add xdelay
                        if (message.XDelay == null)
                        {
                            message.XDelay = new Delay();
                            message.XDelay.Stamp = DateTime.UtcNow;
                        }
                        messageBuffer.Add(message);

                        flush = BUFFER_SIZE <= messageBuffer.Count;
                    }
                    if (flush) FlushMessageBuffer();
                }
            }
        }
		public void StreamStartHandle(XmppStream xmppStream, Stream stream, XmppHandlerContext context)
		{
			var streamHeader = new StringBuilder();
			streamHeader.AppendLine("<?xml version='1.0' encoding='UTF-8'?>");
			streamHeader.AppendFormat("<stream:{0} xmlns:{0}='{1}' xmlns='{2}' from='{3}' id='{4}' version='1.0'>",
				Uri.PREFIX, Uri.STREAM, Uri.CLIENT, stream.To, xmppStream.Id);
			context.Sender.SendTo(xmppStream, streamHeader.ToString());

			var features = new Features();
			features.Prefix = Uri.PREFIX;
			if (xmppStream.Authenticated)
			{
				features.AddChild(new Bind());
				features.AddChild(new Core.protocol.iq.session.Session());
			}
			else
			{
				features.Mechanisms = new Mechanisms();
                var listener = (BoshXmppListener)(context.XmppGateway.GetXmppListener("Bosh Listener"));
                var storage = new DbLdapSettingsStore();
                storage.GetLdapSettings(xmppStream.Domain);
                if (!storage.EnableLdapAuthentication || listener.GetXmppConnection(xmppStream.ConnectionId) != null)
                {
                    features.Mechanisms.AddChild(new Mechanism(MechanismType.DIGEST_MD5));
                }
                else
                {
                    features.Mechanisms.AddChild(new Mechanism(MechanismType.PLAIN));
                }
				features.Mechanisms.AddChild(new Element("required"));
				features.Register = new Register();
			}
			streamHeader.Append(features.ToString());
			context.Sender.SendTo(xmppStream, features);
		}
Ejemplo n.º 4
0
		private IQ GetPrivate(XmppStream stream, IQ iq, XmppHandlerContext context)
		{
			var privateStore = (Private)iq.Query;
			
            if (!privateStore.HasChildElements) return XmppStanzaError.ToNotAcceptable(iq);

			var retrived = new List<Element>();
			foreach (var childNode in privateStore.ChildNodes)
			{
				if (childNode is Element)
				{
					var elementToRetrive = (Element)childNode;
                    var elementRestored = context.StorageManager.PrivateStorage.GetPrivate(iq.From, elementToRetrive);
					retrived.Add(elementRestored ?? elementToRetrive);
				}
			}

            privateStore.RemoveAllChildNodes();
			foreach (var element in retrived)
			{
				privateStore.AddChild(element);
			}
			
            iq.SwitchDirection();
			iq.Type = IqType.result;
			return iq;
		}
Ejemplo n.º 5
0
 public override void StreamEndHandle(XmppStream stream, ICollection<Node> notSendedBuffer, XmppHandlerContext context)
 {
     lock (authData)
     {
         authData.Remove(stream.Id);
     }
 }
Ejemplo n.º 6
0
        private void ProcessAuth(XmppStream stream, Auth auth, XmppHandlerContext context)
        {
            AuthData authStep;
            lock (authData)
            {
                authData.TryGetValue(stream.Id, out authStep);
            }

            if (auth.MechanismType != MechanismType.DIGEST_MD5)
            {
                context.Sender.SendToAndClose(stream, XmppFailureError.InvalidMechanism);
            }
            else if (authStep != null)
            {
                context.Sender.SendToAndClose(stream, XmppFailureError.TemporaryAuthFailure);
            }
            else
            {
                lock (authData)
                {
                    authData[stream.Id] = new AuthData();
                }
                var challenge = GetChallenge(stream.Domain);
                context.Sender.SendTo(stream, challenge);
            }
        }
Ejemplo n.º 7
0
 public override void HandleMessage(XmppStream stream, Message msg, XmppHandlerContext context)
 {
     msg.SwitchDirection();
     msg.Type = MessageType.error;
     msg.Error = new Error(ErrorType.cancel, ErrorCondition.ItemNotFound);
     context.Sender.SendTo(stream, msg);
 }
        public override void HandleMessage(XmppStream stream, Message message, XmppHandlerContext context)
        {
            if (!message.HasTo) return;
            if (archiveStore == null)
            {
                archiveStore = new DbMessageArchive();
                var properties = new Dictionary<string, string>(1);
                properties.Add("connectionStringName", "core");
                archiveStore.Configure(properties);
            }
            if (archiveStore.GetMessageLogging(message.From, message.To))
            {
                if (!string.IsNullOrEmpty(message.Body) ||
                    !string.IsNullOrEmpty(message.Subject) ||
                    !string.IsNullOrEmpty(message.Thread) ||
                    message.Html != null)
                {
                    var flush = false;
                    lock (messageBuffer)
                    {
                        //Add xdelay
                        if (message.XDelay == null)
                        {
                            message.XDelay = new Delay();
                            message.XDelay.Stamp = DateTime.UtcNow;
                        }
                        messageBuffer.Add(message);

                        flush = BUFFER_SIZE <= messageBuffer.Count;
                    }
                    if (flush) FlushMessageBuffer();
                }
            }
        }
Ejemplo n.º 9
0
		public override IQ HandleIQ(XmppStream stream, IQ iq, XmppHandlerContext context)
		{
			if (iq.HasTo && iq.To != Jid) return XmppStanzaError.ToServiceUnavailable(iq);
			if (iq.Query is DiscoInfo && iq.Type == IqType.get) return GetDiscoInfo(stream, iq, context);
			if (iq.Query is DiscoItems && iq.Type == IqType.get) return GetDiscoItems(stream, iq, context);
			return XmppStanzaError.ToServiceUnavailable(iq);
		}
		public void StreamStartHandle(XmppStream xmppStream, Stream stream, XmppHandlerContext context)
		{
			var streamHeader = new StringBuilder();
			streamHeader.AppendLine("<?xml version='1.0' encoding='UTF-8'?>");
			streamHeader.AppendFormat("<stream:{0} xmlns:{0}='{1}' xmlns='{2}' from='{3}' id='{4}' version='1.0'>",
				Uri.PREFIX, Uri.STREAM, Uri.CLIENT, stream.To, xmppStream.Id);
			context.Sender.SendTo(xmppStream, streamHeader.ToString());

			var features = new Features();
			features.Prefix = Uri.PREFIX;
			if (xmppStream.Authenticated)
			{
				features.AddChild(new Bind());
				features.AddChild(new Core.protocol.iq.session.Session());
			}
			else
			{
				features.Mechanisms = new Mechanisms();
				features.Mechanisms.AddChild(new Mechanism(MechanismType.DIGEST_MD5));
				features.Mechanisms.AddChild(new Element("required"));
				features.Register = new Register();
			}
			streamHeader.Append(features.ToString());
			context.Sender.SendTo(xmppStream, features);
		}
        public override void HandleMessage(XmppStream stream, Message message, XmppHandlerContext context)
        {
            if (!message.HasTo || !message.To.HasResource) return;

            string[] commands = message.To.Resource.Split('/');
            if (commands.Length == 1 && commands[0] == ANNOUNCE)
            {
                Announce(stream, message, context);
            }
            else if (commands.Length == 2 && commands[1] == ONLINE)
            {
                AnnounceOnline(stream, message, context);
            }
            else if (commands.Length == 2 && commands[1] == ONLINEBROADCAST)
            {
                AnnounceOnlineBroadcast(stream, message, context);
            }
            else if (commands.Length == 2 && commands[1] == SERVICE)
            {
                AnnounceService(stream, message, context);
            }
            else
            {
                context.Sender.SendTo(stream, XmppStanzaError.ToServiceUnavailable(message));
            }
        }
Ejemplo n.º 12
0
		public override void ElementHandle(XmppStream stream, Element element, XmppHandlerContext context)
		{
			context.Sender.SendTo(stream, new Compressed());
			var connection = context.Sender.GetXmppConnection(stream.ConnectionId);
			connection.SetStreamTransformer(new GZipTransformer());
			context.Sender.ResetStream(stream);
		}
Ejemplo n.º 13
0
		public void StreamStartHandle(XmppStream xmppStream, Stream stream, XmppHandlerContext context)
		{
			lock (this)
			{
				//Check tennats here
				if (ValidateHost(stream.To))
				{
					//Create new services
					foreach (var template in templates)
					{
						var service = (IXmppService)Activator.CreateInstance(template.Value);
						service.Jid = new Jid(Stringprep.NamePrep(string.Format("{0}.{1}", template.Key, stream.To.Server).Trim('.')));

						if (serviceManager.GetService(service.Jid) != null)
						{
							continue;
						}

						service.Name = service.Jid.ToString();
						if (!string.IsNullOrEmpty(template.Key))
						{
							service.ParentService = serviceManager.GetService(new Jid(Stringprep.NamePrep(stream.To.Server)));
						}
						service.Configure(new Dictionary<string, string>());
						serviceManager.RegisterService(service);
					}
					//Reroute
					handlerManager.ProcessStreamStart(stream, Uri.CLIENT, xmppStream);
				}
				else
				{
					context.Sender.SendToAndClose(xmppStream, XmppStreamError.HostUnknown);
				}
			}
		}
Ejemplo n.º 14
0
		private void ProcessAuthIQSet(XmppStream stream, AuthIq iq, XmppHandlerContext context)
		{
			if (string.IsNullOrEmpty(iq.Query.Username) || string.IsNullOrEmpty(iq.Query.Resource))
			{
				context.Sender.SendTo(stream, XmppStanzaError.ToNotAcceptable(iq));
				return;
			}

			bool authorized = false;
			if (!string.IsNullOrEmpty(iq.Query.Digest))
			{
				authorized = AuthDigest(iq.Query.Username, iq.Query.Digest, stream, context.UserManager);
			}
			if (!string.IsNullOrEmpty(iq.Query.Password))
			{
				authorized = AuthPlain(iq.Query.Username, iq.Query.Password, stream, context.UserManager);
			}
			if (authorized)
			{
				stream.Authenticate(iq.Query.Username);

				var answer = new IQ(IqType.result);
				answer.Id = iq.Id;
				answer.To = iq.From;
				answer.From = iq.To;
				context.Sender.SendTo(stream, answer);
			}
			else
			{
				context.Sender.SendTo(stream, XmppStanzaError.ToNotAuthorized(iq));
			}
		}
Ejemplo n.º 15
0
		public override IQ HandleIQ(XmppStream stream, IQ iq, XmppHandlerContext context)
		{
			if (!iq.HasTo) iq.To = iq.From;
			if (iq.Type == IqType.get) return GetVCardSearch(stream, iq, context);
			else if (iq.Type == IqType.set) return SetVCardSearch(stream, iq, context);
			else return XmppStanzaError.ToBadRequest(iq);
		}
Ejemplo n.º 16
0
        public override void ElementHandle(XmppStream stream, Element element, XmppHandlerContext context)
        {
            if (stream.Authenticated) return;

            if (element is Auth) ProcessAuth(stream, (Auth)element, context);
            if (element is Response) ProcessResponse(stream, (Response)element, context);
            if (element is Abort) ProcessAbort(stream, (Abort)element, context);
        }
Ejemplo n.º 17
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;
		}
 public override IQ HandleIQ(XmppStream stream, IQ iq, XmppHandlerContext context)
 {
     if (iq.Query is PrivateLog && iq.Type == IqType.get) return GetPrivateLog(stream, iq, context);
     if (iq.Query is PrivateLog && iq.Type == IqType.set) return SetPrivateLog(stream, iq, context);
     if (iq.Query is PrivateLog && (iq.Type == IqType.result || iq.Type == IqType.error)) return null;
     if (iq.Query is History && iq.Type == IqType.get) return GetHistory(stream, iq, context);
     return XmppStanzaError.ToServiceUnavailable(iq);
 }
Ejemplo n.º 19
0
		public override IQ HandleIQ(XmppStream stream, IQ iq, XmppHandlerContext context)
		{
			if (iq.HasTo && iq.To != iq.From) return XmppStanzaError.ToForbidden(iq);

			if (iq.Type == IqType.get) return GetRoster(stream, iq, context);
			else if (iq.Type == IqType.set) return SetRoster(stream, iq, context);
			else return null;
		}
Ejemplo n.º 20
0
		public override IQ HandleIQ(XmppStream stream, IQ iq, XmppHandlerContext context)
		{
			if (iq.To != null && iq.From != iq.To) return XmppStanzaError.ToForbidden(iq);

			if (iq.Type == IqType.get) return GetPrivate(stream, iq, context);
			else if (iq.Type == IqType.set) return SetPrivate(stream, iq, context);
			else return XmppStanzaError.ToBadRequest(iq);
		}
Ejemplo n.º 21
0
		private void ProcessAuthIQGet(XmppStream stream, AuthIq iq, XmppHandlerContext context)
		{
			iq.SwitchDirection();
			iq.Type = IqType.result;
			iq.Query.AddChild(new Element("password"));
			iq.Query.AddChild(new Element("digest"));
			iq.Query.AddChild(new Element("resource"));
			context.Sender.SendTo(stream, iq);
		}
		public void StreamStartHandle(XmppStream xmppStream, Stream stream, XmppHandlerContext context)
		{
			var streamHeader = new StringBuilder();
			streamHeader.AppendLine("<?xml version='1.0' encoding='UTF-8'?>");
			streamHeader.AppendFormat("<stream:{0} xmlns:{0}='{1}' xmlns='{2}' ", Uri.PREFIX, Uri.STREAM, Uri.ACCEPT);
			streamHeader.AppendFormat("from='{0}' id='{1}' version='1.0'>", stream.To, xmppStream.Id);

			context.Sender.SendTo(xmppStream, streamHeader.ToString());
		}
Ejemplo n.º 23
0
		private IQ ProcessSession(XmppStream stream, IQ iq, XmppHandlerContext context)
		{
			var session = context.SessionManager.GetSession(iq.From);
			if (session == null) return XmppStanzaError.ToItemNotFound(iq);

			var answer = new IQ(IqType.result);
			answer.Id = iq.Id;
			answer.Session = new Core.protocol.iq.session.Session();
			session.Active = true;
			return answer;
		}
Ejemplo n.º 24
0
		private IQ SetVCard(XmppStream stream, IQ iq, XmppHandlerContext context)
		{
			if (iq.From != iq.To) return XmppStanzaError.ToForbidden(iq);

			var answer = new IQ(IqType.result);
			answer.Id = iq.Id;
			answer.To = iq.From;
			answer.From = iq.To;
			context.StorageManager.VCardStorage.SetVCard(iq.To, iq.Vcard);
			answer.Vcard = iq.Vcard;
			return answer;
		}
Ejemplo n.º 25
0
        private IQ GetPrivateLog(XmppStream stream, IQ iq, XmppHandlerContext context)
        {
            if (!iq.HasTo) return XmppStanzaError.ToBadRequest(iq);

            var privateLog = (PrivateLog)iq.Query;
            privateLog.RemoveAllChildNodes();
            var logging = archiveStore.GetMessageLogging(iq.From, iq.To);
            privateLog.AddChild(new PrivateLogItem() { Jid = iq.To, Log = logging });

            iq.SwitchDirection();
            iq.Type = IqType.result;
            return iq;
        }
Ejemplo n.º 26
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));
		}
Ejemplo n.º 27
0
		private IQ GetVCard(XmppStream stream, IQ iq, XmppHandlerContext context)
		{
			var answer = new IQ(IqType.result);
			answer.Id = iq.Id;
			answer.To = iq.From;
			answer.From = iq.To;

			answer.Vcard = iq.To.HasUser ?
				context.StorageManager.VCardStorage.GetVCard(iq.To) :
				GetServiceVcard(iq.To, context);

			if (answer.Vcard == null) return XmppStanzaError.ToNotFound(iq);
			return answer;
		}
Ejemplo n.º 28
0
		protected virtual IQ GetDiscoInfo(XmppStream stream, IQ iq, XmppHandlerContext context)
		{
			if (((DiscoInfo)iq.Query).Node != null) return XmppStanzaError.ToServiceUnavailable(iq);

			var service = ServiceManager.GetService(Jid);
			if (service == null) return XmppStanzaError.ToItemNotFound(iq);

			var answer = new IQ(IqType.result);
			answer.Id = iq.Id;
			answer.From = Jid;
			answer.To = iq.From;
			answer.Query = service.DiscoInfo;
			return answer;
		}
Ejemplo n.º 29
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);
					}
					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;
		}
Ejemplo n.º 30
0
		private IQ GetVCardSearch(XmppStream stream, IQ iq, XmppHandlerContext context)
		{
			var answer = new IQ(IqType.result);
			answer.Id = iq.Id;
			answer.To = iq.From;
			answer.From = iq.To;

			var search = new Search();
			search.Nickname = string.Empty;
			search.Firstname = string.Empty;
			search.Lastname = string.Empty;

			answer.Query = search;
			return answer;
		}
Ejemplo n.º 31
0
 public virtual IQ HandleIQ(XmppStream stream, IQ iq, XmppHandlerContext context)
 {
     return(null);
 }
Ejemplo n.º 32
0
 public virtual void HandleMessage(XmppStream stream, Message message, XmppHandlerContext context)
 {
 }
Ejemplo n.º 33
0
 public virtual void HandlePresence(XmppStream stream, Presence presence, XmppHandlerContext context)
 {
 }
Ejemplo n.º 34
0
 public virtual void ElementHandle(XmppStream stream, Element element, XmppHandlerContext context)
 {
 }
Ejemplo n.º 35
0
 public virtual void StreamEndHandle(XmppStream stream, ICollection <Node> notSendedBuffer, XmppHandlerContext context)
 {
 }