Whatever I'm enthused about at the moment ;-p

Simple Eggdrop Bot On Ubuntu

2010-05-27 - Category: computing

<< The Difference Between A Disk And A Drive Slow Wall Street recovery >>
Here's something for all you IRC kiddies to sink your teeth into!  :-D Yesterday, I was hanging out in the usual IRC channels I gab in.  In one of these channels, there is a friend or two of mine that shares a common interest of mine: stock investing.  One of these friends has a nifty little bot for getting stock quotes.  You simply type "qq:pir" to get the latest quote on Pier 1 Imports, for instance.  qq is the IRC nick of the bot and pir is the stock symbol for Pier 1 Imports.  Simple and useful! His bot, however, did not support stock index quotes.  Stock indexes, such as the Dow Jones Industrial Average and the S&P 500, are pretty useful since they give a general overview of what the economy is doing.  If the Dow or the S&P goes up, it makes it much easier for most other stocks to follow.  If the Dow and S&P are flat or going down, most other stocks are going to show similar patterns.  Ok, this is not a finance seminar, so I'll get back to talking about technical things.  :-) I had already developed a very short, sweet little Perl script for grabbing the Dow quote from msn.com.  Why did I choose msn.com?  Well, they get so much traffic and they're such a huge, conglomerate, find-everything-here portal site that they probably don't care if someone is using programs to spider their site or grab their info.  Besides, this is my insidious little stab at Microsoft!  AR HAR HAR HAR.  Ahem.  No, not really. So, here's the little Perl script:
#!/usr/bin/perl

use LWP::Simple;

###############################################
$url = "http://moneycentral.msn.com/home.asp";
###############################################

$html = get $url;
@html = split(/\n/,$html);

foreach $a (@html) {
    if ($a =~ /%24INDU/) {
        getquote();
        last;
    }
}

sub getquote {
@one = split(/>/,$a);
$one[19] =~ s/<\/div//;
print "DJIA: $one[19]\n";
}
There's lots of ways to parse HTML.  This is my way.  :-)  This script will output "DJIA: 10,123" or whatever number the Dow happens to be at.  I mostly follow the Dow, so I didn't bother with the S&P data. Next, I used aptitude to install eggdrop on one of my home Ubuntu servers.  Eggdrop is the world's most popular IRC bot, and it has more features than I would ever use.  The next step was to follow the instructions listed in the README file, which is located here:
/usr/share/doc/eggdrop/README.Debian
This involved creating an eggdrop user and group, and then reading through and editing the giant configuration file (yes, this takes a while).  I pointed this to an empty channel on irc.freenode.net for testing purposes, and then fired it up. Eggdrop scripts are written in tcl.  I don't know the tcl language, but it appears somewhat simple.  I downloaded some eggdrop scripts online that are designed for executing and displaying the output of other programs, since this was my idea for utilizing my handy dandy Perl script.  Here was the tcl script I came up with:
set userflag "-"
set trig "!dow"
bind PUB $userflag $trig main_func

proc main_func {nick uhost hand chan arg} {
set fp [open "| ./dow.pl"]
set data [read $fp]
close $fp
putserv "PRIVMSG $chan :$data"
}
This script will execute my Perl script (dow.pl) whenever someone types "!dow" in the channel.  It turned out that this system works pretty well!  My stock watching finance friends and I are now happily watching the Dow and chatting vigorously about how the economy is going down the toilet on IRC!  Isn't open source software wonderful?
Copyright © 2001 - 2014, Korey Pelton -- Static HTML blog generated with PeltonBlog