Python
  Home arrow Python arrow Page 3 - IRC on a Higher Level Concluded
Dev Shed Forums  
Administration  
AJAX  
Apache  
BrainDump  
DHTML  
Flash  
Java  
JavaScript  
Multimedia  
MySQL  
Oracle  
Perl  
PHP  
Practices  
Python  
Reviews  
Security  
Smartphone Development  
Style-Sheets  
Web Services  
XML  
Zend  
Zope  
Mobile Linux  
App Generation ROI  
IBM® developerWorks  
Forums Sitemap  
E-Commerce Hosting  
Linux Web Hosting  
Managed Hosting  
Small Business Hosting  
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? 
Google.com  
PYTHON

IRC on a Higher Level Concluded
By: Peyton McCullough
  • Search For More Articles!
  • Disclaimer
  • Author Terms
  • Rating: starstarstarstarstar / 5
    2005-10-26


    Table of Contents:
  • IRC on a Higher Level Concluded
  • SingleServerIRCBot
  • Channel Information
  • Something Practical

  • Rate this Article: Poor Best 
      ADD THIS ARTICLE TO:
      error-file:tidyout.log Del.ici.ous error-file:tidyout.log Digg
      error-file:tidyout.log Blink error-file:tidyout.log Simpy
      error-file:tidyout.log Google error-file:tidyout.log Spurl
      error-file:tidyout.log Y! MyWeb error-file:tidyout.log 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


    IRC on a Higher Level Concluded - Channel Information
    ( Page 3 of 4 )

    The SingleServerIRCBot class contains some tools that allow us to gather information about a channel because it maintains a list of channels it is currently in. The utilities are provided by the Channel class, and we can access them in our bot.

    Let's create an example that uses a few of these tools. The bot we construct will join a channel and send us some basic information about the channel when we request it by messaging “!ChanInfo”:

    import ircbot
    import time

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

    # Create our bot class
    class ExampleBot ( ircbot.SingleServerIRCBot ):

       # When welcomed, join the channel and perform the actions mentioned
       def on_welcome ( self, connection, event ):

          connection.join ( channel )

       def on_pubmsg ( self, connection, event ):

          if event.arguments() [ 0 ].upper() == '!CHANINFO':

             source = event.source().split ( '!' ) [ 0 ]
             channelObject = self.channels [ channel ]

             # Message some statistics
             connection.privmsg ( source, 'Moderated: ' + str ( channelObject.is_moderated() ) )
             connection.privmsg ( source, 'Number of users: ' + str ( len ( channelObject.users() ) ) )
             connection.privmsg ( source, 'Channel operators: ' + str ( len (channelObject.opers() ) ) )

    # Create our bot
    bot = ExampleBot ( [( network, port )], nick, name )
    bot.start()

    Most of the above is already familiar to you, so I'll only explain the new content. In the on_pubmsg method, we define channelObject, which is the Channel object that matches channel. From there, we access three methods. The is_moderated method checks to see whether the channel is moderated. The users method returns a list of users in the channel. The opers method returns a list of operators.

    Of course, there are a number of other Channel methods that can gather information for our bots. Let's take a look at those methods. Notice how we can get a list of all users and a list of operators in a channel. We can also get a list of voiced users:

    ...
    voiced = self.channels [ channel ].users()
    ...

    We can also get information on specific users. To start with, we can check whether a channel has a certain user. A boolean value is returned:

    ...
    hasUser = self.channels [ channel ].has_user ( 'User_Name' )
    ...

    If we need to know whether a user has channel operator permissions, we can use the is_oper method to get a boolean value:

    ...
    isOper = self.channels [ channel ].is_oper ( 'User_Name' )
    ...

    We can check whether users are voiced or not, too:

    ...
    isVoiced = self.channels [ channel ].is_voiced ( 'User_Name' )
    ...

    It may be useful to your application if it knows the channel's modes. We can test for certain modes with the Channel object. For example, let's say we wanted to see if the channel was moderated:

    ...
    isModerated = self.channels [ channel ].has_mode ('m' )
    ...

    Of course, you saw the is_moderated method being used before. It is an alternative to the method used above, and the Channel object contains a few more of these. For example, let's say we wanted to check to see if the channel is invite-only:

    ...
    isInvite = self.channels [ channel ].is_invite_only()
    ...

    We can also check to see if the channel is secret:

    ...
    isSecret = self.channels [ channel ].is_secret()
    ...

    Some channels put a limit on the number of users that can be in a channel. We can check to see whether a channel has such a limit:

    ...
    isLimited = self.channels [ channel ].has_limit()
    ...

     

    Many channels do not allow external messages. The has_allow_external_messages can be used to check this:

    ...
    acceptsExternal = self.channels [ channel ].has_allow_external_messages()
    ...

    Some channels have a password, or key, protecting access to the channel. We can check this:

    ...
    hasKey = self.channels [ channel ].has_key()
    ...

    Many channels choose to lock their topics. The has_topic_lock can tell us if a channel has such a lock in place:

    ...
    hasLock = self.channels [ channel ].has_topic_lock()
    ...



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

       

    PYTHON ARTICLES

    - 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
    - Sequences and Sets in Python
    - Python Expressions and Operators
    - Dictionaries, Variables and Statements in Py...
    - Data Types in Python
    - The Python Language
    - SSH with Twisted





    © 2003-2009 by Developer Shed. All rights reserved. DS Cluster 6 Hosted by Hostway
    For more Enterprise Application Development news, visit eWeek