com.shimari.bot
Class IRC_Connection

java.lang.Object
  extended bycom.shimari.bot.IRC_Connection
All Implemented Interfaces:
Channel, Component, Connection, java.util.EventListener, org.schwering.irc.lib.IRCEventListener

public class IRC_Connection
extends java.lang.Object
implements org.schwering.irc.lib.IRCEventListener, Connection

A Connection manages a connection to an IRC server. It goes beyond being a simple IRC_Connecton and automates some of the connection-management behavior. This leaves the Bot itself free to deal with higher level issues. This interface also isolates us a little from the underlying IRC protocol implementation.


Constructor Summary
IRC_Connection(Config config)
          Create (but do not connect) a new Connection.
 
Method Summary
 void broadcastMessage(java.lang.String message)
          CHANNEL API
 void broadcastNotice(java.lang.String message)
          CHANNEL API
 void connect()
          CONNECTOR API Connect to the IRC server.
 void disconnect()
          CONNECTOR API Disconnect from the IRC server.
 java.lang.String[] getChannels()
          IRC_Connection extension Return a list of the channels which have been added.
 java.lang.String getNick()
          IRC_Connection extension
 int getPort()
          IRC_Connection extension
 java.lang.String getRealName()
          IRC_Connection extension
 java.lang.String getServer()
          IRC_Connection extension
 java.lang.String getUserName()
          IRC_Connection extension
 void init(Registry components)
           
 boolean isActiveListener()
          IRC_Connection extension An active listener will relay all public messages to the Bot, whether or not they are addressed to our nick.
 void isActiveListener(boolean willReceivePublicMessages)
           
 boolean isConnected()
          CONNECTOR API Return true if connected.
 boolean isInvitable()
          IRC_Connection extension An invitable connection will automatically join any channel to which it is invited by another user.
 void isInvitable(boolean willJoinChannelOnInvite)
          IRC_Connection extension Set whether we are invitable
 void joinChannel(java.lang.String channel, boolean broadcastChannel)
          IRC_Connection extension Join a channel.
 void onDisconnected()
          IRCEventListener API -- Fired when the own connection is broken.
 void onError(int num, java.lang.String msg)
          IRCEventListener API -- Fired when a numeric error is received.
 void onError(java.lang.String msg)
          IRCEventListener API -- Fired when an ERROR command is received.
 void onInvite(java.lang.String chan, org.schwering.irc.lib.IRCUser user, java.lang.String passiveNick)
          IRCEventListener API -- Fired when somebody is invited to a channel.
 void onJoin(java.lang.String chan, org.schwering.irc.lib.IRCUser user)
          IRCEventListener API -- Fired when somebody joins a channel.
 void onKick(java.lang.String chan, org.schwering.irc.lib.IRCUser user, java.lang.String passiveNick, java.lang.String msg)
          IRCEventListener API -- Fired when somebody is kicked from a channel.
 void onMode(org.schwering.irc.lib.IRCUser user, java.lang.String passiveNick, java.lang.String mode)
          IRCEventListener API -- Fired when somebody changes somebody's usermodes.
 void onMode(java.lang.String chan, org.schwering.irc.lib.IRCUser user, org.schwering.irc.lib.IRCModeParser modeParser)
          IRCEventListener API -- Fired when an operator changes the modes of a channel.
 void onNick(org.schwering.irc.lib.IRCUser user, java.lang.String newNick)
          IRCEventListener API -- Fired when somebody changes his nickname successfully.
 void onNotice(java.lang.String target, org.schwering.irc.lib.IRCUser user, java.lang.String msg)
          IRCEventListener API -- Fired when somebody sends a NOTICE to a user or a group.
 void onPart(java.lang.String chan, org.schwering.irc.lib.IRCUser user, java.lang.String msg)
          IRCEventListener API -- Fired when somebody parts from a channel.
 void onPing(java.lang.String ping)
          IRCEventListener API -- Fired when a PING comes in.
 void onPrivmsg(java.lang.String target, org.schwering.irc.lib.IRCUser user, java.lang.String msg)
          IRCEventListener API -- Fired when a user sends a PRIVMSG to a user or to a group.
 void onQuit(org.schwering.irc.lib.IRCUser user, java.lang.String msg)
          IRCEventListener API -- Fired when somebody quits from the network.
 void onRegistered()
          IRCEventListener API -- Fired when the own connection is successfully established.
 void onReply(int num, java.lang.String value, java.lang.String msg)
          IRCEventListener API -- Fired when a numeric reply is received.
 void onTopic(java.lang.String chan, org.schwering.irc.lib.IRCUser user, java.lang.String topic)
          IRCEventListener API -- Fired when the topic is changed by operators.
 void sendMessage(java.lang.String target, java.lang.String message)
          CONNECTOR API Target can be a nick or a channel (beginning with '#')
 void setMessageQueue(java.util.Collection queue)
          CONNECTOR API All messages that should be processed must be added to this message queue.
 java.lang.String toString()
           
 void unknown(java.lang.String prefix, java.lang.String command, java.lang.String middle, java.lang.String trailing)
          IRCEventListener API -- This event is fired when the incoming line can not be identified as a known event.
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, wait, wait, wait
 

