IQLastActivityHandler.java 5.53 KB
Newer Older
1
/*
2 3 4 5 6 7 8 9 10 11 12 13 14 15 16
 * Copyright (C) 2004-2009 Jive Software. All rights reserved.
 *
 * 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.
 */

17
package org.jivesoftware.openfire.handler;
18 19

import org.dom4j.Element;
20 21 22 23 24 25 26 27
import org.jivesoftware.openfire.IQHandlerInfo;
import org.jivesoftware.openfire.PresenceManager;
import org.jivesoftware.openfire.XMPPServer;
import org.jivesoftware.openfire.auth.UnauthorizedException;
import org.jivesoftware.openfire.disco.ServerFeaturesProvider;
import org.jivesoftware.openfire.user.User;
import org.jivesoftware.openfire.user.UserManager;
import org.jivesoftware.openfire.user.UserNotFoundException;
28 29 30
import org.xmpp.packet.IQ;
import org.xmpp.packet.PacketError;

31
import java.util.Collections;
32 33 34 35 36 37 38 39 40 41 42 43
import java.util.Iterator;

/**
 * Implements the TYPE_IQ jabber:iq:last protocol (last activity). Allows users to find out
 * the number of seconds another user has been offline. This information is only available to
 * those users that already subscribed to the users presence. Otherwhise, a <tt>forbidden</tt>
 * error will be returned.
 *
 * @author Gaston Dombiak
 */
public class IQLastActivityHandler extends IQHandler implements ServerFeaturesProvider {

Christian Schudt's avatar
Christian Schudt committed
44 45 46
    private static final String NAMESPACE = "jabber:iq:last";

    private final IQHandlerInfo info;
47 48 49 50
    private PresenceManager presenceManager;

    public IQLastActivityHandler() {
        super("XMPP Last Activity Handler");
Christian Schudt's avatar
Christian Schudt committed
51
        info = new IQHandlerInfo("query", NAMESPACE);
52 53
    }

54 55
    @Override
	public IQ handleIQ(IQ packet) throws UnauthorizedException {
56
        IQ reply = IQ.createResultIQ(packet);
Christian Schudt's avatar
Christian Schudt committed
57
        Element lastActivity = reply.setChildElement("query", NAMESPACE);
58 59 60
        String sender = packet.getFrom().getNode();

        // Check if any of the usernames is null
61
        if (sender == null) {
62 63 64
            reply.setError(PacketError.Condition.forbidden);
            return reply;
        }
Christian Schudt's avatar
Christian Schudt committed
65 66

        if (packet.getTo() != null && packet.getTo().getNode() == null && XMPPServer.getInstance().isLocal(packet.getTo())) {
67 68 69 70 71 72 73 74 75
            // http://xmpp.org/extensions/xep-0012.html#server
            // When the last activity query is sent to a server or component (i.e., to a JID of the form <domain.tld>),
            // the information contained in the IQ reply reflects the uptime of the JID sending the reply.
            // The seconds attribute specifies how long the host has been running since it was last (re-)started.
            long uptime = XMPPServer.getInstance().getServerInfo().getLastStarted().getTime();
            long lastActivityTime = (System.currentTimeMillis() - uptime) / 1000;
            lastActivity.addAttribute("seconds", String.valueOf(lastActivityTime));
            return reply;
        }
76

Christian Schudt's avatar
Christian Schudt committed
77 78 79 80
        // If the 'to' attribute is null, treat the IQ on behalf of the account from which received the stanza
        // in accordance with RFC 6120 § 10.3.3.
        String username = packet.getTo() == null ? packet.getFrom().getNode() : packet.getTo().getNode();

81
        try {
Christian Schudt's avatar
Christian Schudt committed
82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101
            if (username != null) {
                // Check that the user requesting this information is subscribed to the user's presence
                if (presenceManager.canProbePresence(packet.getFrom(), username)) {
                    if (sessionManager.getSessions(username).isEmpty()) {
                        User user = UserManager.getInstance().getUser(username);
                        // The user is offline so answer the user's "last available time and the
                        // status message of the last unavailable presence received from the user"
                        long lastActivityTime = presenceManager.getLastActivity(user);
                        if (lastActivityTime > -1) {
                            // Convert it to seconds
                            lastActivityTime = lastActivityTime / 1000;
                        }
                        lastActivity.addAttribute("seconds", String.valueOf(lastActivityTime));
                        String lastStatus = presenceManager.getLastPresenceStatus(user);
                        if (lastStatus != null && lastStatus.length() > 0) {
                            lastActivity.setText(lastStatus);
                        }
                    } else {
                        // The user is online so answer seconds=0
                        lastActivity.addAttribute("seconds", "0");
102
                    }
Christian Schudt's avatar
Christian Schudt committed
103 104
                } else {
                    reply.setError(PacketError.Condition.forbidden);
105 106 107 108 109 110 111 112 113
                }
            }
        }
        catch (UserNotFoundException e) {
            reply.setError(PacketError.Condition.forbidden);
        }
        return reply;
    }

114 115
    @Override
	public IQHandlerInfo getInfo() {
116 117 118
        return info;
    }

119
    @Override
120
    public Iterator<String> getFeatures() {
Christian Schudt's avatar
Christian Schudt committed
121
        return Collections.singleton(NAMESPACE).iterator();
122 123
    }

124 125
    @Override
	public void initialize(XMPPServer server) {
126 127 128 129
        super.initialize(server);
        presenceManager = server.getPresenceManager();
    }
}