re-factor, reduce indentation in some function, whitespace and comment cleanup.

git-svn-id: http://svn.freeswitch.org/svn/freeswitch/trunk@6351 d0543943-73ff-0310-b7d9-9358b9ac24b2
This commit is contained in:
Michael Jerris 2007-11-20 03:26:40 +00:00
parent 1ba6b1b518
commit d9728f2aeb

View File

@ -48,7 +48,13 @@ switch_status_t sofia_presence_chat_send(char *proto, char *from, char *to, char
nua_handle_t *msg_nh; nua_handle_t *msg_nh;
char *contact; char *contact;
if (to && (user = strdup(to))) { if (!to) {
return SWITCH_STATUS_SUCCESS;
}
user = strdup(to);
assert(user);
if ((host = strchr(user, '@'))) { if ((host = strchr(user, '@'))) {
*host++ = '\0'; *host++ = '\0';
} }
@ -68,8 +74,8 @@ switch_status_t sofia_presence_chat_send(char *proto, char *from, char *to, char
from = hint; from = hint;
} else { } else {
char *fp, *p, *fu = NULL; char *fp, *p, *fu = NULL;
fp = strdup(from);
if (!(fp = strdup(from))) { if (!fp) {
switch_log_printf(SWITCH_CHANNEL_LOG, SWITCH_LOG_CRIT, "Memory Error!\n"); switch_log_printf(SWITCH_CHANNEL_LOG, SWITCH_LOG_CRIT, "Memory Error!\n");
return SWITCH_STATUS_FALSE; return SWITCH_STATUS_FALSE;
} }
@ -92,17 +98,14 @@ switch_status_t sofia_presence_chat_send(char *proto, char *from, char *to, char
switch_safe_free(contact); switch_safe_free(contact);
nua_message(msg_nh, SIPTAG_CONTENT_TYPE_STR("text/html"), SIPTAG_PAYLOAD_STR(body), TAG_END()); nua_message(msg_nh, SIPTAG_CONTENT_TYPE_STR("text/html"), SIPTAG_PAYLOAD_STR(body), TAG_END());
switch_safe_free(ffrom); switch_safe_free(ffrom);
free(user); free(user);
if (profile) { if (profile) {
switch_thread_rwlock_unlock(profile->rwlock); switch_thread_rwlock_unlock(profile->rwlock);
} }
}
return SWITCH_STATUS_SUCCESS; return SWITCH_STATUS_SUCCESS;
} }
@ -149,8 +152,6 @@ void sofia_presence_establish_presence(sofia_profile_t *profile)
} }
} }
char *sofia_presence_translate_rpid(char *in, char *ext) char *sofia_presence_translate_rpid(char *in, char *ext)
{ {
char *r = in; char *r = in;
@ -233,7 +234,6 @@ void sofia_presence_mwi_event_handler(switch_event_t *event)
sofia_presence_mwi_callback, sofia_presence_mwi_callback,
profile); profile);
switch_safe_free(sql); switch_safe_free(sql);
switch_safe_free(dup_account); switch_safe_free(dup_account);
if (profile) { if (profile) {
@ -241,7 +241,6 @@ void sofia_presence_mwi_event_handler(switch_event_t *event)
} }
} }
void sofia_presence_event_handler(switch_event_t *event) void sofia_presence_event_handler(switch_event_t *event)
{ {
sofia_profile_t *profile = NULL; sofia_profile_t *profile = NULL;
@ -252,11 +251,9 @@ void sofia_presence_event_handler(switch_event_t *event)
char *rpid = switch_event_get_header(event, "rpid"); char *rpid = switch_event_get_header(event, "rpid");
char *status = switch_event_get_header(event, "status"); char *status = switch_event_get_header(event, "status");
char *event_type = switch_event_get_header(event, "event_type"); char *event_type = switch_event_get_header(event, "event_type");
//char *event_subtype = switch_event_get_header(event, "event_subtype");
char *sql = NULL; char *sql = NULL;
char *euser = NULL, *user = NULL, *host = NULL; char *euser = NULL, *user = NULL, *host = NULL;
if (rpid && !strcasecmp(rpid, "n/a")) { if (rpid && !strcasecmp(rpid, "n/a")) {
rpid = NULL; rpid = NULL;
} }
@ -304,15 +301,12 @@ void sofia_presence_event_handler(switch_event_t *event)
continue; continue;
} }
sofia_glue_execute_sql_callback(profile, sofia_glue_execute_sql_callback(profile,
SWITCH_FALSE, SWITCH_FALSE,
profile->ireg_mutex, profile->ireg_mutex,
sql, sql,
sofia_presence_sub_callback, sofia_presence_sub_callback,
profile); profile);
} }
switch_mutex_unlock(mod_sofia_globals.hash_mutex); switch_mutex_unlock(mod_sofia_globals.hash_mutex);
free(sql); free(sql);
@ -339,12 +333,10 @@ void sofia_presence_event_handler(switch_event_t *event)
} else { } else {
euser = user; euser = user;
} }
} else { } else {
return; return;
} }
switch (event->event_id) { switch (event->event_id) {
case SWITCH_EVENT_PRESENCE_PROBE: case SWITCH_EVENT_PRESENCE_PROBE:
if (proto) { if (proto) {
@ -374,9 +366,7 @@ void sofia_presence_event_handler(switch_event_t *event)
sofia_presence_resub_callback, sofia_presence_resub_callback,
profile); profile);
sofia_glue_release_profile(profile); sofia_glue_release_profile(profile);
switch_safe_free(sql); switch_safe_free(sql);
} }
switch_safe_free(user); switch_safe_free(user);
@ -413,7 +403,6 @@ void sofia_presence_event_handler(switch_event_t *event)
sql, sql,
sofia_presence_sub_callback, sofia_presence_sub_callback,
profile); profile);
} }
} }
switch_mutex_unlock(mod_sofia_globals.hash_mutex); switch_mutex_unlock(mod_sofia_globals.hash_mutex);
@ -425,7 +414,6 @@ void sofia_presence_event_handler(switch_event_t *event)
static int sofia_presence_sub_reg_callback(void *pArg, int argc, char **argv, char **columnNames) static int sofia_presence_sub_reg_callback(void *pArg, int argc, char **argv, char **columnNames)
{ {
sofia_profile_t *profile = (sofia_profile_t *) pArg; sofia_profile_t *profile = (sofia_profile_t *) pArg;
//char *proto = argv[0];
char *user = argv[1]; char *user = argv[1];
char *host = argv[2]; char *host = argv[2];
switch_event_t *event; switch_event_t *event;
@ -603,8 +591,6 @@ static int sofia_presence_sub_callback(void *pArg, int argc, char **argv, char *
SIPTAG_SUBSCRIPTION_STATE_STR("active;expires=3600"), SIPTAG_SUBSCRIPTION_STATE_STR("active;expires=3600"),
SIPTAG_EVENT_STR(event), SIPTAG_CONTENT_TYPE_STR("application/pidf+xml"), SIPTAG_PAYLOAD_STR(pl), TAG_END()); SIPTAG_EVENT_STR(event), SIPTAG_CONTENT_TYPE_STR("application/pidf+xml"), SIPTAG_PAYLOAD_STR(pl), TAG_END());
if (done) { if (done) {
switch_core_hash_delete(profile->sub_hash, call_id); switch_core_hash_delete(profile->sub_hash, call_id);
} }
@ -621,7 +607,7 @@ static int sofia_presence_mwi_callback(void *pArg, int argc, char **argv, char *
{ {
//char *proto = argv[0]; //char *proto = argv[0];
//char *user = argv[1]; //char *user = argv[1];
char *host = argv[2]; //char *host = argv[2];
char *sub_to_user = argv[3]; char *sub_to_user = argv[3];
char *sub_to_host = argv[4]; char *sub_to_host = argv[4];
char *event = argv[5]; char *event = argv[5];
@ -638,7 +624,7 @@ static int sofia_presence_mwi_callback(void *pArg, int argc, char **argv, char *
int expire_sec = atoi(expires); int expire_sec = atoi(expires);
if (!(profile = sofia_glue_find_profile(sub_to_host))) { if (!(profile = sofia_glue_find_profile(sub_to_host))) {
switch_log_printf(SWITCH_CHANNEL_LOG, SWITCH_LOG_ERROR, "Cannot find profile for host %s\n", host); switch_log_printf(SWITCH_CHANNEL_LOG, SWITCH_LOG_ERROR, "Cannot find profile for host %s\n", sub_to_host);
return 0; return 0;
} }
@ -658,17 +644,14 @@ static int sofia_presence_mwi_callback(void *pArg, int argc, char **argv, char *
SIPTAG_SUBSCRIPTION_STATE_STR(exp), SIPTAG_SUBSCRIPTION_STATE_STR(exp),
SIPTAG_EVENT_STR(event), SIPTAG_CONTENT_TYPE_STR("application/simple-message-summary"), SIPTAG_PAYLOAD_STR(body), TAG_END()); SIPTAG_EVENT_STR(event), SIPTAG_CONTENT_TYPE_STR("application/simple-message-summary"), SIPTAG_PAYLOAD_STR(body), TAG_END());
switch_safe_free(id); switch_safe_free(id);
switch_safe_free(exp); switch_safe_free(exp);
sofia_glue_release_profile(profile); sofia_glue_release_profile(profile);
return 0; return 0;
} }
void sofia_presence_handle_sip_i_subscribe(int status, void sofia_presence_handle_sip_i_subscribe(int status,
char const *phrase, char const *phrase,
nua_t *nua, sofia_profile_t *profile, nua_handle_t *nh, sofia_private_t *sofia_private, sip_t const *sip, tagi_t tags[]) nua_t *nua, sofia_profile_t *profile, nua_handle_t *nh, sofia_private_t *sofia_private, sip_t const *sip, tagi_t tags[])
@ -678,10 +661,9 @@ void sofia_presence_handle_sip_i_subscribe(int status,
sip_to_t const *to = sip->sip_to; sip_to_t const *to = sip->sip_to;
sip_from_t const *from = sip->sip_from; sip_from_t const *from = sip->sip_from;
sip_contact_t const *contact = sip->sip_contact; sip_contact_t const *contact = sip->sip_contact;
char *from_user = NULL; const char *from_user = NULL, *from_host = NULL;
char *from_host = NULL; const char *to_user = NULL, *to_host = NULL;
char *to_user = NULL; char *my_to_user = NULL;
char *to_host = NULL;
char *sql, *event = NULL; char *sql, *event = NULL;
char *proto = "sip"; char *proto = "sip";
char *d_user = NULL; char *d_user = NULL;
@ -698,7 +680,6 @@ void sofia_presence_handle_sip_i_subscribe(int status,
tl_gets(tags, tl_gets(tags,
NUTAG_SUBSTATE_REF(sub_state), TAG_END()); NUTAG_SUBSTATE_REF(sub_state), TAG_END());
if (contact) { if (contact) {
char *port = (char *) contact->m_url->url_port; char *port = (char *) contact->m_url->url_port;
@ -732,13 +713,13 @@ void sofia_presence_handle_sip_i_subscribe(int status,
} }
if (to) { if (to) {
to_user = (char *) to->a_url->url_user; to_user = to->a_url->url_user;
to_host = (char *) to->a_url->url_host; to_host = to->a_url->url_host;
} }
if (sip && sip->sip_from) { if (sip && sip->sip_from) {
from_user = (char *) sip->sip_from->a_url->url_user; from_user = sip->sip_from->a_url->url_user;
from_host = (char *) sip->sip_from->a_url->url_host; from_host = sip->sip_from->a_url->url_host;
} else { } else {
from_user = "n/a"; from_user = "n/a";
from_host = "n/a"; from_host = "n/a";
@ -776,8 +757,9 @@ void sofia_presence_handle_sip_i_subscribe(int status,
if (strchr(to_user, '+')) { if (strchr(to_user, '+')) {
char *h; char *h;
if ((proto = (d_user = strdup(to_user)))) { if ((proto = (d_user = strdup(to_user)))) {
if ((to_user = strchr(d_user, '+'))) { if ((my_to_user = strchr(d_user, '+'))) {
*to_user++ = '\0'; *my_to_user++ = '\0';
to_user = my_to_user;
if ((h = strchr(to_user, '+')) || (h = strchr(to_user, '@'))) { if ((h = strchr(to_user, '+')) || (h = strchr(to_user, '@'))) {
*h++ = '\0'; *h++ = '\0';
to_host = h; to_host = h;
@ -791,7 +773,7 @@ void sofia_presence_handle_sip_i_subscribe(int status,
} }
} }
call_id = sip->sip_call_id->i_id; //sip_header_as_string(profile->home, (void *) sip->sip_call_id); call_id = sip->sip_call_id->i_id;
event = sip_header_as_string(profile->home, (void *) sip->sip_event); event = sip_header_as_string(profile->home, (void *) sip->sip_event);
full_from = sip_header_as_string(profile->home, (void *) sip->sip_from); full_from = sip_header_as_string(profile->home, (void *) sip->sip_from);
full_via = sip_header_as_string(profile->home, (void *) sip->sip_via); full_via = sip_header_as_string(profile->home, (void *) sip->sip_via);
@ -799,8 +781,6 @@ void sofia_presence_handle_sip_i_subscribe(int status,
exp_raw = (sip->sip_expires ? sip->sip_expires->ex_delta : 3600); exp_raw = (sip->sip_expires ? sip->sip_expires->ex_delta : 3600);
exp = (long) time(NULL) + exp_raw; exp = (long) time(NULL) + exp_raw;
switch_mutex_lock(profile->ireg_mutex); switch_mutex_lock(profile->ireg_mutex);
sql = switch_mprintf("delete from sip_subscriptions where " sql = switch_mprintf("delete from sip_subscriptions where "
@ -827,11 +807,9 @@ void sofia_presence_handle_sip_i_subscribe(int status,
sofia_glue_execute_sql(profile, SWITCH_FALSE, sql, NULL); sofia_glue_execute_sql(profile, SWITCH_FALSE, sql, NULL);
free(sql); free(sql);
switch_mutex_unlock(profile->ireg_mutex); switch_mutex_unlock(profile->ireg_mutex);
sstr = switch_mprintf("active;expires=%ld", exp_raw); sstr = switch_mprintf("active;expires=%ld", exp_raw);
switch_core_hash_insert(profile->sub_hash, call_id, nh); switch_core_hash_insert(profile->sub_hash, call_id, nh);
} }
nua_respond(nh, SIP_202_ACCEPTED, nua_respond(nh, SIP_202_ACCEPTED,
@ -862,12 +840,6 @@ void sofia_presence_handle_sip_i_subscribe(int status,
su_free(profile->home, event); su_free(profile->home, event);
} }
/*
if (call_id) {
su_free(profile->home, call_id);
}
*/
if (full_from) { if (full_from) {
su_free(profile->home, full_from); su_free(profile->home, full_from);
} }
@ -975,70 +947,52 @@ void sofia_presence_handle_sip_i_publish(nua_t *nua, sofia_profile_t *profile, n
if (event_type) { if (event_type) {
su_free(profile->home, event_type); su_free(profile->home, event_type);
} }
switch_xml_free(xml); switch_xml_free(xml);
} }
} }
} }
nua_respond(nh, SIP_200_OK, NUTAG_WITH_THIS(nua), TAG_END()); nua_respond(nh, SIP_200_OK, NUTAG_WITH_THIS(nua), TAG_END());
} }
void sofia_presence_set_hash_key(char *hash_key, int32_t len, sip_t const *sip) void sofia_presence_set_hash_key(char *hash_key, int32_t len, sip_t const *sip)
{ {
url_t *to = sip->sip_to->a_url;
snprintf(hash_key, len, "%s%s%s", (char *) sip->sip_from->a_url->url_user, (char *) sip->sip_from->a_url->url_host, url_t *from = sip->sip_from->a_url;
(char *) sip->sip_to->a_url->url_user); snprintf(hash_key, len, "%s%s%s", from->url_user, from->url_host, to->url_user);
#if 0
/* nicer one we cant use in both directions >=0 */
snprintf(hash_key, len, "%s%s%s%s%s%s",
(char *) sip->sip_to->a_url->url_user,
(char *) sip->sip_to->a_url->url_host,
(char *) sip->sip_to->a_url->url_params,
(char *) sip->sip_from->a_url->url_user, (char *) sip->sip_from->a_url->url_host, (char *) sip->sip_from->a_url->url_params);
#endif
} }
void sofia_presence_handle_sip_i_message(int status, void sofia_presence_handle_sip_i_message(int status,
char const *phrase, char const *phrase,
nua_t *nua, sofia_profile_t *profile, nua_handle_t *nh, sofia_private_t *sofia_private, sip_t const *sip, tagi_t tags[]) nua_t *nua, sofia_profile_t *profile, nua_handle_t *nh, sofia_private_t *sofia_private, sip_t const *sip, tagi_t tags[])
{ {
if (sip) { if (sip) {
sip_from_t const *from = sip->sip_from; sip_from_t const *from = sip->sip_from;
char *from_user = NULL; const char *from_user = NULL;
char *from_host = NULL; const char *from_host = NULL;
sip_to_t const *to = sip->sip_to; sip_to_t const *to = sip->sip_to;
char *to_user = NULL; const char *to_user = NULL;
char *to_host = NULL; const char *to_host = NULL;
sip_subject_t const *sip_subject = sip->sip_subject; sip_subject_t const *sip_subject = sip->sip_subject;
sip_payload_t *payload = sip->sip_payload; sip_payload_t *payload = sip->sip_payload;
const char *subject = "n/a"; const char *subject = "n/a";
char *msg = NULL; char *msg = NULL;
if (sip->sip_content_type) { if (sip->sip_content_type) {
if (strstr((char *) sip->sip_content_type->c_subtype, "composing")) { if (strstr(sip->sip_content_type->c_subtype, "composing")) {
return; return;
} }
} }
if (from) { if (from) {
from_user = (char *) from->a_url->url_user; from_user = from->a_url->url_user;
from_host = (char *) from->a_url->url_host; from_host = from->a_url->url_host;
} }
if (to) { if (to) {
to_user = (char *) to->a_url->url_user; to_user = to->a_url->url_user;
to_host = (char *) to->a_url->url_host; to_host = to->a_url->url_host;
} }
if (!to_user) { if (!to_user) {
return; return;
} }
@ -1074,14 +1028,12 @@ void sofia_presence_handle_sip_i_message(int status,
*p = '@'; *p = '@';
} }
} }
} else { } else {
to_addr = switch_mprintf("%s@%s", to_user, to_host); to_addr = switch_mprintf("%s@%s", to_user, to_host);
} }
from_addr = switch_mprintf("%s@%s", from_user, from_host); from_addr = switch_mprintf("%s@%s", from_user, from_host);
sofia_presence_set_hash_key(hash_key, sizeof(hash_key), sip); sofia_presence_set_hash_key(hash_key, sizeof(hash_key), sip);
if ((tech_pvt = (private_object_t *) switch_core_hash_find(profile->chat_hash, hash_key))) { if ((tech_pvt = (private_object_t *) switch_core_hash_find(profile->chat_hash, hash_key))) {
channel = switch_core_session_get_channel(tech_pvt->session); channel = switch_core_session_get_channel(tech_pvt->session);
@ -1107,7 +1059,6 @@ void sofia_presence_handle_sip_i_message(int status,
} else { } else {
switch_log_printf(SWITCH_CHANNEL_LOG, SWITCH_LOG_ERROR, "Invalid Chat Interface [%s]!\n", proto); switch_log_printf(SWITCH_CHANNEL_LOG, SWITCH_LOG_ERROR, "Invalid Chat Interface [%s]!\n", proto);
} }
} }
switch_safe_free(to_addr); switch_safe_free(to_addr);
switch_safe_free(from_addr); switch_safe_free(from_addr);
@ -1115,7 +1066,6 @@ void sofia_presence_handle_sip_i_message(int status,
su_free(profile->home, full_from); su_free(profile->home, full_from);
} }
} }
} }
} }