This page and all content Copyright ©2005-2007 Nathan E. Pralle
 
Installation and Setup for HooDaHek
Version 0.7
Caller ID and Notification System for Asterisk
Copyleft (C)2005
Nathan E. Pralle
www.nathanpralle.com

BASIC OPERATION
  1. Caller calls into your Asterisk system via IAX, SIP, or PSTN
  2. Your extensions.conf context sends Caller ID (CLID) information to the hoodahek_dbhandle AGI script.
  3. hoodahek_dbhandle determines if the caller has called before based on the phone number.
    • If YES, it changes the CallerID to what it looked up in the 'hoodahek' table (name and number).
    • If NO, it inserts a row into the 'hoodahek' table to identify the caller on subsequent calls. The initial information used is the information supplied by the telephone company's CLID transmission. If configured, an email is sent notifying someone of the insertion into the system. This information can then be edited by the CGI scripts provided.
  4. Control returns to the dialplan.
  5. The dialplan passes information to the hoodahek_notify AGI script.
  6. hoodahek_notify looks to see if stored information about this caller is in the 'hoodahek' table.
    • If YES, hoodahek_notify uses this information to announce the caller via AIM/MSN/Jabber/Email/SMS.
    • If NO, hoodahek_notify looks to see if the CLID system has passed information about the caller (name, number)
      • If YES, it uses that information to announce the caller via AIM/MSN/Jabber/Email/SMS.
      • If NO, it announces the caller anonymously.
  7. hoodahek_notify uses the information it found to write a call queue file in the queue directory for the hoodahekbot.
  8. Control passes back into the dialplan to do whatever comes next.
  9. hoodahekbot scans the queue directory every second to look for new call files. When it finds one, it opens it, scans in the lines, and deletes it. It then sends an IM to each recipient as configured.
PREREQUISITES:
Several things are needed prior to making this system work:
  • Asterisk System: A working Asterisk PBX system (www.asterisk.org) is required. This package has been tested and ran on Asterisk 1.0.7. Other versions may or may not work.
  • Asterisk CDR MySQL: A working cdr_mysql implementation, so that Call Detail Records are being stored in the standard MySQL table. Setup Information Here
  • Apache web server: A working and running installation of Apache is required for using the CGI scripts to manipulate the database. (www.apache.org)
  • Perl 5: A working Perl 5 installation is required. (www.cpan.org)
    Extra modules required are listed in the Files section.
  • MySQL Database: A working MySQL Database system is required. (www.mysql.org)
  • To run a notification bot you will need one or more of the following:
    • An AOL Instant Messenger account for the bot to run under. (www.aim.com)
    • An MSN Messenger account (Passport account) for the bot to run under. (www.msn.com)
    • A Jabber account and server for the bot to run under, either on a public or private server. (www.jabber.org)

INSTALLATION
Installation is fairly straightforward. There are 3 main directories, 'agi', 'cgi', and 'bot'. Each of these contains files for each of the modules of HooDaHek. Other files are for various other things:
  • /README - A pointer README file
  • /README.html - This file.
  • /LICENSE - The License file
  • /Changelog - List of changes by version
  • /agi/hoodahek_notify - The AGI script that notifies the bot of an incoming call
  • /agi/hoodahek_dbhandle - The AGI script that manipulates the database on incoming calls
  • /agi/HooDaLib.pm - Supplimentary library scripts
  • /bot/MSN - MSN libraries and scripts
  • /bot/hoodahekbot.pl - The bot/notifcation script
  • /bot/HooDaLib.pm - Supplimentary scripts
  • /cgi/hoodahek.pl - CGI script to change the database
  • /hoodahek_table.sql - The SQL to create the hoodahek table.
  • /extensions.conf.example - An example of how to set up your extensions.conf file.


HOODAHEK_TABLE.SQL:
This is a user and table definition for the 'hoodahek' user and table. The user is created with all privileges; yeah, I know that's not the best, but if your machine is local and firewalled, etc etc, you won't be fussed. The table is simple enough as well. To apply the whole file, modify as you like, then run:

     mysql -u root -p asteriskcdrdb <hoodahek_table.sql

