Home arrow Python arrow Page 3 - IRC on a Higher Level Continued

Client-To-Client Protocol - Python

In the last article, you were taught about events in Python-IRCLib. You were taught what some common events are, how to catch them and how to properly respond to them. While a grasp of events is crucial to developing applications that interact through IRC, it's important to realize that Python-IRCLib doesn't just deal with responding to events. The library is a few dozen kilobytes, and all of that space obviously does not contain code that deals with events. The library contains many more features, and we'll take a look at some of them in this article.

TABLE OF CONTENTS:
  1. IRC on a Higher Level Continued
  2. More Action Methods
  3. Client-To-Client Protocol
  4. Sending to Another Client
By: Peyton McCullough
Rating: starstarstarstarstar / 3
October 19, 2005

print this article
SEARCH DEV SHED

TOOLS YOU CAN USE

advertisement

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



 
 
>>> More Python Articles          >>> More By Peyton McCullough
 

blog comments powered by Disqus
escort Bursa Bursa escort Antalya eskort
   

PYTHON ARTICLES

- Python Big Data Company Gets DARPA Funding
- Python 32 Now Available
- Final Alpha for Python 3.2 is Released
- Python 3.1: String Formatting
- Python 3.1: Strings and Quotes
- Python 3.1: Programming Basics and Strings
- Tuples and Other Python Object Types
- The Dictionary Python Object Type
- String and List Python Object Types
- Introducing Python Object Types
- Mobile Programming using PyS60: Advanced UI ...
- Nested Functions in Python
- Python Parameters, Functions and Arguments
- Python Statements and Functions
- Statements and Iterators in Python

Developer Shed Affiliates

 


Dev Shed Tutorial Topics: