~singpolyma/asterisk

ref: 51af79b42e4ae7d1831603633d73bf0df179720e asterisk/main/pbx_hangup_handler.c -rw-r--r-- 7.0 KiB
51af79b4Christopher Vollick Content Created By Initiator 2: Electric Boogaloo 10 months ago
                                                                                
130ba7ae Bernhard Schmidt
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
/*
 * Asterisk -- An open source telephony toolkit.
 *
 * Copyright (C) 2016, CFWare, LLC
 *
 * Corey Farrell <git@cfware.com>
 *
 * See http://www.asterisk.org for more information about
 * the Asterisk project. Please do not directly contact
 * any of the maintainers of this project for assistance;
 * the project provides a web site, mailing lists and IRC
 * channels for your use.
 *
 * This program is free software, distributed under the terms of
 * the GNU General Public License Version 2. See the LICENSE file
 * at the top of the source tree.
 */

/*! \file
 *
 * \brief PBX Hangup Handler management routines.
 *
 * \author Corey Farrell <git@cfware.com>
 */

/*** MODULEINFO
	<support_level>core</support_level>
 ***/

#include "asterisk.h"

#include "asterisk/_private.h"
#include "asterisk/app.h"
#include "asterisk/cli.h"
#include "asterisk/linkedlists.h"
#include "asterisk/pbx.h"
#include "asterisk/stasis_channels.h"
#include "asterisk/utils.h"

/*!
 * \internal
 * \brief Publish a hangup handler related message to \ref stasis
 */
static void publish_hangup_handler_message(const char *action, struct ast_channel *chan, const char *handler)
{
	RAII_VAR(struct ast_json *, blob, NULL, ast_json_unref);

	blob = ast_json_pack("{s: s, s: s}",
			"type", action,
			"handler", S_OR(handler, ""));
	if (!blob) {
		return;
	}

	ast_channel_publish_blob(chan, ast_channel_hangup_handler_type(), blob);
}

int ast_pbx_hangup_handler_run(struct ast_channel *chan)
{
	struct ast_hangup_handler_list *handlers;
	struct ast_hangup_handler *h_handler;

	ast_channel_lock(chan);
	handlers = ast_channel_hangup_handlers(chan);
	if (AST_LIST_EMPTY(handlers)) {
		ast_channel_unlock(chan);
		return 0;
	}

	/*
	 * Make sure that the channel is marked as hungup since we are
	 * going to run the hangup handlers on it.
	 */
	ast_softhangup_nolock(chan, AST_SOFTHANGUP_HANGUP_EXEC);

	for (;;) {
		handlers = ast_channel_hangup_handlers(chan);
		h_handler = AST_LIST_REMOVE_HEAD(handlers, node);
		if (!h_handler) {
			break;
		}

		publish_hangup_handler_message("run", chan, h_handler->args);
		ast_channel_unlock(chan);

		ast_app_exec_sub(NULL, chan, h_handler->args, 1);
		ast_free(h_handler);

		ast_channel_lock(chan);
	}
	ast_channel_unlock(chan);
	return 1;
}

void ast_pbx_hangup_handler_init(struct ast_channel *chan)
{
	struct ast_hangup_handler_list *handlers;

	handlers = ast_channel_hangup_handlers(chan);
	AST_LIST_HEAD_INIT_NOLOCK(handlers);
}

void ast_pbx_hangup_handler_destroy(struct ast_channel *chan)
{
	struct ast_hangup_handler_list *handlers;
	struct ast_hangup_handler *h_handler;

	ast_channel_lock(chan);

	/* Get rid of each of the hangup handlers on the channel */
	handlers = ast_channel_hangup_handlers(chan);
	while ((h_handler = AST_LIST_REMOVE_HEAD(handlers, node))) {
		ast_free(h_handler);
	}

	ast_channel_unlock(chan);
}

int ast_pbx_hangup_handler_pop(struct ast_channel *chan)
{
	struct ast_hangup_handler_list *handlers;
	struct ast_hangup_handler *h_handler;

	ast_channel_lock(chan);
	handlers = ast_channel_hangup_handlers(chan);
	h_handler = AST_LIST_REMOVE_HEAD(handlers, node);
	if (h_handler) {
		publish_hangup_handler_message("pop", chan, h_handler->args);
	}
	ast_channel_unlock(chan);
	if (h_handler) {
		ast_free(h_handler);
		return 1;
	}
	return 0;
}

