public virtual UserRosterItem SaveRosterItem(Jid rosterJid, UserRosterItem item)
        {
            if (item == null) throw new ArgumentNullException("item");

            try
            {
                lock (syncRoot)
                {
                    if (string.IsNullOrEmpty(item.Name)) item.Name = item.Jid.Bare;
                    rosterJid = new Jid(rosterJid.Bare.ToLowerInvariant());

                    ExecuteNonQuery(new SqlInsert("jabber_roster", true)
                        .InColumnValue("jid", rosterJid.ToString())
                        .InColumnValue("item_jid", item.Jid.ToString())
                        .InColumnValue("name", item.Name)
                        .InColumnValue("subscription", (Int32)item.Subscribtion)
                        .InColumnValue("ask", (Int32)item.Ask)
                        .InColumnValue("groups", string.Join(GroupSeparator, item.Groups.ToArray())));

                    if (!RosterItems.ContainsKey(rosterJid)) RosterItems[rosterJid] = new UserRosterItemDic();
                    RosterItems[rosterJid][item.Jid] = item;

                    return item;
                }
            }
            catch (Exception e)
            {
                throw new JabberException("Could not save or update roster item.", e);
            }
        }
Exemple #2
0
        private IDictionary <Jid, UserRosterItemDic> LoadRosterItems()
        {
            var items = new Dictionary <Jid, UserRosterItemDic>();

            ExecuteList(new SqlQuery("jabber_roster").Select("jid", "item_jid", "name", "subscription", "ask", "groups")).ForEach(r =>
            {
                var item = new UserRosterItem(new Jid((string)r[1]))
                {
                    Name         = r[2] as string,
                    Subscribtion = (SubscriptionType)Convert.ToInt32(r[3]),
                    Ask          = (AskType)Convert.ToInt32(r[4]),
                };
                if (r[5] != null)
                {
                    item.Groups.AddRange(((string)r[5]).Split(new[] { GroupSeparator }, StringSplitOptions.RemoveEmptyEntries));
                }

                var jid = new Jid((string)r[0]);
                if (!items.ContainsKey(jid))
                {
                    items[jid] = new UserRosterItemDic();
                }
                items[jid][item.Jid] = item;
            });

            return(items);
        }
        public override UserRosterItem SaveRosterItem(Jid rosterJid, UserRosterItem item)
        {
            if (item == null) throw new ArgumentNullException("item");

            ASCContext.SetCurrentTenant(rosterJid.Server);
            if (IsASCRosterItem(rosterJid, item.Jid)) throw new JabberException(ErrorCode.Forbidden);

            return base.SaveRosterItem(rosterJid, item);
        }
		public static UserRosterItem FromRosterItem(RosterItem ri)
		{
			var item = new UserRosterItem(ri.Jid)
			{
				Name = ri.Name,
				Ask = ri.Ask,
				Subscribtion = ri.Subscription,
			};
			foreach (Element element in ri.GetGroups())
			{
				item.Groups.Add(element.Value);
			}
			return item;
		}
