; 2003-02-16 18:30 GMT  jtodd@loligo.com
;
; This is the extensions.conf file for John Todd's Asterisk
;  server.  Asterisk can be found on http://www.asterisk.org/
;
; More recent versions of this file can be found on:
;   http://www.loligo.com/asterisk/
;
; This file determines where calls are routed when they are 
;  handed to Asterisk by one of various VOIP or analog call
;  presentation channels.  Configuration for those channels
;  are found elsewhere in the /etc/asterisk directory, and
;  examples can be found in my directory, listed above.
;
; I claim no special expertise in Asterisk; I simply have been
;  beating my head against it for month or two in my spare
;  time and getting my home/office phones worked out to a 
;  reasonably understandable call path for the five
;  inbound/outbound call channels I find myself using.
; 
; Many of you will be interested in the SIP features of this 
;  configuration example.  Note that the SIP functionality
;  as of mid-February 2003 is functional, but not robust.
;  There are problems with various SIP servers with the REGISTER
;  process, and quite a few bugs are still floating in the
;  code.  It works, but watch carefully for the first few
;  days to ensure no unpleasant surprises.  Running a background
;  daemon that ensures that asterisk is running is a good
;  idea.
;
; This configuration (and all supporting files) assumes the
;  use of an X100P analog FXO card, BUT IT IS NOT REQUIRED.
;  I use the card to terminate my house line and weave it into
;  the dialplans below.  It can be removed without much
;  difficulty.  For details on the ~$100 card, see:
;   http://www.digium.com/
;
; Config notes:
;   - in /usr/src/zaptel/Makefile, set KFLAGS+=-DECHO_CAN_MARK2
;   - in /usr/src/zaptel/Makefile, set KFLAGS+=-DAGGRESSIVE_SUPPRESSOR 
;
; Known Bugs, Problems, Weirdnesses, etc:
;
;	1) ATA-186 phones fail to stay registered.  Something within
;	Asterisk is causing ATA-186 phones to stop sending REGISTER
;	requests after ~2 hours.   Experiments with 30 through 240
;	second timeouts on the ciscos have similar results.  Phone
;	registry times out, calls fail.  Solution: static IP and
;	registry information per ATA-186.  Not shown in examples.
;
;	2) Multiple ACK messages to certain SIP servers (FWD notably)
;	- doesn't break anything, but why does it send ~8 ACKs to a
;	successful registry?  Lots of fluff traffic.
;
;	3) DTMF relay through ATA-186 phones on SIP calls.  I'm
;	uncertain if this is an ATA-186 issue or not; some in-depth
;	prodding seems to show that it's an Asterisk problem, or lack
;	of a feature.  DTMF reaches Asterisk, codes are shown on the
;	console (in-band RFC2833) but are not played out the remote
;	SIP channel; only slight garbled noise is heard.  Analog
;	replay works fine (ATA -> Asterisk -> X100P)   Perhaps an
;	origination problem with RFC2833 in-band signalling within
;	Asterisk.  I've tried changing to in-band signalling on the
;	ATA-186 (AudioMode: 0x00050005) without success as well.
;
;	4) Calls via certain SIP servers fail if the calling party is
;	an ATA-186 on both sides, seems to be an Asterisk issue.  I
;	can reproduce.
;
;	5) Calls made back to oneself from a remote SIP server
;	crashes Asterisk.
;
;	6) SIP register=  commands are only in the general context,
;	preventing directive actions on inbound SIP calls.  This is a
;	major issue, since a large number of PBX functions rely upon
;	what number the caller was dialing.
;
;	7) Timers for register=  commands should be selectable on a
;	per-service basis.
;
;	8) Asterisk crashes during remote REGISTER processes which
;	have odd timing.
;
; .
; .
; .
; NOTICE[5126]: File chan_sip.c, Line 1763 (sip_reg_timeout): Registration timed out, trying again
; NOTICE[5126]: File chan_sip.c, Line 1763 (sip_reg_timeout): Registration timed out, trying again
; NOTICE[5126]: File chan_sip.c, Line 1763 (sip_reg_timeout): Registration timed out, trying again
; NOTICE[5126]: File chan_sip.c, Line 2728 (handle_response): Registration successful
; NOTICE[5126]: File chan_sip.c, Line 2729 (handle_response): Cancelling timeout 2232
; NOTICE[5126]: File sched.c, Line 247 (ast_sched_del): Attempted to delete non-existant schedule entry 2217!
; !! Forcing immediate crash a-la abort !!
; Segmentation fault
;
;	
;	9) REGISTER attempts to Vocal server (v1.4) fail with "404
;	Not Found" errors, despite correct entries (which work with
;	ATA-186).  Perhaps notable is that even though a "404" error
;	has been returned, and confirmation of failure has been
;	printed on console, this is shown in a continuous loop on the
;	console afterwards:
;
; NOTICE[5126]: File chan_sip.c, Line 1763 (sip_reg_timeout): Registration timed out, trying again
; WARNING[5126]: File chan_sip.c, Line 283 (__sip_xmit): sip_xmit of 0x42c56480 (len 306) to 0.0.0.0 returned -1: Invalid argument
;
;
;
; To-Do:
;   - handle operator ("0") and international ("011") calls
;   - handle 411 (information) requests
;   - create an "all circuits busy" recording on outbound
;      calls that hit total usage blocks
;   - create failure cases for all inbound dialing (hangup)
;   - get the telezapper function running to kill off spam calls
;   - call transfer/hold need to happen
;   - get one-key conference going between extensions so we
;      can talk to friends/relatives on different phones but
;      without complexity of meeting rooms
;   - get callers without caller ID to enter a jail first
;      where they enter caller ID or are disconnected
;   - create external gateway for access to voicemail system
;   - create external gateway to DISA (so I can call Asterisk
;      from my cell phone, and dial to SIP peers)
;   - get cellphone forwarding re-integrated
;   - create quantum teleporter; achieve financial independence
;
; Thanks to: Mark Spencer, Nathan Lutchansky, Martin Pycko,
;   Tim Stewart, and the Asterisk development team for putting
;   up with my questions, adding featuers, and dealing with
;   my bug discoveries.
;
;



