BasicFlapConnection.java 12.3 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14
/**
 * $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.
 *
 * Heavily inspired by joscardemo of the Joust Project: http://joust.kano.net/
 */

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

15 16
import java.io.UnsupportedEncodingException;
import java.text.DateFormat;
17
import java.util.*;
18
import java.util.concurrent.ConcurrentHashMap;
19

20
import org.jivesoftware.util.Log;
21
import org.xmpp.packet.Message;
22
import org.xmpp.packet.Presence;
23
import org.xmpp.packet.JID;
24 25 26
import net.kano.joscar.ByteBlock;
import net.kano.joscar.OscarTools;
import net.kano.joscar.BinaryTools;
27
import net.kano.joscar.net.ConnDescriptor;
28 29 30 31 32 33 34 35 36 37 38 39 40 41 42
import net.kano.joscar.flap.FlapCommand;
import net.kano.joscar.flap.FlapPacketEvent;
import net.kano.joscar.snac.SnacPacketEvent;
import net.kano.joscar.snac.SnacResponseEvent;
import net.kano.joscar.snac.SnacRequest;
import net.kano.joscar.snac.SnacRequestListener;
import net.kano.joscar.flapcmd.LoginFlapCmd;
import net.kano.joscar.flapcmd.SnacCommand;
import net.kano.joscar.snaccmd.conn.*;
import net.kano.joscar.snaccmd.*;
import net.kano.joscar.snaccmd.icbm.RecvImIcbm;
import net.kano.joscar.snaccmd.icbm.InstantMessage;
import net.kano.joscar.snaccmd.buddy.BuddyStatusCmd;
import net.kano.joscar.snaccmd.buddy.BuddyOfflineCmd;
import net.kano.joscar.ratelim.RateLimitingQueueMgr;
43

44 45 46 47 48 49
/**
 * Handles incoming FLAP packets.
 *
 * @author Daniel Henninger
 * Heavily inspired by joscardemo from the joscar project.
 */
50 51 52 53
public abstract class BasicFlapConnection extends BaseFlapConnection {
    protected final ByteBlock cookie;
    protected boolean sentClientReady = false;

54
    public ConcurrentHashMap<String,FullUserInfo> buddystore = new ConcurrentHashMap<String, FullUserInfo>();
55

56
    protected int[] snacFamilies = null;
57
    protected Collection<SnacFamilyInfo> snacFamilyInfos;
58 59
    protected RateLimitingQueueMgr rateMgr = new RateLimitingQueueMgr();

60 61
    public BasicFlapConnection(ConnDescriptor cd, OSCARSession mainSession, ByteBlock cookie) {
        super(cd, mainSession);
62
        this.cookie = cookie;
63
        initBasicFlapConnection();
64 65
    }

66 67
    private void initBasicFlapConnection() {
        sp.setSnacQueueManager(rateMgr);
68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87
    }

    protected DateFormat dateFormat
            = DateFormat.getDateTimeInstance(DateFormat.SHORT,
                    DateFormat.SHORT);

    protected void handleFlapPacket(FlapPacketEvent e) {
        FlapCommand cmd = e.getFlapCommand();

        if (cmd instanceof LoginFlapCmd) {
            getFlapProcessor().sendFlap(new LoginFlapCmd(cookie));
        }
    }

