Search in sources :

Example 1 with MultiUserChatManager

use of org.jivesoftware.openfire.muc.MultiUserChatManager in project Openfire by igniterealtime.

the class MucMamPersistenceManager method findMessages.

@Override
public Collection<ArchivedMessage> findMessages(Date startDate, Date endDate, String owner, String with, XmppResultSet xmppResultSet) {
    JID mucRoom = new JID(owner);
    MultiUserChatManager manager = XMPPServer.getInstance().getMultiUserChatManager();
    MultiUserChatService service = manager.getMultiUserChatService(mucRoom);
    MUCRoom room = service.getChatRoom(mucRoom.getNode());
    Connection connection = null;
    PreparedStatement pstmt = null;
    ResultSet rs = null;
    // If logging isn't enabled, do nothing.
    if (!room.isLogEnabled())
        return null;
    List<ArchivedMessage> msgs = new LinkedList<>();
    if (startDate == null) {
        startDate = new Date(0L);
    }
    if (endDate == null) {
        endDate = new Date();
    }
    int max = xmppResultSet.getMax();
    // TODO: Suppress this, since we don't yet have requestor information for access control.
    with = null;
    try {
        connection = DbConnectionManager.getConnection();
        StringBuilder sql = new StringBuilder(LOAD_HISTORY);
        if (with != null) {
            sql.append(WHERE_SENDER);
        }
        if (xmppResultSet.getAfter() != null) {
            sql.append(WHERE_AFTER);
        }
        if (xmppResultSet.getBefore() != null) {
            sql.append(WHERE_BEFORE);
        }
        sql.append(ORDER_BY);
        pstmt = connection.prepareStatement(sql.toString());
        pstmt.setString(1, StringUtils.dateToMillis(startDate));
        pstmt.setString(2, StringUtils.dateToMillis(endDate));
        pstmt.setLong(3, room.getID());
        int pos = 3;
        if (with != null) {
            pstmt.setString(++pos, with);
        }
        if (xmppResultSet.getAfter() != null) {
            pstmt.setLong(++pos, xmppResultSet.getAfter());
        }
        if (xmppResultSet.getBefore() != null) {
            pstmt.setLong(++pos, xmppResultSet.getBefore());
        }
        rs = pstmt.executeQuery();
        while (rs.next()) {
            String senderJID = rs.getString(1);
            String nickname = rs.getString(2);
            Date sentDate = new Date(Long.parseLong(rs.getString(3).trim()));
            String subject = rs.getString(4);
            String body = rs.getString(5);
            String stanza = rs.getString(6);
            long id = rs.getLong(7);
            if (stanza == null) {
                Message message = new Message();
                message.setType(Message.Type.groupchat);
                message.setSubject(subject);
                message.setBody(body);
                // Set the sender of the message
                if (nickname != null && nickname.trim().length() > 0) {
                    JID roomJID = room.getRole().getRoleAddress();
                    // Recreate the sender address based on the nickname and room's JID
                    message.setFrom(new JID(roomJID.getNode(), roomJID.getDomain(), nickname, true));
                } else {
                    // Set the room as the sender of the message
                    message.setFrom(room.getRole().getRoleAddress());
                }
                stanza = message.toString();
            }
            ArchivedMessage archivedMessage = new ArchivedMessage(sentDate, ArchivedMessage.Direction.from, null, null);
            archivedMessage.setStanza(stanza);
            archivedMessage.setId(id);
            msgs.add(archivedMessage);
        }
    } catch (SQLException e) {
        Log.error("SQL failure during MAM-MUC: ", e);
    } finally {
        DbConnectionManager.closeConnection(rs, pstmt, connection);
    }
    // TODO - Not great, really should be done by suitable LIMIT stuff.
    // Would need to reverse ordering in some cases and then reverse results.
    boolean complete = true;
    xmppResultSet.setCount(msgs.size());
    while (msgs.size() > max) {
        msgs.remove(msgs.size() - 1);
        complete = false;
    }
    xmppResultSet.setComplete(complete);
    if (msgs.size() > 0) {
        xmppResultSet.setFirst(msgs.get(0).getId());
        if (msgs.size() > 1) {
            xmppResultSet.setLast(msgs.get(msgs.size() - 1).getId());
        }
    }
    return msgs;
}
Also used : JID(org.xmpp.packet.JID) MultiUserChatManager(org.jivesoftware.openfire.muc.MultiUserChatManager) ArchivedMessage(com.reucon.openfire.plugin.archive.model.ArchivedMessage) Message(org.xmpp.packet.Message) SQLException(java.sql.SQLException) Connection(java.sql.Connection) MultiUserChatService(org.jivesoftware.openfire.muc.MultiUserChatService) PreparedStatement(java.sql.PreparedStatement) LinkedList(java.util.LinkedList) Date(java.util.Date) MUCRoom(org.jivesoftware.openfire.muc.MUCRoom) ArchivedMessage(com.reucon.openfire.plugin.archive.model.ArchivedMessage) ResultSet(java.sql.ResultSet) XmppResultSet(com.reucon.openfire.plugin.archive.xep0059.XmppResultSet)

