app_chanspy.c 41 KB

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