< Day Day Up > |
Hack 73 Protect the Channel Topic
Set your channel's topic to something inviting and friendly—and make sure it stays that way. The topic of a channel is surprisingly important. Not only is it the first thing people will see when they join the channel, but it is also visible when users perform the /list command (see [Hack #16] ). If users don't like the look of the topic, they certainly aren't going to consider joining your channel. For this reason, some people make efforts to ensure that the topic does not get tampered with. It is obviously desirable to stop people setting the topic to something that is abusive, but on the other hand, it is useful to let people change the topic so they can announce some important news to other users in the channel. 12.2.1 Topic Protection ModeThe simplest form of topic protection can be achieved by setting the mode of the channel to +t. If you are a channel operator, you can do this in most IRC clients by entering /mode #channel +t. Once this mode has been set, only other channel operators will be able to set the topic. Topic protection can be removed by entering /mode #channel -t. Allowing the topic to be changed only by channel operators is generally safe, as it is assumed that only responsible users will ever be granted operator status. However, this is not very friendly to the nonoperator users of that channel; the majority of which will be well behaved and may even want to add important information to the topic. Of course, they could just ask a channel operator to add something to the topic, but this gets rather annoying after a while, and no operators may be watching the channel at that moment. 12.2.2 Topic ErasureUsing an IRC bot or script to manage topic changes is a better way of handling things, as it can be present all the time and do everything automatically. Opening up the channel by applying mode -t will allow anybody to set the topic under the watchful guise of the bot. A trivial task that a bot can carry out is to make it easy to ensure that nobody erases the topic completely. If a rogue user were to erase the topic, all the bot would have to do is set it back to what the topic used to be. The rogue user would be free to erase the topic again, but the bot would just keep resetting the old topic until he got bored and left. An alternative action would be for the bot to apply mode +t for a short period. 12.2.3 Formatting and ColorsSome users are put off by a channel that is brightly colored or heavily formatted. The use of these features certainly makes the topic stand out more, but they often have an adverse effect on legibility—and can make your channel seem cheesy. An IRC bot can easily sit in a channel and ensure that topics never contain any strange formatting or colors. This can be done by refusing to accept the topic completely or by reformatting it without formats and colors. 12.2.4 Profanity FilteringIRC bots are ideal for ensuring that topics never contain any rude words. If a user changes the topic to something that includes rude words, the bot could set the topic back to what it used to be, or some wildcard character could replace the rude words. Although the detection of rude words is difficult to achieve with the same accuracy as a human observer, a bot nonetheless has the advantage of being able to do it automatically. 12.2.5 The CodeThis bot will perform the filtering and erasure protection described so you don't have to. Save the following code as TopicBot.java: import org.jibble.pircbot.*; import java.util.*; public class TopicBot extends PircBot { private String oldTopic = null; private ArrayList badWords = new ArrayList( ); public TopicBot(String name) { setName(name); } public void onTopic(String channel, String topic, String setBy, long date, boolean changed) { if (changed && !setBy.equalsIgnoreCase(getNick( ))) { topic = topic.trim( ); String unformatted = Colors.removeFormattingAndColors(topic); if (topic.length( ) == 0) { // Prevent the topic from being removed. setTopic(channel, oldTopic); } else if (!unformatted.equals(topic)) { // Remove strange formatting from the topic. setTopic(channel, unformatted); } else if (containsBadWord(topic)) { // Set the old topic if the new one contains bad words. setTopic(channel, oldTopic); } } oldTopic = topic; } public void addBadWord(String word) { badWords.add(word.trim( ).toLowerCase( )); } // Returns true if the topic contains any bad words. public boolean containsBadWord(String topic) { topic = topic.toLowerCase( ); for (int i = 0; i < badWords.size( ); i++) { String word = (String) badWords.get(i); if (topic.indexOf(word) >= 0) { return true; } } return false; } } TopicBot observes topic changes in a channel by overriding the onTopic method and then checks to see if they are allowed. When the bot joins a channel, this method is called with the changed variable set to false. This is because the topic has not actually been changed as such, but is merely being sent to us by the IRC server. Whenever a user changes this topic, it will be set to true. Each time the topic changes, the bot will store the topic in the oldTopic field. If somebody tries to set an empty topic, the bot will immediately set the topic back to the old topic. If a user sets a topic that includes formatting or color, the bot will respond by setting the topic to the same text, but without the formatting or color. This preserves the content of the topic while also making it easier to read. If a topic contains "bad" words, the bot will set the old topic. The containsBadWord method is responsible for checking a String to see if it contains any of the bad words in the badWords list. Words must be added to this list with the addBadWord method. Create a file called TopicBotMain.java that will be used to connect to the IRC server and specify which bad words to use: public class TopicBotMain { public static void main(String[] args) throws Exception { TopicBot bot = new TopicBot("TopicBot"); bot.setVerbose(true); bot.connect("irc.freenode.net"); // Add some bad words. These are not allowed in topics. bot.addBadWord("bottoms"); bot.addBadWord("fudge"); bot.addBadWord("unix"); bot.joinChannel("#irchacks"); } } Notice that for simplicity, TopicBot stores the old topic as a String. This bot is therefore suitable for use only in a single channel. You could add support for multiple channels by replacing this String with a HashMap that maps a channel name to its topic, as with the WelcomeBot [Hack #64] . 12.2.6 Running the HackCompile the bot like so: C:\java\TopicBot> javac -classpath pircbot.jar;. *.java And run it: C:\java\TopicBot> java -classpath pircbot.jar;. TopicBotMain The bot will then leap into life and protect your channel topic from users who try to deface it. |
< Day Day Up > |