; The "General" category is for certain variables.  All other categories
; are interpreted as extension contexts
;
[general]
;
; If static is set to no, or omitted, then the pbx_config will rewrite
; this file when extensions are modified.  Remember that all comments
; made in the file will be lost when that happens. 
;
; XXX Not yet implemented XXX
;
static=yes

;
; if stati=yes and writeprotect=no, you can save dialplan by
; CLI command 'save dialplan' too
;
writeprotect=no



; The [globals] context is where you can set variables that
; can be referenced elsewhere in the dialplan with ${VARIABLE}
;
; I decided that for ease of reference, I should create a variable
; called "PHONE1" that I could set to the phone where I normally
; am found.  I then set "ME" to be my extention, raw, for use
; with voicemail forwarding.
; 
; Variable "PHONE2" is the other ATA-186 in the house.
;
; Variable "DIALOUTANALOG" is the analog interface (FXO) card 
;   in the PC on my desk.  See zapta.conf for config details.
;
; Variable "FWDUSERID" is my User ID from Free World Dialup.
;
; I create variables here so that if I decide to update my
;  extensions list, or my dial-out interface list, it's just
;  a simple variable change here at the top of the file.
;
[globals]
ME=2203
PHONE1=SIP/2203
PHONE2=SIP/2204
DIALOUTANALOG=Zap/1
FWDUSERID=18438



; The [intern] context happens to be where I pass calls
;  that are dialed by the SIP phones in my house/office.
;  I name these phones in the sip.conf file when I define the
;  SIP peers.
;
; Everything in this context is for numbers that are dialed
;  from phones locally here in the house/office.  Anytime
;  a call is received from one of the SIP phones on the desks,
;  this is the routine that is run against the dialed number
;  pattern.
;
;
[intern]

