Copyright | (c) 2016 Michael Walker |
---|---|
License | MIT |
Maintainer | Michael Walker <[email protected]> |
Stability | experimental |
Portability | OverloadedStrings |
Safe Haskell | None |
Language | Haskell2010 |
Network.IRC.Client
Contents
Description
A simple IRC client library. Typical usage will be of this form:
run :: ByteString -> Int -> Text -> IO () run host port nick = do let conn = plainConnection host port let cfg = defaultInstanceConfig nick & handlers %~ (yourCustomEventHandlers:) runClient conn cfg ()
You shouldn't really need to tweak anything other than the event handlers, as everything has been designed to be as simple as possible.
- data ConnectionConfig s
- plainConnection :: ByteString -> Int -> ConnectionConfig s
- data TLSConfig
- tlsConnection :: TLSConfig -> ConnectionConfig s
- data Origin
- stdoutLogger :: Origin -> ByteString -> IO ()
- fileLogger :: FilePath -> Origin -> ByteString -> IO ()
- noopLogger :: a -> b -> IO ()
- data InstanceConfig s
- defaultInstanceConfig :: Text -> InstanceConfig s
- data IRC s a
- send :: Message Text -> IRC s ()
- sendBS :: Message ByteString -> IRC s ()
- disconnect :: IRC s ()
- reconnect :: IRC s ()
- module Network.IRC.Client.Events
- data IRCState s
- getIRCState :: IRC s (IRCState s)
- runIRCAction :: MonadIO m => IRC s a -> IRCState s -> m a
- data ConnectionState
- getConnectionState :: IRCState s -> STM ConnectionState
- runClient :: MonadIO m => ConnectionConfig s -> InstanceConfig s -> s -> m ()
- newIRCState :: MonadIO m => ConnectionConfig s -> InstanceConfig s -> s -> m (IRCState s)
- runClientWith :: MonadIO m => IRCState s -> m ()
- data Timeout = Timeout
- fork :: IRC s () -> IRC s ThreadId
- data Disconnect = Disconnect
- module Network.IRC.Client.Lens
- module Network.IRC.Client.Utils
- rawMessage :: ByteString -> [ByteString] -> IrcMessage
- toByteString :: IrcMessage -> ByteString
Configuration
The configuration is logically split into two parts: the
connection configuration (the ConnectionConfig
type) and the
instance configuration (the InstanceConfig
type).
- Connection configuration details how to connect to the IRC server, and cannot be modified after the client has started (although it can be read).
- Instance configuration is everything else: the client's nick, and version, handlers for received messages, and so on. It can be modified after the client has started.
Connection configuration
The following values can be changed with the exported lenses:
username
(default: "irc-client"). The username sent to the server in the "USER" command.realname
(default: "irc-client"). The real name sent to the server in the "USER" command.password
(default:Nothing
). If set, the password sent to the server in the "PASS" command.flood
(default:1
). The minimum time between sending messages, to avoid flooding.timeout
(default:300
). The amount of time to wait for a message from the server before locally timing out.onconnect
(default:defaultOnConnect
). The action to perform after sending the "USER" and "PASS" commands.ondisconnect
(default:defaultOnDisconnect
). The action to perform after disconnecting from the serverlogfunc
(default:noopLogger
). The function to log received and sent messages.
data ConnectionConfig s Source #
The static state of an IRC server connection.
Arguments
:: ByteString | The hostname |
-> Int | The port |
-> ConnectionConfig s |
Connect to a server without TLS.
How to connect to a server over TLS.
Constructors
WithDefaultConfig ByteString Int | |
WithClientConfig TLSClientConfig | Use the given configuration. The hostname and port are stored
as fields of the |
WithVerifier ByteString Int (CertificateStore -> ValidationCache -> ServiceID -> CertificateChain -> IO [FailedReason]) | Use |
Arguments
:: TLSConfig | How to initiate the TLS connection |
-> ConnectionConfig s |
Connect to a server with TLS.
Logging
The logging functions are told whether the message came from the server or the client, and are given the raw bytestring.
The origin of a message.
Constructors
FromServer | |
FromClient |
stdoutLogger :: Origin -> ByteString -> IO () Source #
Print messages to stdout, with the current time.
fileLogger :: FilePath -> Origin -> ByteString -> IO () Source #
Append messages to a file, with the current time.
noopLogger :: a -> b -> IO () Source #
Do no logging.
Instance configuration
The following values can be changed with the exported lenses:
nick
. The nick thatdefaultOnConnect
sends to the server. This is also modified during runtime by thewelcomeNick
andnickMangler
default event handlers.channels
(default:[]
). The channels thatjoinOnWelcome
joins. This is also modified during runtime by thejoinHandler
default event handler.version
(default: "irc-client-$VERSION"). The version thatctcpVersionHandler
sends.handlers
(default:defaultEventHandlers
). The list of event handlers.ignore
(default:[]
). The ignore list, events from matching nicks are not handled.
data InstanceConfig s Source #
The updateable state of an IRC connection.
defaultInstanceConfig Source #
Arguments
:: Text | The nick |
-> InstanceConfig s |
Construct a default IRC configuration from a nick
Writing IRC clients
With this library, IRC clients are mostly composed of event
handlers. Event handlers are monadic actions operating in the
IRC
monad.
The IRC monad.
Instances
MonadState s (IRC s) Source # | |
Monad (IRC s) Source # | |
Functor (IRC s) Source # | |
Applicative (IRC s) Source # | |
Alternative (IRC s) Source # | |
MonadPlus (IRC s) Source # | |
MonadIO (IRC s) Source # | |
MonadThrow (IRC s) Source # | |
MonadCatch (IRC s) Source # | |
MonadMask (IRC s) Source # | |
MonadReader (IRCState s) (IRC s) Source # | |
send :: Message Text -> IRC s () Source #
Send a message as UTF-8, using TLS if enabled. This blocks if messages are sent too rapidly.
sendBS :: Message ByteString -> IRC s () Source #
Send a message, using TLS if enabled. This blocks if messages are sent too rapidly.
disconnect :: IRC s () Source #
Disconnect from the server, properly tearing down the TLS session (if there is one).
reconnect :: IRC s () Source #
Disconnect from the server (this will wait for all messages to be sent, or a minute to pass), and then connect again.
This can be called after the client has already disconnected, in which case it will just connect again.
Like runClient
and runClientWith
, this will not return until
the client terminates (ie, disconnects without reconnecting).
From event handlers
module Network.IRC.Client.Events
From the outside
The ConnectionConfig
, InstanceConfig
, and some other stuff
are combined in the IRCState
type. This can be used to interact
with a client from the outside, by providing a way to run IRC s
a
actions.
The state of an IRC session.
Instances
MonadReader (IRCState s) (IRC s) Source # | |
getIRCState :: IRC s (IRCState s) Source #
Access the client state.
runIRCAction :: MonadIO m => IRC s a -> IRCState s -> m a Source #
Interact with a client from the outside, by using its IRCState
.
data ConnectionState Source #
The state of the connection.
Constructors
Connected | |
Disconnecting | |
Disconnected |
getConnectionState :: IRCState s -> STM ConnectionState Source #
Get the connection state from an IRC state.
Execution
Arguments
:: MonadIO m | |
=> ConnectionConfig s | |
-> InstanceConfig s | |
-> s | The initial value for the user state. |
-> m () |
Connect to the IRC server and run the client: receiving messages and handing them off to handlers as appropriate.
If an IRCState
is constructed with newIRCState
and a client
started with runClientWith
, then runIRCAction
can be used to
interact with that client.
Arguments
:: MonadIO m | |
=> ConnectionConfig s | |
-> InstanceConfig s | |
-> s | The initial value for the user state. |
-> m (IRCState s) |
Construct a new IRC state
runClientWith :: MonadIO m => IRCState s -> m () Source #
If the client times out from the server, the Timeout
exception will be thrown, killing it.
Exception thrown to kill the client if the timeout elapses with nothing received from the server.
Constructors
Timeout |
Concurrency
A client can manage a collection of threads, which get thrown
the Disconnect
exception whenever the client disconnects for
any reason (including a call to reconnect
). These can be
created from event handlers to manage long-running tasks.
fork :: IRC s () -> IRC s ThreadId Source #
Fork a thread which will be thrown a Disconnect
exception when
the client disconnects.
data Disconnect Source #
Exception thrown to all managed threads when the client disconnects.
Constructors
Disconnect |
Lenses
module Network.IRC.Client.Lens
Utilities
module Network.IRC.Client.Utils
Arguments
:: ByteString | The command |
-> [ByteString] | The arguments |
-> IrcMessage |
Construct a raw message.
toByteString :: IrcMessage -> ByteString #
Encode an IRC message into a single bytestring suitable for sending to the server.