/* * FreeSWITCH Modular Media Switching Software Library / Soft-Switch Application * Copyright (C) 2005/2006, Anthony Minessale II * * Version: MPL 1.1 * * The contents of this file are subject to the Mozilla Public License Version * 1.1 (the "License"); you may not use this file except in compliance with * the License. You may obtain a copy of the License at * http://www.mozilla.org/MPL/ * * Software distributed under the License is distributed on an "AS IS" basis, * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License * for the specific language governing rights and limitations under the * License. * * The Original Code is FreeSWITCH Modular Media Switching Software Library / Soft-Switch Application * * The Initial Developer of the Original Code is * Anthony Minessale II * Portions created by the Initial Developer are Copyright (C) * the Initial Developer. All Rights Reserved. * * Contributor(s): * * Anthony Minessale II * Michael Jerris * Johny Kadarisman * Paul Tinsley * Marcel Barbulescu * * * mod_commands.c -- Misc. Command Module * */ #include #include SWITCH_MODULE_LOAD_FUNCTION(mod_commands_load); SWITCH_MODULE_DEFINITION(mod_commands, mod_commands_load, NULL, NULL); SWITCH_STANDARD_API(status_function) { uint8_t html = 0; switch_core_time_duration_t duration; char *http = NULL; if (session) { return SWITCH_STATUS_FALSE; } switch_core_measure_time(switch_core_uptime(), &duration); if (stream->event) { http = switch_event_get_header(stream->event, "http-host"); } if (http || (cmd && strstr(cmd, "html"))) { html = 1; stream->write_function(stream, "

FreeSWITCH Status

