lizongbo at 618119.com 工作,生活,Android,前端,Linode,Ubuntu,nginx,java,apache,tomcat,Resin,mina,Hessian,XMPP,RPC

2008年01月3日

为 OpenFire和Spark增加QQTransport–Openfire篇(一)

Filed under: Java — 标签:, , — lizongbo @ 13:15

在OpenFire中增加QQSession和QQTransport。使用lumaQQ作为底层lib。

1.首先在org.jivesoftware.openfire.gateway.type.TransportType.java中增加一项 qq.

/**
* A gateway to other QQ servers.
*/
qq

2.

D:\Java\openfire\plugins\gateway\i18n\gateway_i18n.properties
增加内容:

# qq Transport
gateway.qq.shortservice=qq
gateway.qq.service=qq Messenger
gateway.qq.name=qq Transport
gateway.qq.username=QQ No
gateway.qq.password=Password
gateway.qq.registration=Please enter your qq No address and password.
gateway.qq.passwordincorrect=The password you registered with is incorrect.  Please re-register with the correct password.
gateway.qq.sendmsgfailed=Unable to send qq message.  Reason:
gateway.qq.illegalaccount=You are registered with the qq transport with an illegal account name.\nThe account name should look like an email address.\nYou registered as:
gateway.qq.disconnect=You were disconnected from the qq service.
gateway.qq.wink=You have received an qq wink.
gateway.qq.nudge=You have received an qq nudge.
gateway.qq.otherloggedin=You were disconnected from qq because your account signed in at another location.
gateway.qq.mail=You have received a new e-mail message from {0} < {1}>.\nSubject: {2}

3.修改org.jivesoftware.openfire.gateway.GatewayPlugin.java的initializePlugin方法
增加下面的代码:
[code]
/* Set up qq transport. */
transports.put(“qq”,
new TransportInstance(TransportType.qq,
LocaleUtils.
getLocalizedString(
“gateway.qq.name”,
“gateway”),
“org.jivesoftware.openfire.gateway.protocols.qq.QQTransport”,
componentManager, sessionRouter));

transports.get(“qq”).enable();
maybeStartService(“qq”);
[/code]

4.QQTransport.java的代码为:
[code]
package org.jivesoftware.openfire.gateway.protocols.qq;

import org.jivesoftware.util.LocaleUtils;
import org.jivesoftware.openfire.gateway.*;
import org.jivesoftware.openfire.gateway.type.PresenceType;
import org.jivesoftware.openfire.gateway.type.TransportLoginStatus;
import org.jivesoftware.openfire.gateway.registration.Registration;
import org.jivesoftware.openfire.gateway.session.TransportSession;
import org.xmpp.packet.JID;
import org.xmpp.packet.Presence;
import edu.tsinghua.lumaqq.qq.QQ;

public class QQTransport extends BaseTransport {
/**
* @see org.jivesoftware.openfire.gateway.BaseTransport#getTerminologyUsername()
*/
public String getTerminologyUsername() {
return LocaleUtils.getLocalizedString(“gateway.qq.username”, “gateway”);
}

/**
* @see org.jivesoftware.openfire.gateway.BaseTransport#getTerminologyPassword()
*/
public String getTerminologyPassword() {
return LocaleUtils.getLocalizedString(“gateway.qq.password”, “gateway”);
}

/**
* @see org.jivesoftware.openfire.gateway.BaseTransport#getTerminologyNickname()
*/
public String getTerminologyNickname() {
return null;
}

/**
* @see org.jivesoftware.openfire.gateway.BaseTransport#getTerminologyRegistration()
*/
public String getTerminologyRegistration() {
return LocaleUtils.getLocalizedString(“gateway.qq.registration”,
“gateway”);
}

/**
* @see org.jivesoftware.openfire.gateway.BaseTransport#isPasswordRequired()
*/
public Boolean isPasswordRequired() {
return true;
}

/**
* @see org.jivesoftware.openfire.gateway.BaseTransport#isNicknameRequired()
*/
public Boolean isNicknameRequired() {
return false;
}

/**
* @see org.jivesoftware.openfire.gateway.BaseTransport#isUsernameValid(String)
*/
public Boolean isUsernameValid(String username) {
try {
Integer.parseInt(username);
return true;
} catch (Exception ex) {
ex.printStackTrace();
}
return false;
}

/**
* Handles creating a QQ session and triggering a login.
*
* @param registration Registration information to be used to log in.
* @param jid JID that is logged into the transport.
* @param presenceType Type of presence.
* @param verboseStatus Longer status description.
*/
public TransportSession registrationLoggedIn(Registration registration,
JID jid,
PresenceType presenceType,
String verboseStatus,
Integer priority) {
TransportSession session = new QQSession(registration, jid, this,
priority);
session.setLoginStatus(TransportLoginStatus.LOGGING_IN);
session.logIn(presenceType, verboseStatus);
return session;
}

/**
* Handles logging out of a QQ session.
*
* @param session The session to be disconnected.
*/
public void registrationLoggedOut(TransportSession session) {
session.setLoginStatus(TransportLoginStatus.LOGGING_OUT);
session.logOut();
session.setLoginStatus(TransportLoginStatus.LOGGED_OUT);
}

/**
* Converts a jabber status to an QQ status.
*
* @param jabStatus Jabber presence type.
* @return QQ user status id.
*/
public byte convertJabStatusToQQ(PresenceType jabStatus) {
if (jabStatus == PresenceType.available) {
return QQ.QQ_STATUS_ONLINE;
} else if (jabStatus == PresenceType.away) {
return QQ.QQ_STATUS_AWAY;
} else if (jabStatus == PresenceType.xa) {
return QQ.QQ_STATUS_AWAY;
} else if (jabStatus == PresenceType.dnd) {
return QQ.QQ_STATUS_AWAY;
} else if (jabStatus == PresenceType.chat) {
return QQ.QQ_STATUS_ONLINE;
} else if (jabStatus == PresenceType.unavailable) {
return QQ.QQ_STATUS_OFFLINE;
} else {
return QQ.QQ_STATUS_ONLINE;
}
}

/**
* Sets up a presence packet according to QQ status.
*
* @param QQStatus QQ ContactStatus constant.
* @param packet Presence packet to set up.
*/
public void setUpPresencePacket(Presence packet, byte qqStatus) {
switch (qqStatus) {
case QQ.QQ_STATUS_AWAY:
packet.setShow(Presence.Show.chat);
break;
case QQ.QQ_STATUS_HIDDEN:
packet.setShow(Presence.Show.xa);
break;
case QQ.QQ_STATUS_OFFLINE:
packet.setType(Presence.Type.unavailable);
break;
case QQ.QQ_STATUS_ONLINE:
packet.setShow(Presence.Show.chat);
break;
default:
packet.setShow(Presence.Show.chat);
break;
}
}

}

[/code]
新修改的代码是在Openfire 3.4.3的源代码上写的,老的代码可参考: http://lizongbo.googlepages.com/qq_src

Powered by WordPress