Search in sources :

Example 1 with ArchivedMessage

use of com.reucon.openfire.plugin.archive.model.ArchivedMessage in project Openfire by igniterealtime.

the class ArchiveFactory method createArchivedMessage.

public static ArchivedMessage createArchivedMessage(Session session, Message message, ArchivedMessage.Direction direction, JID withJid) {
    final ArchivedMessage archivedMessage;
    archivedMessage = new ArchivedMessage(new Date(), direction, message.getType().toString(), withJid);
    archivedMessage.setSubject(message.getSubject());
    archivedMessage.setBody(message.getBody());
    return archivedMessage;
}
Also used : ArchivedMessage(com.reucon.openfire.plugin.archive.model.ArchivedMessage) Date(java.util.Date)

Example 2 with ArchivedMessage

use of com.reucon.openfire.plugin.archive.model.ArchivedMessage in project Openfire by igniterealtime.

the class ArchiveManagerImpl method archiveMessage.

public void archiveMessage(Session session, Message message, boolean incoming) {
    final XMPPServer server = XMPPServer.getInstance();
    final ArchivedMessage.Direction direction;
    final ArchivedMessage archivedMessage;
    final Conversation conversation;
    final JID ownerJid;
    final JID withJid;
    // TODO support groupchat
    if (message.getType() != Message.Type.chat && message.getType() != Message.Type.normal) {
        return;
    }
    if (server.isLocal(message.getFrom()) && incoming) {
        ownerJid = message.getFrom();
        withJid = message.getTo();
        // sent by the owner => to
        direction = ArchivedMessage.Direction.to;
    } else if (server.isLocal(message.getTo()) && !incoming) {
        ownerJid = message.getTo();
        withJid = message.getFrom();
        // received by the owner => from
        direction = ArchivedMessage.Direction.from;
    } else {
        return;
    }
    archivedMessage = ArchiveFactory.createArchivedMessage(session, message, direction, withJid);
    if (archivedMessage.isEmpty()) {
        return;
    }
    conversation = determineConversation(ownerJid, withJid, message.getSubject(), message.getThread(), archivedMessage);
    archivedMessage.setConversation(conversation);
    persistenceManager.createMessage(archivedMessage);
    if (indexManager != null) {
        indexManager.indexObject(archivedMessage);
    }
}
Also used : XMPPServer(org.jivesoftware.openfire.XMPPServer) JID(org.xmpp.packet.JID) ArchivedMessage(com.reucon.openfire.plugin.archive.model.ArchivedMessage) Conversation(com.reucon.openfire.plugin.archive.model.Conversation)

Example 3 with ArchivedMessage

use of com.reucon.openfire.plugin.archive.model.ArchivedMessage in project Openfire by igniterealtime.

the class JdbcPersistenceManager method extractMessage.

private ArchivedMessage extractMessage(ResultSet rs) throws SQLException {
    final ArchivedMessage message;
    Date time = millisToDate(rs.getLong("sentDate"));
    Direction direction = getDirection(rs);
    String type = null;
    String subject = null;
    String body = rs.getString("body");
    String bareJid = rs.getString("bareJID");
    JID withJid = null;
    if (Direction.from == direction) {
        withJid = new JID(rs.getString("fromJID"));
    }
    message = new ArchivedMessage(time, direction, null, withJid);
    // message.setId(id);
    // message.setSubject(subject);
    message.setBody(body);
    return message;
}
Also used : JID(org.xmpp.packet.JID) ArchivedMessage(com.reucon.openfire.plugin.archive.model.ArchivedMessage) Direction(com.reucon.openfire.plugin.archive.model.ArchivedMessage.Direction) Date(java.util.Date)

Example 4 with ArchivedMessage

use of com.reucon.openfire.plugin.archive.model.ArchivedMessage in project Openfire by igniterealtime.

the class JdbcPersistenceManager method getConversation.

