refactor sofia_contact to try the profile_name first then the domain to resolve the profile then fall back to querying every profile to reduce confusion with multi-homers (d'oh) also special profile name * will force a search-all situation

This commit is contained in:
Anthony Minessale 2010-12-29 12:28:12 -06:00
parent 1d8a929711
commit 81608da006

View File

@ -3395,6 +3395,36 @@ SWITCH_STANDARD_API(sofia_count_reg_function)
return SWITCH_STATUS_SUCCESS; return SWITCH_STATUS_SUCCESS;
} }
static void select_from_profile(sofia_profile_t *profile,
const char *user,
const char *domain,
const char *concat,
const char *exclude_contact,
switch_stream_handle_t *stream)
{
struct cb_helper cb;
char *sql;
cb.row_process = 0;
cb.profile = profile;
cb.stream = stream;
if (exclude_contact) {
sql = switch_mprintf("select contact, profile_name, '%q' "
"from sip_registrations where sip_user='%q' and (sip_host='%q' or presence_hosts like '%%%q%%') "
"and contact not like '%%%s%%'", (concat != NULL) ? concat : "", user, domain, domain, exclude_contact);
} else {
sql = switch_mprintf("select contact, profile_name, '%q' "
"from sip_registrations where sip_user='%q' and (sip_host='%q' or presence_hosts like '%%%q%%')",
(concat != NULL) ? concat : "", user, domain, domain);
}
switch_assert(sql);
sofia_glue_execute_sql_callback(profile, profile->ireg_mutex, sql, contact_callback, &cb);
switch_safe_free(sql);
}
SWITCH_STANDARD_API(sofia_contact_function) SWITCH_STANDARD_API(sofia_contact_function)
{ {
char *data; char *data;
@ -3406,6 +3436,7 @@ SWITCH_STANDARD_API(sofia_contact_function)
sofia_profile_t *profile = NULL; sofia_profile_t *profile = NULL;
const char *exclude_contact = NULL; const char *exclude_contact = NULL;
char *reply = "error/facility_not_subscribed"; char *reply = "error/facility_not_subscribed";
switch_stream_handle_t mystream = { 0 };
if (!cmd) { if (!cmd) {
stream->write_function(stream, "%s", ""); stream->write_function(stream, "%s", "");
@ -3440,76 +3471,70 @@ SWITCH_STANDARD_API(sofia_contact_function)
} }
} }
if (!profile_name && domain) { if (zstr(domain)) {
profile_name = domain; domain = switch_core_get_variable("domain");
} }
if (user && profile_name) { if (!user) goto end;
char *sql;
if (!(profile = sofia_glue_find_profile(profile_name))) { if (zstr(profile_name) || strcmp(profile_name, "*") || zstr(domain)) {
profile_name = domain; if (!zstr(profile_name)) {
domain = NULL;
}
if (!profile && profile_name) {
profile = sofia_glue_find_profile(profile_name); profile = sofia_glue_find_profile(profile_name);
} }
if (!profile) {
profile = sofia_glue_find_profile(domain);
}
}
if (profile || !zstr(domain)) {
SWITCH_STANDARD_STREAM(mystream);
switch_assert(mystream.data);
}
if (profile) { if (profile) {
struct cb_helper cb; if (zstr(domain)) {
switch_stream_handle_t mystream = { 0 };
cb.row_process = 0;
if (!domain || (!strchr(domain, '.') && strcmp(profile_name, domain))) {
domain = profile->name; domain = profile->name;
} }
SWITCH_STANDARD_STREAM(mystream); select_from_profile(profile, user, domain, concat, exclude_contact, &mystream);
switch_assert(mystream.data); sofia_glue_release_profile(profile);
cb.profile = profile;
cb.stream = &mystream;
if (exclude_contact) { } else if (!zstr(domain)) {
sql = switch_mprintf("select contact, profile_name, '%q' " switch_mutex_lock(mod_sofia_globals.hash_mutex);
"from sip_registrations where sip_user='%q' and (sip_host='%q' or presence_hosts like '%%%q%%') " if (mod_sofia_globals.profile_hash) {
"and contact not like '%%%s%%'", (concat != NULL) ? concat : "", user, domain, domain, exclude_contact); switch_hash_index_t *hi;
} else { const void *var;
sql = switch_mprintf("select contact, profile_name, '%q' " void *val;
"from sip_registrations where sip_user='%q' and (sip_host='%q' or presence_hosts like '%%%q%%')",
(concat != NULL) ? concat : "", user, domain, domain); for (hi = switch_hash_first(NULL, mod_sofia_globals.profile_hash); hi; hi = switch_hash_next(hi)) {
switch_hash_this(hi, &var, NULL, &val);
if ((profile = (sofia_profile_t *) val) && !strcmp((char *)var, profile->name)) {
select_from_profile(profile, user, domain, concat, exclude_contact, &mystream);
profile = NULL;
}
}
}
switch_mutex_unlock(mod_sofia_globals.hash_mutex);
} }
switch_assert(sql);
sofia_glue_execute_sql_callback(profile, profile->ireg_mutex, sql, contact_callback, &cb);
switch_safe_free(sql);
reply = (char *) mystream.data; reply = (char *) mystream.data;
if (!zstr(reply) && end_of(reply) == ',') {
end_of(reply) = '\0'; end:
}
if (zstr(reply)) { if (zstr(reply)) {
reply = "error/user_not_registered"; reply = "error/user_not_registered";
} else if (end_of(reply) == ',') {
end_of(reply) = '\0';
} }
stream->write_function(stream, "%s", reply); stream->write_function(stream, "%s", reply);
reply = NULL; reply = NULL;
switch_safe_free(mystream.data); switch_safe_free(mystream.data);
}
}
if (reply) {
stream->write_function(stream, "%s", reply);
}
switch_safe_free(data); switch_safe_free(data);
if (profile) {
sofia_glue_release_profile(profile);
}
return SWITCH_STATUS_SUCCESS; return SWITCH_STATUS_SUCCESS;
} }