| 
									
										
										
										
											2005-05-05 05:39:33 +00:00
										 |  |  | /*
 | 
					
						
							| 
									
										
										
										
											2005-09-14 20:46:50 +00:00
										 |  |  |  * Asterisk -- An open source telephony toolkit. | 
					
						
							| 
									
										
										
										
											2005-05-05 05:39:33 +00:00
										 |  |  |  * | 
					
						
							| 
									
										
										
										
											2006-02-12 04:28:58 +00:00
										 |  |  |  * Copyright (C) 1999 - 2006, Digium, Inc. | 
					
						
							| 
									
										
										
										
											2005-09-14 20:46:50 +00:00
										 |  |  |  * | 
					
						
							|  |  |  |  * 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. | 
					
						
							| 
									
										
										
										
											2005-05-05 05:39:33 +00:00
										 |  |  |  * | 
					
						
							|  |  |  |  * This program is free software, distributed under the terms of | 
					
						
							| 
									
										
										
										
											2005-09-14 20:46:50 +00:00
										 |  |  |  * the GNU General Public License Version 2. See the LICENSE file | 
					
						
							|  |  |  |  * at the top of the source tree. | 
					
						
							|  |  |  |  */ | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2005-10-24 20:12:06 +00:00
										 |  |  | /*! \file
 | 
					
						
							| 
									
										
										
										
											2005-09-14 20:46:50 +00:00
										 |  |  |  * | 
					
						
							| 
									
										
										
										
											2005-10-24 20:12:06 +00:00
										 |  |  |  * \brief Channel group related dialplan functions | 
					
						
							| 
									
										
										
										
											2005-09-14 20:46:50 +00:00
										 |  |  |  *  | 
					
						
							| 
									
										
										
										
											2005-05-05 05:39:33 +00:00
										 |  |  |  */ | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2006-06-07 18:54:56 +00:00
										 |  |  | #include "asterisk.h"
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | ASTERISK_FILE_VERSION(__FILE__, "$Revision$") | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2005-05-05 05:39:33 +00:00
										 |  |  | #include <stdlib.h>
 | 
					
						
							| 
									
										
										
										
											2005-11-08 04:48:00 +00:00
										 |  |  | #include <stdio.h>
 | 
					
						
							| 
									
										
										
										
											2005-05-05 05:39:33 +00:00
										 |  |  | #include <string.h>
 | 
					
						
							|  |  |  | #include <sys/types.h>
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2006-02-11 03:14:05 +00:00
										 |  |  | #include "asterisk/module.h"
 | 
					
						
							| 
									
										
										
										
											2005-05-05 05:39:33 +00:00
										 |  |  | #include "asterisk/channel.h"
 | 
					
						
							|  |  |  | #include "asterisk/pbx.h"
 | 
					
						
							|  |  |  | #include "asterisk/logger.h"
 | 
					
						
							|  |  |  | #include "asterisk/utils.h"
 | 
					
						
							|  |  |  | #include "asterisk/app.h"
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2006-02-12 04:28:58 +00:00
										 |  |  | static int group_count_function_read(struct ast_channel *chan, char *cmd, | 
					
						
							|  |  |  | 				     char *data, char *buf, size_t len) | 
					
						
							| 
									
										
										
										
											2005-05-05 05:39:33 +00:00
										 |  |  | { | 
					
						
							|  |  |  | 	int count; | 
					
						
							|  |  |  | 	char group[80] = ""; | 
					
						
							|  |  |  | 	char category[80] = ""; | 
					
						
							| 
									
										
										
										
											2005-12-03 19:25:33 +00:00
										 |  |  | 	const char *grp; | 
					
						
							| 
									
										
										
										
											2005-05-05 05:39:33 +00:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2006-02-12 04:28:58 +00:00
										 |  |  | 	ast_app_group_split_group(data, group, sizeof(group), category, | 
					
						
							|  |  |  | 				  sizeof(category)); | 
					
						
							| 
									
										
										
										
											2005-05-05 05:39:33 +00:00
										 |  |  | 
 | 
					
						
							|  |  |  | 	if (ast_strlen_zero(group)) { | 
					
						
							| 
									
										
										
										
											2005-07-27 22:43:18 +00:00
										 |  |  | 		if ((grp = pbx_builtin_getvar_helper(chan, category))) | 
					
						
							|  |  |  | 			ast_copy_string(group, grp, sizeof(group)); | 
					
						
							|  |  |  | 		else | 
					
						
							| 
									
										
										
										
											2006-02-12 04:28:58 +00:00
										 |  |  | 			ast_log(LOG_NOTICE, "No group could be found for channel '%s'\n", | 
					
						
							|  |  |  | 				chan->name); | 
					
						
							| 
									
										
										
										
											2005-05-05 05:39:33 +00:00
										 |  |  | 	} | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	count = ast_app_group_get_count(group, category); | 
					
						
							|  |  |  | 	snprintf(buf, len, "%d", count); | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2006-02-12 04:28:58 +00:00
										 |  |  | 	return 0; | 
					
						
							| 
									
										
										
										
											2005-05-05 05:39:33 +00:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2006-02-11 03:14:05 +00:00
										 |  |  | static struct ast_custom_function group_count_function = { | 
					
						
							| 
									
										
										
										
											2005-05-05 05:39:33 +00:00
										 |  |  | 	.name = "GROUP_COUNT", | 
					
						
							|  |  |  | 	.syntax = "GROUP_COUNT([groupname][@category])", | 
					
						
							|  |  |  | 	.synopsis = "Counts the number of channels in the specified group", | 
					
						
							| 
									
										
										
										
											2006-02-12 04:28:58 +00:00
										 |  |  | 	.desc = | 
					
						
							|  |  |  | 		"Calculates the group count for the specified group, or uses the\n" | 
					
						
							|  |  |  | 		"channel's current group if not specifed (and non-empty).\n", | 
					
						
							| 
									
										
										
										
											2005-05-05 05:39:33 +00:00
										 |  |  | 	.read = group_count_function_read, | 
					
						
							|  |  |  | }; | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2006-02-12 04:28:58 +00:00
										 |  |  | static int group_match_count_function_read(struct ast_channel *chan, | 
					
						
							|  |  |  | 					   char *cmd, char *data, char *buf, | 
					
						
							|  |  |  | 					   size_t len) | 
					
						
							| 
									
										
										
										
											2005-05-05 05:39:33 +00:00
										 |  |  | { | 
					
						
							|  |  |  | 	int count; | 
					
						
							|  |  |  | 	char group[80] = ""; | 
					
						
							|  |  |  | 	char category[80] = ""; | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2006-02-12 04:28:58 +00:00
										 |  |  | 	ast_app_group_split_group(data, group, sizeof(group), category, | 
					
						
							|  |  |  | 				  sizeof(category)); | 
					
						
							| 
									
										
										
										
											2005-05-05 05:39:33 +00:00
										 |  |  | 
 | 
					
						
							|  |  |  | 	if (!ast_strlen_zero(group)) { | 
					
						
							|  |  |  | 		count = ast_app_group_match_get_count(group, category); | 
					
						
							|  |  |  | 		snprintf(buf, len, "%d", count); | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2006-02-12 04:28:58 +00:00
										 |  |  | 	return 0; | 
					
						
							| 
									
										
										
										
											2005-05-05 05:39:33 +00:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2006-02-11 03:14:05 +00:00
										 |  |  | static struct ast_custom_function group_match_count_function = { | 
					
						
							| 
									
										
										
										
											2005-05-05 05:39:33 +00:00
										 |  |  | 	.name = "GROUP_MATCH_COUNT", | 
					
						
							|  |  |  | 	.syntax = "GROUP_MATCH_COUNT(groupmatch[@category])", | 
					
						
							| 
									
										
										
										
											2006-02-12 04:28:58 +00:00
										 |  |  | 	.synopsis = | 
					
						
							|  |  |  | 		"Counts the number of channels in the groups matching the specified pattern", | 
					
						
							|  |  |  | 	.desc = | 
					
						
							|  |  |  | 		"Calculates the group count for all groups that match the specified pattern.\n" | 
					
						
							|  |  |  | 		"Uses standard regular expression matching (see regex(7)).\n", | 
					
						
							| 
									
										
										
										
											2005-05-05 05:39:33 +00:00
										 |  |  | 	.read = group_match_count_function_read, | 
					
						
							|  |  |  | 	.write = NULL, | 
					
						
							|  |  |  | }; | 
					
						
							| 
									
										
										
										
											2005-05-08 17:17:34 +00:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2006-02-12 04:28:58 +00:00
										 |  |  | static int group_function_read(struct ast_channel *chan, char *cmd, | 
					
						
							|  |  |  | 			       char *data, char *buf, size_t len) | 
					
						
							| 
									
										
										
										
											2005-05-08 17:17:34 +00:00
										 |  |  | { | 
					
						
							|  |  |  | 	char varname[256]; | 
					
						
							| 
									
										
										
										
											2005-12-03 19:25:33 +00:00
										 |  |  | 	const char *group; | 
					
						
							| 
									
										
										
										
											2005-05-08 17:17:34 +00:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2005-11-08 01:55:31 +00:00
										 |  |  | 	if (!ast_strlen_zero(data)) { | 
					
						
							| 
									
										
										
										
											2006-02-12 04:28:58 +00:00
										 |  |  | 		snprintf(varname, sizeof(varname), "%s_%s", GROUP_CATEGORY_PREFIX, | 
					
						
							|  |  |  | 			 data); | 
					
						
							| 
									
										
										
										
											2005-05-08 17:17:34 +00:00
										 |  |  | 	} else { | 
					
						
							|  |  |  | 		ast_copy_string(varname, GROUP_CATEGORY_PREFIX, sizeof(varname)); | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	group = pbx_builtin_getvar_helper(chan, varname); | 
					
						
							|  |  |  | 	if (group) | 
					
						
							|  |  |  | 		ast_copy_string(buf, group, len); | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2006-02-12 04:28:58 +00:00
										 |  |  | 	return 0; | 
					
						
							| 
									
										
										
										
											2005-05-08 17:17:34 +00:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2006-02-12 04:28:58 +00:00
										 |  |  | static int group_function_write(struct ast_channel *chan, char *cmd, | 
					
						
							|  |  |  | 				char *data, const char *value) | 
					
						
							| 
									
										
										
										
											2005-05-08 17:17:34 +00:00
										 |  |  | { | 
					
						
							|  |  |  | 	char grpcat[256]; | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2005-11-08 01:55:31 +00:00
										 |  |  | 	if (!ast_strlen_zero(data)) { | 
					
						
							| 
									
										
										
										
											2005-07-28 21:16:02 +00:00
										 |  |  | 		snprintf(grpcat, sizeof(grpcat), "%s@%s", value, data); | 
					
						
							| 
									
										
										
										
											2005-05-08 17:17:34 +00:00
										 |  |  | 	} else { | 
					
						
							|  |  |  | 		ast_copy_string(grpcat, value, sizeof(grpcat)); | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2006-02-12 04:28:58 +00:00
										 |  |  | 	if (ast_app_group_set_channel(chan, grpcat)) | 
					
						
							|  |  |  | 		ast_log(LOG_WARNING, | 
					
						
							|  |  |  | 				"Setting a group requires an argument (group name)\n"); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	return 0; | 
					
						
							| 
									
										
										
										
											2005-05-08 17:17:34 +00:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2006-02-11 03:14:05 +00:00
										 |  |  | static struct ast_custom_function group_function = { | 
					
						
							| 
									
										
										
										
											2005-05-08 17:17:34 +00:00
										 |  |  | 	.name = "GROUP", | 
					
						
							|  |  |  | 	.syntax = "GROUP([category])", | 
					
						
							|  |  |  | 	.synopsis = "Gets or sets the channel group.", | 
					
						
							|  |  |  | 	.desc = "Gets or sets the channel group.\n", | 
					
						
							|  |  |  | 	.read = group_function_read, | 
					
						
							|  |  |  | 	.write = group_function_write, | 
					
						
							|  |  |  | }; | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2006-02-12 04:28:58 +00:00
										 |  |  | static int group_list_function_read(struct ast_channel *chan, char *cmd, | 
					
						
							|  |  |  | 				    char *data, char *buf, size_t len) | 
					
						
							| 
									
										
										
										
											2005-05-08 17:17:34 +00:00
										 |  |  | { | 
					
						
							|  |  |  | 	struct ast_var_t *current; | 
					
						
							|  |  |  | 	struct varshead *headp; | 
					
						
							|  |  |  | 	char tmp1[1024] = ""; | 
					
						
							|  |  |  | 	char tmp2[1024] = ""; | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2006-02-12 04:28:58 +00:00
										 |  |  | 	headp = &chan->varshead; | 
					
						
							|  |  |  | 	AST_LIST_TRAVERSE(headp, current, entries) { | 
					
						
							| 
									
										
										
										
											2005-05-08 17:17:34 +00:00
										 |  |  | 		if (!strncmp(ast_var_name(current), GROUP_CATEGORY_PREFIX "_", strlen(GROUP_CATEGORY_PREFIX) + 1)) { | 
					
						
							|  |  |  | 			if (!ast_strlen_zero(tmp1)) { | 
					
						
							|  |  |  | 				ast_copy_string(tmp2, tmp1, sizeof(tmp2)); | 
					
						
							| 
									
										
										
										
											2006-02-12 04:28:58 +00:00
										 |  |  | 				snprintf(tmp1, sizeof(tmp1), "%s %s@%s", tmp2, | 
					
						
							|  |  |  | 					 ast_var_value(current), | 
					
						
							|  |  |  | 					 (ast_var_name(current) + | 
					
						
							|  |  |  | 					  strlen(GROUP_CATEGORY_PREFIX) + 1)); | 
					
						
							| 
									
										
										
										
											2005-05-08 17:17:34 +00:00
										 |  |  | 			} else { | 
					
						
							| 
									
										
										
										
											2006-02-12 04:28:58 +00:00
										 |  |  | 				snprintf(tmp1, sizeof(tmp1), "%s@%s", ast_var_value(current), | 
					
						
							|  |  |  | 					 (ast_var_name(current) + | 
					
						
							|  |  |  | 					  strlen(GROUP_CATEGORY_PREFIX) + 1)); | 
					
						
							| 
									
										
										
										
											2005-05-08 17:17:34 +00:00
										 |  |  | 			} | 
					
						
							|  |  |  | 		} else if (!strcmp(ast_var_name(current), GROUP_CATEGORY_PREFIX)) { | 
					
						
							|  |  |  | 			if (!ast_strlen_zero(tmp1)) { | 
					
						
							|  |  |  | 				ast_copy_string(tmp2, tmp1, sizeof(tmp2)); | 
					
						
							| 
									
										
										
										
											2006-02-12 04:28:58 +00:00
										 |  |  | 				snprintf(tmp1, sizeof(tmp1), "%s %s", tmp2, | 
					
						
							|  |  |  | 					 ast_var_value(current)); | 
					
						
							| 
									
										
										
										
											2005-05-08 17:17:34 +00:00
										 |  |  | 			} else { | 
					
						
							|  |  |  | 				snprintf(tmp1, sizeof(tmp1), "%s", ast_var_value(current)); | 
					
						
							|  |  |  | 			} | 
					
						
							|  |  |  | 		} | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | 	ast_copy_string(buf, tmp1, len); | 
					
						
							| 
									
										
										
										
											2006-02-12 04:28:58 +00:00
										 |  |  | 
 | 
					
						
							|  |  |  | 	return 0; | 
					
						
							| 
									
										
										
										
											2005-05-08 17:17:34 +00:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2006-02-11 03:14:05 +00:00
										 |  |  | static struct ast_custom_function group_list_function = { | 
					
						
							| 
									
										
										
										
											2005-05-08 17:17:34 +00:00
										 |  |  | 	.name = "GROUP_LIST", | 
					
						
							|  |  |  | 	.syntax = "GROUP_LIST()", | 
					
						
							|  |  |  | 	.synopsis = "Gets a list of the groups set on a channel.", | 
					
						
							|  |  |  | 	.desc = "Gets a list of the groups set on a channel.\n", | 
					
						
							|  |  |  | 	.read = group_list_function_read, | 
					
						
							|  |  |  | 	.write = NULL, | 
					
						
							|  |  |  | }; | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2006-08-21 02:11:39 +00:00
										 |  |  | static int unload_module(void) | 
					
						
							| 
									
										
										
										
											2006-02-11 03:14:05 +00:00
										 |  |  | { | 
					
						
							|  |  |  | 	int res = 0; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	res |= ast_custom_function_unregister(&group_count_function); | 
					
						
							|  |  |  | 	res |= ast_custom_function_unregister(&group_match_count_function); | 
					
						
							|  |  |  | 	res |= ast_custom_function_unregister(&group_list_function); | 
					
						
							|  |  |  | 	res |= ast_custom_function_unregister(&group_function); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	return res; | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2006-08-21 02:11:39 +00:00
										 |  |  | static int load_module(void) | 
					
						
							| 
									
										
										
										
											2006-02-11 03:14:05 +00:00
										 |  |  | { | 
					
						
							|  |  |  | 	int res = 0; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	res |= ast_custom_function_register(&group_count_function); | 
					
						
							|  |  |  | 	res |= ast_custom_function_register(&group_match_count_function); | 
					
						
							|  |  |  | 	res |= ast_custom_function_register(&group_list_function); | 
					
						
							|  |  |  | 	res |= ast_custom_function_register(&group_function); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	return res; | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2006-08-21 02:11:39 +00:00
										 |  |  | AST_MODULE_INFO_STANDARD(ASTERISK_GPL_KEY, "Channel group dialplan functions"); |