IRC on a Higher Level Continued - Client-To-Client Protocol
(Page 3 of 4 )
The client-to-client (CTCP) protocol allows clients to send each other special messages and queries. For example, I'm sure you've encountered the command “/me action” (or, alternatively, “/describe #channel action”). This command utilizes the client-to-client protocol.
We can utilize CTCP in our application. For example, one common CTCP query is “VERSION”. This requests specifics about the target user's IRC client. If someone queries your application, he or she will not get any data unless you work a response into your application. This is very simple to do. All it involves is setting up a function to handle a CTCP message and then checking to see if the “VERSION” command is part of the message. If it is, then we need to send a reply to the source. Let's put this into Python by building a simple application:
import irclib
# Connection data
network = 'irc.freenode.net'
port = 6667
channel = '#irclib'
nick = 'PyTest'
name = 'Python Test'
# Handle a CTCP query
def handleCTCP ( connection, event ):
# Check to see if it is a "VERSION" query
if event.arguments() [ 0 ].upper() == 'VERSION':
# Reply to the query, giving version information
connection.ctcp_reply ( event.source().split ( '!' ) [ 0 ], 'VERSION Python-IRCLib' )
# Create and IRC object and add the handler
irc = irclib.IRC()
irc.add_global_handler ( 'ctcp', handleCTCP )
# Connect
server = irc.server()
server.connect ( network, port, nick, ircname = name )
server.join ( channel )
# Loop
irc.process_forever()
Send your application a “VERSION” request (“/ctcp PyTest VERSION”) and check the response. As you can see, Python-IRCLib uncomplicates the process. We simply set our application up to handle the appropriate event. The function called then checks to see whether the “VERSION” request was sent. If it was, it responds with the ctcp_reply method. The ctcp_reply method takes two arguments, the target and the message.
Another common query is the “TIME” query. When your application receives a “TIME” query, it should return a string with the current local time. A readable string must be sent, not a timestamp. Let's modify our test application to respond to “TIME” queries:
import irclib
import time
network = 'irc.freenode.net
port = 6667
channel = '#irclib'
nick = 'PyTest'
name = 'Python Test'
def handleCTCP ( connection, event ):
if event.arguments() [ 0 ].upper() == 'VERSION':
connection.ctcp_reply ( event.source().split ( '!' ) [ 0 ], 'VERSION Python-IRCLib' )
# Check to see if it is a "TIME" query
elif event.arguments() [ 0 ].upper() == 'TIME':
# Reply to the query, giving the time
connection.ctcp_reply ( event.source().split ( '!' ) [ 0 ], 'TIME ' + time.ctime() )
irc = irclib.IRC()
irc.add_global_handler ( 'ctcp', handleCTCP )
server = irc.server()
server.connect ( network, port, nick, ircname = name )
server.join ( channel )
irc.process_forever()
Next: Sending to Another Client >>
More Python Articles
More By Peyton McCullough