; EMERGENCY OUTBOUND
;
; United States dialing supports "911" emergency calls
;  which are routed to the "closest" emergency contact center.
;
; Of course, you need to make sure that the center to which
;  you are connecting is actually able to do something useful
;  for the dialer; it doesn't make sense to have your NY branch
;  offices dialing a 911 center in San Francisco.
;
; Note: The ATA-186 is auto-configured to send "911"
;  directly to the SIP server with no delay.  Be very, very
;  careful when experimenting with this - the system does not
;  follow standard rules with 911 calls.
;
; If you're !(USA), feel free to comment this line out entirely.
;
exten => 911,1,Dial(${DIALOUTANALOG}/BYEXTENSION)


; This is for failure-case outbound dialing.  If for some reason
;  the outbound SIP connections aren't working, but aren't giving errors
;  (i.e.: my path to larger Internet is dead) then I can short-circuit
;  my way to the local analog dial port by dialing "91npaxxxyyyy" and
;  the call will be shunted out the analog port.  This is for when
;  the inevitable cry of "IT DOESN'T WORK!!" bellows from the living room.
;
exten => _91.,1,Dial(${DIALOUTANALOG}/${EXTEN-1})
exten => _9.,1,Dial(${DIALOUTANALOG}/${EXTEN-1})


; Next, look to see if it's an INOC-DBA number.  All INOC-DBA
;  dial strings are dialed like this: 812345*223 where 12345 is
;  the AS of the recipient and 223 is the extension of the person
;  with whom I wish to speak.
;
; If any numbers begin with an "8" character, they are pointed
;  at the INOC-DBA SIP server.  The ${EXTEN-1} serves to strip
;  the first digit from the dialed string out (the "8" in this
;  case.)  I could have done the same thing with the StripMSD,1
;  application, but that just adds another step in the dial 
;  process.
; 
; NOTE: It is important to use SetCallerID first, THEN use
;  the SetCIDName application, or data is lost.  Bug.
;
; Note: AS in this example is bogus.
;
exten => _8.,1,SetCallerID,65000
exten => _8.,2,SetCIDName,John Todd
exten => _8.,3,Dial,SIP/${EXTEN-1}@inoc-dba


; Check to see if the called number starts with a "7" and
;  if so, set the call parameters and bounce the call to the
;  Free World Dialup SIP server.
;
; Someone said that the CallerID and CIDName needed to be changed
;  for FWD, so I did.  Doesn't seem to cause harm, so...
;
exten => _7.,1,SetCallerID,${FWDUSERID}
exten => _7.,2,SetCIDName,${FWDUSERID}
exten => _7.,3,Dial,SIP/${EXTEN-1}@fwd


; Experimental "forced" dialing through iconnect to make calls
;  prefixed with "6" go out the iconnect channel.  This is to
;  test some functionality for inbound connections; feel free
;  to comment it out.
;
exten => _61XXXXXXXXXX,1,Dial,SIP/${EXTEN-1}@iconnect



; Next, check for numbers that are available "locally" out of
;  my analog line connected to the house.  In area code 503,
;  you must dial an area code in front of all numbers, and
;  a "1" is not possible in front of local numbers.  Annoying.
;  
; Thus, this rule matches "15035551212" but will dial "5035551212"
;
; If the first line (DIALOUTANALOG) is busy, then fail over
;  and dial out the iconnecthere SIP peer.  This ensures harmony
;  in the household when She is one one line and I require the phone. :)
;
; Note that the iconnect Dial doesn't strip the first digit, since
;  we need the single "1" prefix in those outbound calls.
;
; The list is duplicated for area code "971", which is an overlay
;  for 503
;
exten => _1503.,1,Dial(${DIALOUTANALOG}/${EXTEN-1})
exten => _1503.,102,Dial(SIP/BYEXTENSION@iconnect)

