app_chanspy.c 47 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486
  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. int res;
  473. ast_autochan_channel_lock(autochan);
  474. ast_log(LOG_NOTICE, "Attaching %s to %s\n", spychan_name, ast_channel_name(autochan->chan));
  475. ast_set_flag(audiohook, AST_AUDIOHOOK_TRIGGER_SYNC | AST_AUDIOHOOK_SMALL_QUEUE);
  476. res = ast_audiohook_attach(autochan->chan, audiohook);
  477. ast_autochan_channel_unlock(autochan);
  478. return res;
  479. }
  480. static void change_spy_mode(const char digit, struct ast_flags *flags)
  481. {
  482. if (digit == '4') {
  483. ast_clear_flag(flags, OPTION_WHISPER);
  484. ast_clear_flag(flags, OPTION_BARGE);
  485. } else if (digit == '5') {
  486. ast_clear_flag(flags, OPTION_BARGE);
  487. ast_set_flag(flags, OPTION_WHISPER);
  488. } else if (digit == '6') {
  489. ast_clear_flag(flags, OPTION_WHISPER);
  490. ast_set_flag(flags, OPTION_BARGE);
  491. }
  492. }
  493. static int pack_channel_into_message(struct ast_channel *chan, const char *role,
  494. struct ast_multi_channel_blob *payload)
  495. {
  496. RAII_VAR(struct ast_channel_snapshot *, snapshot,
  497. ast_channel_snapshot_get_latest(ast_channel_uniqueid(chan)),
  498. ao2_cleanup);
  499. if (!snapshot) {
  500. return -1;
  501. }
  502. ast_multi_channel_blob_add_channel(payload, role, snapshot);
  503. return 0;
  504. }
  505. /*! \internal
  506. * \brief Publish the chanspy message over Stasis-Core
  507. * \param spyer The channel doing the spying
  508. * \param spyee Who is being spied upon
  509. * \start start If non-zero, the spying is starting. Otherwise, the spyer is
  510. * finishing
  511. */
  512. static void publish_chanspy_message(struct ast_channel *spyer,
  513. struct ast_channel *spyee,
  514. int start)
  515. {
  516. RAII_VAR(struct ast_json *, blob, NULL, ast_json_unref);
  517. RAII_VAR(struct ast_multi_channel_blob *, payload, NULL, ao2_cleanup);
  518. RAII_VAR(struct stasis_message *, message, NULL, ao2_cleanup);
  519. struct stasis_message_type *type = start ? ast_channel_chanspy_start_type(): ast_channel_chanspy_stop_type();
  520. if (!spyer) {
  521. ast_log(AST_LOG_WARNING, "Attempt to publish ChanSpy message for NULL spyer channel\n");
  522. return;
  523. }
  524. blob = ast_json_null();
  525. if (!blob || !type) {
  526. return;
  527. }
  528. payload = ast_multi_channel_blob_create(blob);
  529. if (!payload) {
  530. return;
  531. }
  532. if (pack_channel_into_message(spyer, "spyer_channel", payload)) {
  533. return;
  534. }
  535. if (spyee) {
  536. if (pack_channel_into_message(spyee, "spyee_channel", payload)) {
  537. return;
  538. }
  539. }
  540. message = stasis_message_create(type, payload);
  541. if (!message) {
  542. return;
  543. }
  544. stasis_publish(ast_channel_topic(spyer), message);
  545. }
  546. static int attach_barge(struct ast_autochan *spyee_autochan,
  547. struct ast_autochan **spyee_bridge_autochan, struct ast_audiohook *bridge_whisper_audiohook,
  548. const char *spyer_name, const char *name)
  549. {
  550. int retval = 0;
  551. struct ast_autochan *internal_bridge_autochan;
  552. struct ast_channel *spyee_chan;
  553. RAII_VAR(struct ast_channel *, bridged, NULL, ast_channel_cleanup);
  554. ast_autochan_channel_lock(spyee_autochan);
  555. spyee_chan = ast_channel_ref(spyee_autochan->chan);
  556. ast_autochan_channel_unlock(spyee_autochan);
  557. bridged = ast_channel_bridge_peer(spyee_chan);
  558. ast_channel_unref(spyee_chan);
  559. if (!bridged) {
  560. return -1;
  561. }
  562. ast_audiohook_init(bridge_whisper_audiohook, AST_AUDIOHOOK_TYPE_WHISPER, "Chanspy", 0);
  563. internal_bridge_autochan = ast_autochan_setup(bridged);
  564. if (!internal_bridge_autochan) {
  565. return -1;
  566. }
  567. if (start_spying(internal_bridge_autochan, spyer_name, bridge_whisper_audiohook)) {
  568. ast_log(LOG_WARNING, "Unable to attach barge audiohook on spyee '%s'. Barge mode disabled.\n", name);
  569. retval = -1;
  570. }
  571. *spyee_bridge_autochan = internal_bridge_autochan;
  572. return retval;
  573. }
  574. static int channel_spy(struct ast_channel *chan, struct ast_autochan *spyee_autochan,
  575. int *volfactor, int fd, struct spy_dtmf_options *user_options, struct ast_flags *flags,
  576. char *exitcontext)
  577. {
  578. struct chanspy_translation_helper csth;
  579. int running = 0, bridge_connected = 0, res, x = 0;
  580. char inp[24] = {0};
  581. char *name;
  582. struct ast_frame *f;
  583. struct ast_silence_generator *silgen = NULL;
  584. struct ast_autochan *spyee_bridge_autochan = NULL;
  585. const char *spyer_name;
  586. ast_channel_lock(chan);
  587. if (ast_check_hangup(chan)) {
  588. ast_channel_unlock(chan);
  589. return 0;
  590. }
  591. spyer_name = ast_strdupa(ast_channel_name(chan));
  592. ast_channel_unlock(chan);
  593. ast_autochan_channel_lock(spyee_autochan);
  594. if (ast_check_hangup(spyee_autochan->chan)
  595. || ast_test_flag(ast_channel_flags(spyee_autochan->chan), AST_FLAG_ZOMBIE)) {
  596. ast_autochan_channel_unlock(spyee_autochan);
  597. return 0;
  598. }
  599. name = ast_strdupa(ast_channel_name(spyee_autochan->chan));
  600. ast_verb(2, "Spying on channel %s\n", name);
  601. publish_chanspy_message(chan, spyee_autochan->chan, 1);
  602. ast_autochan_channel_unlock(spyee_autochan);
  603. memset(&csth, 0, sizeof(csth));
  604. ast_copy_flags(&csth.flags, flags, AST_FLAGS_ALL);
  605. /* This is the audiohook which gives us the audio off the channel we are
  606. spying on.
  607. */
  608. ast_audiohook_init(&csth.spy_audiohook, AST_AUDIOHOOK_TYPE_SPY, "ChanSpy", 0);
  609. if (start_spying(spyee_autochan, spyer_name, &csth.spy_audiohook)) {
  610. ast_audiohook_destroy(&csth.spy_audiohook);
  611. return 0;
  612. }
  613. if (ast_test_flag(flags, OPTION_WHISPER | OPTION_BARGE | OPTION_DTMF_SWITCH_MODES)) {
  614. /* This audiohook will let us inject audio from our channel into the
  615. channel we are currently spying on.
  616. */
  617. ast_audiohook_init(&csth.whisper_audiohook, AST_AUDIOHOOK_TYPE_WHISPER, "ChanSpy", 0);
  618. if (start_spying(spyee_autochan, spyer_name, &csth.whisper_audiohook)) {
  619. ast_log(LOG_WARNING, "Unable to attach whisper audiohook to spyee %s. Whisper mode disabled!\n", name);
  620. }
  621. }
  622. ast_channel_set_flag(chan, AST_FLAG_END_DTMF_ONLY);
  623. csth.volfactor = *volfactor;
  624. if (csth.volfactor) {
  625. csth.spy_audiohook.options.read_volume = csth.volfactor;
  626. csth.spy_audiohook.options.write_volume = csth.volfactor;
  627. }
  628. csth.fd = fd;
  629. if (ast_test_flag(flags, OPTION_PRIVATE))
  630. silgen = ast_channel_start_silence_generator(chan);
  631. else
  632. ast_activate_generator(chan, &spygen, &csth);
  633. /* We can no longer rely on 'spyee' being an actual channel;
  634. it can be hung up and freed out from under us. However, the
  635. channel destructor will put NULL into our csth.spy.chan
  636. field when that happens, so that is our signal that the spyee
  637. channel has gone away.
  638. */
  639. /* Note: it is very important that the ast_waitfor() be the first
  640. condition in this expression, so that if we wait for some period
  641. of time before receiving a frame from our spying channel, we check
  642. for hangup on the spied-on channel _after_ knowing that a frame
  643. has arrived, since the spied-on channel could have gone away while
  644. we were waiting
  645. */
  646. while (ast_waitfor(chan, -1) > -1 && csth.spy_audiohook.status == AST_AUDIOHOOK_STATUS_RUNNING) {
  647. if (!(f = ast_read(chan)) || ast_check_hangup(chan)) {
  648. running = -1;
  649. if (f) {
  650. ast_frfree(f);
  651. }
  652. break;
  653. }
  654. if (ast_test_flag(flags, OPTION_BARGE) && f->frametype == AST_FRAME_VOICE) {
  655. /* This hook lets us inject audio into the channel that the spyee is currently
  656. * bridged with. If the spyee isn't bridged with anything yet, nothing will
  657. * be attached and we'll need to continue attempting to attach the barge
  658. * audio hook. */
  659. if (!bridge_connected && attach_barge(spyee_autochan, &spyee_bridge_autochan,
  660. &csth.bridge_whisper_audiohook, spyer_name, name) == 0) {
  661. bridge_connected = 1;
  662. }
  663. ast_audiohook_lock(&csth.whisper_audiohook);
  664. ast_audiohook_write_frame(&csth.whisper_audiohook, AST_AUDIOHOOK_DIRECTION_WRITE, f);
  665. ast_audiohook_unlock(&csth.whisper_audiohook);
  666. if (bridge_connected) {
  667. ast_audiohook_lock(&csth.bridge_whisper_audiohook);
  668. ast_audiohook_write_frame(&csth.bridge_whisper_audiohook, AST_AUDIOHOOK_DIRECTION_WRITE, f);
  669. ast_audiohook_unlock(&csth.bridge_whisper_audiohook);
  670. }
  671. ast_frfree(f);
  672. continue;
  673. } else if (ast_test_flag(flags, OPTION_WHISPER) && f->frametype == AST_FRAME_VOICE) {
  674. ast_audiohook_lock(&csth.whisper_audiohook);
  675. ast_audiohook_write_frame(&csth.whisper_audiohook, AST_AUDIOHOOK_DIRECTION_WRITE, f);
  676. ast_audiohook_unlock(&csth.whisper_audiohook);
  677. ast_frfree(f);
  678. continue;
  679. }
  680. res = (f->frametype == AST_FRAME_DTMF) ? f->subclass.integer : 0;
  681. ast_frfree(f);
  682. if (!res)
  683. continue;
  684. if (x == sizeof(inp))
  685. x = 0;
  686. if (res < 0) {
  687. running = -1;
  688. break;
  689. }
  690. if (ast_test_flag(flags, OPTION_EXIT)) {
  691. char tmp[2];
  692. tmp[0] = res;
  693. tmp[1] = '\0';
  694. if (!ast_goto_if_exists(chan, exitcontext, tmp, 1)) {
  695. ast_debug(1, "Got DTMF %c, goto context %s\n", tmp[0], exitcontext);
  696. pbx_builtin_setvar_helper(chan, "SPY_CHANNEL", name);
  697. running = -2;
  698. break;
  699. } else {
  700. ast_debug(2, "Exit by single digit did not work in chanspy. Extension %s does not exist in context %s\n", tmp, exitcontext);
  701. }
  702. } else if (res >= '0' && res <= '9') {
  703. if (ast_test_flag(flags, OPTION_DTMF_SWITCH_MODES)) {
  704. change_spy_mode(res, flags);
  705. } else {
  706. inp[x++] = res;
  707. }
  708. }
  709. if (res == user_options->cycle) {
  710. running = 0;
  711. break;
  712. } else if (res == user_options->exit) {
  713. running = -2;
  714. break;
  715. } else if (res == user_options->volume) {
  716. if (!ast_strlen_zero(inp)) {
  717. running = atoi(inp);
  718. break;
  719. }
  720. (*volfactor)++;
  721. if (*volfactor > 4)
  722. *volfactor = -4;
  723. ast_verb(3, "Setting spy volume on %s to %d\n", ast_channel_name(chan), *volfactor);
  724. csth.volfactor = *volfactor;
  725. csth.spy_audiohook.options.read_volume = csth.volfactor;
  726. csth.spy_audiohook.options.write_volume = csth.volfactor;
  727. }
  728. }
  729. if (ast_test_flag(flags, OPTION_PRIVATE))
  730. ast_channel_stop_silence_generator(chan, silgen);
  731. else
  732. ast_deactivate_generator(chan);
  733. ast_channel_clear_flag(chan, AST_FLAG_END_DTMF_ONLY);
  734. if (ast_test_flag(flags, OPTION_WHISPER | OPTION_BARGE | OPTION_DTMF_SWITCH_MODES)) {
  735. ast_audiohook_lock(&csth.whisper_audiohook);
  736. ast_audiohook_detach(&csth.whisper_audiohook);
  737. ast_audiohook_unlock(&csth.whisper_audiohook);
  738. ast_audiohook_destroy(&csth.whisper_audiohook);
  739. }
  740. if (ast_test_flag(flags, OPTION_BARGE | OPTION_DTMF_SWITCH_MODES)) {
  741. ast_audiohook_lock(&csth.bridge_whisper_audiohook);
  742. ast_audiohook_detach(&csth.bridge_whisper_audiohook);
  743. ast_audiohook_unlock(&csth.bridge_whisper_audiohook);
  744. ast_audiohook_destroy(&csth.bridge_whisper_audiohook);
  745. }
  746. ast_audiohook_lock(&csth.spy_audiohook);
  747. ast_audiohook_detach(&csth.spy_audiohook);
  748. ast_audiohook_unlock(&csth.spy_audiohook);
  749. ast_audiohook_destroy(&csth.spy_audiohook);
  750. if (spyee_bridge_autochan) {
  751. ast_autochan_destroy(spyee_bridge_autochan);
  752. }
  753. ast_verb(2, "Done Spying on channel %s\n", name);
  754. publish_chanspy_message(chan, NULL, 0);
  755. return running;
  756. }
  757. static struct ast_autochan *next_channel(struct ast_channel_iterator *iter,
  758. struct ast_channel *chan)
  759. {
  760. struct ast_channel *next;
  761. struct ast_autochan *autochan_store;
  762. const size_t pseudo_len = strlen("DAHDI/pseudo");
  763. if (!iter) {
  764. return NULL;
  765. }
  766. for (; (next = ast_channel_iterator_next(iter)); ast_channel_unref(next)) {
  767. if (!strncmp(ast_channel_name(next), "DAHDI/pseudo", pseudo_len)
  768. || next == chan) {
  769. continue;
  770. }
  771. autochan_store = ast_autochan_setup(next);
  772. ast_channel_unref(next);
  773. return autochan_store;
  774. }
  775. return NULL;
  776. }
  777. static int spy_sayname(struct ast_channel *chan, const char *mailbox, const char *context)
  778. {
  779. char *mailbox_id;
  780. mailbox_id = ast_alloca(strlen(mailbox) + strlen(context) + 2);
  781. sprintf(mailbox_id, "%s@%s", mailbox, context); /* Safe */
  782. return ast_app_sayname(chan, mailbox_id);
  783. }
  784. static int common_exec(struct ast_channel *chan, struct ast_flags *flags,
  785. int volfactor, const int fd, struct spy_dtmf_options *user_options,
  786. const char *mygroup, const char *myenforced, const char *spec, const char *exten,
  787. const char *context, const char *mailbox, const char *name_context)
  788. {
  789. char nameprefix[AST_NAME_STRLEN];
  790. char exitcontext[AST_MAX_CONTEXT] = "";
  791. signed char zero_volume = 0;
  792. int waitms;
  793. int res;
  794. int num_spyed_upon = 1;
  795. struct ast_channel_iterator *iter = NULL;
  796. if (ast_test_flag(flags, OPTION_EXIT)) {
  797. const char *c;
  798. ast_channel_lock(chan);
  799. if ((c = pbx_builtin_getvar_helper(chan, "SPY_EXIT_CONTEXT"))) {
  800. ast_copy_string(exitcontext, c, sizeof(exitcontext));
  801. } else if (!ast_strlen_zero(ast_channel_macrocontext(chan))) {
  802. ast_copy_string(exitcontext, ast_channel_macrocontext(chan), sizeof(exitcontext));
  803. } else {
  804. ast_copy_string(exitcontext, ast_channel_context(chan), sizeof(exitcontext));
  805. }
  806. ast_channel_unlock(chan);
  807. }
  808. if (ast_channel_state(chan) != AST_STATE_UP)
  809. ast_answer(chan);
  810. ast_channel_set_flag(chan, AST_FLAG_SPYING);
  811. waitms = 100;
  812. for (;;) {
  813. struct ast_autochan *autochan = NULL, *next_autochan = NULL;
  814. struct ast_channel *prev = NULL;
  815. if (!ast_test_flag(flags, OPTION_QUIET) && num_spyed_upon) {
  816. res = ast_streamfile(chan, "beep", ast_channel_language(chan));
  817. if (!res)
  818. res = ast_waitstream(chan, "");
  819. else if (res < 0) {
  820. ast_channel_clear_flag(chan, AST_FLAG_SPYING);
  821. break;
  822. }
  823. if (!ast_strlen_zero(exitcontext)) {
  824. char tmp[2];
  825. tmp[0] = res;
  826. tmp[1] = '\0';
  827. if (!ast_goto_if_exists(chan, exitcontext, tmp, 1))
  828. goto exit;
  829. else
  830. ast_debug(2, "Exit by single digit did not work in chanspy. Extension %s does not exist in context %s\n", tmp, exitcontext);
  831. }
  832. }
  833. /* Set up the iterator we'll be using during this call */
  834. if (!ast_strlen_zero(spec)) {
  835. if (ast_test_flag(flags, OPTION_UNIQUEID)) {
  836. struct ast_channel *unique_chan;
  837. unique_chan = ast_channel_get_by_name(spec);
  838. if (!unique_chan) {
  839. res = -1;
  840. goto exit;
  841. }
  842. iter = ast_channel_iterator_by_name_new(ast_channel_name(unique_chan), 0);
  843. ast_channel_unref(unique_chan);
  844. } else {
  845. iter = ast_channel_iterator_by_name_new(spec, strlen(spec));
  846. }
  847. } else if (!ast_strlen_zero(exten)) {
  848. iter = ast_channel_iterator_by_exten_new(exten, context);
  849. } else {
  850. iter = ast_channel_iterator_all_new();
  851. }
  852. if (!iter) {
  853. res = -1;
  854. goto exit;
  855. }
  856. res = ast_waitfordigit(chan, waitms);
  857. if (res < 0) {
  858. iter = ast_channel_iterator_destroy(iter);
  859. ast_channel_clear_flag(chan, AST_FLAG_SPYING);
  860. break;
  861. }
  862. if (!ast_strlen_zero(exitcontext)) {
  863. char tmp[2];
  864. tmp[0] = res;
  865. tmp[1] = '\0';
  866. if (!ast_goto_if_exists(chan, exitcontext, tmp, 1)) {
  867. iter = ast_channel_iterator_destroy(iter);
  868. goto exit;
  869. } else {
  870. ast_debug(2, "Exit by single digit did not work in chanspy. Extension %s does not exist in context %s\n", tmp, exitcontext);
  871. }
  872. }
  873. /* reset for the next loop around, unless overridden later */
  874. waitms = 100;
  875. num_spyed_upon = 0;
  876. for (autochan = next_channel(iter, chan);
  877. autochan;
  878. prev = autochan->chan,
  879. ast_autochan_destroy(autochan),
  880. autochan = next_autochan ?: next_channel(iter, chan),
  881. next_autochan = NULL) {
  882. int igrp = !mygroup;
  883. int ienf = !myenforced;
  884. if (autochan->chan == prev) {
  885. ast_autochan_destroy(autochan);
  886. break;
  887. }
  888. if (ast_check_hangup(chan)) {
  889. ast_autochan_destroy(autochan);
  890. break;
  891. }
  892. ast_autochan_channel_lock(autochan);
  893. if (ast_test_flag(flags, OPTION_BRIDGED)
  894. && !ast_channel_is_bridged(autochan->chan)) {
  895. ast_autochan_channel_unlock(autochan);
  896. continue;
  897. }
  898. if (ast_check_hangup(autochan->chan)
  899. || ast_test_flag(ast_channel_flags(autochan->chan), AST_FLAG_SPYING)) {
  900. ast_autochan_channel_unlock(autochan);
  901. continue;
  902. }
  903. ast_autochan_channel_unlock(autochan);
  904. if (mygroup) {
  905. int num_groups = 0;
  906. int num_mygroups = 0;
  907. char dup_group[512];
  908. char dup_mygroup[512];
  909. char *groups[NUM_SPYGROUPS];
  910. char *mygroups[NUM_SPYGROUPS];
  911. const char *group = NULL;
  912. int x;
  913. int y;
  914. ast_copy_string(dup_mygroup, mygroup, sizeof(dup_mygroup));
  915. num_mygroups = ast_app_separate_args(dup_mygroup, ':', mygroups,
  916. ARRAY_LEN(mygroups));
  917. /* Before dahdi scan was part of chanspy, it would use the "GROUP" variable
  918. * rather than "SPYGROUP", this check is done to preserve expected behavior */
  919. ast_autochan_channel_lock(autochan);
  920. if (ast_test_flag(flags, OPTION_DAHDI_SCAN)) {
  921. group = pbx_builtin_getvar_helper(autochan->chan, "GROUP");
  922. } else {
  923. group = pbx_builtin_getvar_helper(autochan->chan, "SPYGROUP");
  924. }
  925. ast_autochan_channel_unlock(autochan);
  926. if (!ast_strlen_zero(group)) {
  927. ast_copy_string(dup_group, group, sizeof(dup_group));
  928. num_groups = ast_app_separate_args(dup_group, ':', groups,
  929. ARRAY_LEN(groups));
  930. }
  931. for (y = 0; y < num_mygroups; y++) {
  932. for (x = 0; x < num_groups; x++) {
  933. if (!strcmp(mygroups[y], groups[x])) {
  934. igrp = 1;
  935. break;
  936. }
  937. }
  938. }
  939. }
  940. if (!igrp) {
  941. continue;
  942. }
  943. if (myenforced) {
  944. char ext[AST_CHANNEL_NAME + 3];
  945. char buffer[512];
  946. char *end;
  947. snprintf(buffer, sizeof(buffer) - 1, ":%s:", myenforced);
  948. ast_autochan_channel_lock(autochan);
  949. ast_copy_string(ext + 1, ast_channel_name(autochan->chan), sizeof(ext) - 1);
  950. ast_autochan_channel_unlock(autochan);
  951. if ((end = strchr(ext, '-'))) {
  952. *end++ = ':';
  953. *end = '\0';
  954. }
  955. ext[0] = ':';
  956. if (strcasestr(buffer, ext)) {
  957. ienf = 1;
  958. }
  959. }
  960. if (!ienf) {
  961. continue;
  962. }
  963. if (!ast_test_flag(flags, OPTION_QUIET)) {
  964. char peer_name[AST_NAME_STRLEN + 5];
  965. char *ptr, *s;
  966. strcpy(peer_name, "spy-");
  967. ast_autochan_channel_lock(autochan);
  968. strncat(peer_name, ast_channel_name(autochan->chan), AST_NAME_STRLEN - 4 - 1);
  969. ast_autochan_channel_unlock(autochan);
  970. if ((ptr = strchr(peer_name, '/'))) {
  971. *ptr++ = '\0';
  972. for (s = peer_name; s < ptr; s++) {
  973. *s = tolower(*s);
  974. }
  975. if ((s = strchr(ptr, '-'))) {
  976. *s = '\0';
  977. }
  978. }
  979. if (ast_test_flag(flags, OPTION_NAME)) {
  980. const char *local_context = S_OR(name_context, "default");
  981. const char *local_mailbox = S_OR(mailbox, ptr);
  982. if (local_mailbox) {
  983. res = spy_sayname(chan, local_mailbox, local_context);
  984. } else {
  985. res = -1;
  986. }
  987. }
  988. if (!ast_test_flag(flags, OPTION_NAME) || res < 0) {
  989. int num;
  990. if (!ast_test_flag(flags, OPTION_NOTECH)) {
  991. if (ast_fileexists(peer_name, NULL, NULL) > 0) {
  992. res = ast_streamfile(chan, peer_name, ast_channel_language(chan));
  993. if (!res) {
  994. res = ast_waitstream(chan, "");
  995. }
  996. if (res) {
  997. ast_autochan_destroy(autochan);
  998. break;
  999. }
  1000. } else {
  1001. res = ast_say_character_str(chan, peer_name, "", ast_channel_language(chan), AST_SAY_CASE_NONE);
  1002. }
  1003. }
  1004. if (ptr && (num = atoi(ptr))) {
  1005. ast_say_digits(chan, num, "", ast_channel_language(chan));
  1006. }
  1007. }
  1008. }
  1009. res = channel_spy(chan, autochan, &volfactor, fd, user_options, flags, exitcontext);
  1010. num_spyed_upon++;
  1011. if (res == -1) {
  1012. ast_autochan_destroy(autochan);
  1013. iter = ast_channel_iterator_destroy(iter);
  1014. goto exit;
  1015. } else if (res == -2) {
  1016. res = 0;
  1017. ast_autochan_destroy(autochan);
  1018. iter = ast_channel_iterator_destroy(iter);
  1019. goto exit;
  1020. } else if (res > 1 && spec && !ast_test_flag(flags, OPTION_UNIQUEID)) {
  1021. struct ast_channel *next;
  1022. snprintf(nameprefix, AST_NAME_STRLEN, "%s/%d", spec, res);
  1023. if ((next = ast_channel_get_by_name_prefix(nameprefix, strlen(nameprefix)))) {
  1024. next_autochan = ast_autochan_setup(next);
  1025. next = ast_channel_unref(next);
  1026. } else {
  1027. /* stay on this channel, if it is still valid */
  1028. ast_autochan_channel_lock(autochan);
  1029. if (!ast_check_hangup(autochan->chan)) {
  1030. next_autochan = ast_autochan_setup(autochan->chan);
  1031. } else {
  1032. /* the channel is gone */
  1033. next_autochan = NULL;
  1034. }
  1035. ast_autochan_channel_unlock(autochan);
  1036. }
  1037. } else if (res == 0 && ast_test_flag(flags, OPTION_EXITONHANGUP)) {
  1038. ast_autochan_destroy(autochan);
  1039. iter = ast_channel_iterator_destroy(iter);
  1040. goto exit;
  1041. }
  1042. }
  1043. iter = ast_channel_iterator_destroy(iter);
  1044. if (res == -1 || ast_check_hangup(chan))
  1045. break;
  1046. if (ast_test_flag(flags, OPTION_STOP) && !next_autochan) {
  1047. break;
  1048. }
  1049. }
  1050. exit:
  1051. ast_channel_clear_flag(chan, AST_FLAG_SPYING);
  1052. ast_channel_setoption(chan, AST_OPTION_TXGAIN, &zero_volume, sizeof(zero_volume), 0);
  1053. return res;
  1054. }
  1055. static int chanspy_exec(struct ast_channel *chan, const char *data)
  1056. {
  1057. char *myenforced = NULL;
  1058. char *mygroup = NULL;
  1059. char *recbase = NULL;
  1060. int fd = 0;
  1061. struct ast_flags flags;
  1062. struct spy_dtmf_options user_options = {
  1063. .cycle = '*',
  1064. .volume = '#',
  1065. .exit = '\0',
  1066. };
  1067. RAII_VAR(struct ast_format *, oldwf, NULL, ao2_cleanup);
  1068. int volfactor = 0;
  1069. int res;
  1070. char *mailbox = NULL;
  1071. char *name_context = NULL;
  1072. AST_DECLARE_APP_ARGS(args,
  1073. AST_APP_ARG(spec);
  1074. AST_APP_ARG(options);
  1075. );
  1076. char *opts[OPT_ARG_ARRAY_SIZE];
  1077. char *parse = ast_strdupa(data);
  1078. AST_STANDARD_APP_ARGS(args, parse);
  1079. if (args.spec && !strcmp(args.spec, "all"))
  1080. args.spec = NULL;
  1081. if (args.options) {
  1082. char tmp;
  1083. ast_app_parse_options(spy_opts, &flags, opts, args.options);
  1084. if (ast_test_flag(&flags, OPTION_GROUP))
  1085. mygroup = opts[OPT_ARG_GROUP];
  1086. if (ast_test_flag(&flags, OPTION_RECORD) &&
  1087. !(recbase = opts[OPT_ARG_RECORD]))
  1088. recbase = "chanspy";
  1089. if (ast_test_flag(&flags, OPTION_DTMF_EXIT) && opts[OPT_ARG_EXIT]) {
  1090. tmp = opts[OPT_ARG_EXIT][0];
  1091. if (strchr("0123456789*#", tmp) && tmp != '\0') {
  1092. user_options.exit = tmp;
  1093. } else {
  1094. ast_log(LOG_NOTICE, "Argument for option 'x' must be a valid DTMF digit.\n");
  1095. }
  1096. }
  1097. if (ast_test_flag(&flags, OPTION_DTMF_CYCLE) && opts[OPT_ARG_CYCLE]) {
  1098. tmp = opts[OPT_ARG_CYCLE][0];
  1099. if (strchr("0123456789*#", tmp) && tmp != '\0') {
  1100. user_options.cycle = tmp;
  1101. } else {
  1102. ast_log(LOG_NOTICE, "Argument for option 'c' must be a valid DTMF digit.\n");
  1103. }
  1104. }
  1105. if (ast_test_flag(&flags, OPTION_VOLUME) && opts[OPT_ARG_VOLUME]) {
  1106. int vol;
  1107. if ((sscanf(opts[OPT_ARG_VOLUME], "%30d", &vol) != 1) || (vol > 4) || (vol < -4))
  1108. ast_log(LOG_NOTICE, "Volume factor must be a number between -4 and 4\n");
  1109. else
  1110. volfactor = vol;
  1111. }
  1112. if (ast_test_flag(&flags, OPTION_PRIVATE))
  1113. ast_set_flag(&flags, OPTION_WHISPER);
  1114. if (ast_test_flag(&flags, OPTION_ENFORCED))
  1115. myenforced = opts[OPT_ARG_ENFORCED];
  1116. if (ast_test_flag(&flags, OPTION_NAME)) {
  1117. if (!ast_strlen_zero(opts[OPT_ARG_NAME])) {
  1118. char *delimiter;
  1119. if ((delimiter = strchr(opts[OPT_ARG_NAME], '@'))) {
  1120. mailbox = opts[OPT_ARG_NAME];
  1121. *delimiter++ = '\0';
  1122. name_context = delimiter;
  1123. } else {
  1124. mailbox = opts[OPT_ARG_NAME];
  1125. }
  1126. }
  1127. }
  1128. } else {
  1129. ast_clear_flag(&flags, AST_FLAGS_ALL);
  1130. }
  1131. oldwf = ao2_bump(ast_channel_writeformat(chan));
  1132. if (ast_set_write_format(chan, ast_format_slin) < 0) {
  1133. ast_log(LOG_ERROR, "Could Not Set Write Format.\n");
  1134. return -1;
  1135. }
  1136. if (recbase) {
  1137. char filename[PATH_MAX];
  1138. snprintf(filename, sizeof(filename), "%s/%s.%d.raw", ast_config_AST_MONITOR_DIR, recbase, (int) time(NULL));
  1139. if ((fd = open(filename, O_CREAT | O_WRONLY | O_TRUNC, AST_FILE_MODE)) <= 0) {
  1140. ast_log(LOG_WARNING, "Cannot open '%s' for recording\n", filename);
  1141. fd = 0;
  1142. }
  1143. }
  1144. res = common_exec(chan, &flags, volfactor, fd, &user_options, mygroup, myenforced, args.spec, NULL, NULL, mailbox, name_context);
  1145. if (fd)
  1146. close(fd);
  1147. if (oldwf && ast_set_write_format(chan, oldwf) < 0)
  1148. ast_log(LOG_ERROR, "Could Not Set Write Format.\n");
  1149. if (ast_test_flag(&flags, OPTION_EXITONHANGUP)) {
  1150. ast_verb(3, "Stopped spying due to the spied-on channel hanging up.\n");
  1151. }
  1152. return res;
  1153. }
  1154. static int extenspy_exec(struct ast_channel *chan, const char *data)
  1155. {
  1156. char *ptr, *exten = NULL;
  1157. char *mygroup = NULL;
  1158. char *recbase = NULL;
  1159. int fd = 0;
  1160. struct ast_flags flags;
  1161. struct spy_dtmf_options user_options = {
  1162. .cycle = '*',
  1163. .volume = '#',
  1164. .exit = '\0',
  1165. };
  1166. RAII_VAR(struct ast_format *, oldwf, NULL, ao2_cleanup);
  1167. int volfactor = 0;
  1168. int res;
  1169. char *mailbox = NULL;
  1170. char *name_context = NULL;
  1171. AST_DECLARE_APP_ARGS(args,
  1172. AST_APP_ARG(context);
  1173. AST_APP_ARG(options);
  1174. );
  1175. char *parse = ast_strdupa(data);
  1176. AST_STANDARD_APP_ARGS(args, parse);
  1177. if (!ast_strlen_zero(args.context) && (ptr = strchr(args.context, '@'))) {
  1178. exten = args.context;
  1179. *ptr++ = '\0';
  1180. args.context = ptr;
  1181. }
  1182. if (ast_strlen_zero(args.context))
  1183. args.context = ast_strdupa(ast_channel_context(chan));
  1184. if (args.options) {
  1185. char *opts[OPT_ARG_ARRAY_SIZE];
  1186. char tmp;
  1187. ast_app_parse_options(spy_opts, &flags, opts, args.options);
  1188. if (ast_test_flag(&flags, OPTION_GROUP))
  1189. mygroup = opts[OPT_ARG_GROUP];
  1190. if (ast_test_flag(&flags, OPTION_RECORD) &&
  1191. !(recbase = opts[OPT_ARG_RECORD]))
  1192. recbase = "chanspy";
  1193. if (ast_test_flag(&flags, OPTION_DTMF_EXIT) && opts[OPT_ARG_EXIT]) {
  1194. tmp = opts[OPT_ARG_EXIT][0];
  1195. if (strchr("0123456789*#", tmp) && tmp != '\0') {
  1196. user_options.exit = tmp;
  1197. } else {
  1198. ast_log(LOG_NOTICE, "Argument for option 'x' must be a valid DTMF digit.\n");
  1199. }
  1200. }
  1201. if (ast_test_flag(&flags, OPTION_DTMF_CYCLE) && opts[OPT_ARG_CYCLE]) {
  1202. tmp = opts[OPT_ARG_CYCLE][0];
  1203. if (strchr("0123456789*#", tmp) && tmp != '\0') {
  1204. user_options.cycle = tmp;
  1205. } else {
  1206. ast_log(LOG_NOTICE, "Argument for option 'c' must be a valid DTMF digit.\n");
  1207. }
  1208. }
  1209. if (ast_test_flag(&flags, OPTION_VOLUME) && opts[OPT_ARG_VOLUME]) {
  1210. int vol;
  1211. if ((sscanf(opts[OPT_ARG_VOLUME], "%30d", &vol) != 1) || (vol > 4) || (vol < -4))
  1212. ast_log(LOG_NOTICE, "Volume factor must be a number between -4 and 4\n");
  1213. else
  1214. volfactor = vol;
  1215. }
  1216. if (ast_test_flag(&flags, OPTION_PRIVATE))
  1217. ast_set_flag(&flags, OPTION_WHISPER);
  1218. if (ast_test_flag(&flags, OPTION_NAME)) {
  1219. if (!ast_strlen_zero(opts[OPT_ARG_NAME])) {
  1220. char *delimiter;
  1221. if ((delimiter = strchr(opts[OPT_ARG_NAME], '@'))) {
  1222. mailbox = opts[OPT_ARG_NAME];
  1223. *delimiter++ = '\0';
  1224. name_context = delimiter;
  1225. } else {
  1226. mailbox = opts[OPT_ARG_NAME];
  1227. }
  1228. }
  1229. }
  1230. } else {
  1231. /* Coverity - This uninit_use should be ignored since this macro initializes the flags */
  1232. ast_clear_flag(&flags, AST_FLAGS_ALL);
  1233. }
  1234. oldwf = ao2_bump(ast_channel_writeformat(chan));
  1235. if (ast_set_write_format(chan, ast_format_slin) < 0) {
  1236. ast_log(LOG_ERROR, "Could Not Set Write Format.\n");
  1237. return -1;
  1238. }
  1239. if (recbase) {
  1240. char filename[PATH_MAX];
  1241. snprintf(filename, sizeof(filename), "%s/%s.%d.raw", ast_config_AST_MONITOR_DIR, recbase, (int) time(NULL));
  1242. if ((fd = open(filename, O_CREAT | O_WRONLY | O_TRUNC, AST_FILE_MODE)) <= 0) {
  1243. ast_log(LOG_WARNING, "Cannot open '%s' for recording\n", filename);
  1244. fd = 0;
  1245. }
  1246. }
  1247. res = common_exec(chan, &flags, volfactor, fd, &user_options, mygroup, NULL, NULL, exten, args.context, mailbox, name_context);
  1248. if (fd)
  1249. close(fd);
  1250. if (oldwf && ast_set_write_format(chan, oldwf) < 0)
  1251. ast_log(LOG_ERROR, "Could Not Set Write Format.\n");
  1252. return res;
  1253. }
  1254. static int dahdiscan_exec(struct ast_channel *chan, const char *data)
  1255. {
  1256. const char *spec = "DAHDI";
  1257. struct ast_flags flags = {0};
  1258. struct spy_dtmf_options user_options = {
  1259. .cycle = '#',
  1260. .volume = '\0',
  1261. .exit = '*',
  1262. };
  1263. struct ast_format *oldwf;
  1264. int res;
  1265. char *mygroup = NULL;
  1266. /* Coverity - This uninit_use should be ignored since this macro initializes the flags */
  1267. ast_clear_flag(&flags, AST_FLAGS_ALL);
  1268. if (!ast_strlen_zero(data)) {
  1269. mygroup = ast_strdupa(data);
  1270. }
  1271. ast_set_flag(&flags, OPTION_DTMF_EXIT);
  1272. ast_set_flag(&flags, OPTION_DTMF_CYCLE);
  1273. ast_set_flag(&flags, OPTION_DAHDI_SCAN);
  1274. oldwf = ao2_bump(ast_channel_writeformat(chan));
  1275. if (ast_set_write_format(chan, ast_format_slin) < 0) {
  1276. ast_log(LOG_ERROR, "Could Not Set Write Format.\n");
  1277. ao2_cleanup(oldwf);
  1278. return -1;
  1279. }
  1280. res = common_exec(chan, &flags, 0, 0, &user_options, mygroup, NULL, spec, NULL, NULL, NULL, NULL);
  1281. if (oldwf && ast_set_write_format(chan, oldwf) < 0)
  1282. ast_log(LOG_ERROR, "Could Not Set Write Format.\n");
  1283. ao2_cleanup(oldwf);
  1284. return res;
  1285. }
  1286. static int unload_module(void)
  1287. {
  1288. int res = 0;
  1289. res |= ast_unregister_application(app_chan);
  1290. res |= ast_unregister_application(app_ext);
  1291. res |= ast_unregister_application(app_dahdiscan);
  1292. return res;
  1293. }
  1294. static int load_module(void)
  1295. {
  1296. int res = 0;
  1297. res |= ast_register_application_xml(app_chan, chanspy_exec);
  1298. res |= ast_register_application_xml(app_ext, extenspy_exec);
  1299. res |= ast_register_application_xml(app_dahdiscan, dahdiscan_exec);
  1300. return res;
  1301. }
  1302. AST_MODULE_INFO_STANDARD(ASTERISK_GPL_KEY, "Listen to the audio of an active channel");