Whispers from Beyond a Listening Bot on an IRC Channel Wakes up When It Hears Certain Keywords and Notifies A

Whispers from Beyond a Listening Bot on an IRC Channel Wakes up When It Hears Certain Keywords and Notifies A

FEATURES Perl: IRC Monitor Judith: The code font is still Judith: I decreased the font difficult to see on the printed size of @T so the whole title page. Can we get it darker?? would fit. If it’s too small, just A listening bot for IRC -rls use "Whispers." Thnx. -rls Whispers from Beyond A listening bot on an IRC channel wakes up when it hears certain keywords and notifies a defined user via instant messaging. By Mike Schilli pen source projects like Catalyst the eavesdropped text message to a pre- lar expressions stored in the @KEYWORD_ use IRC channels to provide sup- defined Messenger account (Figure 3). LIST array fits the bill, line 27 of the port; experts wait for user re- The Messenger service then notifies the same file triggers the ymsend script in the Oquests and then step in to give user, who immediately interrupts work, same directory. This script accepts the help. That said, IRC chat can make it dif- turns to the IRC channel, and contrib- message text at the command line, logs ficult for the helpers to focus on their on- utes expert knowledge to help hapless in to the Web API, performs a couple of going work. And, if the channel is full, newbies find their way around. authorization steps based on the OAuth conversations are always in full swing. protocol, and finally sends the message The Perl bot I will describe in this article Sniffing Messages text to the user defined in $recipient in listens on a specific IRC channel and no- Listing 1 [2] derives a YMBot class from line 11 of Listing 2. tifies its master when certain keywords the Bot::BasicBot base class on CPAN The script needs to jump through a occur. and overloads its said() method, which few authentication hoops first, requiring The first step in creating an IRC bot, is the bot calls whenever a user says some- the name of the sending Messenger user, fairly simple. After all, the CPAN thing on an IRC channel. Along with a their password, an API key that you Bot::BasicBot module that I’ve covered reference to the object, the bot passes a need to retrieve from the Yahoo! Devel- before provides an easily extensible hash data structure to the method, con- oper Network [3], and a shared secret framework for any kind of IRC bot. But taining the username in the who field and for the application. how can the bot attract the attention of the message text in body. its hard-working user? Instant messaging In this callback method, the bot then OAuth Jungle with pop-up dialogs is one useful ap- calls the keyword_match() function, de- The OAuth protocol [4] [5] lets an au- proach, and Pidgin provides a versatile fined in line 58, and the function com- thenticated user pass a token to an appli- client that supports common protocols pares the message text with a dictionary cation, which then acts on behalf of the such as Yahoo! Messenger, Google Talk, of keywords parsed from user for a certain period of time. The AIM, or MSN. the ~/. irc2ym-key- beauty of the concept is that words file (Figure users don’t have to tell the Chat via Web API 4). The script third-party application Some time ago, Yahoo! opened a web parses the en- their password directly. API [1] to its Messenger service whereby tries in the file The protocol authenti- users would first log in and then use and stores cates just like other on- HTTP requests to exchange messages them in the line offers at Yahoo’s with other Yahoo! Messenger users. The global @KEY- login screen, which then bot script introduced here, irc2ym, joins WORD_LIST issues the token for the an IRC channel and then just shuts up array. If one application to use. The and listens (Figure 1). If a chat user of the regu- concept makes a lot of mentions one of the keywords (Figure 2) sense with web applica- in the ~/.irc-keywords file, the bot tions, because users get launches the ymsend script, which logs trained never to enter into the Messenger Web API and sends their credentials on third-party MIKE SCHILLI sites, but Mike SchilliMike: works as a software irc2ym-keywords??engineer with Yahoo! -rlsin Sunny- vale, California. He can be con- tacted at mschilli@perlmeis- ter.com. Mike’s homepage can be found at http://​­perlmeister.​­com. 62 FEBRUARY 2011 ISSUE 123 LINUX-MAGAZINE.COM | LINUXPROMAGAZINE.COM FEATURES Judith: I decreased the font Perl: IRC Monitor size of @T so the whole title would fit. If it’s too small, just use "Whispers." Thnx. -rls Figure 1: The ymbot does nothing until Figure 2: An IRC participant named “hubbel- Figure 3: The bot has forwarded the message somebody mentions one of the predefined quadrat” mentions the “cpan” keyword, and to the Y! Messenger user. keywords. the eavesdropping bot notifies the user. In line 45, the ymsend script logs in the only on the original login screen of the the script runs very rarely and immedi- user as $user and $passwd at the URL provider. This issue is less evident with ately quits after sending the message, stored in $login_url. Yahoo! sends back desktop applications like my script, storing the token wouldn’t offer signifi- a request token in the body of the re- which need the user’s password anyway cant advantages. Thus, the script re-au- sponse. to authenticate at the login site behind thenticates against the Yahoo login page, The script then sends the token and the scenes. passing in a username and password the API key, with a matching secret key, In the case of Y! Messenger, the token (hard coded as $user and $password in secret, to the next URL, $auth_token_ allows the application (i.e., the script) to ymsend) with every run, then picks up a url, which then generates an access send messages to the IM network and re- new access token and uses it to run the token, oauth_token, and an oauth_token_ ceive responses for one hour. Because send command in the web API. secret. The web server response uses LISTING 1: irc2ym 01 #!/usr/local/bin/perl -w 30 57 ############################# 02 use strict; 31 warn "$ymsend failed: $!" 58 sub keyword_match { 03 use local::lib; 32 if $rc; 59 ############################# 04 33 } 60 my ($said) = @_; 05 ############################# 34 61 06 package YMBot; 35 return $data; 62 for 07 ############################# 36 } 63 my $regex (@KEYWORD_LIST) 08 use base qw( Bot::BasicBot ); 37 64 { 09 use FindBin qw($Bin); 38 ############################# 65 return 1 10 39 sub keyword_list_read { 66 if $said =~ /$regex/i; 11 my $ymsend = "$Bin/ymsend"; 40 ############################# 67 } 12 my ($home) = glob "~"; 41 if (!open FILE, 68 return 0; 13 my $KEYWORD_LIST_FILE = 42 "<$KEYWORD_LIST_FILE") { 69 } 14 "$home/.irc2ym-keywords"; 43 warn "$KEYWORD_LIST_FILE ", 70 15 my @KEYWORD_LIST = (); 44 "not found"; 71 ############################# 16 45 return; 72 package main; 17 keyword_list_read(); 46 } 73 ############################# 18 47 74 use Bot::BasicBot; 19 ############################# 48 while (<FILE>) { 75 20 sub said { 49 chomp; 76 my $bot = YMBot->new( 21 ############################# 50 s/#.*//; 77 server => 22 my ($self, $data) = @_; 51 next if /^\s*$/; 78 "irc.freenode.com", 23 52 push @KEYWORD_LIST, $_; 79 channels => ["#ymtest"], 24 if ( keyword_match( 53 } 80 nick => "ymbot", 25 $data->{body}) ) { 54 close FILE; 81 name => "Relay to Y!M", 26 55 } 82 charset => "utf-8", 27 my $rc = system( $ymsend, 56 83 ); 28 "$data->{who} said: " . 84 29 "'$data->{body}'" ); 85 $bot->run(); LINUX-MAGAZINE.COM | LINUXPROMAGAZINE.COM ISSUE 123 FEBRUARY 2011 63 FEATURES Perl: IRC Monitor encoded correctly. The qquote function exported by the Sysadm::Install CPAN module makes Figure 4: The list of keywords to which the light work of this task. IRC bot will react. Figure 7: The application requires read/ write access to Yahoo! Judith: Creating the Messenger data. These figs the format … , field=value&field=value Auth Token are OK. which the script simply stores in a URI To create an authentica- -rls object in line 86 as a made-up query part tion token with a secret of the URL. It then tells the query_form for the newly created ap- method to parse the object – this works plication (i.e., the ymsend because the data are formatted exactly script), the API developer like a URL using query parameters. must click through My The combination of token and secret Projects and New Project identifies the application as authorized (Yahoo! account re- by the user to use the web service on his quired) on the Yahoo! De- behalf. The script then passes these on veloper Network [3]. to the Messenger web service using the These steps will take you Figure 8: The ready-made API keys for creating the Y! Mes- $session_url, which starts a new Mes- to the pop-up box shown senger client. senger session and logs in the $user into in Figure 5. Because this the Yahoo! Messenger network. Once the is not a web application running in a the Sign Up link to let yahoo.com take session has started, other IM users see browser, but a desktop client, you’ll you to the account registration page. the user appear in their buddy lists, and need to select Or an application using After this, you only need to create a the script uses the POST method in lines these APIs: BOSS, Contacts, Mail, … . list of keywords in ~/.irc-keywords and Mike: 148-155 to send the message passed in at In the form that appears, the developer launch the irc2ym bot. The bot could irc2ym-key- the command line to the Messenger user must enter a short name (e.g., irc2ymes- take up to 20 seconds to log in to the words?? -rls defined in $recipient (who should be senger) and a couple of words of expla- preset channel on a heavily used IRC logged in).

View Full Text

Details

  • File Type
    pdf
  • Upload Time
    -
  • Content Languages
    English
  • Upload User
    Anonymous/Not logged-in
  • File Pages
    4 Page
  • File Size
    -

Download

Channel Download Status
Express Download Enable

Copyright

We respect the copyrights and intellectual property rights of all users. All uploaded documents are either original works of the uploader or authorized works of the rightful owners.

  • Not to be reproduced or distributed without explicit permission.
  • Not used for commercial purposes outside of approved use cases.
  • Not used to infringe on the rights of the original creators.
  • If you believe any content infringes your copyright, please contact us immediately.

Support

For help with questions, suggestions, or problems, please contact us