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  
Style-Sheets  
Web Services  
XML  
Zend  
Zope  
Forums Sitemap 
IBM® developerWorks 
Sun Developer Network 
E-Commerce Hosting 
Linux Web Hosting 
Managed Hosting 
Small Business Hosting 
Mobile Linux 
App Generation ROI 
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 Concluded
By: Peyton McCullough
  • Search For More Articles!
  • Disclaimer
  • Author Terms
  • Rating: 5 stars5 stars5 stars5 stars5 stars / 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:
      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


    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


       · This article is the final article in this short series, dealing with some helpful...
       · The scan function is:def scan(): commands.clear() for moduleSource in...
       · Peyton:your tutorials have been really helpful as there really is no documentation...
     

       

    PYTHON ARTICLES

    - 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
    - Mobile Programming in Python using PyS60: UI...
    - Python: Count on It
    - Python Strings: Spinning Yarns
    - Python: More Fun with Strings
    - Python: Stringing You Along

     
    Application Delivery: Everything You Wanted to Know, but Didn`t Know You Needed to Ask
    A comprehensive guide to examining the topics of Wide-area Data Services and app....

     
    Best Practices: Safe and Secure Hardware Asset Recovery
    Companies increasingly must meet EPA and local requirements for the disposal of ....

     
    Managing SSL Security in Multi-Server Environments
    Read this white paper to learn how to simplify management of your organization's....

     
    Open Source Security Myths
    Open Source Software (OSS) is computer software whose source code is available t....

     
    Power and Cooling Capacity Management for Data Centers
    This paper describes the principles for achieving power and cooling capacity man....

     




    © 2003-2008 by Developer Shed. All rights reserved. DS Cluster 3 hosted by Hostway
    Stay green...Green IT