Example 2 with MultiUserChatManager

use of org.jivesoftware.openfire.muc.MultiUserChatManager in project Openfire by igniterealtime.

the class AbstractXepSupport method start.

public void start() {
    for (IQHandler iqHandler : iqHandlers) {
        try {
            iqHandler.initialize(server);
            iqHandler.start();
        } catch (Exception e) {
            Log.error("Unable to initialize and start " + iqHandler.getClass());
            continue;
        }
        element2Handlers.put(iqHandler.getInfo().getName(), iqHandler);
        if (iqHandler instanceof ServerFeaturesProvider) {
            for (Iterator<String> i = ((ServerFeaturesProvider) iqHandler).getFeatures(); i.hasNext(); ) {
                server.getIQDiscoInfoHandler().addServerFeature(i.next());
            }
        }
        if (muc) {
            MultiUserChatManager manager = server.getMultiUserChatManager();
            for (MultiUserChatService mucService : manager.getMultiUserChatServices()) {
                mucService.addIQHandler(iqHandler);
                mucService.addExtraFeature(namespace);
            }
        }
    }
    server.getIQDiscoInfoHandler().addServerFeature(namespace);
    server.getIQRouter().addHandler(iqDispatcher);
}
Also used : IQHandler(org.jivesoftware.openfire.handler.IQHandler) MultiUserChatManager(org.jivesoftware.openfire.muc.MultiUserChatManager) MultiUserChatService(org.jivesoftware.openfire.muc.MultiUserChatService) ServerFeaturesProvider(org.jivesoftware.openfire.disco.ServerFeaturesProvider) UnauthorizedException(org.jivesoftware.openfire.auth.UnauthorizedException)

Example 3 with MultiUserChatManager

use of org.jivesoftware.openfire.muc.MultiUserChatManager in project Openfire by igniterealtime.

the class AbstractXepSupport method stop.

public void stop() {
    IQRouter iqRouter = server.getIQRouter();
    IQDiscoInfoHandler iqDiscoInfoHandler = server.getIQDiscoInfoHandler();
    for (IQHandler iqHandler : iqHandlers) {
        element2Handlers.remove(iqHandler.getInfo().getName());
        try {
            iqHandler.stop();
            iqHandler.destroy();
        } catch (Exception e) {
            Log.warn("Unable to stop and destroy " + iqHandler.getClass());
        }
        if (iqHandler instanceof ServerFeaturesProvider) {
            for (Iterator<String> i = ((ServerFeaturesProvider) iqHandler).getFeatures(); i.hasNext(); ) {
                if (iqDiscoInfoHandler != null) {
                    iqDiscoInfoHandler.removeServerFeature(i.next());
                }
            }
        }
        if (muc) {
            MultiUserChatManager manager = server.getMultiUserChatManager();
            for (MultiUserChatService mucService : manager.getMultiUserChatServices()) {
                mucService.removeIQHandler(iqHandler);
                mucService.removeExtraFeature(namespace);
            }
        }
    }
    if (iqRouter != null) {
        iqRouter.removeHandler(iqDispatcher);
    }
}
Also used : IQDiscoInfoHandler(org.jivesoftware.openfire.disco.IQDiscoInfoHandler) IQHandler(org.jivesoftware.openfire.handler.IQHandler) MultiUserChatManager(org.jivesoftware.openfire.muc.MultiUserChatManager) MultiUserChatService(org.jivesoftware.openfire.muc.MultiUserChatService) ServerFeaturesProvider(org.jivesoftware.openfire.disco.ServerFeaturesProvider) IQRouter(org.jivesoftware.openfire.IQRouter) UnauthorizedException(org.jivesoftware.openfire.auth.UnauthorizedException)

Aggregations

MultiUserChatManager (org.jivesoftware.openfire.muc.MultiUserChatManager)3 MultiUserChatService (org.jivesoftware.openfire.muc.MultiUserChatService)3 UnauthorizedException (org.jivesoftware.openfire.auth.UnauthorizedException)2 ServerFeaturesProvider (org.jivesoftware.openfire.disco.ServerFeaturesProvider)2 IQHandler (org.jivesoftware.openfire.handler.IQHandler)2 ArchivedMessage (com.reucon.openfire.plugin.archive.model.ArchivedMessage)1 XmppResultSet (com.reucon.openfire.plugin.archive.xep0059.XmppResultSet)1 Connection (java.sql.Connection)1 PreparedStatement (java.sql.PreparedStatement)1 ResultSet (java.sql.ResultSet)1 SQLException (java.sql.SQLException)1 Date (java.util.Date)1 LinkedList (java.util.LinkedList)1 IQRouter (org.jivesoftware.openfire.IQRouter)1 IQDiscoInfoHandler (org.jivesoftware.openfire.disco.IQDiscoInfoHandler)1 MUCRoom (org.jivesoftware.openfire.muc.MUCRoom)1 JID (org.xmpp.packet.JID)1 Message (org.xmpp.packet.Message)1