    protected void handleSnacPacket(SnacPacketEvent e) {
        SnacCommand cmd = e.getSnacCommand();
        if (cmd instanceof ServerReadyCmd) {
            ServerReadyCmd src = (ServerReadyCmd) cmd;
            setSnacFamilies(src.getSnacFamilies());

88
            Collection<SnacFamilyInfo> familyInfos = SnacFamilyInfoFactory.getDefaultFamilyInfos(src.getSnacFamilies());
89 90
            setSnacFamilyInfos(familyInfos);

91
            oscarSession.registerSnacFamilies(this);
92 93 94

            request(new ClientVersionsCmd(familyInfos));
            request(new RateInfoRequest());
95 96
        }
        else if (cmd instanceof RecvImIcbm) {
97 98 99 100
            RecvImIcbm icbm = (RecvImIcbm) cmd;

            String sn = icbm.getSenderInfo().getScreenname();
            InstantMessage message = icbm.getMessage();
Daniel Henninger's avatar
Daniel Henninger committed
101
            String msg = OscarTools.stripHtml(message.getMessage());
102

103 104 105 106 107 108
            Message m = new Message();
            m.setTo(oscarSession.getJIDWithHighestPriority());
            m.setBody(msg);
            m.setType(Message.Type.chat);
            m.setFrom(this.oscarSession.getTransport().convertIDToJID(sn));
            oscarSession.getTransport().sendPacket(m);
109 110
        }
        else if (cmd instanceof WarningNotification) {
111 112 113
            WarningNotification wn = (WarningNotification) cmd;
            MiniUserInfo warner = wn.getWarner();
            if (warner == null) {
114 115 116 117 118 119
                Message m = new Message();
                m.setTo(oscarSession.getJIDWithHighestPriority());
                m.setBody("You have received an anonymous AIM warning.  Your warning level is now "+wn.getNewLevel()+"%.");
                m.setType(Message.Type.headline);
                m.setFrom(this.oscarSession.getTransport().getJID());
                oscarSession.getTransport().sendPacket(m);
120 121
            }
            else {
122 123
                Log.debug("*** " + warner.getScreenname()
                        + " warned you up to " + wn.getNewLevel() + "%");
124 125 126 127 128 129
                Message m = new Message();
                m.setTo(oscarSession.getJIDWithHighestPriority());
                m.setBody("You have received an AIM warning from "+warner.getScreenname()+".  Your warning level is now "+wn.getNewLevel()+"%.");
                m.setType(Message.Type.headline);
                m.setFrom(this.oscarSession.getTransport().getJID());
                oscarSession.getTransport().sendPacket(m);
130
            }
131 132
        }
        else if (cmd instanceof BuddyStatusCmd) {
133
            BuddyStatusCmd bsc = (BuddyStatusCmd)cmd;
134
            FullUserInfo info = bsc.getUserInfo();
135 136 137 138
            buddystore.put(info.getScreenname(), info);
            Presence p = new Presence();
            p.setTo(oscarSession.getJID());
            p.setFrom(oscarSession.getTransport().convertIDToJID(info.getScreenname()));
139

Daniel Henninger's avatar
Daniel Henninger committed
140
            if (info.getAwayStatus()) {
141 142
                p.setShow(Presence.Show.away);
            }
143

144
            List<ExtraInfoBlock> extraInfo = info.getExtraInfoBlocks();
145 146 147
            if (extraInfo != null) {
                for (ExtraInfoBlock i : extraInfo) {
                    ExtraInfoData data = i.getExtraData();
148

149
                    if (i.getType() == ExtraInfoBlock.TYPE_AVAILMSG) {
150
                        String msg = ExtraInfoData.readAvailableMessage(data);
151
                        if (msg.length() > 0) {
152
                            p.setStatus(msg);
153 154 155 156
                        }
                    }
                }
            }
157
            oscarSession.getTransport().sendPacket(p);
158 159
        }
        else if (cmd instanceof BuddyOfflineCmd) {
160 161 162 163 164 165
            BuddyOfflineCmd boc = (BuddyOfflineCmd)cmd;
            buddystore.remove(boc.getScreenname());
            Presence p = new Presence(Presence.Type.unavailable);
            p.setTo(oscarSession.getJID());
            p.setFrom(oscarSession.getTransport().convertIDToJID(boc.getScreenname()));
            oscarSession.getTransport().sendPacket(p);
166
        }
167 168 169 170 171 172 173
    }

    protected void handleSnacResponse(SnacResponseEvent e) {
        SnacCommand cmd = e.getSnacCommand();

        if (cmd instanceof RateInfoCmd) {
            RateInfoCmd ric = (RateInfoCmd) cmd;
174
            List <RateClassInfo> rateClasses = ric.getRateClassInfos();
175

176 177 178
            int[] classes = new int[rateClasses.size()];
            for (int i = 0; i < rateClasses.size(); i++) {
                classes[i] = rateClasses.get(i).getRateClass();
179 180 181 182 183 184 185 186 187
            }

            request(new RateAck(classes));
        }
    }

    public int[] getSnacFamilies() { return snacFamilies; }

    protected void setSnacFamilies(int[] families) {
Daniel Henninger's avatar
Daniel Henninger committed
188
        this.snacFamilies = families.clone();
189 190 191
        Arrays.sort(snacFamilies);
    }

192
    protected void setSnacFamilyInfos(Collection<SnacFamilyInfo> infos) {
193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218
        snacFamilyInfos = infos;
    }

    protected boolean supportsFamily(int family) {
        return Arrays.binarySearch(snacFamilies, family) >= 0;
    }

    protected void clientReady() {
        if (!sentClientReady) {
            sentClientReady = true;
            request(new ClientReadyCmd(snacFamilyInfos));
        }
    }

