All pastes #1770056 Raw Edit

Unnamed

public text v1 · immutable
#1770056 ·published 2010-01-29 00:01 UTC
rendered paste body
/*
 * Copyright 2004-2005 the original author or authors.
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package jwm.msn;
import jwm.*;
import jwm.msn.vo.*;
import jwm.vo.ImEventVO;

import java.io.File;
import java.util.*;

import net.sf.jml.*;
import net.sf.jml.impl.*;
import net.sf.jml.message.MsnControlMessage;
import net.sf.jml.message.MsnDatacastMessage;
import net.sf.jml.message.MsnInstantMessage;
/**
 * @author Roy Tsang
 *
 */
public class MsnClient {
	LoginVO loginVO;
	MsnMessenger messenger;
	MsnOwner msnOwner;
	MyListener myMsnListener;
	
	/**
	 * indicate whether the buddy list is synchronized.
	 */
	public boolean isBuddyListReady=false,isDownloadContactPhoto=false;
	protected ImEvent imEvent;
	private Hashtable switchBoardList=new Hashtable();
	private Hashtable groupedBuddyList=new Hashtable(),buddyList=new Hashtable();
	private String emailAddress=new String(),id=new String(),status=new String();
	public String tempFolder=new String();
	/**
	 * MsnClient object constructor
	 * @param l login value object
	 * @param ie Im Event Listener
	 * @throws Exception
	 */
	public MsnClient(LoginVO l,ImEvent ie) throws Exception
	{
		this.loginVO=l;
		this.setEmailAddress(this.loginVO.getEmailAddress());
		myMsnListener=new MyListener(ie);
		this.imEvent=ie;
		messenger = MsnMessengerFactory.createMsnMessenger(this.loginVO.getEmailAddress(),this.loginVO.getPassword());
		msnOwner=messenger.getOwner();
		messenger.setSupportedProtocol(new MsnProtocol[] { MsnProtocol.MSNP15});
		msnOwner.setInitStatus(this.loginVO.getInitStatus());
		this.setStatus(this.loginVO.getStatus());
		myMsnListener.setMsnClient(this);
		
		messenger.addMessengerListener(myMsnListener);
		messenger.addMessageListener(myMsnListener);
		messenger.addContactListListener(myMsnListener);
		messenger.addSwitchboardListener(myMsnListener);
		messenger.addFileTransferListener(myMsnListener);
		messenger.setLogIncoming(Utility.getSystemParameterBoolean("jwm.printJMLDebugLog")); 
		messenger.setLogOutgoing(Utility.getSystemParameterBoolean("jwm.printJMLDebugLog"));
		
	}
	public boolean haveSuggestedName()throws Exception
	{
		DAO dao=new DAO();
		boolean result=dao.haveSuggestedName(getEmailAddress());
		dao.close();
		dao=null;
		return result;
	}
	public void setInitPhoto(String photoPath)throws Exception
	{
	    MsnObject displayPicture = MsnObject.getInstance(this.emailAddress,photoPath);
		messenger.getOwner().setInitDisplayPicture(displayPicture);
	}

	/**
	 * set the temp. folder for storing contact photo
	 * @param s
	 */
	public void setTempFolder(String s)
	{
		tempFolder=s;
	}
	/*
	 * get IM event Listener
	 * @return IM event listener
	 */
	public ImEvent getImEventListener()
	{
		return this.imEvent;
	}
	/**
	 * add new contact 
	 * @param e new contact email address
	 */
	public void addContact(String e)
	{
		messenger.addFriend(Email.parseStr(e),e);
	}
	/**
	 * refuse an add contact request
	 * @param email contact email address
	 */
	public void refuseAddContactRequest(String email)
	{
		messenger.removeFriend(Email.parseStr(email), true);
	}
	
