Previous Section  < Day Day Up >  Next Section

Hack 42 Blog from IRC

figs/expert.gif figs/hack42.gif

Your IRC channel is essentially a community of people. Publish the thoughts of this community by using an IRC bot to place them on a group web log.

Blogging has taken the Internet by storm in recent years, and why should your IRC channel be left out? Most channels see people regularly paste URLs for the entertainment and information of those present, so why not collect them and stick them on a web site for future reference? Here's a flexible and easily extensible way to do it using a bot. The bot stores any incoming message that contains a URL in a MySQL database. A separate PHP script is used to display all blog entries made in the last seven days.

You will need:

For the sake of simplicity, it will be assumed here that everything's running on the same computer.

First, set up your database according to the following scheme:


    commenter TINYTEXT NOT NULL,


    timestamp TIMESTAMP NOT NULL,

    PRIMARY KEY (timestamp) 


Next, create a user with permissions to access that table:

GRANT SELECT, INSERT ON database.weblog TO weblogbot@localhost

        IDENTIFIED BY 'password';

Replace database with the name of your MySQL database and password with a password of your choice.

6.4.1 The Code

Now let's write the code for the bot. The Weblog class inherits functionality from the PircBot class to allow it to connect to IRC, so you will also need to ensure that you have pircbot.jar at hand.

Create a file called

import org.jibble.pircbot.*;

import java.text.*;

import java.sql.*;

import java.util.regex.*;

public class Weblog extends PircBot {

    private String dbHost = "localhost";

    private String dbName = "database";

    private String dbUserName = "weblogbot";

    private String dbPassword = "password";

    private Pattern urlRegEx;

    private Matcher urlMatcher;

    public Weblog (String botName) {



        // Precompile the URL-matching regular expression.

        urlRegEx = Pattern.compile("^.*(((http|https|ftp)://|www.)" +



    public void onMessage (String channel, String sender,

            String login, String hostname, String message) {

        urlMatcher = urlRegEx.matcher(message);

        if (urlMatcher.matches( )) {

            try {

                // Connect to the MySQL database.

                Class.forName("com.mysql.jdbc.Driver").newInstance( );

                Connection conn = DriverManager.getConnection(

                    "jdbc:mysql://" + dbHost + "/" + dbName + 

                      "?user=" + dbUserName +

                      "&password=" + dbPassword);

                // Prepare and execute the database query.

                PreparedStatement pstmt = conn.prepareStatement(

                   "INSERT INTO weblog (comment, commenter) VALUES(?, ?);");

                pstmt.setString(1, message);

                pstmt.setString(2, sender);

                pstmt.executeUpdate( );


            catch (Exception e) {

                // Something went wrong...





Finally, we'll need some code to create the blog from the database. The following bit of PHP—saved on the web server as weblog.php—does the trick. It executes an SQL query to get all the entries out of the database that are less than seven days old. These are then displayed in a table.



   <title>IRC Hacks weblog example</title>








  // Set up the SQL query.

  $earliest = mktime(0, 0, 0, date("m"), (date("d") - 7), date("Y"));

  $querytext = "SELECT UNIX_TIMESTAMP(timestamp), commenter, comment " .

               "FROM weblog WHERE UNIX_TIMESTAMP(timestamp) > $earliest " .

               "ORDER BY timestamp DESC LIMIT 200";

  // Connect to the database.

  $link = @mysql_connect("localhost", "weblogbot", "password")

          or die("Could not connect to database server");


          or die("Could not select the database");

  // Execute the SQL query.

  $result = mysql_query($querytext)

          or die("database query failed");

  // Format the blog entries.

  while ($row = mysql_fetch_array($result, MYSQL_ASSOC)){

    $comment = strip_tags($row[comment]);

    // Turn URLs into hyperlinks.

    $comment = preg_replace("((((http|https|ftp)://|www.)" .


               "<a href=\"\\1\">\\1</a> ", $comment);

    $time = date("r", $row["UNIX_TIMESTAMP(timestamp)"]);

    print "<tr><td>" . $time . "</td><td>" . $row[commenter] .

          "</td><td>" . $comment . "</td></tr>\n";






Finally, you just need a main method to tell the bot which server to connect to and which channels to join. Save the following as

public class WeblogMain {


    public static void main(String[] args) throws Exception {

        Weblog bot = new Weblog("blogger");







6.4.2 Running the Hack

When you first visit weblog.php with your web browser, the page will obviously be empty. To populate the database, you must make the IRC bot join a channel and send it some URLs.

Compile the bot like so:

C:\java\Weblog> javac -classpath .;pircbot.jar;mysql-connector.jar *.java

Then run the bot:

C:\java\Weblog> java -classpath .;pircbot.jar;mysql-connector.jar WeblogMain

Once the bot is running, it will add all messages containing URLs to the database, ready to appear on the web page.

6.4.3 The Results

As you can see from the rather plain output in Figure 6-2, you could easily spice up the appearance with a few images and other HTML hacks. Other possibilities are to allow searching of the database for entries that are older than seven days or to provide alternative output formats such as RSS.

Figure 6-2. Output from the simple Weblog bot

A more advanced implementation of a blog bot called scot is available from Steve Jolly's web site at

Steve Jolly

    Previous Section  < Day Day Up >  Next Section