Exemple #5
0
        public static UserRosterItem FromRosterItem(RosterItem ri)
        {
            var item = new UserRosterItem(ri.Jid)
            {
                Name         = ri.Name,
                Ask          = ri.Ask,
                Subscribtion = ri.Subscription,
            };

            foreach (Element element in ri.GetGroups())
            {
                item.Groups.Add(element.Value);
            }
            return(item);
        }
		public void RosterTest()
		{
			foreach (var i in store.GetRosterItems(jid)) store.RemoveRosterItem(jid, i.Jid);

			var roster = store.GetRosterItems(jid);
			CollectionAssert.IsEmpty(roster);

			var r = new UserRosterItem(new Jid("x"))
			{
				Name = "y",
				Subscribtion = SubscriptionType.from,
				Ask = AskType.subscribe,
			};
			r.Groups.AddRange(new[] { "g1", "g2" });
			store.SaveRosterItem(jid, r);
			store.SaveRosterItem(jid, r);

			roster = store.GetRosterItems(jid);
			Assert.AreEqual(1, roster.Count);
			Assert.AreEqual("x", roster[0].Jid.ToString());
			Assert.AreEqual("y", roster[0].Name);
			Assert.AreEqual(SubscriptionType.from, roster[0].Subscribtion);
			Assert.AreEqual(AskType.subscribe, roster[0].Ask);
			CollectionAssert.AreEqual(new[] { "g1", "g2" }, roster[0].Groups);

			r = store.GetRosterItem(jid, new Jid("x"));
			Assert.IsNotNull(r);

			roster = store.GetRosterItems(jid, SubscriptionType.from);
			Assert.AreEqual(1, roster.Count);
			roster = store.GetRosterItems(jid, SubscriptionType.both);
			Assert.AreEqual(0, roster.Count);

			foreach (var i in store.GetRosterItems(jid)) store.RemoveRosterItem(jid, i.Jid);

			store.SaveRosterItem(jid, new UserRosterItem(new Jid("x")));
			roster = store.GetRosterItems(jid);
			Assert.AreEqual(1, roster.Count);
			Assert.AreEqual("x", roster[0].Jid.ToString());
			Assert.AreEqual("x", roster[0].Name);
			Assert.AreEqual(default(SubscriptionType), roster[0].Subscribtion);
			Assert.AreEqual(default(AskType), roster[0].Ask);
			CollectionAssert.IsEmpty(roster[0].Groups);
		}
        public virtual UserRosterItem SaveRosterItem(Jid rosterJid, UserRosterItem item)
        {
            if (item == null)
            {
                throw new ArgumentNullException("item");
            }

            try
            {
                lock (syncRoot)
                {
                    if (string.IsNullOrEmpty(item.Name))
                    {
                        item.Name = item.Jid.Bare;
                    }
                    rosterJid = new Jid(rosterJid.Bare.ToLowerInvariant());

                    ExecuteNonQuery(new SqlInsert("jabber_roster", true)
                                    .InColumnValue("jid", rosterJid.ToString())
                                    .InColumnValue("item_jid", item.Jid.ToString())
                                    .InColumnValue("name", item.Name)
                                    .InColumnValue("subscription", (Int32)item.Subscribtion)
                                    .InColumnValue("ask", (Int32)item.Ask)
                                    .InColumnValue("groups", string.Join(GroupSeparator, item.Groups.ToArray())));

                    if (!RosterItems.ContainsKey(rosterJid))
                    {
                        RosterItems[rosterJid] = new UserRosterItemDic();
                    }
                    RosterItems[rosterJid][item.Jid] = item;

                    return(item);
                }
            }
            catch (Exception e)
            {
                throw new JabberException("Could not save or update roster item.", e);
            }
        }
		private void ChangeOutboundPresence(State state, UserRosterItem itemSendingPresence, UserRosterItem itemRecivingPresence, PresenceType newType, out bool bRoute)
		{
			//Change state of sending presence!
			bRoute = false;

			if (newType == PresenceType.subscribed)
			{
				switch (state)
				{
					case State.None_PendingIn:
						bRoute = true;
						SetState(State.From, itemSendingPresence, itemRecivingPresence);
						break;
					case State.None_PendingInOut:
						bRoute = true;
						SetState(State.From_PendingOut, itemSendingPresence, itemRecivingPresence);
						break;
					case State.To_PendingIn:
						bRoute = true;
						SetState(State.Both, itemSendingPresence, itemRecivingPresence);
						break;
				}
			}
			if (newType == PresenceType.unsubscribed)
			{
				switch (state)
				{
					case State.None_PendingIn:
						bRoute = true;
						SetState(State.None, itemSendingPresence, itemRecivingPresence);
						break;
					case State.None_PendingInOut:
						bRoute = true;
						SetState(State.None_PendingOut, itemSendingPresence, itemRecivingPresence);
						break;
					case State.To_PendingIn:
						bRoute = true;
						SetState(State.To, itemSendingPresence, itemRecivingPresence);
						break;
					case State.From:
						bRoute = true;
						SetState(State.None, itemSendingPresence, itemRecivingPresence);
						break;
					case State.From_PendingOut:
						bRoute = true;
						SetState(State.None_PendingOut, itemSendingPresence, itemRecivingPresence);
						break;
					case State.Both:
						bRoute = true;
						SetState(State.To, itemSendingPresence, itemRecivingPresence);
						break;
				}
			}
		}
		private void ChangeInboundPresence(State state, UserRosterItem itemSendingPresence, UserRosterItem itemRecivingPresence, PresenceType newType, out bool bRoute, out bool bAutoReply)
		{
			bRoute = false;
			bAutoReply = false;
			//Inbound! change state of reciving item!!!
			if (newType == PresenceType.subscribe)
			{
				switch (state)
				{
					case State.None:
						SetState(State.None_PendingIn, itemSendingPresence, itemRecivingPresence);
						bRoute = true;
						break;
					case State.None_PendingOut:
						SetState(State.None_PendingInOut, itemSendingPresence, itemRecivingPresence);
						bRoute = true;
						break;
					case State.To:
						SetState(State.To_PendingIn, itemSendingPresence, itemRecivingPresence);
						bRoute = true;
						break;
					case State.From:
					case State.From_PendingOut:
					case State.Both:
						bAutoReply = true;
						break;
				}
			}
			if (newType == PresenceType.unsubscribe)
			{
				switch (state)
				{
					case State.None_PendingIn:
						bRoute = true;
						bAutoReply = true;
						SetState(State.None, itemSendingPresence, itemRecivingPresence);
						break;
					case State.None_PendingInOut:
						bRoute = true;
						bAutoReply = true;
						SetState(State.None_PendingOut, itemSendingPresence, itemRecivingPresence);
						break;
					case State.To_PendingIn:
						bRoute = true;
						bAutoReply = true;
						SetState(State.To, itemSendingPresence, itemRecivingPresence);
						break;
					case State.From:
						bRoute = true;
						bAutoReply = true;
						SetState(State.None, itemSendingPresence, itemRecivingPresence);
						break;
					case State.From_PendingOut:
						bRoute = true;
						bAutoReply = true;
						SetState(State.None_PendingOut, itemSendingPresence, itemRecivingPresence);
						break;
					case State.Both:
						bRoute = true;
						bAutoReply = true;
						SetState(State.To, itemSendingPresence, itemRecivingPresence);
						break;
				}
			}
			if (newType == PresenceType.subscribed)
			{
				switch (state)
				{
					case State.None_PendingOut:
						bRoute = true;
						SetState(State.To, itemSendingPresence, itemRecivingPresence);
						break;
					case State.None_PendingInOut:
						bRoute = true;
						SetState(State.To_PendingIn, itemSendingPresence, itemRecivingPresence);
						break;
					case State.From_PendingOut:
						bRoute = true;
						SetState(State.Both, itemSendingPresence, itemRecivingPresence);
						break;
				}

			}
			if (newType == PresenceType.unsubscribed)
			{
				switch (state)
				{
					case State.None_PendingOut:
						bRoute = true;
						SetState(State.None, itemSendingPresence, itemRecivingPresence);
						break;
					case State.None_PendingInOut:
						bRoute = true;
						SetState(State.None_PendingIn, itemSendingPresence, itemRecivingPresence);
						break;
					case State.To:
						bRoute = true;
						SetState(State.None, itemSendingPresence, itemRecivingPresence);
						break;
					case State.To_PendingIn:
						bRoute = true;
						SetState(State.None_PendingIn, itemSendingPresence, itemRecivingPresence);
						break;
					case State.From_PendingOut:
						bRoute = true;
						SetState(State.From, itemSendingPresence, itemRecivingPresence);
						break;
					case State.Both:
						bRoute = true;
						SetState(State.From, itemSendingPresence, itemRecivingPresence);
						break;
				}
			}
		}
		private void SetState(State state, UserRosterItem itemSendingPresence, UserRosterItem itemRecivingPresence)
		{
			Debug.Assert(itemSendingPresence != null);
			if (state == State.None)
			{
				itemSendingPresence.Subscribtion = SubscriptionType.none;
				itemSendingPresence.Ask = AskType.NONE;
			}
			else if (state == State.None_PendingIn)
			{
				itemSendingPresence.Subscribtion = SubscriptionType.none;
				itemSendingPresence.Ask = AskType.subscribe;
			}
			else if (state == State.None_PendingInOut)
			{
				itemSendingPresence.Subscribtion = SubscriptionType.none;
				itemSendingPresence.Ask = AskType.subscribe;

				Debug.Assert(itemRecivingPresence != null);

				itemRecivingPresence.Ask = AskType.subscribe;
			}
			else if (state == State.From)
			{
				itemSendingPresence.Subscribtion = SubscriptionType.from;
				itemSendingPresence.Ask = AskType.NONE;
			}
			else if (state == State.From_PendingOut)
			{
				itemSendingPresence.Subscribtion = SubscriptionType.from;
				itemSendingPresence.Ask = AskType.NONE;
				Debug.Assert(itemRecivingPresence != null);

				itemRecivingPresence.Ask = AskType.subscribe;
			}
			else if (state == State.To)
			{
				itemSendingPresence.Subscribtion = SubscriptionType.to;
				itemSendingPresence.Ask = AskType.NONE;
			}
			else if (state == State.To_PendingIn)
			{
				itemSendingPresence.Subscribtion = SubscriptionType.to;
				itemSendingPresence.Ask = AskType.subscribe;
			}
			else if (state == State.Both)
			{
				itemSendingPresence.Subscribtion = SubscriptionType.both;
				itemSendingPresence.Ask = AskType.NONE;
				Debug.Assert(itemRecivingPresence != null);

				itemRecivingPresence.Subscribtion = SubscriptionType.both;
				itemRecivingPresence.Ask = AskType.NONE;
			}
		}
		private State GetState(UserRosterItem itemSendingPresence, UserRosterItem itemRecivingPresence)
		{
			var state = State.None;
			Debug.Assert(itemSendingPresence != null);
			if (itemSendingPresence.Subscribtion == SubscriptionType.none)
			{
				if (itemSendingPresence.Ask == AskType.subscribe)
				{
					state = itemRecivingPresence != null && itemRecivingPresence.Ask == AskType.subscribe ?
						State.None_PendingInOut :
						State.None_PendingIn;
				}
				else
				{
					state = itemRecivingPresence.Ask == AskType.subscribe ?
						State.None_PendingOut :
						State.None;
				}
			}
			else if (itemSendingPresence.Subscribtion == SubscriptionType.to)
			{
				state = itemSendingPresence.Ask == AskType.subscribe ?
					State.To_PendingIn :
					State.To;
			}
			else if (itemSendingPresence.Subscribtion == SubscriptionType.from)
			{
				state = itemRecivingPresence != null && itemRecivingPresence.Ask == AskType.subscribe ?
					State.From_PendingOut :
					State.From;
			}
			else if (itemSendingPresence.Subscribtion == SubscriptionType.both)
			{
				state = State.Both;
			}
			return state;
		}
		private UserRosterItem GetUserRosterItem(Jid user, Jid userInRoster)
		{
			var rosterItem = storages.RosterStorage.GetRosterItem(user, userInRoster);
			if (rosterItem == null)
			{
				rosterItem = new UserRosterItem(userInRoster);
				rosterItem.Subscribtion = SubscriptionType.none;
				rosterItem.Ask = AskType.NONE;
			}
			return rosterItem;
		}
		private void UpdateItems(Presence presence, ref UserRosterItem fromRosterItem, ref UserRosterItem toRosterItem)
		{
			try
			{
				storages.RosterStorage.SaveRosterItem(presence.From, toRosterItem);
				storages.RosterStorage.SaveRosterItem(presence.To, fromRosterItem);
			}
			catch (JabberException)
			{
				//Load back
				toRosterItem = GetUserRosterItem(presence.From, presence.To);
				fromRosterItem = GetUserRosterItem(presence.To, presence.From);
			}
		}
        private IDictionary<Jid, UserRosterItemDic> LoadRosterItems()
        {
            var items = new SynchronizedDictionary<Jid, UserRosterItemDic>();

            ExecuteList(new SqlQuery("jabber_roster").Select("jid", "item_jid", "name", "subscription", "ask", "groups"))
                .ForEach(r =>
                {
                    var item = new UserRosterItem(new Jid((string)r[1]))
                    {
                        Name = r[2] as string,
                        Subscribtion = (SubscriptionType)Convert.ToInt32(r[3]),
                        Ask = (AskType)Convert.ToInt32(r[4]),
                    };
                    if (r[5] != null) item.Groups.AddRange(((string)r[5]).Split(new[] { GroupSeparator }, StringSplitOptions.RemoveEmptyEntries));

                    var jid = new Jid((string)r[0]);
                    if (!items.ContainsKey(jid)) items[jid] = new UserRosterItemDic();
                    items[jid][item.Jid] = item;
                });

            return items;
        }
 private UserRosterItem ToUserRosterItem(UserInfo u, string server)
 {
     var item = new UserRosterItem(new Jid(u.UserName + "@" + server))
     {
         Name = UserFormatter.GetUserName(u),
         Subscribtion = SubscriptionType.both,
         Ask = AskType.NONE,
     };
     foreach (var g in ASCContext.UserManager.GetUserGroups(u.ID))
     {
         item.Groups.Add(g.Name);
     }
     return item;
 }