summaryrefslogtreecommitdiffstats
path: root/EssentialsUpdate/src/f00f/net/irc/martyr/services/AutoResponder.java
blob: 08f3a7f2946530626ede15865068ab0a8faa5f97 (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
package f00f.net.irc.martyr.services;

import java.util.Observable;
import java.util.Observer;

import f00f.net.irc.martyr.IRCConnection;
import f00f.net.irc.martyr.commands.ChannelModeCommand;
import f00f.net.irc.martyr.commands.JoinCommand;
import f00f.net.irc.martyr.commands.PingCommand;
import f00f.net.irc.martyr.commands.PongCommand;

/**
 * AutoResponder is where commands that should be auto-responded (such
 * as PING-PONG) should go.
 */
public class AutoResponder implements Observer
{

    private IRCConnection connection;
    private boolean enabled = false;

    public AutoResponder( IRCConnection connection )
    {
        this.connection = connection;
        enable();
    }

    public void enable()
    {
        if( enabled )
            return;

        connection.addCommandObserver( this );
        enabled = true;
    }

    public void disable()
    {
        if( !enabled )
            return;

        connection.removeCommandObserver( this );
        enabled = false;
    }

    /**
     * Does the work of figuring out what to respond to.
     * If a PING is received, send a PONG.  If we JOIN a channel, send a
     * request for modes.
     * */
    public void update( Observable observer, Object updated )
    {

        if( updated instanceof PingCommand )
        {
            // We need to do some pongin'!
            PingCommand ping = (PingCommand)updated;

            String response = ping.getPingSource();

            connection.sendCommand( new PongCommand( response ) );
        }
        else if( updated instanceof JoinCommand )
        {
            // Determine if we joined, and if we did, trigger a MODE discovery
            // request.
            JoinCommand join = (JoinCommand)updated;

            if( join.weJoined( connection.getClientState() ) )
            {
                connection.sendCommand(
                    new ChannelModeCommand( join.getChannel() ) );
            }
        }
    }

    // END AutoResponder
}