app_dial.c 114 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306230723082309231023112312231323142315231623172318231923202321232223232324232523262327232823292330233123322333233423352336233723382339234023412342234323442345234623472348234923502351235223532354235523562357235823592360236123622363236423652366236723682369237023712372237323742375237623772378237923802381238223832384238523862387238823892390239123922393239423952396239723982399240024012402240324042405240624072408240924102411241224132414241524162417241824192420242124222423242424252426242724282429243024312432243324342435243624372438243924402441244224432444244524462447244824492450245124522453245424552456245724582459246024612462246324642465246624672468246924702471247224732474247524762477247824792480248124822483248424852486248724882489249024912492249324942495249624972498249925002501250225032504250525062507250825092510251125122513251425152516251725182519252025212522252325242525252625272528252925302531253225332534253525362537253825392540254125422543254425452546254725482549255025512552255325542555255625572558255925602561256225632564256525662567256825692570257125722573257425752576257725782579258025812582258325842585258625872588258925902591259225932594259525962597259825992600260126022603260426052606260726082609261026112612261326142615261626172618261926202621262226232624262526262627262826292630263126322633263426352636263726382639264026412642264326442645264626472648264926502651265226532654265526562657265826592660266126622663266426652666266726682669267026712672267326742675267626772678267926802681268226832684268526862687268826892690269126922693269426952696269726982699270027012702270327042705270627072708270927102711271227132714271527162717271827192720272127222723272427252726272727282729273027312732273327342735273627372738273927402741274227432744274527462747274827492750275127522753275427552756275727582759276027612762276327642765276627672768276927702771277227732774277527762777277827792780278127822783278427852786278727882789279027912792279327942795279627972798279928002801280228032804280528062807280828092810281128122813281428152816281728182819282028212822282328242825282628272828282928302831283228332834283528362837283828392840284128422843284428452846284728482849285028512852285328542855285628572858285928602861286228632864286528662867286828692870287128722873287428752876287728782879288028812882288328842885288628872888288928902891289228932894289528962897289828992900290129022903290429052906290729082909291029112912291329142915291629172918291929202921292229232924292529262927292829292930293129322933293429352936293729382939294029412942294329442945294629472948294929502951295229532954295529562957295829592960296129622963296429652966296729682969297029712972297329742975297629772978297929802981298229832984298529862987298829892990299129922993299429952996299729982999300030013002300330043005300630073008300930103011301230133014301530163017301830193020302130223023302430253026302730283029303030313032303330343035303630373038303930403041304230433044304530463047304830493050305130523053305430553056305730583059306030613062306330643065306630673068306930703071307230733074307530763077307830793080308130823083308430853086308730883089309030913092309330943095309630973098309931003101310231033104310531063107310831093110311131123113311431153116311731183119312031213122312331243125312631273128312931303131313231333134313531363137313831393140314131423143314431453146314731483149315031513152315331543155315631573158315931603161316231633164316531663167316831693170317131723173317431753176317731783179318031813182318331843185318631873188318931903191319231933194319531963197319831993200320132023203320432053206
  1. /*
  2. * Asterisk -- An open source telephony toolkit.
  3. *
  4. * Copyright (C) 1999 - 2008, Digium, Inc.
  5. *
  6. * Mark Spencer <markster@digium.com>
  7. *
  8. * See http://www.asterisk.org for more information about
  9. * the Asterisk project. Please do not directly contact
  10. * any of the maintainers of this project for assistance;
  11. * the project provides a web site, mailing lists and IRC
  12. * channels for your use.
  13. *
  14. * This program is free software, distributed under the terms of
  15. * the GNU General Public License Version 2. See the LICENSE file
  16. * at the top of the source tree.
  17. */
  18. /*! \file
  19. *
  20. * \brief dial() & retrydial() - Trivial application to dial a channel and send an URL on answer
  21. *
  22. * \author Mark Spencer <markster@digium.com>
  23. *
  24. * \ingroup applications
  25. */
  26. /*** MODULEINFO
  27. <depend>chan_local</depend>
  28. <support_level>core</support_level>
  29. ***/
  30. #include "asterisk.h"
  31. ASTERISK_FILE_VERSION(__FILE__, "$Revision$")
  32. #include <sys/time.h>
  33. #include <sys/signal.h>
  34. #include <sys/stat.h>
  35. #include <netinet/in.h>
  36. #include "asterisk/paths.h" /* use ast_config_AST_DATA_DIR */
  37. #include "asterisk/lock.h"
  38. #include "asterisk/file.h"
  39. #include "asterisk/channel.h"
  40. #include "asterisk/pbx.h"
  41. #include "asterisk/module.h"
  42. #include "asterisk/translate.h"
  43. #include "asterisk/say.h"
  44. #include "asterisk/config.h"
  45. #include "asterisk/features.h"
  46. #include "asterisk/musiconhold.h"
  47. #include "asterisk/callerid.h"
  48. #include "asterisk/utils.h"
  49. #include "asterisk/app.h"
  50. #include "asterisk/causes.h"
  51. #include "asterisk/rtp_engine.h"
  52. #include "asterisk/cdr.h"
  53. #include "asterisk/manager.h"
  54. #include "asterisk/privacy.h"
  55. #include "asterisk/stringfields.h"
  56. #include "asterisk/global_datastores.h"
  57. #include "asterisk/dsp.h"
  58. #include "asterisk/cel.h"
  59. #include "asterisk/aoc.h"
  60. #include "asterisk/ccss.h"
  61. #include "asterisk/indications.h"
  62. #include "asterisk/framehook.h"
  63. /*** DOCUMENTATION
  64. <application name="Dial" language="en_US">
  65. <synopsis>
  66. Attempt to connect to another device or endpoint and bridge the call.
  67. </synopsis>
  68. <syntax>
  69. <parameter name="Technology/Resource" required="true" argsep="&amp;">
  70. <argument name="Technology/Resource" required="true">
  71. <para>Specification of the device(s) to dial. These must be in the format of
  72. <literal>Technology/Resource</literal>, where <replaceable>Technology</replaceable>
  73. represents a particular channel driver, and <replaceable>Resource</replaceable>
  74. represents a resource available to that particular channel driver.</para>
  75. </argument>
  76. <argument name="Technology2/Resource2" required="false" multiple="true">
  77. <para>Optional extra devices to dial in parallel</para>
  78. <para>If you need more then one enter them as
  79. Technology2/Resource2&amp;Technology3/Resourse3&amp;.....</para>
  80. </argument>
  81. </parameter>
  82. <parameter name="timeout" required="false">
  83. <para>Specifies the number of seconds we attempt to dial the specified devices</para>
  84. <para>If not specified, this defaults to 136 years.</para>
  85. </parameter>
  86. <parameter name="options" required="false">
  87. <optionlist>
  88. <option name="A">
  89. <argument name="x" required="true">
  90. <para>The file to play to the called party</para>
  91. </argument>
  92. <para>Play an announcement to the called party, where <replaceable>x</replaceable> is the prompt to be played</para>
  93. </option>
  94. <option name="a">
  95. <para>Immediately answer the calling channel when the called channel answers in
  96. all cases. Normally, the calling channel is answered when the called channel
  97. answers, but when options such as A() and M() are used, the calling channel is
  98. not answered until all actions on the called channel (such as playing an
  99. announcement) are completed. This option can be used to answer the calling
  100. channel before doing anything on the called channel. You will rarely need to use
  101. this option, the default behavior is adequate in most cases.</para>
  102. </option>
  103. <option name="C">
  104. <para>Reset the call detail record (CDR) for this call.</para>
  105. </option>
  106. <option name="c">
  107. <para>If the Dial() application cancels this call, always set the flag to tell the channel
  108. driver that the call is answered elsewhere.</para>
  109. </option>
  110. <option name="d">
  111. <para>Allow the calling user to dial a 1 digit extension while waiting for
  112. a call to be answered. Exit to that extension if it exists in the
  113. current context, or the context defined in the <variable>EXITCONTEXT</variable> variable,
  114. if it exists.</para>
  115. <note>
  116. <para>Many SIP and ISDN phones cannot send DTMF digits until the call is
  117. connected. If you wish to use this option with these phones, you
  118. can use the <literal>Answer</literal> application before dialing.</para>
  119. </note>
  120. </option>
  121. <option name="D" argsep=":">
  122. <argument name="called" />
  123. <argument name="calling" />
  124. <argument name="progress" />
  125. <para>Send the specified DTMF strings <emphasis>after</emphasis> the called
  126. party has answered, but before the call gets bridged. The
  127. <replaceable>called</replaceable> DTMF string is sent to the called party, and the
  128. <replaceable>calling</replaceable> DTMF string is sent to the calling party. Both arguments
  129. can be used alone. If <replaceable>progress</replaceable> is specified, its DTMF is sent
  130. immediately after receiving a PROGRESS message.</para>
  131. </option>
  132. <option name="e">
  133. <para>Execute the <literal>h</literal> extension for peer after the call ends</para>
  134. </option>
  135. <option name="f">
  136. <argument name="x" required="false" />
  137. <para>If <replaceable>x</replaceable> is not provided, force the CallerID sent on a call-forward or
  138. deflection to the dialplan extension of this Dial() using a dialplan <literal>hint</literal>.
  139. For example, some PSTNs do not allow CallerID to be set to anything
  140. other than the numbers assigned to you.
  141. If <replaceable>x</replaceable> is provided, force the CallerID sent to <replaceable>x</replaceable>.</para>
  142. </option>
  143. <option name="F" argsep="^">
  144. <argument name="context" required="false" />
  145. <argument name="exten" required="false" />
  146. <argument name="priority" required="true" />
  147. <para>When the caller hangs up, transfer the <emphasis>called</emphasis> party
  148. to the specified destination and <emphasis>start</emphasis> execution at that location.</para>
  149. <note>
  150. <para>Any channel variables you want the called channel to inherit from the caller channel must be
  151. prefixed with one or two underbars ('_').</para>
  152. </note>
  153. </option>
  154. <option name="F">
  155. <para>When the caller hangs up, transfer the <emphasis>called</emphasis> party to the next priority of the current extension
  156. and <emphasis>start</emphasis> execution at that location.</para>
  157. <note>
  158. <para>Any channel variables you want the called channel to inherit from the caller channel must be
  159. prefixed with one or two underbars ('_').</para>
  160. </note>
  161. <note>
  162. <para>Using this option from a Macro() or GoSub() might not make sense as there would be no return points.</para>
  163. </note>
  164. </option>
  165. <option name="g">
  166. <para>Proceed with dialplan execution at the next priority in the current extension if the
  167. destination channel hangs up.</para>
  168. </option>
  169. <option name="G" argsep="^">
  170. <argument name="context" required="false" />
  171. <argument name="exten" required="false" />
  172. <argument name="priority" required="true" />
  173. <para>If the call is answered, transfer the calling party to
  174. the specified <replaceable>priority</replaceable> and the called party to the specified
  175. <replaceable>priority</replaceable> plus one.</para>
  176. <note>
  177. <para>You cannot use any additional action post answer options in conjunction with this option.</para>
  178. </note>
  179. </option>
  180. <option name="h">
  181. <para>Allow the called party to hang up by sending the DTMF sequence
  182. defined for disconnect in <filename>features.conf</filename>.</para>
  183. </option>
  184. <option name="H">
  185. <para>Allow the calling party to hang up by sending the DTMF sequence
  186. defined for disconnect in <filename>features.conf</filename>.</para>
  187. <note>
  188. <para>Many SIP and ISDN phones cannot send DTMF digits until the call is
  189. connected. If you wish to allow DTMF disconnect before the dialed
  190. party answers with these phones, you can use the <literal>Answer</literal>
  191. application before dialing.</para>
  192. </note>
  193. </option>
  194. <option name="i">
  195. <para>Asterisk will ignore any forwarding requests it may receive on this dial attempt.</para>
  196. </option>
  197. <option name="I">
  198. <para>Asterisk will ignore any connected line update requests or any redirecting party
  199. update requests it may receive on this dial attempt.</para>
  200. </option>
  201. <option name="k">
  202. <para>Allow the called party to enable parking of the call by sending
  203. the DTMF sequence defined for call parking in <filename>features.conf</filename>.</para>
  204. </option>
  205. <option name="K">
  206. <para>Allow the calling party to enable parking of the call by sending
  207. the DTMF sequence defined for call parking in <filename>features.conf</filename>.</para>
  208. </option>
  209. <option name="L" argsep=":">
  210. <argument name="x" required="true">
  211. <para>Maximum call time, in milliseconds</para>
  212. </argument>
  213. <argument name="y">
  214. <para>Warning time, in milliseconds</para>
  215. </argument>
  216. <argument name="z">
  217. <para>Repeat time, in milliseconds</para>
  218. </argument>
  219. <para>Limit the call to <replaceable>x</replaceable> milliseconds. Play a warning when <replaceable>y</replaceable> milliseconds are
  220. left. Repeat the warning every <replaceable>z</replaceable> milliseconds until time expires.</para>
  221. <para>This option is affected by the following variables:</para>
  222. <variablelist>
  223. <variable name="LIMIT_PLAYAUDIO_CALLER">
  224. <value name="yes" default="true" />
  225. <value name="no" />
  226. <para>If set, this variable causes Asterisk to play the prompts to the caller.</para>
  227. </variable>
  228. <variable name="LIMIT_PLAYAUDIO_CALLEE">
  229. <value name="yes" />
  230. <value name="no" default="true"/>
  231. <para>If set, this variable causes Asterisk to play the prompts to the callee.</para>
  232. </variable>
  233. <variable name="LIMIT_TIMEOUT_FILE">
  234. <value name="filename"/>
  235. <para>If specified, <replaceable>filename</replaceable> specifies the sound prompt to play when the timeout is reached.
  236. If not set, the time remaining will be announced.</para>
  237. </variable>
  238. <variable name="LIMIT_CONNECT_FILE">
  239. <value name="filename"/>
  240. <para>If specified, <replaceable>filename</replaceable> specifies the sound prompt to play when the call begins.
  241. If not set, the time remaining will be announced.</para>
  242. </variable>
  243. <variable name="LIMIT_WARNING_FILE">
  244. <value name="filename"/>
  245. <para>If specified, <replaceable>filename</replaceable> specifies the sound prompt to play as
  246. a warning when time <replaceable>x</replaceable> is reached. If not set, the time remaining will be announced.</para>
  247. </variable>
  248. </variablelist>
  249. </option>
  250. <option name="m">
  251. <argument name="class" required="false"/>
  252. <para>Provide hold music to the calling party until a requested
  253. channel answers. A specific music on hold <replaceable>class</replaceable>
  254. (as defined in <filename>musiconhold.conf</filename>) can be specified.</para>
  255. </option>
  256. <option name="M" argsep="^">
  257. <argument name="macro" required="true">
  258. <para>Name of the macro that should be executed.</para>
  259. </argument>
  260. <argument name="arg" multiple="true">
  261. <para>Macro arguments</para>
  262. </argument>
  263. <para>Execute the specified <replaceable>macro</replaceable> for the <emphasis>called</emphasis> channel
  264. before connecting to the calling channel. Arguments can be specified to the Macro
  265. using <literal>^</literal> as a delimiter. The macro can set the variable
  266. <variable>MACRO_RESULT</variable> to specify the following actions after the macro is
  267. finished executing:</para>
  268. <variablelist>
  269. <variable name="MACRO_RESULT">
  270. <para>If set, this action will be taken after the macro finished executing.</para>
  271. <value name="ABORT">
  272. Hangup both legs of the call
  273. </value>
  274. <value name="CONGESTION">
  275. Behave as if line congestion was encountered
  276. </value>
  277. <value name="BUSY">
  278. Behave as if a busy signal was encountered
  279. </value>
  280. <value name="CONTINUE">
  281. Hangup the called party and allow the calling party to continue dialplan execution at the next priority
  282. </value>
  283. <value name="GOTO:[[&lt;context&gt;^]&lt;exten&gt;^]&lt;priority&gt;">
  284. Transfer the call to the specified destination.
  285. </value>
  286. </variable>
  287. </variablelist>
  288. <note>
  289. <para>You cannot use any additional action post answer options in conjunction
  290. with this option. Also, pbx services are run on the peer (called) channel,
  291. so you will not be able to set timeouts via the TIMEOUT() function in this macro.</para>
  292. </note>
  293. <warning><para>Be aware of the limitations that macros have, specifically with regards to use of
  294. the <literal>WaitExten</literal> application. For more information, see the documentation for
  295. Macro()</para></warning>
  296. </option>
  297. <option name="n">
  298. <argument name="delete">
  299. <para>With <replaceable>delete</replaceable> either not specified or set to <literal>0</literal>,
  300. the recorded introduction will not be deleted if the caller hangs up while the remote party has not
  301. yet answered.</para>
  302. <para>With <replaceable>delete</replaceable> set to <literal>1</literal>, the introduction will
  303. always be deleted.</para>
  304. </argument>
  305. <para>This option is a modifier for the call screening/privacy mode. (See the
  306. <literal>p</literal> and <literal>P</literal> options.) It specifies
  307. that no introductions are to be saved in the <directory>priv-callerintros</directory>
  308. directory.</para>
  309. </option>
  310. <option name="N">
  311. <para>This option is a modifier for the call screening/privacy mode. It specifies
  312. that if Caller*ID is present, do not screen the call.</para>
  313. </option>
  314. <option name="o">
  315. <argument name="x" required="false" />
  316. <para>If <replaceable>x</replaceable> is not provided, specify that the CallerID that was present on the
  317. <emphasis>calling</emphasis> channel be stored as the CallerID on the <emphasis>called</emphasis> channel.
  318. This was the behavior of Asterisk 1.0 and earlier.
  319. If <replaceable>x</replaceable> is provided, specify the CallerID stored on the <emphasis>called</emphasis> channel.
  320. Note that o(${CALLERID(all)}) is similar to option o without the parameter.</para>
  321. </option>
  322. <option name="O">
  323. <argument name="mode">
  324. <para>With <replaceable>mode</replaceable> either not specified or set to <literal>1</literal>,
  325. the originator hanging up will cause the phone to ring back immediately.</para>
  326. <para>With <replaceable>mode</replaceable> set to <literal>2</literal>, when the operator
  327. flashes the trunk, it will ring their phone back.</para>
  328. </argument>
  329. <para>Enables <emphasis>operator services</emphasis> mode. This option only
  330. works when bridging a DAHDI channel to another DAHDI channel
  331. only. if specified on non-DAHDI interfaces, it will be ignored.
  332. When the destination answers (presumably an operator services
  333. station), the originator no longer has control of their line.
  334. They may hang up, but the switch will not release their line
  335. until the destination party (the operator) hangs up.</para>
  336. </option>
  337. <option name="p">
  338. <para>This option enables screening mode. This is basically Privacy mode
  339. without memory.</para>
  340. </option>
  341. <option name="P">
  342. <argument name="x" />
  343. <para>Enable privacy mode. Use <replaceable>x</replaceable> as the family/key in the AstDB database if
  344. it is provided. The current extension is used if a database family/key is not specified.</para>
  345. </option>
  346. <option name="r">
  347. <para>Default: Indicate ringing to the calling party, even if the called party isn't actually ringing. Pass no audio to the calling
  348. party until the called channel has answered.</para>
  349. <argument name="tone" required="false">
  350. <para>Indicate progress to calling party. Send audio 'tone' from indications.conf</para>
  351. </argument>
  352. </option>
  353. <option name="S">
  354. <argument name="x" required="true" />
  355. <para>Hang up the call <replaceable>x</replaceable> seconds <emphasis>after</emphasis> the called party has
  356. answered the call.</para>
  357. </option>
  358. <option name="s">
  359. <argument name="x" required="true" />
  360. <para>Force the outgoing callerid tag parameter to be set to the string <replaceable>x</replaceable>.</para>
  361. <para>Works with the f option.</para>
  362. </option>
  363. <option name="t">
  364. <para>Allow the called party to transfer the calling party by sending the
  365. DTMF sequence defined in <filename>features.conf</filename>. This setting does not perform policy enforcement on
  366. transfers initiated by other methods.</para>
  367. </option>
  368. <option name="T">
  369. <para>Allow the calling party to transfer the called party by sending the
  370. DTMF sequence defined in <filename>features.conf</filename>. This setting does not perform policy enforcement on
  371. transfers initiated by other methods.</para>
  372. </option>
  373. <option name="U" argsep="^">
  374. <argument name="x" required="true">
  375. <para>Name of the subroutine to execute via Gosub</para>
  376. </argument>
  377. <argument name="arg" multiple="true" required="false">
  378. <para>Arguments for the Gosub routine</para>
  379. </argument>
  380. <para>Execute via Gosub the routine <replaceable>x</replaceable> for the <emphasis>called</emphasis> channel before connecting
  381. to the calling channel. Arguments can be specified to the Gosub
  382. using <literal>^</literal> as a delimiter. The Gosub routine can set the variable
  383. <variable>GOSUB_RESULT</variable> to specify the following actions after the Gosub returns.</para>
  384. <variablelist>
  385. <variable name="GOSUB_RESULT">
  386. <value name="ABORT">
  387. Hangup both legs of the call.
  388. </value>
  389. <value name="CONGESTION">
  390. Behave as if line congestion was encountered.
  391. </value>
  392. <value name="BUSY">
  393. Behave as if a busy signal was encountered.
  394. </value>
  395. <value name="CONTINUE">
  396. Hangup the called party and allow the calling party
  397. to continue dialplan execution at the next priority.
  398. </value>
  399. <value name="GOTO:[[&lt;context&gt;^]&lt;exten&gt;^]&lt;priority&gt;">
  400. Transfer the call to the specified destination.
  401. </value>
  402. </variable>
  403. </variablelist>
  404. <note>
  405. <para>You cannot use any additional action post answer options in conjunction
  406. with this option. Also, pbx services are run on the peer (called) channel,
  407. so you will not be able to set timeouts via the TIMEOUT() function in this routine.</para>
  408. </note>
  409. </option>
  410. <option name="u">
  411. <argument name = "x" required="true">
  412. <para>Force the outgoing callerid presentation indicator parameter to be set
  413. to one of the values passed in <replaceable>x</replaceable>:
  414. <literal>allowed_not_screened</literal>
  415. <literal>allowed_passed_screen</literal>
  416. <literal>allowed_failed_screen</literal>
  417. <literal>allowed</literal>
  418. <literal>prohib_not_screened</literal>
  419. <literal>prohib_passed_screen</literal>
  420. <literal>prohib_failed_screen</literal>
  421. <literal>prohib</literal>
  422. <literal>unavailable</literal></para>
  423. </argument>
  424. <para>Works with the f option.</para>
  425. </option>
  426. <option name="w">
  427. <para>Allow the called party to enable recording of the call by sending
  428. the DTMF sequence defined for one-touch recording in <filename>features.conf</filename>.</para>
  429. </option>
  430. <option name="W">
  431. <para>Allow the calling party to enable recording of the call by sending
  432. the DTMF sequence defined for one-touch recording in <filename>features.conf</filename>.</para>
  433. </option>
  434. <option name="x">
  435. <para>Allow the called party to enable recording of the call by sending
  436. the DTMF sequence defined for one-touch automixmonitor in <filename>features.conf</filename>.</para>
  437. </option>
  438. <option name="X">
  439. <para>Allow the calling party to enable recording of the call by sending
  440. the DTMF sequence defined for one-touch automixmonitor in <filename>features.conf</filename>.</para>
  441. </option>
  442. <option name="z">
  443. <para>On a call forward, cancel any dial timeout which has been set for this call.</para>
  444. </option>
  445. </optionlist>
  446. </parameter>
  447. <parameter name="URL">
  448. <para>The optional URL will be sent to the called party if the channel driver supports it.</para>
  449. </parameter>
  450. </syntax>
  451. <description>
  452. <para>This application will place calls to one or more specified channels. As soon
  453. as one of the requested channels answers, the originating channel will be
  454. answered, if it has not already been answered. These two channels will then
  455. be active in a bridged call. All other channels that were requested will then
  456. be hung up.</para>
  457. <para>Unless there is a timeout specified, the Dial application will wait
  458. indefinitely until one of the called channels answers, the user hangs up, or
  459. if all of the called channels are busy or unavailable. Dialplan executing will
  460. continue if no requested channels can be called, or if the timeout expires.
  461. This application will report normal termination if the originating channel
  462. hangs up, or if the call is bridged and either of the parties in the bridge
  463. ends the call.</para>
  464. <para>If the <variable>OUTBOUND_GROUP</variable> variable is set, all peer channels created by this
  465. application will be put into that group (as in Set(GROUP()=...).
  466. If the <variable>OUTBOUND_GROUP_ONCE</variable> variable is set, all peer channels created by this
  467. application will be put into that group (as in Set(GROUP()=...). Unlike <variable>OUTBOUND_GROUP</variable>,
  468. however, the variable will be unset after use.</para>
  469. <para>This application sets the following channel variables:</para>
  470. <variablelist>
  471. <variable name="DIALEDTIME">
  472. <para>This is the time from dialing a channel until when it is disconnected.</para>
  473. </variable>
  474. <variable name="ANSWEREDTIME">
  475. <para>This is the amount of time for actual call.</para>
  476. </variable>
  477. <variable name="DIALSTATUS">
  478. <para>This is the status of the call</para>
  479. <value name="CHANUNAVAIL" />
  480. <value name="CONGESTION" />
  481. <value name="NOANSWER" />
  482. <value name="BUSY" />
  483. <value name="ANSWER" />
  484. <value name="CANCEL" />
  485. <value name="DONTCALL">
  486. For the Privacy and Screening Modes.
  487. Will be set if the called party chooses to send the calling party to the 'Go Away' script.
  488. </value>
  489. <value name="TORTURE">
  490. For the Privacy and Screening Modes.
  491. Will be set if the called party chooses to send the calling party to the 'torture' script.
  492. </value>
  493. <value name="INVALIDARGS" />
  494. </variable>
  495. </variablelist>
  496. </description>
  497. </application>
  498. <application name="RetryDial" language="en_US">
  499. <synopsis>
  500. Place a call, retrying on failure allowing an optional exit extension.
  501. </synopsis>
  502. <syntax>
  503. <parameter name="announce" required="true">
  504. <para>Filename of sound that will be played when no channel can be reached</para>
  505. </parameter>
  506. <parameter name="sleep" required="true">
  507. <para>Number of seconds to wait after a dial attempt failed before a new attempt is made</para>
  508. </parameter>
  509. <parameter name="retries" required="true">
  510. <para>Number of retries</para>
  511. <para>When this is reached flow will continue at the next priority in the dialplan</para>
  512. </parameter>
  513. <parameter name="dialargs" required="true">
  514. <para>Same format as arguments provided to the Dial application</para>
  515. </parameter>
  516. </syntax>
  517. <description>
  518. <para>This application will attempt to place a call using the normal Dial application.
  519. If no channel can be reached, the <replaceable>announce</replaceable> file will be played.
  520. Then, it will wait <replaceable>sleep</replaceable> number of seconds before retrying the call.
  521. After <replaceable>retries</replaceable> number of attempts, the calling channel will continue at the next priority in the dialplan.
  522. If the <replaceable>retries</replaceable> setting is set to 0, this application will retry endlessly.
  523. While waiting to retry a call, a 1 digit extension may be dialed. If that
  524. extension exists in either the context defined in <variable>EXITCONTEXT</variable> or the current
  525. one, The call will jump to that extension immediately.
  526. The <replaceable>dialargs</replaceable> are specified in the same format that arguments are provided
  527. to the Dial application.</para>
  528. </description>
  529. </application>
  530. ***/
  531. static const char app[] = "Dial";
  532. static const char rapp[] = "RetryDial";
  533. enum {
  534. OPT_ANNOUNCE = (1 << 0),
  535. OPT_RESETCDR = (1 << 1),
  536. OPT_DTMF_EXIT = (1 << 2),
  537. OPT_SENDDTMF = (1 << 3),
  538. OPT_FORCECLID = (1 << 4),
  539. OPT_GO_ON = (1 << 5),
  540. OPT_CALLEE_HANGUP = (1 << 6),
  541. OPT_CALLER_HANGUP = (1 << 7),
  542. OPT_ORIGINAL_CLID = (1 << 8),
  543. OPT_DURATION_LIMIT = (1 << 9),
  544. OPT_MUSICBACK = (1 << 10),
  545. OPT_CALLEE_MACRO = (1 << 11),
  546. OPT_SCREEN_NOINTRO = (1 << 12),
  547. OPT_SCREEN_NOCALLERID = (1 << 13),
  548. OPT_IGNORE_CONNECTEDLINE = (1 << 14),
  549. OPT_SCREENING = (1 << 15),
  550. OPT_PRIVACY = (1 << 16),
  551. OPT_RINGBACK = (1 << 17),
  552. OPT_DURATION_STOP = (1 << 18),
  553. OPT_CALLEE_TRANSFER = (1 << 19),
  554. OPT_CALLER_TRANSFER = (1 << 20),
  555. OPT_CALLEE_MONITOR = (1 << 21),
  556. OPT_CALLER_MONITOR = (1 << 22),
  557. OPT_GOTO = (1 << 23),
  558. OPT_OPERMODE = (1 << 24),
  559. OPT_CALLEE_PARK = (1 << 25),
  560. OPT_CALLER_PARK = (1 << 26),
  561. OPT_IGNORE_FORWARDING = (1 << 27),
  562. OPT_CALLEE_GOSUB = (1 << 28),
  563. OPT_CALLEE_MIXMONITOR = (1 << 29),
  564. OPT_CALLER_MIXMONITOR = (1 << 30),
  565. };
  566. /* flags are now 64 bits, so keep it up! */
  567. #define DIAL_STILLGOING (1LLU << 31)
  568. #define DIAL_NOFORWARDHTML (1LLU << 32)
  569. #define DIAL_CALLERID_ABSENT (1LLU << 33) /* TRUE if caller id is not available for connected line. */
  570. #define OPT_CANCEL_ELSEWHERE (1LLU << 34)
  571. #define OPT_PEER_H (1LLU << 35)
  572. #define OPT_CALLEE_GO_ON (1LLU << 36)
  573. #define OPT_CANCEL_TIMEOUT (1LLU << 37)
  574. #define OPT_FORCE_CID_TAG (1LLU << 38)
  575. #define OPT_FORCE_CID_PRES (1LLU << 39)
  576. #define OPT_CALLER_ANSWER (1LLU << 40)
  577. enum {
  578. OPT_ARG_ANNOUNCE = 0,
  579. OPT_ARG_SENDDTMF,
  580. OPT_ARG_GOTO,
  581. OPT_ARG_DURATION_LIMIT,
  582. OPT_ARG_MUSICBACK,
  583. OPT_ARG_CALLEE_MACRO,
  584. OPT_ARG_RINGBACK,
  585. OPT_ARG_CALLEE_GOSUB,
  586. OPT_ARG_CALLEE_GO_ON,
  587. OPT_ARG_PRIVACY,
  588. OPT_ARG_DURATION_STOP,
  589. OPT_ARG_OPERMODE,
  590. OPT_ARG_SCREEN_NOINTRO,
  591. OPT_ARG_ORIGINAL_CLID,
  592. OPT_ARG_FORCECLID,
  593. OPT_ARG_FORCE_CID_TAG,
  594. OPT_ARG_FORCE_CID_PRES,
  595. /* note: this entry _MUST_ be the last one in the enum */
  596. OPT_ARG_ARRAY_SIZE,
  597. };
  598. AST_APP_OPTIONS(dial_exec_options, BEGIN_OPTIONS
  599. AST_APP_OPTION_ARG('A', OPT_ANNOUNCE, OPT_ARG_ANNOUNCE),
  600. AST_APP_OPTION('a', OPT_CALLER_ANSWER),
  601. AST_APP_OPTION('C', OPT_RESETCDR),
  602. AST_APP_OPTION('c', OPT_CANCEL_ELSEWHERE),
  603. AST_APP_OPTION('d', OPT_DTMF_EXIT),
  604. AST_APP_OPTION_ARG('D', OPT_SENDDTMF, OPT_ARG_SENDDTMF),
  605. AST_APP_OPTION('e', OPT_PEER_H),
  606. AST_APP_OPTION_ARG('f', OPT_FORCECLID, OPT_ARG_FORCECLID),
  607. AST_APP_OPTION_ARG('F', OPT_CALLEE_GO_ON, OPT_ARG_CALLEE_GO_ON),
  608. AST_APP_OPTION('g', OPT_GO_ON),
  609. AST_APP_OPTION_ARG('G', OPT_GOTO, OPT_ARG_GOTO),
  610. AST_APP_OPTION('h', OPT_CALLEE_HANGUP),
  611. AST_APP_OPTION('H', OPT_CALLER_HANGUP),
  612. AST_APP_OPTION('i', OPT_IGNORE_FORWARDING),
  613. AST_APP_OPTION('I', OPT_IGNORE_CONNECTEDLINE),
  614. AST_APP_OPTION('k', OPT_CALLEE_PARK),
  615. AST_APP_OPTION('K', OPT_CALLER_PARK),
  616. AST_APP_OPTION_ARG('L', OPT_DURATION_LIMIT, OPT_ARG_DURATION_LIMIT),
  617. AST_APP_OPTION_ARG('m', OPT_MUSICBACK, OPT_ARG_MUSICBACK),
  618. AST_APP_OPTION_ARG('M', OPT_CALLEE_MACRO, OPT_ARG_CALLEE_MACRO),
  619. AST_APP_OPTION_ARG('n', OPT_SCREEN_NOINTRO, OPT_ARG_SCREEN_NOINTRO),
  620. AST_APP_OPTION('N', OPT_SCREEN_NOCALLERID),
  621. AST_APP_OPTION_ARG('o', OPT_ORIGINAL_CLID, OPT_ARG_ORIGINAL_CLID),
  622. AST_APP_OPTION_ARG('O', OPT_OPERMODE, OPT_ARG_OPERMODE),
  623. AST_APP_OPTION('p', OPT_SCREENING),
  624. AST_APP_OPTION_ARG('P', OPT_PRIVACY, OPT_ARG_PRIVACY),
  625. AST_APP_OPTION_ARG('r', OPT_RINGBACK, OPT_ARG_RINGBACK),
  626. AST_APP_OPTION_ARG('S', OPT_DURATION_STOP, OPT_ARG_DURATION_STOP),
  627. AST_APP_OPTION_ARG('s', OPT_FORCE_CID_TAG, OPT_ARG_FORCE_CID_TAG),
  628. AST_APP_OPTION_ARG('u', OPT_FORCE_CID_PRES, OPT_ARG_FORCE_CID_PRES),
  629. AST_APP_OPTION('t', OPT_CALLEE_TRANSFER),
  630. AST_APP_OPTION('T', OPT_CALLER_TRANSFER),
  631. AST_APP_OPTION_ARG('U', OPT_CALLEE_GOSUB, OPT_ARG_CALLEE_GOSUB),
  632. AST_APP_OPTION('w', OPT_CALLEE_MONITOR),
  633. AST_APP_OPTION('W', OPT_CALLER_MONITOR),
  634. AST_APP_OPTION('x', OPT_CALLEE_MIXMONITOR),
  635. AST_APP_OPTION('X', OPT_CALLER_MIXMONITOR),
  636. AST_APP_OPTION('z', OPT_CANCEL_TIMEOUT),
  637. END_OPTIONS );
  638. #define CAN_EARLY_BRIDGE(flags,chan,peer) (!ast_test_flag64(flags, OPT_CALLEE_HANGUP | \
  639. OPT_CALLER_HANGUP | OPT_CALLEE_TRANSFER | OPT_CALLER_TRANSFER | \
  640. OPT_CALLEE_MONITOR | OPT_CALLER_MONITOR | OPT_CALLEE_PARK | \
  641. OPT_CALLER_PARK | OPT_ANNOUNCE | OPT_CALLEE_MACRO | OPT_CALLEE_GOSUB) && \
  642. !chan->audiohooks && !peer->audiohooks && \
  643. ast_framehook_list_is_empty(chan->framehooks) && ast_framehook_list_is_empty(peer->framehooks))
  644. /*
  645. * The list of active channels
  646. */
  647. struct chanlist {
  648. struct chanlist *next;
  649. struct ast_channel *chan;
  650. uint64_t flags;
  651. /*! Saved connected party info from an AST_CONTROL_CONNECTED_LINE. */
  652. struct ast_party_connected_line connected;
  653. /*! TRUE if an AST_CONTROL_CONNECTED_LINE update was saved to the connected element. */
  654. unsigned int pending_connected_update:1;
  655. struct ast_aoc_decoded *aoc_s_rate_list;
  656. };
  657. static int detect_disconnect(struct ast_channel *chan, char code, struct ast_str **featurecode);
  658. static void chanlist_free(struct chanlist *outgoing)
  659. {
  660. ast_party_connected_line_free(&outgoing->connected);
  661. ast_aoc_destroy_decoded(outgoing->aoc_s_rate_list);
  662. ast_free(outgoing);
  663. }
  664. static void hanguptree(struct chanlist *outgoing, struct ast_channel *exception, int answered_elsewhere)
  665. {
  666. /* Hang up a tree of stuff */
  667. struct chanlist *oo;
  668. while (outgoing) {
  669. /* Hangup any existing lines we have open */
  670. if (outgoing->chan && (outgoing->chan != exception)) {
  671. if (answered_elsewhere) {
  672. /* The flag is used for local channel inheritance and stuff */
  673. ast_set_flag(outgoing->chan, AST_FLAG_ANSWERED_ELSEWHERE);
  674. /* This is for the channel drivers */
  675. outgoing->chan->hangupcause = AST_CAUSE_ANSWERED_ELSEWHERE;
  676. }
  677. ast_hangup(outgoing->chan);
  678. }
  679. oo = outgoing;
  680. outgoing = outgoing->next;
  681. chanlist_free(oo);
  682. }
  683. }
  684. #define AST_MAX_WATCHERS 256
  685. /*
  686. * argument to handle_cause() and other functions.
  687. */
  688. struct cause_args {
  689. struct ast_channel *chan;
  690. int busy;
  691. int congestion;
  692. int nochan;
  693. };
  694. static void handle_cause(int cause, struct cause_args *num)
  695. {
  696. struct ast_cdr *cdr = num->chan->cdr;
  697. switch(cause) {
  698. case AST_CAUSE_BUSY:
  699. if (cdr)
  700. ast_cdr_busy(cdr);
  701. num->busy++;
  702. break;
  703. case AST_CAUSE_CONGESTION:
  704. if (cdr)
  705. ast_cdr_failed(cdr);
  706. num->congestion++;
  707. break;
  708. case AST_CAUSE_NO_ROUTE_DESTINATION:
  709. case AST_CAUSE_UNREGISTERED:
  710. if (cdr)
  711. ast_cdr_failed(cdr);
  712. num->nochan++;
  713. break;
  714. case AST_CAUSE_NO_ANSWER:
  715. if (cdr) {
  716. ast_cdr_noanswer(cdr);
  717. }
  718. break;
  719. case AST_CAUSE_NORMAL_CLEARING:
  720. break;
  721. default:
  722. num->nochan++;
  723. break;
  724. }
  725. }
  726. static int onedigit_goto(struct ast_channel *chan, const char *context, char exten, int pri)
  727. {
  728. char rexten[2] = { exten, '\0' };
  729. if (context) {
  730. if (!ast_goto_if_exists(chan, context, rexten, pri))
  731. return 1;
  732. } else {
  733. if (!ast_goto_if_exists(chan, chan->context, rexten, pri))
  734. return 1;
  735. else if (!ast_strlen_zero(chan->macrocontext)) {
  736. if (!ast_goto_if_exists(chan, chan->macrocontext, rexten, pri))
  737. return 1;
  738. }
  739. }
  740. return 0;
  741. }
  742. /* do not call with chan lock held */
  743. static const char *get_cid_name(char *name, int namelen, struct ast_channel *chan)
  744. {
  745. const char *context;
  746. const char *exten;
  747. ast_channel_lock(chan);
  748. context = ast_strdupa(S_OR(chan->macrocontext, chan->context));
  749. exten = ast_strdupa(S_OR(chan->macroexten, chan->exten));
  750. ast_channel_unlock(chan);
  751. return ast_get_hint(NULL, 0, name, namelen, chan, context, exten) ? name : "";
  752. }
  753. static void senddialevent(struct ast_channel *src, struct ast_channel *dst, const char *dialstring)
  754. {
  755. struct ast_channel *chans[] = { src, dst };
  756. ast_manager_event_multichan(EVENT_FLAG_CALL, "Dial", 2, chans,
  757. "SubEvent: Begin\r\n"
  758. "Channel: %s\r\n"
  759. "Destination: %s\r\n"
  760. "CallerIDNum: %s\r\n"
  761. "CallerIDName: %s\r\n"
  762. "ConnectedLineNum: %s\r\n"
  763. "ConnectedLineName: %s\r\n"
  764. "UniqueID: %s\r\n"
  765. "DestUniqueID: %s\r\n"
  766. "Dialstring: %s\r\n",
  767. src->name, dst->name,
  768. S_COR(src->caller.id.number.valid, src->caller.id.number.str, "<unknown>"),
  769. S_COR(src->caller.id.name.valid, src->caller.id.name.str, "<unknown>"),
  770. S_COR(src->connected.id.number.valid, src->connected.id.number.str, "<unknown>"),
  771. S_COR(src->connected.id.name.valid, src->connected.id.name.str, "<unknown>"),
  772. src->uniqueid, dst->uniqueid,
  773. dialstring ? dialstring : "");
  774. }
  775. static void senddialendevent(struct ast_channel *src, const char *dialstatus)
  776. {
  777. ast_manager_event(src, EVENT_FLAG_CALL, "Dial",
  778. "SubEvent: End\r\n"
  779. "Channel: %s\r\n"
  780. "UniqueID: %s\r\n"
  781. "DialStatus: %s\r\n",
  782. src->name, src->uniqueid, dialstatus);
  783. }
  784. /*!
  785. * helper function for wait_for_answer()
  786. *
  787. * \param o Outgoing call channel list.
  788. * \param num Incoming call channel cause accumulation
  789. * \param peerflags Dial option flags
  790. * \param single TRUE if there is only one outgoing call.
  791. * \param caller_entertained TRUE if the caller is being entertained by MOH or ringback.
  792. * \param to Remaining call timeout time.
  793. * \param forced_clid OPT_FORCECLID caller id to send
  794. * \param stored_clid Caller id representing the called party if needed
  795. *
  796. * XXX this code is highly suspicious, as it essentially overwrites
  797. * the outgoing channel without properly deleting it.
  798. *
  799. * \todo eventually this function should be intergrated into and replaced by ast_call_forward()
  800. */
  801. static void do_forward(struct chanlist *o, struct cause_args *num,
  802. struct ast_flags64 *peerflags, int single, int caller_entertained, int *to,
  803. struct ast_party_id *forced_clid, struct ast_party_id *stored_clid)
  804. {
  805. char tmpchan[256];
  806. struct ast_channel *original = o->chan;
  807. struct ast_channel *c = o->chan; /* the winner */
  808. struct ast_channel *in = num->chan; /* the input channel */
  809. char *stuff;
  810. char *tech;
  811. int cause;
  812. struct ast_party_caller caller;
  813. ast_copy_string(tmpchan, c->call_forward, sizeof(tmpchan));
  814. if ((stuff = strchr(tmpchan, '/'))) {
  815. *stuff++ = '\0';
  816. tech = tmpchan;
  817. } else {
  818. const char *forward_context;
  819. ast_channel_lock(c);
  820. forward_context = pbx_builtin_getvar_helper(c, "FORWARD_CONTEXT");
  821. if (ast_strlen_zero(forward_context)) {
  822. forward_context = NULL;
  823. }
  824. snprintf(tmpchan, sizeof(tmpchan), "%s@%s", c->call_forward, forward_context ? forward_context : c->context);
  825. ast_channel_unlock(c);
  826. stuff = tmpchan;
  827. tech = "Local";
  828. }
  829. if (!strcasecmp(tech, "Local")) {
  830. /*
  831. * Drop the connected line update block for local channels since
  832. * this is going to run dialplan and the user can change his
  833. * mind about what connected line information he wants to send.
  834. */
  835. ast_clear_flag64(o, OPT_IGNORE_CONNECTEDLINE);
  836. }
  837. ast_cel_report_event(in, AST_CEL_FORWARD, NULL, c->call_forward, NULL);
  838. /* Before processing channel, go ahead and check for forwarding */
  839. ast_verb(3, "Now forwarding %s to '%s/%s' (thanks to %s)\n", in->name, tech, stuff, c->name);
  840. /* If we have been told to ignore forwards, just set this channel to null and continue processing extensions normally */
  841. if (ast_test_flag64(peerflags, OPT_IGNORE_FORWARDING)) {
  842. ast_verb(3, "Forwarding %s to '%s/%s' prevented.\n", in->name, tech, stuff);
  843. c = o->chan = NULL;
  844. cause = AST_CAUSE_BUSY;
  845. } else {
  846. /* Setup parameters */
  847. c = o->chan = ast_request(tech, in->nativeformats, in, stuff, &cause);
  848. if (c) {
  849. if (single && !caller_entertained) {
  850. ast_channel_make_compatible(o->chan, in);
  851. }
  852. ast_channel_lock_both(in, o->chan);
  853. ast_channel_inherit_variables(in, o->chan);
  854. ast_channel_datastore_inherit(in, o->chan);
  855. ast_channel_unlock(in);
  856. ast_channel_unlock(o->chan);
  857. /* When a call is forwarded, we don't want to track new interfaces
  858. * dialed for CC purposes. Setting the done flag will ensure that
  859. * any Dial operations that happen later won't record CC interfaces.
  860. */
  861. ast_ignore_cc(o->chan);
  862. ast_log(LOG_NOTICE, "Not accepting call completion offers from call-forward recipient %s\n", o->chan->name);
  863. } else
  864. ast_log(LOG_NOTICE,
  865. "Forwarding failed to create channel to dial '%s/%s' (cause = %d)\n",
  866. tech, stuff, cause);
  867. }
  868. if (!c) {
  869. ast_clear_flag64(o, DIAL_STILLGOING);
  870. handle_cause(cause, num);
  871. ast_hangup(original);
  872. } else {
  873. ast_channel_lock_both(c, original);
  874. ast_party_redirecting_copy(&c->redirecting, &original->redirecting);
  875. ast_channel_unlock(c);
  876. ast_channel_unlock(original);
  877. ast_channel_lock_both(c, in);
  878. if (single && !caller_entertained && CAN_EARLY_BRIDGE(peerflags, c, in)) {
  879. ast_rtp_instance_early_bridge_make_compatible(c, in);
  880. }
  881. if (!c->redirecting.from.number.valid
  882. || ast_strlen_zero(c->redirecting.from.number.str)) {
  883. /*
  884. * The call was not previously redirected so it is
  885. * now redirected from this number.
  886. */
  887. ast_party_number_free(&c->redirecting.from.number);
  888. ast_party_number_init(&c->redirecting.from.number);
  889. c->redirecting.from.number.valid = 1;
  890. c->redirecting.from.number.str =
  891. ast_strdup(S_OR(in->macroexten, in->exten));
  892. }
  893. c->dialed.transit_network_select = in->dialed.transit_network_select;
  894. /* Determine CallerID to store in outgoing channel. */
  895. ast_party_caller_set_init(&caller, &c->caller);
  896. if (ast_test_flag64(peerflags, OPT_ORIGINAL_CLID)) {
  897. caller.id = *stored_clid;
  898. ast_channel_set_caller_event(c, &caller, NULL);
  899. ast_set_flag64(o, DIAL_CALLERID_ABSENT);
  900. } else if (ast_strlen_zero(S_COR(c->caller.id.number.valid,
  901. c->caller.id.number.str, NULL))) {
  902. /*
  903. * The new channel has no preset CallerID number by the channel
  904. * driver. Use the dialplan extension and hint name.
  905. */
  906. caller.id = *stored_clid;
  907. ast_channel_set_caller_event(c, &caller, NULL);
  908. ast_set_flag64(o, DIAL_CALLERID_ABSENT);
  909. } else {
  910. ast_clear_flag64(o, DIAL_CALLERID_ABSENT);
  911. }
  912. /* Determine CallerID for outgoing channel to send. */
  913. if (ast_test_flag64(o, OPT_FORCECLID)) {
  914. struct ast_party_connected_line connected;
  915. ast_party_connected_line_init(&connected);
  916. connected.id = *forced_clid;
  917. ast_party_connected_line_copy(&c->connected, &connected);
  918. } else {
  919. ast_connected_line_copy_from_caller(&c->connected, &in->caller);
  920. }
  921. ast_string_field_set(c, accountcode, in->accountcode);
  922. c->appl = "AppDial";
  923. c->data = "(Outgoing Line)";
  924. ast_channel_unlock(in);
  925. if (single && !ast_test_flag64(o, OPT_IGNORE_CONNECTEDLINE)) {
  926. struct ast_party_redirecting redirecting;
  927. /*
  928. * Redirecting updates to the caller make sense only on single
  929. * calls.
  930. *
  931. * We must unlock c before calling
  932. * ast_channel_redirecting_macro, because we put c into
  933. * autoservice there. That is pretty much a guaranteed
  934. * deadlock. This is why the handling of c's lock may seem a
  935. * bit unusual here.
  936. */
  937. ast_party_redirecting_init(&redirecting);
  938. ast_party_redirecting_copy(&redirecting, &c->redirecting);
  939. ast_channel_unlock(c);
  940. if (ast_channel_redirecting_macro(c, in, &redirecting, 1, 0)) {
  941. ast_channel_update_redirecting(in, &redirecting, NULL);
  942. }
  943. ast_party_redirecting_free(&redirecting);
  944. } else {
  945. ast_channel_unlock(c);
  946. }
  947. if (ast_test_flag64(peerflags, OPT_CANCEL_TIMEOUT)) {
  948. *to = -1;
  949. }
  950. if (ast_call(c, stuff, 0)) {
  951. ast_log(LOG_NOTICE, "Forwarding failed to dial '%s/%s'\n",
  952. tech, stuff);
  953. ast_clear_flag64(o, DIAL_STILLGOING);
  954. ast_hangup(original);
  955. ast_hangup(c);
  956. c = o->chan = NULL;
  957. num->nochan++;
  958. } else {
  959. ast_channel_lock_both(c, in);
  960. senddialevent(in, c, stuff);
  961. ast_channel_unlock(in);
  962. ast_channel_unlock(c);
  963. /* Hangup the original channel now, in case we needed it */
  964. ast_hangup(original);
  965. }
  966. if (single && !caller_entertained) {
  967. ast_indicate(in, -1);
  968. }
  969. }
  970. }
  971. /* argument used for some functions. */
  972. struct privacy_args {
  973. int sentringing;
  974. int privdb_val;
  975. char privcid[256];
  976. char privintro[1024];
  977. char status[256];
  978. };
  979. static struct ast_channel *wait_for_answer(struct ast_channel *in,
  980. struct chanlist *outgoing, int *to, struct ast_flags64 *peerflags,
  981. char *opt_args[],
  982. struct privacy_args *pa,
  983. const struct cause_args *num_in, int *result, char *dtmf_progress,
  984. const int ignore_cc,
  985. struct ast_party_id *forced_clid, struct ast_party_id *stored_clid)
  986. {
  987. struct cause_args num = *num_in;
  988. int prestart = num.busy + num.congestion + num.nochan;
  989. int orig = *to;
  990. struct ast_channel *peer = NULL;
  991. /* single is set if only one destination is enabled */
  992. int single = outgoing && !outgoing->next;
  993. int caller_entertained = outgoing
  994. && ast_test_flag64(outgoing, OPT_MUSICBACK | OPT_RINGBACK);
  995. #ifdef HAVE_EPOLL
  996. struct chanlist *epollo;
  997. #endif
  998. struct ast_party_connected_line connected_caller;
  999. struct ast_str *featurecode = ast_str_alloca(FEATURE_MAX_LEN + 1);
  1000. int cc_recall_core_id;
  1001. int is_cc_recall;
  1002. int cc_frame_received = 0;
  1003. int num_ringing = 0;
  1004. struct timeval start = ast_tvnow();
  1005. ast_party_connected_line_init(&connected_caller);
  1006. if (single) {
  1007. /* Turn off hold music, etc */
  1008. if (!caller_entertained) {
  1009. ast_deactivate_generator(in);
  1010. /* If we are calling a single channel, and not providing ringback or music, */
  1011. /* then, make them compatible for in-band tone purpose */
  1012. if (ast_channel_make_compatible(outgoing->chan, in) < 0) {
  1013. /* If these channels can not be made compatible,
  1014. * there is no point in continuing. The bridge
  1015. * will just fail if it gets that far.
  1016. */
  1017. *to = -1;
  1018. strcpy(pa->status, "CONGESTION");
  1019. ast_cdr_failed(in->cdr);
  1020. return NULL;
  1021. }
  1022. }
  1023. if (!ast_test_flag64(outgoing, OPT_IGNORE_CONNECTEDLINE)
  1024. && !ast_test_flag64(outgoing, DIAL_CALLERID_ABSENT)) {
  1025. ast_channel_lock(outgoing->chan);
  1026. ast_connected_line_copy_from_caller(&connected_caller, &outgoing->chan->caller);
  1027. ast_channel_unlock(outgoing->chan);
  1028. connected_caller.source = AST_CONNECTED_LINE_UPDATE_SOURCE_ANSWER;
  1029. if (ast_channel_connected_line_macro(outgoing->chan, in, &connected_caller, 1, 0)) {
  1030. ast_channel_update_connected_line(in, &connected_caller, NULL);
  1031. }
  1032. ast_party_connected_line_free(&connected_caller);
  1033. }
  1034. }
  1035. is_cc_recall = ast_cc_is_recall(in, &cc_recall_core_id, NULL);
  1036. #ifdef HAVE_EPOLL
  1037. for (epollo = outgoing; epollo; epollo = epollo->next)
  1038. ast_poll_channel_add(in, epollo->chan);
  1039. #endif
  1040. while ((*to = ast_remaining_ms(start, orig)) && !peer) {
  1041. struct chanlist *o;
  1042. int pos = 0; /* how many channels do we handle */
  1043. int numlines = prestart;
  1044. struct ast_channel *winner;
  1045. struct ast_channel *watchers[AST_MAX_WATCHERS];
  1046. watchers[pos++] = in;
  1047. for (o = outgoing; o; o = o->next) {
  1048. /* Keep track of important channels */
  1049. if (ast_test_flag64(o, DIAL_STILLGOING) && o->chan)
  1050. watchers[pos++] = o->chan;
  1051. numlines++;
  1052. }
  1053. if (pos == 1) { /* only the input channel is available */
  1054. if (numlines == (num.busy + num.congestion + num.nochan)) {
  1055. ast_verb(2, "Everyone is busy/congested at this time (%d:%d/%d/%d)\n", numlines, num.busy, num.congestion, num.nochan);
  1056. if (num.busy)
  1057. strcpy(pa->status, "BUSY");
  1058. else if (num.congestion)
  1059. strcpy(pa->status, "CONGESTION");
  1060. else if (num.nochan)
  1061. strcpy(pa->status, "CHANUNAVAIL");
  1062. } else {
  1063. ast_verb(3, "No one is available to answer at this time (%d:%d/%d/%d)\n", numlines, num.busy, num.congestion, num.nochan);
  1064. }
  1065. *to = 0;
  1066. if (is_cc_recall) {
  1067. ast_cc_failed(cc_recall_core_id, "Everyone is busy/congested for the recall. How sad");
  1068. }
  1069. return NULL;
  1070. }
  1071. winner = ast_waitfor_n(watchers, pos, to);
  1072. for (o = outgoing; o; o = o->next) {
  1073. struct ast_frame *f;
  1074. struct ast_channel *c = o->chan;
  1075. if (c == NULL)
  1076. continue;
  1077. if (ast_test_flag64(o, DIAL_STILLGOING) && c->_state == AST_STATE_UP) {
  1078. if (!peer) {
  1079. ast_verb(3, "%s answered %s\n", c->name, in->name);
  1080. if (!single && !ast_test_flag64(o, OPT_IGNORE_CONNECTEDLINE)) {
  1081. if (o->pending_connected_update) {
  1082. if (ast_channel_connected_line_macro(c, in, &o->connected, 1, 0)) {
  1083. ast_channel_update_connected_line(in, &o->connected, NULL);
  1084. }
  1085. } else if (!ast_test_flag64(o, DIAL_CALLERID_ABSENT)) {
  1086. ast_channel_lock(c);
  1087. ast_connected_line_copy_from_caller(&connected_caller, &c->caller);
  1088. ast_channel_unlock(c);
  1089. connected_caller.source = AST_CONNECTED_LINE_UPDATE_SOURCE_ANSWER;
  1090. if (ast_channel_connected_line_macro(c, in, &connected_caller, 1, 0)) {
  1091. ast_channel_update_connected_line(in, &connected_caller, NULL);
  1092. }
  1093. ast_party_connected_line_free(&connected_caller);
  1094. }
  1095. }
  1096. if (o->aoc_s_rate_list) {
  1097. size_t encoded_size;
  1098. struct ast_aoc_encoded *encoded;
  1099. if ((encoded = ast_aoc_encode(o->aoc_s_rate_list, &encoded_size, o->chan))) {
  1100. ast_indicate_data(in, AST_CONTROL_AOC, encoded, encoded_size);
  1101. ast_aoc_destroy_encoded(encoded);
  1102. }
  1103. }
  1104. peer = c;
  1105. ast_copy_flags64(peerflags, o,
  1106. OPT_CALLEE_TRANSFER | OPT_CALLER_TRANSFER |
  1107. OPT_CALLEE_HANGUP | OPT_CALLER_HANGUP |
  1108. OPT_CALLEE_MONITOR | OPT_CALLER_MONITOR |
  1109. OPT_CALLEE_PARK | OPT_CALLER_PARK |
  1110. OPT_CALLEE_MIXMONITOR | OPT_CALLER_MIXMONITOR |
  1111. DIAL_NOFORWARDHTML);
  1112. ast_string_field_set(c, dialcontext, "");
  1113. ast_copy_string(c->exten, "", sizeof(c->exten));
  1114. }
  1115. continue;
  1116. }
  1117. if (c != winner)
  1118. continue;
  1119. /* here, o->chan == c == winner */
  1120. if (!ast_strlen_zero(c->call_forward)) {
  1121. pa->sentringing = 0;
  1122. if (!ignore_cc && (f = ast_read(c))) {
  1123. if (f->frametype == AST_FRAME_CONTROL && f->subclass.integer == AST_CONTROL_CC) {
  1124. /* This channel is forwarding the call, and is capable of CC, so
  1125. * be sure to add the new device interface to the list
  1126. */
  1127. ast_handle_cc_control_frame(in, c, f->data.ptr);
  1128. }
  1129. ast_frfree(f);
  1130. }
  1131. if (o->pending_connected_update) {
  1132. /*
  1133. * Re-seed the chanlist's connected line information with
  1134. * previously acquired connected line info from the incoming
  1135. * channel. The previously acquired connected line info could
  1136. * have been set through the CONNECTED_LINE dialplan function.
  1137. */
  1138. o->pending_connected_update = 0;
  1139. ast_channel_lock(in);
  1140. ast_party_connected_line_copy(&o->connected, &in->connected);
  1141. ast_channel_unlock(in);
  1142. }
  1143. do_forward(o, &num, peerflags, single, caller_entertained, &orig,
  1144. forced_clid, stored_clid);
  1145. if (single && o->chan
  1146. && !ast_test_flag64(o, OPT_IGNORE_CONNECTEDLINE)
  1147. && !ast_test_flag64(o, DIAL_CALLERID_ABSENT)) {
  1148. ast_channel_lock(o->chan);
  1149. ast_connected_line_copy_from_caller(&connected_caller, &o->chan->caller);
  1150. ast_channel_unlock(o->chan);
  1151. connected_caller.source = AST_CONNECTED_LINE_UPDATE_SOURCE_ANSWER;
  1152. if (ast_channel_connected_line_macro(o->chan, in, &connected_caller, 1, 0)) {
  1153. ast_channel_update_connected_line(in, &connected_caller, NULL);
  1154. }
  1155. ast_party_connected_line_free(&connected_caller);
  1156. }
  1157. continue;
  1158. }
  1159. f = ast_read(winner);
  1160. if (!f) {
  1161. in->hangupcause = c->hangupcause;
  1162. #ifdef HAVE_EPOLL
  1163. ast_poll_channel_del(in, c);
  1164. #endif
  1165. ast_hangup(c);
  1166. c = o->chan = NULL;
  1167. ast_clear_flag64(o, DIAL_STILLGOING);
  1168. handle_cause(in->hangupcause, &num);
  1169. continue;
  1170. }
  1171. switch (f->frametype) {
  1172. case AST_FRAME_CONTROL:
  1173. switch (f->subclass.integer) {
  1174. case AST_CONTROL_ANSWER:
  1175. /* This is our guy if someone answered. */
  1176. if (!peer) {
  1177. ast_verb(3, "%s answered %s\n", c->name, in->name);
  1178. if (!single && !ast_test_flag64(o, OPT_IGNORE_CONNECTEDLINE)) {
  1179. if (o->pending_connected_update) {
  1180. if (ast_channel_connected_line_macro(c, in, &o->connected, 1, 0)) {
  1181. ast_channel_update_connected_line(in, &o->connected, NULL);
  1182. }
  1183. } else if (!ast_test_flag64(o, DIAL_CALLERID_ABSENT)) {
  1184. ast_channel_lock(c);
  1185. ast_connected_line_copy_from_caller(&connected_caller, &c->caller);
  1186. ast_channel_unlock(c);
  1187. connected_caller.source = AST_CONNECTED_LINE_UPDATE_SOURCE_ANSWER;
  1188. if (ast_channel_connected_line_macro(c, in, &connected_caller, 1, 0)) {
  1189. ast_channel_update_connected_line(in, &connected_caller, NULL);
  1190. }
  1191. ast_party_connected_line_free(&connected_caller);
  1192. }
  1193. }
  1194. if (o->aoc_s_rate_list) {
  1195. size_t encoded_size;
  1196. struct ast_aoc_encoded *encoded;
  1197. if ((encoded = ast_aoc_encode(o->aoc_s_rate_list, &encoded_size, o->chan))) {
  1198. ast_indicate_data(in, AST_CONTROL_AOC, encoded, encoded_size);
  1199. ast_aoc_destroy_encoded(encoded);
  1200. }
  1201. }
  1202. peer = c;
  1203. if (peer->cdr) {
  1204. peer->cdr->answer = ast_tvnow();
  1205. peer->cdr->disposition = AST_CDR_ANSWERED;
  1206. }
  1207. ast_copy_flags64(peerflags, o,
  1208. OPT_CALLEE_TRANSFER | OPT_CALLER_TRANSFER |
  1209. OPT_CALLEE_HANGUP | OPT_CALLER_HANGUP |
  1210. OPT_CALLEE_MONITOR | OPT_CALLER_MONITOR |
  1211. OPT_CALLEE_PARK | OPT_CALLER_PARK |
  1212. OPT_CALLEE_MIXMONITOR | OPT_CALLER_MIXMONITOR |
  1213. DIAL_NOFORWARDHTML);
  1214. ast_string_field_set(c, dialcontext, "");
  1215. ast_copy_string(c->exten, "", sizeof(c->exten));
  1216. if (CAN_EARLY_BRIDGE(peerflags, in, peer))
  1217. /* Setup early bridge if appropriate */
  1218. ast_channel_early_bridge(in, peer);
  1219. }
  1220. /* If call has been answered, then the eventual hangup is likely to be normal hangup */
  1221. in->hangupcause = AST_CAUSE_NORMAL_CLEARING;
  1222. c->hangupcause = AST_CAUSE_NORMAL_CLEARING;
  1223. break;
  1224. case AST_CONTROL_BUSY:
  1225. ast_verb(3, "%s is busy\n", c->name);
  1226. in->hangupcause = c->hangupcause;
  1227. ast_hangup(c);
  1228. c = o->chan = NULL;
  1229. ast_clear_flag64(o, DIAL_STILLGOING);
  1230. handle_cause(AST_CAUSE_BUSY, &num);
  1231. break;
  1232. case AST_CONTROL_CONGESTION:
  1233. ast_verb(3, "%s is circuit-busy\n", c->name);
  1234. in->hangupcause = c->hangupcause;
  1235. ast_hangup(c);
  1236. c = o->chan = NULL;
  1237. ast_clear_flag64(o, DIAL_STILLGOING);
  1238. handle_cause(AST_CAUSE_CONGESTION, &num);
  1239. break;
  1240. case AST_CONTROL_RINGING:
  1241. /* This is a tricky area to get right when using a native
  1242. * CC agent. The reason is that we do the best we can to send only a
  1243. * single ringing notification to the caller.
  1244. *
  1245. * Call completion complicates the logic used here. CCNR is typically
  1246. * offered during a ringing message. Let's say that party A calls
  1247. * parties B, C, and D. B and C do not support CC requests, but D
  1248. * does. If we were to receive a ringing notification from B before
  1249. * the others, then we would end up sending a ringing message to
  1250. * A with no CCNR offer present.
  1251. *
  1252. * The approach that we have taken is that if we receive a ringing
  1253. * response from a party and no CCNR offer is present, we need to
  1254. * wait. Specifically, we need to wait until either a) a called party
  1255. * offers CCNR in its ringing response or b) all called parties have
  1256. * responded in some way to our call and none offers CCNR.
  1257. *
  1258. * The drawback to this is that if one of the parties has a delayed
  1259. * response or, god forbid, one just plain doesn't respond to our
  1260. * outgoing call, then this will result in a significant delay between
  1261. * when the caller places the call and hears ringback.
  1262. *
  1263. * Note also that if CC is disabled for this call, then it is perfectly
  1264. * fine for ringing frames to get sent through.
  1265. */
  1266. ++num_ringing;
  1267. if (ignore_cc || cc_frame_received || num_ringing == numlines) {
  1268. ast_verb(3, "%s is ringing\n", c->name);
  1269. /* Setup early media if appropriate */
  1270. if (single && !caller_entertained
  1271. && CAN_EARLY_BRIDGE(peerflags, in, c)) {
  1272. ast_channel_early_bridge(in, c);
  1273. }
  1274. if (!(pa->sentringing) && !ast_test_flag64(outgoing, OPT_MUSICBACK) && ast_strlen_zero(opt_args[OPT_ARG_RINGBACK])) {
  1275. ast_indicate(in, AST_CONTROL_RINGING);
  1276. pa->sentringing++;
  1277. }
  1278. }
  1279. break;
  1280. case AST_CONTROL_PROGRESS:
  1281. ast_verb(3, "%s is making progress passing it to %s\n", c->name, in->name);
  1282. /* Setup early media if appropriate */
  1283. if (single && !caller_entertained
  1284. && CAN_EARLY_BRIDGE(peerflags, in, c)) {
  1285. ast_channel_early_bridge(in, c);
  1286. }
  1287. if (!ast_test_flag64(outgoing, OPT_RINGBACK)) {
  1288. if (single || (!single && !pa->sentringing)) {
  1289. ast_indicate(in, AST_CONTROL_PROGRESS);
  1290. }
  1291. }
  1292. if (!ast_strlen_zero(dtmf_progress)) {
  1293. ast_verb(3,
  1294. "Sending DTMF '%s' to the called party as result of receiving a PROGRESS message.\n",
  1295. dtmf_progress);
  1296. ast_dtmf_stream(c, in, dtmf_progress, 250, 0);
  1297. }
  1298. break;
  1299. case AST_CONTROL_VIDUPDATE:
  1300. case AST_CONTROL_SRCUPDATE:
  1301. case AST_CONTROL_SRCCHANGE:
  1302. if (!single || caller_entertained) {
  1303. break;
  1304. }
  1305. ast_verb(3, "%s requested media update control %d, passing it to %s\n",
  1306. c->name, f->subclass.integer, in->name);
  1307. ast_indicate(in, f->subclass.integer);
  1308. break;
  1309. case AST_CONTROL_CONNECTED_LINE:
  1310. if (ast_test_flag64(o, OPT_IGNORE_CONNECTEDLINE)) {
  1311. ast_verb(3, "Connected line update to %s prevented.\n", in->name);
  1312. break;
  1313. }
  1314. if (!single) {
  1315. struct ast_party_connected_line connected;
  1316. ast_verb(3, "%s connected line has changed. Saving it until answer for %s\n",
  1317. c->name, in->name);
  1318. ast_party_connected_line_set_init(&connected, &o->connected);
  1319. ast_connected_line_parse_data(f->data.ptr, f->datalen, &connected);
  1320. ast_party_connected_line_set(&o->connected, &connected, NULL);
  1321. ast_party_connected_line_free(&connected);
  1322. o->pending_connected_update = 1;
  1323. break;
  1324. }
  1325. if (ast_channel_connected_line_macro(c, in, f, 1, 1)) {
  1326. ast_indicate_data(in, AST_CONTROL_CONNECTED_LINE, f->data.ptr, f->datalen);
  1327. }
  1328. break;
  1329. case AST_CONTROL_AOC:
  1330. {
  1331. struct ast_aoc_decoded *decoded = ast_aoc_decode(f->data.ptr, f->datalen, o->chan);
  1332. if (decoded && (ast_aoc_get_msg_type(decoded) == AST_AOC_S)) {
  1333. ast_aoc_destroy_decoded(o->aoc_s_rate_list);
  1334. o->aoc_s_rate_list = decoded;
  1335. } else {
  1336. ast_aoc_destroy_decoded(decoded);
  1337. }
  1338. }
  1339. break;
  1340. case AST_CONTROL_REDIRECTING:
  1341. if (!single) {
  1342. /*
  1343. * Redirecting updates to the caller make sense only on single
  1344. * calls.
  1345. */
  1346. break;
  1347. }
  1348. if (ast_test_flag64(o, OPT_IGNORE_CONNECTEDLINE)) {
  1349. ast_verb(3, "Redirecting update to %s prevented.\n", in->name);
  1350. break;
  1351. }
  1352. ast_verb(3, "%s redirecting info has changed, passing it to %s\n",
  1353. c->name, in->name);
  1354. if (ast_channel_redirecting_macro(c, in, f, 1, 1)) {
  1355. ast_indicate_data(in, AST_CONTROL_REDIRECTING, f->data.ptr, f->datalen);
  1356. }
  1357. pa->sentringing = 0;
  1358. break;
  1359. case AST_CONTROL_PROCEEDING:
  1360. ast_verb(3, "%s is proceeding passing it to %s\n", c->name, in->name);
  1361. if (single && !caller_entertained
  1362. && CAN_EARLY_BRIDGE(peerflags, in, c)) {
  1363. ast_channel_early_bridge(in, c);
  1364. }
  1365. if (!ast_test_flag64(outgoing, OPT_RINGBACK))
  1366. ast_indicate(in, AST_CONTROL_PROCEEDING);
  1367. break;
  1368. case AST_CONTROL_HOLD:
  1369. /* XXX this should be saved like AST_CONTROL_CONNECTED_LINE for !single || caller_entertained */
  1370. ast_verb(3, "Call on %s placed on hold\n", c->name);
  1371. ast_indicate_data(in, AST_CONTROL_HOLD, f->data.ptr, f->datalen);
  1372. break;
  1373. case AST_CONTROL_UNHOLD:
  1374. /* XXX this should be saved like AST_CONTROL_CONNECTED_LINE for !single || caller_entertained */
  1375. ast_verb(3, "Call on %s left from hold\n", c->name);
  1376. ast_indicate(in, AST_CONTROL_UNHOLD);
  1377. break;
  1378. case AST_CONTROL_OFFHOOK:
  1379. case AST_CONTROL_FLASH:
  1380. /* Ignore going off hook and flash */
  1381. break;
  1382. case AST_CONTROL_CC:
  1383. if (!ignore_cc) {
  1384. ast_handle_cc_control_frame(in, c, f->data.ptr);
  1385. cc_frame_received = 1;
  1386. }
  1387. break;
  1388. case -1:
  1389. if (single && !caller_entertained) {
  1390. ast_verb(3, "%s stopped sounds\n", c->name);
  1391. ast_indicate(in, -1);
  1392. pa->sentringing = 0;
  1393. }
  1394. break;
  1395. default:
  1396. ast_debug(1, "Dunno what to do with control type %d\n", f->subclass.integer);
  1397. break;
  1398. }
  1399. break;
  1400. case AST_FRAME_VOICE:
  1401. case AST_FRAME_IMAGE:
  1402. if (caller_entertained) {
  1403. break;
  1404. }
  1405. /* Fall through */
  1406. case AST_FRAME_TEXT:
  1407. if (single && ast_write(in, f)) {
  1408. ast_log(LOG_WARNING, "Unable to write frametype: %u\n",
  1409. f->frametype);
  1410. }
  1411. break;
  1412. case AST_FRAME_HTML:
  1413. if (single && !ast_test_flag64(outgoing, DIAL_NOFORWARDHTML)
  1414. && ast_channel_sendhtml(in, f->subclass.integer, f->data.ptr, f->datalen) == -1) {
  1415. ast_log(LOG_WARNING, "Unable to send URL\n");
  1416. }
  1417. break;
  1418. default:
  1419. break;
  1420. }
  1421. ast_frfree(f);
  1422. } /* end for */
  1423. if (winner == in) {
  1424. struct ast_frame *f = ast_read(in);
  1425. #if 0
  1426. if (f && (f->frametype != AST_FRAME_VOICE))
  1427. printf("Frame type: %d, %d\n", f->frametype, f->subclass);
  1428. else if (!f || (f->frametype != AST_FRAME_VOICE))
  1429. printf("Hangup received on %s\n", in->name);
  1430. #endif
  1431. if (!f || ((f->frametype == AST_FRAME_CONTROL) && (f->subclass.integer == AST_CONTROL_HANGUP))) {
  1432. /* Got hung up */
  1433. *to = -1;
  1434. strcpy(pa->status, "CANCEL");
  1435. ast_cdr_noanswer(in->cdr);
  1436. if (f) {
  1437. if (f->data.uint32) {
  1438. in->hangupcause = f->data.uint32;
  1439. }
  1440. ast_frfree(f);
  1441. }
  1442. if (is_cc_recall) {
  1443. ast_cc_completed(in, "CC completed, although the caller hung up (cancelled)");
  1444. }
  1445. return NULL;
  1446. }
  1447. /* now f is guaranteed non-NULL */
  1448. if (f->frametype == AST_FRAME_DTMF) {
  1449. if (ast_test_flag64(peerflags, OPT_DTMF_EXIT)) {
  1450. const char *context;
  1451. ast_channel_lock(in);
  1452. context = pbx_builtin_getvar_helper(in, "EXITCONTEXT");
  1453. if (onedigit_goto(in, context, (char) f->subclass.integer, 1)) {
  1454. ast_verb(3, "User hit %c to disconnect call.\n", f->subclass.integer);
  1455. *to = 0;
  1456. ast_cdr_noanswer(in->cdr);
  1457. *result = f->subclass.integer;
  1458. strcpy(pa->status, "CANCEL");
  1459. ast_frfree(f);
  1460. ast_channel_unlock(in);
  1461. if (is_cc_recall) {
  1462. ast_cc_completed(in, "CC completed, but the caller used DTMF to exit");
  1463. }
  1464. return NULL;
  1465. }
  1466. ast_channel_unlock(in);
  1467. }
  1468. if (ast_test_flag64(peerflags, OPT_CALLER_HANGUP) &&
  1469. detect_disconnect(in, f->subclass.integer, &featurecode)) {
  1470. ast_verb(3, "User requested call disconnect.\n");
  1471. *to = 0;
  1472. strcpy(pa->status, "CANCEL");
  1473. ast_cdr_noanswer(in->cdr);
  1474. ast_frfree(f);
  1475. if (is_cc_recall) {
  1476. ast_cc_completed(in, "CC completed, but the caller hung up with DTMF");
  1477. }
  1478. return NULL;
  1479. }
  1480. }
  1481. /* Send the frame from the in channel to all outgoing channels. */
  1482. for (o = outgoing; o; o = o->next) {
  1483. if (!o->chan || !ast_test_flag64(o, DIAL_STILLGOING)) {
  1484. /* This outgoing channel has died so don't send the frame to it. */
  1485. continue;
  1486. }
  1487. switch (f->frametype) {
  1488. case AST_FRAME_HTML:
  1489. /* Forward HTML stuff */
  1490. if (!ast_test_flag64(o, DIAL_NOFORWARDHTML)
  1491. && ast_channel_sendhtml(o->chan, f->subclass.integer, f->data.ptr, f->datalen) == -1) {
  1492. ast_log(LOG_WARNING, "Unable to send URL\n");
  1493. }
  1494. break;
  1495. case AST_FRAME_VOICE:
  1496. case AST_FRAME_IMAGE:
  1497. if (!single || caller_entertained) {
  1498. /*
  1499. * We are calling multiple parties or caller is being
  1500. * entertained and has thus not been made compatible.
  1501. * No need to check any other called parties.
  1502. */
  1503. goto skip_frame;
  1504. }
  1505. /* Fall through */
  1506. case AST_FRAME_TEXT:
  1507. case AST_FRAME_DTMF_BEGIN:
  1508. case AST_FRAME_DTMF_END:
  1509. if (ast_write(o->chan, f)) {
  1510. ast_log(LOG_WARNING, "Unable to forward frametype: %u\n",
  1511. f->frametype);
  1512. }
  1513. break;
  1514. case AST_FRAME_CONTROL:
  1515. switch (f->subclass.integer) {
  1516. case AST_CONTROL_HOLD:
  1517. ast_verb(3, "Call on %s placed on hold\n", o->chan->name);
  1518. ast_indicate_data(o->chan, AST_CONTROL_HOLD, f->data.ptr, f->datalen);
  1519. break;
  1520. case AST_CONTROL_UNHOLD:
  1521. ast_verb(3, "Call on %s left from hold\n", o->chan->name);
  1522. ast_indicate(o->chan, AST_CONTROL_UNHOLD);
  1523. break;
  1524. case AST_CONTROL_VIDUPDATE:
  1525. case AST_CONTROL_SRCUPDATE:
  1526. case AST_CONTROL_SRCCHANGE:
  1527. if (!single || caller_entertained) {
  1528. /*
  1529. * We are calling multiple parties or caller is being
  1530. * entertained and has thus not been made compatible.
  1531. * No need to check any other called parties.
  1532. */
  1533. goto skip_frame;
  1534. }
  1535. ast_verb(3, "%s requested media update control %d, passing it to %s\n",
  1536. in->name, f->subclass.integer, o->chan->name);
  1537. ast_indicate(o->chan, f->subclass.integer);
  1538. break;
  1539. case AST_CONTROL_CONNECTED_LINE:
  1540. if (ast_channel_connected_line_macro(in, o->chan, f, 0, 1)) {
  1541. ast_indicate_data(o->chan, f->subclass.integer, f->data.ptr, f->datalen);
  1542. }
  1543. break;
  1544. case AST_CONTROL_REDIRECTING:
  1545. if (ast_channel_redirecting_macro(in, o->chan, f, 0, 1)) {
  1546. ast_indicate_data(o->chan, f->subclass.integer, f->data.ptr, f->datalen);
  1547. }
  1548. break;
  1549. default:
  1550. /* We are not going to do anything with this frame. */
  1551. goto skip_frame;
  1552. }
  1553. break;
  1554. default:
  1555. /* We are not going to do anything with this frame. */
  1556. goto skip_frame;
  1557. }
  1558. }
  1559. skip_frame:;
  1560. ast_frfree(f);
  1561. }
  1562. }
  1563. if (!*to) {
  1564. ast_verb(3, "Nobody picked up in %d ms\n", orig);
  1565. }
  1566. if (!*to || ast_check_hangup(in)) {
  1567. ast_cdr_noanswer(in->cdr);
  1568. }
  1569. #ifdef HAVE_EPOLL
  1570. for (epollo = outgoing; epollo; epollo = epollo->next) {
  1571. if (epollo->chan)
  1572. ast_poll_channel_del(in, epollo->chan);
  1573. }
  1574. #endif
  1575. if (is_cc_recall) {
  1576. ast_cc_completed(in, "Recall completed!");
  1577. }
  1578. return peer;
  1579. }
  1580. static int detect_disconnect(struct ast_channel *chan, char code, struct ast_str **featurecode)
  1581. {
  1582. struct ast_flags features = { AST_FEATURE_DISCONNECT }; /* only concerned with disconnect feature */
  1583. struct ast_call_feature feature = { 0, };
  1584. int res;
  1585. ast_str_append(featurecode, 1, "%c", code);
  1586. res = ast_feature_detect(chan, &features, ast_str_buffer(*featurecode), &feature);
  1587. if (res != AST_FEATURE_RETURN_STOREDIGITS) {
  1588. ast_str_reset(*featurecode);
  1589. }
  1590. if (feature.feature_mask & AST_FEATURE_DISCONNECT) {
  1591. return 1;
  1592. }
  1593. return 0;
  1594. }
  1595. static void replace_macro_delimiter(char *s)
  1596. {
  1597. for (; *s; s++)
  1598. if (*s == '^')
  1599. *s = ',';
  1600. }
  1601. /* returns true if there is a valid privacy reply */
  1602. static int valid_priv_reply(struct ast_flags64 *opts, int res)
  1603. {
  1604. if (res < '1')
  1605. return 0;
  1606. if (ast_test_flag64(opts, OPT_PRIVACY) && res <= '5')
  1607. return 1;
  1608. if (ast_test_flag64(opts, OPT_SCREENING) && res <= '4')
  1609. return 1;
  1610. return 0;
  1611. }
  1612. static int do_privacy(struct ast_channel *chan, struct ast_channel *peer,
  1613. struct ast_flags64 *opts, char **opt_args, struct privacy_args *pa)
  1614. {
  1615. int res2;
  1616. int loopcount = 0;
  1617. /* Get the user's intro, store it in priv-callerintros/$CID,
  1618. unless it is already there-- this should be done before the
  1619. call is actually dialed */
  1620. /* all ring indications and moh for the caller has been halted as soon as the
  1621. target extension was picked up. We are going to have to kill some
  1622. time and make the caller believe the peer hasn't picked up yet */
  1623. if (ast_test_flag64(opts, OPT_MUSICBACK) && !ast_strlen_zero(opt_args[OPT_ARG_MUSICBACK])) {
  1624. char *original_moh = ast_strdupa(chan->musicclass);
  1625. ast_indicate(chan, -1);
  1626. ast_string_field_set(chan, musicclass, opt_args[OPT_ARG_MUSICBACK]);
  1627. ast_moh_start(chan, opt_args[OPT_ARG_MUSICBACK], NULL);
  1628. ast_string_field_set(chan, musicclass, original_moh);
  1629. } else if (ast_test_flag64(opts, OPT_RINGBACK)) {
  1630. ast_indicate(chan, AST_CONTROL_RINGING);
  1631. pa->sentringing++;
  1632. }
  1633. /* Start autoservice on the other chan ?? */
  1634. res2 = ast_autoservice_start(chan);
  1635. /* Now Stream the File */
  1636. for (loopcount = 0; loopcount < 3; loopcount++) {
  1637. if (res2 && loopcount == 0) /* error in ast_autoservice_start() */
  1638. break;
  1639. if (!res2) /* on timeout, play the message again */
  1640. res2 = ast_play_and_wait(peer, "priv-callpending");
  1641. if (!valid_priv_reply(opts, res2))
  1642. res2 = 0;
  1643. /* priv-callpending script:
  1644. "I have a caller waiting, who introduces themselves as:"
  1645. */
  1646. if (!res2)
  1647. res2 = ast_play_and_wait(peer, pa->privintro);
  1648. if (!valid_priv_reply(opts, res2))
  1649. res2 = 0;
  1650. /* now get input from the called party, as to their choice */
  1651. if (!res2) {
  1652. /* XXX can we have both, or they are mutually exclusive ? */
  1653. if (ast_test_flag64(opts, OPT_PRIVACY))
  1654. res2 = ast_play_and_wait(peer, "priv-callee-options");
  1655. if (ast_test_flag64(opts, OPT_SCREENING))
  1656. res2 = ast_play_and_wait(peer, "screen-callee-options");
  1657. }
  1658. /*! \page DialPrivacy Dial Privacy scripts
  1659. \par priv-callee-options script:
  1660. "Dial 1 if you wish this caller to reach you directly in the future,
  1661. and immediately connect to their incoming call
  1662. Dial 2 if you wish to send this caller to voicemail now and
  1663. forevermore.
  1664. Dial 3 to send this caller to the torture menus, now and forevermore.
  1665. Dial 4 to send this caller to a simple "go away" menu, now and forevermore.
  1666. Dial 5 to allow this caller to come straight thru to you in the future,
  1667. but right now, just this once, send them to voicemail."
  1668. \par screen-callee-options script:
  1669. "Dial 1 if you wish to immediately connect to the incoming call
  1670. Dial 2 if you wish to send this caller to voicemail.
  1671. Dial 3 to send this caller to the torture menus.
  1672. Dial 4 to send this caller to a simple "go away" menu.
  1673. */
  1674. if (valid_priv_reply(opts, res2))
  1675. break;
  1676. /* invalid option */
  1677. res2 = ast_play_and_wait(peer, "vm-sorry");
  1678. }
  1679. if (ast_test_flag64(opts, OPT_MUSICBACK)) {
  1680. ast_moh_stop(chan);
  1681. } else if (ast_test_flag64(opts, OPT_RINGBACK)) {
  1682. ast_indicate(chan, -1);
  1683. pa->sentringing = 0;
  1684. }
  1685. ast_autoservice_stop(chan);
  1686. if (ast_test_flag64(opts, OPT_PRIVACY) && (res2 >= '1' && res2 <= '5')) {
  1687. /* map keypresses to various things, the index is res2 - '1' */
  1688. static const char * const _val[] = { "ALLOW", "DENY", "TORTURE", "KILL", "ALLOW" };
  1689. static const int _flag[] = { AST_PRIVACY_ALLOW, AST_PRIVACY_DENY, AST_PRIVACY_TORTURE, AST_PRIVACY_KILL, AST_PRIVACY_ALLOW};
  1690. int i = res2 - '1';
  1691. ast_verb(3, "--Set privacy database entry %s/%s to %s\n",
  1692. opt_args[OPT_ARG_PRIVACY], pa->privcid, _val[i]);
  1693. ast_privacy_set(opt_args[OPT_ARG_PRIVACY], pa->privcid, _flag[i]);
  1694. }
  1695. switch (res2) {
  1696. case '1':
  1697. break;
  1698. case '2':
  1699. ast_copy_string(pa->status, "NOANSWER", sizeof(pa->status));
  1700. break;
  1701. case '3':
  1702. ast_copy_string(pa->status, "TORTURE", sizeof(pa->status));
  1703. break;
  1704. case '4':
  1705. ast_copy_string(pa->status, "DONTCALL", sizeof(pa->status));
  1706. break;
  1707. case '5':
  1708. /* XXX should we set status to DENY ? */
  1709. if (ast_test_flag64(opts, OPT_PRIVACY))
  1710. break;
  1711. /* if not privacy, then 5 is the same as "default" case */
  1712. default: /* bad input or -1 if failure to start autoservice */
  1713. /* well, if the user messes up, ... he had his chance... What Is The Best Thing To Do? */
  1714. /* well, there seems basically two choices. Just patch the caller thru immediately,
  1715. or,... put 'em thru to voicemail. */
  1716. /* since the callee may have hung up, let's do the voicemail thing, no database decision */
  1717. ast_log(LOG_NOTICE, "privacy: no valid response from the callee. Sending the caller to voicemail, the callee isn't responding\n");
  1718. /* XXX should we set status to DENY ? */
  1719. /* XXX what about the privacy flags ? */
  1720. break;
  1721. }
  1722. if (res2 == '1') { /* the only case where we actually connect */
  1723. /* if the intro is NOCALLERID, then there's no reason to leave it on disk, it'll
  1724. just clog things up, and it's not useful information, not being tied to a CID */
  1725. if (strncmp(pa->privcid, "NOCALLERID", 10) == 0 || ast_test_flag64(opts, OPT_SCREEN_NOINTRO)) {
  1726. ast_filedelete(pa->privintro, NULL);
  1727. if (ast_fileexists(pa->privintro, NULL, NULL) > 0)
  1728. ast_log(LOG_NOTICE, "privacy: ast_filedelete didn't do its job on %s\n", pa->privintro);
  1729. else
  1730. ast_verb(3, "Successfully deleted %s intro file\n", pa->privintro);
  1731. }
  1732. return 0; /* the good exit path */
  1733. } else {
  1734. ast_hangup(peer); /* hang up on the callee -- he didn't want to talk anyway! */
  1735. return -1;
  1736. }
  1737. }
  1738. /*! \brief returns 1 if successful, 0 or <0 if the caller should 'goto out' */
  1739. static int setup_privacy_args(struct privacy_args *pa,
  1740. struct ast_flags64 *opts, char *opt_args[], struct ast_channel *chan)
  1741. {
  1742. char callerid[60];
  1743. int res;
  1744. char *l;
  1745. int silencethreshold;
  1746. if (chan->caller.id.number.valid
  1747. && !ast_strlen_zero(chan->caller.id.number.str)) {
  1748. l = ast_strdupa(chan->caller.id.number.str);
  1749. ast_shrink_phone_number(l);
  1750. if (ast_test_flag64(opts, OPT_PRIVACY) ) {
  1751. ast_verb(3, "Privacy DB is '%s', clid is '%s'\n", opt_args[OPT_ARG_PRIVACY], l);
  1752. pa->privdb_val = ast_privacy_check(opt_args[OPT_ARG_PRIVACY], l);
  1753. } else {
  1754. ast_verb(3, "Privacy Screening, clid is '%s'\n", l);
  1755. pa->privdb_val = AST_PRIVACY_UNKNOWN;
  1756. }
  1757. } else {
  1758. char *tnam, *tn2;
  1759. tnam = ast_strdupa(chan->name);
  1760. /* clean the channel name so slashes don't try to end up in disk file name */
  1761. for (tn2 = tnam; *tn2; tn2++) {
  1762. if (*tn2 == '/') /* any other chars to be afraid of? */
  1763. *tn2 = '=';
  1764. }
  1765. ast_verb(3, "Privacy-- callerid is empty\n");
  1766. snprintf(callerid, sizeof(callerid), "NOCALLERID_%s%s", chan->exten, tnam);
  1767. l = callerid;
  1768. pa->privdb_val = AST_PRIVACY_UNKNOWN;
  1769. }
  1770. ast_copy_string(pa->privcid, l, sizeof(pa->privcid));
  1771. if (strncmp(pa->privcid, "NOCALLERID", 10) != 0 && ast_test_flag64(opts, OPT_SCREEN_NOCALLERID)) {
  1772. /* if callerid is set and OPT_SCREEN_NOCALLERID is set also */
  1773. ast_verb(3, "CallerID set (%s); N option set; Screening should be off\n", pa->privcid);
  1774. pa->privdb_val = AST_PRIVACY_ALLOW;
  1775. } else if (ast_test_flag64(opts, OPT_SCREEN_NOCALLERID) && strncmp(pa->privcid, "NOCALLERID", 10) == 0) {
  1776. ast_verb(3, "CallerID blank; N option set; Screening should happen; dbval is %d\n", pa->privdb_val);
  1777. }
  1778. if (pa->privdb_val == AST_PRIVACY_DENY) {
  1779. ast_verb(3, "Privacy DB reports PRIVACY_DENY for this callerid. Dial reports unavailable\n");
  1780. ast_copy_string(pa->status, "NOANSWER", sizeof(pa->status));
  1781. return 0;
  1782. } else if (pa->privdb_val == AST_PRIVACY_KILL) {
  1783. ast_copy_string(pa->status, "DONTCALL", sizeof(pa->status));
  1784. return 0; /* Is this right? */
  1785. } else if (pa->privdb_val == AST_PRIVACY_TORTURE) {
  1786. ast_copy_string(pa->status, "TORTURE", sizeof(pa->status));
  1787. return 0; /* is this right??? */
  1788. } else if (pa->privdb_val == AST_PRIVACY_UNKNOWN) {
  1789. /* Get the user's intro, store it in priv-callerintros/$CID,
  1790. unless it is already there-- this should be done before the
  1791. call is actually dialed */
  1792. /* make sure the priv-callerintros dir actually exists */
  1793. snprintf(pa->privintro, sizeof(pa->privintro), "%s/sounds/priv-callerintros", ast_config_AST_DATA_DIR);
  1794. if ((res = ast_mkdir(pa->privintro, 0755))) {
  1795. ast_log(LOG_WARNING, "privacy: can't create directory priv-callerintros: %s\n", strerror(res));
  1796. return -1;
  1797. }
  1798. snprintf(pa->privintro, sizeof(pa->privintro), "priv-callerintros/%s", pa->privcid);
  1799. if (ast_fileexists(pa->privintro, NULL, NULL ) > 0 && strncmp(pa->privcid, "NOCALLERID", 10) != 0) {
  1800. /* the DELUX version of this code would allow this caller the
  1801. option to hear and retape their previously recorded intro.
  1802. */
  1803. } else {
  1804. int duration; /* for feedback from play_and_wait */
  1805. /* the file doesn't exist yet. Let the caller submit his
  1806. vocal intro for posterity */
  1807. /* priv-recordintro script:
  1808. "At the tone, please say your name:"
  1809. */
  1810. silencethreshold = ast_dsp_get_threshold_from_settings(THRESHOLD_SILENCE);
  1811. ast_answer(chan);
  1812. res = ast_play_and_record(chan, "priv-recordintro", pa->privintro, 4, "sln", &duration, NULL, silencethreshold, 2000, 0); /* NOTE: I've reduced the total time to 4 sec */
  1813. /* don't think we'll need a lock removed, we took care of
  1814. conflicts by naming the pa.privintro file */
  1815. if (res == -1) {
  1816. /* Delete the file regardless since they hung up during recording */
  1817. ast_filedelete(pa->privintro, NULL);
  1818. if (ast_fileexists(pa->privintro, NULL, NULL) > 0)
  1819. ast_log(LOG_NOTICE, "privacy: ast_filedelete didn't do its job on %s\n", pa->privintro);
  1820. else
  1821. ast_verb(3, "Successfully deleted %s intro file\n", pa->privintro);
  1822. return -1;
  1823. }
  1824. if (!ast_streamfile(chan, "vm-dialout", chan->language) )
  1825. ast_waitstream(chan, "");
  1826. }
  1827. }
  1828. return 1; /* success */
  1829. }
  1830. static void end_bridge_callback(void *data)
  1831. {
  1832. char buf[80];
  1833. time_t end;
  1834. struct ast_channel *chan = data;
  1835. if (!chan->cdr) {
  1836. return;
  1837. }
  1838. time(&end);
  1839. ast_channel_lock(chan);
  1840. if (chan->cdr->answer.tv_sec) {
  1841. snprintf(buf, sizeof(buf), "%ld", (long) end - chan->cdr->answer.tv_sec);
  1842. pbx_builtin_setvar_helper(chan, "ANSWEREDTIME", buf);
  1843. }
  1844. if (chan->cdr->start.tv_sec) {
  1845. snprintf(buf, sizeof(buf), "%ld", (long) end - chan->cdr->start.tv_sec);
  1846. pbx_builtin_setvar_helper(chan, "DIALEDTIME", buf);
  1847. }
  1848. ast_channel_unlock(chan);
  1849. }
  1850. static void end_bridge_callback_data_fixup(struct ast_bridge_config *bconfig, struct ast_channel *originator, struct ast_channel *terminator) {
  1851. bconfig->end_bridge_callback_data = originator;
  1852. }
  1853. static int dial_handle_playtones(struct ast_channel *chan, const char *data)
  1854. {
  1855. struct ast_tone_zone_sound *ts = NULL;
  1856. int res;
  1857. const char *str = data;
  1858. if (ast_strlen_zero(str)) {
  1859. ast_debug(1,"Nothing to play\n");
  1860. return -1;
  1861. }
  1862. ts = ast_get_indication_tone(chan->zone, str);
  1863. if (ts && ts->data[0]) {
  1864. res = ast_playtones_start(chan, 0, ts->data, 0);
  1865. } else {
  1866. res = -1;
  1867. }
  1868. if (ts) {
  1869. ts = ast_tone_zone_sound_unref(ts);
  1870. }
  1871. if (res) {
  1872. ast_log(LOG_WARNING, "Unable to start playtone \'%s\'\n", str);
  1873. }
  1874. return res;
  1875. }
  1876. static int dial_exec_full(struct ast_channel *chan, const char *data, struct ast_flags64 *peerflags, int *continue_exec)
  1877. {
  1878. int res = -1; /* default: error */
  1879. char *rest, *cur; /* scan the list of destinations */
  1880. struct chanlist *outgoing = NULL; /* list of destinations */
  1881. struct ast_channel *peer;
  1882. int to; /* timeout */
  1883. struct cause_args num = { chan, 0, 0, 0 };
  1884. int cause;
  1885. char numsubst[256];
  1886. struct ast_bridge_config config = { { 0, } };
  1887. struct timeval calldurationlimit = { 0, };
  1888. char *dtmfcalled = NULL, *dtmfcalling = NULL, *dtmf_progress=NULL;
  1889. struct privacy_args pa = {
  1890. .sentringing = 0,
  1891. .privdb_val = 0,
  1892. .status = "INVALIDARGS",
  1893. };
  1894. int sentringing = 0, moh = 0;
  1895. const char *outbound_group = NULL;
  1896. int result = 0;
  1897. char *parse;
  1898. int opermode = 0;
  1899. int delprivintro = 0;
  1900. AST_DECLARE_APP_ARGS(args,
  1901. AST_APP_ARG(peers);
  1902. AST_APP_ARG(timeout);
  1903. AST_APP_ARG(options);
  1904. AST_APP_ARG(url);
  1905. );
  1906. struct ast_flags64 opts = { 0, };
  1907. char *opt_args[OPT_ARG_ARRAY_SIZE];
  1908. struct ast_datastore *datastore = NULL;
  1909. int fulldial = 0, num_dialed = 0;
  1910. int ignore_cc = 0;
  1911. char device_name[AST_CHANNEL_NAME];
  1912. char forced_clid_name[AST_MAX_EXTENSION];
  1913. char stored_clid_name[AST_MAX_EXTENSION];
  1914. int force_forwards_only; /*!< TRUE if force CallerID on call forward only. Legacy behaviour.*/
  1915. /*!
  1916. * \brief Forced CallerID party information to send.
  1917. * \note This will not have any malloced strings so do not free it.
  1918. */
  1919. struct ast_party_id forced_clid;
  1920. /*!
  1921. * \brief Stored CallerID information if needed.
  1922. *
  1923. * \note If OPT_ORIGINAL_CLID set then this is the o option
  1924. * CallerID. Otherwise it is the dialplan extension and hint
  1925. * name.
  1926. *
  1927. * \note This will not have any malloced strings so do not free it.
  1928. */
  1929. struct ast_party_id stored_clid;
  1930. /*!
  1931. * \brief CallerID party information to store.
  1932. * \note This will not have any malloced strings so do not free it.
  1933. */
  1934. struct ast_party_caller caller;
  1935. /* Reset all DIAL variables back to blank, to prevent confusion (in case we don't reset all of them). */
  1936. pbx_builtin_setvar_helper(chan, "DIALSTATUS", "");
  1937. pbx_builtin_setvar_helper(chan, "DIALEDPEERNUMBER", "");
  1938. pbx_builtin_setvar_helper(chan, "DIALEDPEERNAME", "");
  1939. pbx_builtin_setvar_helper(chan, "ANSWEREDTIME", "");
  1940. pbx_builtin_setvar_helper(chan, "DIALEDTIME", "");
  1941. if (ast_strlen_zero(data)) {
  1942. ast_log(LOG_WARNING, "Dial requires an argument (technology/number)\n");
  1943. pbx_builtin_setvar_helper(chan, "DIALSTATUS", pa.status);
  1944. return -1;
  1945. }
  1946. parse = ast_strdupa(data);
  1947. AST_STANDARD_APP_ARGS(args, parse);
  1948. if (!ast_strlen_zero(args.options) &&
  1949. ast_app_parse_options64(dial_exec_options, &opts, opt_args, args.options)) {
  1950. pbx_builtin_setvar_helper(chan, "DIALSTATUS", pa.status);
  1951. goto done;
  1952. }
  1953. if (ast_strlen_zero(args.peers)) {
  1954. ast_log(LOG_WARNING, "Dial requires an argument (technology/number)\n");
  1955. pbx_builtin_setvar_helper(chan, "DIALSTATUS", pa.status);
  1956. goto done;
  1957. }
  1958. if (ast_cc_call_init(chan, &ignore_cc)) {
  1959. goto done;
  1960. }
  1961. if (ast_test_flag64(&opts, OPT_SCREEN_NOINTRO) && !ast_strlen_zero(opt_args[OPT_ARG_SCREEN_NOINTRO])) {
  1962. delprivintro = atoi(opt_args[OPT_ARG_SCREEN_NOINTRO]);
  1963. if (delprivintro < 0 || delprivintro > 1) {
  1964. ast_log(LOG_WARNING, "Unknown argument %d specified to n option, ignoring\n", delprivintro);
  1965. delprivintro = 0;
  1966. }
  1967. }
  1968. if (!ast_test_flag64(&opts, OPT_RINGBACK)) {
  1969. opt_args[OPT_ARG_RINGBACK] = NULL;
  1970. }
  1971. if (ast_test_flag64(&opts, OPT_OPERMODE)) {
  1972. opermode = ast_strlen_zero(opt_args[OPT_ARG_OPERMODE]) ? 1 : atoi(opt_args[OPT_ARG_OPERMODE]);
  1973. ast_verb(3, "Setting operator services mode to %d.\n", opermode);
  1974. }
  1975. if (ast_test_flag64(&opts, OPT_DURATION_STOP) && !ast_strlen_zero(opt_args[OPT_ARG_DURATION_STOP])) {
  1976. calldurationlimit.tv_sec = atoi(opt_args[OPT_ARG_DURATION_STOP]);
  1977. if (!calldurationlimit.tv_sec) {
  1978. ast_log(LOG_WARNING, "Dial does not accept S(%s), hanging up.\n", opt_args[OPT_ARG_DURATION_STOP]);
  1979. pbx_builtin_setvar_helper(chan, "DIALSTATUS", pa.status);
  1980. goto done;
  1981. }
  1982. ast_verb(3, "Setting call duration limit to %.3lf seconds.\n", calldurationlimit.tv_sec + calldurationlimit.tv_usec / 1000000.0);
  1983. }
  1984. if (ast_test_flag64(&opts, OPT_SENDDTMF) && !ast_strlen_zero(opt_args[OPT_ARG_SENDDTMF])) {
  1985. dtmf_progress = opt_args[OPT_ARG_SENDDTMF];
  1986. dtmfcalled = strsep(&dtmf_progress, ":");
  1987. dtmfcalling = strsep(&dtmf_progress, ":");
  1988. }
  1989. if (ast_test_flag64(&opts, OPT_DURATION_LIMIT) && !ast_strlen_zero(opt_args[OPT_ARG_DURATION_LIMIT])) {
  1990. if (ast_bridge_timelimit(chan, &config, opt_args[OPT_ARG_DURATION_LIMIT], &calldurationlimit))
  1991. goto done;
  1992. }
  1993. /* Setup the forced CallerID information to send if used. */
  1994. ast_party_id_init(&forced_clid);
  1995. force_forwards_only = 0;
  1996. if (ast_test_flag64(&opts, OPT_FORCECLID)) {
  1997. if (ast_strlen_zero(opt_args[OPT_ARG_FORCECLID])) {
  1998. ast_channel_lock(chan);
  1999. forced_clid.number.str = ast_strdupa(S_OR(chan->macroexten, chan->exten));
  2000. ast_channel_unlock(chan);
  2001. forced_clid_name[0] = '\0';
  2002. forced_clid.name.str = (char *) get_cid_name(forced_clid_name,
  2003. sizeof(forced_clid_name), chan);
  2004. force_forwards_only = 1;
  2005. } else {
  2006. /* Note: The opt_args[OPT_ARG_FORCECLID] string value is altered here. */
  2007. ast_callerid_parse(opt_args[OPT_ARG_FORCECLID], &forced_clid.name.str,
  2008. &forced_clid.number.str);
  2009. }
  2010. if (!ast_strlen_zero(forced_clid.name.str)) {
  2011. forced_clid.name.valid = 1;
  2012. }
  2013. if (!ast_strlen_zero(forced_clid.number.str)) {
  2014. forced_clid.number.valid = 1;
  2015. }
  2016. }
  2017. if (ast_test_flag64(&opts, OPT_FORCE_CID_TAG)
  2018. && !ast_strlen_zero(opt_args[OPT_ARG_FORCE_CID_TAG])) {
  2019. forced_clid.tag = opt_args[OPT_ARG_FORCE_CID_TAG];
  2020. }
  2021. forced_clid.number.presentation = AST_PRES_ALLOWED_USER_NUMBER_PASSED_SCREEN;
  2022. if (ast_test_flag64(&opts, OPT_FORCE_CID_PRES)
  2023. && !ast_strlen_zero(opt_args[OPT_ARG_FORCE_CID_PRES])) {
  2024. int pres;
  2025. pres = ast_parse_caller_presentation(opt_args[OPT_ARG_FORCE_CID_PRES]);
  2026. if (0 <= pres) {
  2027. forced_clid.number.presentation = pres;
  2028. }
  2029. }
  2030. /* Setup the stored CallerID information if needed. */
  2031. ast_party_id_init(&stored_clid);
  2032. if (ast_test_flag64(&opts, OPT_ORIGINAL_CLID)) {
  2033. if (ast_strlen_zero(opt_args[OPT_ARG_ORIGINAL_CLID])) {
  2034. ast_channel_lock(chan);
  2035. ast_party_id_set_init(&stored_clid, &chan->caller.id);
  2036. if (!ast_strlen_zero(chan->caller.id.name.str)) {
  2037. stored_clid.name.str = ast_strdupa(chan->caller.id.name.str);
  2038. }
  2039. if (!ast_strlen_zero(chan->caller.id.number.str)) {
  2040. stored_clid.number.str = ast_strdupa(chan->caller.id.number.str);
  2041. }
  2042. if (!ast_strlen_zero(chan->caller.id.subaddress.str)) {
  2043. stored_clid.subaddress.str = ast_strdupa(chan->caller.id.subaddress.str);
  2044. }
  2045. if (!ast_strlen_zero(chan->caller.id.tag)) {
  2046. stored_clid.tag = ast_strdupa(chan->caller.id.tag);
  2047. }
  2048. ast_channel_unlock(chan);
  2049. } else {
  2050. /* Note: The opt_args[OPT_ARG_ORIGINAL_CLID] string value is altered here. */
  2051. ast_callerid_parse(opt_args[OPT_ARG_ORIGINAL_CLID], &stored_clid.name.str,
  2052. &stored_clid.number.str);
  2053. if (!ast_strlen_zero(stored_clid.name.str)) {
  2054. stored_clid.name.valid = 1;
  2055. }
  2056. if (!ast_strlen_zero(stored_clid.number.str)) {
  2057. stored_clid.number.valid = 1;
  2058. }
  2059. }
  2060. } else {
  2061. /*
  2062. * In case the new channel has no preset CallerID number by the
  2063. * channel driver, setup the dialplan extension and hint name.
  2064. */
  2065. stored_clid_name[0] = '\0';
  2066. stored_clid.name.str = (char *) get_cid_name(stored_clid_name,
  2067. sizeof(stored_clid_name), chan);
  2068. if (ast_strlen_zero(stored_clid.name.str)) {
  2069. stored_clid.name.str = NULL;
  2070. } else {
  2071. stored_clid.name.valid = 1;
  2072. }
  2073. ast_channel_lock(chan);
  2074. stored_clid.number.str = ast_strdupa(S_OR(chan->macroexten, chan->exten));
  2075. stored_clid.number.valid = 1;
  2076. ast_channel_unlock(chan);
  2077. }
  2078. if (ast_test_flag64(&opts, OPT_RESETCDR) && chan->cdr)
  2079. ast_cdr_reset(chan->cdr, NULL);
  2080. if (ast_test_flag64(&opts, OPT_PRIVACY) && ast_strlen_zero(opt_args[OPT_ARG_PRIVACY]))
  2081. opt_args[OPT_ARG_PRIVACY] = ast_strdupa(chan->exten);
  2082. if (ast_test_flag64(&opts, OPT_PRIVACY) || ast_test_flag64(&opts, OPT_SCREENING)) {
  2083. res = setup_privacy_args(&pa, &opts, opt_args, chan);
  2084. if (res <= 0)
  2085. goto out;
  2086. res = -1; /* reset default */
  2087. }
  2088. if (continue_exec)
  2089. *continue_exec = 0;
  2090. /* If a channel group has been specified, get it for use when we create peer channels */
  2091. ast_channel_lock(chan);
  2092. if ((outbound_group = pbx_builtin_getvar_helper(chan, "OUTBOUND_GROUP_ONCE"))) {
  2093. outbound_group = ast_strdupa(outbound_group);
  2094. pbx_builtin_setvar_helper(chan, "OUTBOUND_GROUP_ONCE", NULL);
  2095. } else if ((outbound_group = pbx_builtin_getvar_helper(chan, "OUTBOUND_GROUP"))) {
  2096. outbound_group = ast_strdupa(outbound_group);
  2097. }
  2098. ast_channel_unlock(chan);
  2099. /* Set per dial instance flags. These flags are also passed back to RetryDial. */
  2100. ast_copy_flags64(peerflags, &opts, OPT_DTMF_EXIT | OPT_GO_ON | OPT_ORIGINAL_CLID
  2101. | OPT_CALLER_HANGUP | OPT_IGNORE_FORWARDING | OPT_CANCEL_TIMEOUT
  2102. | OPT_ANNOUNCE | OPT_CALLEE_MACRO | OPT_CALLEE_GOSUB | OPT_FORCECLID);
  2103. /* loop through the list of dial destinations */
  2104. rest = args.peers;
  2105. while ((cur = strsep(&rest, "&")) ) {
  2106. struct chanlist *tmp;
  2107. struct ast_channel *tc; /* channel for this destination */
  2108. /* Get a technology/[device:]number pair */
  2109. char *number = cur;
  2110. char *interface = ast_strdupa(number);
  2111. char *tech = strsep(&number, "/");
  2112. /* find if we already dialed this interface */
  2113. struct ast_dialed_interface *di;
  2114. AST_LIST_HEAD(, ast_dialed_interface) *dialed_interfaces;
  2115. num_dialed++;
  2116. if (ast_strlen_zero(number)) {
  2117. ast_log(LOG_WARNING, "Dial argument takes format (technology/[device:]number1)\n");
  2118. goto out;
  2119. }
  2120. if (!(tmp = ast_calloc(1, sizeof(*tmp))))
  2121. goto out;
  2122. if (opts.flags) {
  2123. /* Set per outgoing call leg options. */
  2124. ast_copy_flags64(tmp, &opts,
  2125. OPT_CANCEL_ELSEWHERE |
  2126. OPT_CALLEE_TRANSFER | OPT_CALLER_TRANSFER |
  2127. OPT_CALLEE_HANGUP | OPT_CALLER_HANGUP |
  2128. OPT_CALLEE_MONITOR | OPT_CALLER_MONITOR |
  2129. OPT_CALLEE_PARK | OPT_CALLER_PARK |
  2130. OPT_CALLEE_MIXMONITOR | OPT_CALLER_MIXMONITOR |
  2131. OPT_RINGBACK | OPT_MUSICBACK | OPT_FORCECLID | OPT_IGNORE_CONNECTEDLINE);
  2132. ast_set2_flag64(tmp, args.url, DIAL_NOFORWARDHTML);
  2133. }
  2134. ast_copy_string(numsubst, number, sizeof(numsubst));
  2135. /* Request the peer */
  2136. ast_channel_lock(chan);
  2137. datastore = ast_channel_datastore_find(chan, &dialed_interface_info, NULL);
  2138. /*
  2139. * Seed the chanlist's connected line information with previously
  2140. * acquired connected line info from the incoming channel. The
  2141. * previously acquired connected line info could have been set
  2142. * through the CONNECTED_LINE dialplan function.
  2143. */
  2144. ast_party_connected_line_copy(&tmp->connected, &chan->connected);
  2145. ast_channel_unlock(chan);
  2146. if (datastore)
  2147. dialed_interfaces = datastore->data;
  2148. else {
  2149. if (!(datastore = ast_datastore_alloc(&dialed_interface_info, NULL))) {
  2150. ast_log(LOG_WARNING, "Unable to create channel datastore for dialed interfaces. Aborting!\n");
  2151. chanlist_free(tmp);
  2152. goto out;
  2153. }
  2154. datastore->inheritance = DATASTORE_INHERIT_FOREVER;
  2155. if (!(dialed_interfaces = ast_calloc(1, sizeof(*dialed_interfaces)))) {
  2156. ast_datastore_free(datastore);
  2157. chanlist_free(tmp);
  2158. goto out;
  2159. }
  2160. datastore->data = dialed_interfaces;
  2161. AST_LIST_HEAD_INIT(dialed_interfaces);
  2162. ast_channel_lock(chan);
  2163. ast_channel_datastore_add(chan, datastore);
  2164. ast_channel_unlock(chan);
  2165. }
  2166. AST_LIST_LOCK(dialed_interfaces);
  2167. AST_LIST_TRAVERSE(dialed_interfaces, di, list) {
  2168. if (!strcasecmp(di->interface, interface)) {
  2169. ast_log(LOG_WARNING, "Skipping dialing interface '%s' again since it has already been dialed\n",
  2170. di->interface);
  2171. break;
  2172. }
  2173. }
  2174. AST_LIST_UNLOCK(dialed_interfaces);
  2175. if (di) {
  2176. fulldial++;
  2177. chanlist_free(tmp);
  2178. continue;
  2179. }
  2180. /* It is always ok to dial a Local interface. We only keep track of
  2181. * which "real" interfaces have been dialed. The Local channel will
  2182. * inherit this list so that if it ends up dialing a real interface,
  2183. * it won't call one that has already been called. */
  2184. if (strcasecmp(tech, "Local")) {
  2185. if (!(di = ast_calloc(1, sizeof(*di) + strlen(interface)))) {
  2186. AST_LIST_UNLOCK(dialed_interfaces);
  2187. chanlist_free(tmp);
  2188. goto out;
  2189. }
  2190. strcpy(di->interface, interface);
  2191. AST_LIST_LOCK(dialed_interfaces);
  2192. AST_LIST_INSERT_TAIL(dialed_interfaces, di, list);
  2193. AST_LIST_UNLOCK(dialed_interfaces);
  2194. }
  2195. tc = ast_request(tech, chan->nativeformats, chan, numsubst, &cause);
  2196. if (!tc) {
  2197. /* If we can't, just go on to the next call */
  2198. ast_log(LOG_WARNING, "Unable to create channel of type '%s' (cause %d - %s)\n",
  2199. tech, cause, ast_cause2str(cause));
  2200. handle_cause(cause, &num);
  2201. if (!rest) /* we are on the last destination */
  2202. chan->hangupcause = cause;
  2203. chanlist_free(tmp);
  2204. if (!ignore_cc && (cause == AST_CAUSE_BUSY || cause == AST_CAUSE_CONGESTION)) {
  2205. if (!ast_cc_callback(chan, tech, numsubst, ast_cc_busy_interface)) {
  2206. ast_cc_extension_monitor_add_dialstring(chan, interface, "");
  2207. }
  2208. }
  2209. continue;
  2210. }
  2211. ast_channel_get_device_name(tc, device_name, sizeof(device_name));
  2212. if (!ignore_cc) {
  2213. ast_cc_extension_monitor_add_dialstring(chan, interface, device_name);
  2214. }
  2215. pbx_builtin_setvar_helper(tc, "DIALEDPEERNUMBER", numsubst);
  2216. ast_channel_lock(tc);
  2217. while (ast_channel_trylock(chan)) {
  2218. CHANNEL_DEADLOCK_AVOIDANCE(tc);
  2219. }
  2220. /* Setup outgoing SDP to match incoming one */
  2221. if (!outgoing && !rest && CAN_EARLY_BRIDGE(peerflags, chan, tc)) {
  2222. ast_rtp_instance_early_bridge_make_compatible(tc, chan);
  2223. }
  2224. /* Inherit specially named variables from parent channel */
  2225. ast_channel_inherit_variables(chan, tc);
  2226. ast_channel_datastore_inherit(chan, tc);
  2227. tc->appl = "AppDial";
  2228. tc->data = "(Outgoing Line)";
  2229. memset(&tc->whentohangup, 0, sizeof(tc->whentohangup));
  2230. /* Determine CallerID to store in outgoing channel. */
  2231. ast_party_caller_set_init(&caller, &tc->caller);
  2232. if (ast_test_flag64(peerflags, OPT_ORIGINAL_CLID)) {
  2233. caller.id = stored_clid;
  2234. ast_channel_set_caller_event(tc, &caller, NULL);
  2235. ast_set_flag64(tmp, DIAL_CALLERID_ABSENT);
  2236. } else if (ast_strlen_zero(S_COR(tc->caller.id.number.valid,
  2237. tc->caller.id.number.str, NULL))) {
  2238. /*
  2239. * The new channel has no preset CallerID number by the channel
  2240. * driver. Use the dialplan extension and hint name.
  2241. */
  2242. caller.id = stored_clid;
  2243. if (!caller.id.name.valid
  2244. && !ast_strlen_zero(S_COR(chan->connected.id.name.valid,
  2245. chan->connected.id.name.str, NULL))) {
  2246. /*
  2247. * No hint name available. We have a connected name supplied by
  2248. * the dialplan we can use instead.
  2249. */
  2250. caller.id.name.valid = 1;
  2251. caller.id.name = chan->connected.id.name;
  2252. }
  2253. ast_channel_set_caller_event(tc, &caller, NULL);
  2254. ast_set_flag64(tmp, DIAL_CALLERID_ABSENT);
  2255. } else if (ast_strlen_zero(S_COR(tc->caller.id.name.valid, tc->caller.id.name.str,
  2256. NULL))) {
  2257. /* The new channel has no preset CallerID name by the channel driver. */
  2258. if (!ast_strlen_zero(S_COR(chan->connected.id.name.valid,
  2259. chan->connected.id.name.str, NULL))) {
  2260. /*
  2261. * We have a connected name supplied by the dialplan we can
  2262. * use instead.
  2263. */
  2264. caller.id.name.valid = 1;
  2265. caller.id.name = chan->connected.id.name;
  2266. ast_channel_set_caller_event(tc, &caller, NULL);
  2267. }
  2268. }
  2269. /* Determine CallerID for outgoing channel to send. */
  2270. if (ast_test_flag64(peerflags, OPT_FORCECLID) && !force_forwards_only) {
  2271. struct ast_party_connected_line connected;
  2272. ast_party_connected_line_set_init(&connected, &tc->connected);
  2273. connected.id = forced_clid;
  2274. ast_channel_set_connected_line(tc, &connected, NULL);
  2275. } else {
  2276. ast_connected_line_copy_from_caller(&tc->connected, &chan->caller);
  2277. }
  2278. ast_party_redirecting_copy(&tc->redirecting, &chan->redirecting);
  2279. tc->dialed.transit_network_select = chan->dialed.transit_network_select;
  2280. if (!ast_strlen_zero(chan->accountcode)) {
  2281. ast_string_field_set(tc, accountcode, chan->accountcode);
  2282. }
  2283. if (ast_strlen_zero(tc->musicclass))
  2284. ast_string_field_set(tc, musicclass, chan->musicclass);
  2285. /* Pass ADSI CPE and transfer capability */
  2286. tc->adsicpe = chan->adsicpe;
  2287. tc->transfercapability = chan->transfercapability;
  2288. /* If we have an outbound group, set this peer channel to it */
  2289. if (outbound_group)
  2290. ast_app_group_set_channel(tc, outbound_group);
  2291. /* If the calling channel has the ANSWERED_ELSEWHERE flag set, inherit it. This is to support local channels */
  2292. if (ast_test_flag(chan, AST_FLAG_ANSWERED_ELSEWHERE))
  2293. ast_set_flag(tc, AST_FLAG_ANSWERED_ELSEWHERE);
  2294. /* Check if we're forced by configuration */
  2295. if (ast_test_flag64(&opts, OPT_CANCEL_ELSEWHERE))
  2296. ast_set_flag(tc, AST_FLAG_ANSWERED_ELSEWHERE);
  2297. /* Inherit context and extension */
  2298. ast_string_field_set(tc, dialcontext, ast_strlen_zero(chan->macrocontext) ? chan->context : chan->macrocontext);
  2299. if (!ast_strlen_zero(chan->macroexten))
  2300. ast_copy_string(tc->exten, chan->macroexten, sizeof(tc->exten));
  2301. else
  2302. ast_copy_string(tc->exten, chan->exten, sizeof(tc->exten));
  2303. ast_channel_unlock(tc);
  2304. ast_channel_unlock(chan);
  2305. res = ast_call(tc, numsubst, 0); /* Place the call, but don't wait on the answer */
  2306. ast_channel_lock(chan);
  2307. /* Save the info in cdr's that we called them */
  2308. if (chan->cdr)
  2309. ast_cdr_setdestchan(chan->cdr, tc->name);
  2310. /* check the results of ast_call */
  2311. if (res) {
  2312. /* Again, keep going even if there's an error */
  2313. ast_debug(1, "ast call on peer returned %d\n", res);
  2314. ast_verb(3, "Couldn't call %s/%s\n", tech, numsubst);
  2315. if (tc->hangupcause) {
  2316. chan->hangupcause = tc->hangupcause;
  2317. }
  2318. ast_channel_unlock(chan);
  2319. ast_cc_call_failed(chan, tc, interface);
  2320. ast_hangup(tc);
  2321. tc = NULL;
  2322. chanlist_free(tmp);
  2323. continue;
  2324. } else {
  2325. senddialevent(chan, tc, numsubst);
  2326. ast_verb(3, "Called %s/%s\n", tech, numsubst);
  2327. ast_channel_unlock(chan);
  2328. }
  2329. /* Put them in the list of outgoing thingies... We're ready now.
  2330. XXX If we're forcibly removed, these outgoing calls won't get
  2331. hung up XXX */
  2332. ast_set_flag64(tmp, DIAL_STILLGOING);
  2333. tmp->chan = tc;
  2334. tmp->next = outgoing;
  2335. outgoing = tmp;
  2336. /* If this line is up, don't try anybody else */
  2337. if (outgoing->chan->_state == AST_STATE_UP)
  2338. break;
  2339. }
  2340. if (ast_strlen_zero(args.timeout)) {
  2341. to = -1;
  2342. } else {
  2343. to = atoi(args.timeout);
  2344. if (to > 0)
  2345. to *= 1000;
  2346. else {
  2347. ast_log(LOG_WARNING, "Invalid timeout specified: '%s'. Setting timeout to infinite\n", args.timeout);
  2348. to = -1;
  2349. }
  2350. }
  2351. if (!outgoing) {
  2352. strcpy(pa.status, "CHANUNAVAIL");
  2353. if (fulldial == num_dialed) {
  2354. res = -1;
  2355. goto out;
  2356. }
  2357. } else {
  2358. /* Our status will at least be NOANSWER */
  2359. strcpy(pa.status, "NOANSWER");
  2360. if (ast_test_flag64(outgoing, OPT_MUSICBACK)) {
  2361. moh = 1;
  2362. if (!ast_strlen_zero(opt_args[OPT_ARG_MUSICBACK])) {
  2363. char *original_moh = ast_strdupa(chan->musicclass);
  2364. ast_string_field_set(chan, musicclass, opt_args[OPT_ARG_MUSICBACK]);
  2365. ast_moh_start(chan, opt_args[OPT_ARG_MUSICBACK], NULL);
  2366. ast_string_field_set(chan, musicclass, original_moh);
  2367. } else {
  2368. ast_moh_start(chan, NULL, NULL);
  2369. }
  2370. ast_indicate(chan, AST_CONTROL_PROGRESS);
  2371. } else if (ast_test_flag64(outgoing, OPT_RINGBACK)) {
  2372. if (!ast_strlen_zero(opt_args[OPT_ARG_RINGBACK])) {
  2373. if (dial_handle_playtones(chan, opt_args[OPT_ARG_RINGBACK])){
  2374. ast_indicate(chan, AST_CONTROL_RINGING);
  2375. sentringing++;
  2376. } else {
  2377. ast_indicate(chan, AST_CONTROL_PROGRESS);
  2378. }
  2379. } else {
  2380. ast_indicate(chan, AST_CONTROL_RINGING);
  2381. sentringing++;
  2382. }
  2383. }
  2384. }
  2385. peer = wait_for_answer(chan, outgoing, &to, peerflags, opt_args, &pa, &num, &result,
  2386. dtmf_progress, ignore_cc, &forced_clid, &stored_clid);
  2387. /* The ast_channel_datastore_remove() function could fail here if the
  2388. * datastore was moved to another channel during a masquerade. If this is
  2389. * the case, don't free the datastore here because later, when the channel
  2390. * to which the datastore was moved hangs up, it will attempt to free this
  2391. * datastore again, causing a crash
  2392. */
  2393. ast_channel_lock(chan);
  2394. datastore = ast_channel_datastore_find(chan, &dialed_interface_info, NULL); /* make sure we weren't cleaned up already */
  2395. if (datastore && !ast_channel_datastore_remove(chan, datastore)) {
  2396. ast_datastore_free(datastore);
  2397. }
  2398. ast_channel_unlock(chan);
  2399. if (!peer) {
  2400. if (result) {
  2401. res = result;
  2402. } else if (to) { /* Musta gotten hung up */
  2403. res = -1;
  2404. } else { /* Nobody answered, next please? */
  2405. res = 0;
  2406. }
  2407. } else {
  2408. const char *number;
  2409. if (ast_test_flag64(&opts, OPT_CALLER_ANSWER))
  2410. ast_answer(chan);
  2411. strcpy(pa.status, "ANSWER");
  2412. pbx_builtin_setvar_helper(chan, "DIALSTATUS", pa.status);
  2413. /* Ah ha! Someone answered within the desired timeframe. Of course after this
  2414. we will always return with -1 so that it is hung up properly after the
  2415. conversation. */
  2416. hanguptree(outgoing, peer, 1);
  2417. outgoing = NULL;
  2418. /* If appropriate, log that we have a destination channel and set the answer time */
  2419. if (chan->cdr) {
  2420. ast_cdr_setdestchan(chan->cdr, peer->name);
  2421. ast_cdr_setanswer(chan->cdr, peer->cdr->answer);
  2422. }
  2423. if (peer->name)
  2424. pbx_builtin_setvar_helper(chan, "DIALEDPEERNAME", peer->name);
  2425. ast_channel_lock(peer);
  2426. number = pbx_builtin_getvar_helper(peer, "DIALEDPEERNUMBER");
  2427. if (!number)
  2428. number = numsubst;
  2429. pbx_builtin_setvar_helper(chan, "DIALEDPEERNUMBER", number);
  2430. ast_channel_unlock(peer);
  2431. if (!ast_strlen_zero(args.url) && ast_channel_supports_html(peer) ) {
  2432. ast_debug(1, "app_dial: sendurl=%s.\n", args.url);
  2433. ast_channel_sendurl( peer, args.url );
  2434. }
  2435. if ( (ast_test_flag64(&opts, OPT_PRIVACY) || ast_test_flag64(&opts, OPT_SCREENING)) && pa.privdb_val == AST_PRIVACY_UNKNOWN) {
  2436. if (do_privacy(chan, peer, &opts, opt_args, &pa)) {
  2437. res = 0;
  2438. goto out;
  2439. }
  2440. }
  2441. if (!ast_test_flag64(&opts, OPT_ANNOUNCE) || ast_strlen_zero(opt_args[OPT_ARG_ANNOUNCE])) {
  2442. res = 0;
  2443. } else {
  2444. int digit = 0;
  2445. struct ast_channel *chans[2];
  2446. struct ast_channel *active_chan;
  2447. chans[0] = chan;
  2448. chans[1] = peer;
  2449. /* we need to stream the announcment while monitoring the caller for a hangup */
  2450. /* stream the file */
  2451. res = ast_streamfile(peer, opt_args[OPT_ARG_ANNOUNCE], peer->language);
  2452. if (res) {
  2453. res = 0;
  2454. ast_log(LOG_ERROR, "error streaming file '%s' to callee\n", opt_args[OPT_ARG_ANNOUNCE]);
  2455. }
  2456. ast_set_flag(peer, AST_FLAG_END_DTMF_ONLY);
  2457. while (peer->stream) {
  2458. int ms;
  2459. ms = ast_sched_wait(peer->sched);
  2460. if (ms < 0 && !peer->timingfunc) {
  2461. ast_stopstream(peer);
  2462. break;
  2463. }
  2464. if (ms < 0)
  2465. ms = 1000;
  2466. active_chan = ast_waitfor_n(chans, 2, &ms);
  2467. if (active_chan) {
  2468. struct ast_frame *fr = ast_read(active_chan);
  2469. if (!fr) {
  2470. ast_hangup(peer);
  2471. res = -1;
  2472. goto done;
  2473. }
  2474. switch(fr->frametype) {
  2475. case AST_FRAME_DTMF_END:
  2476. digit = fr->subclass.integer;
  2477. if (active_chan == peer && strchr(AST_DIGIT_ANY, res)) {
  2478. ast_stopstream(peer);
  2479. res = ast_senddigit(chan, digit, 0);
  2480. }
  2481. break;
  2482. case AST_FRAME_CONTROL:
  2483. switch (fr->subclass.integer) {
  2484. case AST_CONTROL_HANGUP:
  2485. ast_frfree(fr);
  2486. ast_hangup(peer);
  2487. res = -1;
  2488. goto done;
  2489. default:
  2490. break;
  2491. }
  2492. break;
  2493. default:
  2494. /* Ignore all others */
  2495. break;
  2496. }
  2497. ast_frfree(fr);
  2498. }
  2499. ast_sched_runq(peer->sched);
  2500. }
  2501. ast_clear_flag(peer, AST_FLAG_END_DTMF_ONLY);
  2502. }
  2503. if (chan && peer && ast_test_flag64(&opts, OPT_GOTO) && !ast_strlen_zero(opt_args[OPT_ARG_GOTO])) {
  2504. /* chan and peer are going into the PBX, they both
  2505. * should probably get CDR records. */
  2506. ast_clear_flag(chan->cdr, AST_CDR_FLAG_DIALED);
  2507. ast_clear_flag(peer->cdr, AST_CDR_FLAG_DIALED);
  2508. replace_macro_delimiter(opt_args[OPT_ARG_GOTO]);
  2509. ast_parseable_goto(chan, opt_args[OPT_ARG_GOTO]);
  2510. /* peer goes to the same context and extension as chan, so just copy info from chan*/
  2511. ast_copy_string(peer->context, chan->context, sizeof(peer->context));
  2512. ast_copy_string(peer->exten, chan->exten, sizeof(peer->exten));
  2513. peer->priority = chan->priority + 2;
  2514. if (ast_pbx_start(peer)) {
  2515. ast_hangup(peer);
  2516. }
  2517. hanguptree(outgoing, NULL, ast_test_flag64(&opts, OPT_CANCEL_ELSEWHERE) ? 1 : 0);
  2518. if (continue_exec)
  2519. *continue_exec = 1;
  2520. res = 0;
  2521. goto done;
  2522. }
  2523. if (ast_test_flag64(&opts, OPT_CALLEE_MACRO) && !ast_strlen_zero(opt_args[OPT_ARG_CALLEE_MACRO])) {
  2524. struct ast_app *theapp;
  2525. const char *macro_result;
  2526. res = ast_autoservice_start(chan);
  2527. if (res) {
  2528. ast_log(LOG_ERROR, "Unable to start autoservice on calling channel\n");
  2529. res = -1;
  2530. }
  2531. theapp = pbx_findapp("Macro");
  2532. if (theapp && !res) { /* XXX why check res here ? */
  2533. /* Set peer->exten and peer->context so that MACRO_EXTEN and MACRO_CONTEXT get set */
  2534. ast_copy_string(peer->context, chan->context, sizeof(peer->context));
  2535. ast_copy_string(peer->exten, chan->exten, sizeof(peer->exten));
  2536. replace_macro_delimiter(opt_args[OPT_ARG_CALLEE_MACRO]);
  2537. res = pbx_exec(peer, theapp, opt_args[OPT_ARG_CALLEE_MACRO]);
  2538. ast_debug(1, "Macro exited with status %d\n", res);
  2539. res = 0;
  2540. } else {
  2541. ast_log(LOG_ERROR, "Could not find application Macro\n");
  2542. res = -1;
  2543. }
  2544. if (ast_autoservice_stop(chan) < 0) {
  2545. res = -1;
  2546. }
  2547. ast_channel_lock(peer);
  2548. if (!res && (macro_result = pbx_builtin_getvar_helper(peer, "MACRO_RESULT"))) {
  2549. char *macro_transfer_dest;
  2550. if (!strcasecmp(macro_result, "BUSY")) {
  2551. ast_copy_string(pa.status, macro_result, sizeof(pa.status));
  2552. ast_set_flag64(peerflags, OPT_GO_ON);
  2553. res = -1;
  2554. } else if (!strcasecmp(macro_result, "CONGESTION") || !strcasecmp(macro_result, "CHANUNAVAIL")) {
  2555. ast_copy_string(pa.status, macro_result, sizeof(pa.status));
  2556. ast_set_flag64(peerflags, OPT_GO_ON);
  2557. res = -1;
  2558. } else if (!strcasecmp(macro_result, "CONTINUE")) {
  2559. /* hangup peer and keep chan alive assuming the macro has changed
  2560. the context / exten / priority or perhaps
  2561. the next priority in the current exten is desired.
  2562. */
  2563. ast_set_flag64(peerflags, OPT_GO_ON);
  2564. res = -1;
  2565. } else if (!strcasecmp(macro_result, "ABORT")) {
  2566. /* Hangup both ends unless the caller has the g flag */
  2567. res = -1;
  2568. } else if (!strncasecmp(macro_result, "GOTO:", 5) && (macro_transfer_dest = ast_strdupa(macro_result + 5))) {
  2569. res = -1;
  2570. /* perform a transfer to a new extension */
  2571. if (strchr(macro_transfer_dest, '^')) { /* context^exten^priority*/
  2572. replace_macro_delimiter(macro_transfer_dest);
  2573. }
  2574. if (!ast_parseable_goto(chan, macro_transfer_dest)) {
  2575. ast_set_flag64(peerflags, OPT_GO_ON);
  2576. }
  2577. }
  2578. }
  2579. ast_channel_unlock(peer);
  2580. }
  2581. if (ast_test_flag64(&opts, OPT_CALLEE_GOSUB) && !ast_strlen_zero(opt_args[OPT_ARG_CALLEE_GOSUB])) {
  2582. struct ast_app *theapp;
  2583. const char *gosub_result;
  2584. char *gosub_args, *gosub_argstart;
  2585. int res9 = -1;
  2586. res9 = ast_autoservice_start(chan);
  2587. if (res9) {
  2588. ast_log(LOG_ERROR, "Unable to start autoservice on calling channel\n");
  2589. res9 = -1;
  2590. }
  2591. theapp = pbx_findapp("Gosub");
  2592. if (theapp && !res9) {
  2593. replace_macro_delimiter(opt_args[OPT_ARG_CALLEE_GOSUB]);
  2594. /* Set where we came from */
  2595. ast_copy_string(peer->context, "app_dial_gosub_virtual_context", sizeof(peer->context));
  2596. ast_copy_string(peer->exten, "s", sizeof(peer->exten));
  2597. peer->priority = 0;
  2598. gosub_argstart = strchr(opt_args[OPT_ARG_CALLEE_GOSUB], ',');
  2599. if (gosub_argstart) {
  2600. const char *what_is_s = "s";
  2601. *gosub_argstart = 0;
  2602. if (!ast_exists_extension(peer, opt_args[OPT_ARG_CALLEE_GOSUB], "s", 1, S_COR(peer->caller.id.number.valid, peer->caller.id.number.str, NULL)) &&
  2603. ast_exists_extension(peer, opt_args[OPT_ARG_CALLEE_GOSUB], "~~s~~", 1, S_COR(peer->caller.id.number.valid, peer->caller.id.number.str, NULL))) {
  2604. what_is_s = "~~s~~";
  2605. }
  2606. if (ast_asprintf(&gosub_args, "%s,%s,1(%s)", opt_args[OPT_ARG_CALLEE_GOSUB], what_is_s, gosub_argstart + 1) < 0) {
  2607. gosub_args = NULL;
  2608. }
  2609. *gosub_argstart = ',';
  2610. } else {
  2611. const char *what_is_s = "s";
  2612. if (!ast_exists_extension(peer, opt_args[OPT_ARG_CALLEE_GOSUB], "s", 1, S_COR(peer->caller.id.number.valid, peer->caller.id.number.str, NULL)) &&
  2613. ast_exists_extension(peer, opt_args[OPT_ARG_CALLEE_GOSUB], "~~s~~", 1, S_COR(peer->caller.id.number.valid, peer->caller.id.number.str, NULL))) {
  2614. what_is_s = "~~s~~";
  2615. }
  2616. if (ast_asprintf(&gosub_args, "%s,%s,1", opt_args[OPT_ARG_CALLEE_GOSUB], what_is_s) < 0) {
  2617. gosub_args = NULL;
  2618. }
  2619. }
  2620. if (gosub_args) {
  2621. res9 = pbx_exec(peer, theapp, gosub_args);
  2622. if (!res9) {
  2623. struct ast_pbx_args args;
  2624. /* A struct initializer fails to compile for this case ... */
  2625. memset(&args, 0, sizeof(args));
  2626. args.no_hangup_chan = 1;
  2627. ast_pbx_run_args(peer, &args);
  2628. }
  2629. ast_free(gosub_args);
  2630. ast_debug(1, "Gosub exited with status %d\n", res9);
  2631. } else {
  2632. ast_log(LOG_ERROR, "Could not Allocate string for Gosub arguments -- Gosub Call Aborted!\n");
  2633. }
  2634. } else if (!res9) {
  2635. ast_log(LOG_ERROR, "Could not find application Gosub\n");
  2636. res9 = -1;
  2637. }
  2638. if (ast_autoservice_stop(chan) < 0) {
  2639. res9 = -1;
  2640. }
  2641. ast_channel_lock(peer);
  2642. if (!res9 && (gosub_result = pbx_builtin_getvar_helper(peer, "GOSUB_RESULT"))) {
  2643. char *gosub_transfer_dest;
  2644. const char *gosub_retval = pbx_builtin_getvar_helper(peer, "GOSUB_RETVAL");
  2645. /* Inherit return value from the peer, so it can be used in the master */
  2646. if (gosub_retval) {
  2647. pbx_builtin_setvar_helper(chan, "GOSUB_RETVAL", gosub_retval);
  2648. }
  2649. if (!strcasecmp(gosub_result, "BUSY")) {
  2650. ast_copy_string(pa.status, gosub_result, sizeof(pa.status));
  2651. ast_set_flag64(peerflags, OPT_GO_ON);
  2652. res = -1;
  2653. } else if (!strcasecmp(gosub_result, "CONGESTION") || !strcasecmp(gosub_result, "CHANUNAVAIL")) {
  2654. ast_copy_string(pa.status, gosub_result, sizeof(pa.status));
  2655. ast_set_flag64(peerflags, OPT_GO_ON);
  2656. res = -1;
  2657. } else if (!strcasecmp(gosub_result, "CONTINUE")) {
  2658. /* hangup peer and keep chan alive assuming the macro has changed
  2659. the context / exten / priority or perhaps
  2660. the next priority in the current exten is desired.
  2661. */
  2662. ast_set_flag64(peerflags, OPT_GO_ON);
  2663. res = -1;
  2664. } else if (!strcasecmp(gosub_result, "ABORT")) {
  2665. /* Hangup both ends unless the caller has the g flag */
  2666. res = -1;
  2667. } else if (!strncasecmp(gosub_result, "GOTO:", 5) && (gosub_transfer_dest = ast_strdupa(gosub_result + 5))) {
  2668. res = -1;
  2669. /* perform a transfer to a new extension */
  2670. if (strchr(gosub_transfer_dest, '^')) { /* context^exten^priority*/
  2671. replace_macro_delimiter(gosub_transfer_dest);
  2672. }
  2673. if (!ast_parseable_goto(chan, gosub_transfer_dest)) {
  2674. ast_set_flag64(peerflags, OPT_GO_ON);
  2675. }
  2676. }
  2677. }
  2678. ast_channel_unlock(peer);
  2679. }
  2680. if (!res) {
  2681. if (!ast_tvzero(calldurationlimit)) {
  2682. struct timeval whentohangup = calldurationlimit;
  2683. peer->whentohangup = ast_tvadd(ast_tvnow(), whentohangup);
  2684. }
  2685. if (!ast_strlen_zero(dtmfcalled)) {
  2686. ast_verb(3, "Sending DTMF '%s' to the called party.\n", dtmfcalled);
  2687. res = ast_dtmf_stream(peer, chan, dtmfcalled, 250, 0);
  2688. }
  2689. if (!ast_strlen_zero(dtmfcalling)) {
  2690. ast_verb(3, "Sending DTMF '%s' to the calling party.\n", dtmfcalling);
  2691. res = ast_dtmf_stream(chan, peer, dtmfcalling, 250, 0);
  2692. }
  2693. }
  2694. if (res) { /* some error */
  2695. res = -1;
  2696. } else {
  2697. if (ast_test_flag64(peerflags, OPT_CALLEE_TRANSFER))
  2698. ast_set_flag(&(config.features_callee), AST_FEATURE_REDIRECT);
  2699. if (ast_test_flag64(peerflags, OPT_CALLER_TRANSFER))
  2700. ast_set_flag(&(config.features_caller), AST_FEATURE_REDIRECT);
  2701. if (ast_test_flag64(peerflags, OPT_CALLEE_HANGUP))
  2702. ast_set_flag(&(config.features_callee), AST_FEATURE_DISCONNECT);
  2703. if (ast_test_flag64(peerflags, OPT_CALLER_HANGUP))
  2704. ast_set_flag(&(config.features_caller), AST_FEATURE_DISCONNECT);
  2705. if (ast_test_flag64(peerflags, OPT_CALLEE_MONITOR))
  2706. ast_set_flag(&(config.features_callee), AST_FEATURE_AUTOMON);
  2707. if (ast_test_flag64(peerflags, OPT_CALLER_MONITOR))
  2708. ast_set_flag(&(config.features_caller), AST_FEATURE_AUTOMON);
  2709. if (ast_test_flag64(peerflags, OPT_CALLEE_PARK))
  2710. ast_set_flag(&(config.features_callee), AST_FEATURE_PARKCALL);
  2711. if (ast_test_flag64(peerflags, OPT_CALLER_PARK))
  2712. ast_set_flag(&(config.features_caller), AST_FEATURE_PARKCALL);
  2713. if (ast_test_flag64(peerflags, OPT_CALLEE_MIXMONITOR))
  2714. ast_set_flag(&(config.features_callee), AST_FEATURE_AUTOMIXMON);
  2715. if (ast_test_flag64(peerflags, OPT_CALLER_MIXMONITOR))
  2716. ast_set_flag(&(config.features_caller), AST_FEATURE_AUTOMIXMON);
  2717. if (ast_test_flag64(peerflags, OPT_GO_ON))
  2718. ast_set_flag(&(config.features_caller), AST_FEATURE_NO_H_EXTEN);
  2719. config.end_bridge_callback = end_bridge_callback;
  2720. config.end_bridge_callback_data = chan;
  2721. config.end_bridge_callback_data_fixup = end_bridge_callback_data_fixup;
  2722. if (moh) {
  2723. moh = 0;
  2724. ast_moh_stop(chan);
  2725. } else if (sentringing) {
  2726. sentringing = 0;
  2727. ast_indicate(chan, -1);
  2728. }
  2729. /* Be sure no generators are left on it and reset the visible indication */
  2730. ast_deactivate_generator(chan);
  2731. chan->visible_indication = 0;
  2732. /* Make sure channels are compatible */
  2733. res = ast_channel_make_compatible(chan, peer);
  2734. if (res < 0) {
  2735. ast_log(LOG_WARNING, "Had to drop call because I couldn't make %s compatible with %s\n", chan->name, peer->name);
  2736. ast_hangup(peer);
  2737. res = -1;
  2738. goto done;
  2739. }
  2740. if (opermode) {
  2741. struct oprmode oprmode;
  2742. oprmode.peer = peer;
  2743. oprmode.mode = opermode;
  2744. ast_channel_setoption(chan, AST_OPTION_OPRMODE, &oprmode, sizeof(oprmode), 0);
  2745. }
  2746. res = ast_bridge_call(chan, peer, &config);
  2747. }
  2748. strcpy(peer->context, chan->context);
  2749. if (ast_test_flag64(&opts, OPT_PEER_H)
  2750. && ast_exists_extension(peer, peer->context, "h", 1,
  2751. S_COR(peer->caller.id.number.valid, peer->caller.id.number.str, NULL))) {
  2752. int autoloopflag;
  2753. int found;
  2754. int res9;
  2755. strcpy(peer->exten, "h");
  2756. peer->priority = 1;
  2757. autoloopflag = ast_test_flag(peer, AST_FLAG_IN_AUTOLOOP); /* save value to restore at the end */
  2758. ast_set_flag(peer, AST_FLAG_IN_AUTOLOOP);
  2759. while ((res9 = ast_spawn_extension(peer, peer->context, peer->exten,
  2760. peer->priority,
  2761. S_COR(peer->caller.id.number.valid, peer->caller.id.number.str, NULL),
  2762. &found, 1)) == 0) {
  2763. peer->priority++;
  2764. }
  2765. if (found && res9) {
  2766. /* Something bad happened, or a hangup has been requested. */
  2767. ast_debug(1, "Spawn extension (%s,%s,%d) exited non-zero on '%s'\n", peer->context, peer->exten, peer->priority, peer->name);
  2768. ast_verb(2, "Spawn extension (%s, %s, %d) exited non-zero on '%s'\n", peer->context, peer->exten, peer->priority, peer->name);
  2769. }
  2770. ast_set2_flag(peer, autoloopflag, AST_FLAG_IN_AUTOLOOP); /* set it back the way it was */
  2771. }
  2772. if (!ast_check_hangup(peer)) {
  2773. if (ast_test_flag64(&opts, OPT_CALLEE_GO_ON)) {
  2774. int goto_res;
  2775. if (!ast_strlen_zero(opt_args[OPT_ARG_CALLEE_GO_ON])) {
  2776. replace_macro_delimiter(opt_args[OPT_ARG_CALLEE_GO_ON]);
  2777. goto_res = ast_parseable_goto(peer, opt_args[OPT_ARG_CALLEE_GO_ON]);
  2778. } else { /* F() */
  2779. goto_res = ast_goto_if_exists(peer, chan->context,
  2780. chan->exten, chan->priority + 1);
  2781. }
  2782. if (!goto_res && !ast_pbx_start(peer)) {
  2783. /* The peer is now running its own PBX. */
  2784. goto out;
  2785. }
  2786. }
  2787. } else if (!ast_check_hangup(chan)) {
  2788. chan->hangupcause = peer->hangupcause;
  2789. }
  2790. ast_hangup(peer);
  2791. }
  2792. out:
  2793. if (moh) {
  2794. moh = 0;
  2795. ast_moh_stop(chan);
  2796. } else if (sentringing) {
  2797. sentringing = 0;
  2798. ast_indicate(chan, -1);
  2799. }
  2800. if (delprivintro && ast_fileexists(pa.privintro, NULL, NULL) > 0) {
  2801. ast_filedelete(pa.privintro, NULL);
  2802. if (ast_fileexists(pa.privintro, NULL, NULL) > 0) {
  2803. ast_log(LOG_NOTICE, "privacy: ast_filedelete didn't do its job on %s\n", pa.privintro);
  2804. } else {
  2805. ast_verb(3, "Successfully deleted %s intro file\n", pa.privintro);
  2806. }
  2807. }
  2808. ast_channel_early_bridge(chan, NULL);
  2809. /* When dialing local channels, the hangupcause of the parent channel
  2810. * tells us whether the call was answered elsewhere. */
  2811. hanguptree(outgoing, NULL, chan->hangupcause == AST_CAUSE_ANSWERED_ELSEWHERE ? 1 : 0);
  2812. pbx_builtin_setvar_helper(chan, "DIALSTATUS", pa.status);
  2813. senddialendevent(chan, pa.status);
  2814. ast_debug(1, "Exiting with DIALSTATUS=%s.\n", pa.status);
  2815. if ((ast_test_flag64(peerflags, OPT_GO_ON)) && !ast_check_hangup(chan) && (res != AST_PBX_INCOMPLETE)) {
  2816. if (!ast_tvzero(calldurationlimit))
  2817. memset(&chan->whentohangup, 0, sizeof(chan->whentohangup));
  2818. res = 0;
  2819. }
  2820. done:
  2821. if (config.warning_sound) {
  2822. ast_free((char *)config.warning_sound);
  2823. }
  2824. if (config.end_sound) {
  2825. ast_free((char *)config.end_sound);
  2826. }
  2827. if (config.start_sound) {
  2828. ast_free((char *)config.start_sound);
  2829. }
  2830. ast_ignore_cc(chan);
  2831. return res;
  2832. }
  2833. static int dial_exec(struct ast_channel *chan, const char *data)
  2834. {
  2835. struct ast_flags64 peerflags;
  2836. memset(&peerflags, 0, sizeof(peerflags));
  2837. return dial_exec_full(chan, data, &peerflags, NULL);
  2838. }
  2839. static int retrydial_exec(struct ast_channel *chan, const char *data)
  2840. {
  2841. char *parse;
  2842. const char *context = NULL;
  2843. int sleepms = 0, loops = 0, res = -1;
  2844. struct ast_flags64 peerflags = { 0, };
  2845. AST_DECLARE_APP_ARGS(args,
  2846. AST_APP_ARG(announce);
  2847. AST_APP_ARG(sleep);
  2848. AST_APP_ARG(retries);
  2849. AST_APP_ARG(dialdata);
  2850. );
  2851. if (ast_strlen_zero(data)) {
  2852. ast_log(LOG_WARNING, "RetryDial requires an argument!\n");
  2853. return -1;
  2854. }
  2855. parse = ast_strdupa(data);
  2856. AST_STANDARD_APP_ARGS(args, parse);
  2857. if (!ast_strlen_zero(args.sleep) && (sleepms = atoi(args.sleep)))
  2858. sleepms *= 1000;
  2859. if (!ast_strlen_zero(args.retries)) {
  2860. loops = atoi(args.retries);
  2861. }
  2862. if (!args.dialdata) {
  2863. ast_log(LOG_ERROR, "%s requires a 4th argument (dialdata)\n", rapp);
  2864. goto done;
  2865. }
  2866. if (sleepms < 1000)
  2867. sleepms = 10000;
  2868. if (!loops)
  2869. loops = -1; /* run forever */
  2870. ast_channel_lock(chan);
  2871. context = pbx_builtin_getvar_helper(chan, "EXITCONTEXT");
  2872. context = !ast_strlen_zero(context) ? ast_strdupa(context) : NULL;
  2873. ast_channel_unlock(chan);
  2874. res = 0;
  2875. while (loops) {
  2876. int continue_exec;
  2877. chan->data = "Retrying";
  2878. if (ast_test_flag(chan, AST_FLAG_MOH))
  2879. ast_moh_stop(chan);
  2880. res = dial_exec_full(chan, args.dialdata, &peerflags, &continue_exec);
  2881. if (continue_exec)
  2882. break;
  2883. if (res == 0) {
  2884. if (ast_test_flag64(&peerflags, OPT_DTMF_EXIT)) {
  2885. if (!ast_strlen_zero(args.announce)) {
  2886. if (ast_fileexists(args.announce, NULL, chan->language) > 0) {
  2887. if (!(res = ast_streamfile(chan, args.announce, chan->language)))
  2888. ast_waitstream(chan, AST_DIGIT_ANY);
  2889. } else
  2890. ast_log(LOG_WARNING, "Announce file \"%s\" specified in Retrydial does not exist\n", args.announce);
  2891. }
  2892. if (!res && sleepms) {
  2893. if (!ast_test_flag(chan, AST_FLAG_MOH))
  2894. ast_moh_start(chan, NULL, NULL);
  2895. res = ast_waitfordigit(chan, sleepms);
  2896. }
  2897. } else {
  2898. if (!ast_strlen_zero(args.announce)) {
  2899. if (ast_fileexists(args.announce, NULL, chan->language) > 0) {
  2900. if (!(res = ast_streamfile(chan, args.announce, chan->language)))
  2901. res = ast_waitstream(chan, "");
  2902. } else
  2903. ast_log(LOG_WARNING, "Announce file \"%s\" specified in Retrydial does not exist\n", args.announce);
  2904. }
  2905. if (sleepms) {
  2906. if (!ast_test_flag(chan, AST_FLAG_MOH))
  2907. ast_moh_start(chan, NULL, NULL);
  2908. if (!res)
  2909. res = ast_waitfordigit(chan, sleepms);
  2910. }
  2911. }
  2912. }
  2913. if (res < 0 || res == AST_PBX_INCOMPLETE) {
  2914. break;
  2915. } else if (res > 0) { /* Trying to send the call elsewhere (1 digit ext) */
  2916. if (onedigit_goto(chan, context, (char) res, 1)) {
  2917. res = 0;
  2918. break;
  2919. }
  2920. }
  2921. loops--;
  2922. }
  2923. if (loops == 0)
  2924. res = 0;
  2925. else if (res == 1)
  2926. res = 0;
  2927. if (ast_test_flag(chan, AST_FLAG_MOH))
  2928. ast_moh_stop(chan);
  2929. done:
  2930. return res;
  2931. }
  2932. static int unload_module(void)
  2933. {
  2934. int res;
  2935. struct ast_context *con;
  2936. res = ast_unregister_application(app);
  2937. res |= ast_unregister_application(rapp);
  2938. if ((con = ast_context_find("app_dial_gosub_virtual_context"))) {
  2939. ast_context_remove_extension2(con, "s", 1, NULL, 0);
  2940. ast_context_destroy(con, "app_dial"); /* leave nothing behind */
  2941. }
  2942. return res;
  2943. }
  2944. static int load_module(void)
  2945. {
  2946. int res;
  2947. struct ast_context *con;
  2948. con = ast_context_find_or_create(NULL, NULL, "app_dial_gosub_virtual_context", "app_dial");
  2949. if (!con)
  2950. ast_log(LOG_ERROR, "Dial virtual context 'app_dial_gosub_virtual_context' does not exist and unable to create\n");
  2951. else
  2952. ast_add_extension2(con, 1, "s", 1, NULL, NULL, "NoOp", ast_strdup(""), ast_free_ptr, "app_dial");
  2953. res = ast_register_application_xml(app, dial_exec);
  2954. res |= ast_register_application_xml(rapp, retrydial_exec);
  2955. return res;
  2956. }
  2957. AST_MODULE_INFO_STANDARD(ASTERISK_GPL_KEY, "Dialing Application");