res_stasis_recording: Add a "target_uri" field to recording events.
[asterisk/asterisk.git] / res / ari / resource_channels.c
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
19 /*! \file
20  *
21  * \brief Implementation for ARI stubs.
22  *
23  * \author David M. Lee, II <dlee@digium.com>
24  */
25
26 /*** MODULEINFO
27         <depend type="module">res_stasis_app_playback</depend>
28         <support_level>core</support_level>
29  ***/
30
31 #include "asterisk.h"
32
33 ASTERISK_FILE_VERSION(__FILE__, "$Revision$")
34
35 #include "asterisk/file.h"
36 #include "asterisk/pbx.h"
37 #include "asterisk/bridge.h"
38 #include "asterisk/callerid.h"
39 #include "asterisk/stasis_app.h"
40 #include "asterisk/stasis_app_playback.h"
41 #include "asterisk/stasis_app_recording.h"
42 #include "asterisk/stasis_app_snoop.h"
43 #include "asterisk/stasis_channels.h"
44 #include "asterisk/causes.h"
45 #include "resource_channels.h"
46
47 #include <limits.h>
48
49 /*!
50  * \brief Finds the control object for a channel, filling the response with an
51  * error, if appropriate.
52  * \param[out] response Response to fill with an error if control is not found.
53  * \param channel_id ID of the channel to lookup.
54  * \return Channel control object.
55  * \return \c NULL if control object does not exist.
56  */
57 static struct stasis_app_control *find_control(
58         struct ast_ari_response *response,
59         const char *channel_id)
60 {
61         RAII_VAR(struct stasis_app_control *, control, NULL, ao2_cleanup);
62
63         ast_assert(response != NULL);
64
65         control = stasis_app_control_find_by_channel_id(channel_id);
66         if (control == NULL) {
67                 /* Distinguish between 404 and 409 errors */
68                 RAII_VAR(struct ast_channel *, chan, NULL, ao2_cleanup);
69                 chan = ast_channel_get_by_name(channel_id);
70                 if (chan == NULL) {
71                         ast_ari_response_error(response, 404, "Not Found",
72                                    "Channel not found");
73                         return NULL;
74                 }
75
76                 ast_ari_response_error(response, 409, "Conflict",
77                            "Channel not in Stasis application");
78                 return NULL;
79         }
80
81         ao2_ref(control, +1);
82         return control;
83 }
84
85 void ast_ari_channels_continue_in_dialplan(
86         struct ast_variable *headers,
87         struct ast_ari_channels_continue_in_dialplan_args *args,
88         struct ast_ari_response *response)
89 {
90         RAII_VAR(struct stasis_app_control *, control, NULL, ao2_cleanup);
91
92         ast_assert(response != NULL);
93
94         control = find_control(response, args->channel_id);
95         if (control == NULL) {
96                 return;
97         }
98
99         if (stasis_app_control_continue(control, args->context, args->extension, args->priority)) {
100                 ast_ari_response_alloc_failed(response);
101                 return;
102         }
103
104         ast_ari_response_no_content(response);
105 }
106
107 void ast_ari_channels_answer(struct ast_variable *headers,
108         struct ast_ari_channels_answer_args *args,
109         struct ast_ari_response *response)
110 {
111         RAII_VAR(struct stasis_app_control *, control, NULL, ao2_cleanup);
112
113         control = find_control(response, args->channel_id);
114         if (control == NULL) {
115                 return;
116         }
117
118         if (stasis_app_control_answer(control) != 0) {
119                 ast_ari_response_error(
120                         response, 500, "Internal Server Error",
121                         "Failed to answer channel");
122                 return;
123         }
124
125         ast_ari_response_no_content(response);
126 }
127
128 void ast_ari_channels_ring(struct ast_variable *headers,
129         struct ast_ari_channels_ring_args *args,
130         struct ast_ari_response *response)
131 {
132         RAII_VAR(struct stasis_app_control *, control, NULL, ao2_cleanup);
133
134         control = find_control(response, args->channel_id);
135         if (control == NULL) {
136                 return;
137         }
138
139         stasis_app_control_ring(control);
140
141         ast_ari_response_no_content(response);
142 }
143
144 void ast_ari_channels_ring_stop(struct ast_variable *headers,
145         struct ast_ari_channels_ring_stop_args *args,
146         struct ast_ari_response *response)
147 {
148         RAII_VAR(struct stasis_app_control *, control, NULL, ao2_cleanup);
149
150         control = find_control(response, args->channel_id);
151         if (control == NULL) {
152                 return;
153         }
154
155         stasis_app_control_ring_stop(control);
156
157         ast_ari_response_no_content(response);
158 }
159
160 void ast_ari_channels_mute(struct ast_variable *headers,
161         struct ast_ari_channels_mute_args *args,
162         struct ast_ari_response *response)
163 {
164         RAII_VAR(struct stasis_app_control *, control, NULL, ao2_cleanup);
165         unsigned int direction = 0;
166         enum ast_frame_type frametype = AST_FRAME_VOICE;
167
168         control = find_control(response, args->channel_id);
169         if (control == NULL) {
170                 return;
171         }
172
173         if (ast_strlen_zero(args->direction)) {
174                 ast_ari_response_error(
175                         response, 400, "Bad Request",
176                         "Direction is required");
177                 return;
178         }
179
180         if (!strcmp(args->direction, "in")) {
181                 direction = AST_MUTE_DIRECTION_READ;
182         } else if (!strcmp(args->direction, "out")) {
183                 direction = AST_MUTE_DIRECTION_WRITE;
184         } else if (!strcmp(args->direction, "both")) {
185                 direction = AST_MUTE_DIRECTION_READ | AST_MUTE_DIRECTION_WRITE;
186         } else {
187                 ast_ari_response_error(
188                         response, 400, "Bad Request",
189                         "Invalid direction specified");
190                 return;
191         }
192
193         stasis_app_control_mute(control, direction, frametype);
194
195         ast_ari_response_no_content(response);
196 }
197
198 void ast_ari_channels_unmute(struct ast_variable *headers,
199         struct ast_ari_channels_unmute_args *args,
200         struct ast_ari_response *response)
201 {
202         RAII_VAR(struct stasis_app_control *, control, NULL, ao2_cleanup);
203         unsigned int direction = 0;
204         enum ast_frame_type frametype = AST_FRAME_VOICE;
205
206         control = find_control(response, args->channel_id);
207         if (control == NULL) {
208                 return;
209         }
210
211         if (ast_strlen_zero(args->direction)) {
212                 ast_ari_response_error(
213                         response, 400, "Bad Request",
214                         "Direction is required");
215                 return;
216         }
217
218         if (!strcmp(args->direction, "in")) {
219                 direction = AST_MUTE_DIRECTION_READ;
220         } else if (!strcmp(args->direction, "out")) {
221                 direction = AST_MUTE_DIRECTION_WRITE;
222         } else if (!strcmp(args->direction, "both")) {
223                 direction = AST_MUTE_DIRECTION_READ | AST_MUTE_DIRECTION_WRITE;
224         } else {
225                 ast_ari_response_error(
226                         response, 400, "Bad Request",
227                         "Invalid direction specified");
228                 return;
229         }
230
231         stasis_app_control_unmute(control, direction, frametype);
232
233         ast_ari_response_no_content(response);
234 }
235
236 void ast_ari_channels_send_dtmf(struct ast_variable *headers,
237         struct ast_ari_channels_send_dtmf_args *args,
238         struct ast_ari_response *response)
239 {
240         RAII_VAR(struct stasis_app_control *, control, NULL, ao2_cleanup);
241
242         control = find_control(response, args->channel_id);
243         if (control == NULL) {
244                 return;
245         }
246
247         if (ast_strlen_zero(args->dtmf)) {
248                 ast_ari_response_error(
249                         response, 400, "Bad Request",
250                         "DTMF is required");
251                 return;
252         }
253
254         stasis_app_control_dtmf(control, args->dtmf, args->before, args->between, args->duration, args->after);
255
256         ast_ari_response_no_content(response);
257 }
258
259 void ast_ari_channels_hold(struct ast_variable *headers,
260         struct ast_ari_channels_hold_args *args,
261         struct ast_ari_response *response)
262 {
263         RAII_VAR(struct stasis_app_control *, control, NULL, ao2_cleanup);
264
265         control = find_control(response, args->channel_id);
266         if (control == NULL) {
267                 /* Response filled in by find_control */
268                 return;
269         }
270
271         stasis_app_control_hold(control);
272
273         ast_ari_response_no_content(response);
274 }
275
276 void ast_ari_channels_unhold(struct ast_variable *headers,
277         struct ast_ari_channels_unhold_args *args,
278         struct ast_ari_response *response)
279 {
280         RAII_VAR(struct stasis_app_control *, control, NULL, ao2_cleanup);
281
282         control = find_control(response, args->channel_id);
283         if (control == NULL) {
284                 /* Response filled in by find_control */
285                 return;
286         }
287
288         stasis_app_control_unhold(control);
289
290         ast_ari_response_no_content(response);
291 }
292
293 void ast_ari_channels_start_moh(struct ast_variable *headers,
294         struct ast_ari_channels_start_moh_args *args,
295         struct ast_ari_response *response)
296 {
297         RAII_VAR(struct stasis_app_control *, control, NULL, ao2_cleanup);
298
299         control = find_control(response, args->channel_id);
300         if (control == NULL) {
301                 /* Response filled in by find_control */
302                 return;
303         }
304
305         stasis_app_control_moh_start(control, args->moh_class);
306         ast_ari_response_no_content(response);
307 }
308
309 void ast_ari_channels_stop_moh(struct ast_variable *headers,
310         struct ast_ari_channels_stop_moh_args *args,
311         struct ast_ari_response *response)
312 {
313         RAII_VAR(struct stasis_app_control *, control, NULL, ao2_cleanup);
314
315         control = find_control(response, args->channel_id);
316         if (control == NULL) {
317                 /* Response filled in by find_control */
318                 return;
319         }
320
321         stasis_app_control_moh_stop(control);
322         ast_ari_response_no_content(response);
323 }
324
325 void ast_ari_channels_start_silence(struct ast_variable *headers,
326         struct ast_ari_channels_start_silence_args *args,
327         struct ast_ari_response *response)
328 {
329         RAII_VAR(struct stasis_app_control *, control, NULL, ao2_cleanup);
330
331         control = find_control(response, args->channel_id);
332         if (control == NULL) {
333                 /* Response filled in by find_control */
334                 return;
335         }
336
337         stasis_app_control_silence_start(control);
338         ast_ari_response_no_content(response);
339 }
340
341 void ast_ari_channels_stop_silence(struct ast_variable *headers,
342         struct ast_ari_channels_stop_silence_args *args,
343         struct ast_ari_response *response)
344 {
345         RAII_VAR(struct stasis_app_control *, control, NULL, ao2_cleanup);
346
347         control = find_control(response, args->channel_id);
348         if (control == NULL) {
349                 /* Response filled in by find_control */
350                 return;
351         }
352
353         stasis_app_control_silence_stop(control);
354         ast_ari_response_no_content(response);
355 }
356
357 void ast_ari_channels_play(struct ast_variable *headers,
358         struct ast_ari_channels_play_args *args,
359         struct ast_ari_response *response)
360 {
361         RAII_VAR(struct stasis_app_control *, control, NULL, ao2_cleanup);
362         RAII_VAR(struct ast_channel_snapshot *, snapshot, NULL, ao2_cleanup);
363         RAII_VAR(struct stasis_app_playback *, playback, NULL, ao2_cleanup);
364         RAII_VAR(char *, playback_url, NULL, ast_free);
365         struct ast_json *json;
366         const char *language;
367
368         ast_assert(response != NULL);
369
370         control = find_control(response, args->channel_id);
371         if (control == NULL) {
372                 /* Response filled in by find_control */
373                 return;
374         }
375
376         snapshot = stasis_app_control_get_snapshot(control);
377         if (!snapshot) {
378                 ast_ari_response_error(
379                         response, 404, "Not Found",
380                         "Channel not found");
381                 return;
382         }
383
384         if (args->skipms < 0) {
385                 ast_ari_response_error(
386                         response, 400, "Bad Request",
387                         "skipms cannot be negative");
388                 return;
389         }
390
391         if (args->offsetms < 0) {
392                 ast_ari_response_error(
393                         response, 400, "Bad Request",
394                         "offsetms cannot be negative");
395                 return;
396         }
397
398         language = S_OR(args->lang, snapshot->language);
399
400         playback = stasis_app_control_play_uri(control, args->media, language,
401                 args->channel_id, STASIS_PLAYBACK_TARGET_CHANNEL, args->skipms, args->offsetms);
402         if (!playback) {
403                 ast_ari_response_error(
404                         response, 500, "Internal Server Error",
405                         "Failed to queue media for playback");
406                 return;
407         }
408
409         ast_asprintf(&playback_url, "/playback/%s",
410                 stasis_app_playback_get_id(playback));
411         if (!playback_url) {
412                 ast_ari_response_error(
413                         response, 500, "Internal Server Error",
414                         "Out of memory");
415                 return;
416         }
417
418         json = stasis_app_playback_to_json(playback);
419         if (!json) {
420                 ast_ari_response_error(
421                         response, 500, "Internal Server Error",
422                         "Out of memory");
423                 return;
424         }
425
426         ast_ari_response_created(response, playback_url, json);
427 }
428
429 void ast_ari_channels_record(struct ast_variable *headers,
430         struct ast_ari_channels_record_args *args,
431         struct ast_ari_response *response)
432 {
433         RAII_VAR(struct stasis_app_control *, control, NULL, ao2_cleanup);
434         RAII_VAR(struct ast_channel_snapshot *, snapshot, NULL, ao2_cleanup);
435         RAII_VAR(struct stasis_app_recording *, recording, NULL, ao2_cleanup);
436         RAII_VAR(char *, recording_url, NULL, ast_free);
437         struct ast_json *json;
438         RAII_VAR(struct stasis_app_recording_options *, options, NULL,
439                 ao2_cleanup);
440         RAII_VAR(char *, uri_encoded_name, NULL, ast_free);
441         size_t uri_name_maxlen;
442
443         ast_assert(response != NULL);
444
445         if (args->max_duration_seconds < 0) {
446                 ast_ari_response_error(
447                         response, 400, "Bad Request",
448                         "max_duration_seconds cannot be negative");
449                 return;
450         }
451
452         if (args->max_silence_seconds < 0) {
453                 ast_ari_response_error(
454                         response, 400, "Bad Request",
455                         "max_silence_seconds cannot be negative");
456                 return;
457         }
458
459         control = find_control(response, args->channel_id);
460         if (control == NULL) {
461                 /* Response filled in by find_control */
462                 return;
463         }
464
465         options = stasis_app_recording_options_create(args->name, args->format);
466         if (options == NULL) {
467                 ast_ari_response_error(
468                         response, 500, "Internal Server Error",
469                         "Out of memory");
470         }
471         ast_string_field_build(options, target, "channel:%s", args->channel_id);
472         options->max_silence_seconds = args->max_silence_seconds;
473         options->max_duration_seconds = args->max_duration_seconds;
474         options->terminate_on =
475                 stasis_app_recording_termination_parse(args->terminate_on);
476         options->if_exists =
477                 stasis_app_recording_if_exists_parse(args->if_exists);
478         options->beep = args->beep;
479
480         if (options->terminate_on == STASIS_APP_RECORDING_TERMINATE_INVALID) {
481                 ast_ari_response_error(
482                         response, 400, "Bad Request",
483                         "terminateOn invalid");
484                 return;
485         }
486
487         if (options->if_exists == -1) {
488                 ast_ari_response_error(
489                         response, 400, "Bad Request",
490                         "ifExists invalid");
491                 return;
492         }
493
494         if (!ast_get_format_for_file_ext(options->format)) {
495                 ast_ari_response_error(
496                         response, 422, "Unprocessable Entity",
497                         "specified format is unknown on this system");
498                 return;
499         }
500
501         recording = stasis_app_control_record(control, options);
502         if (recording == NULL) {
503                 switch(errno) {
504                 case EINVAL:
505                         /* While the arguments are invalid, we should have
506                          * caught them prior to calling record.
507                          */
508                         ast_ari_response_error(
509                                 response, 500, "Internal Server Error",
510                                 "Error parsing request");
511                         break;
512                 case EEXIST:
513                         ast_ari_response_error(response, 409, "Conflict",
514                                 "Recording '%s' already exists and can not be overwritten",
515                                 args->name);
516                         break;
517                 case ENOMEM:
518                         ast_ari_response_error(
519                                 response, 500, "Internal Server Error",
520                                 "Out of memory");
521                         break;
522                 case EPERM:
523                         ast_ari_response_error(
524                                 response, 400, "Bad Request",
525                                 "Recording name invalid");
526                         break;
527                 default:
528                         ast_log(LOG_WARNING,
529                                 "Unrecognized recording error: %s\n",
530                                 strerror(errno));
531                         ast_ari_response_error(
532                                 response, 500, "Internal Server Error",
533                                 "Internal Server Error");
534                         break;
535                 }
536                 return;
537         }
538
539         uri_name_maxlen = strlen(args->name) * 3;
540         uri_encoded_name = ast_malloc(uri_name_maxlen);
541         if (!uri_encoded_name) {
542                 ast_ari_response_error(
543                         response, 500, "Internal Server Error",
544                         "Out of memory");
545                 return;
546         }
547         ast_uri_encode(args->name, uri_encoded_name, uri_name_maxlen,
548                 ast_uri_http);
549
550         ast_asprintf(&recording_url, "/recordings/live/%s", uri_encoded_name);
551         if (!recording_url) {
552                 ast_ari_response_error(
553                         response, 500, "Internal Server Error",
554                         "Out of memory");
555                 return;
556         }
557
558         json = stasis_app_recording_to_json(recording);
559         if (!json) {
560                 ast_ari_response_error(
561                         response, 500, "Internal Server Error",
562                         "Out of memory");
563                 return;
564         }
565
566         ast_ari_response_created(response, recording_url, json);
567 }
568
569 void ast_ari_channels_get(struct ast_variable *headers,
570         struct ast_ari_channels_get_args *args,
571         struct ast_ari_response *response)
572 {
573         RAII_VAR(struct stasis_message *, msg, NULL, ao2_cleanup);
574         struct stasis_cache *cache;
575         struct ast_channel_snapshot *snapshot;
576
577         cache = ast_channel_cache();
578         if (!cache) {
579                 ast_ari_response_error(
580                         response, 500, "Internal Server Error",
581                         "Message bus not initialized");
582                 return;
583         }
584
585         msg = stasis_cache_get(cache, ast_channel_snapshot_type(),
586                                args->channel_id);
587         if (!msg) {
588                 ast_ari_response_error(
589                         response, 404, "Not Found",
590                         "Channel not found");
591                 return;
592         }
593
594         snapshot = stasis_message_data(msg);
595         ast_assert(snapshot != NULL);
596
597         ast_ari_response_ok(response,
598                                 ast_channel_snapshot_to_json(snapshot, NULL));
599 }
600
601 void ast_ari_channels_hangup(struct ast_variable *headers,
602         struct ast_ari_channels_hangup_args *args,
603         struct ast_ari_response *response)
604 {
605         RAII_VAR(struct ast_channel *, chan, NULL, ao2_cleanup);
606         int cause;
607
608         chan = ast_channel_get_by_name(args->channel_id);
609         if (chan == NULL) {
610                 ast_ari_response_error(
611                         response, 404, "Not Found",
612                         "Channel not found");
613                 return;
614         }
615
616         if (ast_strlen_zero(args->reason) || !strcmp(args->reason, "normal")) {
617                 cause = AST_CAUSE_NORMAL;
618         } else if (!strcmp(args->reason, "busy")) {
619                 cause = AST_CAUSE_BUSY;
620         } else if (!strcmp(args->reason, "congestion")) {
621                 cause = AST_CAUSE_CONGESTION;
622         } else {
623                 ast_ari_response_error(
624                         response, 400, "Invalid Reason",
625                         "Invalid reason for hangup provided");
626                 return;
627         }
628
629         ast_channel_hangupcause_set(chan, cause);
630         ast_softhangup(chan, AST_SOFTHANGUP_EXPLICIT);
631
632         ast_ari_response_no_content(response);
633 }
634
635 void ast_ari_channels_list(struct ast_variable *headers,
636         struct ast_ari_channels_list_args *args,
637         struct ast_ari_response *response)
638 {
639         RAII_VAR(struct stasis_cache *, cache, NULL, ao2_cleanup);
640         RAII_VAR(struct ao2_container *, snapshots, NULL, ao2_cleanup);
641         RAII_VAR(struct ast_json *, json, NULL, ast_json_unref);
642         struct ao2_iterator i;
643         void *obj;
644         struct stasis_message_sanitizer *sanitize = stasis_app_get_sanitizer();
645
646         cache = ast_channel_cache();
647         if (!cache) {
648                 ast_ari_response_error(
649                         response, 500, "Internal Server Error",
650                         "Message bus not initialized");
651                 return;
652         }
653         ao2_ref(cache, +1);
654
655         snapshots = stasis_cache_dump(cache, ast_channel_snapshot_type());
656         if (!snapshots) {
657                 ast_ari_response_alloc_failed(response);
658                 return;
659         }
660
661         json = ast_json_array_create();
662         if (!json) {
663                 ast_ari_response_alloc_failed(response);
664                 return;
665         }
666
667         i = ao2_iterator_init(snapshots, 0);
668         while ((obj = ao2_iterator_next(&i))) {
669                 RAII_VAR(struct stasis_message *, msg, obj, ao2_cleanup);
670                 struct ast_channel_snapshot *snapshot = stasis_message_data(msg);
671                 int r;
672
673                 if (sanitize && sanitize->channel_snapshot
674                         && sanitize->channel_snapshot(snapshot)) {
675                         continue;
676                 }
677
678                 r = ast_json_array_append(
679                         json, ast_channel_snapshot_to_json(snapshot, NULL));
680                 if (r != 0) {
681                         ast_ari_response_alloc_failed(response);
682                         ao2_iterator_destroy(&i);
683                         return;
684                 }
685         }
686         ao2_iterator_destroy(&i);
687
688         ast_ari_response_ok(response, ast_json_ref(json));
689 }
690
691 static int json_to_ast_variables(struct ast_json *json_variables, struct ast_variable **variables)
692 {
693         struct ast_variable *current = NULL;
694         struct ast_json_iter *it_json_var;
695
696         for (it_json_var = ast_json_object_iter(json_variables); it_json_var;
697                  it_json_var = ast_json_object_iter_next(json_variables, it_json_var)) {
698                 struct ast_variable *new_var;
699
700                 new_var = ast_variable_new(ast_json_object_iter_key(it_json_var),
701                                            ast_json_string_get(ast_json_object_iter_value(it_json_var)),
702                                            "");
703                 if (!new_var) {
704                         ast_variables_destroy(*variables);
705                         *variables = NULL;
706                         return 1;
707                 }
708
709                 if (!current) {
710                         *variables = new_var;
711                         current = *variables;
712                 } else {
713                         current->next = new_var;
714                         current = new_var;
715                 }
716         }
717
718         return 0;
719 }
720
721 void ast_ari_channels_originate(struct ast_variable *headers,
722         struct ast_ari_channels_originate_args *args,
723         struct ast_ari_response *response)
724 {
725         char *dialtech;
726         char dialdevice[AST_CHANNEL_NAME];
727         char *caller_id = NULL;
728         char *cid_num = NULL;
729         char *cid_name = NULL;
730         int timeout = 30000;
731         RAII_VAR(struct ast_format_cap *, cap,
732                 ast_format_cap_alloc(AST_FORMAT_CAP_FLAG_NOLOCK), ast_format_cap_destroy);
733         struct ast_format tmp_fmt;
734         RAII_VAR(struct ast_variable *, variables, NULL, ast_variables_destroy);
735         char *stuff;
736         struct ast_channel *chan;
737         RAII_VAR(struct ast_channel_snapshot *, snapshot, NULL, ao2_cleanup);
738
739         if (!cap) {
740                 ast_ari_response_alloc_failed(response);
741                 return;
742         }
743         ast_format_cap_add(cap, ast_format_set(&tmp_fmt, AST_FORMAT_SLINEAR, 0));
744
745         /* Parse any query parameters out of the body parameter */
746         if (args->variables) {
747                 struct ast_json *json_variables;
748
749                 ast_ari_channels_originate_parse_body(args->variables, args);
750                 json_variables = ast_json_object_get(args->variables, "variables");
751                 if (json_variables) {
752                         if (json_to_ast_variables(json_variables, &variables)) {
753                                 ast_log(AST_LOG_ERROR, "Unable to convert 'variables' in JSON body to channel variables\n");
754                                 ast_ari_response_alloc_failed(response);
755                                 return;
756                         }
757                 }
758         }
759
760         if (ast_strlen_zero(args->endpoint)) {
761                 ast_ari_response_error(response, 400, "Bad Request",
762                         "Endpoint must be specified");
763                 return;
764         }
765
766         dialtech = ast_strdupa(args->endpoint);
767         if ((stuff = strchr(dialtech, '/'))) {
768                 *stuff++ = '\0';
769                 ast_copy_string(dialdevice, stuff, sizeof(dialdevice));
770         }
771
772         if (ast_strlen_zero(dialtech) || ast_strlen_zero(dialdevice)) {
773                 ast_ari_response_error(response, 400, "Bad Request",
774                         "Invalid endpoint specified");
775                 return;
776         }
777
778         if (args->timeout > 0) {
779                 timeout = args->timeout * 1000;
780         } else if (args->timeout == -1) {
781                 timeout = -1;
782         }
783
784         if (!ast_strlen_zero(args->caller_id)) {
785                 caller_id = ast_strdupa(args->caller_id);
786                 ast_callerid_parse(caller_id, &cid_name, &cid_num);
787
788                 if (ast_is_shrinkable_phonenumber(cid_num)) {
789                         ast_shrink_phone_number(cid_num);
790                 }
791         }
792
793         if (!ast_strlen_zero(args->app)) {
794                 const char *app = "Stasis";
795
796                 RAII_VAR(struct ast_str *, appdata, ast_str_create(64), ast_free);
797
798                 if (!appdata) {
799                         ast_ari_response_alloc_failed(response);
800                         return;
801                 }
802
803                 ast_str_set(&appdata, 0, "%s", args->app);
804                 if (!ast_strlen_zero(args->app_args)) {
805                         ast_str_append(&appdata, 0, ",%s", args->app_args);
806                 }
807
808                 /* originate a channel, putting it into an application */
809                 if (ast_pbx_outgoing_app(dialtech, cap, dialdevice, timeout, app, ast_str_buffer(appdata), NULL, 0, cid_num, cid_name, variables, NULL, &chan)) {
810                         ast_ari_response_alloc_failed(response);
811                         return;
812                 }
813         } else if (!ast_strlen_zero(args->extension)) {
814                 /* originate a channel, sending it to an extension */
815                 if (ast_pbx_outgoing_exten(dialtech, cap, dialdevice, timeout, S_OR(args->context, "default"), args->extension, args->priority ? args->priority : 1, NULL, 0, cid_num, cid_name, variables, NULL, &chan, 0)) {
816                         ast_ari_response_alloc_failed(response);
817                         return;
818                 }
819         } else {
820                 ast_ari_response_error(response, 400, "Bad Request",
821                         "Application or extension must be specified");
822                 return;
823         }
824
825         snapshot = ast_channel_snapshot_create(chan);
826         ast_channel_unlock(chan);
827
828         if (!ast_strlen_zero(args->app)) {
829                 /* channel: + channel ID + null terminator */
830                 char uri[9 + strlen(ast_channel_uniqueid(chan))];
831                 const char *uris[1] = { uri, };
832
833                 sprintf(uri, "channel:%s", ast_channel_uniqueid(chan));
834                 stasis_app_subscribe(args->app, uris, 1, NULL);
835         }
836
837         ast_ari_response_ok(response, ast_channel_snapshot_to_json(snapshot, NULL));
838         ast_channel_unref(chan);
839 }
840
841 void ast_ari_channels_get_channel_var(struct ast_variable *headers,
842         struct ast_ari_channels_get_channel_var_args *args,
843         struct ast_ari_response *response)
844 {
845         RAII_VAR(struct ast_json *, json, NULL, ast_json_unref);
846         RAII_VAR(struct stasis_app_control *, control, NULL, ao2_cleanup);
847         RAII_VAR(char *, value, NULL, ast_free);
848
849         ast_assert(response != NULL);
850
851         if (ast_strlen_zero(args->variable)) {
852                 ast_ari_response_error(
853                         response, 400, "Bad Request",
854                         "Variable name is required");
855                 return;
856         }
857
858         control = find_control(response, args->channel_id);
859         if (control == NULL) {
860                 /* response filled in by find_control */
861                 return;
862         }
863
864         value = stasis_app_control_get_channel_var(control, args->variable);
865
866         if (!(json = ast_json_pack("{s: s}", "value", S_OR(value, "")))) {
867                 ast_ari_response_alloc_failed(response);
868                 return;
869         }
870
871         ast_ari_response_ok(response, ast_json_ref(json));
872 }
873
874 void ast_ari_channels_set_channel_var(struct ast_variable *headers,
875         struct ast_ari_channels_set_channel_var_args *args,
876         struct ast_ari_response *response)
877 {
878         RAII_VAR(struct stasis_app_control *, control, NULL, ao2_cleanup);
879
880         ast_assert(response != NULL);
881
882         if (ast_strlen_zero(args->variable)) {
883                 ast_ari_response_error(
884                         response, 400, "Bad Request",
885                         "Variable name is required");
886                 return;
887         }
888
889         control = find_control(response, args->channel_id);
890         if (control == NULL) {
891                 /* response filled in by find_control */
892                 return;
893         }
894
895         if (stasis_app_control_set_channel_var(control, args->variable, args->value)) {
896                 ast_ari_response_error(
897                         response, 400, "Bad Request",
898                         "Failed to execute function");
899                 return;
900         }
901
902         ast_ari_response_no_content(response);
903 }
904
905 void ast_ari_channels_snoop_channel(struct ast_variable *headers, struct ast_ari_channels_snoop_channel_args *args, struct ast_ari_response *response)
906 {
907         enum stasis_app_snoop_direction spy, whisper;
908         RAII_VAR(struct ast_channel *, chan, NULL, ast_channel_cleanup);
909         RAII_VAR(struct ast_channel *, snoop, NULL, ast_channel_cleanup);
910         RAII_VAR(struct ast_channel_snapshot *, snapshot, NULL, ao2_cleanup);
911
912         ast_assert(response != NULL);
913
914         if (ast_strlen_zero(args->spy) || !strcmp(args->spy, "none")) {
915                 spy = STASIS_SNOOP_DIRECTION_NONE;
916         } else if (!strcmp(args->spy, "both")) {
917                 spy = STASIS_SNOOP_DIRECTION_BOTH;
918         } else if (!strcmp(args->spy, "out")) {
919                 spy = STASIS_SNOOP_DIRECTION_OUT;
920         } else if (!strcmp(args->spy, "in")) {
921                 spy = STASIS_SNOOP_DIRECTION_IN;
922         } else {
923                 ast_ari_response_error(
924                         response, 400, "Bad Request",
925                         "Invalid direction specified for spy");
926                 return;
927         }
928
929         if (ast_strlen_zero(args->whisper) || !strcmp(args->whisper, "none")) {
930                 whisper = STASIS_SNOOP_DIRECTION_NONE;
931         } else if (!strcmp(args->whisper, "both")) {
932                 whisper = STASIS_SNOOP_DIRECTION_BOTH;
933         } else if (!strcmp(args->whisper, "out")) {
934                 whisper = STASIS_SNOOP_DIRECTION_OUT;
935         } else if (!strcmp(args->whisper, "in")) {
936                 whisper = STASIS_SNOOP_DIRECTION_IN;
937         } else {
938                 ast_ari_response_error(
939                         response, 400, "Bad Request",
940                         "Invalid direction specified for whisper");
941                 return;
942         }
943
944         if (spy == STASIS_SNOOP_DIRECTION_NONE && whisper == STASIS_SNOOP_DIRECTION_NONE) {
945                 ast_ari_response_error(
946                         response, 400, "Bad Request",
947                         "Direction must be specified for at least spy or whisper");
948                 return;
949         } else if (ast_strlen_zero(args->app)) {
950                 ast_ari_response_error(
951                         response, 400, "Bad Request",
952                         "Application name is required");
953                 return;
954         }
955
956         chan = ast_channel_get_by_name(args->channel_id);
957         if (chan == NULL) {
958                 ast_ari_response_error(
959                         response, 404, "Channel Not Found",
960                         "Provided channel was not found");
961                 return;
962         }
963
964         snoop = stasis_app_control_snoop(chan, spy, whisper, args->app, args->app_args);
965         if (snoop == NULL) {
966                 ast_ari_response_error(
967                         response, 500, "Internal error",
968                         "Snoop channel could not be created");
969                 return;
970         }
971
972         snapshot = ast_channel_snapshot_create(snoop);
973         ast_ari_response_ok(response, ast_channel_snapshot_to_json(snapshot, NULL));
974 }