Constructor Detail

IRC_Connection

public IRC_Connection(Config config)
               throws ConfigException
Create (but do not connect) a new Connection. The password may be null if none is required. Note that this connection does not establish join any channels--you must call join explicitly, either before or after a connect.

Method Detail

init

public void init(Registry components)
Specified by:
init in interface Component

setMessageQueue

public void setMessageQueue(java.util.Collection queue)
CONNECTOR API All messages that should be processed must be added to this message queue. The Connection is required to synchronize on this queue before accessing it and should only add messages to the queue.

Specified by:
setMessageQueue in interface Connection

isConnected

public boolean isConnected()
CONNECTOR API Return true if connected.

Specified by:
isConnected in interface Connection

toString

public java.lang.String toString()

sendMessage

public void sendMessage(java.lang.String target,
                        java.lang.String message)
CONNECTOR API Target can be a nick or a channel (beginning with '#')

Specified by:
sendMessage in interface Connection

broadcastNotice

public void broadcastNotice(java.lang.String message)
CHANNEL API

Specified by:
broadcastNotice in interface Connection

broadcastMessage

public void broadcastMessage(java.lang.String message)
CHANNEL API

Specified by:
broadcastMessage in interface Connection

connect

public void connect()
             throws java.io.IOException
CONNECTOR API Connect to the IRC server. If already connected, disconnect and reconnect. This starts a thread to listen to messages from the server.

Specified by:
connect in interface Connection
Throws:
java.io.IOException

disconnect

public void disconnect()
CONNECTOR API Disconnect from the IRC server.

Specified by:
disconnect in interface Connection

getNick

public java.lang.String getNick()
IRC_Connection extension


getServer

public java.lang.String getServer()
IRC_Connection extension


getPort

public int getPort()
IRC_Connection extension


getUserName

public java.lang.String getUserName()
IRC_Connection extension


getRealName

public java.lang.String getRealName()
IRC_Connection extension


getChannels

public java.lang.String[] getChannels()
IRC_Connection extension Return a list of the channels which have been added.


isActiveListener

public boolean isActiveListener()
IRC_Connection extension An active listener will relay all public messages to the Bot, whether or not they are addressed to our nick. A non-active listener will relay private messages, as well as any public message which explicitly references the connection's nick. The default is FALSE: relay only directly addressed messages.


isActiveListener

public void isActiveListener(boolean willReceivePublicMessages)

isInvitable

public boolean isInvitable()
IRC_Connection extension An invitable connection will automatically join any channel to which it is invited by another user.


isInvitable

public void isInvitable(boolean willJoinChannelOnInvite)
IRC_Connection extension Set whether we are invitable


joinChannel

public void joinChannel(java.lang.String channel,
                        boolean broadcastChannel)
