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