Hack89.IRC Your Web Application


Hack 89. IRC Your Web Application

Use Net_SmartIRC to have a conversation with your web application through your web server.

Sometimes a web page is not the most convenient way to talk with an application. A lot of people are using instant messaging and chat systems (like IRC) to converse with each other. So why not allow them to have a conversation with your web application?

Figure 9-8 illustrates the user and a bot having a conversation through an IRC server. The bot is run on the command line as a standalone PHP process. The Net_SmartIRC PEAR module [Hack #2] allows your web application to log into an IRC server and respond to commands.

9.5.1. The Code

Save the code in Example 9-8 as ircbot.php.

Figure 9-8. The user and the bot talking over IRC


Example 9-8. A simple IRC bot
 <?php include_once('Net/SmartIRC.php'); require_once('DB.php'); $dsn = 'mysql://root:password@localhost/books'; $db =& DB::Connect( $dsn, array( ) ); if (PEAR::isError($db)) { die($db->getMessage( )); } class dbbot { function listdata(&$irc, &$data) { global $db; $irc->message(SMARTIRC_TYPE_CHANNEL, $data->channel, 'Books: '); $res = $db->query( "SELECT name FROM book", array( ) ); while( $res->fetchInto( $row, DB_FETCHMODE_ASSOC ) ) { $irc->message(SMARTIRC_TYPE_CHANNEL, $data->channel, ' '. $row['name'] ); } } } $host = "localhost"; $port = 6667; $nick = "DBBot"; $chan = "#db"; $bot = &new dbbot( ); $irc = &new Net_SmartIRC( ); $irc->setUseSockets( TRUE ); $irc->registerActionhandler( SMARTIRC_TYPE_CHANNEL, '^list', $bot, 'listdata' ); $irc->connect( $host, $port ); $irc->login( $nick, 'Database bot', 0, $nick ); $irc->join( array( $chan ) ); $irc->listen( ); $irc->disconnect( ); ?> 

9.5.2. Running the Hack

Install the Net_SmartIRC PEAR module [Hack #2] and then run the ircbot.php script like this:

 % php ircbot.php 

Use your IRC client to connect to the IRC server specified in the script. Then join the #db channel and type the text "list" into the channel. As you can see in Figure 9-9, the IRC bot responds with a list of records from the books database.

Figure 9-9. Talking with your application through IRC


Your application will undoubtedly reference your own business logic and have a different set of commands and responses. You can add more commands by calling registerActionHandler( ) with other command handlers.

We used this mechanism at a prior job to interact with our bug database. IRC used the bug name, which was the same as was used in the bug database. Then, a simple set of commands listed active bugs or the bugs fixed that week, described a bug in detail, marked a bug as fixed, or even downloaded the files associated with the bug.


9.5.3. See Also

  • "Send RSS Feeds to Your IM Application Using Jabber" [Hack #88]



PHP Hacks
PHP Hacks: Tips & Tools For Creating Dynamic Websites
ISBN: 0596101392
EAN: 2147483647
Year: 2006
Pages: 163

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net