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 org.jivesoftware.smack.*;
024    import org.jivesoftware.smack.packet.*;
025    
026    import com.valhalla.gui.*;
027    import com.valhalla.jbother.*;
028    
029    import java.io.*;
030    
031    /**
032     * Listens for and responds to jabber:iq:version requests
033     *
034     * @author Adam Olsen
035     * @version 1.0
036     * @see <code>com.valhalla.jbother.jabber.smack.Version</code>
037    */
038    public class VersionListener implements PacketListener
039    {
040            /**
041             * Event listener called when a Version packet is recieved
042             * @param message the version packet recieved
043            */
044            public void processPacket( Packet message )
045            {
046                    if( !( message instanceof Version ) || ((IQ)message).getType() != IQ.Type.GET ) return;
047                    Version version = (Version)message;
048    
049                    String from = version.getFrom();
050                    String to = version.getTo();
051    
052                    String jbVersion = JBother.JBOTHER_VERSION;
053    
054                    if( jbVersion.toLowerCase().indexOf( "cvs" ) >= 0 )
055                    {
056                            ResourceBundle bundle = ResourceBundle.getBundle( "buildid" );
057                            jbVersion += " [BID:" + bundle.getString( "build.number" ) + "]";
058                    }
059    
060                    // set up the version response
061                    version.setType( IQ.Type.RESULT );
062                    version.setFrom( to );
063                    version.setTo( from );
064                    version.setName( "JBother" );
065                    version.setVersion( jbVersion + " / Java " + System.getProperty( "java.version" ) );
066    
067                    // if the machine is a linux machine, see if we can find out what distro
068                    String osName = System.getProperty( "os.name" );
069                    if( osName.equals( "Linux" ) )
070                    {
071                            osName = getDistro();
072                            if( osName.equals( "Linux" ) ) osName += " " + System.getProperty( "os.version" );
073                    }
074                    else osName += " " + System.getProperty( "os.version" );
075    
076                    version.setOs( osName + " [" + System.getProperty( "os.arch" ) + "]" );
077    
078                    // send the response
079                    if( BuddyList.getInstance().checkConnection() )
080                            BuddyList.getInstance().getConnection().sendPacket( version );
081            }
082    
083            /**
084             * Attempts to determine the Linux distrobution
085            */
086            private String getDistro()
087            {
088                    File etc = new File( "/etc" );
089                    if( !etc.isDirectory() ) return "Linux";
090    
091                    String[] files = etc.list();
092                    File distroFile = null;
093                    Pattern p = Pattern.compile( "^([^_-]*)([-_])(version|release)$" );
094    
095                    // finds the name of every file in /etc - if it matches the above
096                    // regex, read it in and we call that the distro
097                    for( int i = 0; i < files.length; i++ )
098                    {
099                            String name = files[i];
100                            Matcher m = p.matcher( name );
101                            if( m.matches() )
102                            {
103                                    distroFile = new File( etc, m.group( 1 ) + m.group( 2 ) + m.group( 3 ) );
104                            }
105                    }
106    
107                    if( distroFile == null ) return "Linux";
108                    String line = "Linux";
109    
110                    try {
111                            BufferedReader in = new BufferedReader( new FileReader( distroFile ) );
112    
113                            line = in.readLine();
114    
115                            in.close();
116                    }
117                    catch( IOException e )
118                    {
119                            return "Linux";
120                    }
121    
122                    return line;
123            }
124    }