\n"); } stream->write_function(stream, "UP %u year%s, %u day%s, %u hour%s, %u minute%s, %u second%s, %u millisecond%s, %u microsecond%s\n", duration.yr, duration.yr == 1 ? "" : "s", duration.day, duration.day == 1 ? "" : "s", duration.hr, duration.hr == 1 ? "" : "s", duration.min, duration.min == 1 ? "" : "s", duration.sec, duration.sec == 1 ? "" : "s", duration.ms, duration.ms == 1 ? "" : "s", duration.mms, duration.mms == 1 ? "" : "s"); stream->write_function(stream, "%"SWITCH_SIZE_T_FMT" sessions since startup\n", switch_core_session_id() - 1 ); stream->write_function(stream, "%d sessions\n", switch_core_session_count()); if (html) { stream->write_function(stream, "\n"); } if (cmd && strstr(cmd, "refresh=")) { char *refresh = strchr(cmd, '='); if (refresh) { int r; refresh++; r = atoi(refresh); if (r > 0) { stream->write_function(stream, "\n", r, r, html ? "html=1" : ""); } } } return SWITCH_STATUS_SUCCESS; } #define CTL_SYNTAX "[hupall|pause|resume|shutdown]" SWITCH_STANDARD_API(ctl_function) { int argc; char *mydata, *argv[5]; uint32_t arg = 0; if (switch_strlen_zero(cmd)) { stream->write_function(stream, "USAGE: %s\n", CTL_SYNTAX); return SWITCH_STATUS_SUCCESS; } if ((mydata = strdup(cmd))) { argc = switch_separate_string(mydata, ' ', argv, (sizeof(argv) / sizeof(argv[0]))); if (!strcasecmp(argv[0], "hupall")) { arg = 1; switch_core_session_ctl(SCSC_HUPALL, &arg); } else if (!strcasecmp(argv[0], "pause")) { arg = 1; switch_core_session_ctl(SCSC_PAUSE_INBOUND, &arg); } else if (!strcasecmp(argv[0], "resume")) { arg = 0; switch_core_session_ctl(SCSC_PAUSE_INBOUND, &arg); } else if (!strcasecmp(argv[0], "shutdown")) { arg = 0; switch_core_session_ctl(SCSC_SHUTDOWN, &arg); } else { stream->write_function(stream, "INVALID COMMAND\nUSAGE: fsctl [hupall|pause|resume|shutdown]\n"); goto end; } stream->write_function(stream, "OK\n"); end: free(mydata); } else { stream->write_function(stream, "MEM ERR\n"); } return SWITCH_STATUS_SUCCESS; } #define LOAD_SYNTAX "" SWITCH_STANDARD_API(load_function) { const char *err; if (session) { return SWITCH_STATUS_FALSE; } if (switch_strlen_zero(cmd)) { stream->write_function(stream, "USAGE: %s\n", LOAD_SYNTAX); return SWITCH_STATUS_SUCCESS; } if (switch_loadable_module_load_module((char *) SWITCH_GLOBAL_dirs.mod_dir, (char *) cmd, SWITCH_TRUE, &err) == SWITCH_STATUS_SUCCESS) { stream->write_function(stream, "OK\n"); } else { stream->write_function(stream, "ERROR [%s]\n", err); } return SWITCH_STATUS_SUCCESS; } SWITCH_STANDARD_API(unload_function) { const char *err; if (session) { return SWITCH_STATUS_FALSE; } if (switch_strlen_zero(cmd)) { stream->write_function(stream, "USAGE: %s\n", LOAD_SYNTAX); return SWITCH_STATUS_SUCCESS; } if (switch_loadable_module_unload_module((char *) SWITCH_GLOBAL_dirs.mod_dir, (char *) cmd, &err) == SWITCH_STATUS_SUCCESS) { stream->write_function(stream, "OK\n"); } else { stream->write_function(stream, "ERROR [%s]\n", err); } return SWITCH_STATUS_SUCCESS; } SWITCH_STANDARD_API(reload_function) { const char *err; switch_xml_t xml_root; if (session) { return SWITCH_STATUS_FALSE; } if ((xml_root = switch_xml_open_root(1, &err))) { switch_xml_free(xml_root); } stream->write_function(stream, "OK [%s]\n", err); return SWITCH_STATUS_SUCCESS; } #define KILL_SYNTAX "" SWITCH_STANDARD_API(kill_function) { switch_core_session_t *ksession = NULL; if (session) { return SWITCH_STATUS_FALSE; } if (!cmd) { stream->write_function(stream, "USAGE: %s\n", KILL_SYNTAX); } else if ((ksession = switch_core_session_locate(cmd))) { switch_channel_t *channel = switch_core_session_get_channel(ksession); switch_channel_hangup(channel, SWITCH_CAUSE_NORMAL_CLEARING); switch_core_session_rwunlock(ksession); stream->write_function(stream, "OK\n"); } else { stream->write_function(stream, "No Such Channel!\n"); } return SWITCH_STATUS_SUCCESS; } #define TRANSFER_SYNTAX " [-bleg|-both] [] []" SWITCH_STANDARD_API(transfer_function) { switch_core_session_t *tsession = NULL, *other_session = NULL; char *mycmd = NULL, *argv[5] = { 0 }; int argc = 0; if (session) { return SWITCH_STATUS_FALSE; } if (!switch_strlen_zero(cmd) && (mycmd = strdup(cmd))) { argc = switch_separate_string(mycmd, ' ', argv, (sizeof(argv) / sizeof(argv[0]))); if (argc >= 2 && argc <= 4) { char *uuid = argv[0]; char *dest = argv[1]; char *dp = argv[2]; char *context = argv[3]; char *arg = NULL; if ((tsession = switch_core_session_locate(uuid))) { if (*dest == '-') { arg = dest; dest = argv[2]; dp = argv[3]; context = argv[4]; } if (arg) { switch_channel_t *channel = switch_core_session_get_channel(tsession); arg++; if (!strcasecmp(arg, "bleg")) { char *uuid = switch_channel_get_variable(channel, SWITCH_BRIDGE_VARIABLE); if (uuid && (other_session = switch_core_session_locate(uuid))) { switch_core_session_t *tmp = tsession; tsession = other_session; other_session = NULL; switch_core_session_rwunlock(tmp); } } else if (!strcasecmp(arg, "both")) { char *uuid = switch_channel_get_variable(channel, SWITCH_BRIDGE_VARIABLE); switch_core_session_t *other_session; if (uuid && (other_session = switch_core_session_locate(uuid))) { switch_ivr_session_transfer(other_session, dest, dp, context); switch_core_session_rwunlock(other_session); } } } if (switch_ivr_session_transfer(tsession, dest, dp, context) == SWITCH_STATUS_SUCCESS) { stream->write_function(stream, "OK\n"); } else { stream->write_function(stream, "ERROR\n"); } switch_core_session_rwunlock(tsession); } else { stream->write_function(stream, "No Such Channel!\n"); } goto done; } } stream->write_function(stream, "USAGE: %s\n", TRANSFER_SYNTAX); done: switch_safe_free(mycmd); return SWITCH_STATUS_SUCCESS; } #define TONE_DETECT_SYNTAX " [ ]" SWITCH_STANDARD_API(tone_detect_session_function) { char *argv[7] = { 0 }; int argc; char *mydata = NULL; time_t to = 0; switch_core_session_t *rsession; mydata = strdup(cmd); assert(mydata != NULL); if ((argc = switch_separate_string(mydata, ' ', argv, sizeof(argv) / sizeof(argv[0]))) < 3) { switch_log_printf(SWITCH_CHANNEL_LOG, SWITCH_LOG_ERROR, "INVALID ARGS!\n"); } if (!(rsession = switch_core_session_locate(argv[0]))) { stream->write_function(stream, "-Error Cannot locate session!\n"); return SWITCH_STATUS_SUCCESS; } if (argv[4]) { uint32_t mto; if (*argv[4] == '+') { if ((mto = atoi(argv[4]+1)) > 0) { to = time(NULL) + mto; } else { switch_log_printf(SWITCH_CHANNEL_LOG, SWITCH_LOG_ERROR, "INVALID Timeout!\n"); goto done; } } else { if ((to = atoi(argv[4])) < time(NULL)) { switch_log_printf(SWITCH_CHANNEL_LOG, SWITCH_LOG_ERROR, "INVALID Timeout!\n"); to = 0; goto done; } } } switch_ivr_tone_detect_session(rsession, argv[1], argv[2], argv[3], to, argv[5], argv[6]); stream->write_function(stream, "Enabling tone detection '%s' '%s' '%s'\n", argv[1], argv[2], argv[3]); done: free(mydata); switch_core_session_rwunlock(rsession); return SWITCH_STATUS_SUCCESS; } SWITCH_STANDARD_API(uuid_function) { switch_uuid_t uuid; char uuid_str[SWITCH_UUID_FORMATTED_LENGTH + 1]; switch_uuid_get(&uuid); switch_uuid_format(uuid_str, &uuid); stream->write_function(stream, "%s", uuid_str); return SWITCH_STATUS_SUCCESS; } #define SCHED_TRANSFER_SYNTAX "[+]