Well, I know that I just said that I was done adding things to Asterisk, but it appears that I lied. I just added database-driven call forwarding to Asterisk, and took under 5 minutes.

The problem that I’m trying to solve here is simple: sometimes specific people call my home phone number when I’d really rather talk to them elsewhere. I have one specific example in mind–I’ve been beta-testing some software, and one of their sales guys calls me from time to time to ask how things are going. I don’t mind talking to him, but he keeps calling my home phone, when he’d be a lot better off calling my office number. I could just send him mail and give him the new contact information, but where’s the fun in that?

Instead, I added another macro:

; Do call forwarding out of a database.  Look up the caller ID number,
; and if it exists in the forward DB, then forward the call to the 
; specified number.
[macro-dbcallforward]
  exten => s,1,DBGet(FORWARDTO=forward/${CALLERIDNUM})
  exten => s,2,Macro(condsetcid)
  exten => s,3,Dial(Local/${FORWARDTO}@inside)

This macro looks in Asterisk’s internal DB for an entry called forward/$PHONENUMBER. If it exists, then the call is forwarded to wherever the DB entry points. I’m using the Local dialing channel, which basically just loops the call back around into the inside context, so it behaves just like I’d dialed it from an internal phone. This let me do things like forward calls to specific extensions, voicemail, conference rooms, or external phone numbers. Finally, if the call fails, or if there’s no matching DB entry, then we return to whoever called us.

So, the net result is that one quick DB entry is enough to send calls from some specific caller to wherever I want. Here’s how you set up the DB entries from Asterisk’s CLI:

CLI> database put forward 9199999999 12062629999

To delete them, just do this:

CLI> database delete forward 9199999999