summaryrefslogtreecommitdiffstats
path: root/EssentialsUpdate/src/f00f/net/irc/martyr/commands/QuitCommand.java
blob: e12a2520d741e8756d2d40a504f4c884a5dd05c7 (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
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.util.FullNick;

/**
 * <p>Defines QUIT command.  The QUIT command asks the irc server to
 * disconnect us, and we can optionally give a reason.  The QUIT
 * command is also received by us if someone on a channel we are on
 * quits.</p>
 *
 * <p>What should be done to signal to the framework that the
 * disconnection that should come from the server is legit, and we
 * shouldn't try to re-connecet?  For now it will be assumed that the
 * user of the framework will signal all the appropriate classes that
 * a legit disconnection will happen (ie AutoRegister which will try
 * to re-connect otherwise).</p>
 */
public class QuitCommand extends AbstractCommand
{
    //static Logger log = Logger.getLogger(QuitCommand.class);

    private String reason;
    private FullNick user;

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

    /**
     * For use as an incoming command.
     *
     * @param user User that has quit
     * @param reason Specified reason for quitting
     */
    public QuitCommand( FullNick user, String reason )
    {
        this.user = user;
        this.reason = reason;
    }

    /**
     * For use as an outgoing command.
     *
     * @param reason Specified reason for quitting
     */
    public QuitCommand( String reason )
    {
        this( null, reason );
    }

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

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

    public String renderParams()
    {
        return ":" + reason;
    }

    public String getReason()
    {
        return reason;
    }

    public FullNick getUser()
    {
        return user;
    }

    /**
     * Returns true if we are the ones quitting.
     *
     * @param state Client state we are checking against
     * @return True or false if the quit is us quitting
     */
    public boolean isOurQuit( ClientState state )
    {
        return user.equals( state.getNick() );
    }

    /** If we are quitting, we won't be worrying about our client state.
     * If someone else is leaving, then remove them from all the groups
     * they are in.
     */
    public boolean updateClientState( ClientState state )
    {
        //log.debug( "Nick: " + state.getNick().toString() );
        if( isOurQuit(state) )
        {
            // We've quit
            //log.debug("QUIT: We've quit: " + reason);

            // What should we do with the client state here?
            return true;
        }
        else
        {
            // Someone else quit.  We need to remove them from each group
            // they are in.
            //log.debug("QUIT: " + user + " quit: " + reason);

            // 1) Grab channels
            Enumeration channelNames = state.getChannelNames();
            while( channelNames.hasMoreElements() )
            {
                String chanName = channelNames.nextElement().toString();

                // 2) Remove from group.
                Channel channelObj = state.getChannel( chanName);
                channelObj.removeMember( user, this );
            }

            return true;
        }
    }

}