SimpleIRCClient In the first article of this series, I explained how to catch and respond to events in an application. However, you probably noticed that the code behind this was a bit messy and long. First, we have to define a handler function that responds to the triggered event. Then, we need to wire our handler function to our IRC object, using the add_global_handler method, which accepts a string corresponding to the event we want to work with and the name of the handler function. The code doesn't look pretty at all, but, fortunately, the library contains a shortcut to this all: the SimpleIRCClient class. The SimpleIRCClient class can be subclassed to create a functional IRC client. It eliminates many of the steps normally required. All you have to do is create an instance of the SimpleIRCClient and call the connect method to get started. From there, you can access the methods of ServerConnection. After that, all that's left is calling the start method to accept incoming data. Let's create a simple client that joins a given channel and sits there: import irclib # Connection information # Subclass SimpleIRCClient pass # Create an instance of ClientClass # Connect # Join the specified channel # Start As you can see, this object-oriented approach to creating a client is very simple to employ. In case you don't immediately see the advantage of using the SimpleIRCClient class, however, let's move on to the next topic now that we've established a connection. Events are, as I've stated several times in the previous articles, crucial to most applications of purpose. The SimpleIRCClient really shines when it comes to events, allowing you to create methods of your subclass that handle events. These methods come in the form of on_event, and they are called automatically when the appropriate event is triggered. For example, here is an application that logs messages in the file channelLog.txt: import irclib # Connection information # Open a file to store the logs in # Subclass SimpleIRCClient # Handle private messages # Write to the log # Create our client and connect # Start the client As you can see from the above example, the handler method is structured the exact same way as a normal handler, except that it's a method and its name corresponds to the event we want to handle. We omit the add_global_handler method, compacting our code. Any event will work with the SimpleIRCClient class. You are certainly not limited to the common ones. Here's an application that gathers information on clients joining the application's channel through the use of the client-to-client protocol: import irclib # Connection variables # Our client class # Query users who join the channel source = event.source().split ( '!' ) [ 0 ] # Handle CTCP replies source = event.source().split ( '!' ) [ 0 ] # Display the user's local time # Display the user's verison # Display the user's information # Display the user's ping # Create the client and connect # Loop Even in a more complex application, the code is neat and organized.
blog comments powered by Disqus |
|
|
|
|
|
|
|