Skip to main content
summaryrefslogtreecommitdiffstats
blob: f1b62ca7a304646e7f8f36a40914c1541bc99bca (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
/****************************************************************************
 * Copyright (c) 2004 Composent, Inc. and others.
 * All rights reserved. This program and the accompanying materials
 * are made available under the terms of the Eclipse Public License v1.0
 * which accompanies this distribution, and is available at
 * http://www.eclipse.org/legal/epl-v10.html
 *
 * Contributors:
 *    Composent, Inc. - initial API and implementation
 *****************************************************************************/
package org.eclipse.ecf.provider.xmpp.identity;

import java.net.URISyntaxException;

import org.eclipse.ecf.core.identity.BaseID;
import org.eclipse.ecf.core.identity.Namespace;
import org.eclipse.ecf.presence.im.IChatID;
import org.jivesoftware.smack.XMPPConnection;

public class XMPPRoomID extends BaseID implements IChatID {

	private static final long serialVersionUID = -4843967090539640622L;
	public static final String DOMAIN_DEFAULT = "conference"; //$NON-NLS-1$
	public static final String NICKNAME = "nickname"; //$NON-NLS-1$
	public static final String AT_SIGN = "@"; //$NON-NLS-1$
	public static final String DOT = "."; //$NON-NLS-1$
	public static final String SLASH = "/"; //$NON-NLS-1$
	public static final char DOT_CHAR = DOT.charAt(0);

	String domain;
	String host;
	String username;
	String roomname;
	String nickname = ""; //$NON-NLS-1$
	String longName;

	public static String fixConferenceDomain(String domain, String host) {
		if (domain == null)
			domain = DOMAIN_DEFAULT;
		return domain + DOT + host;
	}

	public XMPPRoomID(Namespace namespace, String username, String host, String domain, String roomname, String nickname) throws URISyntaxException {
		super(namespace);
		this.domain = domain;
		this.host = host;
		this.username = username;
		this.roomname = roomname;
		this.nickname = ((nickname == null) ? username : nickname);
	}

	public XMPPRoomID(Namespace namespace, XMPPID userid, String domain, String groupname, String nickname) throws URISyntaxException {
		this(namespace, userid.getUsername(), userid.getHostname(), domain, groupname, nickname);
	}

	public XMPPRoomID(Namespace namespace, XMPPConnection conn, String roomid, String longName) throws URISyntaxException {
		super(namespace);
		final String connUsername = conn.getUser();
		int atIndex = connUsername.indexOf(AT_SIGN);
		this.username = (atIndex == -1) ? connUsername : connUsername.substring(0, atIndex);
		atIndex = roomid.indexOf(AT_SIGN);
		if (atIndex == -1) {
			this.roomname = roomid;
			this.host = conn.getHost();
			this.domain = DOMAIN_DEFAULT;
		} else {
			this.roomname = roomid.substring(0, atIndex);
			final String fullHost = roomid.substring(atIndex + 1);
			final int dotIndex = fullHost.indexOf(DOT_CHAR);
			this.domain = fullHost.substring(0, dotIndex);
			this.host = fullHost.substring(dotIndex + 1);
		}
		this.nickname = this.username;
		this.longName = (longName == null) ? this.roomname + AT_SIGN + this.domain + DOT + this.host : longName;
	}

	public XMPPRoomID(Namespace namespace, XMPPConnection conn, String roomid) throws URISyntaxException {
		this(namespace, conn, roomid, null);
	}

	protected int namespaceCompareTo(BaseID o) {
		return getName().compareTo(o.getName());
	}

	protected boolean fieldEquals(XMPPRoomID o) {
		return (this.domain.equals(o.domain) && (this.host.equals(o.host)) && (this.nickname.equals(o.nickname)) && (this.roomname.equals(o.roomname)) && (this.username.equals(o.username)));
	}

	protected boolean namespaceEquals(BaseID o) {
		if (!(o instanceof XMPPRoomID)) {
			return false;
		}
		final XMPPRoomID other = (XMPPRoomID) o;
		return fieldEquals(other);
	}

	protected String namespaceGetName() {
		return this.roomname;
	}

	protected int namespaceHashCode() {
		return this.domain.hashCode() ^ this.host.hashCode() ^ this.nickname.hashCode() ^ this.roomname.hashCode() ^ this.username.hashCode();
	}

	/* (non-Javadoc)
	 * @see org.eclipse.ecf.core.identity.BaseID#namespaceToExternalForm()
	 */
	protected String namespaceToExternalForm() {
		return namespace.getScheme() + Namespace.SCHEME_SEPARATOR + this.longName;
	}

	public String getMucString() {
		return this.roomname + AT_SIGN + this.domain + DOT + this.host;
	}

	public String getNickname() {
		return nickname;
	}

	public String getLongName() {
		return longName;
	}

	public String toString() {
		final StringBuffer sb = new StringBuffer("XMPPRoomID["); //$NON-NLS-1$
		sb.append(getNamespace().getScheme() + "://" + getUsername() + AT_SIGN //$NON-NLS-1$
				+ this.domain + DOT + this.host + SLASH + this.roomname).append("]"); //$NON-NLS-1$
		return sb.toString();
	}

	public Object getAdapter(Class clazz) {
		if (clazz.isInstance(this)) {
			return this;
		} else
			return super.getAdapter(clazz);
	}

	public String getUsername() {
		return getNickname();
	}

	public String getHostname() {
		return this.host;
	}
}

Back to the top