Give the root password and it'll run and return to the prompt.

This assumes that your cdr_mysql table is named 'astersiskcdrdb' and not something like 'mypants'. Change as appropriate.



HOODAHEK_DBHANDLE and HOODAHEK_NOTIFY:
Open each file and look at the Configuration Options. Adjust as necessary.

Place both of these files inside your Asterisk AGI directory, usually located in: /var/lib/asterisk/agi-bin/ You might want to check to make sure you have all the necessary Perl modules installed by using:
     perl -c /var/lib/asterisk/agi-bin/hoodahek_notify
     perl -c /var/lib/asterisk/agi-bin/hoodahek_dbhandle

If you see errors, run for your life. Get the appropriate modules from CPAN (search.cpan.org).



EXTENSIONS.CONF.EXAMPLE:
This is an example extensions.conf. This is by NO MEANS COMPLETE! You'll have to add this functionality to your own extensions.conf file, usually located in /etc/asterisk/extensions.conf.

Syntax for hoodahek_dbhandle:      This script handles the database insertion for new callers. If all you want to do is keep track of who has called, this is all you really need. Syntax is CALLERIDNAME, CALLERIDNUM, UNIQUEID, as shown below:

     exten => _X.,2,agi,hoodahek_dbhandle|${CALLERIDNAME}|${CALLERIDNUM}|${UNIQUEID}

Syntax for hoodahek_notify:      This script handles the notification of an incoming call via AIM/MSN/Jabber. If you don't want this, don't add it to your extensions.conf. Syntax is CALLERIDNAME, CALLERIDNUM, phone they are calling. IE: If they're calling the landline, I put "landline". Or the IAX line is "VoIP Phone". Don't include quotes.

     exten => s,2,agi,hoodahek_notify|${CALLERIDNAME}|${CALLERIDNUM}|landline

At this point, you can reload the Asterisk extensions and test it:

     asterisk -vvvvvvvvvvvvvvvvvvvvr

     > extensions reload

Place an incoming call. You should see the AGI scripts run. If you don't, you didn't set it up right. Nothing else should happen, really. We haven't set up the notification bot yet. :) HOWEVER -- if you look in your 'hoodahek' table of the MySQL database, you should see an entry for the call. If not, check your setup.

Easy way to look: mysqldump -u hoodahek -p asteriskcdrdb hoodahek
Give the password and it'll dump the table contents.

Good? Good. Quit out of the Asterisk CLI.



HOODAHEKBOT.PL:
Now it's time to setup the AIM/MSN/Jabber bot that will notify you of an incoming call.

Make sure you have the right Perl modules installed:
     perl -c hoodahekbot.pl

First, create the location where it will live: (as root)
     mkdir /usr/local/hoodahekbot
     mkdir /usr/local/hoodahekbot/queue
     chmod 777 /usr/local/hoodahekbot/queue

Copy the files:
     cp hoodahekbot.pl /usr/local/hoodahekbot
     chmod 755 /usr/local/hoodahekbot/hoodahekbot.pl

Open /usr/local/hoodahek/hoodahekbot.pl in your favorite text editor and modify anything underneath the Configuration Options that needs to be adjusted. Be sure to set the username and password (optionally: server) of your specified AIM/MSN/Jabber user correctly. You must "turn on" whichever bot(s) you want to use -- make sure to find those sections.

Now, start it up:
     /usr/local/hoodahekbot/hoodahekbot.pl &

Assuming you're logged into AIM/MSN/Jabber with a different username, and you have your bot in your buddy list, you should see them log on. Nifty. If not, and the process quits, find out why. (usually has to do with a directory permission or something similar) The bot logs ALL activity to /var/log/messages. I highly recommend you startup 'tail' to watch that file while you're playing with the bot, like so:
tail -f /var/log/messages
XWindows Users: Note that if you start this within an xterm/konsole/etc. window, you'll want to close the window by typing 'exit' and not clicking the X in the corner or it'll kill the process. :)

