• Aucun résultat trouvé

Example: IMAP by Phone

Dans le document 436_XSS_FM.qxd 4/20/07 1:18 PM Page ii (Page 145-148)

Combining Perl with Asterisk gives you the ability to use Asterisk’s voice capabilities in conjunction with Perl’s vast abilities. Perl has a large array of libraries that can do anything from make a neural processing network to calculate which day Easter will fall on for a specific year. Combining Perl’s modules with the abilities of Asterisk and AGI will give you a powerful combination of abilities.

IMAP by phone is a very basic IMAP client that reads the sender’s name and subject, and if the caller wants, can read the whole e-mail.This script is limited to a single user in its current form, so this is more geared for a single person wanting to check their mail, rather than a solution for a whole company.

Ingredients

Asterisk

Perl, with the following modules:

1. Net::IMAP::Simple 2. Email::Simple 3. Asterisk::AGI

Festival TTS Engine, configured to work with Asterisk

Instructions

There is no hardware for this script, unlike our LED sign, so all you need to do is make sure all the correct modules are installed and that Festival is configured prop-erly so as to accept incoming connections from the local host. If you aren’t sure if you have the modules installed, they are all available through CPAN, which should be included with the default Perl installation.You can grab them by running the fol-lowing command either as root or the user that Asterisk runs under:

perl -MCPAN -e 'install <modulename>'

Replace <modulename> with one of the modules listed earlier. Run this com-mand once for every module. If you have never run CPAN before, the script will prompt you for configuration options.The instructions are fairly straightforward, and the default settings work 99 percent of the time.

If all that is set, place the following script into your AGI directory, which is /var/lib/asterisk/agi-bin by default.

#!/usr/bin/perl

# AGI Script that reads back e-mail from an IMAP account.

# Requires the Asterisk::AGI, Net::IMAP::Simple, and Email::Simple modules.

use Net::IMAP::Simple;

use Email::Simple;

use Asterisk::AGI;

my $server = '127.0.0.1'; #INSERT YOUR SERVER HERE my $username = 'username'; #INSERT YOUR USERNAME HERE my $password = 'password'; #INSERT YOUR PASSWORD HERE

$AGI = new Asterisk::AGI;

my %input = $AGI->ReadParse();

# Create the object

my $imap = Net::IMAP::Simple->new($server) ||

die "Unable to connect to IMAP: $Net::IMAP::Simple::errstr\n";

# Log on

if(!$imap->login($username,$password)){

$AGI->exec('Festival', 'Login failed ' . $imap->errstr);

$AGI->verbose('Login Failed: ' . $imap->errstr, 1);

exit(64);

}

# Retrieve all the messages in the INBOX my $nm = $imap->select('INBOX');

$AGI->stream_file('vm-youhave');

$AGI->say_number($nm);

$AGI->stream_file('vm-messages');

for(my $i = 1; $i <= $nm; $i++){

my $es = Email::Simple->new(join '', @{ $imap->top($i) } );

$AGI->stream_file('vm-message');

$AGI->say_number($i);

$AGI->exec('Festival', $es->header('Subject'));

$AGI->stream_file('vm-from');

AGI->exec('Festival', ('From'));

while($input eq ''){

$AGI->exec('Festival', "1, Play, 2, Next, Pound, Exit");

my $input = chr($AGI->wait_for_digit('5000'));

This script features many methods that have been discussed already in this chapter. It starts off by connecting to the IMAP server and logging in. It finds out how many messages are in the INBOX and then tells the user. From here, the script starts reading the messages, prompting the user to press 1 to read the message, press 2 to go to the next message, or press # to exit the script. It then continues to loop through every message until the user exits, or there are no more messages left.

After placing the script in the directory, make sure the script is executable by the user that the Asterisk process runs under.Then, open up your extensions.conf, which is the context you wish to make this script available to:

exten => 4627,n,AGI(imap.pl);

You may want to alter the extension, because that line puts it on extension 4627 (“IMAP”).You also might want to place an Authenticate() command before it as well since this script doesn’t have any kind of password support.

Once you’ve adjusted your extensions.conf, open up the Asterisk CLI and exe-cute a reload command. Now you should be ready to go.

Taking It for a Spin

The script can be accessed by dialing the extension you assigned it, in the context you put it in. If all goes well, you should hear the mechanical voice of Festival start reading your mail to you. If something isn’t right, open up the Asterisk CLI and see if any errors are displayed on the console. As mentioned earlier, sometimes Festival doesn’t play well with Asterisk and this causes the voice to sound like it is speaking in tongues and your console to start spitting out error messages repeatedly. Usually searching for these error messages on Google will show you how to solve whatever problem it is currently having.

Moving on from Here

This script has very basic functionality, allowing the user to only access their INBOX, and is limited to one user.This could easily be built upon to support a group and allow them to listen to their e-mail from their phone by adding an authentication system and the ability for users to manage their password and other settings. Support for multiple folders could also be added.This script is a fun weekend project just waiting to happen.

phpAGI

phpAGI is an AGI library designed for PHP. PHP started out as a Web-based lan-guage, but is slowly starting to creep into shell scripting as more and more people who cut their teeth learning the language start using it for shell work. phpAGI is available at http://phpagi.sourceforge.net/ and is maintained by a group of developers.

Dans le document 436_XSS_FM.qxd 4/20/07 1:18 PM Page ii (Page 145-148)