#!/bin/bash # Convert a dialplan from Asterisk language to AEL # # Not perfect but far better than nothing # # Does not aim to create a fully-functional AEL dialplan (although it may if you haven't used too many Asterisk language constructs) # but is more intended to deal with the 98% of tedious stuff and leave the human to deal with the 2% of interesting stuff. # # Handles: # - Goto # - GotoIf # - ExecIf # - Set # - Comments # # Things that will need human attention to tidy up afterwards: # - Gosub # - GosubIf # - any conditional where the 'true' response contains a colon # - mathematical expressions using $[ ... ] # - While .. EndWhile # # There may be more... if [ -n "$1" ] then dialplan=$1 else echo "Please tell me which dialplan to try to convert." exit 1 fi indent=0 (cat asterisk/dialplan.$dialplan.conf; echo EndOfFile) | while read stuff do restofline= # Is this a comment? if [ "${stuff:0:1}" = ";" ] then echo "// ${stuff:1}" # Is this a new context name? elif [ "${stuff:0:1}" = "[" ] then while [ "$indent" -gt 0 ] do indent=$(($indent-2)) printf "%${indent}s" echo "}" done echo echo "context $stuff {" | tr -d '[]' indent=2 # Start of a new extension definition? elif [ "${stuff:0:9}" = "exten => " ] then if [ "$indent" -gt 2 ] then indent=$(($indent-2)) printf "%${indent}s" echo "}" echo fi printf "%${indent}s" echo -n "${stuff#exten => }" | sed 's/,.*//' echo " => {" indent=$(($indent+2)) restofline=`echo -n "$stuff" | sed 's/exten => [^,]*,1,//'` # Have we finished and need to close down the braces? elif [ "$stuff" = "EndOfFile" ] then while [ "$indent" -gt 0 ] do indent=$(($indent-2)) printf "%${indent}s}\n" done # Do we have a label? elif [ "${stuff:0:10}" = "same => n(" ] then echo -n "${stuff:10}" | sed 's/).*//' echo ":" restofline="${stuff#*),}" # Standard line in the dialplan? elif [ "${stuff:0:10}" = "same => n," ] then restofline="${stuff:10}" # No idea what anything else could be elif [ -n "$stuff" ] then echo "//// $stuff" fi # Goto/Gosub? if [ "${restofline:0:5}" = "Goto(" ] then printf "%${indent}s" echo "goto ${restofline:5}" | sed 's/)$/;/' restofline= fi if [ "${restofline:0:6}" = "Gosub(" ] then printf "%${indent}s" echo "&${restofline:6};" | sed 's/,/(/' restofline= fi # Conditional goto/gosub? if [ "${restofline:0:9}" = "GotoIf(\$[" -o "${restofline:0:10}" = "GosubIf(\$[" ] then printf "%${indent}s" [ "${restofline:2:1}" = "t" ] && condition="${restofline:9}" [ "${restofline:2:1}" = "s" ] && condition="${restofline:10}" condition="${condition%]\?*}" true="${restofline#*]\?}" true="${true%)}" echo "if ($condition)" indent=$(($indent+2)) printf "%${indent}s" [ "${restofline:2:1}" = "t" ] && echo "goto ${true%,1};" [ "${restofline:2:1}" = "s" ] && echo "&${true});" | sed 's/,/(/' indent=$(($indent-2)) true= restofline= fi # Conditional exec? # ExecIf($["${Scode}"="203"]?Set(Status=Cannot place two simultaneous calls)); if [ "${restofline:0:9}" = "ExecIf(\$[" ] then printf "%${indent}s" condition="${restofline:9}"; condition="${condition%]\?*}" true="${restofline#*]\?}"; false="${true#*:}" if [ "$true" = "$false" ] then true="${false%)}" false= else false="${false%)}" fi echo "if ($condition)" restofline= fi # Loop through conditionals, once only otherwise while [ -n "$true$false$restofline" ] do if [ -n "$true" ] then indent=$(($indent+2)) restofline="$true" elif [ -n "$false" ] then printf "%${indent}s" echo "else" indent=$(($indent+2)) restofline="$false" fi # Assignment? if [ "${restofline:0:4}" = "Set(" ] then printf "%${indent}s" echo "${restofline:4}" | sed 's/)$/;/' elif [ -n "$restofline" ] then printf "%${indent}s" echo "$restofline;" fi # Are we in a conditional? if [ -n "$true" ] then true= indent=$(($indent-2)) elif [ -n "$false" ] then false= indent=$(($indent-2)) fi restofline= done # Finally catch any debug lines we had done | sed 's# ; debug;$#; // debug#'