void ast_pbx_hangup_handler_push(struct ast_channel *chan, const char *handler)
{
	struct ast_hangup_handler_list *handlers;
	struct ast_hangup_handler *h_handler;
	const char *expanded_handler;

	if (ast_strlen_zero(handler)) {
		return;
	}

	expanded_handler = ast_app_expand_sub_args(chan, handler);
	if (!expanded_handler) {
		return;
	}
	h_handler = ast_malloc(sizeof(*h_handler) + 1 + strlen(expanded_handler));
	if (!h_handler) {
		ast_free((char *) expanded_handler);
		return;
	}
	strcpy(h_handler->args, expanded_handler);/* Safe */
	ast_free((char *) expanded_handler);

	ast_channel_lock(chan);

	handlers = ast_channel_hangup_handlers(chan);
	AST_LIST_INSERT_HEAD(handlers, h_handler, node);
	publish_hangup_handler_message("push", chan, h_handler->args);
	ast_channel_unlock(chan);
}

#define HANDLER_FORMAT	"%-30s %s\n"

/*!
 * \internal
 * \brief CLI output the hangup handler headers.
 * \since 11.0
 *
 * \param fd CLI file descriptor to use.
 *
 * \return Nothing
 */
static void ast_pbx_hangup_handler_headers(int fd)
{
	ast_cli(fd, HANDLER_FORMAT, "Channel", "Handler");
}

/*!
 * \internal
 * \brief CLI output the channel hangup handlers.
 * \since 11.0
 *
 * \param fd CLI file descriptor to use.
 * \param chan Channel to show hangup handlers.
 *
 * \return Nothing
 */
static void ast_pbx_hangup_handler_show(int fd, struct ast_channel *chan)
{
	struct ast_hangup_handler_list *handlers;
	struct ast_hangup_handler *h_handler;
	int first = 1;

	ast_channel_lock(chan);
	handlers = ast_channel_hangup_handlers(chan);
	AST_LIST_TRAVERSE(handlers, h_handler, node) {
		ast_cli(fd, HANDLER_FORMAT, first ? ast_channel_name(chan) : "", h_handler->args);
		first = 0;
	}
	ast_channel_unlock(chan);
}

/*
 * \brief 'show hanguphandlers <channel>' CLI command implementation function...
 */
static char *handle_show_hangup_channel(struct ast_cli_entry *e, int cmd, struct ast_cli_args *a)
{
	struct ast_channel *chan;

	switch (cmd) {
	case CLI_INIT:
		e->command = "core show hanguphandlers";
		e->usage =
			"Usage: core show hanguphandlers <channel>\n"
			"       Show hangup handlers of a specified channel.\n";
		return NULL;
	case CLI_GENERATE:
		return ast_complete_channels(a->line, a->word, a->pos, a->n, e->args);
	}

	if (a->argc < 4) {
		return CLI_SHOWUSAGE;
	}

	chan = ast_channel_get_by_name(a->argv[3]);
	if (!chan) {
		ast_cli(a->fd, "Channel does not exist.\n");
		return CLI_FAILURE;
	}

	ast_pbx_hangup_handler_headers(a->fd);
	ast_pbx_hangup_handler_show(a->fd, chan);

	ast_channel_unref(chan);

	return CLI_SUCCESS;
}

/*
 * \brief 'show hanguphandlers all' CLI command implementation function...
 */
static char *handle_show_hangup_all(struct ast_cli_entry *e, int cmd, struct ast_cli_args *a)
{
	struct ast_channel_iterator *iter;
	struct ast_channel *chan;

	switch (cmd) {
	case CLI_INIT:
		e->command = "core show hanguphandlers all";
		e->usage =
			"Usage: core show hanguphandlers all\n"
			"       Show hangup handlers for all channels.\n";
		return NULL;
	case CLI_GENERATE:
		return NULL;
	}

	if (a->argc < 4) {
		return CLI_SHOWUSAGE;
	}

	iter = ast_channel_iterator_all_new();
	if (!iter) {
		return CLI_FAILURE;
	}

	ast_pbx_hangup_handler_headers(a->fd);
	for (; (chan = ast_channel_iterator_next(iter)); ast_channel_unref(chan)) {
		ast_pbx_hangup_handler_show(a->fd, chan);
	}
	ast_channel_iterator_destroy(iter);

	return CLI_SUCCESS;
}

static struct ast_cli_entry cli[] = {
	AST_CLI_DEFINE(handle_show_hangup_all, "Show hangup handlers of all channels"),
	AST_CLI_DEFINE(handle_show_hangup_channel, "Show hangup handlers of a specified channel"),
};

static void unload_pbx_hangup_handler(void)
{
	ast_cli_unregister_multiple(cli, ARRAY_LEN(cli));
}

int load_pbx_hangup_handler(void)
{
	ast_cli_register_multiple(cli, ARRAY_LEN(cli));
	ast_register_cleanup(unload_pbx_hangup_handler);

	return 0;
}