res_ari: Fix some off-nominal paths just dropping the HTTP connection.
[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         if (ast_asprintf(&playback_url, "/playback/%s",
415                         stasis_app_playback_get_id(playback)) == -1) {
416                 playback_url = NULL;
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         if (ast_asprintf(&recording_url, "/recordings/live/%s",
583                         uri_encoded_name) == -1) {
584                 recording_url = NULL;
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 = {
780                 .uniqueid = args_channel_id,
781                 .uniqueid2 = args_other_channel_id,
782         };
783
784         if (!cap) {
785                 ast_ari_response_alloc_failed(response);
786                 return;
787         }
788         ast_format_cap_add(cap, ast_format_set(&tmp_fmt, AST_FORMAT_SLINEAR, 0));
789
790         if ((assignedids.uniqueid && AST_MAX_PUBLIC_UNIQUEID < strlen(assignedids.uniqueid))
791                 || (assignedids.uniqueid2 && AST_MAX_PUBLIC_UNIQUEID < strlen(assignedids.uniqueid2))) {
792                 ast_ari_response_error(response, 400, "Bad Request",
793                         "Uniqueid length exceeds maximum of %d", AST_MAX_PUBLIC_UNIQUEID);
794                 return;
795         }
796
797         if (ast_strlen_zero(args_endpoint)) {
798                 ast_ari_response_error(response, 400, "Bad Request",
799                         "Endpoint must be specified");
800                 return;
801         }
802
803         dialtech = ast_strdupa(args_endpoint);
804         if ((stuff = strchr(dialtech, '/'))) {
805                 *stuff++ = '\0';
806                 ast_copy_string(dialdevice, stuff, sizeof(dialdevice));
807         }
808
809         if (ast_strlen_zero(dialtech) || ast_strlen_zero(dialdevice)) {
810                 ast_ari_response_error(response, 400, "Bad Request",
811                         "Invalid endpoint specified");
812                 return;
813         }
814
815         if (args_timeout > 0) {
816                 timeout = args_timeout * 1000;
817         } else if (args_timeout == -1) {
818                 timeout = -1;
819         }
820
821         if (!ast_strlen_zero(args_caller_id)) {
822                 caller_id = ast_strdupa(args_caller_id);
823                 ast_callerid_parse(caller_id, &cid_name, &cid_num);
824
825                 if (ast_is_shrinkable_phonenumber(cid_num)) {
826                         ast_shrink_phone_number(cid_num);
827                 }
828         }
829
830         if (!ast_strlen_zero(args_app)) {
831                 const char *app = "Stasis";
832
833                 RAII_VAR(struct ast_str *, appdata, ast_str_create(64), ast_free);
834
835                 if (!appdata) {
836                         ast_ari_response_alloc_failed(response);
837                         return;
838                 }
839
840                 ast_str_set(&appdata, 0, "%s", args_app);
841                 if (!ast_strlen_zero(args_app_args)) {
842                         ast_str_append(&appdata, 0, ",%s", args_app_args);
843                 }
844
845                 /* originate a channel, putting it into an application */
846                 if (ast_pbx_outgoing_app(dialtech, cap, dialdevice, timeout, app, ast_str_buffer(appdata), NULL, 0, cid_num, cid_name, variables, NULL, &chan, &assignedids)) {
847                         ast_ari_response_alloc_failed(response);
848                         return;
849                 }
850         } else if (!ast_strlen_zero(args_extension)) {
851                 /* originate a channel, sending it to an extension */
852                 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)) {
853                         ast_ari_response_alloc_failed(response);
854                         return;
855                 }
856         } else {
857                 ast_ari_response_error(response, 400, "Bad Request",
858                         "Application or extension must be specified");
859                 return;
860         }
861
862         snapshot = ast_channel_snapshot_get_latest(ast_channel_uniqueid(chan));
863         ast_channel_unlock(chan);
864
865         if (!ast_strlen_zero(args_app)) {
866                 /* channel: + channel ID + null terminator */
867                 char uri[9 + strlen(ast_channel_uniqueid(chan))];
868                 const char *uris[1] = { uri, };
869
870                 sprintf(uri, "channel:%s", ast_channel_uniqueid(chan));
871                 stasis_app_subscribe(args_app, uris, 1, NULL);
872         }
873
874         ast_ari_response_ok(response, ast_channel_snapshot_to_json(snapshot, NULL));
875         ast_channel_unref(chan);
876 }
877
878 void ast_ari_channels_originate_with_id(struct ast_variable *headers,
879         struct ast_ari_channels_originate_with_id_args *args,
880         struct ast_ari_response *response)
881 {
882         RAII_VAR(struct ast_variable *, variables, NULL, ast_variables_destroy);
883
884         /* Parse any query parameters out of the body parameter */
885         if (args->variables) {
886                 struct ast_json *json_variables;
887
888                 ast_ari_channels_originate_with_id_parse_body(args->variables, args);
889                 json_variables = ast_json_object_get(args->variables, "variables");
890                 if (json_variables) {
891                         if (json_to_ast_variables(json_variables, &variables)) {
892                                 ast_log(AST_LOG_ERROR, "Unable to convert 'variables' in JSON body to channel variables\n");
893                                 ast_ari_response_alloc_failed(response);
894                                 return;
895                         }
896                 }
897         }
898
899         ari_channels_handle_originate_with_id(
900                 args->endpoint,
901                 args->extension,
902                 args->context,
903                 args->priority,
904                 args->app,
905                 args->app_args,
906                 args->caller_id,
907                 args->timeout,
908                 variables,
909                 args->channel_id,
910                 args->other_channel_id,
911                 response);
912 }
913
914 void ast_ari_channels_originate(struct ast_variable *headers,
915         struct ast_ari_channels_originate_args *args,
916         struct ast_ari_response *response)
917 {
918         RAII_VAR(struct ast_variable *, variables, NULL, ast_variables_destroy);
919
920         /* Parse any query parameters out of the body parameter */
921         if (args->variables) {
922                 struct ast_json *json_variables;
923
924                 ast_ari_channels_originate_parse_body(args->variables, args);
925                 json_variables = ast_json_object_get(args->variables, "variables");
926                 if (json_variables) {
927                         if (json_to_ast_variables(json_variables, &variables)) {
928                                 ast_log(AST_LOG_ERROR, "Unable to convert 'variables' in JSON body to channel variables\n");
929                                 ast_ari_response_alloc_failed(response);
930                                 return;
931                         }
932                 }
933         }
934
935         ari_channels_handle_originate_with_id(
936                 args->endpoint,
937                 args->extension,
938                 args->context,
939                 args->priority,
940                 args->app,
941                 args->app_args,
942                 args->caller_id,
943                 args->timeout,
944                 variables,
945                 args->channel_id,
946                 args->other_channel_id,
947                 response);
948 }
949
950 void ast_ari_channels_get_channel_var(struct ast_variable *headers,
951         struct ast_ari_channels_get_channel_var_args *args,
952         struct ast_ari_response *response)
953 {
954         RAII_VAR(struct ast_json *, json, NULL, ast_json_unref);
955         RAII_VAR(struct stasis_app_control *, control, NULL, ao2_cleanup);
956         RAII_VAR(char *, value, NULL, ast_free);
957
958         ast_assert(response != NULL);
959
960         if (ast_strlen_zero(args->variable)) {
961                 ast_ari_response_error(
962                         response, 400, "Bad Request",
963                         "Variable name is required");
964                 return;
965         }
966
967         control = find_control(response, args->channel_id);
968         if (control == NULL) {
969                 /* response filled in by find_control */
970                 return;
971         }
972
973         value = stasis_app_control_get_channel_var(control, args->variable);
974
975         if (!(json = ast_json_pack("{s: s}", "value", S_OR(value, "")))) {
976                 ast_ari_response_alloc_failed(response);
977                 return;
978         }
979
980         ast_ari_response_ok(response, ast_json_ref(json));
981 }
982
983 void ast_ari_channels_set_channel_var(struct ast_variable *headers,
984         struct ast_ari_channels_set_channel_var_args *args,
985         struct ast_ari_response *response)
986 {
987         RAII_VAR(struct stasis_app_control *, control, NULL, ao2_cleanup);
988
989         ast_assert(response != NULL);
990
991         if (ast_strlen_zero(args->variable)) {
992                 ast_ari_response_error(
993                         response, 400, "Bad Request",
994                         "Variable name is required");
995                 return;
996         }
997
998         control = find_control(response, args->channel_id);
999         if (control == NULL) {
1000                 /* response filled in by find_control */
1001                 return;
1002         }
1003
1004         if (stasis_app_control_set_channel_var(control, args->variable, args->value)) {
1005                 ast_ari_response_error(
1006                         response, 400, "Bad Request",
1007                         "Failed to execute function");
1008                 return;
1009         }
1010
1011         ast_ari_response_no_content(response);
1012 }
1013
1014 static void ari_channels_handle_snoop_channel(
1015         const char *args_channel_id,
1016         const char *args_spy,
1017         const char *args_whisper,
1018         const char *args_app,
1019         const char *args_app_args,
1020         const char *args_snoop_id,
1021         struct ast_ari_response *response)
1022 {
1023         enum stasis_app_snoop_direction spy, whisper;
1024         RAII_VAR(struct ast_channel *, chan, NULL, ast_channel_cleanup);
1025         RAII_VAR(struct ast_channel *, snoop, NULL, ast_channel_cleanup);
1026         RAII_VAR(struct ast_channel_snapshot *, snapshot, NULL, ao2_cleanup);
1027
1028         ast_assert(response != NULL);
1029
1030         if (ast_strlen_zero(args_spy) || !strcmp(args_spy, "none")) {
1031                 spy = STASIS_SNOOP_DIRECTION_NONE;
1032         } else if (!strcmp(args_spy, "both")) {
1033                 spy = STASIS_SNOOP_DIRECTION_BOTH;
1034         } else if (!strcmp(args_spy, "out")) {
1035                 spy = STASIS_SNOOP_DIRECTION_OUT;
1036         } else if (!strcmp(args_spy, "in")) {
1037                 spy = STASIS_SNOOP_DIRECTION_IN;
1038         } else {
1039                 ast_ari_response_error(
1040                         response, 400, "Bad Request",
1041                         "Invalid direction specified for spy");
1042                 return;
1043         }
1044
1045         if (ast_strlen_zero(args_whisper) || !strcmp(args_whisper, "none")) {
1046                 whisper = STASIS_SNOOP_DIRECTION_NONE;
1047         } else if (!strcmp(args_whisper, "both")) {
1048                 whisper = STASIS_SNOOP_DIRECTION_BOTH;
1049         } else if (!strcmp(args_whisper, "out")) {
1050                 whisper = STASIS_SNOOP_DIRECTION_OUT;
1051         } else if (!strcmp(args_whisper, "in")) {
1052                 whisper = STASIS_SNOOP_DIRECTION_IN;
1053         } else {
1054                 ast_ari_response_error(
1055                         response, 400, "Bad Request",
1056                         "Invalid direction specified for whisper");
1057                 return;
1058         }
1059
1060         if (spy == STASIS_SNOOP_DIRECTION_NONE && whisper == STASIS_SNOOP_DIRECTION_NONE) {
1061                 ast_ari_response_error(
1062                         response, 400, "Bad Request",
1063                         "Direction must be specified for at least spy or whisper");
1064                 return;
1065         } else if (ast_strlen_zero(args_app)) {
1066                 ast_ari_response_error(
1067                         response, 400, "Bad Request",
1068                         "Application name is required");
1069                 return;
1070         }
1071
1072         chan = ast_channel_get_by_name(args_channel_id);
1073         if (chan == NULL) {
1074                 ast_ari_response_error(
1075                         response, 404, "Channel Not Found",
1076                         "Provided channel was not found");
1077                 return;
1078         }
1079
1080         snoop = stasis_app_control_snoop(chan, spy, whisper, args_app, args_app_args,
1081                 args_snoop_id);
1082         if (snoop == NULL) {
1083                 ast_ari_response_error(
1084                         response, 500, "Internal error",
1085                         "Snoop channel could not be created");
1086                 return;
1087         }
1088
1089         snapshot = ast_channel_snapshot_get_latest(ast_channel_uniqueid(snoop));
1090         ast_ari_response_ok(response, ast_channel_snapshot_to_json(snapshot, NULL));
1091 }
1092
1093 void ast_ari_channels_snoop_channel(struct ast_variable *headers,
1094         struct ast_ari_channels_snoop_channel_args *args,
1095         struct ast_ari_response *response)
1096 {
1097         ari_channels_handle_snoop_channel(
1098                 args->channel_id,
1099                 args->spy,
1100                 args->whisper,
1101                 args->app,
1102                 args->app_args,
1103                 args->snoop_id,
1104                 response);
1105 }
1106
1107 void ast_ari_channels_snoop_channel_with_id(struct ast_variable *headers,
1108         struct ast_ari_channels_snoop_channel_with_id_args *args,
1109         struct ast_ari_response *response)
1110 {
1111         ari_channels_handle_snoop_channel(
1112                 args->channel_id,
1113                 args->spy,
1114                 args->whisper,
1115                 args->app,
1116                 args->app_args,
1117                 args->snoop_id,
1118                 response);
1119 }