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

import java.util.List;
import java.util.Arrays;

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

public class NamesReply extends GenericReply
{
    static Logger log = Logger.getLogger(NamesReply.class.getName());

    private List<String> names;
    private String channel;

    /** For use as a factory. */
    public NamesReply()
    {
    }

    public NamesReply( String channel, List<String> names )
    {
        this.names = names;
        this.channel = channel;
    }

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

    public InCommand parse( String prefix, String identifier, String params )
    {
        return new NamesReply( getParameter( params, 2 ), Arrays.asList(getParameter( params, 3 ).split(" ")) );
    }

    /**
     * Adds the list of names to the client state.
     */
    public boolean updateClientState( ClientState state )
    {
        boolean stateChanged = false;

        // 1) Get the Channel
        Channel channelObj = state.getChannel( channel );

        if( channel == null )
        {
            log.severe("NamesReply: Channel is null");
            return false;
        }

        if( channelObj == null )
        {
            log.severe("NamesReply: No channel object for channel: " + channel);
            return false;
        }


        // 2) Parse out names
        for (String nick : names) {
            // 3) Check that the user is not already in the list
            if( !channelObj.isMemberInChannel( nick ) )
            {
                channelObj.addMember( nick, this );
                stateChanged = true;
            }
        }

        return stateChanged;
    }

    public List<String> getNames()
    {
        return names;
    }

    public String getChannel()
    {
        return channel;
    }

}