public void TestAdd() { RosterManager rm = new RosterManager(); RosterIQ riq = new RosterIQ(doc); riq.Type = IQType.set; Roster r = riq.Instruction; Item i = r.AddItem(); i.JID = new JID("foo", "bar", null); i.Nickname = "FOO"; i.Subscription = Subscription.both; rm.AddRoster(riq); Assert.AreEqual(Subscription.both, rm["foo@bar"].Subscription); Assert.AreEqual("FOO", rm["foo@bar"].Nickname); riq = new RosterIQ(doc); riq.Type = IQType.set; r = riq.Instruction; i = r.AddItem(); i.JID = new JID("foo", "bar", null); i.Nickname = "BAR"; i.Subscription = Subscription.to; rm.AddRoster(riq); Assert.AreEqual(Subscription.to, rm["foo@bar"].Subscription); Assert.AreEqual("BAR", rm["foo@bar"].Nickname); }
/// <summary> /// Sends a presence subscription request and updates the roster /// for a new roster contact. /// </summary> /// <param name="to">The JID of the contact (required)</param> /// <param name="nickname">The nickname to show for the contact.</param> /// <param name="groups">A list of groups to put the contact in. May be null. Hint: new string[] {"foo", "bar"}</param> public void Subscribe(JID to, string nickname, string[] groups) { Debug.Assert(to != null); RosterIQ riq = new RosterIQ(Document); riq.Type = IQType.set; Roster r = riq.Instruction; Item i = r.AddItem(); i.JID = to; if (nickname != null) { i.Nickname = nickname; } if (groups != null) { foreach (string g in groups) { i.AddGroup(g); } } Write(riq); // don't care about result. we should get a iq/response and a roster push. Presence pres = new Presence(Document); pres.To = to; pres.Type = PresenceType.subscribe; Write(pres); }
[Test] public void Test_Groups() { RosterIQ riq = new RosterIQ(doc); Roster r = riq.Instruction; Item i = r.AddItem(); i.JID = new JID("*****@*****.**"); Group g = i.AddGroup("foo"); Assert.AreEqual("<iq id=\"" + riq.ID + "\" type=\"get\"><query xmlns=\"jabber:iq:roster\">" + "<item jid=\"[email protected]\"><group>foo</group></item></query></iq>", riq.ToString()); g = i.AddGroup("foo"); Assert.AreEqual("<iq id=\"" + riq.ID + "\" type=\"get\"><query xmlns=\"jabber:iq:roster\">" + "<item jid=\"[email protected]\"><group>foo</group></item></query></iq>", riq.ToString()); g = i.AddGroup("bar"); Assert.AreEqual("<iq id=\"" + riq.ID + "\" type=\"get\"><query xmlns=\"jabber:iq:roster\">" + "<item jid=\"[email protected]\"><group>foo</group><group>bar</group></item></query></iq>", riq.ToString()); Assert.AreEqual(2, i.GetGroups().Length); Assert.AreEqual("foo", i.GetGroup("foo").GroupName); Assert.AreEqual("bar", i.GetGroup("bar").GroupName); i.RemoveGroup("foo"); Assert.AreEqual(1, i.GetGroups().Length); Assert.AreEqual(null, i.GetGroup("foo")); }
[Test] public void Test_GetItems() { RosterIQ riq = new RosterIQ(doc); Roster r = riq.Instruction; Item i = r.AddItem(); i.JID = new JID("*****@*****.**"); i = r.AddItem(); i.Subscription = Subscription.from; i.JID = new JID("*****@*****.**"); i.Subscription = Subscription.both; Item[] items = r.GetItems(); Assert.AreEqual(items.Length, 2); Assert.AreEqual(items[0].JID, "*****@*****.**"); Assert.AreEqual(items[1].JID, "*****@*****.**"); }
[Test] public void Test_Item() { RosterIQ riq = new RosterIQ(doc); Roster r = riq.Instruction; Item i = r.AddItem(); i.JID = new JID("*****@*****.**"); Assert.AreEqual("<iq id=\"" + riq.ID + "\" type=\"get\"><query xmlns=\"jabber:iq:roster\">" + "<item jid=\"[email protected]\" /></query></iq>", riq.ToString()); }
[Test] public void Test_Ask() { RosterIQ riq = new RosterIQ(doc); Roster r = riq.Instruction; Item i = r.AddItem(); Assert.AreEqual("", i.GetAttribute("ask")); Assert.AreEqual(Ask.NONE, i.Ask); i.Ask = Ask.subscribe; Assert.AreEqual("subscribe", i.GetAttribute("ask")); Assert.AreEqual(Ask.subscribe, i.Ask); i.Ask = Ask.NONE; Assert.AreEqual("", i.GetAttribute("ask")); Assert.AreEqual(Ask.NONE, i.Ask); }
public void TestNumeric() { RosterManager rm = new RosterManager(); RosterIQ riq = new RosterIQ(doc); riq.Type = IQType.set; Roster r = riq.Instruction; Item i = r.AddItem(); i.JID = new JID("support", "conference.192.168.32.109", null); i.Nickname = "FOO"; i.Subscription = Subscription.both; rm.AddRoster(riq); Assert.AreEqual(Subscription.both, rm["[email protected]"].Subscription); Assert.AreEqual("FOO", rm["[email protected]"].Nickname); }
/// <summary> /// Remove a contact from the roster /// </summary> /// <param name="jid">Typically just a user@host JID</param> public void Remove(JID jid) { /* * C: <iq from='[email protected]/balcony' type='set' id='delete_1'> * <query xmlns='jabber:iq:roster'> * <item jid='*****@*****.**' subscription='remove'/> * </query> * </iq> */ RosterIQ iq = new RosterIQ(m_stream.Document); iq.Type = IQType.set; Roster r = iq.Instruction; Item item = r.AddItem(); item.JID = jid; item.Subscription = Subscription.remove; Write(iq); // ignore response }
/// <summary> /// Removes a contact from the roster. /// This will also remove the subscription for that contact being removed. /// </summary> /// <param name="to">The JID to remove</param> public void RemoveRosterItem(JID to) { Debug.Assert(to != null); /* * <iq from='[email protected]/balcony' type='set' id='roster_4'> * <query xmlns='jabber:iq:roster'> * <item jid='*****@*****.**' subscription='remove'/> * </query> * </iq> */ RosterIQ riq = new RosterIQ(Document); riq.Type = IQType.set; Roster r = riq.Instruction; Item i = r.AddItem(); i.JID = to; i.Subscription = Subscription.remove; Write(riq); // don't care about result. we should get a iq/response and a roster push. }
public void Add(JID jid) { Item item = this[jid]; // only create a new roster item if it does not already exist if (item == null) { RosterIQ iq = new RosterIQ(m_stream.Document); iq.Type = IQType.set; Roster r = iq.Instruction; item = r.AddItem(); item.JID = jid; Write(iq); } // subscribe to presence Presence sub = new Presence(m_stream.Document); sub.To = jid; sub.Type = PresenceType.subscribe; Write(sub); }