IRC_Connection extension Join a channel. We'll remember that we joined and rejoin the channel if we reconnect.


onRegistered

public void onRegistered()
IRCEventListener API -- Fired when the own connection is successfully established. This is the case when the first PING? is received.
This happens between the connection is opened with a socket and the connection is registered: The client sends his information to the server (nickname, username). The server says hello to you by sending you some NOTICEs. And if your nickname is invalid or in use or anything else is wrong with your nickname, it asks you for a new one.

Specified by:
onRegistered in interface org.schwering.irc.lib.IRCEventListener

onDisconnected

public void onDisconnected()
IRCEventListener API -- Fired when the own connection is broken.

Specified by:
onDisconnected in interface org.schwering.irc.lib.IRCEventListener

onError

public void onError(java.lang.String msg)
IRCEventListener API -- Fired when an ERROR command is received.

Specified by:
onError in interface org.schwering.irc.lib.IRCEventListener
Parameters:
msg - The message of the error.

onError

public void onError(int num,
                    java.lang.String msg)
IRCEventListener API -- Fired when a numeric error is received. The server often sends numeric errors (wrong nickname etc.). The msg's format is different for every reply. All replies' formats are described in the IRCUtil. The first word in the msg is always your own nickname!

Specified by:
onError in interface org.schwering.irc.lib.IRCEventListener
Parameters:
num - The identifier (usually a 3-digit number).
msg - The message of the error.

onInvite

public void onInvite(java.lang.String chan,
                     org.schwering.irc.lib.IRCUser user,
                     java.lang.String passiveNick)
IRCEventListener API -- Fired when somebody is invited to a channel.

Specified by:
onInvite in interface org.schwering.irc.lib.IRCEventListener
Parameters:
chan - The channel the user is invited to.
user - The user who invites another. Contains nick, username and host.
passiveNick - The nickname of the user who is invited by another user (passive).

onJoin

public void onJoin(java.lang.String chan,
                   org.schwering.irc.lib.IRCUser user)
IRCEventListener API -- Fired when somebody joins a channel.

Specified by:
onJoin in interface org.schwering.irc.lib.IRCEventListener
Parameters:
chan - The channel the person joins.
user - The user who joins. Contains nick, username and host.

onKick

public void onKick(java.lang.String chan,
                   org.schwering.irc.lib.IRCUser user,
                   java.lang.String passiveNick,
                   java.lang.String msg)
IRCEventListener API -- Fired when somebody is kicked from a channel.

Specified by:
onKick in interface org.schwering.irc.lib.IRCEventListener
Parameters:
chan - The channel somebody is kicked from.
user - The user who kicks another user from a channel. Contains nick, username and host.
passiveNick - The nickname of the user who is kicked from a channel (passive).
msg - The message the active user has set. This is "" if no message was set.

onMode

public void onMode(java.lang.String chan,
                   org.schwering.irc.lib.IRCUser user,
                   org.schwering.irc.lib.IRCModeParser modeParser)
IRCEventListener API -- Fired when an operator changes the modes of a channel. For example, he can set somebody as an operator, too, or take him the oper-status. Also keys, moderated and other channelmodes are fired here.

Specified by:
onMode in interface org.schwering.irc.lib.IRCEventListener
Parameters:
chan - The channel in which the modes are changed.
user - The user who changes the modes. Contains nick, username and host.
modeParser - The IRCModeParser object which contains the parsed information about the modes which are changed.

onMode

public void onMode(org.schwering.irc.lib.IRCUser user,
                   java.lang.String passiveNick,
                   java.lang.String mode)
IRCEventListener API -- Fired when somebody changes somebody's usermodes. Note that this event is not fired when a channel-mode is set, for example when someone sets another user as operator or the mode moderated.

Specified by:
onMode in interface org.schwering.irc.lib.IRCEventListener
Parameters:
user - The user who changes the modes of another user or himself. Contains nick, username and host.
passiveNick - The nickname of the person whose modes are changed by another user or himself.
mode - The changed modes which are set.

