app_chanspy.c 46 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460
  1. /*
  2. * Asterisk -- An open source telephony toolkit.
  3. *
  4. * Copyright (C) 2005 Anthony Minessale II (anthmct@yahoo.com)
  5. * Copyright (C) 2005 - 2008, Digium, Inc.
  6. *
  7. * A license has been granted to Digium (via disclaimer) for the use of
  8. * this code.
  9. *
  10. * See http://www.asterisk.org for more information about
  11. * the Asterisk project. Please do not directly contact
  12. * any of the maintainers of this project for assistance;
  13. * the project provides a web site, mailing lists and IRC
  14. * channels for your use.
  15. *
  16. * This program is free software, distributed under the terms of
  17. * the GNU General Public License Version 2. See the LICENSE file
  18. * at the top of the source tree.
  19. */
  20. /*! \file
  21. *
  22. * \brief ChanSpy: Listen in on any channel.
  23. *
  24. * \author Anthony Minessale II <anthmct@yahoo.com>
  25. * \author Joshua Colp <jcolp@digium.com>
  26. * \author Russell Bryant <russell@digium.com>
  27. *
  28. * \ingroup applications
  29. */
  30. /*** MODULEINFO
  31. <support_level>core</support_level>
  32. ***/
  33. #include "asterisk.h"
  34. ASTERISK_FILE_VERSION(__FILE__, "$Revision$")
  35. #include <ctype.h>
  36. #include <errno.h>
  37. #include "asterisk/paths.h" /* use ast_config_AST_MONITOR_DIR */
  38. #include "asterisk/file.h"
  39. #include "asterisk/channel.h"
  40. #include "asterisk/audiohook.h"
  41. #include "asterisk/features.h"
  42. #include "asterisk/app.h"
  43. #include "asterisk/utils.h"
  44. #include "asterisk/say.h"
  45. #include "asterisk/pbx.h"
  46. #include "asterisk/translate.h"
  47. #include "asterisk/manager.h"
  48. #include "asterisk/module.h"
  49. #include "asterisk/lock.h"
  50. #include "asterisk/options.h"
  51. #include "asterisk/autochan.h"
  52. #include "asterisk/stasis_channels.h"
  53. #include "asterisk/json.h"
  54. #include "asterisk/format_cache.h"
  55. #define AST_NAME_STRLEN 256
  56. #define NUM_SPYGROUPS 128
  57. /*** DOCUMENTATION
  58. <application name="ChanSpy" language="en_US">
  59. <synopsis>
  60. Listen to a channel, and optionally whisper into it.
  61. </synopsis>
  62. <syntax>
  63. <parameter name="chanprefix" />
  64. <parameter name="options">
  65. <optionlist>
  66. <option name="b">
  67. <para>Only spy on channels involved in a bridged call.</para>
  68. </option>
  69. <option name="B">
  70. <para>Instead of whispering on a single channel barge in on both
  71. channels involved in the call.</para>
  72. </option>
  73. <option name="c">
  74. <argument name="digit" required="true">
  75. <para>Specify a DTMF digit that can be used to spy on the next available channel.</para>
  76. </argument>
  77. </option>
  78. <option name="d">
  79. <para>Override the typical numeric DTMF functionality and instead
  80. use DTMF to switch between spy modes.</para>
  81. <enumlist>
  82. <enum name="4">
  83. <para>spy mode</para>
  84. </enum>
  85. <enum name="5">
  86. <para>whisper mode</para>
  87. </enum>
  88. <enum name="6">
  89. <para>barge mode</para>
  90. </enum>
  91. </enumlist>
  92. </option>
  93. <option name="e">
  94. <argument name="ext" required="true" />
  95. <para>Enable <emphasis>enforced</emphasis> mode, so the spying channel can
  96. only monitor extensions whose name is in the <replaceable>ext</replaceable> : delimited
  97. list.</para>
  98. </option>
  99. <option name="E">
  100. <para>Exit when the spied-on channel hangs up.</para>
  101. </option>
  102. <option name="g">
  103. <argument name="grp" required="true">
  104. <para>Only spy on channels in which one or more of the groups
  105. listed in <replaceable>grp</replaceable> matches one or more groups from the
  106. <variable>SPYGROUP</variable> variable set on the channel to be spied upon.</para>
  107. </argument>
  108. <note><para>both <replaceable>grp</replaceable> and <variable>SPYGROUP</variable> can contain
  109. either a single group or a colon-delimited list of groups, such
  110. as <literal>sales:support:accounting</literal>.</para></note>
  111. </option>
  112. <option name="n" argsep="@">
  113. <para>Say the name of the person being spied on if that person has recorded
  114. his/her name. If a context is specified, then that voicemail context will
  115. be searched when retrieving the name, otherwise the <literal>default</literal> context
  116. be used when searching for the name (i.e. if SIP/1000 is the channel being
  117. spied on and no mailbox is specified, then <literal>1000</literal> will be used when searching
  118. for the name).</para>
  119. <argument name="mailbox" />
  120. <argument name="context" />
  121. </option>
  122. <option name="o">
  123. <para>Only listen to audio coming from this channel.</para>
  124. </option>
  125. <option name="q">
  126. <para>Don't play a beep when beginning to spy on a channel, or speak the
  127. selected channel name.</para>
  128. </option>
  129. <option name="r">
  130. <para>Record the session to the monitor spool directory. An optional base for the filename
  131. may be specified. The default is <literal>chanspy</literal>.</para>
  132. <argument name="basename" />
  133. </option>
  134. <option name="s">
  135. <para>Skip the playback of the channel type (i.e. SIP, IAX, etc) when
  136. speaking the selected channel name.</para>
  137. </option>
  138. <option name="S">
  139. <para>Stop when no more channels are left to spy on.</para>
  140. </option>
  141. <option name="u">
  142. <para>The <literal>chanprefix</literal> parameter is a channel uniqueid
  143. or fully specified channel name.</para>
  144. </option>
  145. <option name="v">
  146. <argument name="value" />
  147. <para>Adjust the initial volume in the range from <literal>-4</literal>
  148. to <literal>4</literal>. A negative value refers to a quieter setting.</para>
  149. </option>
  150. <option name="w">
  151. <para>Enable <literal>whisper</literal> mode, so the spying channel can talk to
  152. the spied-on channel.</para>
  153. </option>
  154. <option name="W">
  155. <para>Enable <literal>private whisper</literal> mode, so the spying channel can
  156. talk to the spied-on channel but cannot listen to that channel.</para>
  157. </option>
  158. <option name="x">
  159. <argument name="digit" required="true">
  160. <para>Specify a DTMF digit that can be used to exit the application while actively
  161. spying on a channel. If there is no channel being spied on, the DTMF digit will be
  162. ignored.</para>
  163. </argument>
  164. </option>
  165. <option name="X">
  166. <para>Allow the user to exit ChanSpy to a valid single digit
  167. numeric extension in the current context or the context
  168. specified by the <variable>SPY_EXIT_CONTEXT</variable> channel variable. The
  169. name of the last channel that was spied on will be stored
  170. in the <variable>SPY_CHANNEL</variable> variable.</para>
  171. </option>
  172. </optionlist>
  173. </parameter>
  174. </syntax>
  175. <description>
  176. <para>This application is used to listen to the audio from an Asterisk channel. This includes the audio
  177. coming in and out of the channel being spied on. If the <literal>chanprefix</literal> parameter is specified,
  178. only channels beginning with this string will be spied upon.</para>
  179. <para>While spying, the following actions may be performed:</para>
  180. <para> - Dialing <literal>#</literal> cycles the volume level.</para>
  181. <para> - Dialing <literal>*</literal> will stop spying and look for another channel to spy on.</para>
  182. <para> - Dialing a series of digits followed by <literal>#</literal> builds a channel name to append
  183. to <literal>chanprefix</literal>. For example, executing ChanSpy(Agent) and then dialing the digits '1234#'
  184. while spying will begin spying on the channel 'Agent/1234'. Note that this feature will be overridden
  185. if the 'd' or 'u' options are used.</para>
  186. <note><para>The <replaceable>X</replaceable> option supersedes the three features above in that if a valid
  187. single digit extension exists in the correct context ChanSpy will exit to it.
  188. This also disables choosing a channel based on <literal>chanprefix</literal> and a digit sequence.</para></note>
  189. </description>
  190. <see-also>
  191. <ref type="application">ExtenSpy</ref>
  192. <ref type="managerEvent">ChanSpyStart</ref>
  193. <ref type="managerEvent">ChanSpyStop</ref>
  194. </see-also>
  195. </application>
  196. <application name="ExtenSpy" language="en_US">
  197. <synopsis>
  198. Listen to a channel, and optionally whisper into it.
  199. </synopsis>
  200. <syntax>
  201. <parameter name="exten" required="true" argsep="@">
  202. <argument name="exten" required="true">
  203. <para>Specify extension.</para>
  204. </argument>
  205. <argument name="context">
  206. <para>Optionally specify a context, defaults to <literal>default</literal>.</para>
  207. </argument>
  208. </parameter>
  209. <parameter name="options">
  210. <optionlist>
  211. <option name="b">
  212. <para>Only spy on channels involved in a bridged call.</para>
  213. </option>
  214. <option name="B">
  215. <para>Instead of whispering on a single channel barge in on both
  216. channels involved in the call.</para>
  217. </option>
  218. <option name="c">
  219. <argument name="digit" required="true">
  220. <para>Specify a DTMF digit that can be used to spy on the next available channel.</para>
  221. </argument>
  222. </option>
  223. <option name="d">
  224. <para>Override the typical numeric DTMF functionality and instead
  225. use DTMF to switch between spy modes.</para>
  226. <enumlist>
  227. <enum name="4">
  228. <para>spy mode</para>
  229. </enum>
  230. <enum name="5">
  231. <para>whisper mode</para>
  232. </enum>
  233. <enum name="6">
  234. <para>barge mode</para>
  235. </enum>
  236. </enumlist>
  237. </option>
  238. <option name="e">
  239. <argument name="ext" required="true" />
  240. <para>Enable <emphasis>enforced</emphasis> mode, so the spying channel can
  241. only monitor extensions whose name is in the <replaceable>ext</replaceable> : delimited
  242. list.</para>
  243. </option>
  244. <option name="E">
  245. <para>Exit when the spied-on channel hangs up.</para>
  246. </option>
  247. <option name="g">
  248. <argument name="grp" required="true">
  249. <para>Only spy on channels in which one or more of the groups
  250. listed in <replaceable>grp</replaceable> matches one or more groups from the
  251. <variable>SPYGROUP</variable> variable set on the channel to be spied upon.</para>
  252. </argument>
  253. <note><para>both <replaceable>grp</replaceable> and <variable>SPYGROUP</variable> can contain
  254. either a single group or a colon-delimited list of groups, such
  255. as <literal>sales:support:accounting</literal>.</para></note>
  256. </option>
  257. <option name="n" argsep="@">
  258. <para>Say the name of the person being spied on if that person has recorded
  259. his/her name. If a context is specified, then that voicemail context will
  260. be searched when retrieving the name, otherwise the <literal>default</literal> context
  261. be used when searching for the name (i.e. if SIP/1000 is the channel being
  262. spied on and no mailbox is specified, then <literal>1000</literal> will be used when searching
  263. for the name).</para>
  264. <argument name="mailbox" />
  265. <argument name="context" />
  266. </option>
  267. <option name="o">
  268. <para>Only listen to audio coming from this channel.</para>
  269. </option>
  270. <option name="q">
  271. <para>Don't play a beep when beginning to spy on a channel, or speak the
  272. selected channel name.</para>
  273. </option>
  274. <option name="r">
  275. <para>Record the session to the monitor spool directory. An optional base for the filename
  276. may be specified. The default is <literal>chanspy</literal>.</para>
  277. <argument name="basename" />
  278. </option>
  279. <option name="s">
  280. <para>Skip the playback of the channel type (i.e. SIP, IAX, etc) when
  281. speaking the selected channel name.</para>
  282. </option>
  283. <option name="S">
  284. <para>Stop when there are no more extensions left to spy on.</para>
  285. </option>
  286. <option name="v">
  287. <argument name="value" />
  288. <para>Adjust the initial volume in the range from <literal>-4</literal>
  289. to <literal>4</literal>. A negative value refers to a quieter setting.</para>
  290. </option>
  291. <option name="w">
  292. <para>Enable <literal>whisper</literal> mode, so the spying channel can talk to
  293. the spied-on channel.</para>
  294. </option>
  295. <option name="W">
  296. <para>Enable <literal>private whisper</literal> mode, so the spying channel can
  297. talk to the spied-on channel but cannot listen to that channel.</para>
  298. </option>
  299. <option name="x">
  300. <argument name="digit" required="true">
  301. <para>Specify a DTMF digit that can be used to exit the application while actively
  302. spying on a channel. If there is no channel being spied on, the DTMF digit will be
  303. ignored.</para>
  304. </argument>
  305. </option>
  306. <option name="X">
  307. <para>Allow the user to exit ChanSpy to a valid single digit
  308. numeric extension in the current context or the context
  309. specified by the <variable>SPY_EXIT_CONTEXT</variable> channel variable. The
  310. name of the last channel that was spied on will be stored
  311. in the <variable>SPY_CHANNEL</variable> variable.</para>
  312. </option>
  313. </optionlist>
  314. </parameter>
  315. </syntax>
  316. <description>
  317. <para>This application is used to listen to the audio from an Asterisk channel. This includes
  318. the audio coming in and out of the channel being spied on. Only channels created by outgoing calls for the
  319. specified extension will be selected for spying. If the optional context is not supplied,
  320. the current channel's context will be used.</para>
  321. <para>While spying, the following actions may be performed:</para>
  322. <para> - Dialing <literal>#</literal> cycles the volume level.</para>
  323. <para> - Dialing <literal>*</literal> will stop spying and look for another channel to spy on.</para>
  324. <note><para>The <replaceable>X</replaceable> option supersedes the three features above in that if a valid
  325. single digit extension exists in the correct context ChanSpy will exit to it.
  326. This also disables choosing a channel based on <literal>chanprefix</literal> and a digit sequence.</para></note>
  327. </description>
  328. <see-also>
  329. <ref type="application">ChanSpy</ref>
  330. <ref type="managerEvent">ChanSpyStart</ref>
  331. <ref type="managerEvent">ChanSpyStop</ref>
  332. </see-also>
  333. </application>
  334. <application name="DAHDIScan" language="en_US">
  335. <synopsis>
  336. Scan DAHDI channels to monitor calls.
  337. </synopsis>
  338. <syntax>
  339. <parameter name="group">
  340. <para>Limit scanning to a channel <replaceable>group</replaceable> by setting this option.</para>
  341. </parameter>
  342. </syntax>
  343. <description>
  344. <para>Allows a call center manager to monitor DAHDI channels in a
  345. convenient way. Use <literal>#</literal> to select the next channel and use <literal>*</literal> to exit.</para>
  346. </description>
  347. <see-also>
  348. <ref type="managerEvent">ChanSpyStart</ref>
  349. <ref type="managerEvent">ChanSpyStop</ref>
  350. </see-also>
  351. </application>
  352. ***/
  353. static const char app_chan[] = "ChanSpy";
  354. static const char app_ext[] = "ExtenSpy";
  355. static const char app_dahdiscan[] = "DAHDIScan";
  356. enum {
  357. OPTION_QUIET = (1 << 0), /* Quiet, no announcement */
  358. OPTION_BRIDGED = (1 << 1), /* Only look at bridged calls */
  359. OPTION_VOLUME = (1 << 2), /* Specify initial volume */
  360. OPTION_GROUP = (1 << 3), /* Only look at channels in group */
  361. OPTION_RECORD = (1 << 4),
  362. OPTION_WHISPER = (1 << 5),
  363. OPTION_PRIVATE = (1 << 6), /* Private Whisper mode */
  364. OPTION_READONLY = (1 << 7), /* Don't mix the two channels */
  365. OPTION_EXIT = (1 << 8), /* Exit to a valid single digit extension */
  366. OPTION_ENFORCED = (1 << 9), /* Enforced mode */
  367. OPTION_NOTECH = (1 << 10), /* Skip technology name playback */
  368. OPTION_BARGE = (1 << 11), /* Barge mode (whisper to both channels) */
  369. OPTION_NAME = (1 << 12), /* Say the name of the person on whom we will spy */
  370. OPTION_DTMF_SWITCH_MODES = (1 << 13), /* Allow numeric DTMF to switch between chanspy modes */
  371. OPTION_DTMF_EXIT = (1 << 14), /* Set DTMF to exit, added for DAHDIScan integration */
  372. OPTION_DTMF_CYCLE = (1 << 15), /* Custom DTMF for cycling next available channel, (default is '*') */
  373. OPTION_DAHDI_SCAN = (1 << 16), /* Scan groups in DAHDIScan mode */
  374. OPTION_STOP = (1 << 17),
  375. OPTION_EXITONHANGUP = (1 << 18), /* Hang up when the spied-on channel hangs up. */
  376. OPTION_UNIQUEID = (1 << 19), /* The chanprefix is a channel uniqueid or fully specified channel name. */
  377. };
  378. enum {
  379. OPT_ARG_VOLUME = 0,
  380. OPT_ARG_GROUP,
  381. OPT_ARG_RECORD,
  382. OPT_ARG_ENFORCED,
  383. OPT_ARG_NAME,
  384. OPT_ARG_EXIT,
  385. OPT_ARG_CYCLE,
  386. OPT_ARG_ARRAY_SIZE,
  387. };
  388. AST_APP_OPTIONS(spy_opts, {
  389. AST_APP_OPTION('b', OPTION_BRIDGED),
  390. AST_APP_OPTION('B', OPTION_BARGE),
  391. AST_APP_OPTION_ARG('c', OPTION_DTMF_CYCLE, OPT_ARG_CYCLE),
  392. AST_APP_OPTION('d', OPTION_DTMF_SWITCH_MODES),
  393. AST_APP_OPTION_ARG('e', OPTION_ENFORCED, OPT_ARG_ENFORCED),
  394. AST_APP_OPTION('E', OPTION_EXITONHANGUP),
  395. AST_APP_OPTION_ARG('g', OPTION_GROUP, OPT_ARG_GROUP),
  396. AST_APP_OPTION_ARG('n', OPTION_NAME, OPT_ARG_NAME),
  397. AST_APP_OPTION('o', OPTION_READONLY),
  398. AST_APP_OPTION('q', OPTION_QUIET),
  399. AST_APP_OPTION_ARG('r', OPTION_RECORD, OPT_ARG_RECORD),
  400. AST_APP_OPTION('s', OPTION_NOTECH),
  401. AST_APP_OPTION('S', OPTION_STOP),
  402. AST_APP_OPTION('u', OPTION_UNIQUEID),
  403. AST_APP_OPTION_ARG('v', OPTION_VOLUME, OPT_ARG_VOLUME),
  404. AST_APP_OPTION('w', OPTION_WHISPER),
  405. AST_APP_OPTION('W', OPTION_PRIVATE),
  406. AST_APP_OPTION_ARG('x', OPTION_DTMF_EXIT, OPT_ARG_EXIT),
  407. AST_APP_OPTION('X', OPTION_EXIT),
  408. });
  409. struct chanspy_translation_helper {
  410. /* spy data */
  411. struct ast_audiohook spy_audiohook;
  412. struct ast_audiohook whisper_audiohook;
  413. struct ast_audiohook bridge_whisper_audiohook;
  414. int fd;
  415. int volfactor;
  416. struct ast_flags flags;
  417. };
  418. struct spy_dtmf_options {
  419. char exit;
  420. char cycle;
  421. char volume;
  422. };
  423. static void *spy_alloc(struct ast_channel *chan, void *data)
  424. {
  425. /* just store the data pointer in the channel structure */
  426. return data;
  427. }
  428. static void spy_release(struct ast_channel *chan, void *data)
  429. {
  430. /* nothing to do */
  431. }
  432. static int spy_generate(struct ast_channel *chan, void *data, int len, int samples)
  433. {
  434. struct chanspy_translation_helper *csth = data;
  435. struct ast_frame *f, *cur;
  436. ast_audiohook_lock(&csth->spy_audiohook);
  437. if (csth->spy_audiohook.status != AST_AUDIOHOOK_STATUS_RUNNING) {
  438. /* Channel is already gone more than likely */
  439. ast_audiohook_unlock(&csth->spy_audiohook);
  440. return -1;
  441. }
  442. if (ast_test_flag(&csth->flags, OPTION_READONLY)) {
  443. /* Option 'o' was set, so don't mix channel audio */
  444. f = ast_audiohook_read_frame(&csth->spy_audiohook, samples, AST_AUDIOHOOK_DIRECTION_READ, ast_format_slin);
  445. } else {
  446. f = ast_audiohook_read_frame(&csth->spy_audiohook, samples, AST_AUDIOHOOK_DIRECTION_BOTH, ast_format_slin);
  447. }
  448. ast_audiohook_unlock(&csth->spy_audiohook);
  449. if (!f)
  450. return 0;
  451. for (cur = f; cur; cur = AST_LIST_NEXT(cur, frame_list)) {
  452. if (ast_write(chan, cur)) {
  453. ast_frfree(f);
  454. return -1;
  455. }
  456. if (csth->fd) {
  457. if (write(csth->fd, cur->data.ptr, cur->datalen) < 0) {
  458. ast_log(LOG_WARNING, "write() failed: %s\n", strerror(errno));
  459. }
  460. }
  461. }
  462. ast_frfree(f);
  463. return 0;
  464. }
  465. static struct ast_generator spygen = {
  466. .alloc = spy_alloc,
  467. .release = spy_release,
  468. .generate = spy_generate,
  469. };
  470. static int start_spying(struct ast_autochan *autochan, const char *spychan_name, struct ast_audiohook *audiohook)
  471. {
  472. ast_log(LOG_NOTICE, "Attaching %s to %s\n", spychan_name, ast_channel_name(autochan->chan));
  473. ast_set_flag(audiohook, AST_AUDIOHOOK_TRIGGER_SYNC | AST_AUDIOHOOK_SMALL_QUEUE);
  474. return ast_audiohook_attach(autochan->chan, audiohook);
  475. }
  476. static void change_spy_mode(const char digit, struct ast_flags *flags)
  477. {
  478. if (digit == '4') {
  479. ast_clear_flag(flags, OPTION_WHISPER);
  480. ast_clear_flag(flags, OPTION_BARGE);
  481. } else if (digit == '5') {
  482. ast_clear_flag(flags, OPTION_BARGE);
  483. ast_set_flag(flags, OPTION_WHISPER);
  484. } else if (digit == '6') {
  485. ast_clear_flag(flags, OPTION_WHISPER);
  486. ast_set_flag(flags, OPTION_BARGE);
  487. }
  488. }
  489. static int pack_channel_into_message(struct ast_channel *chan, const char *role,
  490. struct ast_multi_channel_blob *payload)
  491. {
  492. RAII_VAR(struct ast_channel_snapshot *, snapshot,
  493. ast_channel_snapshot_get_latest(ast_channel_uniqueid(chan)),
  494. ao2_cleanup);
  495. if (!snapshot) {
  496. return -1;
  497. }
  498. ast_multi_channel_blob_add_channel(payload, role, snapshot);
  499. return 0;
  500. }
  501. /*! \internal
  502. * \brief Publish the chanspy message over Stasis-Core
  503. * \param spyer The channel doing the spying
  504. * \param spyee Who is being spied upon
  505. * \start start If non-zero, the spying is starting. Otherwise, the spyer is
  506. * finishing
  507. */
  508. static void publish_chanspy_message(struct ast_channel *spyer,
  509. struct ast_channel *spyee,
  510. int start)
  511. {
  512. RAII_VAR(struct ast_json *, blob, NULL, ast_json_unref);
  513. RAII_VAR(struct ast_multi_channel_blob *, payload, NULL, ao2_cleanup);
  514. RAII_VAR(struct stasis_message *, message, NULL, ao2_cleanup);
  515. struct stasis_message_type *type = start ? ast_channel_chanspy_start_type(): ast_channel_chanspy_stop_type();
  516. if (!spyer) {
  517. ast_log(AST_LOG_WARNING, "Attempt to publish ChanSpy message for NULL spyer channel\n");
  518. return;
  519. }
  520. blob = ast_json_null();
  521. if (!blob || !type) {
  522. return;
  523. }
  524. payload = ast_multi_channel_blob_create(blob);
  525. if (!payload) {
  526. return;
  527. }
  528. if (pack_channel_into_message(spyer, "spyer_channel", payload)) {
  529. return;
  530. }
  531. if (spyee) {
  532. if (pack_channel_into_message(spyee, "spyee_channel", payload)) {
  533. return;
  534. }
  535. }
  536. message = stasis_message_create(type, payload);
  537. if (!message) {
  538. return;
  539. }
  540. stasis_publish(ast_channel_topic(spyer), message);
  541. }
  542. static int attach_barge(struct ast_autochan *spyee_autochan,
  543. struct ast_autochan **spyee_bridge_autochan, struct ast_audiohook *bridge_whisper_audiohook,
  544. const char *spyer_name, const char *name)
  545. {
  546. int retval = 0;
  547. struct ast_autochan *internal_bridge_autochan;
  548. RAII_VAR(struct ast_channel *, bridged, ast_channel_bridge_peer(spyee_autochan->chan), ast_channel_cleanup);
  549. if (!bridged) {
  550. return -1;
  551. }
  552. ast_audiohook_init(bridge_whisper_audiohook, AST_AUDIOHOOK_TYPE_WHISPER, "Chanspy", 0);
  553. internal_bridge_autochan = ast_autochan_setup(bridged);
  554. if (!internal_bridge_autochan) {
  555. return -1;
  556. }
  557. ast_channel_lock(internal_bridge_autochan->chan);
  558. if (start_spying(internal_bridge_autochan, spyer_name, bridge_whisper_audiohook)) {
  559. ast_log(LOG_WARNING, "Unable to attach barge audiohook on spyee '%s'. Barge mode disabled.\n", name);
  560. retval = -1;
  561. }
  562. ast_channel_unlock(internal_bridge_autochan->chan);
  563. *spyee_bridge_autochan = internal_bridge_autochan;
  564. return retval;
  565. }
  566. static int channel_spy(struct ast_channel *chan, struct ast_autochan *spyee_autochan,
  567. int *volfactor, int fd, struct spy_dtmf_options *user_options, struct ast_flags *flags,
  568. char *exitcontext)
  569. {
  570. struct chanspy_translation_helper csth;
  571. int running = 0, bridge_connected = 0, res, x = 0;
  572. char inp[24] = {0};
  573. char *name;
  574. struct ast_frame *f;
  575. struct ast_silence_generator *silgen = NULL;
  576. struct ast_autochan *spyee_bridge_autochan = NULL;
  577. const char *spyer_name;
  578. if (ast_check_hangup(chan) || ast_check_hangup(spyee_autochan->chan) ||
  579. ast_test_flag(ast_channel_flags(spyee_autochan->chan), AST_FLAG_ZOMBIE)) {
  580. return 0;
  581. }
  582. ast_channel_lock(chan);
  583. spyer_name = ast_strdupa(ast_channel_name(chan));
  584. ast_channel_unlock(chan);
  585. ast_channel_lock(spyee_autochan->chan);
  586. name = ast_strdupa(ast_channel_name(spyee_autochan->chan));
  587. ast_channel_unlock(spyee_autochan->chan);
  588. ast_verb(2, "Spying on channel %s\n", name);
  589. publish_chanspy_message(chan, spyee_autochan->chan, 1);
  590. memset(&csth, 0, sizeof(csth));
  591. ast_copy_flags(&csth.flags, flags, AST_FLAGS_ALL);
  592. /* This is the audiohook which gives us the audio off the channel we are
  593. spying on.
  594. */
  595. ast_audiohook_init(&csth.spy_audiohook, AST_AUDIOHOOK_TYPE_SPY, "ChanSpy", 0);
  596. if (start_spying(spyee_autochan, spyer_name, &csth.spy_audiohook)) {
  597. ast_audiohook_destroy(&csth.spy_audiohook);
  598. return 0;
  599. }
  600. if (ast_test_flag(flags, OPTION_WHISPER | OPTION_BARGE | OPTION_DTMF_SWITCH_MODES)) {
  601. /* This audiohook will let us inject audio from our channel into the
  602. channel we are currently spying on.
  603. */
  604. ast_audiohook_init(&csth.whisper_audiohook, AST_AUDIOHOOK_TYPE_WHISPER, "ChanSpy", 0);
  605. if (start_spying(spyee_autochan, spyer_name, &csth.whisper_audiohook)) {
  606. ast_log(LOG_WARNING, "Unable to attach whisper audiohook to spyee %s. Whisper mode disabled!\n", name);
  607. }
  608. }
  609. ast_channel_lock(chan);
  610. ast_set_flag(ast_channel_flags(chan), AST_FLAG_END_DTMF_ONLY);
  611. ast_channel_unlock(chan);
  612. csth.volfactor = *volfactor;
  613. if (csth.volfactor) {
  614. csth.spy_audiohook.options.read_volume = csth.volfactor;
  615. csth.spy_audiohook.options.write_volume = csth.volfactor;
  616. }
  617. csth.fd = fd;
  618. if (ast_test_flag(flags, OPTION_PRIVATE))
  619. silgen = ast_channel_start_silence_generator(chan);
  620. else
  621. ast_activate_generator(chan, &spygen, &csth);
  622. /* We can no longer rely on 'spyee' being an actual channel;
  623. it can be hung up and freed out from under us. However, the
  624. channel destructor will put NULL into our csth.spy.chan
  625. field when that happens, so that is our signal that the spyee
  626. channel has gone away.
  627. */
  628. /* Note: it is very important that the ast_waitfor() be the first
  629. condition in this expression, so that if we wait for some period
  630. of time before receiving a frame from our spying channel, we check
  631. for hangup on the spied-on channel _after_ knowing that a frame
  632. has arrived, since the spied-on channel could have gone away while
  633. we were waiting
  634. */
  635. while (ast_waitfor(chan, -1) > -1 && csth.spy_audiohook.status == AST_AUDIOHOOK_STATUS_RUNNING) {
  636. if (!(f = ast_read(chan)) || ast_check_hangup(chan)) {
  637. running = -1;
  638. break;
  639. }
  640. if (ast_test_flag(flags, OPTION_BARGE) && f->frametype == AST_FRAME_VOICE) {
  641. /* This hook lets us inject audio into the channel that the spyee is currently
  642. * bridged with. If the spyee isn't bridged with anything yet, nothing will
  643. * be attached and we'll need to continue attempting to attach the barge
  644. * audio hook. */
  645. if (!bridge_connected && attach_barge(spyee_autochan, &spyee_bridge_autochan,
  646. &csth.bridge_whisper_audiohook, spyer_name, name) == 0) {
  647. bridge_connected = 1;
  648. }
  649. ast_audiohook_lock(&csth.whisper_audiohook);
  650. ast_audiohook_write_frame(&csth.whisper_audiohook, AST_AUDIOHOOK_DIRECTION_WRITE, f);
  651. ast_audiohook_unlock(&csth.whisper_audiohook);
  652. if (bridge_connected) {
  653. ast_audiohook_lock(&csth.bridge_whisper_audiohook);
  654. ast_audiohook_write_frame(&csth.bridge_whisper_audiohook, AST_AUDIOHOOK_DIRECTION_WRITE, f);
  655. ast_audiohook_unlock(&csth.bridge_whisper_audiohook);
  656. }
  657. ast_frfree(f);
  658. continue;
  659. } else if (ast_test_flag(flags, OPTION_WHISPER) && f->frametype == AST_FRAME_VOICE) {
  660. ast_audiohook_lock(&csth.whisper_audiohook);
  661. ast_audiohook_write_frame(&csth.whisper_audiohook, AST_AUDIOHOOK_DIRECTION_WRITE, f);
  662. ast_audiohook_unlock(&csth.whisper_audiohook);
  663. ast_frfree(f);
  664. continue;
  665. }
  666. res = (f->frametype == AST_FRAME_DTMF) ? f->subclass.integer : 0;
  667. ast_frfree(f);
  668. if (!res)
  669. continue;
  670. if (x == sizeof(inp))
  671. x = 0;
  672. if (res < 0) {
  673. running = -1;
  674. break;
  675. }
  676. if (ast_test_flag(flags, OPTION_EXIT)) {
  677. char tmp[2];
  678. tmp[0] = res;
  679. tmp[1] = '\0';
  680. if (!ast_goto_if_exists(chan, exitcontext, tmp, 1)) {
  681. ast_debug(1, "Got DTMF %c, goto context %s\n", tmp[0], exitcontext);
  682. pbx_builtin_setvar_helper(chan, "SPY_CHANNEL", name);
  683. running = -2;
  684. break;
  685. } else {
  686. ast_debug(2, "Exit by single digit did not work in chanspy. Extension %s does not exist in context %s\n", tmp, exitcontext);
  687. }
  688. } else if (res >= '0' && res <= '9') {
  689. if (ast_test_flag(flags, OPTION_DTMF_SWITCH_MODES)) {
  690. change_spy_mode(res, flags);
  691. } else {
  692. inp[x++] = res;
  693. }
  694. }
  695. if (res == user_options->cycle) {
  696. running = 0;
  697. break;
  698. } else if (res == user_options->exit) {
  699. running = -2;
  700. break;
  701. } else if (res == user_options->volume) {
  702. if (!ast_strlen_zero(inp)) {
  703. running = atoi(inp);
  704. break;
  705. }
  706. (*volfactor)++;
  707. if (*volfactor > 4)
  708. *volfactor = -4;
  709. ast_verb(3, "Setting spy volume on %s to %d\n", ast_channel_name(chan), *volfactor);
  710. csth.volfactor = *volfactor;
  711. csth.spy_audiohook.options.read_volume = csth.volfactor;
  712. csth.spy_audiohook.options.write_volume = csth.volfactor;
  713. }
  714. }
  715. if (ast_test_flag(flags, OPTION_PRIVATE))
  716. ast_channel_stop_silence_generator(chan, silgen);
  717. else
  718. ast_deactivate_generator(chan);
  719. ast_channel_lock(chan);
  720. ast_clear_flag(ast_channel_flags(chan), AST_FLAG_END_DTMF_ONLY);
  721. ast_channel_unlock(chan);
  722. if (ast_test_flag(flags, OPTION_WHISPER | OPTION_BARGE | OPTION_DTMF_SWITCH_MODES)) {
  723. ast_audiohook_lock(&csth.whisper_audiohook);
  724. ast_audiohook_detach(&csth.whisper_audiohook);
  725. ast_audiohook_unlock(&csth.whisper_audiohook);
  726. ast_audiohook_destroy(&csth.whisper_audiohook);
  727. }
  728. if (ast_test_flag(flags, OPTION_BARGE | OPTION_DTMF_SWITCH_MODES)) {
  729. ast_audiohook_lock(&csth.bridge_whisper_audiohook);
  730. ast_audiohook_detach(&csth.bridge_whisper_audiohook);
  731. ast_audiohook_unlock(&csth.bridge_whisper_audiohook);
  732. ast_audiohook_destroy(&csth.bridge_whisper_audiohook);
  733. }
  734. ast_audiohook_lock(&csth.spy_audiohook);
  735. ast_audiohook_detach(&csth.spy_audiohook);
  736. ast_audiohook_unlock(&csth.spy_audiohook);
  737. ast_audiohook_destroy(&csth.spy_audiohook);
  738. if (spyee_bridge_autochan) {
  739. ast_autochan_destroy(spyee_bridge_autochan);
  740. }
  741. ast_verb(2, "Done Spying on channel %s\n", name);
  742. publish_chanspy_message(chan, NULL, 0);
  743. return running;
  744. }
  745. static struct ast_autochan *next_channel(struct ast_channel_iterator *iter,
  746. struct ast_autochan *autochan, struct ast_channel *chan)
  747. {
  748. struct ast_channel *next;
  749. struct ast_autochan *autochan_store;
  750. const size_t pseudo_len = strlen("DAHDI/pseudo");
  751. if (!iter) {
  752. return NULL;
  753. }
  754. for (; (next = ast_channel_iterator_next(iter)); ast_channel_unref(next)) {
  755. if (!strncmp(ast_channel_name(next), "DAHDI/pseudo", pseudo_len)
  756. || next == chan) {
  757. continue;
  758. }
  759. autochan_store = ast_autochan_setup(next);
  760. ast_channel_unref(next);
  761. return autochan_store;
  762. }
  763. return NULL;
  764. }
  765. static int spy_sayname(struct ast_channel *chan, const char *mailbox, const char *context)
  766. {
  767. char *mailbox_id;
  768. mailbox_id = ast_alloca(strlen(mailbox) + strlen(context) + 2);
  769. sprintf(mailbox_id, "%s@%s", mailbox, context); /* Safe */
  770. return ast_app_sayname(chan, mailbox_id);
  771. }
  772. static int common_exec(struct ast_channel *chan, struct ast_flags *flags,
  773. int volfactor, const int fd, struct spy_dtmf_options *user_options,
  774. const char *mygroup, const char *myenforced, const char *spec, const char *exten,
  775. const char *context, const char *mailbox, const char *name_context)
  776. {
  777. char nameprefix[AST_NAME_STRLEN];
  778. char exitcontext[AST_MAX_CONTEXT] = "";
  779. signed char zero_volume = 0;
  780. int waitms;
  781. int res;
  782. int num_spyed_upon = 1;
  783. struct ast_channel_iterator *iter = NULL;
  784. if (ast_test_flag(flags, OPTION_EXIT)) {
  785. const char *c;
  786. ast_channel_lock(chan);
  787. if ((c = pbx_builtin_getvar_helper(chan, "SPY_EXIT_CONTEXT"))) {
  788. ast_copy_string(exitcontext, c, sizeof(exitcontext));
  789. } else if (!ast_strlen_zero(ast_channel_macrocontext(chan))) {
  790. ast_copy_string(exitcontext, ast_channel_macrocontext(chan), sizeof(exitcontext));
  791. } else {
  792. ast_copy_string(exitcontext, ast_channel_context(chan), sizeof(exitcontext));
  793. }
  794. ast_channel_unlock(chan);
  795. }
  796. if (ast_channel_state(chan) != AST_STATE_UP)
  797. ast_answer(chan);
  798. ast_set_flag(ast_channel_flags(chan), AST_FLAG_SPYING); /* so nobody can spy on us while we are spying */
  799. waitms = 100;
  800. for (;;) {
  801. struct ast_autochan *autochan = NULL, *next_autochan = NULL;
  802. struct ast_channel *prev = NULL;
  803. if (!ast_test_flag(flags, OPTION_QUIET) && num_spyed_upon) {
  804. res = ast_streamfile(chan, "beep", ast_channel_language(chan));
  805. if (!res)
  806. res = ast_waitstream(chan, "");
  807. else if (res < 0) {
  808. ast_clear_flag(ast_channel_flags(chan), AST_FLAG_SPYING);
  809. break;
  810. }
  811. if (!ast_strlen_zero(exitcontext)) {
  812. char tmp[2];
  813. tmp[0] = res;
  814. tmp[1] = '\0';
  815. if (!ast_goto_if_exists(chan, exitcontext, tmp, 1))
  816. goto exit;
  817. else
  818. ast_debug(2, "Exit by single digit did not work in chanspy. Extension %s does not exist in context %s\n", tmp, exitcontext);
  819. }
  820. }
  821. /* Set up the iterator we'll be using during this call */
  822. if (!ast_strlen_zero(spec)) {
  823. if (ast_test_flag(flags, OPTION_UNIQUEID)) {
  824. struct ast_channel *unique_chan;
  825. unique_chan = ast_channel_get_by_name(spec);
  826. if (!unique_chan) {
  827. res = -1;
  828. goto exit;
  829. }
  830. iter = ast_channel_iterator_by_name_new(ast_channel_name(unique_chan), 0);
  831. ast_channel_unref(unique_chan);
  832. } else {
  833. iter = ast_channel_iterator_by_name_new(spec, strlen(spec));
  834. }
  835. } else if (!ast_strlen_zero(exten)) {
  836. iter = ast_channel_iterator_by_exten_new(exten, context);
  837. } else {
  838. iter = ast_channel_iterator_all_new();
  839. }
  840. if (!iter) {
  841. res = -1;
  842. goto exit;
  843. }
  844. res = ast_waitfordigit(chan, waitms);
  845. if (res < 0) {
  846. iter = ast_channel_iterator_destroy(iter);
  847. ast_clear_flag(ast_channel_flags(chan), AST_FLAG_SPYING);
  848. break;
  849. }
  850. if (!ast_strlen_zero(exitcontext)) {
  851. char tmp[2];
  852. tmp[0] = res;
  853. tmp[1] = '\0';
  854. if (!ast_goto_if_exists(chan, exitcontext, tmp, 1)) {
  855. iter = ast_channel_iterator_destroy(iter);
  856. goto exit;
  857. } else {
  858. ast_debug(2, "Exit by single digit did not work in chanspy. Extension %s does not exist in context %s\n", tmp, exitcontext);
  859. }
  860. }
  861. /* reset for the next loop around, unless overridden later */
  862. waitms = 100;
  863. num_spyed_upon = 0;
  864. for (autochan = next_channel(iter, autochan, chan);
  865. autochan;
  866. prev = autochan->chan, ast_autochan_destroy(autochan),
  867. autochan = next_autochan ? next_autochan :
  868. next_channel(iter, autochan, chan), next_autochan = NULL) {
  869. int igrp = !mygroup;
  870. int ienf = !myenforced;
  871. if (autochan->chan == prev) {
  872. ast_autochan_destroy(autochan);
  873. break;
  874. }
  875. if (ast_check_hangup(chan)) {
  876. ast_autochan_destroy(autochan);
  877. break;
  878. }
  879. if (ast_test_flag(flags, OPTION_BRIDGED) && !ast_channel_is_bridged(autochan->chan)) {
  880. continue;
  881. }
  882. if (ast_check_hangup(autochan->chan) || ast_test_flag(ast_channel_flags(autochan->chan), AST_FLAG_SPYING)) {
  883. continue;
  884. }
  885. if (mygroup) {
  886. int num_groups = 0;
  887. int num_mygroups = 0;
  888. char dup_group[512];
  889. char dup_mygroup[512];
  890. char *groups[NUM_SPYGROUPS];
  891. char *mygroups[NUM_SPYGROUPS];
  892. const char *group = NULL;
  893. int x;
  894. int y;
  895. ast_copy_string(dup_mygroup, mygroup, sizeof(dup_mygroup));
  896. num_mygroups = ast_app_separate_args(dup_mygroup, ':', mygroups,
  897. ARRAY_LEN(mygroups));
  898. /* Before dahdi scan was part of chanspy, it would use the "GROUP" variable
  899. * rather than "SPYGROUP", this check is done to preserve expected behavior */
  900. if (ast_test_flag(flags, OPTION_DAHDI_SCAN)) {
  901. group = pbx_builtin_getvar_helper(autochan->chan, "GROUP");
  902. } else {
  903. group = pbx_builtin_getvar_helper(autochan->chan, "SPYGROUP");
  904. }
  905. if (!ast_strlen_zero(group)) {
  906. ast_copy_string(dup_group, group, sizeof(dup_group));
  907. num_groups = ast_app_separate_args(dup_group, ':', groups,
  908. ARRAY_LEN(groups));
  909. }
  910. for (y = 0; y < num_mygroups; y++) {
  911. for (x = 0; x < num_groups; x++) {
  912. if (!strcmp(mygroups[y], groups[x])) {
  913. igrp = 1;
  914. break;
  915. }
  916. }
  917. }
  918. }
  919. if (!igrp) {
  920. continue;
  921. }
  922. if (myenforced) {
  923. char ext[AST_CHANNEL_NAME + 3];
  924. char buffer[512];
  925. char *end;
  926. snprintf(buffer, sizeof(buffer) - 1, ":%s:", myenforced);
  927. ast_copy_string(ext + 1, ast_channel_name(autochan->chan), sizeof(ext) - 1);
  928. if ((end = strchr(ext, '-'))) {
  929. *end++ = ':';
  930. *end = '\0';
  931. }
  932. ext[0] = ':';
  933. if (strcasestr(buffer, ext)) {
  934. ienf = 1;
  935. }
  936. }
  937. if (!ienf) {
  938. continue;
  939. }
  940. if (!ast_test_flag(flags, OPTION_QUIET)) {
  941. char peer_name[AST_NAME_STRLEN + 5];
  942. char *ptr, *s;
  943. strcpy(peer_name, "spy-");
  944. strncat(peer_name, ast_channel_name(autochan->chan), AST_NAME_STRLEN - 4 - 1);
  945. if ((ptr = strchr(peer_name, '/'))) {
  946. *ptr++ = '\0';
  947. for (s = peer_name; s < ptr; s++) {
  948. *s = tolower(*s);
  949. }
  950. if ((s = strchr(ptr, '-'))) {
  951. *s = '\0';
  952. }
  953. }
  954. if (ast_test_flag(flags, OPTION_NAME)) {
  955. const char *local_context = S_OR(name_context, "default");
  956. const char *local_mailbox = S_OR(mailbox, ptr);
  957. if (local_mailbox) {
  958. res = spy_sayname(chan, local_mailbox, local_context);
  959. } else {
  960. res = -1;
  961. }
  962. }
  963. if (!ast_test_flag(flags, OPTION_NAME) || res < 0) {
  964. int num;
  965. if (!ast_test_flag(flags, OPTION_NOTECH)) {
  966. if (ast_fileexists(peer_name, NULL, NULL) > 0) {
  967. res = ast_streamfile(chan, peer_name, ast_channel_language(chan));
  968. if (!res) {
  969. res = ast_waitstream(chan, "");
  970. }
  971. if (res) {
  972. ast_autochan_destroy(autochan);
  973. break;
  974. }
  975. } else {
  976. res = ast_say_character_str(chan, peer_name, "", ast_channel_language(chan), AST_SAY_CASE_NONE);
  977. }
  978. }
  979. if (ptr && (num = atoi(ptr))) {
  980. ast_say_digits(chan, num, "", ast_channel_language(chan));
  981. }
  982. }
  983. }
  984. res = channel_spy(chan, autochan, &volfactor, fd, user_options, flags, exitcontext);
  985. num_spyed_upon++;
  986. if (res == -1) {
  987. ast_autochan_destroy(autochan);
  988. iter = ast_channel_iterator_destroy(iter);
  989. goto exit;
  990. } else if (res == -2) {
  991. res = 0;
  992. ast_autochan_destroy(autochan);
  993. iter = ast_channel_iterator_destroy(iter);
  994. goto exit;
  995. } else if (res > 1 && spec && !ast_test_flag(flags, OPTION_UNIQUEID)) {
  996. struct ast_channel *next;
  997. snprintf(nameprefix, AST_NAME_STRLEN, "%s/%d", spec, res);
  998. if ((next = ast_channel_get_by_name_prefix(nameprefix, strlen(nameprefix)))) {
  999. next_autochan = ast_autochan_setup(next);
  1000. next = ast_channel_unref(next);
  1001. } else {
  1002. /* stay on this channel, if it is still valid */
  1003. if (!ast_check_hangup(autochan->chan)) {
  1004. next_autochan = ast_autochan_setup(autochan->chan);
  1005. } else {
  1006. /* the channel is gone */
  1007. next_autochan = NULL;
  1008. }
  1009. }
  1010. } else if (res == 0 && ast_test_flag(flags, OPTION_EXITONHANGUP)) {
  1011. ast_autochan_destroy(autochan);
  1012. iter = ast_channel_iterator_destroy(iter);
  1013. goto exit;
  1014. }
  1015. }
  1016. iter = ast_channel_iterator_destroy(iter);
  1017. if (res == -1 || ast_check_hangup(chan))
  1018. break;
  1019. if (ast_test_flag(flags, OPTION_STOP) && !next_autochan) {
  1020. break;
  1021. }
  1022. }
  1023. exit:
  1024. ast_clear_flag(ast_channel_flags(chan), AST_FLAG_SPYING);
  1025. ast_channel_setoption(chan, AST_OPTION_TXGAIN, &zero_volume, sizeof(zero_volume), 0);
  1026. return res;
  1027. }
  1028. static int chanspy_exec(struct ast_channel *chan, const char *data)
  1029. {
  1030. char *myenforced = NULL;
  1031. char *mygroup = NULL;
  1032. char *recbase = NULL;
  1033. int fd = 0;
  1034. struct ast_flags flags;
  1035. struct spy_dtmf_options user_options = {
  1036. .cycle = '*',
  1037. .volume = '#',
  1038. .exit = '\0',
  1039. };
  1040. RAII_VAR(struct ast_format *, oldwf, NULL, ao2_cleanup);
  1041. int volfactor = 0;
  1042. int res;
  1043. char *mailbox = NULL;
  1044. char *name_context = NULL;
  1045. AST_DECLARE_APP_ARGS(args,
  1046. AST_APP_ARG(spec);
  1047. AST_APP_ARG(options);
  1048. );
  1049. char *opts[OPT_ARG_ARRAY_SIZE];
  1050. char *parse = ast_strdupa(data);
  1051. AST_STANDARD_APP_ARGS(args, parse);
  1052. if (args.spec && !strcmp(args.spec, "all"))
  1053. args.spec = NULL;
  1054. if (args.options) {
  1055. char tmp;
  1056. ast_app_parse_options(spy_opts, &flags, opts, args.options);
  1057. if (ast_test_flag(&flags, OPTION_GROUP))
  1058. mygroup = opts[OPT_ARG_GROUP];
  1059. if (ast_test_flag(&flags, OPTION_RECORD) &&
  1060. !(recbase = opts[OPT_ARG_RECORD]))
  1061. recbase = "chanspy";
  1062. if (ast_test_flag(&flags, OPTION_DTMF_EXIT) && opts[OPT_ARG_EXIT]) {
  1063. tmp = opts[OPT_ARG_EXIT][0];
  1064. if (strchr("0123456789*#", tmp) && tmp != '\0') {
  1065. user_options.exit = tmp;
  1066. } else {
  1067. ast_log(LOG_NOTICE, "Argument for option 'x' must be a valid DTMF digit.\n");
  1068. }
  1069. }
  1070. if (ast_test_flag(&flags, OPTION_DTMF_CYCLE) && opts[OPT_ARG_CYCLE]) {
  1071. tmp = opts[OPT_ARG_CYCLE][0];
  1072. if (strchr("0123456789*#", tmp) && tmp != '\0') {
  1073. user_options.cycle = tmp;
  1074. } else {
  1075. ast_log(LOG_NOTICE, "Argument for option 'c' must be a valid DTMF digit.\n");
  1076. }
  1077. }
  1078. if (ast_test_flag(&flags, OPTION_VOLUME) && opts[OPT_ARG_VOLUME]) {
  1079. int vol;
  1080. if ((sscanf(opts[OPT_ARG_VOLUME], "%30d", &vol) != 1) || (vol > 4) || (vol < -4))
  1081. ast_log(LOG_NOTICE, "Volume factor must be a number between -4 and 4\n");
  1082. else
  1083. volfactor = vol;
  1084. }
  1085. if (ast_test_flag(&flags, OPTION_PRIVATE))
  1086. ast_set_flag(&flags, OPTION_WHISPER);
  1087. if (ast_test_flag(&flags, OPTION_ENFORCED))
  1088. myenforced = opts[OPT_ARG_ENFORCED];
  1089. if (ast_test_flag(&flags, OPTION_NAME)) {
  1090. if (!ast_strlen_zero(opts[OPT_ARG_NAME])) {
  1091. char *delimiter;
  1092. if ((delimiter = strchr(opts[OPT_ARG_NAME], '@'))) {
  1093. mailbox = opts[OPT_ARG_NAME];
  1094. *delimiter++ = '\0';
  1095. name_context = delimiter;
  1096. } else {
  1097. mailbox = opts[OPT_ARG_NAME];
  1098. }
  1099. }
  1100. }
  1101. } else {
  1102. ast_clear_flag(&flags, AST_FLAGS_ALL);
  1103. }
  1104. oldwf = ao2_bump(ast_channel_writeformat(chan));
  1105. if (ast_set_write_format(chan, ast_format_slin) < 0) {
  1106. ast_log(LOG_ERROR, "Could Not Set Write Format.\n");
  1107. return -1;
  1108. }
  1109. if (recbase) {
  1110. char filename[PATH_MAX];
  1111. snprintf(filename, sizeof(filename), "%s/%s.%d.raw", ast_config_AST_MONITOR_DIR, recbase, (int) time(NULL));
  1112. if ((fd = open(filename, O_CREAT | O_WRONLY | O_TRUNC, AST_FILE_MODE)) <= 0) {
  1113. ast_log(LOG_WARNING, "Cannot open '%s' for recording\n", filename);
  1114. fd = 0;
  1115. }
  1116. }
  1117. res = common_exec(chan, &flags, volfactor, fd, &user_options, mygroup, myenforced, args.spec, NULL, NULL, mailbox, name_context);
  1118. if (fd)
  1119. close(fd);
  1120. if (oldwf && ast_set_write_format(chan, oldwf) < 0)
  1121. ast_log(LOG_ERROR, "Could Not Set Write Format.\n");
  1122. if (ast_test_flag(&flags, OPTION_EXITONHANGUP)) {
  1123. ast_verb(3, "Stopped spying due to the spied-on channel hanging up.\n");
  1124. }
  1125. return res;
  1126. }
  1127. static int extenspy_exec(struct ast_channel *chan, const char *data)
  1128. {
  1129. char *ptr, *exten = NULL;
  1130. char *mygroup = NULL;
  1131. char *recbase = NULL;
  1132. int fd = 0;
  1133. struct ast_flags flags;
  1134. struct spy_dtmf_options user_options = {
  1135. .cycle = '*',
  1136. .volume = '#',
  1137. .exit = '\0',
  1138. };
  1139. RAII_VAR(struct ast_format *, oldwf, NULL, ao2_cleanup);
  1140. int volfactor = 0;
  1141. int res;
  1142. char *mailbox = NULL;
  1143. char *name_context = NULL;
  1144. AST_DECLARE_APP_ARGS(args,
  1145. AST_APP_ARG(context);
  1146. AST_APP_ARG(options);
  1147. );
  1148. char *parse = ast_strdupa(data);
  1149. AST_STANDARD_APP_ARGS(args, parse);
  1150. if (!ast_strlen_zero(args.context) && (ptr = strchr(args.context, '@'))) {
  1151. exten = args.context;
  1152. *ptr++ = '\0';
  1153. args.context = ptr;
  1154. }
  1155. if (ast_strlen_zero(args.context))
  1156. args.context = ast_strdupa(ast_channel_context(chan));
  1157. if (args.options) {
  1158. char *opts[OPT_ARG_ARRAY_SIZE];
  1159. char tmp;
  1160. ast_app_parse_options(spy_opts, &flags, opts, args.options);
  1161. if (ast_test_flag(&flags, OPTION_GROUP))
  1162. mygroup = opts[OPT_ARG_GROUP];
  1163. if (ast_test_flag(&flags, OPTION_RECORD) &&
  1164. !(recbase = opts[OPT_ARG_RECORD]))
  1165. recbase = "chanspy";
  1166. if (ast_test_flag(&flags, OPTION_DTMF_EXIT) && opts[OPT_ARG_EXIT]) {
  1167. tmp = opts[OPT_ARG_EXIT][0];
  1168. if (strchr("0123456789*#", tmp) && tmp != '\0') {
  1169. user_options.exit = tmp;
  1170. } else {
  1171. ast_log(LOG_NOTICE, "Argument for option 'x' must be a valid DTMF digit.\n");
  1172. }
  1173. }
  1174. if (ast_test_flag(&flags, OPTION_DTMF_CYCLE) && opts[OPT_ARG_CYCLE]) {
  1175. tmp = opts[OPT_ARG_CYCLE][0];
  1176. if (strchr("0123456789*#", tmp) && tmp != '\0') {
  1177. user_options.cycle = tmp;
  1178. } else {
  1179. ast_log(LOG_NOTICE, "Argument for option 'c' must be a valid DTMF digit.\n");
  1180. }
  1181. }
  1182. if (ast_test_flag(&flags, OPTION_VOLUME) && opts[OPT_ARG_VOLUME]) {
  1183. int vol;
  1184. if ((sscanf(opts[OPT_ARG_VOLUME], "%30d", &vol) != 1) || (vol > 4) || (vol < -4))
  1185. ast_log(LOG_NOTICE, "Volume factor must be a number between -4 and 4\n");
  1186. else
  1187. volfactor = vol;
  1188. }
  1189. if (ast_test_flag(&flags, OPTION_PRIVATE))
  1190. ast_set_flag(&flags, OPTION_WHISPER);
  1191. if (ast_test_flag(&flags, OPTION_NAME)) {
  1192. if (!ast_strlen_zero(opts[OPT_ARG_NAME])) {
  1193. char *delimiter;
  1194. if ((delimiter = strchr(opts[OPT_ARG_NAME], '@'))) {
  1195. mailbox = opts[OPT_ARG_NAME];
  1196. *delimiter++ = '\0';
  1197. name_context = delimiter;
  1198. } else {
  1199. mailbox = opts[OPT_ARG_NAME];
  1200. }
  1201. }
  1202. }
  1203. } else {
  1204. /* Coverity - This uninit_use should be ignored since this macro initializes the flags */
  1205. ast_clear_flag(&flags, AST_FLAGS_ALL);
  1206. }
  1207. oldwf = ao2_bump(ast_channel_writeformat(chan));
  1208. if (ast_set_write_format(chan, ast_format_slin) < 0) {
  1209. ast_log(LOG_ERROR, "Could Not Set Write Format.\n");
  1210. return -1;
  1211. }
  1212. if (recbase) {
  1213. char filename[PATH_MAX];
  1214. snprintf(filename, sizeof(filename), "%s/%s.%d.raw", ast_config_AST_MONITOR_DIR, recbase, (int) time(NULL));
  1215. if ((fd = open(filename, O_CREAT | O_WRONLY | O_TRUNC, AST_FILE_MODE)) <= 0) {
  1216. ast_log(LOG_WARNING, "Cannot open '%s' for recording\n", filename);
  1217. fd = 0;
  1218. }
  1219. }
  1220. res = common_exec(chan, &flags, volfactor, fd, &user_options, mygroup, NULL, NULL, exten, args.context, mailbox, name_context);
  1221. if (fd)
  1222. close(fd);
  1223. if (oldwf && ast_set_write_format(chan, oldwf) < 0)
  1224. ast_log(LOG_ERROR, "Could Not Set Write Format.\n");
  1225. return res;
  1226. }
  1227. static int dahdiscan_exec(struct ast_channel *chan, const char *data)
  1228. {
  1229. const char *spec = "DAHDI";
  1230. struct ast_flags flags;
  1231. struct spy_dtmf_options user_options = {
  1232. .cycle = '#',
  1233. .volume = '\0',
  1234. .exit = '*',
  1235. };
  1236. struct ast_format *oldwf;
  1237. int res;
  1238. char *mygroup = NULL;
  1239. /* Coverity - This uninit_use should be ignored since this macro initializes the flags */
  1240. ast_clear_flag(&flags, AST_FLAGS_ALL);
  1241. if (!ast_strlen_zero(data)) {
  1242. mygroup = ast_strdupa(data);
  1243. }
  1244. ast_set_flag(&flags, OPTION_DTMF_EXIT);
  1245. ast_set_flag(&flags, OPTION_DTMF_CYCLE);
  1246. ast_set_flag(&flags, OPTION_DAHDI_SCAN);
  1247. oldwf = ao2_bump(ast_channel_writeformat(chan));
  1248. if (ast_set_write_format(chan, ast_format_slin) < 0) {
  1249. ast_log(LOG_ERROR, "Could Not Set Write Format.\n");
  1250. ao2_cleanup(oldwf);
  1251. return -1;
  1252. }
  1253. res = common_exec(chan, &flags, 0, 0, &user_options, mygroup, NULL, spec, NULL, NULL, NULL, NULL);
  1254. if (oldwf && ast_set_write_format(chan, oldwf) < 0)
  1255. ast_log(LOG_ERROR, "Could Not Set Write Format.\n");
  1256. ao2_cleanup(oldwf);
  1257. return res;
  1258. }
  1259. static int unload_module(void)
  1260. {
  1261. int res = 0;
  1262. res |= ast_unregister_application(app_chan);
  1263. res |= ast_unregister_application(app_ext);
  1264. res |= ast_unregister_application(app_dahdiscan);
  1265. return res;
  1266. }
  1267. static int load_module(void)
  1268. {
  1269. int res = 0;
  1270. res |= ast_register_application_xml(app_chan, chanspy_exec);
  1271. res |= ast_register_application_xml(app_ext, extenspy_exec);
  1272. res |= ast_register_application_xml(app_dahdiscan, dahdiscan_exec);
  1273. return res;
  1274. }
  1275. AST_MODULE_INFO_STANDARD(ASTERISK_GPL_KEY, "Listen to the audio of an active channel");