6d85781da530df45c32ded983640484065134c85
[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         RAII_VAR(struct ast_json *, json, NULL, ast_json_unref);
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         RAII_VAR(struct ast_json *, json, NULL, ast_json_unref);
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         options->max_silence_seconds = args->max_silence_seconds;
472         options->max_duration_seconds = args->max_duration_seconds;
473         options->terminate_on =
474                 stasis_app_recording_termination_parse(args->terminate_on);
475         options->if_exists =
476                 stasis_app_recording_if_exists_parse(args->if_exists);
477         options->beep = args->beep;
478
479         if (options->terminate_on == STASIS_APP_RECORDING_TERMINATE_INVALID) {
480                 ast_ari_response_error(
481                         response, 400, "Bad Request",
482                         "terminateOn invalid");
483                 return;
484         }
485
486         if (options->if_exists == -1) {
487                 ast_ari_response_error(
488                         response, 400, "Bad Request",
489                         "ifExists invalid");
490                 return;
491         }
492
493         if (!ast_get_format_for_file_ext(options->format)) {
494                 ast_ari_response_error(
495                         response, 422, "Unprocessable Entity",
496                         "specified format is unknown on this system");
497                 return;
498         }
499
500         recording = stasis_app_control_record(control, options);
501         if (recording == NULL) {
502                 switch(errno) {
503                 case EINVAL:
504                         /* While the arguments are invalid, we should have
505                          * caught them prior to calling record.
506                          */
507                         ast_ari_response_error(
508                                 response, 500, "Internal Server Error",
509                                 "Error parsing request");
510                         break;
511                 case EEXIST:
512                         ast_ari_response_error(response, 409, "Conflict",
513                                 "Recording '%s' already exists and can not be overwritten",
514                                 args->name);
515                         break;
516                 case ENOMEM:
517                         ast_ari_response_error(
518                                 response, 500, "Internal Server Error",
519                                 "Out of memory");
520                         break;
521                 case EPERM:
522                         ast_ari_response_error(
523                                 response, 400, "Bad Request",
524                                 "Recording name invalid");
525                         break;
526                 default:
527                         ast_log(LOG_WARNING,
528                                 "Unrecognized recording error: %s\n",
529                                 strerror(errno));
530                         ast_ari_response_error(
531                                 response, 500, "Internal Server Error",
532                                 "Internal Server Error");
533                         break;
534                 }
535                 return;
536         }
537
538         uri_name_maxlen = strlen(args->name) * 3;
539         uri_encoded_name = ast_malloc(uri_name_maxlen);
540         if (!uri_encoded_name) {
541                 ast_ari_response_error(
542                         response, 500, "Internal Server Error",
543                         "Out of memory");
544                 return;
545         }
546         ast_uri_encode(args->name, uri_encoded_name, uri_name_maxlen,
547                 ast_uri_http);
548
549         ast_asprintf(&recording_url, "/recordings/live/%s", uri_encoded_name);
550         if (!recording_url) {
551                 ast_ari_response_error(
552                         response, 500, "Internal Server Error",
553                         "Out of memory");
554                 return;
555         }
556
557         json = stasis_app_recording_to_json(recording);
558         if (!json) {
559                 ast_ari_response_error(
560                         response, 500, "Internal Server Error",
561                         "Out of memory");
562                 return;
563         }
564
565         ast_ari_response_created(response, recording_url, json);
566 }
567
568 void ast_ari_channels_get(struct ast_variable *headers,
569         struct ast_ari_channels_get_args *args,
570         struct ast_ari_response *response)
571 {
572         RAII_VAR(struct stasis_message *, msg, NULL, ao2_cleanup);
573         struct stasis_cache *cache;
574         struct ast_channel_snapshot *snapshot;
575
576         cache = ast_channel_cache();
577         if (!cache) {
578                 ast_ari_response_error(
579                         response, 500, "Internal Server Error",
580                         "Message bus not initialized");
581                 return;
582         }
583
584         msg = stasis_cache_get(cache, ast_channel_snapshot_type(),
585                                args->channel_id);
586         if (!msg) {
587                 ast_ari_response_error(
588                         response, 404, "Not Found",
589                         "Channel not found");
590                 return;
591         }
592
593         snapshot = stasis_message_data(msg);
594         ast_assert(snapshot != NULL);
595
596         ast_ari_response_ok(response,
597                                 ast_channel_snapshot_to_json(snapshot, NULL));
598 }
599
600 void ast_ari_channels_hangup(struct ast_variable *headers,
601         struct ast_ari_channels_hangup_args *args,
602         struct ast_ari_response *response)
603 {
604         RAII_VAR(struct ast_channel *, chan, NULL, ao2_cleanup);
605         int cause;
606
607         chan = ast_channel_get_by_name(args->channel_id);
608         if (chan == NULL) {
609                 ast_ari_response_error(
610                         response, 404, "Not Found",
611                         "Channel not found");
612                 return;
613         }
614
615         if (ast_strlen_zero(args->reason) || !strcmp(args->reason, "normal")) {
616                 cause = AST_CAUSE_NORMAL;
617         } else if (!strcmp(args->reason, "busy")) {
618                 cause = AST_CAUSE_BUSY;
619         } else if (!strcmp(args->reason, "congestion")) {
620                 cause = AST_CAUSE_CONGESTION;
621         } else {
622                 ast_ari_response_error(
623                         response, 400, "Invalid Reason",
624                         "Invalid reason for hangup provided");
625                 return;
626         }
627
628         ast_channel_hangupcause_set(chan, cause);
629         ast_softhangup(chan, AST_SOFTHANGUP_EXPLICIT);
630
631         ast_ari_response_no_content(response);
632 }
633
634 void ast_ari_channels_list(struct ast_variable *headers,
635         struct ast_ari_channels_list_args *args,
636         struct ast_ari_response *response)
637 {
638         RAII_VAR(struct stasis_cache *, cache, NULL, ao2_cleanup);
639         RAII_VAR(struct ao2_container *, snapshots, NULL, ao2_cleanup);
640         RAII_VAR(struct ast_json *, json, NULL, ast_json_unref);
641         struct ao2_iterator i;
642         void *obj;
643         struct stasis_message_sanitizer *sanitize = stasis_app_get_sanitizer();
644
645         cache = ast_channel_cache();
646         if (!cache) {
647                 ast_ari_response_error(
648                         response, 500, "Internal Server Error",
649                         "Message bus not initialized");
650                 return;
651         }
652         ao2_ref(cache, +1);
653
654         snapshots = stasis_cache_dump(cache, ast_channel_snapshot_type());
655         if (!snapshots) {
656                 ast_ari_response_alloc_failed(response);
657                 return;
658         }
659
660         json = ast_json_array_create();
661         if (!json) {
662                 ast_ari_response_alloc_failed(response);
663                 return;
664         }
665
666         for (i = ao2_iterator_init(snapshots, 0);
667                 (obj = ao2_iterator_next(&i)); ao2_cleanup(obj)) {
668                 RAII_VAR(struct stasis_message *, msg, obj, ao2_cleanup);
669                 struct ast_channel_snapshot *snapshot = stasis_message_data(msg);
670                 int r;
671
672                 if (sanitize && sanitize->channel_snapshot
673                         && sanitize->channel_snapshot(snapshot)) {
674                         continue;
675                 }
676
677                 r = ast_json_array_append(
678                         json, ast_channel_snapshot_to_json(snapshot, NULL));
679                 if (r != 0) {
680                         ast_ari_response_alloc_failed(response);
681                         ao2_cleanup(obj);
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 ari_channels_set_channel_var(struct ast_channel *chan,
692         const char *variable, const char *value, struct ast_ari_response *response)
693 {
694         if (pbx_builtin_setvar_helper(chan, variable, value)) {
695                 ast_ari_response_error(
696                         response, 400, "Bad Request",
697                         "Unable to set channel variable %s=%s", variable, value);
698                 return -1;
699         }
700
701         return 0;
702 }
703
704 static int ari_channels_set_channel_vars(struct ast_channel *chan,
705         struct ast_json *variables, struct ast_ari_response *response)
706 {
707         struct ast_json_iter *i;
708
709         if (!variables) {
710                 /* nothing to do */
711                 return 0;
712         }
713
714         for (i = ast_json_object_iter(variables); i;
715              i = ast_json_object_iter_next(variables, i)) {
716                 if (ari_channels_set_channel_var(
717                         chan, ast_json_object_iter_key(i),
718                         ast_json_string_get(ast_json_object_iter_value(i)),
719                         response)) {
720                         /* response filled in by called function */
721                         return -1;
722                 }
723         }
724
725         return 0;
726 }
727
728 void ast_ari_channels_originate(struct ast_variable *headers,
729         struct ast_ari_channels_originate_args *args,
730         struct ast_ari_response *response)
731 {
732         char *dialtech;
733         char dialdevice[AST_CHANNEL_NAME];
734         char *caller_id = NULL;
735         char *cid_num = NULL;
736         char *cid_name = NULL;
737         int timeout = 30000;
738
739         char *stuff;
740         struct ast_channel *chan;
741         RAII_VAR(struct ast_channel_snapshot *, snapshot, NULL, ao2_cleanup);
742
743         if (ast_strlen_zero(args->endpoint)) {
744                 ast_ari_response_error(response, 400, "Bad Request",
745                         "Endpoint must be specified");
746                 return;
747         }
748
749         dialtech = ast_strdupa(args->endpoint);
750         if ((stuff = strchr(dialtech, '/'))) {
751                 *stuff++ = '\0';
752                 ast_copy_string(dialdevice, stuff, sizeof(dialdevice));
753         }
754
755         if (ast_strlen_zero(dialtech) || ast_strlen_zero(dialdevice)) {
756                 ast_ari_response_error(response, 400, "Bad Request",
757                         "Invalid endpoint specified");
758                 return;
759         }
760
761         if (args->timeout > 0) {
762                 timeout = args->timeout * 1000;
763         } else if (args->timeout == -1) {
764                 timeout = -1;
765         }
766
767         if (!ast_strlen_zero(args->caller_id)) {
768                 caller_id = ast_strdupa(args->caller_id);
769                 ast_callerid_parse(caller_id, &cid_name, &cid_num);
770
771                 if (ast_is_shrinkable_phonenumber(cid_num)) {
772                         ast_shrink_phone_number(cid_num);
773                 }
774         }
775
776         if (!ast_strlen_zero(args->app)) {
777                 const char *app = "Stasis";
778
779                 RAII_VAR(struct ast_str *, appdata, ast_str_create(64), ast_free);
780
781                 if (!appdata) {
782                         ast_ari_response_alloc_failed(response);
783                         return;
784                 }
785
786                 ast_str_set(&appdata, 0, "%s", args->app);
787                 if (!ast_strlen_zero(args->app_args)) {
788                         ast_str_append(&appdata, 0, ",%s", args->app_args);
789                 }
790
791                 /* originate a channel, putting it into an application */
792                 if (ast_pbx_outgoing_app(dialtech, NULL, dialdevice, timeout, app, ast_str_buffer(appdata), NULL, 0, cid_num, cid_name, NULL, NULL, &chan)) {
793                         ast_ari_response_alloc_failed(response);
794                         return;
795                 }
796         } else if (!ast_strlen_zero(args->extension)) {
797                 /* originate a channel, sending it to an extension */
798                 if (ast_pbx_outgoing_exten(dialtech, NULL, dialdevice, timeout, S_OR(args->context, "default"), args->extension, args->priority ? args->priority : 1, NULL, 0, cid_num, cid_name, NULL, NULL, &chan, 0)) {
799                         ast_ari_response_alloc_failed(response);
800                         return;
801                 }
802         } else {
803                 ast_ari_response_error(response, 400, "Bad Request",
804                         "Application or extension must be specified");
805                 return;
806         }
807
808         if (ari_channels_set_channel_vars(chan, args->variables, response)) {
809                 /* response filled in by called function */
810                 return;
811         }
812
813         snapshot = ast_channel_snapshot_create(chan);
814         ast_channel_unlock(chan);
815
816         if (!ast_strlen_zero(args->app)) {
817                 /* channel: + channel ID + null terminator */
818                 char uri[9 + strlen(ast_channel_uniqueid(chan))];
819                 const char *uris[1] = { uri, };
820
821                 sprintf(uri, "channel:%s", ast_channel_uniqueid(chan));
822                 stasis_app_subscribe(args->app, uris, 1, NULL);
823         }
824
825         ast_ari_response_ok(response, ast_channel_snapshot_to_json(snapshot, NULL));
826         ast_channel_unref(chan);
827 }
828
829 void ast_ari_channels_get_channel_var(struct ast_variable *headers,
830         struct ast_ari_channels_get_channel_var_args *args,
831         struct ast_ari_response *response)
832 {
833         RAII_VAR(struct ast_json *, json, NULL, ast_json_unref);
834         RAII_VAR(struct stasis_app_control *, control, NULL, ao2_cleanup);
835         RAII_VAR(char *, value, NULL, ast_free);
836
837         ast_assert(response != NULL);
838
839         if (ast_strlen_zero(args->variable)) {
840                 ast_ari_response_error(
841                         response, 400, "Bad Request",
842                         "Variable name is required");
843                 return;
844         }
845
846         control = find_control(response, args->channel_id);
847         if (control == NULL) {
848                 /* response filled in by find_control */
849                 return;
850         }
851
852         value = stasis_app_control_get_channel_var(control, args->variable);
853
854         if (!(json = ast_json_pack("{s: s}", "value", S_OR(value, "")))) {
855                 ast_ari_response_alloc_failed(response);
856                 return;
857         }
858
859         ast_ari_response_ok(response, ast_json_ref(json));
860 }
861
862 void ast_ari_channels_set_channel_var(struct ast_variable *headers,
863         struct ast_ari_channels_set_channel_var_args *args,
864         struct ast_ari_response *response)
865 {
866         RAII_VAR(struct stasis_app_control *, control, NULL, ao2_cleanup);
867
868         ast_assert(response != NULL);
869
870         if (ast_strlen_zero(args->variable)) {
871                 ast_ari_response_error(
872                         response, 400, "Bad Request",
873                         "Variable name is required");
874                 return;
875         }
876
877         control = find_control(response, args->channel_id);
878         if (control == NULL) {
879                 /* response filled in by find_control */
880                 return;
881         }
882
883         if (stasis_app_control_set_channel_var(control, args->variable, args->value)) {
884                 ast_ari_response_error(
885                         response, 400, "Bad Request",
886                         "Failed to execute function");
887                 return;
888         }
889
890         ast_ari_response_no_content(response);
891 }
892
893 void ast_ari_channels_snoop_channel(struct ast_variable *headers, struct ast_ari_channels_snoop_channel_args *args, struct ast_ari_response *response)
894 {
895         enum stasis_app_snoop_direction spy, whisper;
896         RAII_VAR(struct ast_channel *, chan, NULL, ast_channel_cleanup);
897         RAII_VAR(struct ast_channel *, snoop, NULL, ast_channel_cleanup);
898         RAII_VAR(struct ast_channel_snapshot *, snapshot, NULL, ao2_cleanup);
899
900         ast_assert(response != NULL);
901
902         if (ast_strlen_zero(args->spy) || !strcmp(args->spy, "none")) {
903                 spy = STASIS_SNOOP_DIRECTION_NONE;
904         } else if (!strcmp(args->spy, "both")) {
905                 spy = STASIS_SNOOP_DIRECTION_BOTH;
906         } else if (!strcmp(args->spy, "out")) {
907                 spy = STASIS_SNOOP_DIRECTION_OUT;
908         } else if (!strcmp(args->spy, "in")) {
909                 spy = STASIS_SNOOP_DIRECTION_IN;
910         } else {
911                 ast_ari_response_error(
912                         response, 400, "Bad Request",
913                         "Invalid direction specified for spy");
914                 return;
915         }
916
917         if (ast_strlen_zero(args->whisper) || !strcmp(args->whisper, "none")) {
918                 whisper = STASIS_SNOOP_DIRECTION_NONE;
919         } else if (!strcmp(args->whisper, "both")) {
920                 whisper = STASIS_SNOOP_DIRECTION_BOTH;
921         } else if (!strcmp(args->whisper, "out")) {
922                 whisper = STASIS_SNOOP_DIRECTION_OUT;
923         } else if (!strcmp(args->whisper, "in")) {
924                 whisper = STASIS_SNOOP_DIRECTION_IN;
925         } else {
926                 ast_ari_response_error(
927                         response, 400, "Bad Request",
928                         "Invalid direction specified for whisper");
929                 return;
930         }
931
932         if (spy == STASIS_SNOOP_DIRECTION_NONE && whisper == STASIS_SNOOP_DIRECTION_NONE) {
933                 ast_ari_response_error(
934                         response, 400, "Bad Request",
935                         "Direction must be specified for at least spy or whisper");
936                 return;
937         } else if (ast_strlen_zero(args->app)) {
938                 ast_ari_response_error(
939                         response, 400, "Bad Request",
940                         "Application name is required");
941                 return;
942         }
943
944         chan = ast_channel_get_by_name(args->channel_id);
945         if (chan == NULL) {
946                 ast_ari_response_error(
947                         response, 404, "Channel Not Found",
948                         "Provided channel was not found");
949                 return;
950         }
951
952         snoop = stasis_app_control_snoop(chan, spy, whisper, args->app, args->app_args);
953         if (snoop == NULL) {
954                 ast_ari_response_error(
955                         response, 500, "Internal error",
956                         "Snoop channel could not be created");
957                 return;
958         }
959
960         snapshot = ast_channel_snapshot_create(snoop);
961         ast_ari_response_ok(response, ast_channel_snapshot_to_json(snapshot, NULL));
962 }