You are looking at historical revision 32807 of this page. It may differ significantly from its current revision.

Outdated egg!

This is an egg for CHICKEN 3, the unsupported old release. You're almost certainly looking for the CHICKEN 4 version of this egg, if it exists.

If it does not exist, there may be equivalent functionality provided by another egg; have a look at the egg index. Otherwise, please consider porting this egg to the current version of CHICKEN.

irc

Description

A simple IRC client library.

Author

felix winkelmann

Requirements

None

Download

irc.egg

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).

irc:connection

[procedure] (irc:connection #!key user password server nick real-name port)

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

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-channel CONNECTION)
[procedure] (irc:connection-password 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 descontruction 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: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)

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-match, with the exception of the body keyword argument, which 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:

(require-extension irc posix)

(define con
  (irc:connection server: "irc.freenode.net" nick: "PongoTwistleton") )

(define (bleep _)
  (irc:say con (seconds->string (current-seconds))) )

(irc:connect con)

(irc:join con "#scheme")

(irc:add-message-handler!
 con bleep
 command: "PRIVMSG" body: "time" 
 receiver: (irc:connection-nick con) )

(irc:run-message-loop con debug: #t)

Changelog

License

 Copyright (c) 2000-2005, 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.