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 * Anthony Minessale II <anthm@freeswitch.org>
27 *
28 * mod_snom.c -- SNOM Specific Features
29 *
30 */
31 #include <switch.h>
32 #include <switch_curl.h>
33
34 SWITCH_MODULE_LOAD_FUNCTION(mod_snom_load);
35 SWITCH_MODULE_DEFINITION(mod_snom, mod_snom_load, NULL, NULL);
36
snom_bind_key(const char * key,const char * light,const char * label,const char * user,const char * host,const char * profile,const char * action_name,const char * action)37 static switch_bool_t snom_bind_key(const char *key,
38 const char *light,
39 const char *label, const char *user, const char *host, const char *profile, const char *action_name, const char *action)
40 {
41 switch_event_t *event;
42
43
44 if (user && host && profile) {
45 if (switch_event_create(&event, SWITCH_EVENT_SEND_MESSAGE) == SWITCH_STATUS_SUCCESS) {
46 switch_event_add_header_string(event, SWITCH_STACK_BOTTOM, "user", user);
47 switch_event_add_header_string(event, SWITCH_STACK_BOTTOM, "host", host);
48 switch_event_add_header_string(event, SWITCH_STACK_BOTTOM, "profile", profile);
49 switch_event_add_header_string(event, SWITCH_STACK_BOTTOM, "content-type", "application/x-buttons");
50 if (action && action_name) {
51 switch_event_add_body(event, "k=%s\nc=%s\nl=%s\nn=%s\na=%s\n", key, light, label, action, action_name);
52 } else {
53 switch_event_add_body(event, "k=%s\nc=%s\nl=%s\n\n", key, light, label);
54 }
55
56 switch_event_fire(&event);
57 }
58 return SWITCH_TRUE;
59 }
60
61 return SWITCH_FALSE;
62 }
63
64
65 #define URL_SYNTAX ""
SWITCH_STANDARD_API(snom_url_api_function)66 SWITCH_STANDARD_API(snom_url_api_function)
67 {
68 #if 0
69 char *tmp;
70 switch_event_serialize(stream->param_event, &tmp, SWITCH_TRUE);
71 printf("W00t\n%s\n", tmp);
72 free(tmp);
73 #endif
74
75 return SWITCH_STATUS_SUCCESS;
76
77 }
78
79 #define KEY_BIND_SYNTAX "<key> <light> <label> <user> <host> <profile> <action_name> <action>"
SWITCH_STANDARD_API(snom_bind_key_api_function)80 SWITCH_STANDARD_API(snom_bind_key_api_function)
81 {
82 int argc;
83 char *mydata = NULL, *argv[8];
84
85 mydata = strdup(cmd);
86 argc = switch_separate_string(mydata, ' ', argv, (sizeof(argv) / sizeof(argv[0])));
87
88 if (argc < 6) {
89 goto err;
90 }
91
92 if (snom_bind_key(argv[0], argv[1], argv[2], argv[3], argv[4], argv[5], argv[6], argv[7])) {
93 stream->write_function(stream, "+OK %s\n", cmd);
94 goto end;
95 }
96
97 err:
98
99 stream->write_function(stream, "-Error %s\n", KEY_BIND_SYNTAX);
100
101 end:
102
103 free(mydata);
104
105 return SWITCH_STATUS_SUCCESS;
106 }
107
curl_callback(void * ptr,size_t size,size_t nmemb,void * data)108 static size_t curl_callback(void *ptr, size_t size, size_t nmemb, void *data)
109 {
110 register unsigned int realsize = (unsigned int) (size * nmemb);
111 return realsize;
112 }
113
114 #define COMMAND_SYNTAX "<ip|user> <command> <type> <username> <password>"
SWITCH_STANDARD_API(snom_command_api_function)115 SWITCH_STANDARD_API(snom_command_api_function)
116 {
117 int argc;
118 long httpRes = 0;
119 char *key = NULL;
120 char *url = NULL;
121 char *argv[5] = { 0 };
122 char host[32];
123 char *argdata = NULL;
124 char *userpwd = NULL;
125 char *apiresp = NULL;
126 ip_t ip;
127 switch_CURL *curl_handle = NULL;
128
129 if (zstr(cmd) || !(argdata = strdup(cmd))) {
130 goto end;
131 }
132
133 argc = switch_separate_string(argdata, ' ', argv, (sizeof(argv) / sizeof(argv[0])));
134
135 if (argc < 3 || (argc > 3 && argc < 5)) {
136 stream->write_function(stream, "-ERR %s\n", COMMAND_SYNTAX);
137 goto end;
138 }
139
140 if (strcasecmp(argv[1],"key") && strcasecmp(argv[1],"action")) {
141 stream->write_function(stream, "-ERR only key or action commands allowed at the moment\n");
142 goto end;
143 }
144
145 if (switch_inet_pton(AF_INET, argv[0], &ip)) {
146 strncpy(host, argv[0], sizeof(host));
147 } else {
148 char *sql = NULL;
149 char *ret = NULL;
150 switch_cache_db_handle_t *db = NULL;
151 switch_stream_handle_t apistream = { 0 };
152 switch_status_t status;
153
154 SWITCH_STANDARD_STREAM(apistream);
155 if ((status = switch_api_execute("sofia_contact", argv[0], NULL, &apistream)) != SWITCH_STATUS_SUCCESS) {
156 stream->write_function(stream, "-ERR error executing sofia_contact\n");
157 goto end;
158 }
159 apiresp = (char*) apistream.data;
160
161 if (!zstr(apiresp)) {
162 if (!strcasecmp(apistream.data,"error/user_not_registered")) {
163 stream->write_function(stream, "-ERR user '%s' not registered\n",argv[0]);
164 goto end;
165 }
166 } else {
167 goto end;
168 }
169
170 if (switch_core_db_handle(&db) != SWITCH_STATUS_SUCCESS) {
171 stream->write_function(stream, "%s", "-ERR Database Error!\n");
172 goto end;
173 }
174
175 sql = switch_mprintf("select network_ip from registrations where url = '%q'", apiresp);
176
177 ret = switch_cache_db_execute_sql2str(db, sql, host, sizeof(host), NULL);
178 switch_safe_free(sql);
179 switch_cache_db_release_db_handle(&db);
180
181 if (!ret) {
182 stream->write_function(stream, "%s", "-ERR Query '%s' failed!\n", sql);
183 goto end;
184 }
185 }
186
187 curl_handle = curl_easy_init();
188
189 if (0 == strcasecmp(argv[1],"key")) {
190 key = curl_easy_escape(curl_handle, argv[2], 0);
191 url = switch_mprintf("http://%s/command.htm?key=%s", host, key);
192 curl_free(key);
193 }
194
195 if (0 == strcasecmp(argv[1],"action")) {
196 if (0 == strcasecmp(argv[2],"reboot")) {
197 url = switch_mprintf("http://%s/advanced_update.htm?reboot=Reboot", host);
198 } else if (0 == strcasecmp(argv[2],"reset")) {
199 url = switch_mprintf("http://%s/advanced_update.htm?reset=Reset", host);
200 } else if (0 == strcasecmp(argv[2],"dialeddel")) {
201 url = switch_mprintf("http://%s/index.htm?dialeddel=0", host);
202 } else if (0 == strcasecmp(argv[2],"misseddel")) {
203 url = switch_mprintf("http://%s/index.htm?misseddel=0", host);
204 } else if (0 == strcasecmp(argv[2],"receiveddel")) {
205 url = switch_mprintf("http://%s/index.htm?receiveddel=0", host);
206 } else {
207 stream->write_function(stream, "-ERR action '%s' not supported (supported actions are reboot, reset, dialeddel, misseddel, receiveddel)\n", argv[2]);
208 curl_easy_cleanup(curl_handle);
209 goto end;
210 }
211 }
212
213 curl_easy_setopt(curl_handle, CURLOPT_HTTPGET, 1);
214 curl_easy_setopt(curl_handle, CURLOPT_WRITEFUNCTION, curl_callback);
215 curl_easy_setopt(curl_handle, CURLOPT_URL, url);
216 curl_easy_setopt(curl_handle, CURLOPT_USERAGENT, "freeswitch-curl/1.0");
217 curl_easy_setopt(curl_handle, CURLOPT_FOLLOWLOCATION, 1);
218 curl_easy_setopt(curl_handle, CURLOPT_MAXREDIRS, 15);
219 curl_easy_setopt(curl_handle, CURLOPT_NOSIGNAL, 1);
220
221 if (argc == 5) {
222 userpwd = switch_mprintf("%s:%s",argv[3],argv[4]);
223 curl_easy_setopt(curl_handle, CURLOPT_HTTPAUTH, (long)CURLAUTH_ANY);
224 curl_easy_setopt(curl_handle, CURLOPT_USERPWD, userpwd);
225 }
226 curl_easy_perform(curl_handle);
227 curl_easy_getinfo(curl_handle, CURLINFO_RESPONSE_CODE, &httpRes);
228 curl_easy_cleanup(curl_handle);
229
230 switch_log_printf(SWITCH_CHANNEL_LOG, SWITCH_LOG_DEBUG, "curl url %s , result %ld\n", url,httpRes);
231
232 if (httpRes != 200)
233 stream->write_function(stream, "-ERR %s [HTTP:%ld]\n", cmd, httpRes);
234 else
235 stream->write_function(stream, "+OK %s\n", cmd);
236
237 end:
238 switch_safe_free(apiresp);
239 switch_safe_free(userpwd);
240 switch_safe_free(argdata);
241 switch_safe_free(url);
242
243 return SWITCH_STATUS_SUCCESS;
244 }
245
SWITCH_MODULE_LOAD_FUNCTION(mod_snom_load)246 SWITCH_MODULE_LOAD_FUNCTION(mod_snom_load)
247 {
248
249 switch_api_interface_t *commands_api_interface;
250
251 /* connect my internal structure to the blank pointer passed to me */
252 *module_interface = switch_loadable_module_create_module_interface(pool, modname);
253
254
255 SWITCH_ADD_API(commands_api_interface, "snom_bind_key", "Bind a key", snom_bind_key_api_function, KEY_BIND_SYNTAX);
256 SWITCH_ADD_API(commands_api_interface, "snom_url", "url", snom_url_api_function, URL_SYNTAX);
257 SWITCH_ADD_API(commands_api_interface, "snom_command", "Sends Command over HTTP Request", snom_command_api_function, COMMAND_SYNTAX);
258
259
260 /* indicate that the module should continue to be loaded */
261 return SWITCH_STATUS_SUCCESS;
262 }
263
264
265
266
267
268 /* For Emacs:
269 * Local Variables:
270 * mode:c
271 * indent-tabs-mode:t
272 * tab-width:4
273 * c-basic-offset:4
274 * End:
275 * For VIM:
276 * vim:set softtabstop=4 shiftwidth=4 tabstop=4 noet:
277 */
278