⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 bsmessengerbean.java

📁 一款即时通讯软件
💻 JAVA
📖 第 1 页 / 共 2 页
字号:
package edu.ou.kmi.buddyspace.core;

/*
 * BSMessengerBean.java
 *
 * Project: BuddySpace
 * (C) Copyright Knowledge Media Institute 2002
 *
 *
 * Created on 18 July 2002, 18:31
 */

import java.util.*;

import org.jabber.jabberbeans.*;
import org.jabber.jabberbeans.util.*;
import org.jabber.jabberbeans.Extension.*;

import edu.ou.kmi.buddyspace.xml.*;
import edu.ou.kmi.buddyspace.utils.*;

/**
 * <code>BSMessengerBean</code> provides message handling.
 * It relies on <code>BSConnectionBean</code>, which must be set after each
 * reconnection. It uses <code>MessengerBean</code>.
 *
 * @author  Jiri Komzak, Knowledge Media Institute, Open University, United Kingdom
 */
public class BSMessengerBean implements PacketListener {
    //??? protected Hashtable chatThreads = null;
    private MessengerBean msgBean = null;
    private String name = "Messanger";
    private Hashtable unreadMsgs = null;
    private Hashtable msgTimeStamps = null;
    private Vector msgListeners = null;
    private Vector blockedJIDs = null;
    private boolean blockGroupChat = false;
    
    public static final String PLAIN_MESSAGE = "plain";
    
    /**
     * Constructor
     */
    BSMessengerBean() {
        //chatThreads = new Hashtable();
        unreadMsgs = new Hashtable();
        msgTimeStamps = new Hashtable();
        msgListeners = new Vector();
        blockedJIDs = new Vector();
    }
    
    /**
     * Constructor, which sets existing and connected <code>ConnectionBean</code>.
     * Then <code>MessengerBean</code> is created and this is registered
     * as listener for message packets.
     */
    BSMessengerBean(ConnectionBean connection) {
        this();
        setConnection(connection);
    }
    
    /**
     * Sets existing and connected <code>ConnectionBean</code>.
     * Then <code>MessengerBean</code> is created and this is registered
     * as listener for message packets.
     */
    protected void setConnection(ConnectionBean connection) {
        if (msgBean != null)
            msgBean.delPacketListener(this);
        msgBean = new MessengerBean(connection);
        if (msgBean != null)
            msgBean.addPacketListener(this);
    }
    
    /**
     * Returns currently used <code>ConnectionBean</code>.
     */
    protected ConnectionBean getConnection() {
        return (msgBean != null)? msgBean.getConnection() : null;
    }
    
    /**
     * Returns currently used <code>MessengerBean</code>.
     */
    public MessengerBean getMessengerBean() {
        return msgBean;
    }
    
    /**
     * Frees all object bindings to allow object destroy
     */
    protected void prepareToDestroy() {
        if (msgBean != null)
            msgBean.delPacketListener(this);
        removeAllMessageListeners();
        msgBean = null;
    }
    
    /**
     * Sends given message packet.
     */
    public boolean sendMessage(Message msg) {
        if (msg == null) {
            fireMessageError(null, "---", "Null message cannot be sent");
            return false;
        }
        if (msgBean == null || msgBean.getConnection() == null) {
            fireMessageError(msg.getToAddress(), "---", "Not connected");
            return false;
        }
            
        msgBean.getConnection().send(msg);
        return true;
    }
    
    /**
     * Sends message of given <code>type</code> with <code>subject</code>, 
     * <code>body</code> and <code>thread</code> to given <code>jid</code>.
     * Returns if sending was successfull.
     */
    public boolean sendMessage(JID jid, String type, String subject, String body,
                               String thread) {
        MessageBuilder msgBuilder = new MessageBuilder();
        if (body != null)
            msgBuilder.setBody(body);
        if (jid != null)
            msgBuilder.setToAddress(jid);
        if (subject != null)
            msgBuilder.setSubject(subject);
        if (type != null && (type.equals("chat") || type.equals("groupchat") ||
            type.equals("headline")))
            msgBuilder.setType(type);
        if (thread != null)
            msgBuilder.setThread(thread);
        try {
            //msgBean.send((Message)msgBuilder.build());
            if (!sendMessage((Message)msgBuilder.build()))
                return false;
        } catch (InstantiationException e) {
            //BSCore.logEvent(name, "error: message builder failed");
            fireMessageError(jid, "---", "Message builder failed");
            return false;
        }
        
        return true;
    }
    
    
    /**
     * Sends message of given <code>type</code> with embedded URL.
     * Returns sent <code>Message</code> packet - if failed returns null.
     */
    public Message sendURLMessage(JID jid, String type, String subject, String body,
                               String thread, String url, String urlDesc) {
        MessageBuilder msgBuilder = new MessageBuilder();
        if (body != null)
            msgBuilder.setBody(body);
        if (jid != null)
            msgBuilder.setToAddress(jid);
        if (subject != null)
            msgBuilder.setSubject(subject);
        if (type != null && (type.equals("chat") || type.equals("groupchat") ||
            type.equals("headline")))
            msgBuilder.setType(type);
        if (thread != null)
            msgBuilder.setThread(thread);
        
        OOBBuilder oobBuilder = new OOBBuilder();
        oobBuilder.setDescription(urlDesc);
        oobBuilder.setURL(url);
        oobBuilder.setIQ(false);
        
        msgBuilder.addExtension(oobBuilder.build());
        
        Message msg;
        try {
            msg = (Message)msgBuilder.build();
            //msgBean.send((Message)msgBuilder.build());
            if (!sendMessage(msg))
                return null;
        } catch (InstantiationException e) {
            //BSCore.logEvent(name, "error: message builder failed");
            fireMessageError(jid, "---", "Message builder failed");
            return null;
        }
        
        return msg;
    }
    
    
    /**
     * Sends plain message with given <code>body</code> and 
     * <code>subject</code> to given <code>jid</code>.
     * Returns if sending was successfull.
     */
    public boolean sendMessage(JID jid, String body, String subject) {
        return sendMessage(jid, null, subject, body, null);
    }
    
    /**
     * Sends chat message with given <code>body</code> and <code>thread</code>
     * to given <code>jid</code>.
     * Returns if sending was successfull.
     */
    public boolean sendChatMessage(JID jid, String body, String thread) {
        return sendMessage(jid, "chat", null, body, thread);
    }
    
    /**
     * Returns the <code>Enumeration</code> of message packets for given 
     * <code>jid</code> (or all JIDs if <code>jid</code> is null) of any from
     * given <code>types</code>. Plain message type is specified by 
     * <code>BSMessengerBean.PLAIN_MESSAGE</code>.
     */
    public Enumeration getMessagePackets(JID jid, Vector types) {
        Vector msgs = new Vector();
        
        if (types == null || types.isEmpty()) return msgs.elements();
        
        // if messages for all jids
        if (jid == null) {
            Enumeration jids = unreadMsgs.elements();
            // for all jids
            while (jids.hasMoreElements()) {
                Vector jidsMsgs = (Vector) jids.nextElement();
                Enumeration msgsEnum = jidsMsgs.elements();
                // for all jid's messages
                while (msgsEnum.hasMoreElements()) {
                    Message m = (Message) msgsEnum.nextElement();
                    if ((m.getType() == null && types.contains(PLAIN_MESSAGE)) ||
                        ("normal".equals(m.getType()) && types.contains(PLAIN_MESSAGE)) ||
                        ("".equals(m.getType()) && types.contains(PLAIN_MESSAGE)) ||
                        (m.getType() != null && types.contains(m.getType())))
                        msgs.add(m);
                }
            }
            return msgs.elements();
        }
        
        // else jid specified
        //String str = BSPresenceBean.getJIDHashString(jid, false);
        JID key = jid.getSimpleJID();
        //Vector jidsMsgs = (Vector) unreadMsgs.get(str);
        Vector jidsMsgs = (Vector) unreadMsgs.get(key);
        
        // if no messages
        if (jidsMsgs == null || jidsMsgs.isEmpty()) return msgs.elements();
        
        Enumeration msgsEnum = jidsMsgs.elements();
        // for all jid's messages
        while (msgsEnum.hasMoreElements()) {
            Message m = (Message) msgsEnum.nextElement();
            if ((jid.getResource() == null || jid.equals(m.getFromAddress())) &&
                (m.getType() == null && types.contains(PLAIN_MESSAGE)) ||
                ("normal".equals(m.getType()) && types.contains(PLAIN_MESSAGE)) ||
                ("".equals(m.getType()) && types.contains(PLAIN_MESSAGE)) ||
                (m.getType() != null && types.contains(m.getType())))
                msgs.add(m);
        }
        
        return msgs.elements();
    }
    
