summaryrefslogtreecommitdiffstats
path: root/EssentialsUpdate/src/f00f/net/irc/martyr/commands/NickCommand.java
blob: 6cdcb0224826d3965b5a14a633ee4c2fa519e66f (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
/*
 * Original version: Ben Damm <bdamm@dammfine.com>
 * Changes by: Mog
 * 	- added getOldNick
 * 	*/
package f00f.net.irc.martyr.commands;

import java.util.Enumeration;

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.clientstate.Member;
import f00f.net.irc.martyr.util.FullNick;

/**
 * Defines NICK command.
 */
public class NickCommand extends AbstractCommand
{

    private FullNick oldNick;
    private FullNick newNick;

    /** For use as a factory */
    public NickCommand()
    {
        this( null, null );
    }

    public NickCommand( FullNick oldNick, FullNick newNick )
    {
        this.oldNick = oldNick;
        this.newNick = newNick;
    }

    public NickCommand( String newNick )
    {
        this( null, new FullNick( newNick ) );
    }

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

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

    public String renderParams()
    {
        return getNick();
    }

    public String getNick()
    {
        return newNick.getNick();
    }

    public String getOldNick()
    {
        return oldNick.getNick();
    }

    public boolean updateClientState( ClientState state )
    {
        // Does this apply to us?
        if( oldNick.equals( state.getNick() ) )
        {
            state.setNick( newNick );
            return true;
        }
        else
        {
            // Ok, so we need to change someone's nick.
            // This needs to occur for each member with that nick in each
            // channel that we are in.  Just use Member.setNick for each
            // occurance.
            // Note: I do not believe this code has received a vigorous
            // test.
            Enumeration channels = state.getChannels();
            while( channels.hasMoreElements() )
            {
                Channel channel = (Channel)channels.nextElement();
                Member member = channel.findMember( oldNick.getNick() );
                if( member != null )
                    member.setNick( newNick );
            }
        }
        return false;
    }

}