Python
  Home arrow Python arrow Page 3 - IRC on a Higher Level Continued
Dev Shed Forums 
Administration  
AJAX  
Apache  
BrainDump  
DHTML  
Flash  
Java  
JavaScript  
Multimedia  
MySQL  
Oracle  
Perl  
PHP  
Practices  
Python  
Reviews  
Security  
Style-Sheets  
Web Services  
XML  
Zend  
Zope  
Forums Sitemap 
IBM® developerWorks 
Sun Developer Network 
Dedicated Servers 
E-Commerce Hosting 
Linux Web Hosting 
Managed Hosting 
Small Business Hosting 
Actuate Whitepapers 
VeriSign Whitepapers 
VPS Hosting 
Weekly Newsletter

 
Developer Updates  
Free Website Content 
 RSS  Articles
 RSS  Forums
 RSS  All Feeds
Write For Us Get Paid 
Request Media Kit
Contact Us 
Site Map 
Privacy Policy 
Support 
 USERNAME
 
 PASSWORD
 
 
  >>> SIGN UP!  
  Lost Password? 
PYTHON

IRC on a Higher Level Continued
By: Peyton McCullough
  • Search For More Articles!
  • Disclaimer
  • Author Terms
  • Rating: 5 stars5 stars5 stars5 stars5 stars / 3
    2005-10-19

    Table of Contents:
  • IRC on a Higher Level Continued
  • More Action Methods
  • Client-To-Client Protocol
  • Sending to Another Client

  • Rate this Article: Poor Best 
      ADD THIS ARTICLE TO:
      Del.ici.ous Digg
      Blink Simpy
      Google Spurl
      Y! MyWeb Furl
    Email Me Similar Content When Posted
    Add Developer Shed Article Feed To Your Site
    Email Article To Friend
    Print Version Of Article
    PDF Version Of Article
     
     
    ADVERTISEMENT

    Stay one step ahead of the competition. Evaluate and give feedback on some of the hottest web development tools on the market today. Make your opinion heard! Click Here

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

    More Python Articles
    More By Peyton McCullough


       · This article is simply a continuation of the previous article, introducing more of...
     

       

    PYTHON ARTICLES

    - SSH with Twisted
    - Mobile Programming in Python using PyS60: UI...
    - Python: Count on It
    - Python Strings: Spinning Yarns
    - Python: More Fun with Strings
    - Python: Stringing You Along
    - Python Operators
    - Bluetooth Programming in Python: Network Pro...
    - Python Sets
    - Python Conditionals, Lists, Dictionaries, an...
    - Python: Input and Variables
    - Introduction to Python Programming
    - Mobile Programming in Python using PyS60: Ge...
    - Bluetooth Programming using Python
    - Finishing the PyMailGUI Client: User Help To...





    © 2003-2008 by Developer Shed. All rights reserved. DS Cluster 6 hosted by Hostway