    /**
     * Returns the <code>Enumeration</code> of message packets of 
     * given <code>type</code>.
     */
    public Enumeration getMessagePackets(String type) {
        Vector types = new Vector();
        if (type == null || type.equals(""))
            types.add(PLAIN_MESSAGE);
        else
            types.add(type);
            
        return getMessagePackets(null, types);
    }
    
    /**
     * Returns the <code>Enumeration</code> of message packets for given 
     * <code>jid</code> (or all JIDs if <code>jid</code> is null) of 
     * given <code>type</code>.
     */
    public Enumeration getMessagePackets(JID jid, String type) {
        Vector types = new Vector();
        if (type == null || type.equals(""))
            types.add(PLAIN_MESSAGE);
        else
            types.add(type);
            
        return getMessagePackets(jid, types);
    }
    
    /**
     * Returns the <code>Enumeration</code> of message packets for given 
     * <code>jid</code> (or all JIDs if <code>jid</code> is null) of any type.
     */
    public Enumeration getMessagePackets(JID jid) {
        Vector types = new Vector();
        types.add(PLAIN_MESSAGE);
        types.add(new String("chat"));
        types.add(new String("headline"));
        types.add(new String("groupchat"));
        
        return getMessagePackets(jid, types);
    }
    
    /** Returns time-stamp when the given message was received */
    public Date getTimeStamp(Message msg) {
        return (Date) msgTimeStamps.get(msg);
    }
    
    /**
     * Returns the oldest unread message body for given <code>jid</code> and
     * removes the message from list of unread. Then it notifies listeners
     * that the message was read.
     */
    public String popFirstMessage(JID jid) {
        Message msg = popFirstMessagePacket(jid);

        //return new String("<" + jid.toString() + "> " + msg.getBody());
        if (msg == null) return null;
        String body = msg.getBody();
        return (body != null)? new String(body) : new String("");
    }
    
    /**
     * Returns the oldest unread message packet for given <code>jid</code> and
     * removes the message from list of unread. Then it notifies listeners
     * that the message was read.
     */
    public Message popFirstMessagePacket(JID jid) {
        if (jid == null) return null;
        
        //String str = BSPresenceBean.getJIDHashString(jid, false);
        JID key = jid.getSimpleJID();
        //Vector jidsMsgs = (Vector) unreadMsgs.get(str);
        Vector jidsMsgs = (Vector) unreadMsgs.get(key);
        
        if (jidsMsgs == null || jidsMsgs.isEmpty()) return null;
        
        Message msg = null;
        
        // if no resource
        if (jid.getResource() == null || jid.getResource().equals("")) {
            try {
                msg = (Message) jidsMsgs.firstElement();
            } catch (NoSuchElementException e) {
                msg = null;
            }
        }
        // else some resource
        else {
            Enumeration msgs = jidsMsgs.elements();
            while (msgs.hasMoreElements()) {
                Message m = (Message) msgs.nextElement();
                if (jid.equals(m.getFromAddress()))
                    msg = m;
            }
        }
        
        // removes the read message
        jidsMsgs.remove(msg);
        fireMessageRead(msg);

        return msg;
    }
    
    /**
     * Deletes given message from unread messages and fires messageRead event.
     */
    public void deleteMessagePacket(Message msg) {
        JID jid;

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -