SunQuest
 
       Python
  Home arrow Python arrow Page 4 - 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 - Sending to Another Client


    (Page 4 of 4 )

    Now, what if your application needs to send another client a CTCP query? First, you would call the ctcp method. Then, you would have to catch the client's reply to your query –- if any. This is easily done. Let's build a simple application that sends another user a “TIME” query:

    import irclib

    # Connection information
    network = 'irc.freenode.net'
    port = 6667
    channel = '#irclib'
    nick = 'PyTest'
    name = 'Python Test'

    # Handle the reply
    def handleReply ( connection, event ):

       print event.source().split ( '!' ) [ 0 ] + '-> ' + event.arguments() [ 0 ] + ': ' + event.arguments() [ 1 ]

    # Create an IRC object
    irc = irclib.IRC()
    irc.add_global_handler ( 'ctcpreply', handleReply )

    # Create a server object and connect
    server = irc.server()
    server.connect ( network, port, nick, ircname = name )
    server.join ( channel )

    # Send the query
    # Note that the username comes last
    server.ctcp ( 'TIME', 'User_Name' )

    # Loop
    irc.process_forever()

    Since the first argument sent in a reply will be the query type (“TIME”, etc.), the above handler will work on almost everything. You are not limited to the “TIME” command with the above application.

    There are a few other specified commands that you might be interested in using and responding to, so I'll describe them here. The first such command is the “ACTION” command, and I briefly mentioned it earlier. To send an “ACTION” command, you may use the action method. It is simply a shortcut method, and it is used like this:

    ...
    server.action ( '#channel', 'does something interesting.' )
    ...

    The “FINGER” command is sent to users to see how long they have been idle. The user's full name and idle time are supposed to be sent in the reply:

    ...
    server.ctcp ( 'FINGER', 'User_Name' )
    ...

    The “SOURCE” command is sent to users when you want to figure out where to obtain their client:

    ...
    server.ctcp ( 'SOURCE', 'User_Name' )
    ...

    The “PING” command is used to figure out how long it takes for a message to reach a given user. It takes a timestamp as an argument. The target client sends back the message in a reply, and the source client compares the received timestamp with the current time. The difference is the time in seconds that the message took to travel:

    ...
    import time
    ...
    def handleReply ( connection, event ):

       if event.arguments() [ 0 ].upper() == 'PING':
          print event.source().split ( '!' ) [ 0 ] + '-> ' + str ( time.time() - float ( event.arguments() [ 1 ] ) )
       else:
          print event.source().split ( '!' ) [ 0 ] + '-> ' + event.arguments() [ 0 ] + ': ' + event.arguments() [ 1 ]
    ...
    irc.add_global_handler ( 'ctcpreply', handleReply )
    ...
    server.ctcp ( 'PING', 'User_Name', str ( time.time() ) )
    ...

    The “ERRMSG” comand is used as a reply. If your application receives a CTCP query that it is not familiar with, you might want to consider sending an “ERRMSG” command, along with a message explaining that the query type is not recognized:

    ...
    server.ctcp_reply ( 'ERRMSG', 'User_Name', 'Query uknown.' )
    ...

    If the command is received as a query, then you should reply saying that no error has ocurred.

    The “USERINFO” command asks for information about the user of the target client:

    ...
    server.ctcp ( 'USERINFO', 'User_Name' )
    ...

    When your application is given the command, you should return information about the user of your application, according to the IRC protocol documentation. However, instead of returning information about you for an automated bot, I don't see why you couldn't return information about your application:

    ...
    server.ctcp_reply ( 'USERINFO', 'User_Name', 'I am just a bot.' )
    ...

    Finally, the “CLIENTINFO” command is used to query a client for information about what commands it accepts:

    ...
    server.ctcp ( 'CLIENTINFO', 'User_Name' )
    ...

    You are, of course, not limited to the above commands in your application. You can create new commands that your applications can share with each other –- a medium for communication.

    Conclusion

    In the first article, you learned how to connect to an IRC network and stay connected. You also learned about events and how to catch and respond to them. In this article, you learned about methods that act as shortcuts around the IRC protocol, simplifying it so that a full understanding of the protocol is not necessary to develop an application that communicates through IRC. You also learned about the client-to-client protocol, which your application can use to query other users or other applications. Users and other applications can also query and communicate with your application.

    These skills allow for the construction of a variety of IRC applications, from simple and primitive to complex and complete. The library we are using eliminates much of the tricky work, allowing us to concentrate on the logic of the applications rather than the medium through which they are communicating.


    DISCLAIMER: The content provided in this article is not warranted or guaranteed by Developer Shed, Inc. The content provided is intended for entertainment and/or educational purposes in order to introduce to the reader key ideas, concepts, and/or product reviews. As such it is incumbent upon the reader to employ real-world tactics for security and implementation of best practices. We are not liable for any negative consequences that may result from implementing any information covered in our articles or tutorials. If this is a hardware review, it is not recommended to open and/or modify your hardware.

       · 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 4 hosted by Hostway