resource_channels: Check if a passed in ID is NULL before checking its length
[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 static void ari_channels_handle_play(
358         const char *args_channel_id,
359         const char *args_media,
360         const char *args_lang,
361         int args_offsetms,
362         int args_skipms,
363         const char *args_playback_id,
364         struct ast_ari_response *response)
365 {
366         RAII_VAR(struct stasis_app_control *, control, NULL, ao2_cleanup);
367         RAII_VAR(struct ast_channel_snapshot *, snapshot, NULL, ao2_cleanup);
368         RAII_VAR(struct stasis_app_playback *, playback, NULL, ao2_cleanup);
369         RAII_VAR(char *, playback_url, NULL, ast_free);
370         struct ast_json *json;
371         const char *language;
372
373         ast_assert(response != NULL);
374
375         control = find_control(response, args_channel_id);
376         if (control == NULL) {
377                 /* Response filled in by find_control */
378                 return;
379         }
380
381         snapshot = stasis_app_control_get_snapshot(control);
382         if (!snapshot) {
383                 ast_ari_response_error(
384                         response, 404, "Not Found",
385                         "Channel not found");
386                 return;
387         }
388
389         if (args_skipms < 0) {
390                 ast_ari_response_error(
391                         response, 400, "Bad Request",
392                         "skipms cannot be negative");
393                 return;
394         }
395
396         if (args_offsetms < 0) {
397                 ast_ari_response_error(
398                         response, 400, "Bad Request",
399                         "offsetms cannot be negative");
400                 return;
401         }
402
403         language = S_OR(args_lang, snapshot->language);
404
405         playback = stasis_app_control_play_uri(control, args_media, language,
406                 args_channel_id, STASIS_PLAYBACK_TARGET_CHANNEL, args_skipms, args_offsetms, args_playback_id);
407         if (!playback) {
408                 ast_ari_response_error(
409                         response, 500, "Internal Server Error",
410                         "Failed to queue media for playback");
411                 return;
412         }
413
414         ast_asprintf(&playback_url, "/playback/%s",
415                 stasis_app_playback_get_id(playback));
416         if (!playback_url) {
417                 ast_ari_response_error(
418                         response, 500, "Internal Server Error",
419                         "Out of memory");
420                 return;
421         }
422
423         json = stasis_app_playback_to_json(playback);
424         if (!json) {
425                 ast_ari_response_error(
426                         response, 500, "Internal Server Error",
427                         "Out of memory");
428                 return;
429         }
430
431         ast_ari_response_created(response, playback_url, json);
432 }
433
434 void ast_ari_channels_play(struct ast_variable *headers,
435         struct ast_ari_channels_play_args *args,
436         struct ast_ari_response *response)
437 {
438         ari_channels_handle_play(
439                 args->channel_id,
440                 args->media,
441                 args->lang,
442                 args->offsetms,
443                 args->skipms,
444                 args->playback_id,
445                 response);
446 }
447
448 void ast_ari_channels_play_with_id(struct ast_variable *headers,
449         struct ast_ari_channels_play_with_id_args *args,
450         struct ast_ari_response *response)
451 {
452         ari_channels_handle_play(
453                 args->channel_id,
454                 args->media,
455                 args->lang,
456                 args->offsetms,
457                 args->skipms,
458                 args->playback_id,
459                 response);
460 }
461
462 void ast_ari_channels_record(struct ast_variable *headers,
463         struct ast_ari_channels_record_args *args,
464         struct ast_ari_response *response)
465 {
466         RAII_VAR(struct stasis_app_control *, control, NULL, ao2_cleanup);
467         RAII_VAR(struct ast_channel_snapshot *, snapshot, NULL, ao2_cleanup);
468         RAII_VAR(struct stasis_app_recording *, recording, NULL, ao2_cleanup);
469         RAII_VAR(char *, recording_url, NULL, ast_free);
470         struct ast_json *json;
471         RAII_VAR(struct stasis_app_recording_options *, options, NULL,
472                 ao2_cleanup);
473         RAII_VAR(char *, uri_encoded_name, NULL, ast_free);
474         size_t uri_name_maxlen;
475
476         ast_assert(response != NULL);
477
478         if (args->max_duration_seconds < 0) {
479                 ast_ari_response_error(
480                         response, 400, "Bad Request",
481                         "max_duration_seconds cannot be negative");
482                 return;
483         }
484
485         if (args->max_silence_seconds < 0) {
486                 ast_ari_response_error(
487                         response, 400, "Bad Request",
488                         "max_silence_seconds cannot be negative");
489                 return;
490         }
491
492         control = find_control(response, args->channel_id);
493         if (control == NULL) {
494                 /* Response filled in by find_control */
495                 return;
496         }
497
498         options = stasis_app_recording_options_create(args->name, args->format);
499         if (options == NULL) {
500                 ast_ari_response_error(
501                         response, 500, "Internal Server Error",
502                         "Out of memory");
503         }
504         ast_string_field_build(options, target, "channel:%s", args->channel_id);
505         options->max_silence_seconds = args->max_silence_seconds;
506         options->max_duration_seconds = args->max_duration_seconds;
507         options->terminate_on =
508                 stasis_app_recording_termination_parse(args->terminate_on);
509         options->if_exists =
510                 stasis_app_recording_if_exists_parse(args->if_exists);
511         options->beep = args->beep;
512
513         if (options->terminate_on == STASIS_APP_RECORDING_TERMINATE_INVALID) {
514                 ast_ari_response_error(
515                         response, 400, "Bad Request",
516                         "terminateOn invalid");
517                 return;
518         }
519
520         if (options->if_exists == -1) {
521                 ast_ari_response_error(
522                         response, 400, "Bad Request",
523                         "ifExists invalid");
524                 return;
525         }
526
527         if (!ast_get_format_for_file_ext(options->format)) {
528                 ast_ari_response_error(
529                         response, 422, "Unprocessable Entity",
530                         "specified format is unknown on this system");
531                 return;
532         }
533
534         recording = stasis_app_control_record(control, options);
535         if (recording == NULL) {
536                 switch(errno) {
537                 case EINVAL:
538                         /* While the arguments are invalid, we should have
539                          * caught them prior to calling record.
540                          */
541                         ast_ari_response_error(
542                                 response, 500, "Internal Server Error",
543                                 "Error parsing request");
544                         break;
545                 case EEXIST:
546                         ast_ari_response_error(response, 409, "Conflict",
547                                 "Recording '%s' already exists and can not be overwritten",
548                                 args->name);
549                         break;
550                 case ENOMEM:
551                         ast_ari_response_error(
552                                 response, 500, "Internal Server Error",
553                                 "Out of memory");
554                         break;
555                 case EPERM:
556                         ast_ari_response_error(
557                                 response, 400, "Bad Request",
558                                 "Recording name invalid");
559                         break;
560                 default:
561                         ast_log(LOG_WARNING,
562                                 "Unrecognized recording error: %s\n",
563                                 strerror(errno));
564                         ast_ari_response_error(
565                                 response, 500, "Internal Server Error",
566                                 "Internal Server Error");
567                         break;
568                 }
569                 return;
570         }
571
572         uri_name_maxlen = strlen(args->name) * 3;
573         uri_encoded_name = ast_malloc(uri_name_maxlen);
574         if (!uri_encoded_name) {
575                 ast_ari_response_error(
576                         response, 500, "Internal Server Error",
577                         "Out of memory");
578                 return;
579         }
580         ast_uri_encode(args->name, uri_encoded_name, uri_name_maxlen,
581                 ast_uri_http);
582
583         ast_asprintf(&recording_url, "/recordings/live/%s", uri_encoded_name);
584         if (!recording_url) {
585                 ast_ari_response_error(
586                         response, 500, "Internal Server Error",
587                         "Out of memory");
588                 return;
589         }
590
591         json = stasis_app_recording_to_json(recording);
592         if (!json) {
593                 ast_ari_response_error(
594                         response, 500, "Internal Server Error",
595                         "Out of memory");
596                 return;
597         }
598
599         ast_ari_response_created(response, recording_url, json);
600 }
601
602 void ast_ari_channels_get(struct ast_variable *headers,
603         struct ast_ari_channels_get_args *args,
604         struct ast_ari_response *response)
605 {
606         RAII_VAR(struct stasis_message *, msg, NULL, ao2_cleanup);
607         struct stasis_cache *cache;
608         struct ast_channel_snapshot *snapshot;
609
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
618         msg = stasis_cache_get(cache, ast_channel_snapshot_type(),
619                                    args->channel_id);
620         if (!msg) {
621                 ast_ari_response_error(
622                         response, 404, "Not Found",
623                         "Channel not found");
624                 return;
625         }
626
627         snapshot = stasis_message_data(msg);
628         ast_assert(snapshot != NULL);
629
630         ast_ari_response_ok(response,
631                                 ast_channel_snapshot_to_json(snapshot, NULL));
632 }
633
634 void ast_ari_channels_hangup(struct ast_variable *headers,
635         struct ast_ari_channels_hangup_args *args,
636         struct ast_ari_response *response)
637 {
638         RAII_VAR(struct ast_channel *, chan, NULL, ao2_cleanup);
639         int cause;
640
641         chan = ast_channel_get_by_name(args->channel_id);
642         if (chan == NULL) {
643                 ast_ari_response_error(
644                         response, 404, "Not Found",
645                         "Channel not found");
646                 return;
647         }
648
649         if (ast_strlen_zero(args->reason) || !strcmp(args->reason, "normal")) {
650                 cause = AST_CAUSE_NORMAL;
651         } else if (!strcmp(args->reason, "busy")) {
652                 cause = AST_CAUSE_BUSY;
653         } else if (!strcmp(args->reason, "congestion")) {
654                 cause = AST_CAUSE_CONGESTION;
655         } else {
656                 ast_ari_response_error(
657                         response, 400, "Invalid Reason",
658                         "Invalid reason for hangup provided");
659                 return;
660         }
661
662         ast_channel_hangupcause_set(chan, cause);
663         ast_softhangup(chan, AST_SOFTHANGUP_EXPLICIT);
664
665         ast_ari_response_no_content(response);
666 }
667
668 void ast_ari_channels_list(struct ast_variable *headers,
669         struct ast_ari_channels_list_args *args,
670         struct ast_ari_response *response)
671 {
672         RAII_VAR(struct stasis_cache *, cache, NULL, ao2_cleanup);
673         RAII_VAR(struct ao2_container *, snapshots, NULL, ao2_cleanup);
674         RAII_VAR(struct ast_json *, json, NULL, ast_json_unref);
675         struct ao2_iterator i;
676         void *obj;
677         struct stasis_message_sanitizer *sanitize = stasis_app_get_sanitizer();
678
679         cache = ast_channel_cache();
680         if (!cache) {
681                 ast_ari_response_error(
682                         response, 500, "Internal Server Error",
683                         "Message bus not initialized");
684                 return;
685         }
686         ao2_ref(cache, +1);
687
688         snapshots = stasis_cache_dump(cache, ast_channel_snapshot_type());
689         if (!snapshots) {
690                 ast_ari_response_alloc_failed(response);
691                 return;
692         }
693
694         json = ast_json_array_create();
695         if (!json) {
696                 ast_ari_response_alloc_failed(response);
697                 return;
698         }
699
700         i = ao2_iterator_init(snapshots, 0);
701         while ((obj = ao2_iterator_next(&i))) {
702                 RAII_VAR(struct stasis_message *, msg, obj, ao2_cleanup);
703                 struct ast_channel_snapshot *snapshot = stasis_message_data(msg);
704                 int r;
705
706                 if (sanitize && sanitize->channel_snapshot
707                         && sanitize->channel_snapshot(snapshot)) {
708                         continue;
709                 }
710
711                 r = ast_json_array_append(
712                         json, ast_channel_snapshot_to_json(snapshot, NULL));
713                 if (r != 0) {
714                         ast_ari_response_alloc_failed(response);
715                         ao2_iterator_destroy(&i);
716                         return;
717                 }
718         }
719         ao2_iterator_destroy(&i);
720
721         ast_ari_response_ok(response, ast_json_ref(json));
722 }
723
724 static int json_to_ast_variables(struct ast_json *json_variables, struct ast_variable **variables)
725 {
726         struct ast_variable *current = NULL;
727         struct ast_json_iter *it_json_var;
728
729         for (it_json_var = ast_json_object_iter(json_variables); it_json_var;
730                  it_json_var = ast_json_object_iter_next(json_variables, it_json_var)) {
731                 struct ast_variable *new_var;
732
733                 new_var = ast_variable_new(ast_json_object_iter_key(it_json_var),
734                                                                    ast_json_string_get(ast_json_object_iter_value(it_json_var)),
735                                                                    "");
736                 if (!new_var) {
737                         ast_variables_destroy(*variables);
738                         *variables = NULL;
739                         return 1;
740                 }
741
742                 if (!current) {
743                         *variables = new_var;
744                         current = *variables;
745                 } else {
746                         current->next = new_var;
747                         current = new_var;
748                 }
749         }
750
751         return 0;
752 }
753
754 static void ari_channels_handle_originate_with_id(const char *args_endpoint,
755         const char *args_extension,
756         const char *args_context,
757         long args_priority,
758         const char *args_app,
759         const char *args_app_args,
760         const char *args_caller_id,
761         int args_timeout,
762         struct ast_variable *variables,
763         const char *args_channel_id,
764         const char *args_other_channel_id,
765         struct ast_ari_response *response)
766 {
767         char *dialtech;
768         char dialdevice[AST_CHANNEL_NAME];
769         char *caller_id = NULL;
770         char *cid_num = NULL;
771         char *cid_name = NULL;
772         int timeout = 30000;
773         RAII_VAR(struct ast_format_cap *, cap,
774                 ast_format_cap_alloc(AST_FORMAT_CAP_FLAG_NOLOCK), ast_format_cap_destroy);
775         struct ast_format tmp_fmt;
776         char *stuff;
777         struct ast_channel *chan;
778         RAII_VAR(struct ast_channel_snapshot *, snapshot, NULL, ao2_cleanup);
779         struct ast_assigned_ids assignedids = {args_channel_id, args_other_channel_id};
780
781         if ((!ast_strlen_zero(assignedids.uniqueid) && strlen(assignedids.uniqueid) >= AST_MAX_UNIQUEID) || 
782                 (!ast_strlen_zero(assignedids.uniqueid) && strlen(assignedids.uniqueid2) >= AST_MAX_UNIQUEID)) {
783                 ast_log(LOG_WARNING, "Uniqueid length exceeds maximum of %d\n", AST_MAX_UNIQUEID);
784         }
785
786         if (!cap) {
787                 ast_ari_response_alloc_failed(response);
788                 return;
789         }
790         ast_format_cap_add(cap, ast_format_set(&tmp_fmt, AST_FORMAT_SLINEAR, 0));
791
792         if (ast_strlen_zero(args_endpoint)) {
793                 ast_ari_response_error(response, 400, "Bad Request",
794                         "Endpoint must be specified");
795                 return;
796         }
797
798         dialtech = ast_strdupa(args_endpoint);
799         if ((stuff = strchr(dialtech, '/'))) {
800                 *stuff++ = '\0';
801                 ast_copy_string(dialdevice, stuff, sizeof(dialdevice));
802         }
803
804         if (ast_strlen_zero(dialtech) || ast_strlen_zero(dialdevice)) {
805                 ast_ari_response_error(response, 400, "Bad Request",
806                         "Invalid endpoint specified");
807                 return;
808         }
809
810         if (args_timeout > 0) {
811                 timeout = args_timeout * 1000;
812         } else if (args_timeout == -1) {
813                 timeout = -1;
814         }
815
816         if (!ast_strlen_zero(args_caller_id)) {
817                 caller_id = ast_strdupa(args_caller_id);
818                 ast_callerid_parse(caller_id, &cid_name, &cid_num);
819
820                 if (ast_is_shrinkable_phonenumber(cid_num)) {
821                         ast_shrink_phone_number(cid_num);
822                 }
823         }
824
825         if (!ast_strlen_zero(args_app)) {
826                 const char *app = "Stasis";
827
828                 RAII_VAR(struct ast_str *, appdata, ast_str_create(64), ast_free);
829
830                 if (!appdata) {
831                         ast_ari_response_alloc_failed(response);
832                         return;
833                 }
834
835                 ast_str_set(&appdata, 0, "%s", args_app);
836                 if (!ast_strlen_zero(args_app_args)) {
837                         ast_str_append(&appdata, 0, ",%s", args_app_args);
838                 }
839
840                 /* originate a channel, putting it into an application */
841                 if (ast_pbx_outgoing_app(dialtech, cap, dialdevice, timeout, app, ast_str_buffer(appdata), NULL, 0, cid_num, cid_name, variables, NULL, &chan, &assignedids)) {
842                         ast_ari_response_alloc_failed(response);
843                         return;
844                 }
845         } else if (!ast_strlen_zero(args_extension)) {
846                 /* originate a channel, sending it to an extension */
847                 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, &assignedids)) {
848                         ast_ari_response_alloc_failed(response);
849                         return;
850                 }
851         } else {
852                 ast_ari_response_error(response, 400, "Bad Request",
853                         "Application or extension must be specified");
854                 return;
855         }
856
857         snapshot = ast_channel_snapshot_create(chan);
858         ast_channel_unlock(chan);
859
860         if (!ast_strlen_zero(args_app)) {
861                 /* channel: + channel ID + null terminator */
862                 char uri[9 + strlen(ast_channel_uniqueid(chan))];
863                 const char *uris[1] = { uri, };
864
865                 sprintf(uri, "channel:%s", ast_channel_uniqueid(chan));
866                 stasis_app_subscribe(args_app, uris, 1, NULL);
867         }
868
869         ast_ari_response_ok(response, ast_channel_snapshot_to_json(snapshot, NULL));
870         ast_channel_unref(chan);
871 }
872
873 void ast_ari_channels_originate_with_id(struct ast_variable *headers,
874         struct ast_ari_channels_originate_with_id_args *args,
875         struct ast_ari_response *response)
876 {
877         RAII_VAR(struct ast_variable *, variables, NULL, ast_variables_destroy);
878
879         /* Parse any query parameters out of the body parameter */
880         if (args->variables) {
881                 struct ast_json *json_variables;
882
883                 ast_ari_channels_originate_with_id_parse_body(args->variables, args);
884                 json_variables = ast_json_object_get(args->variables, "variables");
885                 if (json_variables) {
886                         if (json_to_ast_variables(json_variables, &variables)) {
887                                 ast_log(AST_LOG_ERROR, "Unable to convert 'variables' in JSON body to channel variables\n");
888                                 ast_ari_response_alloc_failed(response);
889                                 return;
890                         }
891                 }
892         }
893
894         ari_channels_handle_originate_with_id(
895                 args->endpoint,
896                 args->extension,
897                 args->context,
898                 args->priority,
899                 args->app,
900                 args->app_args,
901                 args->caller_id,
902                 args->timeout,
903                 variables,
904                 args->channel_id,
905                 args->other_channel_id,
906                 response);
907 }
908
909 void ast_ari_channels_originate(struct ast_variable *headers,
910         struct ast_ari_channels_originate_args *args,
911         struct ast_ari_response *response)
912 {
913         RAII_VAR(struct ast_variable *, variables, NULL, ast_variables_destroy);
914
915         /* Parse any query parameters out of the body parameter */
916         if (args->variables) {
917                 struct ast_json *json_variables;
918
919                 ast_ari_channels_originate_parse_body(args->variables, args);
920                 json_variables = ast_json_object_get(args->variables, "variables");
921                 if (json_variables) {
922                         if (json_to_ast_variables(json_variables, &variables)) {
923                                 ast_log(AST_LOG_ERROR, "Unable to convert 'variables' in JSON body to channel variables\n");
924                                 ast_ari_response_alloc_failed(response);
925                                 return;
926                         }
927                 }
928         }
929
930         ari_channels_handle_originate_with_id(
931                 args->endpoint,
932                 args->extension,
933                 args->context,
934                 args->priority,
935                 args->app,
936                 args->app_args,
937                 args->caller_id,
938                 args->timeout,
939                 variables,
940                 args->channel_id,
941                 args->other_channel_id,
942                 response);
943 }
944
945 void ast_ari_channels_get_channel_var(struct ast_variable *headers,
946         struct ast_ari_channels_get_channel_var_args *args,
947         struct ast_ari_response *response)
948 {
949         RAII_VAR(struct ast_json *, json, NULL, ast_json_unref);
950         RAII_VAR(struct stasis_app_control *, control, NULL, ao2_cleanup);
951         RAII_VAR(char *, value, NULL, ast_free);
952
953         ast_assert(response != NULL);
954
955         if (ast_strlen_zero(args->variable)) {
956                 ast_ari_response_error(
957                         response, 400, "Bad Request",
958                         "Variable name is required");
959                 return;
960         }
961
962         control = find_control(response, args->channel_id);
963         if (control == NULL) {
964                 /* response filled in by find_control */
965                 return;
966         }
967
968         value = stasis_app_control_get_channel_var(control, args->variable);
969
970         if (!(json = ast_json_pack("{s: s}", "value", S_OR(value, "")))) {
971                 ast_ari_response_alloc_failed(response);
972                 return;
973         }
974
975         ast_ari_response_ok(response, ast_json_ref(json));
976 }
977
978 void ast_ari_channels_set_channel_var(struct ast_variable *headers,
979         struct ast_ari_channels_set_channel_var_args *args,
980         struct ast_ari_response *response)
981 {
982         RAII_VAR(struct stasis_app_control *, control, NULL, ao2_cleanup);
983
984         ast_assert(response != NULL);
985
986         if (ast_strlen_zero(args->variable)) {
987                 ast_ari_response_error(
988                         response, 400, "Bad Request",
989                         "Variable name is required");
990                 return;
991         }
992
993         control = find_control(response, args->channel_id);
994         if (control == NULL) {
995                 /* response filled in by find_control */
996                 return;
997         }
998
999         if (stasis_app_control_set_channel_var(control, args->variable, args->value)) {
1000                 ast_ari_response_error(
1001                         response, 400, "Bad Request",
1002                         "Failed to execute function");
1003                 return;
1004         }
1005
1006         ast_ari_response_no_content(response);
1007 }
1008
1009 static void ari_channels_handle_snoop_channel(
1010         const char *args_channel_id,
1011         const char *args_spy,
1012         const char *args_whisper,
1013         const char *args_app,
1014         const char *args_app_args,
1015         const char *args_snoop_id,
1016         struct ast_ari_response *response)
1017 {
1018         enum stasis_app_snoop_direction spy, whisper;
1019         RAII_VAR(struct ast_channel *, chan, NULL, ast_channel_cleanup);
1020         RAII_VAR(struct ast_channel *, snoop, NULL, ast_channel_cleanup);
1021         RAII_VAR(struct ast_channel_snapshot *, snapshot, NULL, ao2_cleanup);
1022
1023         ast_assert(response != NULL);
1024
1025         if (ast_strlen_zero(args_spy) || !strcmp(args_spy, "none")) {
1026                 spy = STASIS_SNOOP_DIRECTION_NONE;
1027         } else if (!strcmp(args_spy, "both")) {
1028                 spy = STASIS_SNOOP_DIRECTION_BOTH;
1029         } else if (!strcmp(args_spy, "out")) {
1030                 spy = STASIS_SNOOP_DIRECTION_OUT;
1031         } else if (!strcmp(args_spy, "in")) {
1032                 spy = STASIS_SNOOP_DIRECTION_IN;
1033         } else {
1034                 ast_ari_response_error(
1035                         response, 400, "Bad Request",
1036                         "Invalid direction specified for spy");
1037                 return;
1038         }
1039
1040         if (ast_strlen_zero(args_whisper) || !strcmp(args_whisper, "none")) {
1041                 whisper = STASIS_SNOOP_DIRECTION_NONE;
1042         } else if (!strcmp(args_whisper, "both")) {
1043                 whisper = STASIS_SNOOP_DIRECTION_BOTH;
1044         } else if (!strcmp(args_whisper, "out")) {
1045                 whisper = STASIS_SNOOP_DIRECTION_OUT;
1046         } else if (!strcmp(args_whisper, "in")) {
1047                 whisper = STASIS_SNOOP_DIRECTION_IN;
1048         } else {
1049                 ast_ari_response_error(
1050                         response, 400, "Bad Request",
1051                         "Invalid direction specified for whisper");
1052                 return;
1053         }
1054
1055         if (spy == STASIS_SNOOP_DIRECTION_NONE && whisper == STASIS_SNOOP_DIRECTION_NONE) {
1056                 ast_ari_response_error(
1057                         response, 400, "Bad Request",
1058                         "Direction must be specified for at least spy or whisper");
1059                 return;
1060         } else if (ast_strlen_zero(args_app)) {
1061                 ast_ari_response_error(
1062                         response, 400, "Bad Request",
1063                         "Application name is required");
1064                 return;
1065         }
1066
1067         chan = ast_channel_get_by_name(args_channel_id);
1068         if (chan == NULL) {
1069                 ast_ari_response_error(
1070                         response, 404, "Channel Not Found",
1071                         "Provided channel was not found");
1072                 return;
1073         }
1074
1075         snoop = stasis_app_control_snoop(chan, spy, whisper, args_app, args_app_args,
1076                 args_snoop_id);
1077         if (snoop == NULL) {
1078                 ast_ari_response_error(
1079                         response, 500, "Internal error",
1080                         "Snoop channel could not be created");
1081                 return;
1082         }
1083
1084         snapshot = ast_channel_snapshot_create(snoop);
1085         ast_ari_response_ok(response, ast_channel_snapshot_to_json(snapshot, NULL));
1086 }
1087
1088 void ast_ari_channels_snoop_channel(struct ast_variable *headers,
1089         struct ast_ari_channels_snoop_channel_args *args,
1090         struct ast_ari_response *response)
1091 {
1092         ari_channels_handle_snoop_channel(
1093                 args->channel_id,
1094                 args->spy,
1095                 args->whisper,
1096                 args->app,
1097                 args->app_args,
1098                 args->snoop_id,
1099                 response);
1100 }
1101
1102 void ast_ari_channels_snoop_channel_with_id(struct ast_variable *headers,
1103         struct ast_ari_channels_snoop_channel_with_id_args *args,
1104         struct ast_ari_response *response)
1105 {
1106         ari_channels_handle_snoop_channel(
1107                 args->channel_id,
1108                 args->spy,
1109                 args->whisper,
1110                 args->app,
1111                 args->app_args,
1112                 args->snoop_id,
1113                 response);
1114 }