onNick

public void onNick(org.schwering.irc.lib.IRCUser user,
                   java.lang.String newNick)
IRCEventListener API -- Fired when somebody changes his nickname successfully.

Specified by:
onNick in interface org.schwering.irc.lib.IRCEventListener
Parameters:
user - The user who changes his nickname. Contains nick, username and host.
newNick - The new nickname of the user who changes his nickname.

onNotice

public void onNotice(java.lang.String target,
                     org.schwering.irc.lib.IRCUser user,
                     java.lang.String msg)
IRCEventListener API -- Fired when somebody sends a NOTICE to a user or a group.

Specified by:
onNotice in interface org.schwering.irc.lib.IRCEventListener
Parameters:
target - The channel or nickname the user sent a NOTICE to.
user - The user who notices another person or a group. Contains nick, username and host.
msg - The message.

onPart

public void onPart(java.lang.String chan,
                   org.schwering.irc.lib.IRCUser user,
                   java.lang.String msg)
IRCEventListener API -- Fired when somebody parts from a channel.

Specified by:
onPart in interface org.schwering.irc.lib.IRCEventListener
Parameters:
chan - The channel somebody parts from.
user - The user who parts from a channel. Contains nick, username and host.
msg - The part-message which is optionally. If it's empty, msg is "".

onPing

public void onPing(java.lang.String ping)
IRCEventListener API -- Fired when a PING comes in. The IRC server tests in different periods if the client is still there by sending PING <ping>. The client must response PONG <ping>.

Specified by:
onPing in interface org.schwering.irc.lib.IRCEventListener
Parameters:
ping - The ping which is received from the server.

onPrivmsg

public void onPrivmsg(java.lang.String target,
                      org.schwering.irc.lib.IRCUser user,
                      java.lang.String msg)
IRCEventListener API -- Fired when a user sends a PRIVMSG to a user or to a group.

Specified by:
onPrivmsg in interface org.schwering.irc.lib.IRCEventListener
Parameters:
target - The channel or nickname the user sent a PRIVMSG to.
user - The user who sent the PRIVMSG. Contains nick, username and host.
msg - The message the user transmits.

onQuit

public void onQuit(org.schwering.irc.lib.IRCUser user,
                   java.lang.String msg)
IRCEventListener API -- Fired when somebody quits from the network.

Specified by:
onQuit in interface org.schwering.irc.lib.IRCEventListener
Parameters:
user - The user who quits. Contains nick, username and host.
msg - The optional message. "" if no message is set by the user.

onReply

public void onReply(int num,
                    java.lang.String value,
                    java.lang.String msg)
IRCEventListener API -- Fired when a numeric reply is received. For example, WHOIS queries are answered by the server with numeric replies. The msg's format is different for every reply. All replies' formats are described in the IRCUtil. The first word in the msg is always your own nickname!

Specified by:
onReply in interface org.schwering.irc.lib.IRCEventListener
Parameters:
num - The numeric reply.
value - The first part of the message.
msg - The main part of the message.

onTopic

public void onTopic(java.lang.String chan,
                    org.schwering.irc.lib.IRCUser user,
                    java.lang.String topic)
IRCEventListener API -- Fired when the topic is changed by operators. Note that the topic is given as a numeric reply fired in onReply when you join a channel.

Specified by:
onTopic in interface org.schwering.irc.lib.IRCEventListener
Parameters:
chan - The channel where the topic is changed.
user - The user who changes the topic. Contains nick, username and host.
topic - The new topic.

unknown

public void unknown(java.lang.String prefix,
                    java.lang.String command,
                    java.lang.String middle,
                    java.lang.String trailing)
IRCEventListener API -- This event is fired when the incoming line can not be identified as a known event.

Specified by:
unknown in interface org.schwering.irc.lib.IRCEventListener
Parameters:
prefix - The prefix of the incoming line.
command - The command of the incoming line.
middle - The part until the colon (:).
trailing - The part behind the colon (:).