exten => _1971.,1,Dial(${DIALOUTANALOG}/${EXTEN-1})
exten => _1971.,102,Dial(SIP/BYEXTENSION@iconnect)


; Well, what about calls that are long distance in my local (503) area?
;  I handle this by dialing them as "115035551212" and the system will
;  then match and remove the first one.  Ugly, but it works.
; 
; The question is, of course, "should these go via iconnect"?  I don't
;  have the costs in front of me, but I may well remove this whole logic
;  path once I figure it out.
;
; This rule will match '115036661313" and will dial "15036661313"
;
; Again, fail over to iconnect peer if analog busy, but this time
;  we're going to strip the first digit, since "11" is not a valid
;  prefix for iconnecthere calls.
;
; The list is duplicated for area code "971", which is an overlay
;  for 503.
;
;
exten => _11503.,1,Dial(${DIALOUTANALOG}/${EXTEN-1})
exten => _11503.,102,Dial(SIP/${EXTEN-1}@iconnect)

exten => _11971.,1,Dial(${DIALOUTANALOG}/${EXTEN-1})
exten => _11971.,102,Dial(SIP/${EXTEN-1}@iconnect)



; Dial toll-free numbers (800, 877, 888, 866) from the analog line
;  and roll over to iconnect peer if analog is busy.
;
exten => _1888.,1,Dial(${DIALOUTANALOG}/BYEXTENSION)
exten => _1888.,102,Dial(SIP/BYEXTENSION@iconnect)

exten => _1877.,1,Dial(${DIALOUTANALOG}/BYEXTENSION)
exten => _1877.,102,Dial(SIP/BYEXTENSION@iconnect)

exten => _1866.,1,Dial(${DIALOUTANALOG}/BYEXTENSION)
exten => _1866.,102,Dial(SIP/BYEXTENSION@iconnect)

exten => _1800.,1,Dial(${DIALOUTANALOG}/BYEXTENSION)
exten => _1800.,102,Dial(SIP/BYEXTENSION@iconnect)



; Now, this is the list of area codes that should be shunted out the SIP
;  gateway in Laurel.  The area codes of 301, 410, 703, and 240 are "local"
;  to the gateway in Laurel, so I select them manually.
; 
; See "sip.conf" for details on how I set up the "coloco" SIP peer
;
; If you have no other SIP peers that have visibility into local
;  area codes, feel free to comment these lines out entirely.
;
; I have these commented out but ready to go when someone fixes the
;  Vocal REGISTER problems (with Asterisk or Vocal)
;
;exten => _1301.,1,Dial,SIP/BYEXTENSION@coloco
;exten => _1410.,1,Dial,SIP/BYEXTENSION@coloco
;exten => _1703.,1,Dial,SIP/BYEXTENSION@coloco
;exten => _1240.,1,Dial,SIP/BYEXTENSION@coloco



; All other calls that have a "1" at the front are North American
;  dialing prefixes, and should be sent to my iconnecthere account.
;
; These guys have a decent deal on LD minutes; something like $4.95 
;  for 400 minutes of LD anywhere in the US lower 48 states, and
;  they have international plans as well.
;
; They also offer optional inbound dialing; see other parts of 
;  this config file for notes on that.
;
; see sip.conf for details on configuration of outbound iconnecthere
;  SIP peer
;
exten => _1XXXXXXXXXX,1,Dial,SIP/BYEXTENSION@iconnect


; Include the "local" set of extensions, so that phones
;  on the desktop can dial each other, and voicemail, and
;  whatever other "general" services are offered by Asterisk.
;
include => local

; end of [intern] context



; The [inbound-analog] context is where calls coming in from the
;  zaptel X100P analog circuit are passed.  Right now, this
;  is a very simple dial plan that rings my two SIP phones.
;
; If the line rings for >20 seconds, dial the "u"navaialable voicemail box.
; If the line is busy, dial the "b"usy voicemail box.
;
;
; See the file zapata.conf for how to configure the Zaptel X100P
;  to forward calls to this context when called
;
[inbound-analog]
exten => s,1,Dial(${PHONE1}&${PHONE2},15)
exten => s,2,Wait,2
exten => s,3,Voicemail,u${ME}
exten => s,102,Voicemail,b${ME}



