summaryrefslogtreecommitdiffstats
path: root/EssentialsUpdate/src/f00f/net/irc/martyr/clientstate/ClientState.java
blob: c508463d9c29862f001d834b4baa23b1eea9e847 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
/*
 * Original version: Ben Damm <bdamm@dammfine.com>
 * Changes by: mog
 *	- Added isOnChannel
 *
 */
package f00f.net.irc.martyr.clientstate;

import java.util.Enumeration;
import java.util.Hashtable;

import f00f.net.irc.martyr.util.FullNick;
//import org.apache.log4j.Logger;

/**
 * <p>Maintains a list of client-related facts such as what channels we
 * are in, who else is in the channels, what our nick is, etc.</p>
 *
 * <p>ClientState is a critical part of martyr.  To get access to events
 * that change the client state, the framework user can subclass
 * ClientState and then pass the subclass to the IRCConnection's
 * constructor.  Then, when a command detects a change in client
 * state, it will call the corresponding method in the custom
 * ClientState.</p>
 *
 * <p>If a user of the framework wishes to grab client state information
 * about a channel (when a user joins, when a user leaves, topic
 * change, etc), the user can do so in a similar manner.  Simply
 * override the 'addChannel(String)' method to instantiate their own
 * Channel subclass, and call the protected 'addChannel' method.  See
 * the addChannel method for an example.
 * </p>
 *
 */
public class ClientState
{

    //static Logger log = Logger.getLogger(ClientState.class);

    private FullNick nick = null;
    private String user = "";
    private String name = "";
    private String pass = null;
    private String server = "";
    private int port = -1;

    // Hashtable is threadsafe so we don't have to be.
    protected Hashtable<String,Channel> channels = new Hashtable<String,Channel>();

    public void setNick( FullNick nick )
    {
        if( nick == null )
        {
            //log.debug("ClientState: Set nick to null");
        }
        else
        {
            //log.debug("ClientState: Set nick to \"" + nick + "\"");
        }
        this.nick = nick;
    }

    public FullNick getNick()
    {
        return nick;
    }

    public void setUser( String user )
    {
        this.user = user;
    }

    /**
     * @return the username that was used to register.
     * */
    public String getUser()
    {
        return user;
    }

    public void setName( String name )
    {
        this.name = name;
    }

    /**
     * @return the name (any arbitrary string) that was used to register.
     * */
    public String getName()
    {
        return name;
    }

    /**
     * @return the password that was used to register.
     */
    public String getPass()
    {
        return pass;
    }

    public void setPass(String pass)
    {
        this.pass = pass;
    }

    public void setServer( String server )
    {
        this.server = server;
    }

    public String getServer()
    {
        return server;
    }

    public void setPort( int port )
    {
        this.port = port;
    }

    public int getPort()
    {
        return port;
    }

    /**
     * <p>Adds a channel to the list of channels we know about.  If you
     * want to supply your own Channel object, override this method
     * with:</p>
     * <pre>
     * public void addChannel( String channame )
     * {
     *     addChannel( new MyChannel( channame ) );
     * }
     * </pre>
     *
     * @param channame Channel to add to list of channels
     */
    public void addChannel( String channame )
    {
        addChannel( new Channel( channame ) );
    }

    protected void addChannel( Channel channel )
    {
        //log.debug("ClientState: Channel added: " + channel.getName());
        channels.put( channel.getName().toLowerCase(), channel );
    }

    public Channel getChannel( String chanName )
    {
        return channels.get( chanName.toLowerCase() );
    }

    /**
     * Removes a channel from the state, does nothing if the channel name
     * is invalid.
     * Should we throw an exception here?
     *
     * @param channel Channel to remove from list
     */
    public void removeChannel( String channel )
    {
        //log.debug("ClientState: Channel removed: " + channel);
        channels.remove( channel.toLowerCase() );
    }

    public boolean isOnChannel( String channel )
    {
        for (Enumeration iter = getChannelNames(); iter.hasMoreElements();)
        {
            if(channel.equalsIgnoreCase((String) iter.nextElement()))
            {
                return true;
            }
        }
        return false;
    }

    public Enumeration getChannelNames()
    {
        return channels.keys();
    }

    public Enumeration getChannels()
    {
        return channels.elements();
    }

}