private Conversation getConversation(Long conversationId, String ownerJid, String withJid, Date start) {
    Conversation conversation = null;
    StringBuilder querySB;
    Connection con = null;
    PreparedStatement pstmt = null;
    ResultSet rs = null;
    querySB = new StringBuilder(SELECT_CONVERSATIONS);
    querySB.append(" WHERE ");
    if (conversationId != null) {
        querySB.append(CONVERSATION_ID).append(" = ? ");
    } else {
        querySB.append(CONVERSATION_OWNER_JID).append(" = ?");
        if (withJid != null) {
            querySB.append(" AND ");
            querySB.append(CONVERSATION_WITH_JID).append(" = ? ");
        }
        if (start != null) {
            querySB.append(" AND ");
            querySB.append(CONVERSATION_START_TIME).append(" = ? ");
        }
    }
    querySB.append(SELECT_CONVERSATIONS_GROUP_BY);
    try {
        con = DbConnectionManager.getConnection();
        pstmt = con.prepareStatement(querySB.toString());
        int i = 1;
        if (conversationId != null) {
            pstmt.setLong(1, conversationId);
        } else {
            pstmt.setString(i++, ownerJid);
            if (withJid != null) {
                pstmt.setString(i++, withJid);
            }
            if (start != null) {
                pstmt.setLong(i++, dateToMillis(start));
            }
        }
        rs = pstmt.executeQuery();
        Log.debug("getConversation: SELECT_CONVERSATIONS: " + pstmt.toString());
        if (rs.next()) {
            conversation = extractConversation(rs);
        } else {
            return null;
        }
        rs.close();
        pstmt.close();
        pstmt = con.prepareStatement(SELECT_PARTICIPANTS_BY_CONVERSATION);
        pstmt.setLong(1, conversation.getId());
        rs = pstmt.executeQuery();
        Log.debug("getConversation: SELECT_PARTICIPANTS_BY_CONVERSATION: " + pstmt.toString());
        while (rs.next()) {
            for (Participant participant : extractParticipant(rs)) {
                conversation.addParticipant(participant);
            }
        }
        rs.close();
        pstmt.close();
        pstmt = con.prepareStatement(SELECT_MESSAGES_BY_CONVERSATION);
        pstmt.setLong(1, conversation.getId());
        pstmt.setString(2, conversation.getOwnerJid());
        rs = pstmt.executeQuery();
        Log.debug("getConversation: SELECT_MESSAGES_BY_CONVERSATION: " + pstmt.toString());
        while (rs.next()) {
            ArchivedMessage message;
            message = extractMessage(rs);
            message.setConversation(conversation);
            conversation.addMessage(message);
        }
    } catch (SQLException sqle) {
        Log.error("Error selecting conversation", sqle);
    } finally {
        DbConnectionManager.closeConnection(rs, pstmt, con);
    }
    return conversation;
}
Also used : Participant(com.reucon.openfire.plugin.archive.model.Participant) ArchivedMessage(com.reucon.openfire.plugin.archive.model.ArchivedMessage) SQLException(java.sql.SQLException) Connection(java.sql.Connection) XmppResultSet(com.reucon.openfire.plugin.archive.xep0059.XmppResultSet) ResultSet(java.sql.ResultSet) Conversation(com.reucon.openfire.plugin.archive.model.Conversation) PreparedStatement(java.sql.PreparedStatement)

Example 5 with ArchivedMessage

use of com.reucon.openfire.plugin.archive.model.ArchivedMessage 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)

Aggregations

ArchivedMessage (com.reucon.openfire.plugin.archive.model.ArchivedMessage)9 XmppResultSet (com.reucon.openfire.plugin.archive.xep0059.XmppResultSet)4 Date (java.util.Date)4 Conversation (com.reucon.openfire.plugin.archive.model.Conversation)3 Connection (java.sql.Connection)3 PreparedStatement (java.sql.PreparedStatement)3 ResultSet (java.sql.ResultSet)3 SQLException (java.sql.SQLException)3 JID (org.xmpp.packet.JID)3 MUCRoom (org.jivesoftware.openfire.muc.MUCRoom)2 MultiUserChatService (org.jivesoftware.openfire.muc.MultiUserChatService)2 Direction (com.reucon.openfire.plugin.archive.model.ArchivedMessage.Direction)1 Participant (com.reucon.openfire.plugin.archive.model.Participant)1 Statement (java.sql.Statement)1 LinkedList (java.util.LinkedList)1 TreeMap (java.util.TreeMap)1 Element (org.dom4j.Element)1 XMPPServer (org.jivesoftware.openfire.XMPPServer)1 Forwarded (org.jivesoftware.openfire.forward.Forwarded)1 MUCRole (org.jivesoftware.openfire.muc.MUCRole)1