    protected SnacRequest dispatchRequest(SnacCommand cmd) {
        return dispatchRequest(cmd, null);
    }

    protected SnacRequest dispatchRequest(SnacCommand cmd,
            SnacRequestListener listener) {
        SnacRequest req = new SnacRequest(cmd, listener);
        dispatchRequest(req);
        return req;
    }

    protected void dispatchRequest(SnacRequest req) {
219
        oscarSession.handleRequest(req);
220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235
    }

    protected SnacRequest request(SnacCommand cmd,
            SnacRequestListener listener) {
        SnacRequest req = new SnacRequest(cmd, listener);

        handleReq(req);

        return req;
    }

    private void handleReq(SnacRequest request) {
        int family = request.getCommand().getFamily();
        if (snacFamilies == null || supportsFamily(family)) {
            // this connection supports this snac, so we'll send it here
            sendRequest(request);
236 237
        }
        else {
238
            oscarSession.handleRequest(request);
239 240 241
        }
    }

242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257
    /**
     * Retrieves and sends last known status.
     *
     * This retrieves the last known status of the user and sends it on
     * to the JID associated with this session.  Meant for probe packets.
     *
     * @param sn Screen name to check on.
     */
    public void getAndSendStatus(String sn) {
        if (buddystore.containsKey(sn)) {
            FullUserInfo info = buddystore.get(sn);
            buddystore.put(info.getScreenname(), info);
            Presence p = new Presence();
            p.setTo(oscarSession.getJID());
            p.setFrom(oscarSession.getTransport().convertIDToJID(info.getScreenname()));

Daniel Henninger's avatar
Daniel Henninger committed
258
            if (info.getAwayStatus()) {
259 260 261
                p.setShow(Presence.Show.away);
            }

262
            List<ExtraInfoBlock> extraInfo = info.getExtraInfoBlocks();
263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294
            if (extraInfo != null) {
                for (ExtraInfoBlock i : extraInfo) {
                    ExtraInfoData data = i.getExtraData();

                    if (i.getType() == ExtraInfoBlock.TYPE_AVAILMSG) {
                        ByteBlock msgBlock = data.getData();
                        int len = BinaryTools.getUShort(msgBlock, 0);
                        byte[] msgBytes = msgBlock.subBlock(2, len).toByteArray(
);
                        String msg;
                        try {
                            msg = new String(msgBytes, "UTF-8");
                        }
                        catch (UnsupportedEncodingException e1) {
                            continue;
                        }
                        if (msg.length() > 0) {
                            p.setStatus(msg);
                        }
                    }
                }
            }
            oscarSession.getTransport().sendPacket(p);
        }
        else {
            Presence p = new Presence(Presence.Type.unavailable);
            p.setTo(oscarSession.getJID());
            p.setFrom(oscarSession.getTransport().convertIDToJID(sn));
            oscarSession.getTransport().sendPacket(p);
        }
    }

295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313
    /**
     * Retrieves and sends last known status for all buddies.
     *
     * This retrieves all known statuses and sends each one of them to the specified JID.
     * This is typically used when a new resource comes online.
     *
     * @param jid JID (with resource) to send the list to.
     */
    public void getAndSendAllStatuses(JID jid) {
        for (FullUserInfo info : buddystore.values()) {
            buddystore.put(info.getScreenname(), info);
            Presence p = new Presence();
            p.setTo(oscarSession.getJID());
            p.setFrom(oscarSession.getTransport().convertIDToJID(info.getScreenname()));

            if (info.getAwayStatus()) {
                p.setShow(Presence.Show.away);
            }

314
            List<ExtraInfoBlock> extraInfo = info.getExtraInfoBlocks();
315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338
            if (extraInfo != null) {
                for (ExtraInfoBlock i : extraInfo) {
                    ExtraInfoData data = i.getExtraData();

                    if (i.getType() == ExtraInfoBlock.TYPE_AVAILMSG) {
                        ByteBlock msgBlock = data.getData();
                        int len = BinaryTools.getUShort(msgBlock, 0);
                        byte[] msgBytes = msgBlock.subBlock(2, len).toByteArray(
);
                        String msg;
                        try {
                            msg = new String(msgBytes, "UTF-8");
                        }
                        catch (UnsupportedEncodingException e1) {
                            continue;
                        }
                        if (msg.length() > 0) {
                            p.setStatus(msg);
                        }
                    }
                }
            }
            oscarSession.getTransport().sendPacket(p);
        }
339

340 341
    }

342
}