Ejemplo n.º 1
0
		/**
     * Constructs a new Packet. The JID address contained in the XML Element may not be
     * validated. When validation can be skipped then stringprep operations will not be performed
     * on the JIDs to verify that addresses are well-formed. However, when validation cannot be
     * skipped then <tt>only</tt> the TO address will be verified. The FROM address is assigned by
     * the server so there is no need to verify it. 
     *
     * @param element the XML Element that contains the packet contents.
     * @param skipValidation true if stringprep should not be applied to the TO address.
     */
		public Packet(XElement element, bool skipValidation) {
			this.element = element;
			// Apply stringprep profiles to the "to" and "from" values.
			string to = element.Attribute("to").Value;
			if (to != null) {
				if (to.Length == 0) {
					// Remove empty TO values
					element.SetAttributeValue("to", null);
				}
				else {
					string[] parts = JID.getParts(to);
					toJID = new JID(parts[0], parts[1], parts[2], skipValidation);
					element.SetAttributeValue("to", toJID.ToString());
				}
			}
			string from = element.Attribute("from").Value;
			if (from != null) {
				if (from.Length == 0) {
					// Remove empty FROM values
					element.SetAttributeValue("from", null);
				}
				else {
					string[] parts = JID.getParts(from);
					fromJID = new JID(parts[0], parts[1], parts[2], true);
					element.SetAttributeValue("from", fromJID.ToString());
				}
			}
		}
Ejemplo n.º 2
0
		/**
     * Creates a new DestroyRoom with the reason for the destruction and an alternate room JID.
     *
     * @param alternateJID JID of the alternate room or <tt>null</tt> if none.
     * @param reason       reason for the destruction or <tt>null</tt> if none.
     */
		public DestroyRoom(JID alternateJID, String reason)
        {
			super();
			setType(Type.set);
			Element query = setChildElement("query", "http://jabber.org/protocol/muc#owner");
			Element destroy = query.addElement("destroy");
			if (alternateJID != null) {
				destroy.addAttribute("jid", alternateJID.toString());
			}
			if (reason != null) {
				destroy.addElement("reason").setText(reason);
			}
		}
Ejemplo n.º 3
0
			/**
         * Constructs a new roster item.
         *
         * @param jid the JID.
         * @param name the nickname.
         * @param ask the ask state.
         * @param subscription the subscription state.
         * @param groups the item groups.
         */
			private Item(JID jid, String name, Ask ask, Subscription subscription,
			         Collection<String> groups) {
				this.jid = jid;
				this.name = name;
				this.ask = ask;
				this.subscription = subscription;
				this.groups = groups;
			}
Ejemplo n.º 4
0
		/**
     * Removes an item from this roster.
     *
     * @param jid the JID of the item to remove.
     */
		public void removeItem(JID jid) {
            XElement query = element.Element(XName.Get("query", "jabber:iq:roster"));
			if (query != null) {
				for (Iterator<Element> i=query.elementIterator("item"); i.hasNext(); ) {
					XElement item = i.next();
					if (item.attributeValue("jid").equals(jid.toString())) {
						query.remove(item);
						return;
					}
				}
			}
		}
