app_dial.c 120 KB

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