• Aucun résultat trouvé

Writing Macros

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

include-ing (other contexts within the current one is a handy way to save lines of code and duplication of code. Another easy way to increase efficiency and decrease code duplication is through Asterisk’s macro abilities. Macros can be described as special contexts that accept arguments.They allow for more flexibility than contexts, and allow common tasks to be automated and not repeated.

In our previous examples, if someone dialed an extension, it rang a channel. It would continue ringing the channel until someone picked up, or the call terminated.

What happens if we want to have that extension drop to voice mail playing the user’s

“I’m not here” message after 20 seconds of ringing, or playing the user’s “I’m cur-rently on the phone” message if the phone line is busy?

Example 3.5 Creating Voice Mail Support for Existing Extensions without the Use of Macros

exten => s-100-BUSY,1,Voicemail(b100)

Yikes.That got complicated quickly. Can you imagine having to set that up for multiple extensions? A single typo in the various extensions could suddenly have people’s voice mails intended for one person wind up in someone else’s voice-mail box. Plus, the various extensions would get out of hand very quickly; your

extensions.confcould start topping over thousands of lines of code. Let’s insert a Macro to tame this beast.The macro,macro-stdexten, is included in Asterisk by default for this exact reason.

Example 3.6 Creating Voice Mail Support for Existing Extensions with the Use of Macros

[default]

include => specialextensions exten => s,1,Answer()

exten => s,2,Background(thank-you-for-calling-conglomocorp)

Using the macro allowed us to write a single piece of code that would duplicate the function of the code in the previous example. It’s also modular, allowing for the easy addition of extra extensions and extra voice-mail boxes.The stdextenmacro takes two arguments:The first being the channel to ring, and the second being the voice-mail box to send the call to if the channel is busy or does not answer.The macro rings the channel for 20 seconds and then sends it to voice mail telling voice mail to use the unavailable message. If the channel is busy, it immediately sends the caller to voice mail, telling voice mail to use the busy message if the user has one. If there is

some other condition on the call, like if the phone cannot be found on the network, the macro sends it to voice mail with the unavailable message.

The Macro() command takes at least one argument, the macro name.You can also pass multiple arguments to the macro by calling the Macro() command with additional arguments. In our example, macro-stdexten takes two arguments: the channel to ring, and the voice-mail box to call. Upon calling the macro, the macro is executed like a normal context, with the exception of extra variables ${ARGX}, where X is 1

through the number of variables you passed to the macro.

This takes care of incoming calls, but what about phones on the inside dialing out? Setting these up is as simple as setting up another context. Each time you set up a connection, you need to specify which context calls coming from that connection will go into. Setting up a context in which calls can use your outside line and then assigning all internal phones into that context will allow the phones to send calls via the outside lines. Continuing our example, let’s set up a context for internal calls:

[internal]

Let’s go over what each line accomplishes. Each one shows a different way of composing a dial command.The first line tells Asterisk that if a user dials a telephone number in the 617 area code, it will match the _1617NXXXXXX wildcard and the phone call will be sent out via the fist Zaptel device.The next line matches anything within the 310-454 prefix and will connect to a server called

“cali.conglomocorp.com” with the username “mass” and the password “Sk5S” and send the phone call through them.This is an explicit connection created in

extensions.conf. If a user dials a U.S. telephone number that isn’t in 617 or 310-454, it will match the _1NXXNXXXXXXX wildcard, and will be sent via the

IAXProvider connection, which would be created in iax.conf. Finally, if a user dials an international number beginning with 011, it will match the “_011X.” wildcard and be sent via the SIPProvider connection, which would be created in sip.conf. Also, the user can dial either of the two extensions on the system and be connected to them directly.These extensions would already be connected in sip.conf.

It is important to note that if we placed the _1NXXNXXXXXXX wildcard above the _1617NXXXXXX wildcard or the _1310454XXXX wildcard, anything below the _1NXXNXXXXXX wildcard would never be used since the

_1NXXNXXXXXXX wildcard would match everything. Asterisk reads lines from the top down and will match the first line it sees. Remembering this can save you a lot of headaches, and depending on your setup, possibly some money.

Configuring extensions.ael

The alternative to extensions.conf is extensions.ael.extensions.ael is extensions.conf written in a scripting language called Asterisk Extensions Language (AEL). AEL is language maintained by Digium solely for writing dial plans in Asterisk. While it is function-ally equivalent to extensions.conf, AEL is syntacticfunction-ally much more powerful and allows for greater flexibility in simple scripting and logical operations. If you’re familiar with scripting in other languages, AEL can often be easier to pick up than the regular extensions.conf syntax.

extensions.ael can be used as a replacement for extensions.conf or have both used side by side.extensions.ael is not in widespread use in today’s installations. However, due to its greater functionality, it would not be surprising to see extensions.conf depre-ciated in future versions of Asterisk in favor of extensions.ael.

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