In the last article, you were taught about events in Python-IRCLib. You were taught what some common events are, how to catch them and how to properly respond to them. While a grasp of events is crucial to developing applications that interact through IRC, it's important to realize that Python-IRCLib doesn't just deal with responding to events. The library is a few dozen kilobytes, and all of that space obviously does not contain code that deals with events. The library contains many more features, and we'll take a look at some of them in this article.
Recall the code to private message a channel or user:
... server.privmsg ( '#channel', 'Message to #channel.' ) server.privmsg ( 'UserName', 'Message to UserName' ) ...
The library contains more methods similar to the privmsg method, and a simple method is much easier to use than tinkering with the protocol itself. One such method is an extension of the privmsg method – privmsg_many. While privmsg handles a single target, privmsg_many can easily handle multiple targets. Here's how it works:
... targets = [ '#channel1', '#channel2', 'UserA', 'UserB' ] server.privmsg_many ( targets, 'This is a test message.' ) ...
When you connect to a network, Python-IRCLib will automatically provide the server with a nickname. However, you may wish to change the nickname assigned to your application based on events or the status of your application. This is done with the nick method, which takes a single string as an argument:
... server.nick ( 'New_Name' ) ...
It may be necessary for your application to acquire information about the network it is operating on. Several methods are availible that send out the proper signal. However, keep in mind that you will need to set up the proper handlers to catch the server's response. If you've read the previous article thorougly, then this shouldn't be too much of a challenge. Let's catch one event, though, just to refresh your memory. The who method sends out a “WHO” command like this:
More specific information about a user can be obtained using the whois method. Again, it takes a single argument:
... server.whois ( 'User_Name' ) ...
Again, the server sends a response that must be caught. In the last article, I presented a list of all supported events. Go ahead and search through the list for the appropriate event codes, and create an application that catches the server's reply to the “WHOIS” command.
The last of the “who” commands is the “WHOWAS” command. It is accessed like this: