resource_channels.c 39 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461
  1. /*
  2. * Asterisk -- An open source telephony toolkit.
  3. *
  4. * Copyright (C) 2012 - 2013, Digium, Inc.
  5. *
  6. * David M. Lee, II <dlee@digium.com>
  7. *
  8. * See http://www.asterisk.org for more information about
  9. * the Asterisk project. Please do not directly contact
  10. * any of the maintainers of this project for assistance;
  11. * the project provides a web site, mailing lists and IRC
  12. * channels for your use.
  13. *
  14. * This program is free software, distributed under the terms of
  15. * the GNU General Public License Version 2. See the LICENSE file
  16. * at the top of the source tree.
  17. */
  18. /*! \file
  19. *
  20. * \brief Implementation for ARI stubs.
  21. *
  22. * \author David M. Lee, II <dlee@digium.com>
  23. */
  24. /*** MODULEINFO
  25. <depend type="module">res_stasis_app_playback</depend>
  26. <support_level>core</support_level>
  27. ***/
  28. #include "asterisk.h"
  29. ASTERISK_FILE_VERSION(__FILE__, "$Revision$")
  30. #include "asterisk/file.h"
  31. #include "asterisk/pbx.h"
  32. #include "asterisk/bridge.h"
  33. #include "asterisk/callerid.h"
  34. #include "asterisk/stasis_app.h"
  35. #include "asterisk/stasis_app_playback.h"
  36. #include "asterisk/stasis_app_recording.h"
  37. #include "asterisk/stasis_app_snoop.h"
  38. #include "asterisk/stasis_channels.h"
  39. #include "asterisk/causes.h"
  40. #include "asterisk/format_cache.h"
  41. #include "asterisk/core_local.h"
  42. #include "asterisk/dial.h"
  43. #include "resource_channels.h"
  44. #include <limits.h>
  45. /*!
  46. * \brief Finds the control object for a channel, filling the response with an
  47. * error, if appropriate.
  48. * \param[out] response Response to fill with an error if control is not found.
  49. * \param channel_id ID of the channel to lookup.
  50. * \return Channel control object.
  51. * \return \c NULL if control object does not exist.
  52. */
  53. static struct stasis_app_control *find_control(
  54. struct ast_ari_response *response,
  55. const char *channel_id)
  56. {
  57. RAII_VAR(struct stasis_app_control *, control, NULL, ao2_cleanup);
  58. ast_assert(response != NULL);
  59. control = stasis_app_control_find_by_channel_id(channel_id);
  60. if (control == NULL) {
  61. /* Distinguish between 404 and 409 errors */
  62. RAII_VAR(struct ast_channel *, chan, NULL, ao2_cleanup);
  63. chan = ast_channel_get_by_name(channel_id);
  64. if (chan == NULL) {
  65. ast_ari_response_error(response, 404, "Not Found",
  66. "Channel not found");
  67. return NULL;
  68. }
  69. ast_ari_response_error(response, 409, "Conflict",
  70. "Channel not in Stasis application");
  71. return NULL;
  72. }
  73. ao2_ref(control, +1);
  74. return control;
  75. }
  76. void ast_ari_channels_continue_in_dialplan(
  77. struct ast_variable *headers,
  78. struct ast_ari_channels_continue_in_dialplan_args *args,
  79. struct ast_ari_response *response)
  80. {
  81. RAII_VAR(struct stasis_app_control *, control, NULL, ao2_cleanup);
  82. RAII_VAR(struct ast_channel_snapshot *, snapshot, NULL, ao2_cleanup);
  83. int ipri;
  84. const char *context;
  85. const char *exten;
  86. ast_assert(response != NULL);
  87. control = find_control(response, args->channel_id);
  88. if (control == NULL) {
  89. return;
  90. }
  91. snapshot = stasis_app_control_get_snapshot(control);
  92. if (!snapshot) {
  93. return;
  94. }
  95. if (ast_strlen_zero(args->context)) {
  96. context = snapshot->context;
  97. exten = S_OR(args->extension, snapshot->exten);
  98. } else {
  99. context = args->context;
  100. exten = S_OR(args->extension, "s");
  101. }
  102. if (!ast_strlen_zero(args->label)) {
  103. /* A label was provided in the request, use that */
  104. if (sscanf(args->label, "%30d", &ipri) != 1) {
  105. ipri = ast_findlabel_extension(NULL, context, exten, args->label, NULL);
  106. if (ipri == -1) {
  107. ast_log(AST_LOG_ERROR, "Requested label: %s can not be found in context: %s\n", args->label, context);
  108. ast_ari_response_error(response, 404, "Not Found", "Requested label can not be found");
  109. return;
  110. }
  111. } else {
  112. ast_debug(3, "Numeric value provided for label, jumping to that priority\n");
  113. }
  114. if (ipri == 0) {
  115. ast_log(AST_LOG_ERROR, "Invalid priority label '%s' specified for extension %s in context: %s\n",
  116. args->label, exten, context);
  117. ast_ari_response_error(response, 400, "Bad Request", "Requested priority is illegal");
  118. return;
  119. }
  120. } else if (args->priority) {
  121. /* No label provided, use provided priority */
  122. ipri = args->priority;
  123. } else if (ast_strlen_zero(args->context) && ast_strlen_zero(args->extension)) {
  124. /* Special case. No exten, context, or priority provided, then move on to the next priority */
  125. ipri = snapshot->priority + 1;
  126. } else {
  127. ipri = 1;
  128. }
  129. if (stasis_app_control_continue(control, context, exten, ipri)) {
  130. ast_ari_response_alloc_failed(response);
  131. return;
  132. }
  133. ast_ari_response_no_content(response);
  134. }
  135. void ast_ari_channels_redirect(struct ast_variable *headers,
  136. struct ast_ari_channels_redirect_args *args,
  137. struct ast_ari_response *response)
  138. {
  139. RAII_VAR(struct stasis_app_control *, control, NULL, ao2_cleanup);
  140. RAII_VAR(struct ast_channel_snapshot *, chan_snapshot, NULL, ao2_cleanup);
  141. char *tech;
  142. char *resource;
  143. int tech_len;
  144. control = find_control(response, args->channel_id);
  145. if (!control) {
  146. return;
  147. }
  148. if (ast_strlen_zero(args->endpoint)) {
  149. ast_ari_response_error(response, 400, "Not Found",
  150. "Required parameter 'endpoint' not provided.");
  151. return;
  152. }
  153. tech = ast_strdupa(args->endpoint);
  154. if (!(resource = strchr(tech, '/')) || !(tech_len = resource - tech)) {
  155. ast_ari_response_error(response, 422, "Unprocessable Entity",
  156. "Endpoint parameter '%s' does not contain tech/resource", args->endpoint);
  157. return;
  158. }
  159. *resource++ = '\0';
  160. if (ast_strlen_zero(resource)) {
  161. ast_ari_response_error(response, 422, "Unprocessable Entity",
  162. "No resource provided in endpoint parameter '%s'", args->endpoint);
  163. return;
  164. }
  165. chan_snapshot = ast_channel_snapshot_get_latest(args->channel_id);
  166. if (!chan_snapshot) {
  167. ast_ari_response_error(response, 500, "Internal Server Error",
  168. "Unable to find channel snapshot for '%s'", args->channel_id);
  169. return;
  170. }
  171. if (strncasecmp(chan_snapshot->type, tech, tech_len)) {
  172. ast_ari_response_error(response, 422, "Unprocessable Entity",
  173. "Endpoint technology '%s' does not match channel technology '%s'",
  174. tech, chan_snapshot->type);
  175. return;
  176. }
  177. if (stasis_app_control_redirect(control, resource)) {
  178. ast_ari_response_error(response, 500, "Internal Server Error",
  179. "Failed to redirect channel");
  180. return;
  181. }
  182. ast_ari_response_no_content(response);
  183. }
  184. void ast_ari_channels_answer(struct ast_variable *headers,
  185. struct ast_ari_channels_answer_args *args,
  186. struct ast_ari_response *response)
  187. {
  188. RAII_VAR(struct stasis_app_control *, control, NULL, ao2_cleanup);
  189. control = find_control(response, args->channel_id);
  190. if (control == NULL) {
  191. return;
  192. }
  193. if (stasis_app_control_answer(control) != 0) {
  194. ast_ari_response_error(
  195. response, 500, "Internal Server Error",
  196. "Failed to answer channel");
  197. return;
  198. }
  199. ast_ari_response_no_content(response);
  200. }
  201. void ast_ari_channels_ring(struct ast_variable *headers,
  202. struct ast_ari_channels_ring_args *args,
  203. struct ast_ari_response *response)
  204. {
  205. RAII_VAR(struct stasis_app_control *, control, NULL, ao2_cleanup);
  206. control = find_control(response, args->channel_id);
  207. if (control == NULL) {
  208. return;
  209. }
  210. stasis_app_control_ring(control);
  211. ast_ari_response_no_content(response);
  212. }
  213. void ast_ari_channels_ring_stop(struct ast_variable *headers,
  214. struct ast_ari_channels_ring_stop_args *args,
  215. struct ast_ari_response *response)
  216. {
  217. RAII_VAR(struct stasis_app_control *, control, NULL, ao2_cleanup);
  218. control = find_control(response, args->channel_id);
  219. if (control == NULL) {
  220. return;
  221. }
  222. stasis_app_control_ring_stop(control);
  223. ast_ari_response_no_content(response);
  224. }
  225. void ast_ari_channels_mute(struct ast_variable *headers,
  226. struct ast_ari_channels_mute_args *args,
  227. struct ast_ari_response *response)
  228. {
  229. RAII_VAR(struct stasis_app_control *, control, NULL, ao2_cleanup);
  230. unsigned int direction = 0;
  231. enum ast_frame_type frametype = AST_FRAME_VOICE;
  232. control = find_control(response, args->channel_id);
  233. if (control == NULL) {
  234. return;
  235. }
  236. if (ast_strlen_zero(args->direction)) {
  237. ast_ari_response_error(
  238. response, 400, "Bad Request",
  239. "Direction is required");
  240. return;
  241. }
  242. if (!strcmp(args->direction, "in")) {
  243. direction = AST_MUTE_DIRECTION_READ;
  244. } else if (!strcmp(args->direction, "out")) {
  245. direction = AST_MUTE_DIRECTION_WRITE;
  246. } else if (!strcmp(args->direction, "both")) {
  247. direction = AST_MUTE_DIRECTION_READ | AST_MUTE_DIRECTION_WRITE;
  248. } else {
  249. ast_ari_response_error(
  250. response, 400, "Bad Request",
  251. "Invalid direction specified");
  252. return;
  253. }
  254. stasis_app_control_mute(control, direction, frametype);
  255. ast_ari_response_no_content(response);
  256. }
  257. void ast_ari_channels_unmute(struct ast_variable *headers,
  258. struct ast_ari_channels_unmute_args *args,
  259. struct ast_ari_response *response)
  260. {
  261. RAII_VAR(struct stasis_app_control *, control, NULL, ao2_cleanup);
  262. unsigned int direction = 0;
  263. enum ast_frame_type frametype = AST_FRAME_VOICE;
  264. control = find_control(response, args->channel_id);
  265. if (control == NULL) {
  266. return;
  267. }
  268. if (ast_strlen_zero(args->direction)) {
  269. ast_ari_response_error(
  270. response, 400, "Bad Request",
  271. "Direction is required");
  272. return;
  273. }
  274. if (!strcmp(args->direction, "in")) {
  275. direction = AST_MUTE_DIRECTION_READ;
  276. } else if (!strcmp(args->direction, "out")) {
  277. direction = AST_MUTE_DIRECTION_WRITE;
  278. } else if (!strcmp(args->direction, "both")) {
  279. direction = AST_MUTE_DIRECTION_READ | AST_MUTE_DIRECTION_WRITE;
  280. } else {
  281. ast_ari_response_error(
  282. response, 400, "Bad Request",
  283. "Invalid direction specified");
  284. return;
  285. }
  286. stasis_app_control_unmute(control, direction, frametype);
  287. ast_ari_response_no_content(response);
  288. }
  289. void ast_ari_channels_send_dtmf(struct ast_variable *headers,
  290. struct ast_ari_channels_send_dtmf_args *args,
  291. struct ast_ari_response *response)
  292. {
  293. RAII_VAR(struct stasis_app_control *, control, NULL, ao2_cleanup);
  294. control = find_control(response, args->channel_id);
  295. if (control == NULL) {
  296. return;
  297. }
  298. if (ast_strlen_zero(args->dtmf)) {
  299. ast_ari_response_error(
  300. response, 400, "Bad Request",
  301. "DTMF is required");
  302. return;
  303. }
  304. stasis_app_control_dtmf(control, args->dtmf, args->before, args->between, args->duration, args->after);
  305. ast_ari_response_no_content(response);
  306. }
  307. void ast_ari_channels_hold(struct ast_variable *headers,
  308. struct ast_ari_channels_hold_args *args,
  309. struct ast_ari_response *response)
  310. {
  311. RAII_VAR(struct stasis_app_control *, control, NULL, ao2_cleanup);
  312. control = find_control(response, args->channel_id);
  313. if (control == NULL) {
  314. /* Response filled in by find_control */
  315. return;
  316. }
  317. stasis_app_control_hold(control);
  318. ast_ari_response_no_content(response);
  319. }
  320. void ast_ari_channels_unhold(struct ast_variable *headers,
  321. struct ast_ari_channels_unhold_args *args,
  322. struct ast_ari_response *response)
  323. {
  324. RAII_VAR(struct stasis_app_control *, control, NULL, ao2_cleanup);
  325. control = find_control(response, args->channel_id);
  326. if (control == NULL) {
  327. /* Response filled in by find_control */
  328. return;
  329. }
  330. stasis_app_control_unhold(control);
  331. ast_ari_response_no_content(response);
  332. }
  333. void ast_ari_channels_start_moh(struct ast_variable *headers,
  334. struct ast_ari_channels_start_moh_args *args,
  335. struct ast_ari_response *response)
  336. {
  337. RAII_VAR(struct stasis_app_control *, control, NULL, ao2_cleanup);
  338. control = find_control(response, args->channel_id);
  339. if (control == NULL) {
  340. /* Response filled in by find_control */
  341. return;
  342. }
  343. stasis_app_control_moh_start(control, args->moh_class);
  344. ast_ari_response_no_content(response);
  345. }
  346. void ast_ari_channels_stop_moh(struct ast_variable *headers,
  347. struct ast_ari_channels_stop_moh_args *args,
  348. struct ast_ari_response *response)
  349. {
  350. RAII_VAR(struct stasis_app_control *, control, NULL, ao2_cleanup);
  351. control = find_control(response, args->channel_id);
  352. if (control == NULL) {
  353. /* Response filled in by find_control */
  354. return;
  355. }
  356. stasis_app_control_moh_stop(control);
  357. ast_ari_response_no_content(response);
  358. }
  359. void ast_ari_channels_start_silence(struct ast_variable *headers,
  360. struct ast_ari_channels_start_silence_args *args,
  361. struct ast_ari_response *response)
  362. {
  363. RAII_VAR(struct stasis_app_control *, control, NULL, ao2_cleanup);
  364. control = find_control(response, args->channel_id);
  365. if (control == NULL) {
  366. /* Response filled in by find_control */
  367. return;
  368. }
  369. stasis_app_control_silence_start(control);
  370. ast_ari_response_no_content(response);
  371. }
  372. void ast_ari_channels_stop_silence(struct ast_variable *headers,
  373. struct ast_ari_channels_stop_silence_args *args,
  374. struct ast_ari_response *response)
  375. {
  376. RAII_VAR(struct stasis_app_control *, control, NULL, ao2_cleanup);
  377. control = find_control(response, args->channel_id);
  378. if (control == NULL) {
  379. /* Response filled in by find_control */
  380. return;
  381. }
  382. stasis_app_control_silence_stop(control);
  383. ast_ari_response_no_content(response);
  384. }
  385. static void ari_channels_handle_play(
  386. const char *args_channel_id,
  387. const char *args_media,
  388. const char *args_lang,
  389. int args_offsetms,
  390. int args_skipms,
  391. const char *args_playback_id,
  392. struct ast_ari_response *response)
  393. {
  394. RAII_VAR(struct stasis_app_control *, control, NULL, ao2_cleanup);
  395. RAII_VAR(struct ast_channel_snapshot *, snapshot, NULL, ao2_cleanup);
  396. RAII_VAR(struct stasis_app_playback *, playback, NULL, ao2_cleanup);
  397. RAII_VAR(char *, playback_url, NULL, ast_free);
  398. struct ast_json *json;
  399. const char *language;
  400. ast_assert(response != NULL);
  401. control = find_control(response, args_channel_id);
  402. if (control == NULL) {
  403. /* Response filled in by find_control */
  404. return;
  405. }
  406. snapshot = stasis_app_control_get_snapshot(control);
  407. if (!snapshot) {
  408. ast_ari_response_error(
  409. response, 404, "Not Found",
  410. "Channel not found");
  411. return;
  412. }
  413. if (args_skipms < 0) {
  414. ast_ari_response_error(
  415. response, 400, "Bad Request",
  416. "skipms cannot be negative");
  417. return;
  418. }
  419. if (args_offsetms < 0) {
  420. ast_ari_response_error(
  421. response, 400, "Bad Request",
  422. "offsetms cannot be negative");
  423. return;
  424. }
  425. language = S_OR(args_lang, snapshot->language);
  426. playback = stasis_app_control_play_uri(control, args_media, language,
  427. args_channel_id, STASIS_PLAYBACK_TARGET_CHANNEL, args_skipms, args_offsetms, args_playback_id);
  428. if (!playback) {
  429. ast_ari_response_error(
  430. response, 500, "Internal Server Error",
  431. "Failed to queue media for playback");
  432. return;
  433. }
  434. if (ast_asprintf(&playback_url, "/playback/%s",
  435. stasis_app_playback_get_id(playback)) == -1) {
  436. playback_url = NULL;
  437. ast_ari_response_error(
  438. response, 500, "Internal Server Error",
  439. "Out of memory");
  440. return;
  441. }
  442. json = stasis_app_playback_to_json(playback);
  443. if (!json) {
  444. ast_ari_response_error(
  445. response, 500, "Internal Server Error",
  446. "Out of memory");
  447. return;
  448. }
  449. ast_ari_response_created(response, playback_url, json);
  450. }
  451. void ast_ari_channels_play(struct ast_variable *headers,
  452. struct ast_ari_channels_play_args *args,
  453. struct ast_ari_response *response)
  454. {
  455. ari_channels_handle_play(
  456. args->channel_id,
  457. args->media,
  458. args->lang,
  459. args->offsetms,
  460. args->skipms,
  461. args->playback_id,
  462. response);
  463. }
  464. void ast_ari_channels_play_with_id(struct ast_variable *headers,
  465. struct ast_ari_channels_play_with_id_args *args,
  466. struct ast_ari_response *response)
  467. {
  468. ari_channels_handle_play(
  469. args->channel_id,
  470. args->media,
  471. args->lang,
  472. args->offsetms,
  473. args->skipms,
  474. args->playback_id,
  475. response);
  476. }
  477. void ast_ari_channels_record(struct ast_variable *headers,
  478. struct ast_ari_channels_record_args *args,
  479. struct ast_ari_response *response)
  480. {
  481. RAII_VAR(struct stasis_app_control *, control, NULL, ao2_cleanup);
  482. RAII_VAR(struct stasis_app_recording *, recording, NULL, ao2_cleanup);
  483. RAII_VAR(char *, recording_url, NULL, ast_free);
  484. struct ast_json *json;
  485. RAII_VAR(struct stasis_app_recording_options *, options, NULL,
  486. ao2_cleanup);
  487. RAII_VAR(char *, uri_encoded_name, NULL, ast_free);
  488. size_t uri_name_maxlen;
  489. ast_assert(response != NULL);
  490. if (args->max_duration_seconds < 0) {
  491. ast_ari_response_error(
  492. response, 400, "Bad Request",
  493. "max_duration_seconds cannot be negative");
  494. return;
  495. }
  496. if (args->max_silence_seconds < 0) {
  497. ast_ari_response_error(
  498. response, 400, "Bad Request",
  499. "max_silence_seconds cannot be negative");
  500. return;
  501. }
  502. control = find_control(response, args->channel_id);
  503. if (control == NULL) {
  504. /* Response filled in by find_control */
  505. return;
  506. }
  507. options = stasis_app_recording_options_create(args->name, args->format);
  508. if (options == NULL) {
  509. ast_ari_response_error(
  510. response, 500, "Internal Server Error",
  511. "Out of memory");
  512. }
  513. ast_string_field_build(options, target, "channel:%s", args->channel_id);
  514. options->max_silence_seconds = args->max_silence_seconds;
  515. options->max_duration_seconds = args->max_duration_seconds;
  516. options->terminate_on =
  517. stasis_app_recording_termination_parse(args->terminate_on);
  518. options->if_exists =
  519. stasis_app_recording_if_exists_parse(args->if_exists);
  520. options->beep = args->beep;
  521. if (options->terminate_on == STASIS_APP_RECORDING_TERMINATE_INVALID) {
  522. ast_ari_response_error(
  523. response, 400, "Bad Request",
  524. "terminateOn invalid");
  525. return;
  526. }
  527. if (options->if_exists == AST_RECORD_IF_EXISTS_ERROR) {
  528. ast_ari_response_error(
  529. response, 400, "Bad Request",
  530. "ifExists invalid");
  531. return;
  532. }
  533. if (!ast_get_format_for_file_ext(options->format)) {
  534. ast_ari_response_error(
  535. response, 422, "Unprocessable Entity",
  536. "specified format is unknown on this system");
  537. return;
  538. }
  539. recording = stasis_app_control_record(control, options);
  540. if (recording == NULL) {
  541. switch(errno) {
  542. case EINVAL:
  543. /* While the arguments are invalid, we should have
  544. * caught them prior to calling record.
  545. */
  546. ast_ari_response_error(
  547. response, 500, "Internal Server Error",
  548. "Error parsing request");
  549. break;
  550. case EEXIST:
  551. ast_ari_response_error(response, 409, "Conflict",
  552. "Recording '%s' already exists and can not be overwritten",
  553. args->name);
  554. break;
  555. case ENOMEM:
  556. ast_ari_response_error(
  557. response, 500, "Internal Server Error",
  558. "Out of memory");
  559. break;
  560. case EPERM:
  561. ast_ari_response_error(
  562. response, 400, "Bad Request",
  563. "Recording name invalid");
  564. break;
  565. default:
  566. ast_log(LOG_WARNING,
  567. "Unrecognized recording error: %s\n",
  568. strerror(errno));
  569. ast_ari_response_error(
  570. response, 500, "Internal Server Error",
  571. "Internal Server Error");
  572. break;
  573. }
  574. return;
  575. }
  576. uri_name_maxlen = strlen(args->name) * 3;
  577. uri_encoded_name = ast_malloc(uri_name_maxlen);
  578. if (!uri_encoded_name) {
  579. ast_ari_response_error(
  580. response, 500, "Internal Server Error",
  581. "Out of memory");
  582. return;
  583. }
  584. ast_uri_encode(args->name, uri_encoded_name, uri_name_maxlen,
  585. ast_uri_http);
  586. if (ast_asprintf(&recording_url, "/recordings/live/%s",
  587. uri_encoded_name) == -1) {
  588. recording_url = NULL;
  589. ast_ari_response_error(
  590. response, 500, "Internal Server Error",
  591. "Out of memory");
  592. return;
  593. }
  594. json = stasis_app_recording_to_json(recording);
  595. if (!json) {
  596. ast_ari_response_error(
  597. response, 500, "Internal Server Error",
  598. "Out of memory");
  599. return;
  600. }
  601. ast_ari_response_created(response, recording_url, json);
  602. }
  603. void ast_ari_channels_get(struct ast_variable *headers,
  604. struct ast_ari_channels_get_args *args,
  605. struct ast_ari_response *response)
  606. {
  607. RAII_VAR(struct stasis_message *, msg, NULL, ao2_cleanup);
  608. struct stasis_cache *cache;
  609. struct ast_channel_snapshot *snapshot;
  610. cache = ast_channel_cache();
  611. if (!cache) {
  612. ast_ari_response_error(
  613. response, 500, "Internal Server Error",
  614. "Message bus not initialized");
  615. return;
  616. }
  617. msg = stasis_cache_get(cache, ast_channel_snapshot_type(),
  618. args->channel_id);
  619. if (!msg) {
  620. ast_ari_response_error(
  621. response, 404, "Not Found",
  622. "Channel not found");
  623. return;
  624. }
  625. snapshot = stasis_message_data(msg);
  626. ast_assert(snapshot != NULL);
  627. ast_ari_response_ok(response,
  628. ast_channel_snapshot_to_json(snapshot, NULL));
  629. }
  630. void ast_ari_channels_hangup(struct ast_variable *headers,
  631. struct ast_ari_channels_hangup_args *args,
  632. struct ast_ari_response *response)
  633. {
  634. RAII_VAR(struct ast_channel *, chan, NULL, ao2_cleanup);
  635. int cause;
  636. chan = ast_channel_get_by_name(args->channel_id);
  637. if (chan == NULL) {
  638. ast_ari_response_error(
  639. response, 404, "Not Found",
  640. "Channel not found");
  641. return;
  642. }
  643. if (ast_strlen_zero(args->reason) || !strcmp(args->reason, "normal")) {
  644. cause = AST_CAUSE_NORMAL;
  645. } else if (!strcmp(args->reason, "busy")) {
  646. cause = AST_CAUSE_BUSY;
  647. } else if (!strcmp(args->reason, "congestion")) {
  648. cause = AST_CAUSE_CONGESTION;
  649. } else if (!strcmp(args->reason, "no_answer")) {
  650. cause = AST_CAUSE_NOANSWER;
  651. } else {
  652. ast_ari_response_error(
  653. response, 400, "Invalid Reason",
  654. "Invalid reason for hangup provided");
  655. return;
  656. }
  657. ast_channel_hangupcause_set(chan, cause);
  658. ast_softhangup(chan, AST_SOFTHANGUP_EXPLICIT);
  659. ast_ari_response_no_content(response);
  660. }
  661. void ast_ari_channels_list(struct ast_variable *headers,
  662. struct ast_ari_channels_list_args *args,
  663. struct ast_ari_response *response)
  664. {
  665. RAII_VAR(struct stasis_cache *, cache, NULL, ao2_cleanup);
  666. RAII_VAR(struct ao2_container *, snapshots, NULL, ao2_cleanup);
  667. RAII_VAR(struct ast_json *, json, NULL, ast_json_unref);
  668. struct ao2_iterator i;
  669. void *obj;
  670. struct stasis_message_sanitizer *sanitize = stasis_app_get_sanitizer();
  671. cache = ast_channel_cache();
  672. if (!cache) {
  673. ast_ari_response_error(
  674. response, 500, "Internal Server Error",
  675. "Message bus not initialized");
  676. return;
  677. }
  678. ao2_ref(cache, +1);
  679. snapshots = stasis_cache_dump(cache, ast_channel_snapshot_type());
  680. if (!snapshots) {
  681. ast_ari_response_alloc_failed(response);
  682. return;
  683. }
  684. json = ast_json_array_create();
  685. if (!json) {
  686. ast_ari_response_alloc_failed(response);
  687. return;
  688. }
  689. i = ao2_iterator_init(snapshots, 0);
  690. while ((obj = ao2_iterator_next(&i))) {
  691. RAII_VAR(struct stasis_message *, msg, obj, ao2_cleanup);
  692. struct ast_channel_snapshot *snapshot = stasis_message_data(msg);
  693. int r;
  694. if (sanitize && sanitize->channel_snapshot
  695. && sanitize->channel_snapshot(snapshot)) {
  696. continue;
  697. }
  698. r = ast_json_array_append(
  699. json, ast_channel_snapshot_to_json(snapshot, NULL));
  700. if (r != 0) {
  701. ast_ari_response_alloc_failed(response);
  702. ao2_iterator_destroy(&i);
  703. return;
  704. }
  705. }
  706. ao2_iterator_destroy(&i);
  707. ast_ari_response_ok(response, ast_json_ref(json));
  708. }
  709. /*! \brief Structure used for origination */
  710. struct ari_origination {
  711. /*! \brief Dialplan context */
  712. char context[AST_MAX_CONTEXT];
  713. /*! \brief Dialplan extension */
  714. char exten[AST_MAX_EXTENSION];
  715. /*! \brief Dialplan priority */
  716. int priority;
  717. /*! \brief Application data to pass to Stasis application */
  718. char appdata[0];
  719. };
  720. /*! \brief Thread which dials and executes upon answer */
  721. static void *ari_originate_dial(void *data)
  722. {
  723. struct ast_dial *dial = data;
  724. struct ari_origination *origination = ast_dial_get_user_data(dial);
  725. enum ast_dial_result res;
  726. res = ast_dial_run(dial, NULL, 0);
  727. if (res != AST_DIAL_RESULT_ANSWERED) {
  728. goto end;
  729. }
  730. if (!ast_strlen_zero(origination->appdata)) {
  731. struct ast_app *app = pbx_findapp("Stasis");
  732. if (app) {
  733. ast_verb(4, "Launching Stasis(%s) on %s\n", origination->appdata,
  734. ast_channel_name(ast_dial_answered(dial)));
  735. pbx_exec(ast_dial_answered(dial), app, origination->appdata);
  736. } else {
  737. ast_log(LOG_WARNING, "No such application 'Stasis'\n");
  738. }
  739. } else {
  740. struct ast_channel *answered = ast_dial_answered(dial);
  741. if (!ast_strlen_zero(origination->context)) {
  742. ast_channel_context_set(answered, origination->context);
  743. }
  744. if (!ast_strlen_zero(origination->exten)) {
  745. ast_channel_exten_set(answered, origination->exten);
  746. }
  747. if (origination->priority > 0) {
  748. ast_channel_priority_set(answered, origination->priority);
  749. }
  750. if (ast_pbx_run(answered)) {
  751. ast_log(LOG_ERROR, "Failed to start PBX on %s\n", ast_channel_name(answered));
  752. } else {
  753. /* PBX will have taken care of hanging up, so we steal the answered channel so dial doesn't do it */
  754. ast_dial_answered_steal(dial);
  755. }
  756. }
  757. end:
  758. ast_dial_destroy(dial);
  759. ast_free(origination);
  760. return NULL;
  761. }
  762. static void ari_channels_handle_originate_with_id(const char *args_endpoint,
  763. const char *args_extension,
  764. const char *args_context,
  765. long args_priority,
  766. const char *args_label,
  767. const char *args_app,
  768. const char *args_app_args,
  769. const char *args_caller_id,
  770. int args_timeout,
  771. struct ast_variable *variables,
  772. const char *args_channel_id,
  773. const char *args_other_channel_id,
  774. const char *args_originator,
  775. struct ast_ari_response *response)
  776. {
  777. char *dialtech;
  778. char dialdevice[AST_CHANNEL_NAME];
  779. struct ast_dial *dial;
  780. char *caller_id = NULL;
  781. char *cid_num = NULL;
  782. char *cid_name = NULL;
  783. char *stuff;
  784. struct ast_channel *other = NULL;
  785. struct ast_channel *chan = NULL;
  786. RAII_VAR(struct ast_channel_snapshot *, snapshot, NULL, ao2_cleanup);
  787. struct ast_assigned_ids assignedids = {
  788. .uniqueid = args_channel_id,
  789. .uniqueid2 = args_other_channel_id,
  790. };
  791. struct ari_origination *origination;
  792. pthread_t thread;
  793. if ((assignedids.uniqueid && AST_MAX_PUBLIC_UNIQUEID < strlen(assignedids.uniqueid))
  794. || (assignedids.uniqueid2 && AST_MAX_PUBLIC_UNIQUEID < strlen(assignedids.uniqueid2))) {
  795. ast_ari_response_error(response, 400, "Bad Request",
  796. "Uniqueid length exceeds maximum of %d", AST_MAX_PUBLIC_UNIQUEID);
  797. return;
  798. }
  799. if (ast_strlen_zero(args_endpoint)) {
  800. ast_ari_response_error(response, 400, "Bad Request",
  801. "Endpoint must be specified");
  802. return;
  803. }
  804. dialtech = ast_strdupa(args_endpoint);
  805. if ((stuff = strchr(dialtech, '/'))) {
  806. *stuff++ = '\0';
  807. ast_copy_string(dialdevice, stuff, sizeof(dialdevice));
  808. }
  809. if (ast_strlen_zero(dialtech) || ast_strlen_zero(dialdevice)) {
  810. ast_ari_response_error(response, 400, "Bad Request",
  811. "Invalid endpoint specified");
  812. return;
  813. }
  814. if (!ast_strlen_zero(args_app)) {
  815. RAII_VAR(struct ast_str *, appdata, ast_str_create(64), ast_free);
  816. if (!appdata) {
  817. ast_ari_response_alloc_failed(response);
  818. return;
  819. }
  820. ast_str_set(&appdata, 0, "%s", args_app);
  821. if (!ast_strlen_zero(args_app_args)) {
  822. ast_str_append(&appdata, 0, ",%s", args_app_args);
  823. }
  824. origination = ast_calloc(1, sizeof(*origination) + ast_str_size(appdata) + 1);
  825. if (!origination) {
  826. ast_ari_response_alloc_failed(response);
  827. return;
  828. }
  829. strcpy(origination->appdata, ast_str_buffer(appdata));
  830. } else if (!ast_strlen_zero(args_extension)) {
  831. origination = ast_calloc(1, sizeof(*origination) + 1);
  832. if (!origination) {
  833. ast_ari_response_alloc_failed(response);
  834. return;
  835. }
  836. ast_copy_string(origination->context, S_OR(args_context, "default"), sizeof(origination->context));
  837. ast_copy_string(origination->exten, args_extension, sizeof(origination->exten));
  838. if (!ast_strlen_zero(args_label)) {
  839. /* A label was provided in the request, use that */
  840. int ipri = 1;
  841. if (sscanf(args_label, "%30d", &ipri) != 1) {
  842. ipri = ast_findlabel_extension(chan, origination->context, origination->exten, args_label, args_caller_id);
  843. if (ipri == -1) {
  844. ast_log(AST_LOG_ERROR, "Requested label: %s can not be found in context: %s\n", args_label, args_context);
  845. ast_ari_response_error(response, 404, "Not Found", "Requested label can not be found");
  846. return;
  847. }
  848. } else {
  849. ast_debug(3, "Numeric value provided for label, jumping to that priority\n");
  850. }
  851. if (ipri == 0) {
  852. ast_log(AST_LOG_ERROR, "Invalid priority label '%s' specified for extension %s in context: %s\n",
  853. args_label, args_extension, args_context);
  854. ast_ari_response_error(response, 400, "Bad Request", "Requested priority is illegal");
  855. return;
  856. }
  857. /* Our priority was provided by a label */
  858. origination->priority = ipri;
  859. } else {
  860. /* No label provided, use provided priority */
  861. origination->priority = args_priority ? args_priority : 1;
  862. }
  863. origination->appdata[0] = '\0';
  864. } else {
  865. ast_ari_response_error(response, 400, "Bad Request",
  866. "Application or extension must be specified");
  867. return;
  868. }
  869. dial = ast_dial_create();
  870. if (!dial) {
  871. ast_ari_response_alloc_failed(response);
  872. ast_free(origination);
  873. return;
  874. }
  875. ast_dial_set_user_data(dial, origination);
  876. if (ast_dial_append(dial, dialtech, dialdevice, &assignedids)) {
  877. ast_ari_response_alloc_failed(response);
  878. ast_dial_destroy(dial);
  879. ast_free(origination);
  880. return;
  881. }
  882. if (args_timeout > 0) {
  883. ast_dial_set_global_timeout(dial, args_timeout * 1000);
  884. } else if (args_timeout == -1) {
  885. ast_dial_set_global_timeout(dial, -1);
  886. } else {
  887. ast_dial_set_global_timeout(dial, 30000);
  888. }
  889. if (!ast_strlen_zero(args_caller_id)) {
  890. caller_id = ast_strdupa(args_caller_id);
  891. ast_callerid_parse(caller_id, &cid_name, &cid_num);
  892. if (ast_is_shrinkable_phonenumber(cid_num)) {
  893. ast_shrink_phone_number(cid_num);
  894. }
  895. }
  896. if (!ast_strlen_zero(args_originator)) {
  897. other = ast_channel_get_by_name(args_originator);
  898. if (!other) {
  899. ast_ari_response_error(
  900. response, 400, "Bad Request",
  901. "Provided originator channel was not found");
  902. ast_dial_destroy(dial);
  903. ast_free(origination);
  904. return;
  905. }
  906. }
  907. if (ast_dial_prerun(dial, other, NULL)) {
  908. ast_ari_response_alloc_failed(response);
  909. ast_dial_destroy(dial);
  910. ast_free(origination);
  911. ast_channel_cleanup(other);
  912. return;
  913. }
  914. ast_channel_cleanup(other);
  915. chan = ast_dial_get_channel(dial, 0);
  916. if (!chan) {
  917. ast_ari_response_alloc_failed(response);
  918. ast_dial_destroy(dial);
  919. ast_free(origination);
  920. return;
  921. }
  922. if (!ast_strlen_zero(cid_num) || !ast_strlen_zero(cid_name)) {
  923. struct ast_party_connected_line connected;
  924. /*
  925. * It seems strange to set the CallerID on an outgoing call leg
  926. * to whom we are calling, but this function's callers are doing
  927. * various Originate methods. This call leg goes to the local
  928. * user. Once the called party answers, the dialplan needs to
  929. * be able to access the CallerID from the CALLERID function as
  930. * if the called party had placed this call.
  931. */
  932. ast_set_callerid(chan, cid_num, cid_name, cid_num);
  933. ast_party_connected_line_set_init(&connected, ast_channel_connected(chan));
  934. if (!ast_strlen_zero(cid_num)) {
  935. connected.id.number.valid = 1;
  936. connected.id.number.str = (char *) cid_num;
  937. connected.id.number.presentation = AST_PRES_ALLOWED_USER_NUMBER_NOT_SCREENED;
  938. }
  939. if (!ast_strlen_zero(cid_name)) {
  940. connected.id.name.valid = 1;
  941. connected.id.name.str = (char *) cid_name;
  942. connected.id.name.presentation = AST_PRES_ALLOWED_USER_NUMBER_NOT_SCREENED;
  943. }
  944. ast_channel_set_connected_line(chan, &connected, NULL);
  945. }
  946. ast_channel_lock(chan);
  947. if (variables) {
  948. ast_set_variables(chan, variables);
  949. }
  950. ast_set_flag(ast_channel_flags(chan), AST_FLAG_ORIGINATED);
  951. if (!ast_strlen_zero(args_app)) {
  952. struct ast_channel *local_peer;
  953. stasis_app_subscribe_channel(args_app, chan);
  954. /* Subscribe to the Local channel peer also. */
  955. local_peer = ast_local_get_peer(chan);
  956. if (local_peer) {
  957. stasis_app_subscribe_channel(args_app, local_peer);
  958. ast_channel_unref(local_peer);
  959. }
  960. }
  961. snapshot = ast_channel_snapshot_get_latest(ast_channel_uniqueid(chan));
  962. ast_channel_unlock(chan);
  963. /* Before starting the async dial bump the ref in case the dial quickly goes away and takes
  964. * the reference with it
  965. */
  966. ast_channel_ref(chan);
  967. if (ast_pthread_create_detached(&thread, NULL, ari_originate_dial, dial)) {
  968. ast_ari_response_alloc_failed(response);
  969. ast_dial_destroy(dial);
  970. ast_free(origination);
  971. } else {
  972. ast_ari_response_ok(response, ast_channel_snapshot_to_json(snapshot, NULL));
  973. }
  974. ast_channel_unref(chan);
  975. return;
  976. }
  977. /*!
  978. * \internal
  979. * \brief Convert a \c ast_json list of key/value pair tuples into a \c ast_variable list
  980. * \since 13.3.0
  981. *
  982. * \param[out] response HTTP response if error
  983. * \param json_variables The JSON blob containing the variable
  984. * \param[out] variables An out reference to the variables to populate.
  985. *
  986. * \retval 0 on success.
  987. * \retval -1 on error.
  988. */
  989. static int json_to_ast_variables(struct ast_ari_response *response, struct ast_json *json_variables, struct ast_variable **variables)
  990. {
  991. enum ast_json_to_ast_vars_code res;
  992. res = ast_json_to_ast_variables(json_variables, variables);
  993. switch (res) {
  994. case AST_JSON_TO_AST_VARS_CODE_SUCCESS:
  995. return 0;
  996. case AST_JSON_TO_AST_VARS_CODE_INVALID_TYPE:
  997. ast_ari_response_error(response, 400, "Bad Request",
  998. "Only string values in the 'variables' object allowed");
  999. break;
  1000. case AST_JSON_TO_AST_VARS_CODE_OOM:
  1001. ast_ari_response_alloc_failed(response);
  1002. break;
  1003. }
  1004. ast_log(AST_LOG_ERROR, "Unable to convert 'variables' in JSON body to channel variables\n");
  1005. return -1;
  1006. }
  1007. void ast_ari_channels_originate_with_id(struct ast_variable *headers,
  1008. struct ast_ari_channels_originate_with_id_args *args,
  1009. struct ast_ari_response *response)
  1010. {
  1011. struct ast_variable *variables = NULL;
  1012. /* Parse any query parameters out of the body parameter */
  1013. if (args->variables) {
  1014. struct ast_json *json_variables;
  1015. ast_ari_channels_originate_with_id_parse_body(args->variables, args);
  1016. json_variables = ast_json_object_get(args->variables, "variables");
  1017. if (json_variables
  1018. && json_to_ast_variables(response, json_variables, &variables)) {
  1019. return;
  1020. }
  1021. }
  1022. ari_channels_handle_originate_with_id(
  1023. args->endpoint,
  1024. args->extension,
  1025. args->context,
  1026. args->priority,
  1027. args->label,
  1028. args->app,
  1029. args->app_args,
  1030. args->caller_id,
  1031. args->timeout,
  1032. variables,
  1033. args->channel_id,
  1034. args->other_channel_id,
  1035. args->originator,
  1036. response);
  1037. ast_variables_destroy(variables);
  1038. }
  1039. void ast_ari_channels_originate(struct ast_variable *headers,
  1040. struct ast_ari_channels_originate_args *args,
  1041. struct ast_ari_response *response)
  1042. {
  1043. struct ast_variable *variables = NULL;
  1044. /* Parse any query parameters out of the body parameter */
  1045. if (args->variables) {
  1046. struct ast_json *json_variables;
  1047. ast_ari_channels_originate_parse_body(args->variables, args);
  1048. json_variables = ast_json_object_get(args->variables, "variables");
  1049. if (json_variables
  1050. && json_to_ast_variables(response, json_variables, &variables)) {
  1051. return;
  1052. }
  1053. }
  1054. ari_channels_handle_originate_with_id(
  1055. args->endpoint,
  1056. args->extension,
  1057. args->context,
  1058. args->priority,
  1059. args->label,
  1060. args->app,
  1061. args->app_args,
  1062. args->caller_id,
  1063. args->timeout,
  1064. variables,
  1065. args->channel_id,
  1066. args->other_channel_id,
  1067. args->originator,
  1068. response);
  1069. ast_variables_destroy(variables);
  1070. }
  1071. void ast_ari_channels_get_channel_var(struct ast_variable *headers,
  1072. struct ast_ari_channels_get_channel_var_args *args,
  1073. struct ast_ari_response *response)
  1074. {
  1075. RAII_VAR(struct ast_json *, json, NULL, ast_json_unref);
  1076. RAII_VAR(struct stasis_app_control *, control, NULL, ao2_cleanup);
  1077. RAII_VAR(struct ast_str *, value, ast_str_create(32), ast_free);
  1078. RAII_VAR(struct ast_channel *, channel, NULL, ast_channel_cleanup);
  1079. ast_assert(response != NULL);
  1080. if (!value) {
  1081. ast_ari_response_alloc_failed(response);
  1082. return;
  1083. }
  1084. if (ast_strlen_zero(args->variable)) {
  1085. ast_ari_response_error(
  1086. response, 400, "Bad Request",
  1087. "Variable name is required");
  1088. return;
  1089. }
  1090. if (ast_strlen_zero(args->channel_id)) {
  1091. ast_ari_response_error(
  1092. response, 400, "Bad Request",
  1093. "Channel ID is required");
  1094. return;
  1095. }
  1096. channel = ast_channel_get_by_name(args->channel_id);
  1097. if (!channel) {
  1098. ast_ari_response_error(
  1099. response, 404, "Channel Not Found",
  1100. "Provided channel was not found");
  1101. return;
  1102. }
  1103. /* You may be tempted to lock the channel you're about to read from. You
  1104. * would be wrong. Some dialplan functions put the channel into
  1105. * autoservice, which deadlocks if the channel is already locked.
  1106. * ast_str_retrieve_variable() does its own locking, and the dialplan
  1107. * functions need to as well. We should be fine without the lock.
  1108. */
  1109. if (args->variable[strlen(args->variable) - 1] == ')') {
  1110. if (ast_func_read2(channel, args->variable, &value, 0)) {
  1111. ast_ari_response_error(
  1112. response, 500, "Error With Function",
  1113. "Unable to read provided function");
  1114. return;
  1115. }
  1116. } else {
  1117. if (!ast_str_retrieve_variable(&value, 0, channel, NULL, args->variable)) {
  1118. ast_ari_response_error(
  1119. response, 404, "Variable Not Found",
  1120. "Provided variable was not found");
  1121. return;
  1122. }
  1123. }
  1124. if (!(json = ast_json_pack("{s: s}", "value", S_OR(ast_str_buffer(value), "")))) {
  1125. ast_ari_response_alloc_failed(response);
  1126. return;
  1127. }
  1128. ast_ari_response_ok(response, ast_json_ref(json));
  1129. }
  1130. void ast_ari_channels_set_channel_var(struct ast_variable *headers,
  1131. struct ast_ari_channels_set_channel_var_args *args,
  1132. struct ast_ari_response *response)
  1133. {
  1134. RAII_VAR(struct stasis_app_control *, control, NULL, ao2_cleanup);
  1135. ast_assert(response != NULL);
  1136. if (ast_strlen_zero(args->variable)) {
  1137. ast_ari_response_error(
  1138. response, 400, "Bad Request",
  1139. "Variable name is required");
  1140. return;
  1141. }
  1142. control = find_control(response, args->channel_id);
  1143. if (control == NULL) {
  1144. /* response filled in by find_control */
  1145. return;
  1146. }
  1147. if (stasis_app_control_set_channel_var(control, args->variable, args->value)) {
  1148. ast_ari_response_error(
  1149. response, 400, "Bad Request",
  1150. "Failed to execute function");
  1151. return;
  1152. }
  1153. ast_ari_response_no_content(response);
  1154. }
  1155. static void ari_channels_handle_snoop_channel(
  1156. const char *args_channel_id,
  1157. const char *args_spy,
  1158. const char *args_whisper,
  1159. const char *args_app,
  1160. const char *args_app_args,
  1161. const char *args_snoop_id,
  1162. struct ast_ari_response *response)
  1163. {
  1164. enum stasis_app_snoop_direction spy, whisper;
  1165. RAII_VAR(struct ast_channel *, chan, NULL, ast_channel_cleanup);
  1166. RAII_VAR(struct ast_channel *, snoop, NULL, ast_channel_cleanup);
  1167. RAII_VAR(struct ast_channel_snapshot *, snapshot, NULL, ao2_cleanup);
  1168. ast_assert(response != NULL);
  1169. if (ast_strlen_zero(args_spy) || !strcmp(args_spy, "none")) {
  1170. spy = STASIS_SNOOP_DIRECTION_NONE;
  1171. } else if (!strcmp(args_spy, "both")) {
  1172. spy = STASIS_SNOOP_DIRECTION_BOTH;
  1173. } else if (!strcmp(args_spy, "out")) {
  1174. spy = STASIS_SNOOP_DIRECTION_OUT;
  1175. } else if (!strcmp(args_spy, "in")) {
  1176. spy = STASIS_SNOOP_DIRECTION_IN;
  1177. } else {
  1178. ast_ari_response_error(
  1179. response, 400, "Bad Request",
  1180. "Invalid direction specified for spy");
  1181. return;
  1182. }
  1183. if (ast_strlen_zero(args_whisper) || !strcmp(args_whisper, "none")) {
  1184. whisper = STASIS_SNOOP_DIRECTION_NONE;
  1185. } else if (!strcmp(args_whisper, "both")) {
  1186. whisper = STASIS_SNOOP_DIRECTION_BOTH;
  1187. } else if (!strcmp(args_whisper, "out")) {
  1188. whisper = STASIS_SNOOP_DIRECTION_OUT;
  1189. } else if (!strcmp(args_whisper, "in")) {
  1190. whisper = STASIS_SNOOP_DIRECTION_IN;
  1191. } else {
  1192. ast_ari_response_error(
  1193. response, 400, "Bad Request",
  1194. "Invalid direction specified for whisper");
  1195. return;
  1196. }
  1197. if (spy == STASIS_SNOOP_DIRECTION_NONE && whisper == STASIS_SNOOP_DIRECTION_NONE) {
  1198. ast_ari_response_error(
  1199. response, 400, "Bad Request",
  1200. "Direction must be specified for at least spy or whisper");
  1201. return;
  1202. } else if (ast_strlen_zero(args_app)) {
  1203. ast_ari_response_error(
  1204. response, 400, "Bad Request",
  1205. "Application name is required");
  1206. return;
  1207. }
  1208. chan = ast_channel_get_by_name(args_channel_id);
  1209. if (chan == NULL) {
  1210. ast_ari_response_error(
  1211. response, 404, "Channel Not Found",
  1212. "Provided channel was not found");
  1213. return;
  1214. }
  1215. snoop = stasis_app_control_snoop(chan, spy, whisper, args_app, args_app_args,
  1216. args_snoop_id);
  1217. if (snoop == NULL) {
  1218. ast_ari_response_error(
  1219. response, 500, "Internal error",
  1220. "Snoop channel could not be created");
  1221. return;
  1222. }
  1223. snapshot = ast_channel_snapshot_get_latest(ast_channel_uniqueid(snoop));
  1224. ast_ari_response_ok(response, ast_channel_snapshot_to_json(snapshot, NULL));
  1225. }
  1226. void ast_ari_channels_snoop_channel(struct ast_variable *headers,
  1227. struct ast_ari_channels_snoop_channel_args *args,
  1228. struct ast_ari_response *response)
  1229. {
  1230. ari_channels_handle_snoop_channel(
  1231. args->channel_id,
  1232. args->spy,
  1233. args->whisper,
  1234. args->app,
  1235. args->app_args,
  1236. args->snoop_id,
  1237. response);
  1238. }
  1239. void ast_ari_channels_snoop_channel_with_id(struct ast_variable *headers,
  1240. struct ast_ari_channels_snoop_channel_with_id_args *args,
  1241. struct ast_ari_response *response)
  1242. {
  1243. ari_channels_handle_snoop_channel(
  1244. args->channel_id,
  1245. args->spy,
  1246. args->whisper,
  1247. args->app,
  1248. args->app_args,
  1249. args->snoop_id,
  1250. response);
  1251. }