Now make a phone call into your Asterisk box. Assuming you have your extensions.conf set up correctly, and the bot running correctly, you should get an IM from your bot saying who called.

COMMANDS: HooDaHekBot responds to 4 different commands: 'name', 'phone', 'last', and 'quitnow'. It will ONLY respond to someone who is in its notify list -- IE: someone it sends notification of a call to. This is for security reasons, of course. The usage is as such:
  • name - This command is to search for a caller ID by name, case insensitive. Examples:
    • name:nathan
    • name:han
    • name:john
  • phone - This command is to search for a caller ID by phone number. Examples:
    • phone:6415553333
    • phone:641555
    • phone:6
  • last - This command shows you the last n phone calls into the system. Used without a parameter it'll show you the last call. With a parameter it'll show you the last n calls. Examples:
    • last
    • last:3
    • last:20
  • quitnow - This command will shut down the notification bot immediately. You don't need an example, do you?
The bot will then respond to these commands after you type them. Search results, if there are more than one, will take a bit of time to come back. This is because AIM and MSN have implemented a "flood" barrier that if you type too fast or send too many IMs too quickly, it'll drop your messages for a bit. It is a real pain in the ass, but what do you do? So the bot will sleep between 2 and 5 seconds between each IM, which is each resulting row. Hopefully in the future I can figure out how to get around this better.

HIRES TIMING: The bot can take up to 1 second after a call comes in to let you know who it is. You can significantly reduce this delay by activating HiRes timing. Install the Time::HiRes Perl module and then set $use_hires=1 inside hoodahekbot.pl.



HOODAHEK.PL:
CLID is pretty decent but not all names come through correctly, or the number does but not the name, or any number of screwups. Here's where the real power of the extra database comes in -- the ability to customize the CLID name.

Copy hoodahek.pl to your cgi-bin directory on the same server. Load it up in a web browser. Assuming all is well with the world, you should see a listing of all entries in the 'hoodahek' table. If not, something's messed up. You can see that there is an update button to allow you to change the CLID name. Do so as you please. The next time you receive a call, you'll notice that both the CallerID of the call is changed and the CallerID displayed by hoodahek_notify is changed; it'll use that name instead of the default name provided by the telco. Groooovy.



KNOWN ISSUES:
  • The hoodahekbot.pl seems to only stay up for a certain amount of time and then it'll hang, as if AOL/MSN saw it was idle or something and made it go dormant. I have a cronjob that kills it every night and starts it back up just to keep it fresh. Something like:
         0 4 * * * killall hoodahekbot.pl
         1 4 * * * cd /usr/local/hoodahek;./hoodahekbot.pl &
    Your mileage may vary on this one.



    QUESTIONS/SUPPORT/COMMENTS:
    Please feel free to use my Contact Page on my website to contact me if you have questions, ideas, concerns, etc. I'll try to help the best I can, but I can't promise everything.



    CONTRIBUTORS:
    The MSN code was taken/derived/bastardized from work by: William Kelley, Eric Hodges, Matt Austin, Siebe Tolsma, Talha Asad, Steve Fullbrook, and of course, the original MSN.pm writer and all others who made minor improvements. Your efforts saved my ass time and time again. Thank you every so much.

    The AIM/Jabber code was taken from many different sources around the 'net; thanks to those who went before me and figured out the dirty, sweaty bits first. You folks have way too much time on your hands. :)

    Thanks to Adam Bumpus for the inspiration and hints on how to do the SMS stuff. He wrote (and figured out) a very cool SMS mailing script to scan your inbox and text-message you every time you get a new mail. Isn't that the shit? Check it out here.

    Thanks to Steven BerkHolz for suggesting that I actually modify the CLID. Gee, now that was obvious. :) Thanks, Steven, for pointing out my dufusness.

    I hope you enjoy HooDaHek. I've had fun writing it.