diff options
author | snowleo <schneeleo@gmail.com> | 2011-10-12 03:14:07 +0200 |
---|---|---|
committer | snowleo <schneeleo@gmail.com> | 2011-10-12 03:14:26 +0200 |
commit | 860d446d28776ec842fa53e8e08538d4e093d6e9 (patch) | |
tree | 0c4598eae4eb8c59fd36e8312eab1b27a8018794 /EssentialsUpdate/src/f00f/net/irc/martyr/services/AutoResponder.java | |
parent | 9ec398b39b0f48392a9d635041b392c7dba2ca0c (diff) | |
download | Essentials-860d446d28776ec842fa53e8e08538d4e093d6e9.tar Essentials-860d446d28776ec842fa53e8e08538d4e093d6e9.tar.gz Essentials-860d446d28776ec842fa53e8e08538d4e093d6e9.tar.lz Essentials-860d446d28776ec842fa53e8e08538d4e093d6e9.tar.xz Essentials-860d446d28776ec842fa53e8e08538d4e093d6e9.zip |
EssentialsUpdate WIP
Diffstat (limited to 'EssentialsUpdate/src/f00f/net/irc/martyr/services/AutoResponder.java')
-rw-r--r-- | EssentialsUpdate/src/f00f/net/irc/martyr/services/AutoResponder.java | 82 |
1 files changed, 82 insertions, 0 deletions
diff --git a/EssentialsUpdate/src/f00f/net/irc/martyr/services/AutoResponder.java b/EssentialsUpdate/src/f00f/net/irc/martyr/services/AutoResponder.java new file mode 100644 index 000000000..08f3a7f29 --- /dev/null +++ b/EssentialsUpdate/src/f00f/net/irc/martyr/services/AutoResponder.java @@ -0,0 +1,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 +} + + + + |