; My inbound line from Coloco is 2405551212 and that is what
;  shows up as the "called" ID when the Cisco passes calls to me.
;  Thus, I have to match against that "originally dialed" number
;  in this context, since that peer will be sending it only.
;
; See the file sip.conf for details on how I configured the 
;  coloco SIP peer
;
; Currently, this doesn't work.  All calls are
;  routed through context [from-sip]
;
; Of course, actual line # is bogus, but you get the idea.
;
[coloco]
exten => 2405551212,1,Dial,${PHONE1}
exten => 2405551212,2,Voicemail,u${ME}
exten => 2405551212,102,Voicemail,b${ME}



; Inbound calls from INOC-DBA SIP peer
;  are routed to this context. See sip.conf
;  for more details on INOC-DBA peer.
;  My AS number is 65000, so any inbound
;  calls for that extension should end up
;  on 2203, which is my ATA-186 (which in 
;  turn is defined in the [local] context
;  below and included.
;
; Currently, this doesn't work.  All calls are
;  routed through context [from-sip]
;
[inoc-dba]
exten => s,1,Wait,1                     ; Wait a second, just for fun
exten => s,2,Answer                     ; Answer the line
exten => s,3,DigitTimeout,5             ; Set Digit Timeout to 5 seconds
exten => s,4,ResponseTimeout,10         ; Set Response Timeout to 10 seconds
exten => s,5,Playback,vm-last		; JT's "it works!" flag - test only
exten => 65000,1,Dial,${PHONE1}|10	; Dial my desk extension for 10s
exten => 65000,2,Voicemail,u${ME}	; send to VM unavailable if no answer
exten => 65000,102,Voicemail,b${ME}	; send to VM busy if step 1 is busy

; I should put a "no such extension" playback here for mis-routed
;  inoc-dba calls.  Later.



; All inbound SIP gateways (FWD, inoc-dba, iconnect, etc.) will
;  be pointed at this context from sip.conf
;
; All these calls go right to extension 2203.  In the near future,
;  I will change this to a recorded greeting before transfer, or
;  perhaps put the caller in phone-jail for a little while to determine
;  what number they called to reach here, since I can't determine that
;  from contextual hints in sip.conf (nag, nag, nag)
;
[from-sip]
exten => s,1,Wait,1                     ; Wait a second, just for fun
exten => s,2,Answer                     ; Answer the line
exten => s,3,DigitTimeout,5             ; Set Digit Timeout to 5 seconds
exten => s,4,ResponseTimeout,10         ; Set Response Timeout to 10 seconds
;exten => s,5,Playback,demo-thanks	; JT's "it works!" flag - test only
exten => s,5,Dial,${PHONE1}|10		; Dial my desk extension for 10s
exten => s,6,Voicemail,u${ME}		; send to VM unavailable if no answer
exten => s,106,Voicemail,b${ME}		; send to VM busy if step 5 is busy



; The [local] context is where I put all of the locally dialable
;  numbers.  These entries could be made in each context where I
;  reference them, but that would be redundant.  
;
; Mostly, they are used from context [intern] right now, but I'm sure
;  I'll include them in other contexts later.
; 
; Note: due to my wacky/kludgey prefix dialing crap, all the "internal"
;  extensions that are local to this system must begin with a "2"
; This means that voicemail, echo, all users, etc. must have an extension
;  that starts with the digit "2" or one of the other dialplans will
;  snag the call and do whatever with it.
;
[local]
exten => 2203,1,Dial,${PHONE1}
exten => 2204,1,Dial,${PHONE2}


; Dial 2500 from any phone to go to the voicemail system
;
exten => 2500,1,VoicemailMain
exten => 2500,2,Hangup

