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

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


/**
 * Defines the commands that a server issues to welcome us.  These are
 * identified with 001, 002... etc.  These commands are only received
 * after we register, unlike the NOTICE command.
 */
public class WelcomeCommand extends AbstractInCommand
{
    static Logger log = Logger.getLogger(WelcomeCommand.class.getName());

    private String notice;
    private String nick;

    /** Factory */
    public WelcomeCommand()
    {
        this( null, null );
    }

    /**
     * Used by parse to create an instance of WelcomeCommand.
     *
     * @param nick Nick that send the welcome
     * @param notice Notice that was sent
     * */
    public WelcomeCommand( String nick, String notice )
    {
        this.notice = notice;
        this.nick = nick;
        //log.debug("WelcomeCommand: Nick is: `" + nick + "'");
        //log.debug("WelcomeCommand: Notice is: `"+notice+"'");
    }

    /**
     * Parses a string and produces a formed command object, if it can.
     * Should return null if it cannot form the command object.
     */
    public InCommand parse( String prefix, String identifier, String params )
    {
        ParameterIterator pi = new ParameterIterator( params );
        String nick = pi.next().toString();
        String notice;
        if( pi.hasNext() )
        {
            // We are looking at a "nick :msg" pair
            notice = pi.next().toString();
        }
        else
        {
            // There is only one parameter, a notice.
            notice = nick;
            nick = null;
        }
        if( pi.hasNext() )
        {
            //log.severe("WelcomeCommand: More than two parameters, confused.");
        }


        //String str = getParameter( params, 0 );
        //
        return new WelcomeCommand( nick, notice );
    }

    /**
     * Sets the nick of the client state, if there is one included with
     * this command.
     */
    public boolean updateClientState( ClientState state )
    {
        //log.debug("WelcomeCommand: updated client state with: " + new FullNick( nick ));
        state.setNick( new FullNick( nick ) );

        return true;
    }

    /**
     * Returns the string IRC uses to identify this command.  Examples:
     * NICK, PING, KILL, 332.  In our case, there is no one thing.
     */
    public String getIrcIdentifier()
    {
        return "001";
    }

    public void selfRegister( CommandRegister commandRegister )
    {
        commandRegister.addCommand( "001", this );
        commandRegister.addCommand( "002", this );
        commandRegister.addCommand( "003", this );
        commandRegister.addCommand( "004", this );
        commandRegister.addCommand( "005", this );
    }

    public String getNotice()
    {
        return notice;
    }

    /**
     * @return the nick received with this command, or null if there isn't
     * one.
     * */
    public String getNick()
    {
        return nick;
    }

    public String toString()
    {
        return "WelcomeCommand";
    }

}