in Projects

Using WWW::Mechanize to Post to a Forum from IRC

The DeadCoderSociety IRC channel is easily our busiest electronic meeting place. The IRC channel has overshadowed the once-popular forum, turning it into a warehouse for quoted IRC conversations. The new automatic quote feature allows a person to quote a recent conversation, edit it in a quiet and convenient way, and post it to the forum. It saves time and hassle by minimizing interaction with the forum and allowing the user to stay in IRC. The workflow looks like this:

  1. Interesting/funny conversation happens in the channel.
  2. Someone says !quote.
  3. The bot messages the quoter with the last 13 lines of the conversation.
  4. From here, the person can “keep” or “delete” specific lines, or chunks of lines.
  5. Each action results in a paste of the edited conversation until the person is happy with the preview.
  6. Person messages the bot with “go” and it shows up on the forum.

My bot already logs the conversation, so it just runs tail -n13 on the log file (prepending line numbers to the front) and sends a private message to the caller. The actual editing is a simple combination of regular expressions and array splicing. Editing continues until the user posts the message or quotes something new.

WWW::Mechanize makes posting the final message simple:

sub _postIt {
  my $message = shift;
  my $mech = WWW::Mechanize->new();
 
  # go to the login page
  $mech->get("http://forum.deadcodersociety.org/index.php?action=login");
  sleep(5);
 
  # submit credentials
  $mech->submit_form(
    with_fields => {
      "user"    => "stanbot",
      "passwrd" => "password"
    }
  );
  sleep(5);
 
  # grab the "Quotes" thread
  $mech->get("http://forum.deadcodersociety.org/index.php/topic,78.0.html");
  sleep(5);
 
  # grab the reply link and click it
  $mech->follow_link(text_regex=> qr/REPLY/i);
  sleep(5);
 
  # submit our new quote
  $mech->submit_form(
    with_fields => {
      "message" => "[quote]" . join("",@$message) . "[/quote]",
      "subject" => "Re: DCS Quotes!",
    }
  );
}

I added the calls to sleep because I was triggering the forum’s bot-detection code. The submit_form method is smart and will automatically detect the form based on the fields passed in. The channel is made aware of the post after it finishes since the bot scans for new messages posted to the forums.

We’ve had this for about two months now and it’s been used by everyone for nearly every quote. It encourages people to quote more conversations and ultimately strengthens the bond in the community. Pretty good for 33 lines of code!

Write a Comment

Comment