irssibot.core
Class ServerConnection

java.lang.Object
  |
  +--java.lang.Thread
        |
        +--irssibot.core.ServerConnection
All Implemented Interfaces:
java.lang.Runnable

public class ServerConnection
extends java.lang.Thread

Represents a connection to an IRC server.

Version:
$Name: $ $Revision: 1.17 $
Author:
Matti Dahlbom

Field Summary
private  Host botHost
           
private  long bytesRead
          Number of bytes read from the server
private  long bytesWritten
          Number of bytes written to server
private  java.util.Vector channelConnects
           
private  long channelRejoinTime
           
private  java.util.Hashtable channels
           
private  boolean connectionAlive
           
private  boolean connectionReady
           
private  boolean continueConnecting
           
private  Core core
           
private  java.lang.String currentServer
           
private  int currentServerIndex
           
private  java.lang.String errorMsg
           
private  ServerInstanceData instanceData
           
private  long lastSaveTime
           
private static java.lang.String moduleName
           
private  long nickRegainTime
           
private  OutputQueue outputQueue
           
private  java.lang.String serverChannelModes
           
private  java.io.BufferedInputStream serverIn
           
private  java.io.BufferedReader serverInReader
           
private  java.io.BufferedOutputStream serverOut
           
private  java.lang.String serverUserModes
           
private  java.net.Socket socket
           
private  java.lang.String statusString
           
private  boolean useAltNick
           
private  boolean userDataChanged
           
private  java.util.Vector users
           
 
Fields inherited from class java.lang.Thread
MAX_PRIORITY, MIN_PRIORITY, NORM_PRIORITY
 
Constructor Summary
ServerConnection(ServerInstanceData instanceData, Core core)
          Constructs.
 
Method Summary
 boolean addChannel(java.lang.String channelName, java.lang.String channelKey)
          Adds a channel to bots channels hash and makes bot join the channel.
 java.lang.String addChannelConnect(java.lang.String sourceChannel, ServerConnection destinationNetwork, java.lang.String destinationChannel)
          Adds a channel connect.
 void addUser(User user)
           
 void channelBroadcast(java.lang.String message)
          Broadcasts a message to all channels.
private  void connect(java.lang.String addr)
          Attempts to connect to an irc server.
 boolean delChannel(Channel channel)
          Removes a named channel from bots channels hash and makes the bot leave the channel.
 boolean delUser(User user)
          Deletes a user from bots user record.
 void doMaintain()
          Calls doMaintain() for all channels
private  void doUserHost(IrcMessage message)
          Handle RPL_USERHOST reply message: Finish up connecting Parse bot host from message Join channels
 Channel findChannel(java.lang.String name)
          finds a Channel matching given name or null if not found
private  User findMatchingUser(Host host)
          finds a user matching given host in bot's user record.
 User findUser(Host host)
          Finds a User by host.
 User findUser(java.lang.String name)
          Finds a User by exact (in-casesensitive) name.
 java.util.Vector getChannelConnects()
           
 java.util.Hashtable getChannels()
           
 java.lang.String getConnectionStatus()
           
 Host getHost()
           
 ServerInstanceData getInstanceData()
           
 ModuleHandler getModuleHandler()
           
 OutputQueue getOutputQueue()
           
 java.lang.String getServerChannelModes()
          Returns the allowed channel modes for the current server.
 java.lang.String getTrafficInfo()
          Returns an info string describing server traffic statistics.
 java.util.Vector getUsers()
           
 java.lang.String getXML()
          Returns XML representation of this server connection to be written to configuration file.
 void notifyUserDataChanged()
          Notifies ServerConnection that its user data has been changed and it should rewrite the user file soon.
private  void processNick(IrcMessage message)
          Forwards a NICK message to all channels and handles changes to own nick.
private  void processPrivmsg(IrcMessage message)
          Processes PRIVMSGs to either a channel or to bot himself.
private  void processQuit(IrcMessage message)
          Forwards a QUIT message to all channels.
private  void processServerMessage(IrcMessage message)
          Processes message from server.
 void quit(java.lang.String quitMessage)
          QUIT from IRC server and finish execution.
 void removeChannelConnects()
          Removes all channel connects
 void run()
          run()
private  void setStatusString(java.lang.String statusString)
          Sets the ServerConnection's status string.
 java.lang.String toString()
           
 void write(java.lang.String str)
          Write bytes/string to server.
 void writeUserFile()
          Writes user file to disk.
 
Methods inherited from class java.lang.Thread
activeCount, checkAccess, countStackFrames, currentThread, destroy, dumpStack, enumerate, getContextClassLoader, getName, getPriority, getThreadGroup, holdsLock, interrupt, interrupted, isAlive, isDaemon, isInterrupted, join, join, join, resume, setContextClassLoader, setDaemon, setName, setPriority, sleep, sleep, start, stop, stop, suspend, yield
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, wait, wait, wait
 

Field Detail

moduleName

private static java.lang.String moduleName

core

private Core core

serverIn

private java.io.BufferedInputStream serverIn

serverInReader

private java.io.BufferedReader serverInReader

serverOut

private java.io.BufferedOutputStream serverOut

socket

private java.net.Socket socket

errorMsg

private java.lang.String errorMsg

statusString

private java.lang.String statusString

connectionAlive

private boolean connectionAlive

continueConnecting

private boolean continueConnecting

instanceData

private ServerInstanceData instanceData

users

private java.util.Vector users

channelConnects

private java.util.Vector channelConnects

channels

private java.util.Hashtable channels

botHost