Ejemplo n.º 5
0
		/**
     * Adds a new item to the roster. If the roster packet already contains an item
     * using the same JID, the information in the existing item will be overwritten
     * with the new information.<p>
     *
     * The XMPP specification recommends that if the roster item is associated with another
     * instant messaging user (human), that the JID be in bare form (e.g. user@domain).
     * Use the {@link JID#toBareJID() toBareJID()} method for a bare JID.
     *
     * @param jid the JID.
     * @param name the nickname.
     * @param ask the ask type.
     * @param subscription the subscription type.
     * @param groups a Collection of groups.
     * @return the newly created item.
     */
		public Item addItem(JID jid, String name, Ask ask, Subscription subscription,
		                Collection<String> groups)
		{
			if (jid == null) {
                throw new ArgumentException("JID cannot be null");
			}
			if (subscription == null) {
                throw new ArgumentException("Subscription cannot be null");
			}
            XElement query = element.Element(XName.Get("query", "jabber:iq:roster"));
			if (query == null)
			{
			    query = new XElement(XName.Get("query", "jabber:iq:roster");
				element.Add(query);
			}
			XElement item = null;
			for (Iterator<Element> i=query.elementIterator("item"); i.hasNext(); ) {
				XElement el = i.next();
				if (el.attributeValue("jid").equals(jid.toString())) {
					item = el;
				}
			}
			if (item == null)
			{
			    item = new XElement("item");
				query.Add(item);
			}
			item.Add(new XAttribute("jid", jid.toBareJID()));
			item.Add(new XAttribute("name", name));
			if (ask != null) {
				item.Add(new XAttribute("ask", ask.ToString()));
			}
			item.Add(new XAttribute("subscription", subscription.ToString()));
			// Erase existing groups in case the item previously existed.
			for (Iterator<XElement> i=item.elementIterator("group"); i.hasNext(); ) {
				item.remove(i.next());
			}
			// Add in groups.
			if (groups != null) {
				foreach (string group in groups) {
					item.Add(new XElement("group", group));
				}
			}
			return new Item(jid, name, ask, subscription, groups);
		}
Ejemplo n.º 6
0
		/**
     * Adds a new item to the roster. The name and groups are set to <tt>null</tt>
     * If the roster packet already contains an item using the same JID, the
     * information in the existing item will be overwritten with the new information.<p>
     *
     * The XMPP specification recommends that if the roster item is associated with another
     * instant messaging user (human), that the JID be in bare form (e.g. user@domain).
     * Use the {@link JID#toBareJID() toBareJID()} method for a bare JID.
     *
     * @param jid the JID.
     * @param subscription the subscription type.
     * @return the newly created item.
     */
		public Item addItem(JID jid, Subscription subscription)  {
			if (getType() != IQ.Type.result && getType() != IQ.Type.set) {
                throw new ArgumentException("IQ type must be 'result' or 'set'");
			}
			if (jid == null) {
                throw new ArgumentException("JID cannot be null");
			}
			return addItem(jid, null, null, subscription, null);
		}
Ejemplo n.º 7
0
		public int compareTo(JID jid) {
			// Comparison order is domain, node, resource.
			int compare = domain.CompareTo(jid.domain);
			if (compare == 0) {
				String myNode = node != null ? node : "";
				String hisNode = jid.node != null ? jid.node : "";
				compare = myNode.CompareTo(hisNode);
			}
			if (compare == 0) {
				String myResource = resource != null ? resource : "";
				String hisResource = jid.resource != null ? jid.resource : "";
				compare = myResource.CompareTo(hisResource);
			}
			return compare;
		}
			assertEquals(JID.escapeNode("space cadet"), "space\\20cadet");
Ejemplo n.º 9
0
		/**
     * Sets the XMPP address (JID) that the packet comes from. The XMPP protocol
     * often makes the "from" attribute optional, so it does not always need to be set.
     *
     * @param from the XMPP address (JID) that the packet comes from.
     */
		public void setFrom(JID from) {
			fromJID = from;
			if (from == null) {
				element.Add(new XAttribute("from", null));
			}
			else {
				element.Add(new XAttribute("from", from.ToString()));
			}
		}
Ejemplo n.º 10
0
		/**
     * Sets the XMPP address (JID) that the packet comes from. The XMPP protocol
     * often makes the "from" attribute optional, so it does not always need to be set.
     *
     * @param from the XMPP address (JID) that the packet comes from.
     */
		public void setFrom(string from) {
			// Apply stringprep profiles to value.
			if (from != null) {
				fromJID = new JID(from);
				from = fromJID.ToString();
			} else {
				fromJID = null;
			}
			element.Add(new XAttribute("from", from));
		}
Ejemplo n.º 11
0
		/**
     * Returns the XMPP address (JID) that the packet is from, or <tt>null</tt>
     * if the "from" attribute is not set. The XMPP protocol often makes the "from"
     * attribute optional, so it does not always need to be set.
     *
     * @return the XMPP address that the packet is from, or <tt>null</tt>
     *      if not set.
     */
		public JID getFrom() {
			String from = element.Attribute("from").Value;
			if (from == null || from.Length == 0) {
				return null;
			}
			else {
				if (fromJID != null && from.Equals(fromJID.ToString())) {
					return fromJID;
				}
				else {
					// Return a new JID that bypasses stringprep profile checking.
					// This improves speed and is safe as long as the user doesn't
					// directly manipulate the attributes of the underlying Element
					// that represent JID's.
					String[] parts = JID.getParts(from);
					fromJID = new JID(parts[0], parts[1], parts[2], true);
					return fromJID;
				}
			}
		}
Ejemplo n.º 12
0
		/**
     * Sets the XMPP address (JID) that the packet is address to. The XMPP protocol
     * often makes the "to" attribute optional, so it does not always need to be set.
     *
     * @param to the XMPP address (JID) that the packet is addressed to.
     */
		public void setTo(JID to) {
			toJID = to;
			if (to == null) {
                element.Add(new XAttribute("to", null));
			}
			else {
				element.Add(new XAttribute("to", to.ToString()));
			}
		}
Ejemplo n.º 13
0
		/**
     * Sets the XMPP address (JID) that the packet is addressed to. The XMPP protocol
     * often makes the "to" attribute optional, so it does not always need to be set.
     *
     * @param to the XMPP address (JID) that the packet is addressed to.
     */
		public void setTo(String to) {
			// Apply stringprep profiles to value.
			if (to !=  null) {
				toJID = new JID(to);
				to = toJID.ToString();
			} else {
				toJID = null;
			}
			element.Add(new XAttribute("to", to));
		}