1 /*
2  * FreeSWITCH Modular Media Switching Software Library / Soft-Switch Application
3  * Copyright (C) 2005-2014, Anthony Minessale II <anthm@freeswitch.org>
4  *
5  * Version: MPL 1.1
6  *
7  * The contents of this file are subject to the Mozilla Public License Version
8  * 1.1 (the "License"); you may not use this file except in compliance with
9  * the License. You may obtain a copy of the License at
10  * http://www.mozilla.org/MPL/
11  *
12  * Software distributed under the License is distributed on an "AS IS" basis,
13  * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
14  * for the specific language governing rights and limitations under the
15  * License.
16  *
17  * The Original Code is FreeSWITCH Modular Media Switching Software Library / Soft-Switch Application
18  *
19  * The Initial Developer of the Original Code is
20  * Anthony Minessale II <anthm@freeswitch.org>
21  * Portions created by the Initial Developer are Copyright (C)
22  * the Initial Developer. All Rights Reserved.
23  *
24  * Contributor(s):
25  *
26  * Marc Olivier Chouinard <mochouinard@moctel.com>
27  *
28  *
29  * mod_abstraction.c -- Abstraction
30  *
31  */
32 #include <switch.h>
33 
34 /* Prototypes */
35 SWITCH_MODULE_SHUTDOWN_FUNCTION(mod_abstraction_shutdown);
36 SWITCH_MODULE_RUNTIME_FUNCTION(mod_abstraction_runtime);
37 SWITCH_MODULE_LOAD_FUNCTION(mod_abstraction_load);
38 
39 const char *global_cf = "abstraction.conf";
40 
41 /* SWITCH_MODULE_DEFINITION(name, load, shutdown, runtime)
42  * Defines a switch_loadable_module_function_table_t and a static const char[] modname
43  */
44 SWITCH_MODULE_DEFINITION(mod_abstraction, mod_abstraction_load, mod_abstraction_shutdown, NULL);
45 
SWITCH_STANDARD_API(api_abstraction_function)46 SWITCH_STANDARD_API(api_abstraction_function)
47 {
48 	const char *api_name = switch_event_get_header(stream->param_event, "API-Command");
49 	switch_xml_t cfg, xml, x_apis, x_api;
50 
51 	if (!(xml = switch_xml_open_cfg(global_cf, &cfg, NULL))) {
52 		switch_log_printf(SWITCH_CHANNEL_LOG, SWITCH_LOG_ERROR, "Open of %s failed\n", global_cf);
53 		goto end;
54 	}
55 
56 	if (!(x_apis = switch_xml_child(cfg, "apis"))) {
57 		switch_log_printf(SWITCH_CHANNEL_LOG, SWITCH_LOG_ERROR, "No apis group\n");
58 		goto end;
59 	}
60 
61 	if ((x_api = switch_xml_find_child_multi(x_apis, "api", "name", api_name , NULL))) {
62 		const char *parse = switch_xml_attr_soft(x_api, "parse");
63 		const char *destination = switch_xml_attr_soft(x_api, "destination");
64 		const char *arguments = switch_xml_attr_soft(x_api, "argument");
65 
66 		int proceed;
67 		switch_regex_t *re = NULL;
68 		int ovector[30];
69 
70 		if ((proceed = switch_regex_perform(cmd, parse, &re, ovector, sizeof(ovector) / sizeof(ovector[0])))) {
71 			const char *api_args = NULL;
72 			char *substituted = NULL;
73 
74 			if (cmd && strchr(parse, '(')) {
75 				uint32_t len = (uint32_t) (strlen(cmd) + strlen(arguments) + 10) * proceed;
76 				if (!(substituted = malloc(len))) {
77 					switch_log_printf(SWITCH_CHANNEL_SESSION_LOG(session), SWITCH_LOG_CRIT, "Memory Error!\n");
78 					goto end;
79 				}
80 				memset(substituted, 0, len);
81 				switch_perform_substitution(re, proceed, arguments, cmd , substituted, len, ovector);
82 				api_args = substituted;
83 			} else {
84 				api_args = arguments;
85 			}
86 			switch_api_execute(destination, api_args, session, stream);
87 
88 			switch_safe_free(substituted);
89 		} else {
90 			switch_log_printf(SWITCH_CHANNEL_LOG, SWITCH_LOG_ERROR, "No match for API %s  (%s != %s)\n", api_name, parse, cmd);
91 		}
92 		switch_regex_safe_free(re);
93 
94 	} else {
95 		switch_log_printf(SWITCH_CHANNEL_LOG, SWITCH_LOG_ERROR, "API %s doesn't exist inside the xml structure.  You might have forgot to reload the module after editing it\n", api_name);
96 	}
97 
98 end:
99 	if (xml)
100 		switch_xml_free(xml);
101 
102 
103 	return SWITCH_STATUS_SUCCESS;
104 }
105 
106 /* Macro expands to: switch_status_t mod_abstraction_load(switch_loadable_module_interface_t **module_interface, switch_memory_pool_t *pool) */
SWITCH_MODULE_LOAD_FUNCTION(mod_abstraction_load)107 SWITCH_MODULE_LOAD_FUNCTION(mod_abstraction_load)
108 {
109 	switch_status_t status = SWITCH_STATUS_TERM;
110 	switch_api_interface_t *api_interface;
111 	switch_xml_t cfg, xml, x_apis, x_api;
112 	int count = 0;
113 
114 	/* connect my internal structure to the blank pointer passed to me */
115 	*module_interface = switch_loadable_module_create_module_interface(pool, modname);
116 
117 	if (!(xml = switch_xml_open_cfg(global_cf, &cfg, NULL))) {
118 		switch_log_printf(SWITCH_CHANNEL_LOG, SWITCH_LOG_ERROR, "Open of %s failed\n", global_cf);
119 		goto end;
120 	}
121 
122 	if (!(x_apis = switch_xml_child(cfg, "apis"))) {
123 		switch_log_printf(SWITCH_CHANNEL_LOG, SWITCH_LOG_ERROR, "No apis group\n");
124 		goto end;
125 	}
126 
127 	for (x_api = switch_xml_child(x_apis, "api"); x_api; x_api = x_api->next) {
128 		const char *name = switch_xml_attr_soft(x_api, "name");
129 		const char *description = switch_xml_attr_soft(x_api, "description");
130 		const char *syntax = switch_xml_attr_soft(x_api, "syntax");
131 		SWITCH_ADD_API(api_interface, name, description, api_abstraction_function, syntax);
132 		count++;
133 
134 	}
135 	if (count > 0) {
136 		status = SWITCH_STATUS_SUCCESS;
137 	} else {
138 		switch_log_printf(SWITCH_CHANNEL_LOG, SWITCH_LOG_ERROR, "No API abstraction defined\n");
139 	}
140 end:
141 	if (xml)
142 		switch_xml_free(xml);
143 
144 	return status;
145 }
146 
147 /*
148    Called when the system shuts down
149    Macro expands to: switch_status_t mod_abstraction_shutdown() */
SWITCH_MODULE_SHUTDOWN_FUNCTION(mod_abstraction_shutdown)150 SWITCH_MODULE_SHUTDOWN_FUNCTION(mod_abstraction_shutdown)
151 {
152 	/* Cleanup dynamically allocated config settings */
153 	return SWITCH_STATUS_SUCCESS;
154 }
155 
156 /* For Emacs:
157  * Local Variables:
158  * mode:c
159  * indent-tabs-mode:t
160  * tab-width:4
161  * c-basic-offset:4
162  * End:
163  * For VIM:
164  * vim:set softtabstop=4 shiftwidth=4 tabstop=4 noet
165  */
166