MSNSession.java 10.8 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12
/**
 * $Revision$
 * $Date$
 *
 * Copyright (C) 2006 Jive Software. All rights reserved.
 *
 * This software is published under the terms of the GNU Public License (GPL),
 * a copy of which is included in this distribution.
 */

package org.jivesoftware.wildfire.gateway.protocols.msn;

13 14
import net.sf.jml.*;
import net.sf.jml.impl.MsnMessengerFactory;
15
import org.jivesoftware.wildfire.gateway.PresenceType;
16 17
import org.jivesoftware.wildfire.gateway.Registration;
import org.jivesoftware.wildfire.gateway.TransportSession;
18 19
import org.jivesoftware.wildfire.gateway.TransportBuddy;
import org.jivesoftware.wildfire.user.UserNotFoundException;
20
import org.jivesoftware.wildfire.roster.RosterItem;
21
import org.jivesoftware.util.Log;
22 23 24
import org.xmpp.packet.JID;
import org.xmpp.packet.Presence;

25 26
import java.util.List;
import java.util.ArrayList;
27
import java.util.concurrent.ConcurrentHashMap;
28

29 30
/**
 * Represents a MSN session.
31
 *
32 33 34 35 36 37 38 39 40 41 42
 * This is the interface with which the base transport functionality will
 * communicate with MSN.
 *
 * @author Daniel Henninger
 */
public class MSNSession extends TransportSession {

    /**
     * Create a MSN Session instance.
     *
     * @param registration Registration informationed used for logging in.
43 44 45
     * @param jid JID associated with this session.
     * @param transport Transport instance associated with this session.
     * @param priority Priority of this session.
46
     */
47 48
    public MSNSession(Registration registration, JID jid, MSNTransport transport, Integer priority) {
        super(registration, jid, transport, priority);
49

50
        msnMessenger = MsnMessengerFactory.createMsnMessenger(registration.getUsername(), registration.getPassword());
51
        msnMessenger.setSupportedProtocol(MsnProtocol.getAllSupportedProtocol());
52 53 54 55 56
    }

    /**
     * MSN session
     */
57
    private MsnMessenger msnMessenger = null;
58

59 60 61
    /**
     * MSN contacts/friends.
     */
62
    private ConcurrentHashMap<String,MsnContact> msnContacts = new ConcurrentHashMap<String,MsnContact>();
63

64 65 66
    /**
     * MSN groups.
     */
67
    private ConcurrentHashMap<String,MsnGroup> msnGroups = new ConcurrentHashMap<String,MsnGroup>();
68

69 70 71 72
    /**
     * Login status
     */
    private boolean loginStatus = false;
73 74 75

    /**
     * Log in to MSN.
76 77 78
     *
     * @param presenceType Type of presence.
     * @param verboseStatus Long representation of status.
79
     */
80 81
    public void logIn(PresenceType presenceType, String verboseStatus) {
        if (!this.isLoggedIn()) {
82
            msnMessenger.getOwner().setInitStatus(((MSNTransport)getTransport()).convertJabStatusToMSN(presenceType));
83 84
            msnMessenger.setLogIncoming(false);
            msnMessenger.setLogOutgoing(false);
85
            msnMessenger.addListener(new MSNListener(this));
86
            msnMessenger.login();
87 88 89 90 91 92 93
        }
    }

    /**
     * Log out of MSN.
     */
    public void logOut() {
94
        if (this.isLoggedIn()) {
95
            msnMessenger.logout();
96
        }
97 98 99 100
        Presence p = new Presence(Presence.Type.unavailable);
        p.setTo(getJID());
        p.setFrom(getTransport().getJID());
        getTransport().sendPacket(p);
101
        loginStatus = false;
102 103 104
    }

    /**
105 106
     * Retrieves the manager for this session.
     */
107
    public MsnMessenger getManager() {
108
        return msnMessenger;
109 110 111 112
    }

    /**
     * @see org.jivesoftware.wildfire.gateway.TransportSession#isLoggedIn()
113 114
     */
    public Boolean isLoggedIn() {
115
        return loginStatus;
116 117
    }

118 119 120 121 122 123 124 125 126 127
    /**
     * Sets login status flag (am i logged in or not)
     */
    public void setLoginStatus(Boolean status) {
        loginStatus = status;
    }

