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

import f00f.net.irc.martyr.InCommand;
import f00f.net.irc.martyr.CommandRegister;

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

/**
 * Defines the ISON command, which is used to determine if a user or a list of users is online.
 *
 * @author Daniel Henninger
 */
public class IsonCommand extends AbstractCommand
{

    public static final String IDENTIFIER_PRIMARY = "ISON";
    public static final String IDENTIFIER_SECONDARY = "303";

    /* List of nicks that we will check for online status */
    List<String> nicks = new ArrayList<String>();

    /* Destination nick */
    String dest = null;

    /**
     * No parameter passed to the ISON is not valid.  This is used for factories.
     */
    public IsonCommand()
    {
        // Nothing to do
    }

    /**
     * Check online status of a single nickname.
     *
     * @param nick Nick you want to check the online status of.
     */
    public IsonCommand(String nick)
    {
        this.nicks.add(nick);
    }

    public IsonCommand(String dest, String nick) {
        this.dest = dest;
        this.nicks.add(nick);
    }

    /**
     * Check online status of a number of nicknames.
     *
     * @param nicks List of nicks you want to check the online status of.
     */
    public IsonCommand(List<String> nicks)
    {
        this.nicks.addAll(nicks);
    }

    public IsonCommand(String dest, List<String> nicks) {
        this.dest = dest;
        this.nicks.addAll(nicks);
    }

    /**
     * @see AbstractCommand#parse(String, String, String)
     */
    public InCommand parse(String prefix, String identifier, String params)
    {
        // when the command is used as a reply, the nick is parameter 0 and the rest are parameter 1.
        if ( identifier.equals( IDENTIFIER_SECONDARY ) ) {
            String nickParam = getParameter(params, 1);
            List<String> nicks = Arrays.asList(nickParam.split(" "));
            return new IsonCommand(getParameter(params, 0), nicks);
        }
        else {
            String nickParam = getParameter(params, 0);
            List<String> nicks = Arrays.asList(nickParam.split(" "));
            return new IsonCommand(nicks);
        }
    }

    /**
     * @see f00f.net.irc.martyr.commands.AbstractCommand#renderParams()
     */
    public String renderParams()
    {
        String ret = "";
        if (nicks.size() > 0) {
            Boolean isFirst = true;
            for (String nick : nicks) {
                if (isFirst) {
                    ret = ret + nick;
                    isFirst = false;
                }
                else {
                    ret = ret + " " + nick;
                }
            }
        }
        return ret;
    }

    /**
     * @see f00f.net.irc.martyr.Command#getIrcIdentifier()
     */
    public String getIrcIdentifier()
    {
        //
        // This command uses "ISON" on outgoing, so that is why we use
        // "ISON" here instead of "303".
        //
        return IDENTIFIER_PRIMARY;
    }

    /**
     * @see AbstractCommand#selfRegister(f00f.net.irc.martyr.CommandRegister)
     */
    public void selfRegister(CommandRegister commandRegister)
    {
        commandRegister.addCommand( IDENTIFIER_PRIMARY, this );
        commandRegister.addCommand( IDENTIFIER_SECONDARY, this );
    }

    /**
     * Retrieves the target of the ISON command
     *
     * @return Target of command
     */
    public String getDest() {
        return dest;
    }

    /**
     * Retrieves the list of nicks that are online after an ISON command
     *
     * @return List of online nicks
     */
    public List<String> getNicks()
    {
        return nicks;
    }

}