O'Reilly Hacks
oreilly.comO'Reilly NetworkSafari BookshelfConferences Sign In/My Account | View Cart   
Book List Learning Lab PDFs O'Reilly Gear Newsletters Press Room Jobs  

Buy the book!
IRC Hacks
By Paul Mutton
July 2004
More Info

Search the Web with Google
Performing Google searches from IRC is not only convenient, but also efficient. See how fast you can Google for something on IRC and click on the URL highlighted by your IRC client
The Code
[Discuss (0) | Link to this hack]

When someone pops into your IRC channel with a question, you can bet your life that 9 times out of 10, he could have easily found the answer on Google. If you think this is the case, you could tell him that—or you could do it slightly more subtly by suggesting a Google search term to an IRC bot, which will then go and look for a result.

Most IRC clients are capable of highlighting URLs in channels. Clicking on a highlighted URL will open your default web browser and load the page. For some people, this is a lot quicker than finding the icon to start your web browser and then typing or pasting the URL. More obviously, a single Google search will present its result to everybody in the channel.

The Code

Create a file called GoogleBot.java:

import org.jibble.pircbot.*;
import com.google.soap.search.*;

public class GoogleBot extends PircBot {
    // Change this so it uses your license key!
    private static final String googleKey = "000000000000000000000000000000";
    public GoogleBot(String name) {
    public void onMessage(String channel, String sender, String login,
            String hostname, String message) {
        message = message.toLowerCase( ).trim( );
        if (message.startsWith("!google ")) {
            String searchTerms = message.substring(8);
            String result = null;
            try {
                GoogleSearch search = new GoogleSearch( );
                GoogleSearchResult searchResult = search.doSearch( );
                GoogleSearchResultElement[] elements =
                        searchResult.getResultElements( );
                if (elements.length == 1) {
                    GoogleSearchResultElement element = elements[0];
                    // Remove all HTML tags from the title.
                    String title = element.getTitle( ).replaceAll("<.*?>", "");
                    result = element.getURL( ) + " (" + title + ")";
                    if (!element.getCachedSize( ).equals("0")) {
                        result = result + " - " + element.getCachedSize( );
            catch (GoogleSearchFault e) {
                // Something went wrong. Say why.
                result = "Unable to perform your search: " + e;
            if (result == null) {
                // No results were found for the search terms.
                result = "I could not find anything on Google.";
            // Send the result to the channel.
            sendMessage(channel, sender + ": " + result);

You now need a main method to tell the bot which channels to join. If you want, you can tell the bot to join more than one channel, but remember you are limited in the number of Google searches you can do per day.

Create the file GoogleBotMain.java:

public class GoogleBotMain {
    public static void main(String[] args) throws Exception {
        GoogleBot bot = new GoogleBot("GoogleBot");        

O'Reilly Home | Privacy Policy

© 2007 O'Reilly Media, Inc.
Website: | Customer Service: | Book issues:

All trademarks and registered trademarks appearing on oreilly.com are the property of their respective owners.