	/**
	 * add switch board to switchboard list
	 * @param id msn id
	 * @param s msn switchboard
	 */
	@SuppressWarnings("unchecked")
	public void addSwitchBoard(String id,MsnSwitchboard s)
	{
		switchBoardList.put(id,s);
	}
	/**
	 * remove switch board from switchboar list
	 * @param id msn id
	 * 
	 */
	public void removeSwitchBoard(String id)
	{
		switchBoardList.remove(id);
	}
	/**
	 * login msn server
	 * @throws Exception
	 */
	public void login()throws Exception
	{
		messenger.login();
	}
	/**
	 * logout msn server
	 */
	public void logout()
	{
		messenger.logout();
		messenger=null;
		File f=new File(tempFolder);
		if (f.exists())
		{
			File []files=f.listFiles();
			for (int i=0;i<files.length;i++)
			{
				files[i].delete();
			}
			f.delete();
		}
		f=null;
	}
	/**
	 * set msn user status
	 * @param s msn user status string
	 */
	public void setStatus(String s)
	{
		this.status=s;
		msnOwner.setStatus(MsnUserStatus.parseStr(s));
	}
	/**
	 * get msn user status in string format
	 * @return msn user status in string format
	 */
	public String getStatus()
	{
		return this.status;
	}
	/**
	 * send a nudge to the specified msn account
	 * @param id user id
	 * @param msg message
	 */
	public void sendNudge(String id)
	{
		if (switchBoardList.containsKey(id))
		{
			MsnSwitchboard switchBoard=(MsnSwitchboard)switchBoardList.get(id);
			MsnDatacastMessage nudgeMessage=new MsnDatacastMessage(); 
			nudgeMessage.setId(1);
			switchBoard.sendMessage(nudgeMessage);
		}
		else
		{
			MsnContact contact=this.getBuddy(id);
			Vector attachment=new Vector();
			attachment.add(MsnEvent.SENDNUDGE);
			attachment.add(contact);
			
			messenger.newSwitchboard(attachment);
		}
	}	
	/**
	 * send text message to the specified msn account
	 * @param id user id
	 * @param msg message
	 */
	public void sendMsg(String id,MsnInstantMessage msg)
	{
		MsnContact []contacts;
		MsnContact contact;
		String temp=new String();
		contact=this.getBuddy(id);
		if (contact.getStatus().equals(MsnUserStatus.OFFLINE))
		{
			sendOffLineMsg(contact,msg.getContent());
		}
		else
		{
			if (switchBoardList.containsKey(id))
			{
				MsnSwitchboard switchBoard=(MsnSwitchboard)switchBoardList.get(id);
				MsnControlMessage typingMessage = new MsnControlMessage();
				typingMessage.setTypingUser(switchBoard.getMessenger()
	                .getOwner().getDisplayName());
				try{
					switchBoard.sendMessage(typingMessage);
					boolean msgSentOK=switchBoard.sendMessage(msg,true);
					if (!msgSentOK)
					{
						contacts=switchBoard.getAllContacts();
						this.removeSwitchBoard(id);
						for (int qk=0;qk<contacts.length;qk++)
						{
							if (contacts[qk].getStatus().equals(MsnUserStatus.OFFLINE))
							{
								sendOffLineMsg(contact,msg.getContent());
							}
							else
							{
								sendMsg(contact,msg);
							}
						}
					}
				}
				catch (Exception ise)
				{
					this.removeSwitchBoard(id);
					sendMsg(contact,msg);
				}
			}
			else
			{
				sendMsg(contact,msg);
			}
		}
		
	}
	private void sendMsg(MsnContact contact,MsnInstantMessage msg)
	{
		Vector attachment=new Vector();
		attachment.add(MsnEvent.SENDMESSAGE);
		attachment.add(contact);
		attachment.add(msg);
		messenger.newSwitchboard(attachment);
	}
	private void sendOffLineMsg(MsnContact contact,String content)
	{
		messenger.sendText(contact.getEmail(),content);
		Vector eventDetail=new Vector();
		eventDetail.add(contact.getId());
		ImEventVO imEventVO=new ImEventVO();
		imEventVO.setEventType(MsnEvent.MESSAGESENTFAIL);
		imEventVO.setEventDetail(eventDetail);
		imEvent.writeToEventList(imEventVO);
	}
	private void setEmailAddress(String e)
	{
		this.emailAddress=e;
	}
	/**
	 * get msn user email address
	 * @return msn user email address(i.e. login name)
	 */
	public String getEmailAddress()
	{
		return this.emailAddress;
	}
	public MsnContact[] getBuddies()
	{
		return messenger.getContactList().getContactsInList(MsnList.FL);
 	}
	/**
	 * get buddy group
	 * @return buddy group
	 */
	public MsnGroup[] getBuddyGroups()
	{
		return messenger.getContactList().getGroups();
	}
	/**
	 * get grouped buddy list
	 * @return grouped buddy list
	 */
	public Hashtable getGroupedBuddyList()
	{
		return this.groupedBuddyList;
	}
	/**
	 * get buddy
	 * @param id msn contact id
	 * @return msn contact
	 */
	public MsnContact getBuddy(String id)
	{
		return (MsnContact) buddyList.get(id);
	}
	/**
	 * get user personal message
	 * @return user personal message 
	 */
	public String getPersonalMessage()
	{
		return msnOwner.getPersonalMessage();
	}
	/**
	 * set user personal message
	 * @param pm user personal message 
	 */
	public void setPersonalMessage(String pm)
	{
		msnOwner.setPersonalMessage(pm);
	}
	/**
	 * get user display name
	 * @return user display name
	 */
	public String getDisplayName()
	{
		return messenger.getOwner().getDisplayName();
	}
	/**
	 * set user display name
	 * @param dm user display name
	 */
	public void setDisplayName(String dm)
	{
		messenger.getOwner().setDisplayName(dm);
	}	
	/**
	 * Initialize the contact list when the CONTACTLISTINITCOMPLETED event occur
	 */
	protected void initContactList()
	{
		MsnGroup[] groups=this.getBuddyGroups();
		MsnContact[] contact=messenger.getContactList().getContactsInList(MsnList.FL);
									
		Vector[] contactList=new Vector[groups.length+1];
		MsnContact c;
		this.groupedBuddyList=new Hashtable();
		this.buddyList=new Hashtable();
		boolean found=false;
		StoreContactPhoto storeContactPhoto; 
		int j,i;
		for (j=0;j<groups.length+1;j++)
		{
			contactList[j]=new Vector();
		}
		for (i=0;i<contact.length;i++)
		{
			c=contact[i];
			buddyList.put(c.getId(),c);
			if (this.isDownloadContactPhoto)
				storeContactPhoto=new StoreContactPhoto(this.tempFolder,c,messenger,this);
			found=false;
			for (j=0;j<groups.length;j++)
			{
				if (c.belongGroup(groups[j]))
				{
					contactList[j].add(c);
					found=true;
					//break;
				} 
			}
			if(!found)
			{	
				contactList[j].add(c);
			}
		}
		
		for (i=0;i<groups.length;i++)
		{
			groupedBuddyList.put(groups[i].getGroupId()+"_"+groups[i].getGroupName(),contactList[i]);
		}
		if (groupedBuddyList.get("0_Default group")==null)
			groupedBuddyList.put("0_Default group",contactList[i]);
		else
		{
		   Vector temp=(Vector)groupedBuddyList.get("0_Default group");
		   for (Enumeration e=contactList[i].elements();e.hasMoreElements();)
		   {
			   temp.add(e.nextElement());
		   }
		   groupedBuddyList.put("0_Default group",temp);
		}
		isBuddyListReady=true;
	}
	public void retreiveOfflineMessages()
	{
		System.out.println("retreive offline message.");
		messenger.retreiveOfflineMessages();
		System.out.println("retreive offline message completed.");
	}
	public void retreiveOfflineAddContactRequest()
	{
		MsnContactPending[] pending= messenger.getPendingList().toArray(new MsnContactPending[] {}); 
		System.out.println("Pending contact request count="+pending.length);	
		if (pending.length>0)
		{
			int addContactCount=messenger.getPendingList().size();
			for (int i=0;i<addContactCount;i++)
			{
				Vector eventDetail=new Vector();
				eventDetail.add(pending[i]);
				ImEventVO imEventVO=new ImEventVO();
				imEventVO.setEventType(MsnEvent.OFFLINEADDCONTACTREQUEST);
				imEventVO.setEventDetail(eventDetail);
				imEvent.writeToEventList(imEventVO);
			
			}
		}
	}
}