private Host botHost

outputQueue

private OutputQueue outputQueue

serverUserModes

private java.lang.String serverUserModes

serverChannelModes

private java.lang.String serverChannelModes

currentServer

private java.lang.String currentServer

currentServerIndex

private int currentServerIndex

useAltNick

private boolean useAltNick

lastSaveTime

private long lastSaveTime

userDataChanged

private boolean userDataChanged

connectionReady

private boolean connectionReady

nickRegainTime

private long nickRegainTime

channelRejoinTime

private long channelRejoinTime

bytesRead

private long bytesRead
Number of bytes read from the server


bytesWritten

private long bytesWritten
Number of bytes written to server

Constructor Detail

ServerConnection

public ServerConnection(ServerInstanceData instanceData,
                        Core core)
Constructs.

Parameters:
instanceData - instance data from configuration file
core - reference to bot core object
Method Detail

getServerChannelModes

public java.lang.String getServerChannelModes()
Returns the allowed channel modes for the current server.

Returns:
possible channel modes for the server

toString

public java.lang.String toString()
Overrides:
toString in class java.lang.Thread

getUsers

public java.util.Vector getUsers()

getInstanceData

public ServerInstanceData getInstanceData()

getConnectionStatus

public java.lang.String getConnectionStatus()

getChannels

public java.util.Hashtable getChannels()

getHost

public Host getHost()

getChannelConnects

public java.util.Vector getChannelConnects()

getModuleHandler

public ModuleHandler getModuleHandler()

getOutputQueue

public OutputQueue getOutputQueue()

addUser

public void addUser(User user)

getTrafficInfo

public java.lang.String getTrafficInfo()
Returns an info string describing server traffic statistics.

Returns:
server traffic statistics

delUser

public boolean delUser(User user)
Deletes a user from bots user record.

Parameters:
user - user to delete
Returns:
true if succesful

channelBroadcast

public void channelBroadcast(java.lang.String message)
Broadcasts a message to all channels.

Parameters:
message - message to broadcast

addChannelConnect

public java.lang.String addChannelConnect(java.lang.String sourceChannel,
                                          ServerConnection destinationNetwork,
                                          java.lang.String destinationChannel)
Adds a channel connect.

Parameters:
sourceChannel - channel to forward from
destinationNetwork - network (ServerConnection) to forward to
destinationChannel - channel on destination network to forward to
Returns:
null if successful, otherwise error message

removeChannelConnects

public void removeChannelConnects()
Removes all channel connects


findUser

public User findUser(java.lang.String name)
Finds a User by exact (in-casesensitive) name.

Parameters:
name - name of user as specified in config file / bots user record.
Returns:
User object, or null if no such user for bot.

findUser

public User findUser(Host host)
Finds a User by host. user must be logged in.

Parameters:
host - host
Returns:
User object, or null if not found or not logged in.
See Also:
User.isLoggedIn()

findMatchingUser

private User findMatchingUser(Host host)
finds a user matching given host in bot's user record. need not be logged in. the returned User should not be used in any action requiring login, but merely in inspection.

Parameters:
host - host
Returns:
User
See Also:
User.isLoggedIn()

findChannel

public Channel findChannel(java.lang.String name)
finds a Channel matching given name or null if not found

Parameters:
name - name of channel
Returns:
Channel/null

connect

private void connect(java.lang.String addr)
Attempts to connect to an irc server.


setStatusString

private void setStatusString(java.lang.String statusString)
Sets the ServerConnection's status string.


doUserHost

private void doUserHost(IrcMessage message)
Handle RPL_USERHOST reply message:

Parameters:
message - the RPL_USERHOST message

getXML

public java.lang.String getXML()
Returns XML representation of this server connection to be written to configuration file.

Returns:
XML representing this server instance

run

public void run()
run()

Specified by:
run in interface java.lang.Runnable
Overrides:
run in class java.lang.Thread

processServerMessage

private void processServerMessage(IrcMessage message)
Processes message from server.

Parameters:
message - message to be processed

processPrivmsg

private void processPrivmsg(IrcMessage message)
Processes PRIVMSGs to either a channel or to bot himself.

Parameters:
message - message

processNick

private void processNick(IrcMessage message)
Forwards a NICK message to all channels and handles changes to own nick.

Parameters:
message - message to send

processQuit

private void processQuit(IrcMessage message)
Forwards a QUIT message to all channels.

Parameters:
message - message to send

addChannel

public boolean addChannel(java.lang.String channelName,
                          java.lang.String channelKey)
Adds a channel to bots channels hash and makes bot join the channel.

Parameters:
channelName - name of channel
channelKey - channel key to use
Returns:
true if successful, false if channel already define for this server connection

delChannel

public boolean delChannel(Channel channel)
Removes a named channel from bots channels hash and makes the bot leave the channel.

Parameters:
channel - Channel object of channel to remove
Returns:
true on success, false if no such channel was found

quit

public void quit(java.lang.String quitMessage)
QUIT from IRC server and finish execution.

Parameters:
quitMessage - message to send to IRC server as quit message

doMaintain

public void doMaintain()
Calls doMaintain() for all channels


notifyUserDataChanged

public void notifyUserDataChanged()
Notifies ServerConnection that its user data has been changed and it should rewrite the user file soon.


writeUserFile

public void writeUserFile()
Writes user file to disk.


write

public void write(java.lang.String str)
Write bytes/string to server. This function will be replaced by a bursting one to avoid excess flood.

Parameters:
str - string to write


IrssiBot (c) 2000-2002 Matti Dahlbom