From abc1937ae0efe903500d49e6a65bb102541eceb6 Mon Sep 17 00:00:00 2001 From: Damian Minkov Date: Wed, 31 May 2006 15:40:57 +0000 Subject: [PATCH] Moved authorization code to the new channel four cmdFactory --- ...OperationSetPersistentPresenceIcqImpl.java | 15 +- .../icq/message/auth/AuthCmdFactory.java | 212 +++++++----------- .../icq/message/auth/AuthOldMsgCmd.java | 97 -------- 3 files changed, 98 insertions(+), 226 deletions(-) delete mode 100644 src/net/java/sip/communicator/impl/protocol/icq/message/auth/AuthOldMsgCmd.java diff --git a/src/net/java/sip/communicator/impl/protocol/icq/OperationSetPersistentPresenceIcqImpl.java b/src/net/java/sip/communicator/impl/protocol/icq/OperationSetPersistentPresenceIcqImpl.java index 989db1d0d..cdbdb16ec 100644 --- a/src/net/java/sip/communicator/impl/protocol/icq/OperationSetPersistentPresenceIcqImpl.java +++ b/src/net/java/sip/communicator/impl/protocol/icq/OperationSetPersistentPresenceIcqImpl.java @@ -26,6 +26,7 @@ import net.kano.joustsim.oscar.oscar.service.buddy.*; import java.beans.PropertyChangeEvent; import net.java.sip.communicator.impl.protocol.icq.message.auth.*; +import net.java.sip.communicator.impl.protocol.icq.message.imicbm.*; /** * The ICQ implementation of a Persistent Presence Operation set. This class @@ -788,8 +789,18 @@ public void setAuthorizationHandler(AuthorizationHandler handler) AuthCmdFactory authCmdFactory = new AuthCmdFactory(icqProvider, icqProvider.getAimConnection(), handler); - snacProcessor.getCmdFactoryMgr(). - getDefaultFactoryList().registerAll(authCmdFactory); + ChannelFourCmdFactory channelFourFactory = + ((OperationSetBasicInstantMessagingIcqImpl) + icqProvider.getSupportedOperationSets() + .get(OperationSetBasicInstantMessagingIcqImpl.class.getName())). + getChannelFourFactory(); + + channelFourFactory.addCommandHandler( + IcbmChannelFourCommand.MTYPE_AUTHREQ, authCmdFactory); + channelFourFactory.addCommandHandler( + IcbmChannelFourCommand.MTYPE_AUTHDENY, authCmdFactory); + channelFourFactory.addCommandHandler( + IcbmChannelFourCommand.MTYPE_AUTHOK, authCmdFactory); snacProcessor.addGlobalResponseListener(authCmdFactory); diff --git a/src/net/java/sip/communicator/impl/protocol/icq/message/auth/AuthCmdFactory.java b/src/net/java/sip/communicator/impl/protocol/icq/message/auth/AuthCmdFactory.java index 770212ae2..06ef7b29d 100644 --- a/src/net/java/sip/communicator/impl/protocol/icq/message/auth/AuthCmdFactory.java +++ b/src/net/java/sip/communicator/impl/protocol/icq/message/auth/AuthCmdFactory.java @@ -19,6 +19,7 @@ import net.kano.joscar.snaccmd.ssi.*; import net.kano.joustsim.oscar.*; import net.kano.joustsim.Screenname; +import net.java.sip.communicator.impl.protocol.icq.message.imicbm.*; /** * Extending the normal messages factory as its not handling the channel 4 @@ -36,15 +37,11 @@ * @author Damian Minkov */ public class AuthCmdFactory - extends ClientIcbmCmdFactory implements SnacResponseListener + implements SnacResponseListener, ChFourPacketHandler { private static final Logger logger = Logger.getLogger(AuthCmdFactory.class); - protected static List SUPPORTED_TYPES = null; - - public static final int CHANNEL_AUTH = 0x0004; - private ProtocolProviderServiceIcqImpl icqProvider; private AuthorizationHandler authorizationHandler; private OperationSetPersistentPresenceIcqImpl operationSetPresence; @@ -58,133 +55,12 @@ public AuthCmdFactory(ProtocolProviderServiceIcqImpl icqProvider, this.authorizationHandler = authorizationHandler; this.aimConnection = aimConnection; - List types = super.getSupportedTypes(); - ArrayList tempTypes = new ArrayList(types); - tempTypes.add(new CmdType(4, 7)); - - this.SUPPORTED_TYPES = DefensiveTools.getUnmodifiable(tempTypes); - this.operationSetPresence = (OperationSetPersistentPresenceIcqImpl) icqProvider.getSupportedOperationSets(). get(OperationSetPresence.class.getName()); } - /** - * Attempts to convert the given SNAC packet to a - * SnacCommand. - * - * @param packet the packet to use for generation of a - * SnacCommand - * @return an appropriate SnacCommand for representing the - * given SnacPacket, or null if no such - * object can be created - */ - public SnacCommand genSnacCommand(SnacPacket packet) - { - if (AbstractIcbm.getIcbmChannel(packet) == CHANNEL_AUTH) - { - AuthOldMsgCmd messageCommand = new AuthOldMsgCmd(packet); - - int messageType = messageCommand.getMessageType(); - - String uin = String.valueOf(messageCommand.getSender()); - Contact srcContact = operationSetPresence.findContactByID(uin); - - // Contact may be not in the contact list - // as we added it as Volatile stopped the application - // and after that received authorization response - if(srcContact == null) - srcContact = operationSetPresence.createVolatileContact(uin); - - if (messageType == AuthOldMsgCmd.MTYPE_AUTHREQ) - { - // this is a authorisation request with or without reason - AuthorizationRequest authRequest = new AuthorizationRequest(); - authRequest.setReason(messageCommand.getReason()); - - AuthorizationResponse authResponse = - authorizationHandler.processAuthorisationRequest( - authRequest, - srcContact - ); - - if (authResponse.getResponseCode() == - AuthorizationResponse.ACCEPT) - { - aimConnection.getInfoService().sendSnac( - new AuthReplyCmd( - String.valueOf(icqProvider.getAccountID(). - getAccountUserID()), - authResponse.getReason(), - true)); - } - else if (authResponse.getResponseCode() == - AuthorizationResponse.REJECT) - { - aimConnection.getInfoService().sendSnac( - new AuthReplyCmd( - String.valueOf(icqProvider.getAccountID(). - getAccountUserID()), - authResponse.getReason(), - false)); - } - // all other is ignored - } - else - if (messageType == AuthOldMsgCmd.MTYPE_AUTHDENY) - { - // this is authorisation reply deny - // with or without reason - AuthorizationResponse authResponse = - new AuthorizationResponse( - AuthorizationResponse.REJECT, - messageCommand.getReason()); - - authorizationHandler.processAuthorizationResponse( - authResponse, - srcContact); - } - else - if (messageType == AuthOldMsgCmd.MTYPE_AUTHOK) - { - // this is authorization reply with accept - // with reason == null - AuthorizationResponse authResponse = - new AuthorizationResponse( - AuthorizationResponse.ACCEPT, - messageCommand.getReason()); - - authorizationHandler.processAuthorizationResponse( - authResponse, - srcContact); - } - else - if (messageType == AuthOldMsgCmd.MTYPE_ADDED) - { - /** Info that user has added us to their contact list */ - logger.trace("User (" + messageCommand.getSender() + - ") has added us to his contact list!"); - } - - return messageCommand; - } - - return super.genSnacCommand(packet); - } - - /** - * Returns a list of the SNAC command types this factory can possibly - * convert to SnacCommands. - * - * @return a list of command types that can be passed to - * genSnacCommand - */ - public List getSupportedTypes() - { - return SUPPORTED_TYPES; - } - /** * Listen for errors comming from the server * when somebody tries to add a buddy which requires @@ -219,8 +95,11 @@ public void handleResponse(SnacResponseEvent snacResponseEvent) logger.trace("finding buddy : " + uinToAskForAuth); Contact srcContact = -// new VolatileBuddy(); operationSetPresence.findContactByID(uinToAskForAuth); + + if(srcContact == null) + srcContact = operationSetPresence.createVolatileContact(uinToAskForAuth); + AuthorizationRequest authRequest = authorizationHandler.createAuthorizationRequest( srcContact); @@ -264,4 +143,83 @@ public void handleResponse(SnacResponseEvent snacResponseEvent) } } + + public SnacCommand handle(IcbmChannelFourCommand messageCommand) + { + int messageType = messageCommand.getMessageType(); + + String uin = String.valueOf(messageCommand.getSender()); + Contact srcContact = operationSetPresence.findContactByID(uin); + + // Contact may be not in the contact list + // as we added it as Volatile stopped the application + // and after that received authorization response + if(srcContact == null) + srcContact = operationSetPresence.createVolatileContact(uin); + + if (messageType == IcbmChannelFourCommand.MTYPE_AUTHREQ) + { + // this is a authorisation request with or without reason + AuthorizationRequest authRequest = new AuthorizationRequest(); + authRequest.setReason(messageCommand.getReason()); + + AuthorizationResponse authResponse = + authorizationHandler.processAuthorisationRequest( + authRequest, + srcContact + ); + + if (authResponse.getResponseCode() == + AuthorizationResponse.ACCEPT) + { + aimConnection.getInfoService().sendSnac( + new AuthReplyCmd( + String.valueOf(icqProvider.getAccountID(). + getAccountUserID()), + authResponse.getReason(), + true)); + } + else if (authResponse.getResponseCode() == + AuthorizationResponse.REJECT) + { + aimConnection.getInfoService().sendSnac( + new AuthReplyCmd( + String.valueOf(icqProvider.getAccountID(). + getAccountUserID()), + authResponse.getReason(), + false)); + } + // all other is ignored + } + else + if (messageType == IcbmChannelFourCommand.MTYPE_AUTHDENY) + { + // this is authorisation reply deny + // with or without reason + AuthorizationResponse authResponse = + new AuthorizationResponse( + AuthorizationResponse.REJECT, + messageCommand.getReason()); + + authorizationHandler.processAuthorizationResponse( + authResponse, + srcContact); + } + else + if (messageType == IcbmChannelFourCommand.MTYPE_AUTHOK) + { + // this is authorization reply with accept + // with reason == null + AuthorizationResponse authResponse = + new AuthorizationResponse( + AuthorizationResponse.ACCEPT, + messageCommand.getReason()); + + authorizationHandler.processAuthorizationResponse( + authResponse, + srcContact); + } + + return messageCommand; + } } diff --git a/src/net/java/sip/communicator/impl/protocol/icq/message/auth/AuthOldMsgCmd.java b/src/net/java/sip/communicator/impl/protocol/icq/message/auth/AuthOldMsgCmd.java deleted file mode 100644 index 2a9374f72..000000000 --- a/src/net/java/sip/communicator/impl/protocol/icq/message/auth/AuthOldMsgCmd.java +++ /dev/null @@ -1,97 +0,0 @@ -/* - * SIP Communicator, the OpenSource Java VoIP and Instant Messaging client. - * - * Distributable under LGPL license. - * See terms of license at gnu.org. - */ -package net.java.sip.communicator.impl.protocol.icq.message.auth; - -import java.io.*; - -import net.java.sip.communicator.impl.protocol.icq.message.common.*; -import net.kano.joscar.*; -import net.kano.joscar.flapcmd.*; -import net.kano.joscar.snaccmd.*; -import net.kano.joscar.snaccmd.icbm.*; -import net.kano.joscar.tlv.*; - -/** - * Parses incoming data from the messages of channel 4 - * - * @author Damian Minkov - */ -public class AuthOldMsgCmd - extends AbstractImIcbm -{ - /** A TLV type containing the text of the Instant Message. */ - private static final int TYPE_MESSAGE_DATA = 0x0005; - - /** Message type - Authorization Request */ - public static final int MTYPE_AUTHREQ = 0x06; - /** Message type - Authorization Denied */ - public static final int MTYPE_AUTHDENY = 0x07; - /** Message type - Authorization Accepted */ - public static final int MTYPE_AUTHOK = 0x08; - /** Message type - Buddy has added you to his contact list */ - public static final int MTYPE_ADDED = 0x0c; - - private int messageType = -1; - - /** Information about the sender of this IM. */ - private final FullUserInfo userInfo; - - private long sender; - private String reason; - - public AuthOldMsgCmd(SnacPacket packet) - { - super(IcbmCommand.CMD_ICBM, packet); - - DefensiveTools.checkNull(packet, "packet"); - - ByteBlock snacData = getChannelData(); - - userInfo = FullUserInfo.readUserInfo(snacData); - - ByteBlock tlvBlock = snacData.subBlock(userInfo.getTotalSize()); - - TlvChain chain = TlvTools.readChain(tlvBlock); - - Tlv messageDataTlv = chain.getLastTlv(TYPE_MESSAGE_DATA); - ByteBlock messageData = messageDataTlv.getData(); - - sender = LEBinaryTools.getUInt(messageData, 0); - - messageType = LEBinaryTools.getUByte(messageData, 4); - - short msgFlags = LEBinaryTools.getUByte(messageData, 5); - - int textlen = LEBinaryTools.getUShort(messageData, 6) - 1; - ByteBlock field = messageData.subBlock(8, textlen); - reason = OscarTools.getString(field, "US-ASCII"); - } - - protected void writeChannelData(OutputStream out) throws IOException - { - } - - public String getReason() - { - return reason; - } - - public long getSender() - { - return sender; - } - - public FullUserInfo getUserInfo() - { - return userInfo; - } - - public int getMessageType() - { - return messageType; - } -}