    /**
     * Records information about a person on the user's contact list.
     */
128 129
    public void storeFriend(MsnContact msnContact) {
        msnContacts.put(msnContact.getEmail().toString(), msnContact);
130 131
    }

132 133 134
    /**
     * Records information about a group on the user's contact list.
     */
135 136
    public void storeGroup(MsnGroup msnGroup) {
        msnGroups.put(msnGroup.getGroupName(), msnGroup);
137 138
    }

139 140 141 142 143
    /**
     * Syncs up the MSN roster with the jabber roster.
     */
    public void syncUsers() {
        List<TransportBuddy> legacyusers = new ArrayList<TransportBuddy>();
144
        for (MsnContact friend : msnContacts.values()) {
145
            ArrayList<String> friendGroups = new ArrayList<String>();
146 147 148
            for (MsnGroup group : friend.getBelongGroups()) {
                friendGroups.add(group.getGroupName());
            }
149 150 151
            if (friendGroups.size() < 1) {
                friendGroups.add("MSN Contacts");
            }
152
            legacyusers.add(new TransportBuddy(friend.getEmail().toString(), friend.getDisplayName(), friendGroups.get(0)));
153 154 155 156 157 158 159 160 161
        }
        try {
            getTransport().syncLegacyRoster(getJID(), legacyusers);
        }
        catch (UserNotFoundException e) {
            Log.error("Unable to sync MSN contact list for " + getJID());
        }

        // Lets send initial presence statuses
162
        for (MsnContact friend : msnContacts.values()) {
163 164
            Presence p = new Presence();
            p.setTo(getJID());
165
            p.setFrom(getTransport().convertIDToJID(friend.getEmail().toString()));
166 167 168 169 170
            ((MSNTransport)getTransport()).setUpPresencePacket(p, friend.getStatus());
            getTransport().sendPacket(p);
        }
    }

171
    /**
172
     * @see org.jivesoftware.wildfire.gateway.TransportSession#addContact(org.jivesoftware.wildfire.roster.RosterItem)
173
     */
174
    public void addContact(RosterItem item) {
175 176 177 178 179 180 181
//        Email contact = Email.parseStr(getTransport().convertJIDToID(item.getJid()));
//        String nickname = getTransport().convertJIDToID(item.getJid());
//        if (item.getNickname() != null && !item.getNickname().equals("")) {
//            nickname = item.getNickname();
//        }
//        msnMessenger.addFriend(contact, nickname);
//        syncContactGroups(contact, item.getGroups());
182 183 184
    }

    /**
185
     * @see org.jivesoftware.wildfire.gateway.TransportSession#removeContact(org.jivesoftware.wildfire.roster.RosterItem)
186
     */
187
    public void removeContact(RosterItem item) {
188 189 190 191 192
//        Email contact = Email.parseStr(getTransport().convertJIDToID(item.getJid()));
//        MsnContact msnContact = msnContacts.get(contact.toString());
//        for (MsnGroup msnGroup : msnContact.getBelongGroups()) {
//            msnMessenger.removeFriend(contact, msnGroup.getGroupId());
//        }
193 194 195 196 197 198
    }

    /**
     * @see org.jivesoftware.wildfire.gateway.TransportSession#updateContact(org.jivesoftware.wildfire.roster.RosterItem)
     */
    public void updateContact(RosterItem item) {
199 200 201 202 203 204 205
//        Email contact = Email.parseStr(getTransport().convertJIDToID(item.getJid()));
//        String nickname = getTransport().convertJIDToID(item.getJid());
//        if (item.getNickname() != null && !item.getNickname().equals("")) {
//            nickname = item.getNickname();
//        }
//        msnMessenger.renameFriend(contact, nickname);
//        syncContactGroups(contact, item.getGroups());
206 207 208 209 210 211 212 213 214
    }

