diff --git a/src/mod/applications/mod_commands/mod_commands.c b/src/mod/applications/mod_commands/mod_commands.c index 87443f5fd4..057c85bdd2 100644 --- a/src/mod/applications/mod_commands/mod_commands.c +++ b/src/mod/applications/mod_commands/mod_commands.c @@ -102,19 +102,18 @@ end: return SWITCH_STATUS_SUCCESS; } - static switch_status_t _find_user(const char *cmd, switch_core_session_t *session, switch_stream_handle_t *stream, switch_bool_t tf) { switch_xml_t x_domain = NULL, x_user = NULL, xml = NULL; int argc; - char *mydata = NULL, *argv[3]; + char *mydata = NULL, *argv[3]; char *key, *user, *domain; char *xmlstr; char *path_info = NULL; char delim = ' '; char *host = NULL; const char *err = NULL; - + if (stream->event && (host = switch_event_get_header(stream->event, "http-host"))) { stream->write_function(stream, "Content-Type: text/xml\r\n\r\n"); if ((path_info = switch_event_get_header(stream->event, "http-path-info"))) { @@ -122,30 +121,30 @@ static switch_status_t _find_user(const char *cmd, switch_core_session_t *sessio delim = '/'; } } - - if (!cmd) { - err = "bad args"; - goto end; - } - mydata = strdup(cmd); - switch_assert(mydata); - - argc = switch_separate_string(mydata, delim, argv, (sizeof(argv) / sizeof(argv[0]))); - - if (argc < 3) { + if (!cmd) { err = "bad args"; - goto end; - } + goto end; + } + + mydata = strdup(cmd); + switch_assert(mydata); + + argc = switch_separate_string(mydata, delim, argv, (sizeof(argv) / sizeof(argv[0]))); + + if (argc < 3) { + err = "bad args"; + goto end; + } key = argv[0]; user = argv[1]; domain = argv[2]; - if (!(key && user && domain)) { + if (!(key && user && domain)) { err = "bad args"; - goto end; - } + goto end; + } if (switch_xml_locate_user(key, user, domain, NULL, &xml, &x_domain, &x_user, NULL) != SWITCH_STATUS_SUCCESS) { err = "can't find user"; @@ -168,7 +167,7 @@ end: if (xml && x_user) { xmlstr = switch_xml_toxml(x_user, SWITCH_FALSE); switch_assert(xmlstr); - + stream->write_function(stream, "%s", xmlstr); free(xmlstr); switch_xml_free(xml); @@ -177,7 +176,6 @@ end: free(mydata); return SWITCH_STATUS_SUCCESS; - } SWITCH_STANDARD_API(user_exists_function) @@ -194,7 +192,7 @@ SWITCH_STANDARD_API(xml_locate_function) { switch_xml_t xml = NULL, obj = NULL; int argc; - char *mydata = NULL, *argv[4]; + char *mydata = NULL, *argv[4]; char *section, *tag, *tag_attr_name, *tag_attr_val; switch_event_t *params = NULL; char *xmlstr; @@ -210,33 +208,33 @@ SWITCH_STANDARD_API(xml_locate_function) } } - if (!cmd) { + if (!cmd) { err = "bad args"; - goto end; - } + goto end; + } mydata = strdup(cmd); - switch_assert(mydata); - - argc = switch_separate_string(mydata, delim, argv, (sizeof(argv) / sizeof(argv[0]))); + switch_assert(mydata); - if (argc == 1 && !strcasecmp(argv[0], "root")) { + argc = switch_separate_string(mydata, delim, argv, (sizeof(argv) / sizeof(argv[0]))); + + if (argc == 1 && !strcasecmp(argv[0], "root")) { const char *error; xml = switch_xml_open_root(0, &error); obj = xml; - goto end; - } + goto end; + } - if (argc < 4) { + if (argc < 4) { err = "bad args"; - goto end; - } + goto end; + } section = argv[0]; tag = argv[1]; tag_attr_name = argv[2]; tag_attr_val = argv[3]; - + switch_event_create(¶ms, SWITCH_EVENT_MESSAGE); switch_assert(params); switch_event_add_header_string(params, SWITCH_STACK_BOTTOM, "section", section); @@ -250,8 +248,6 @@ SWITCH_STANDARD_API(xml_locate_function) } end: - switch_event_destroy(¶ms); - if (err) { if (host) { stream->write_function(stream, "%s\n", err); @@ -260,8 +256,6 @@ end: } } - switch_safe_free(params); - if (xml && obj) { xmlstr = switch_xml_toxml(obj, SWITCH_FALSE); switch_assert(xmlstr); @@ -269,12 +263,12 @@ end: stream->write_function(stream, "%s", xmlstr); free(xmlstr); switch_xml_free(xml); - + } + switch_event_destroy(¶ms); free(mydata); return SWITCH_STATUS_SUCCESS; - } SWITCH_STANDARD_API(regex_function) @@ -282,23 +276,23 @@ SWITCH_STANDARD_API(regex_function) switch_regex_t *re = NULL; int ovector[30]; int argc; - char *mydata = NULL, *argv[3]; + char *mydata = NULL, *argv[3]; size_t len = 0; char *substituted = NULL; int proceed = 0; - if (!cmd) { - goto error; - } + if (!cmd) { + goto error; + } - mydata = strdup(cmd); - switch_assert(mydata); - - argc = switch_separate_string(mydata, '|', argv, (sizeof(argv) / sizeof(argv[0]))); + mydata = strdup(cmd); + switch_assert(mydata); - if (argc < 2) { - goto error; - } + argc = switch_separate_string(mydata, '|', argv, (sizeof(argv) / sizeof(argv[0]))); + + if (argc < 2) { + goto error; + } if ((proceed = switch_regex_perform(argv[0], argv[1], &re, ovector, sizeof(ovector) / sizeof(ovector[0])))) { if (argc > 2) { @@ -317,146 +311,141 @@ SWITCH_STANDARD_API(regex_function) } else { stream->write_function(stream, "false"); } - goto ok; - error: - stream->write_function(stream, "-ERR"); - ok: - +error: + stream->write_function(stream, "-ERR"); +ok: switch_regex_safe_free(re); - switch_safe_free(mydata); - - return SWITCH_STATUS_SUCCESS; + switch_safe_free(mydata); + return SWITCH_STATUS_SUCCESS; } typedef enum { - O_NONE, - O_EQ, - O_NE, - O_GT, - O_GE, - O_LT, - O_LE + O_NONE, + O_EQ, + O_NE, + O_GT, + O_GE, + O_LT, + O_LE } o_t; SWITCH_STANDARD_API(cond_function) { - int argc; + int argc; char *mydata = NULL, *argv[3]; - char *expr; - char *a, *b; - double a_f = 0.0, b_f = 0.0; - o_t o = O_NONE; - int is_true = 0; - char *p; + char *expr; + char *a, *b; + double a_f = 0.0, b_f = 0.0; + o_t o = O_NONE; + int is_true = 0; + char *p; - if (!cmd) { - goto error; - } + if (!cmd) { + goto error; + } - mydata = strdup(cmd); - switch_assert(mydata); + mydata = strdup(cmd); + switch_assert(mydata); - if ((p = strchr(mydata, '?'))) { - *p = ':'; - } else { - goto error; - } + if ((p = strchr(mydata, '?'))) { + *p = ':'; + } else { + goto error; + } - argc = switch_separate_string(mydata, ':', argv, (sizeof(argv) / sizeof(argv[0]))); + argc = switch_separate_string(mydata, ':', argv, (sizeof(argv) / sizeof(argv[0]))); - if (argc != 3) { - goto error; - } + if (argc != 3) { + goto error; + } - a = argv[0]; - - if ((expr = strchr(a, '!'))) { - *expr++ = '\0'; - if (*expr == '=') { - o = O_NE; - } - } else if ((expr = strchr(a, '>'))) { - if (*(expr+1) == '=') { - *expr++ = '\0'; - o = O_GE; - } else { - o = O_GT; - } - } else if ((expr = strchr(a, '<'))) { - if (*(expr+1) == '=') { - *expr++ = '\0'; - o = O_LE; - } else { - o = O_LT; - } - } else if ((expr = strchr(a, '='))) { - *expr++ = '\0'; - if (*expr == '=') { - o = O_EQ; - } - } + a = argv[0]; + if ((expr = strchr(a, '!'))) { + *expr++ = '\0'; + if (*expr == '=') { + o = O_NE; + } + } else if ((expr = strchr(a, '>'))) { + if (*(expr+1) == '=') { + *expr++ = '\0'; + o = O_GE; + } else { + o = O_GT; + } + } else if ((expr = strchr(a, '<'))) { + if (*(expr+1) == '=') { + *expr++ = '\0'; + o = O_LE; + } else { + o = O_LT; + } + } else if ((expr = strchr(a, '='))) { + *expr++ = '\0'; + if (*expr == '=') { + o = O_EQ; + } + } - if (o) { - char *s_a = NULL, *s_b = NULL; - int a_is_num, b_is_num; - *expr++ = '\0'; - b = expr; - s_a = switch_strip_spaces(a); - s_b = switch_strip_spaces(b); - a_is_num = switch_is_number(s_a); - b_is_num = switch_is_number(s_b); + if (o) { + char *s_a = NULL, *s_b = NULL; + int a_is_num, b_is_num; + *expr++ = '\0'; + b = expr; + s_a = switch_strip_spaces(a); + s_b = switch_strip_spaces(b); + a_is_num = switch_is_number(s_a); + b_is_num = switch_is_number(s_b); - a_f = a_is_num ? atof(s_a) : (float) strlen(s_a); - b_f = b_is_num ? atof(s_b) : (float) strlen(s_b); - - switch (o) { - case O_EQ: - if (!a_is_num && !b_is_num) { - is_true = !strcmp(s_a, s_b); - } else { - is_true = a_f == b_f; - } - break; - case O_NE: - if (!a_is_num && !b_is_num) { - is_true = strcmp(s_a, s_b); - } else { - is_true = a_f != b_f; - } - break; - case O_GT: - is_true = a_f > b_f; - break; - case O_GE: - is_true = a_f >= b_f; - break; - case O_LT: - is_true = a_f < b_f; - break; - case O_LE: - is_true = a_f <= b_f; - break; - default: - break; - } - switch_safe_free(s_a); - switch_safe_free(s_b); - stream->write_function(stream, "%s", is_true ? argv[1] : argv[2]); - goto ok; - } + a_f = a_is_num ? atof(s_a) : (float) strlen(s_a); + b_f = b_is_num ? atof(s_b) : (float) strlen(s_b); - error: - stream->write_function(stream, "-ERR"); - ok: + switch (o) { + case O_EQ: + if (!a_is_num && !b_is_num) { + is_true = !strcmp(s_a, s_b); + } else { + is_true = a_f == b_f; + } + break; + case O_NE: + if (!a_is_num && !b_is_num) { + is_true = strcmp(s_a, s_b); + } else { + is_true = a_f != b_f; + } + break; + case O_GT: + is_true = a_f > b_f; + break; + case O_GE: + is_true = a_f >= b_f; + break; + case O_LT: + is_true = a_f < b_f; + break; + case O_LE: + is_true = a_f <= b_f; + break; + default: + break; + } + switch_safe_free(s_a); + switch_safe_free(s_b); + stream->write_function(stream, "%s", is_true ? argv[1] : argv[2]); + goto ok; + } - switch_safe_free(mydata); - return SWITCH_STATUS_SUCCESS; +error: + stream->write_function(stream, "-ERR"); +ok: + + switch_safe_free(mydata); + return SWITCH_STATUS_SUCCESS; } - SWITCH_STANDARD_API(lan_addr_function) { stream->write_function(stream, "%s", switch_is_lan_addr(cmd) ? "yes" : "no"); @@ -469,7 +458,7 @@ SWITCH_STANDARD_API(status_function) switch_core_time_duration_t duration = {0}; char *http = NULL; int sps = 0, last_sps = 0; - + if (session) { return SWITCH_STATUS_FALSE; } @@ -486,11 +475,11 @@ SWITCH_STANDARD_API(status_function) } 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"); + "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" session(s) since startup\n", switch_core_session_id() - 1 ); switch_core_session_ctl(SCSC_LAST_SPS, &last_sps); @@ -589,14 +578,13 @@ SWITCH_STANDARD_API(ctl_function) } stream->write_function(stream, "+OK\n"); - end: +end: free(mydata); } else { stream->write_function(stream, "-ERR Memory error\n"); } return SWITCH_STATUS_SUCCESS; - } #define LOAD_SYNTAX "" @@ -685,7 +673,6 @@ SWITCH_STANDARD_API(kill_function) return SWITCH_STATUS_SUCCESS; } - #define PARK_SYNTAX "" SWITCH_STANDARD_API(park_function) { @@ -788,7 +775,6 @@ SWITCH_STANDARD_API(tone_detect_session_function) time_t to = 0; switch_core_session_t *rsession; - if (!cmd) { stream->write_function(stream, "-USAGE: %s\n", TONE_DETECT_SYNTAX); return SWITCH_STATUS_SUCCESS; @@ -802,13 +788,11 @@ SWITCH_STANDARD_API(tone_detect_session_function) return SWITCH_STATUS_SUCCESS; } - if (!(rsession = switch_core_session_locate(argv[0]))) { stream->write_function(stream, "-ERR Error Cannot locate session!\n"); return SWITCH_STATUS_SUCCESS; } - if (argv[4]) { uint32_t mto; if (*argv[4] == '+') { @@ -830,7 +814,7 @@ SWITCH_STANDARD_API(tone_detect_session_function) switch_ivr_tone_detect_session(rsession, argv[1], argv[2], argv[3], to, argv[5], argv[6]); stream->write_function(stream, "+OK Enabling tone detection '%s' '%s' '%s'\n", argv[1], argv[2], argv[3]); - done: +done: free(mydata); switch_core_session_rwunlock(rsession); @@ -838,14 +822,13 @@ SWITCH_STANDARD_API(tone_detect_session_function) 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); + switch_uuid_format(uuid_str, &uuid); stream->write_function(stream, "%s", uuid_str); return SWITCH_STATUS_SUCCESS; } @@ -886,8 +869,6 @@ SWITCH_STANDARD_API(uuid_chat) return SWITCH_STATUS_SUCCESS; } - - #define SCHED_TRANSFER_SYNTAX "[+]