irc
- irc
- Description
- Author
- Requirements
- Documentation
- irc:connection
- irc:connection-raw-filter-set!
- irc:connect
- irc:disconnect
- irc:reconnect
- irc:connected?
- irc:quit
- irc:join
- irc:part
- irc:nick
- irc:say
- irc:notice
- irc:action
- irc:command
- irc:listen
- irc:message-sender
- irc:extended-data?
- irc:extended-data-tag
- irc:wait
- irc:process-message
- irc:run-message-loop
- irc:add-message-handler!
- irc:remove-message-handler!
- Example
- Changelog
- License
Description
A simple IRC client library.
Author
Requirements
Documentation
This extension provides basic support for the IRC client protocol (RFC 2812). Note that some basic knowledge of the protocol will be needed to use this library.
Error-replies from the server are signalled as composite conditions of kind (exn irc) with the properties code (the IRC error code) and reply (the actual message object received). If the connection is terminated by the server, a composite condition of the kinds (exn irc/eof) is signalled. Automatic reconnection on timeout or eof is supported and optional.
irc:connection
[procedure] (irc:connection #!key user password server nick real-name port log-traffic ping-timeout reconnect reconnect-timeout)Returns a connection object, where the following keyword arguments specify the connection parameters:
keyword | argument type | default |
---|---|---|
user | string | nobody |
password | string | none |
server | string | none |
nick | string | some random symbol |
real-name | string | same as the user parameter |
port | integer | 6667 |
log-traffic | port or #f | #f |
reconnect-timeout | milliseconds (integer) | 3600000 |
reconnect | boolean | #f |
A server must be provided in any case.
The connection object can be queried using the following accessors:
[procedure] (irc:connection? X)Returns #t if X is a connection object, or #f otherwise.
[procedure] (irc:connection-in CONNECTION)[procedure] (irc:connection-out CONNECTION)
[procedure] (irc:connection-server CONNECTION)
[procedure] (irc:connection-nick CONNECTION)
[procedure] (irc:connection-user CONNECTION)
[procedure] (irc:connection-real-name CONNECTION)
[procedure] (irc:connection-port CONNECTION)
[procedure] (irc:connection-channels CONNECTION)
[procedure] (irc:connection-log-file CONNECTION)
[procedure] (irc:connection-reconnect-timeout CONNECTION)
[procedure] (irc:connection-password CONNECTION)
[procedure] (irc:connection-reconnect? CONNECTION)
Accessors for values stored in a connection object. The in and out values are ports, port is an integer, channel is a string and all other values are strings.
irc:connection-raw-filter-set!
[procedure] (irc:connection-raw-filter-set! CONNECTION PROC)When set, then each line of received input will first be processed by the one argument procedure PROC, and the result be passed to the message deconstruction machinery instead. Use this facility to perform very low level pre-processing of input from the IRC server.
irc:connect
[procedure] (irc:connect CONNECTION)[procedure] (irc:connect #!key KEYWORD ...)
Connects to an IRC server, with the parameters given in the connection object CONNECTION or via the parameters passed as keywords arguments (just like in the call to irc:connection). Returns the connection object. A connection can only be made to a single server at the same time.
irc:disconnect
[procedure] (irc:disconnect CONNECTION)Disconnects any currently active connection.
irc:reconnect
[procedure] (irc:reconnect CONNECTION)Disconnect and reconnect to CONNECTION, automatically re-joining all channels that we have currently joined.
irc:connected?
[procedure] (irc:connected? CONNECTION)Returns #t if there exists an open connection for CONNECTION, or #f otherwise.
irc:quit
[procedure] (irc:quit CONNECTION [MESSAGE])Sends a QUIT message to the IRC server (optionally with the text MESSAGE) and closes the connection designated by the connection object CONNECTION.
irc:join
[procedure] (irc:join CONNECTION CHANNEL)Sends JOIN messages for the given CHANNEL (a string).
irc:part
[procedure] (irc:part CONNECTION CHANNEL)[procedure] (irc:leave CONNECTION CHANNEL)
Leaves the channels given in the string CHANNEL by sending a PART message.
irc:nick
[procedure] (irc:nick CONNECTION NICK)Changes the nickname to the one given in the string NICK.
irc:say
[procedure] (irc:say CONNECTION MESSAGE DESTINATION ...)Sends the string MESSAGE via a PRIVMSG command to the given destinations, which should be strings designating either channels or nicknames. If no destinations are given, the message is sent to the last channel that has been joined. The message is split into multiple PRIVMSG operations, if it contains newline characters.
irc:notice
[procedure] (irc:notice CONNECTION MESSAGE DESTINATION ...)Similar to irc:say but uses a NOTICE command instead.
irc:action
[procedure] (irc:action CONNECTION MESSAGE DESTINATION ...)Similar to irc:say but Sends an "action" instead of a normal message.
irc:command
[procedure] (irc:command CONNECTION STRING)Sends an arbitrary IRC command to the server.
irc:listen
[procedure] (irc:listen CONNECTION)If data is available for reading, then the incoming message is parsed and a "message" object is returned. If no data is currently available, #f is returned.
The message object can be queried with the following procedures:
[procedure] (irc:message? X)Returns #t if X is a message object, or #f otherwise.
[procedure] (irc:message-prefix MESSAGE)[procedure] (irc:message-command MESSAGE)
[procedure] (irc:message-timestamp MESSAGE)
[procedure] (irc:message-code MESSAGE)
[procedure] (irc:message-body MESSAGE)
[procedure] (irc:message-parameters MESSAGE)
[procedure] (irc:message-index MESSAGE)
Return the components of a message object. The prefix is either a list of the form (SERVERNAME) or a list containing the message prefix of the form (NICK USER HOST) (all values are strings). The timestamp holds the current number of seconds (as returned by (current-seconds)) at the point when the message was parsed. The body contains the complete message string. The code is a numerical command-code for the message or #f. The parameters is a list of strings, containing message destination and message body. If the message contains extended information (mostly ACTIONs), then the last value in the list is an extended data object. The index is a numeric message index.
irc:message-sender
[procedure] (irc:message-sender MESSAGE)[procedure] (irc:message-receiver MESSAGE)
Returns the sender and receiver of a message, respectively. The receiver may be a channel name or the nickname of another client.
irc:extended-data?
[procedure] (irc:extended-data? X)Returns #t if X is an extended data object, or #f otherwise.
irc:extended-data-tag
[procedure] (irc:extended-data-tag EXTENDED)[procedure] (irc:extended-data-content EXTENDED)
Return the tag (symbol) and content (string) parts of an extended data object.
irc:wait
[procedure] (irc:wait CONNECTION)Waits until data is available from CONNECTION and returns the parsed message object.
irc:process-message
[procedure] (irc:process-message CONNECTION MESSAGE [VERBOSE])Calls any registered message handlers that apply to MESSAGE. If the optional argument VERBOSE is given and true, then diagnostic output will be written to the port returned by (current-output-port).
irc:run-message-loop
[procedure] (irc:run-message-loop CONNECTION #!key debug pong filter)Repeatedly calls irc:wait and irc:process-message. If the keyword argument debug is given and true, then each incoming message will be dumped to the port given by the value of (current-output-port). The keyword argument pong specifies whether automatic processing of PING messages should be done. If yes, then a message handler with the tag ping (a symbol) will be registered as with the following commands:
(irc:add-message-handler! con (lambda (msg) (irc:command con (string-append "PONG :" (car (irc:message-parameters msg)))) ) tag: 'ping command: "PING") )
The keyword argument filter can be used to specify a procedure that will be called for each icoming message object (the result will be used for further processing instead).
irc:add-message-handler!
[procedure] (irc:add-message-handler! CONNECTION PROC #!key command sender receiver body tag code)Defines a message handler for the given CONNECTION that will invoke the one argument procedure PROC with the received message object when all of the regular expressions given for the keyword arguments match (uses string-search), and the code, which should be an exact integer (and is compared using eq?).
The keyword tag defines a message-handler tag, which can be used to remove the handler at a later time.
Note that if this procedure is called with two arguments (and no keyword args), then the message handler will be invoked for all incoming messages.
Message handlers are run in the order in which they are defined. A message handler should return #f if further handlers should be invoked for the same message, or true if other handlers should not be called.
The command, sender, receiver and body arguments may optionally be predicates instead of strings (which will be called with the respective part of the checked message) to allow more fine-grained matching.
irc:remove-message-handler!
[procedure] (irc:remove-message-handler! CONNECTION TAG)Removes the message handler with the given tag.
Example
A minimalistic bot that shows the current time, when asked:
(import irc (chicken time) (chicken time posix)) (define con (irc:connection server: "irc.libera.chat" nick: "PongoTwistleton") ) (define (bleep _) (irc:say con (seconds->string (current-seconds)) "#somechannel") ) (irc:connect con) (irc:join con "#somechannel") (irc:add-message-handler! con bleep command: "PRIVMSG" body: "time") (irc:run-message-loop con debug: #t)
Changelog
- 1.9.9 Ported to CHICKEN 5
- 1.9.8 reading input did not apply the timeout-parameter correctly (fix contributed by Alex Charlton)
- 1.9.3 reconnecting on timeout
- 1.9.2 reconnect feature
- 1.9.1 handling of server-EOF
- 1.9 added logging and ping-timeout support
- 1.8 uses string-search for matching handlers; allows regexp objects
- 1.7 Ported to CHICKEN 4
- 1.6 Added irc:disconnect
- 1.5 Multimessage-support [Thanks to Andreas Scholta, again]
- 1.4 Fixed bug in irc:remove-message-handler! [Thanks to Andreas Scholta]
- 1.3 irc:join accepts only a single channel
- 1.2 Fixed a bug in processing of handler tags [Thanks to Vesa, again!]
- 1.1 Added irc:connection-raw-handler-set! and further options to irc:run-message-loop (in addition to PING handling) [Thanks to Vesa Kaihlavirta]
- 1.0
License
Copyright (c) 2000-2009, Felix L. Winkelmann All rights reserved. Redistribution and use in source and binary forms, with or without modification, are permitted provided that the following conditions are met: Redistributions of source code must retain the above copyright notice, this list of conditions and the following disclaimer. Redistributions in binary form must reproduce the above copyright notice, this list of conditions and the following disclaimer in the documentation and/or other materials provided with the distribution. Neither the name of the author nor the names of its contributors may be used to endorse or promote products derived from this software without specific prior written permission. THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDERS OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICESLOSS OF USE, DATA, OR PROFITSOR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.