    /**
     * Given a legacy contact and a list of groups, makes sure that the list is in sync with
     * the actual group list.
     *
     * @param contact Email address of contact.
     * @param groups List of groups contact should be in.
     */
215 216 217 218
    public void syncContactGroups(Email contact, List<String> groups) {
        if (groups.isEmpty()) {
            groups.add("Transport Buddies");
        }
219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242
//        MsnContact msnContact = msnContacts.get(contact.toString());
//        // Create groups that do not currently exist.
//        for (String group : groups) {
//            if (!msnGroups.containsKey(group)) {
//                msnMessenger.addGroup(group);
//            }
//        }
//        // Lets update our list of groups.
//        for (MsnGroup msnGroup : msnMessenger.getContactList().getGroups()) {
//            storeGroup(msnGroup);
//        }
//        // Make sure contact belongs to groups that we want.
//        for (String group : groups) {
//            MsnGroup msnGroup = msnGroups.get(group);
//            if (!msnContact.belongGroup(msnGroup)) {
//                msnMessenger.copyFriend(contact, group);
//            }
//        }
//        // Now we will clean up groups that we should no longer belong to.
//        for (MsnGroup msnGroup : msnContact.getBelongGroups()) {
//            if (!groups.contains(msnGroup.getGroupName())) {
//                msnMessenger.removeFriend(contact, msnGroup.getGroupId());
//            }
//        }
243
    }
244 245

    /**
246
     * @see org.jivesoftware.wildfire.gateway.TransportSession#sendMessage(org.xmpp.packet.JID, String)
247 248
     */
    public void sendMessage(JID jid, String message) {
249
        msnMessenger.sendText(Email.parseStr(getTransport().convertJIDToID(jid)), message);
250 251
    }

252 253 254 255 256 257 258
    /**
     * @see org.jivesoftware.wildfire.gateway.TransportSession#sendServerMessage(String)
     */
    public void sendServerMessage(String message) {
        // We don't care.
    }

259
    /**
260
     * @see org.jivesoftware.wildfire.gateway.TransportSession#retrieveContactStatus(org.xmpp.packet.JID)
261 262
     */
    public void retrieveContactStatus(JID jid) {
263
        MsnContact msnContact = msnContacts.get(getTransport().convertJIDToID(jid));
264 265 266 267 268
        if (msnContact == null) {
            return;
        }
        Presence p = new Presence();
        p.setTo(getJID());
269
        p.setFrom(getTransport().convertIDToJID(msnContact.getEmail().toString()));
270 271
        ((MSNTransport)getTransport()).setUpPresencePacket(p, msnContact.getStatus());
        getTransport().sendPacket(p);
272 273
    }

274
    /**
275
     * @see org.jivesoftware.wildfire.gateway.TransportSession#updateStatus(org.jivesoftware.wildfire.gateway.PresenceType, String)
276 277
     */
    public void updateStatus(PresenceType presenceType, String verboseStatus) {
278
        if (isLoggedIn()) {
279 280 281 282
            try {
                msnMessenger.getOwner().setStatus(((MSNTransport)getTransport()).convertJabStatusToMSN(presenceType));
            }
            catch (IllegalStateException e) {
283 284 285
//                // Hrm, not logged in?  Lets fix that.
//                msnMessenger.getOwner().setInitStatus(((MSNTransport)getTransport()).convertJabStatusToMSN(presenceType));
//                msnMessenger.login();
286
            }
287
        }
288
        else {
289 290 291
//            // Hrm, not logged in?  Lets fix that.
//            msnMessenger.getOwner().setInitStatus(((MSNTransport)getTransport()).convertJabStatusToMSN(presenceType));
//            msnMessenger.login();
292
        }
293 294
    }

295
    /**
296
     * @see org.jivesoftware.wildfire.gateway.TransportSession#resendContactStatuses(org.xmpp.packet.JID)
297
     */
298
    public void resendContactStatuses(JID jid) {
299
        for (MsnContact friend : msnContacts.values()) {
300 301
            Presence p = new Presence();
            p.setTo(getJID());
302
            p.setFrom(getTransport().convertIDToJID(friend.getEmail().toString()));
303 304 305
            ((MSNTransport)getTransport()).setUpPresencePacket(p, friend.getStatus());
            getTransport().sendPacket(p);
        }
306 307
    }

308
}