001    /*
002            Copyright (C) 2003 Adam Olsen
003    
004            This program is free software; you can redistribute it and/or modify
005            it under the terms of the GNU General Public License as published by
006            the Free Software Foundation; either version 1, or (at your option)
007            any later version.
008    
009            This program is distributed in the hope that it will be useful,
010            but WITHOUT ANY WARRANTY; without even the implied warranty of
011            MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
012            GNU General Public License for more details.
013    
014            You should have received a copy of the GNU General Public License
015            along with this program; if not, write to the Free Software
016            Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
017    */
018    
019    package com.valhalla.jbother.jabber.smack;
020    
021    import java.util.*;
022    import java.util.regex.*;
023    import javax.swing.*;
024    import org.jivesoftware.smack.PacketListener;
025    import org.jivesoftware.smack.RosterEntry;
026    import org.jivesoftware.smack.packet.Message;
027    import org.jivesoftware.smack.packet.Packet;
028    
029    import com.valhalla.jbother.*;
030    import com.valhalla.jbother.groupchat.*;
031    import com.valhalla.jbother.jabber.*;
032    
033    /**
034     * Listens for a message packet, and sends it to the appropriate buddies
035     * conversation window.  If the window does not exist a new window will be created
036     *
037     * @author Adam Olsen
038     * @version 1.0
039     */
040    
041    public class MessagePacketListener implements PacketListener
042    {
043            private Hashtable buddyStatuses;  //all the available buddy statuses
044    
045            /**
046             * Constructor for the packet listener
047             */
048            public MessagePacketListener()
049            {
050                    buddyStatuses = BuddyList.getInstance().getBuddyStatuses();
051            }
052    
053            /**
054             * Processes the message packet
055            */
056            public void processPacket( Packet message )
057            {
058                    final Message packet = (Message)message;
059    
060                    if( packet.getType() == Message.Type.NORMAL )
061                    {
062                            if( packet.getBody() == null ) return;
063    
064                            ParsedBuddyInfo info = new ParsedBuddyInfo( packet.getFrom() );
065                            String userId = info.getUserId().toLowerCase();
066                            if( BuddyList.getInstance().getBlockedUsers().containsKey( userId ) ) return;
067    
068                            SwingUtilities.invokeLater( new Runnable()
069                            {
070                                    public void run()
071                                    {
072                                            MessagePanel window = new MessagePanel();
073                                            window.recieveMessage( packet );
074                                    }
075                            } );
076    
077                            return;
078                    }
079    
080                    if( packet.getType() != Message.Type.CHAT && packet.getType() != Message.Type.HEADLINE ) return;
081    
082                    String from = packet.getFrom();
083    
084                    //check to see if it's a private message
085                    if( BuddyList.getInstance().getTabFrame() != null &&
086                                    BuddyList.getInstance().getTabFrame().isRoomOpen( from.replaceAll( "\\/.*", "" ) ) )
087                    {
088                            ChatRoomPanel window = BuddyList.getInstance().getTabFrame().getChatPanel( from.replaceAll( "\\/.*", "" ) );
089                            if( window != null ) initiatePMSession( window, packet );
090                            return;
091                    }
092    
093                    if( from != null )
094                    {
095                            ParsedBuddyInfo info = new ParsedBuddyInfo( from );
096                            String userId = info.getUserId().toLowerCase();
097                            final String resource = info.getResource();
098                            String server = info.getServer();
099                            from = info.getBareAddress();
100    
101                            final BuddyStatus buddyStatus = BuddyList.getInstance().getBuddyStatus( userId );
102                            boolean inList = true;
103    
104                            if( BuddyList.getInstance().getBlockedUsers().containsKey( userId ) )
105                            {
106                                    com.valhalla.Logger.debug( "Blocking user: " + userId );
107                                    return;
108                            }
109    
110                            final String messageSbj  = packet.getSubject();
111                            final String messageBody = packet.getBody();
112    
113                            if( !BuddyList.getInstance().checkConnection() ) return;
114    
115                            // we don't want null messages to be displayed.
116                            if( ( messageBody == null ) ||
117                                    ( !BuddyList.getInstance().getBuddyListTree().getShowAgentMessages() && ( userId == from ) ) ) return;
118    
119                            RosterEntry entry = BuddyList.getInstance().getConnection().getRoster().getEntry( from );
120                            if( entry != null ) userId = entry.getName();
121    
122                            if( buddyStatus.getName() != null ) userId = buddyStatus.getName();
123    
124                            if( buddyStatus.getConversation() == null )
125                            {
126                                      if (packet.getType() == Message.Type.HEADLINE)
127                                            buddyStatus.setConversation( new HeadlinesPanel(buddyStatus) );
128                                      else
129                                            buddyStatus.setConversation( new ChatPanel(buddyStatus) );
130                            }
131    
132                            if( inList && buddyStatus.size() <= 0 )
133                                    buddyStatus.getConversation().setOfflineMessage();
134    
135                            SwingUtilities.invokeLater( new Runnable()
136                            {
137                                    public void run()
138                                    {
139                                            buddyStatus.getConversation().recieveMessage( messageSbj, messageBody, resource );
140                                    }
141                            } );
142                    }
143            }
144    
145            /**
146             * If it's a group chat packet listener, process the packet
147             * @param window the chat room window
148             * @param packet the message
149            */
150            private void initiatePMSession( ChatRoomPanel window, Message packet )
151            {
152                    BuddyStatus buddy = window.getBuddyStatus( packet.getFrom() );
153    
154                    String nick = "";
155                    String room = "";
156                    String parts[] = new String[2];
157                    parts = buddy.getUser().split( "\\/" );
158                    nick = parts[1];
159                    room = parts[0];
160    
161                    if( buddy.getConversation() == null )
162                    {
163                            ChatPanel conver = new ChatPanel( buddy );
164                            conver.setVisible( true );
165                            buddy.setConversation( conver );
166                    }
167    
168                    String messageBody = packet.getBody();
169                    if( messageBody == null ) return;
170    
171                    buddy.getConversation().recieveMessage( "", messageBody, null );
172            }
173    }