summaryrefslogtreecommitdiffstats
path: root/EssentialsUpdate/src/f00f/net/irc/martyr/commands/JoinCommand.java
blob: 32f2d1e75c677fb6c2a89fcdbff43502325c2b2c (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
package f00f.net.irc.martyr.commands;

import f00f.net.irc.martyr.InCommand;
import f00f.net.irc.martyr.clientstate.Channel;
import f00f.net.irc.martyr.clientstate.ClientState;
import f00f.net.irc.martyr.util.FullNick;
import java.util.logging.Logger;


/**
 * Defines JOIN command.
 */
public class JoinCommand extends AbstractCommand
{

    static Logger log = Logger.getLogger(JoinCommand.class.getName());

    private String channel;
    private String secret;
    private FullNick user;

    /** For use as a factory */
    public JoinCommand()
    {
        this.user = null;
        this.channel = null;
        this.secret = null;
    }

    /**
     * This constructor is used with an incoming JOIN command from the server.
     *
     * @param user User that joined the channel
     * @param channel Channel that was joined
     */
    private JoinCommand( FullNick user, String channel )
    {
        this.user = user;
        this.channel = channel;
        this.secret = null;
    }

    /**
     * This constructor is used to make a request to join a channel that
     * requires a secret key to join.
     *
     * @param channel The channel
     * @param secret The secret key required to enter the channel, or null of
     * none.
     */
    public JoinCommand( String channel, String secret )
    {
        this.secret = secret;
        this.user = null;
        this.channel = channel;
    }

    /**
     * This constructor is used to make a request to join a channel.
     *
     * @param channel Channel that will be joined
     */
    public JoinCommand( String channel )
    {
        this.secret = null;
        this.user = null;
        this.channel = channel;
    }

    public InCommand parse( String prefix, String identifier, String params )
    {
        return new JoinCommand( new FullNick( prefix ), getParameter( params, 0 ) );
    }

    public String getIrcIdentifier()
    {
        return "JOIN";
    }

    public String renderParams()
    {
        if( secret == null )
            return channel;
        else
            return channel + " " + secret;
    }

    public String getChannel()
    {
        return channel;
    }

    public String getSecret()
    {
        return secret;
    }

    public FullNick getUser()
    {
        return user;
    }

    public boolean weJoined( ClientState state )
    {
        return user.equals( state.getNick() );
    }

    public boolean updateClientState( ClientState state )
    {
        if( weJoined( state ) )
        {
            // We've joined a group.
            //log.debug("JOIN: We've joined " + channel);
            state.addChannel( channel );
            return true;
        }
        else
        {
            // Someone else joined the group.
            //log.debug("JOIN: " + user + " joined " + channel);
            // 1) Grab group
            Channel channelObj = state.getChannel( channel );
            // 2) Add user
            channelObj.addMember( user, this );
            return true;
        }
    }

}