2007-04-02 19:54:25 +00:00
|
|
|
/*
|
|
|
|
* FreeSWITCH Modular Media Switching Software Library / Soft-Switch Application
|
2009-02-13 23:37:37 +00:00
|
|
|
* Copyright (C) 2005-2009, Anthony Minessale II <anthm@freeswitch.org>
|
2007-04-02 19:54:25 +00:00
|
|
|
*
|
|
|
|
* 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
|
2009-02-04 21:20:54 +00:00
|
|
|
* Anthony Minessale II <anthm@freeswitch.org>
|
2007-04-02 19:54:25 +00:00
|
|
|
* Portions created by the Initial Developer are Copyright (C)
|
|
|
|
* the Initial Developer. All Rights Reserved.
|
|
|
|
*
|
|
|
|
* Contributor(s):
|
|
|
|
*
|
2009-02-04 21:20:54 +00:00
|
|
|
* Anthony Minessale II <anthm@freeswitch.org>
|
2007-04-02 19:54:25 +00:00
|
|
|
* Ken Rice, Asteria Solutions Group, Inc <ken@asteriasgi.com>
|
|
|
|
* Paul D. Tinsley <pdt at jackhammer.org>
|
|
|
|
* Bret McDanel <trixter AT 0xdecafbad.com>
|
|
|
|
*
|
|
|
|
*
|
|
|
|
* sofia_presence.c -- SOFIA SIP Endpoint (presence code)
|
|
|
|
*
|
|
|
|
*/
|
2007-03-31 19:01:33 +00:00
|
|
|
#include "mod_sofia.h"
|
|
|
|
|
2008-12-11 20:20:20 +00:00
|
|
|
#define SUB_OVERLAP 300
|
|
|
|
|
2007-04-02 19:54:25 +00:00
|
|
|
static int sofia_presence_mwi_callback(void *pArg, int argc, char **argv, char **columnNames);
|
2007-12-13 23:30:16 +00:00
|
|
|
static int sofia_presence_mwi_callback2(void *pArg, int argc, char **argv, char **columnNames);
|
2007-04-02 19:54:25 +00:00
|
|
|
static int sofia_presence_sub_reg_callback(void *pArg, int argc, char **argv, char **columnNames);
|
|
|
|
static int sofia_presence_resub_callback(void *pArg, int argc, char **argv, char **columnNames);
|
|
|
|
static int sofia_presence_sub_callback(void *pArg, int argc, char **argv, char **columnNames);
|
2007-03-31 19:01:33 +00:00
|
|
|
|
2007-12-15 00:39:53 +00:00
|
|
|
struct presence_helper {
|
|
|
|
sofia_profile_t *profile;
|
|
|
|
switch_event_t *event;
|
2007-12-18 01:12:50 +00:00
|
|
|
switch_stream_handle_t stream;
|
2008-04-11 19:41:24 +00:00
|
|
|
char last_uuid[512];
|
2007-12-15 00:39:53 +00:00
|
|
|
};
|
|
|
|
|
2009-01-20 20:49:47 +00:00
|
|
|
switch_status_t sofia_presence_chat_send(const char *proto, const char *from, const char *to, const char *subject,
|
|
|
|
const char *body, const char *type, const char *hint)
|
2007-03-31 19:01:33 +00:00
|
|
|
{
|
|
|
|
char buf[256];
|
2008-09-27 20:52:33 +00:00
|
|
|
char *prof = NULL, *user = NULL, *host = NULL;
|
2007-04-29 01:16:49 +00:00
|
|
|
sofia_profile_t *profile = NULL;
|
2007-03-31 19:01:33 +00:00
|
|
|
char *ffrom = NULL;
|
|
|
|
nua_handle_t *msg_nh;
|
|
|
|
char *contact;
|
2008-09-27 20:52:33 +00:00
|
|
|
char *dup = NULL;
|
2008-06-27 17:46:04 +00:00
|
|
|
switch_status_t status = SWITCH_STATUS_FALSE;
|
2008-07-23 18:19:56 +00:00
|
|
|
const char *ct = "text/html";
|
|
|
|
|
2007-11-20 03:26:40 +00:00
|
|
|
if (!to) {
|
2008-06-27 17:46:04 +00:00
|
|
|
switch_log_printf(SWITCH_CHANNEL_LOG, SWITCH_LOG_ERROR, "Missing To: header.\n");
|
|
|
|
goto end;
|
2007-11-20 03:26:40 +00:00
|
|
|
}
|
2007-03-31 19:01:33 +00:00
|
|
|
|
2009-01-20 21:24:37 +00:00
|
|
|
if (!switch_strlen_zero(type)) {
|
|
|
|
ct = type;
|
|
|
|
}
|
|
|
|
|
2008-09-27 20:52:33 +00:00
|
|
|
dup = strdup(to);
|
|
|
|
switch_assert(dup);
|
|
|
|
prof = dup;
|
|
|
|
|
2008-10-11 19:20:11 +00:00
|
|
|
/* Do we have a user of the form profile/user[@host]? */
|
2008-09-27 20:52:33 +00:00
|
|
|
if ((user = strchr(prof, '/'))) {
|
|
|
|
*user++ = '\0';
|
|
|
|
} else {
|
|
|
|
user = prof;
|
|
|
|
prof = NULL;
|
|
|
|
}
|
2007-03-31 19:01:33 +00:00
|
|
|
|
2007-11-20 03:26:40 +00:00
|
|
|
if ((host = strchr(user, '@'))) {
|
|
|
|
*host++ = '\0';
|
2008-09-27 20:52:33 +00:00
|
|
|
if (!prof) prof = host;
|
2007-11-20 03:26:40 +00:00
|
|
|
}
|
2007-03-31 19:01:33 +00:00
|
|
|
|
2009-01-20 21:24:37 +00:00
|
|
|
if (!prof || !(profile = sofia_glue_find_profile(prof))) {
|
2007-11-20 03:26:40 +00:00
|
|
|
switch_log_printf(SWITCH_CHANNEL_LOG, SWITCH_LOG_ERROR,
|
|
|
|
"Chat proto [%s]\nfrom [%s]\nto [%s]\n%s\nInvalid Profile %s\n", proto, from, to,
|
2008-09-27 20:52:33 +00:00
|
|
|
body ? body : "[no body]", prof ? prof : "NULL");
|
2008-06-27 17:46:04 +00:00
|
|
|
goto end;
|
2007-11-20 03:26:40 +00:00
|
|
|
}
|
2007-03-31 19:01:33 +00:00
|
|
|
|
2009-01-20 21:24:37 +00:00
|
|
|
if (switch_strlen_zero(host)) {
|
|
|
|
host = profile->domain_name;
|
|
|
|
if (switch_strlen_zero(host)) {
|
2009-01-20 21:28:25 +00:00
|
|
|
host = prof;
|
2009-01-20 21:24:37 +00:00
|
|
|
}
|
|
|
|
}
|
2007-11-20 03:26:40 +00:00
|
|
|
if (!sofia_reg_find_reg_url(profile, user, host, buf, sizeof(buf))) {
|
2008-06-27 17:46:04 +00:00
|
|
|
switch_log_printf(SWITCH_CHANNEL_LOG, SWITCH_LOG_ERROR, "Cannot find user. [%s][%s]\n", user, host);
|
|
|
|
goto end;
|
2007-11-20 03:26:40 +00:00
|
|
|
}
|
2007-03-31 19:01:33 +00:00
|
|
|
|
2008-06-27 17:46:04 +00:00
|
|
|
if (!strcasecmp(proto, SOFIA_CHAT_PROTO)) {
|
2007-11-20 03:26:40 +00:00
|
|
|
from = hint;
|
|
|
|
} else {
|
2009-01-20 21:24:37 +00:00
|
|
|
char *fp, *p = NULL;
|
|
|
|
|
2007-11-20 03:26:40 +00:00
|
|
|
fp = strdup(from);
|
2009-01-20 21:24:37 +00:00
|
|
|
|
2007-11-20 03:26:40 +00:00
|
|
|
if (!fp) {
|
|
|
|
switch_log_printf(SWITCH_CHANNEL_LOG, SWITCH_LOG_CRIT, "Memory Error!\n");
|
2008-06-27 17:46:04 +00:00
|
|
|
goto end;
|
2007-11-20 03:26:40 +00:00
|
|
|
}
|
2007-03-31 19:01:33 +00:00
|
|
|
|
2007-11-20 03:26:40 +00:00
|
|
|
if ((p = strchr(fp, '@'))) {
|
2009-01-20 21:24:37 +00:00
|
|
|
*p++ = '\0';
|
2007-03-31 19:01:33 +00:00
|
|
|
}
|
|
|
|
|
2009-01-20 21:24:37 +00:00
|
|
|
if (switch_strlen_zero(p)) {
|
2009-01-20 21:28:54 +00:00
|
|
|
p = profile->domain_name;
|
2009-01-20 21:24:37 +00:00
|
|
|
if (switch_strlen_zero(p)) {
|
2009-01-20 21:28:25 +00:00
|
|
|
p = host;
|
2009-01-20 21:24:37 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
ffrom = switch_mprintf("\"%s\" <sip:%s+%s@%s>", fp, proto, fp, p);
|
|
|
|
|
2007-11-20 03:26:40 +00:00
|
|
|
from = ffrom;
|
|
|
|
switch_safe_free(fp);
|
|
|
|
}
|
2007-03-31 19:01:33 +00:00
|
|
|
|
2008-06-27 17:46:04 +00:00
|
|
|
status = SWITCH_STATUS_SUCCESS;
|
2007-11-20 03:26:40 +00:00
|
|
|
contact = sofia_glue_get_url_from_contact(buf, 1);
|
2008-10-11 19:20:11 +00:00
|
|
|
/* if this cries, add contact here too, change the 1 to 0 and omit the safe_free */
|
2008-06-27 17:46:04 +00:00
|
|
|
msg_nh = nua_handle(profile->nua, NULL, SIPTAG_FROM_STR(from), NUTAG_URL(contact), SIPTAG_TO_STR(buf),
|
2007-11-20 03:26:40 +00:00
|
|
|
SIPTAG_CONTACT_STR(profile->url), TAG_END());
|
2007-03-31 19:01:33 +00:00
|
|
|
|
2007-11-20 03:26:40 +00:00
|
|
|
switch_safe_free(contact);
|
2008-07-23 18:19:56 +00:00
|
|
|
nua_message(msg_nh, SIPTAG_CONTENT_TYPE_STR(ct), SIPTAG_PAYLOAD_STR(body), TAG_END());
|
2007-03-31 19:01:33 +00:00
|
|
|
|
2008-06-27 17:46:04 +00:00
|
|
|
end:
|
|
|
|
|
2007-11-20 03:26:40 +00:00
|
|
|
switch_safe_free(ffrom);
|
2008-09-27 20:52:33 +00:00
|
|
|
switch_safe_free(dup);
|
2008-06-27 17:46:04 +00:00
|
|
|
|
2007-11-20 03:26:40 +00:00
|
|
|
if (profile) {
|
|
|
|
switch_thread_rwlock_unlock(profile->rwlock);
|
2007-03-31 19:01:33 +00:00
|
|
|
}
|
|
|
|
|
2008-06-27 17:46:04 +00:00
|
|
|
return status;
|
2007-03-31 19:01:33 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void sofia_presence_cancel(void)
|
|
|
|
{
|
2007-04-04 03:08:17 +00:00
|
|
|
char *sql;
|
2007-03-31 19:01:33 +00:00
|
|
|
sofia_profile_t *profile;
|
|
|
|
switch_hash_index_t *hi;
|
|
|
|
void *val;
|
2007-12-18 01:12:50 +00:00
|
|
|
struct presence_helper helper = { 0 };
|
2008-02-04 19:01:22 +00:00
|
|
|
|
2008-05-27 04:54:52 +00:00
|
|
|
if (!mod_sofia_globals.profile_hash)
|
|
|
|
return;
|
2008-09-18 00:01:03 +00:00
|
|
|
|
|
|
|
if ((sql = switch_mprintf(
|
|
|
|
"select proto,sip_user,sip_host,sub_to_user,sub_to_host,event,contact,call_id,full_from,"
|
|
|
|
"full_via,expires,user_agent,accept,profile_name"
|
|
|
|
",-1,'unavailable','unavailable' from sip_subscriptions where event='presence'"))) {
|
2007-04-01 01:16:16 +00:00
|
|
|
switch_mutex_lock(mod_sofia_globals.hash_mutex);
|
2007-09-24 19:34:25 +00:00
|
|
|
for (hi = switch_hash_first(NULL, mod_sofia_globals.profile_hash); hi; hi = switch_hash_next(hi)) {
|
2007-03-31 19:01:33 +00:00
|
|
|
switch_hash_this(hi, NULL, NULL, &val);
|
|
|
|
profile = (sofia_profile_t *) val;
|
2008-09-18 00:01:03 +00:00
|
|
|
if (profile->pres_type != PRES_TYPE_FULL) {
|
2007-03-31 19:01:33 +00:00
|
|
|
continue;
|
|
|
|
}
|
2007-12-15 00:39:53 +00:00
|
|
|
helper.profile = profile;
|
|
|
|
helper.event = NULL;
|
|
|
|
if (sofia_glue_execute_sql_callback(profile, SWITCH_FALSE, profile->ireg_mutex, sql, sofia_presence_sub_callback, &helper) != SWITCH_TRUE) {
|
2007-03-31 19:01:33 +00:00
|
|
|
continue;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
switch_safe_free(sql);
|
2007-04-04 03:08:17 +00:00
|
|
|
switch_mutex_unlock(mod_sofia_globals.hash_mutex);
|
2007-03-31 19:01:33 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2007-04-13 22:15:58 +00:00
|
|
|
void sofia_presence_establish_presence(sofia_profile_t *profile)
|
2007-03-31 19:01:33 +00:00
|
|
|
{
|
|
|
|
|
2008-05-27 04:54:52 +00:00
|
|
|
if (sofia_glue_execute_sql_callback(profile, SWITCH_FALSE, profile->ireg_mutex,
|
|
|
|
"select sip_user,sip_host,'Registered','unknown','' from sip_registrations",
|
2007-04-04 03:08:17 +00:00
|
|
|
sofia_presence_resub_callback, profile) != SWITCH_TRUE) {
|
2007-03-31 19:01:33 +00:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2007-04-04 03:08:17 +00:00
|
|
|
if (sofia_glue_execute_sql_callback(profile, SWITCH_FALSE, profile->ireg_mutex,
|
|
|
|
"select sub_to_user,sub_to_host,'Online','unknown',proto from sip_subscriptions "
|
2008-05-27 04:54:52 +00:00
|
|
|
"where proto='ext' or proto='user' or proto='conf'", sofia_presence_resub_callback, profile) != SWITCH_TRUE) {
|
2007-04-04 03:08:17 +00:00
|
|
|
return;
|
2007-03-31 19:01:33 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
char *sofia_presence_translate_rpid(char *in, char *ext)
|
|
|
|
{
|
2007-10-24 23:20:47 +00:00
|
|
|
char *r = in;
|
2007-03-31 19:01:33 +00:00
|
|
|
|
2007-11-15 16:22:18 +00:00
|
|
|
if (in && (switch_stristr("null", in))) {
|
2007-03-31 19:01:33 +00:00
|
|
|
in = NULL;
|
|
|
|
}
|
2008-05-27 04:54:52 +00:00
|
|
|
|
2007-03-31 19:01:33 +00:00
|
|
|
if (!in) {
|
|
|
|
in = ext;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!in) {
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
2007-10-24 23:20:47 +00:00
|
|
|
if (!strcasecmp(in, "dnd") || !strcasecmp(in, "idle")) {
|
2007-03-31 19:01:33 +00:00
|
|
|
r = "busy";
|
|
|
|
}
|
|
|
|
|
|
|
|
return r;
|
|
|
|
}
|
|
|
|
|
2008-05-08 16:09:49 +00:00
|
|
|
struct mwi_helper {
|
|
|
|
sofia_profile_t *profile;
|
|
|
|
int total;
|
|
|
|
};
|
|
|
|
|
2008-03-14 03:46:54 +00:00
|
|
|
static void actual_sofia_presence_mwi_event_handler(switch_event_t *event)
|
2007-04-02 19:54:25 +00:00
|
|
|
{
|
2008-01-11 01:03:51 +00:00
|
|
|
char *account, *dup_account, *yn, *host, *user;
|
2007-04-02 19:54:25 +00:00
|
|
|
char *sql;
|
2007-04-29 01:16:49 +00:00
|
|
|
sofia_profile_t *profile = NULL;
|
2007-04-02 19:54:25 +00:00
|
|
|
switch_stream_handle_t stream = { 0 };
|
|
|
|
switch_event_header_t *hp;
|
2008-05-08 16:09:49 +00:00
|
|
|
struct mwi_helper h = { 0 };
|
|
|
|
char *pname = NULL;
|
|
|
|
|
2007-12-12 23:21:45 +00:00
|
|
|
switch_assert(event != NULL);
|
2007-04-02 19:54:25 +00:00
|
|
|
|
2008-01-11 01:03:51 +00:00
|
|
|
if (!(account = switch_event_get_header(event, "mwi-message-account"))) {
|
2007-04-02 19:54:25 +00:00
|
|
|
switch_log_printf(SWITCH_CHANNEL_LOG, SWITCH_LOG_ERROR, "Missing required Header 'MWI-Message-Account'\n");
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!(yn = switch_event_get_header(event, "mwi-messages-waiting"))) {
|
|
|
|
switch_log_printf(SWITCH_CHANNEL_LOG, SWITCH_LOG_ERROR, "Missing required Header 'MWI-Messages-Waiting'\n");
|
|
|
|
return;
|
|
|
|
}
|
2008-01-11 01:03:51 +00:00
|
|
|
|
|
|
|
dup_account = strdup(account);
|
2007-12-12 23:21:45 +00:00
|
|
|
switch_assert(dup_account != NULL);
|
2007-04-02 19:54:25 +00:00
|
|
|
sofia_glue_get_user_host(dup_account, &user, &host);
|
|
|
|
|
2008-05-08 16:09:49 +00:00
|
|
|
|
|
|
|
if ((pname = switch_event_get_header(event, "sofia-profile"))) {
|
2008-05-09 19:59:42 +00:00
|
|
|
if (!(profile = sofia_glue_find_profile(pname))) {
|
2008-10-11 05:42:52 +00:00
|
|
|
switch_log_printf(SWITCH_CHANNEL_LOG, SWITCH_LOG_DEBUG, "No profile %s\n", pname);
|
2008-05-08 16:09:49 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!profile) {
|
|
|
|
if (!host || !(profile = sofia_glue_find_profile(host))) {
|
|
|
|
switch_log_printf(SWITCH_CHANNEL_LOG, SWITCH_LOG_ERROR, "Cannot find profile %s\n", switch_str_nil(host));
|
|
|
|
switch_safe_free(dup_account);
|
|
|
|
return;
|
|
|
|
}
|
2007-04-02 19:54:25 +00:00
|
|
|
}
|
2008-05-27 04:54:52 +00:00
|
|
|
|
2008-08-13 17:24:33 +00:00
|
|
|
if (profile->domain_name && strcasecmp(profile->domain_name, host)) {
|
2008-05-08 16:09:49 +00:00
|
|
|
host = profile->domain_name;
|
|
|
|
}
|
|
|
|
|
|
|
|
h.profile = profile;
|
|
|
|
h.total = 0;
|
|
|
|
|
2007-04-02 19:54:25 +00:00
|
|
|
SWITCH_STANDARD_STREAM(stream);
|
|
|
|
|
|
|
|
for (hp = event->headers; hp; hp = hp->next) {
|
|
|
|
if (!strncasecmp(hp->name, "mwi-", 4)) {
|
2007-10-12 22:08:30 +00:00
|
|
|
char *tmp = NULL;
|
|
|
|
char *value = hp->value;
|
|
|
|
if (!strcasecmp(hp->name, "mwi-message-account") && strncasecmp(hp->value, "sip:", 4)) {
|
|
|
|
tmp = switch_mprintf("sip:%s", hp->value);
|
|
|
|
value = tmp;
|
2008-05-27 04:54:52 +00:00
|
|
|
}
|
2007-10-12 22:08:30 +00:00
|
|
|
stream.write_function(&stream, "%s: %s\r\n", hp->name + 4, value);
|
|
|
|
switch_safe_free(tmp);
|
2007-04-02 19:54:25 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
stream.write_function(&stream, "\r\n");
|
2008-09-18 00:01:03 +00:00
|
|
|
|
|
|
|
sql = switch_mprintf("select proto,sip_user,sip_host,sub_to_user,sub_to_host,event,contact,call_id,full_from,"
|
|
|
|
"full_via,expires,user_agent,accept,profile_name"
|
|
|
|
",'%q','%q' from sip_subscriptions where event='message-summary' "
|
|
|
|
"and sub_to_user='%q' and (sub_to_host='%q' or presence_hosts like '%%%q%%')",
|
|
|
|
stream.data, host, user, host, host);
|
2008-05-27 04:54:52 +00:00
|
|
|
|
|
|
|
switch_assert(sql != NULL);
|
|
|
|
sofia_glue_execute_sql_callback(profile, SWITCH_FALSE, profile->ireg_mutex, sql, sofia_presence_mwi_callback, &h);
|
2008-09-18 00:01:03 +00:00
|
|
|
|
2007-04-02 19:54:25 +00:00
|
|
|
switch_safe_free(sql);
|
2007-12-13 23:30:16 +00:00
|
|
|
|
2008-09-18 00:01:03 +00:00
|
|
|
sql = switch_mprintf("select sip_user,sip_host,contact,profile_name,'%q' from sip_registrations where sip_user='%q' and sip_host='%q'",
|
|
|
|
stream.data, user, host);
|
2008-05-27 04:54:52 +00:00
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
switch_assert(sql != NULL);
|
|
|
|
sofia_glue_execute_sql_callback(profile, SWITCH_FALSE, profile->ireg_mutex, sql, sofia_presence_mwi_callback2, &h);
|
|
|
|
|
2008-05-26 16:09:08 +00:00
|
|
|
switch_safe_free(sql);
|
2007-12-13 23:30:16 +00:00
|
|
|
|
|
|
|
switch_safe_free(stream.data);
|
|
|
|
|
2007-04-02 19:54:25 +00:00
|
|
|
switch_safe_free(dup_account);
|
2007-04-29 01:16:49 +00:00
|
|
|
if (profile) {
|
2007-05-03 01:55:25 +00:00
|
|
|
sofia_glue_release_profile(profile);
|
2007-04-29 01:16:49 +00:00
|
|
|
}
|
2007-04-02 19:54:25 +00:00
|
|
|
}
|
|
|
|
|
2008-03-14 03:46:54 +00:00
|
|
|
static void actual_sofia_presence_event_handler(switch_event_t *event)
|
2007-03-31 19:01:33 +00:00
|
|
|
{
|
2007-04-29 01:16:49 +00:00
|
|
|
sofia_profile_t *profile = NULL;
|
2007-03-31 19:01:33 +00:00
|
|
|
switch_hash_index_t *hi;
|
2008-12-10 20:54:24 +00:00
|
|
|
const void *var;
|
2007-03-31 19:01:33 +00:00
|
|
|
void *val;
|
|
|
|
char *from = switch_event_get_header(event, "from");
|
|
|
|
char *proto = switch_event_get_header(event, "proto");
|
|
|
|
char *rpid = switch_event_get_header(event, "rpid");
|
|
|
|
char *status = switch_event_get_header(event, "status");
|
|
|
|
char *event_type = switch_event_get_header(event, "event_type");
|
2007-12-15 00:39:53 +00:00
|
|
|
char *alt_event_type = switch_event_get_header(event, "alt_event_type");
|
2007-03-31 19:01:33 +00:00
|
|
|
char *sql = NULL;
|
|
|
|
char *euser = NULL, *user = NULL, *host = NULL;
|
|
|
|
|
2008-03-14 03:46:54 +00:00
|
|
|
if (!mod_sofia_globals.running) {
|
|
|
|
return;
|
|
|
|
}
|
2008-05-27 04:54:52 +00:00
|
|
|
|
2007-03-31 19:01:33 +00:00
|
|
|
if (rpid && !strcasecmp(rpid, "n/a")) {
|
|
|
|
rpid = NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (status && !strcasecmp(status, "n/a")) {
|
|
|
|
status = NULL;
|
|
|
|
}
|
2008-11-22 16:14:56 +00:00
|
|
|
|
2008-11-25 20:54:58 +00:00
|
|
|
if (status && switch_stristr("CS_HANGUP", status)) {
|
|
|
|
status = "Call Ended";
|
2008-11-22 16:14:56 +00:00
|
|
|
}
|
2007-03-31 19:01:33 +00:00
|
|
|
|
|
|
|
if (rpid) {
|
|
|
|
rpid = sofia_presence_translate_rpid(rpid, status);
|
|
|
|
}
|
2008-11-25 20:54:58 +00:00
|
|
|
|
2007-03-31 19:01:33 +00:00
|
|
|
if (event->event_id == SWITCH_EVENT_ROSTER) {
|
2007-12-18 01:12:50 +00:00
|
|
|
struct presence_helper helper = { 0 };
|
2008-02-04 19:01:22 +00:00
|
|
|
|
2008-05-27 04:54:52 +00:00
|
|
|
if (!mod_sofia_globals.profile_hash)
|
|
|
|
return;
|
|
|
|
|
2007-03-31 19:01:33 +00:00
|
|
|
if (from) {
|
2008-09-18 00:01:03 +00:00
|
|
|
sql = switch_mprintf(
|
2008-11-22 01:34:19 +00:00
|
|
|
"select sip_subscriptions.proto,sip_subscriptions.sip_user,sip_subscriptions.sip_host,"
|
|
|
|
"sip_subscriptions.sub_to_user,sip_subscriptions.sub_to_host,sip_subscriptions.event,"
|
|
|
|
"sip_subscriptions.contact,sip_subscriptions.call_id,sip_subscriptions.full_from,"
|
|
|
|
"sip_subscriptions.full_via,sip_subscriptions.expires,sip_subscriptions.user_agent,"
|
|
|
|
"sip_subscriptions.accept,sip_subscriptions.profile_name"
|
|
|
|
",1,'%q','%q',sip_presence.status,sip_presence.rpid "
|
|
|
|
"from sip_subscriptions left join sip_presence on "
|
2008-11-22 02:51:00 +00:00
|
|
|
"(sip_subscriptions.sub_to_user=sip_presence.sip_user and sip_subscriptions.sub_to_host=sip_presence.sip_host and "
|
2008-11-22 01:34:19 +00:00
|
|
|
"sip_subscriptions.profile_name=sip_presence.profile_name) "
|
2008-11-25 20:54:58 +00:00
|
|
|
"where sip_subscriptions.event='presence' and sip_subscriptions.full_from like '%%%q%%'",
|
|
|
|
switch_str_nil(status), switch_str_nil(rpid), from);
|
2007-03-31 19:01:33 +00:00
|
|
|
} else {
|
2008-11-22 01:34:19 +00:00
|
|
|
sql = switch_mprintf(
|
|
|
|
"select sip_subscriptions.proto,sip_subscriptions.sip_user,sip_subscriptions.sip_host,"
|
|
|
|
"sip_subscriptions.sub_to_user,sip_subscriptions.sub_to_host,sip_subscriptions.event,"
|
|
|
|
"sip_subscriptions.contact,sip_subscriptions.call_id,sip_subscriptions.full_from,"
|
|
|
|
"sip_subscriptions.full_via,sip_subscriptions.expires,sip_subscriptions.user_agent,"
|
|
|
|
"sip_subscriptions.accept,sip_subscriptions.profile_name"
|
|
|
|
",1,'%q','%q',sip_presence.status,sip_presence.rpid "
|
|
|
|
"from sip_subscriptions left join sip_presence on "
|
2008-11-22 02:51:00 +00:00
|
|
|
"(sip_subscriptions.sub_to_user=sip_presence.sip_user and sip_subscriptions.sub_to_host=sip_presence.sip_host and "
|
2008-11-22 01:34:19 +00:00
|
|
|
"sip_subscriptions.profile_name=sip_presence.profile_name) "
|
2008-11-25 20:54:58 +00:00
|
|
|
"where sip_subscriptions.event='presence'", switch_str_nil(status), switch_str_nil(rpid));
|
2007-03-31 19:01:33 +00:00
|
|
|
}
|
2007-10-24 23:20:47 +00:00
|
|
|
|
2007-12-12 23:21:45 +00:00
|
|
|
switch_assert(sql != NULL);
|
2007-04-01 01:16:16 +00:00
|
|
|
switch_mutex_lock(mod_sofia_globals.hash_mutex);
|
2007-09-24 19:34:25 +00:00
|
|
|
for (hi = switch_hash_first(NULL, mod_sofia_globals.profile_hash); hi; hi = switch_hash_next(hi)) {
|
2008-12-10 20:54:24 +00:00
|
|
|
switch_hash_this(hi, &var, NULL, &val);
|
2007-03-31 19:01:33 +00:00
|
|
|
profile = (sofia_profile_t *) val;
|
2008-12-10 20:54:24 +00:00
|
|
|
|
|
|
|
if (strcmp((char *)var, profile->name)) {
|
|
|
|
if (mod_sofia_globals.debug_presence > 0) {
|
|
|
|
switch_log_printf(SWITCH_CHANNEL_LOG, SWITCH_LOG_WARNING, "%s is an alias, skipping\n", (char *) var);
|
|
|
|
}
|
|
|
|
continue;
|
|
|
|
}
|
2008-09-18 00:01:03 +00:00
|
|
|
if (profile->pres_type != PRES_TYPE_FULL) {
|
2008-12-10 20:54:24 +00:00
|
|
|
if (mod_sofia_globals.debug_presence > 0) {
|
|
|
|
switch_log_printf(SWITCH_CHANNEL_LOG, SWITCH_LOG_WARNING, "%s is passive, skipping\n", (char *) var);
|
|
|
|
}
|
2007-03-31 19:01:33 +00:00
|
|
|
continue;
|
|
|
|
}
|
2007-12-15 00:39:53 +00:00
|
|
|
helper.profile = profile;
|
2008-05-27 04:54:52 +00:00
|
|
|
helper.event = NULL;
|
|
|
|
sofia_glue_execute_sql_callback(profile, SWITCH_FALSE, profile->ireg_mutex, sql, sofia_presence_sub_callback, &helper);
|
2007-03-31 19:01:33 +00:00
|
|
|
}
|
2007-04-01 01:16:16 +00:00
|
|
|
switch_mutex_unlock(mod_sofia_globals.hash_mutex);
|
2007-04-04 03:08:17 +00:00
|
|
|
free(sql);
|
2007-03-31 19:01:33 +00:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (switch_strlen_zero(event_type)) {
|
|
|
|
event_type = "presence";
|
|
|
|
}
|
|
|
|
|
2007-12-15 00:39:53 +00:00
|
|
|
if (switch_strlen_zero(alt_event_type)) {
|
|
|
|
alt_event_type = "presence";
|
|
|
|
}
|
2008-05-27 04:54:52 +00:00
|
|
|
|
2007-03-31 19:01:33 +00:00
|
|
|
if ((user = strdup(from))) {
|
|
|
|
if ((host = strchr(user, '@'))) {
|
|
|
|
char *p;
|
|
|
|
*host++ = '\0';
|
|
|
|
if ((p = strchr(host, '/'))) {
|
|
|
|
*p = '\0';
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
switch_safe_free(user);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
if ((euser = strchr(user, '+'))) {
|
|
|
|
euser++;
|
|
|
|
} else {
|
|
|
|
euser = user;
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2008-12-10 20:54:24 +00:00
|
|
|
|
2007-03-31 19:01:33 +00:00
|
|
|
switch (event->event_id) {
|
|
|
|
case SWITCH_EVENT_PRESENCE_PROBE:
|
|
|
|
if (proto) {
|
|
|
|
char *to = switch_event_get_header(event, "to");
|
2008-12-15 21:13:03 +00:00
|
|
|
char *probe_user = NULL, *probe_euser, *probe_host, *p;
|
2007-03-31 19:01:33 +00:00
|
|
|
|
2007-12-13 04:01:29 +00:00
|
|
|
if (!to || !(probe_user = strdup(to))) {
|
2008-12-15 21:13:03 +00:00
|
|
|
goto done;
|
2007-03-31 19:01:33 +00:00
|
|
|
}
|
|
|
|
|
2007-12-13 04:01:29 +00:00
|
|
|
if ((probe_host = strchr(probe_user, '@'))) {
|
|
|
|
*probe_host++ = '\0';
|
2007-03-31 19:01:33 +00:00
|
|
|
}
|
2007-12-13 04:01:29 +00:00
|
|
|
probe_euser = probe_user;
|
|
|
|
if ((p = strchr(probe_euser, '+'))) {
|
|
|
|
probe_euser = (p + 1);
|
2007-03-31 19:01:33 +00:00
|
|
|
}
|
|
|
|
|
2007-12-18 01:12:50 +00:00
|
|
|
if (probe_euser && probe_host && (profile = sofia_glue_find_profile(probe_host))) {
|
2008-09-18 01:31:56 +00:00
|
|
|
sql = switch_mprintf("select sip_registrations.sip_user, '%q', sip_registrations.status, "
|
2008-06-02 16:40:44 +00:00
|
|
|
"sip_registrations.rpid,'', sip_dialogs.uuid, sip_dialogs.state, sip_dialogs.direction, "
|
2008-11-22 01:34:19 +00:00
|
|
|
"sip_dialogs.sip_to_user, sip_dialogs.sip_to_host, sip_presence.status,sip_presence.rpid "
|
2007-12-18 01:12:50 +00:00
|
|
|
"from sip_registrations left join sip_dialogs on "
|
2008-11-22 01:34:19 +00:00
|
|
|
"(sip_dialogs.sip_from_user = sip_registrations.sip_user "
|
|
|
|
"and sip_dialogs.sip_from_host = sip_registrations.sip_host) "
|
|
|
|
"left join sip_presence on "
|
|
|
|
"(sip_registrations.sip_user=sip_presence.sip_user and sip_registrations.sip_host=sip_presence.sip_host and "
|
|
|
|
"sip_registrations.profile_name=sip_presence.profile_name) "
|
2008-09-18 01:31:56 +00:00
|
|
|
"where sip_registrations.sip_user='%q' and "
|
|
|
|
"(sip_registrations.sip_host='%q' or sip_registrations.presence_hosts like '%%%q%%')",
|
|
|
|
probe_host, probe_euser, probe_host, probe_host);
|
2007-12-18 01:12:50 +00:00
|
|
|
switch_assert(sql);
|
2008-12-10 20:54:24 +00:00
|
|
|
|
|
|
|
|
|
|
|
if (mod_sofia_globals.debug_presence > 0) {
|
|
|
|
switch_log_printf(SWITCH_CHANNEL_LOG, SWITCH_LOG_INFO, "%s START_PRESENCE_PROBE_SQL\n", profile->name);
|
|
|
|
}
|
|
|
|
|
|
|
|
if (mod_sofia_globals.debug_presence > 1) {
|
|
|
|
switch_log_printf(SWITCH_CHANNEL_LOG, SWITCH_LOG_ERROR, "%s DUMP PRESENCE_PROBE_SQL:\n%s\n", profile->name, sql);
|
|
|
|
}
|
|
|
|
|
2008-05-27 04:54:52 +00:00
|
|
|
sofia_glue_execute_sql_callback(profile, SWITCH_FALSE, profile->ireg_mutex, sql, sofia_presence_resub_callback, profile);
|
2008-12-10 20:54:24 +00:00
|
|
|
if (mod_sofia_globals.debug_presence > 0) {
|
|
|
|
switch_log_printf(SWITCH_CHANNEL_LOG, SWITCH_LOG_INFO, "%s END_PRESENCE_PROBE_SQL\n\n", profile->name);
|
|
|
|
}
|
2007-03-31 19:01:33 +00:00
|
|
|
|
2007-05-03 01:55:25 +00:00
|
|
|
sofia_glue_release_profile(profile);
|
2007-03-31 19:01:33 +00:00
|
|
|
switch_safe_free(sql);
|
|
|
|
}
|
2007-12-18 01:12:50 +00:00
|
|
|
|
2008-06-08 01:34:17 +00:00
|
|
|
switch_safe_free(probe_user);
|
2007-03-31 19:01:33 +00:00
|
|
|
}
|
2008-12-15 21:13:03 +00:00
|
|
|
goto done;
|
2008-12-10 20:54:24 +00:00
|
|
|
default:
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
if (!mod_sofia_globals.profile_hash)
|
|
|
|
goto done;
|
|
|
|
|
|
|
|
switch_mutex_lock(mod_sofia_globals.hash_mutex);
|
|
|
|
for (hi = switch_hash_first(NULL, mod_sofia_globals.profile_hash); hi; hi = switch_hash_next(hi)) {
|
|
|
|
switch_hash_this(hi, &var, NULL, &val);
|
|
|
|
profile = (sofia_profile_t *) val;
|
|
|
|
|
|
|
|
if (strcmp((char *)var, profile->name)) {
|
|
|
|
if (mod_sofia_globals.debug_presence > 0) {
|
|
|
|
switch_log_printf(SWITCH_CHANNEL_LOG, SWITCH_LOG_WARNING, "%s is an alias, skipping\n", (char *) var);
|
|
|
|
}
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (profile->pres_type != PRES_TYPE_FULL) {
|
|
|
|
if (mod_sofia_globals.debug_presence > 0) {
|
|
|
|
switch_log_printf(SWITCH_CHANNEL_LOG, SWITCH_LOG_WARNING, "%s is passive, skipping\n", (char *) var);
|
|
|
|
}
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2008-12-11 20:20:20 +00:00
|
|
|
if ((sql = switch_mprintf(
|
2008-11-22 01:34:19 +00:00
|
|
|
|
|
|
|
"select sip_subscriptions.proto,sip_subscriptions.sip_user,sip_subscriptions.sip_host,"
|
|
|
|
"sip_subscriptions.sub_to_user,sip_subscriptions.sub_to_host,sip_subscriptions.event,"
|
|
|
|
"sip_subscriptions.contact,sip_subscriptions.call_id,sip_subscriptions.full_from,"
|
|
|
|
"sip_subscriptions.full_via,sip_subscriptions.expires,sip_subscriptions.user_agent,"
|
|
|
|
"sip_subscriptions.accept,sip_subscriptions.profile_name"
|
2008-12-11 20:20:20 +00:00
|
|
|
",'%q','%q','%q',sip_presence.status,sip_presence.rpid "
|
2008-11-22 01:34:19 +00:00
|
|
|
"from sip_subscriptions "
|
|
|
|
"left join sip_presence on "
|
2008-11-22 02:51:00 +00:00
|
|
|
"(sip_subscriptions.sub_to_user=sip_presence.sip_user and sip_subscriptions.sub_to_host=sip_presence.sip_host and "
|
2008-11-22 01:34:19 +00:00
|
|
|
"sip_subscriptions.profile_name=sip_presence.profile_name) "
|
|
|
|
"where (event='%q' or event='%q') and sub_to_user='%q' "
|
2008-12-10 20:54:24 +00:00
|
|
|
"and (sub_to_host='%q' or presence_hosts like '%%%q%%') "
|
|
|
|
"and (sip_subscriptions.profile_name = '%q' or sip_subscriptions.presence_hosts != sip_subscriptions.sub_to_host)",
|
2008-12-11 20:20:20 +00:00
|
|
|
switch_str_nil(status), switch_str_nil(rpid), host, event_type, alt_event_type, euser, host, host, profile->name))) {
|
2007-03-31 19:01:33 +00:00
|
|
|
|
2007-12-18 01:12:50 +00:00
|
|
|
struct presence_helper helper = { 0 };
|
2007-12-15 00:39:53 +00:00
|
|
|
helper.profile = profile;
|
2008-05-27 04:54:52 +00:00
|
|
|
helper.event = event;
|
2007-12-18 01:12:50 +00:00
|
|
|
SWITCH_STANDARD_STREAM(helper.stream);
|
2008-03-14 03:46:54 +00:00
|
|
|
switch_assert(helper.stream.data);
|
2008-12-10 20:54:24 +00:00
|
|
|
|
|
|
|
if (mod_sofia_globals.debug_presence > 0) {
|
2008-12-11 20:20:20 +00:00
|
|
|
switch_log_printf(SWITCH_CHANNEL_LOG, SWITCH_LOG_INFO, "%s START_PRESENCE_SQL (%s)\n",
|
|
|
|
event->event_id == SWITCH_EVENT_PRESENCE_IN ? "IN" : "OUT", profile->name);
|
2008-12-10 20:54:24 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
if (mod_sofia_globals.debug_presence > 1) {
|
|
|
|
char *buf;
|
|
|
|
switch_event_serialize(event, &buf, SWITCH_FALSE);
|
|
|
|
switch_assert(buf);
|
|
|
|
switch_log_printf(SWITCH_CHANNEL_LOG, SWITCH_LOG_ERROR, "DUMP PRESENCE SQL:\n%s\nEVENT DUMP:\n%s\n", sql, buf);
|
|
|
|
free(buf);
|
|
|
|
}
|
|
|
|
|
2008-05-27 04:54:52 +00:00
|
|
|
sofia_glue_execute_sql_callback(profile, SWITCH_FALSE,
|
|
|
|
NULL, sql, sofia_presence_sub_callback, &helper);
|
2008-12-11 20:20:20 +00:00
|
|
|
|
2008-12-10 20:54:24 +00:00
|
|
|
if (mod_sofia_globals.debug_presence > 0) {
|
2008-12-11 20:20:20 +00:00
|
|
|
switch_log_printf(SWITCH_CHANNEL_LOG, SWITCH_LOG_INFO, "%s END_PRESENCE_SQL (%s)\n",
|
|
|
|
event->event_id == SWITCH_EVENT_PRESENCE_IN ? "IN" : "OUT", profile->name);
|
2008-12-10 20:54:24 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
switch_safe_free(sql);
|
2008-05-27 04:54:52 +00:00
|
|
|
|
|
|
|
if (!switch_strlen_zero((char *) helper.stream.data)) {
|
2008-12-10 20:54:24 +00:00
|
|
|
char *this_sql = (char *) helper.stream.data;
|
2008-04-11 17:07:06 +00:00
|
|
|
char *next = NULL;
|
2008-04-11 19:41:24 +00:00
|
|
|
char *last = NULL;
|
2008-05-27 04:54:52 +00:00
|
|
|
|
2008-04-11 17:07:06 +00:00
|
|
|
do {
|
2008-12-10 20:54:24 +00:00
|
|
|
if ((next = strchr(this_sql, ';'))) {
|
2008-04-11 17:07:06 +00:00
|
|
|
*next++ = '\0';
|
2008-05-27 04:54:52 +00:00
|
|
|
while (*next == '\n' || *next == ' ' || *next == '\r') {
|
2008-04-11 17:07:06 +00:00
|
|
|
*next++ = '\0';
|
|
|
|
}
|
|
|
|
}
|
2008-05-27 04:54:52 +00:00
|
|
|
|
2008-12-10 20:54:24 +00:00
|
|
|
if (!switch_strlen_zero(this_sql) && (!last || strcmp(last, this_sql))) {
|
|
|
|
sofia_glue_execute_sql(profile, &this_sql, SWITCH_FALSE);
|
|
|
|
last = this_sql;
|
2008-04-11 17:07:06 +00:00
|
|
|
}
|
2008-12-10 20:54:24 +00:00
|
|
|
this_sql = next;
|
|
|
|
} while (this_sql);
|
2007-12-18 01:12:50 +00:00
|
|
|
}
|
2008-04-11 17:07:06 +00:00
|
|
|
switch_safe_free(helper.stream.data);
|
2008-03-14 03:46:54 +00:00
|
|
|
helper.stream.data = NULL;
|
2007-03-31 19:01:33 +00:00
|
|
|
}
|
|
|
|
}
|
2007-04-01 01:16:16 +00:00
|
|
|
switch_mutex_unlock(mod_sofia_globals.hash_mutex);
|
2007-03-31 19:01:33 +00:00
|
|
|
|
2008-05-27 04:54:52 +00:00
|
|
|
done:
|
2007-03-31 19:01:33 +00:00
|
|
|
switch_safe_free(sql);
|
|
|
|
switch_safe_free(user);
|
|
|
|
}
|
|
|
|
|
2008-03-14 13:05:28 +00:00
|
|
|
static int EVENT_THREAD_RUNNING = 0;
|
|
|
|
static int EVENT_THREAD_STARTED = 0;
|
2008-03-14 03:46:54 +00:00
|
|
|
|
|
|
|
void *SWITCH_THREAD_FUNC sofia_presence_event_thread_run(switch_thread_t *thread, void *obj)
|
|
|
|
{
|
|
|
|
void *pop;
|
2008-03-14 13:05:28 +00:00
|
|
|
int done = 0;
|
2008-03-14 03:46:54 +00:00
|
|
|
|
|
|
|
switch_mutex_lock(mod_sofia_globals.mutex);
|
2008-03-14 13:05:28 +00:00
|
|
|
if (!EVENT_THREAD_RUNNING) {
|
|
|
|
EVENT_THREAD_RUNNING++;
|
|
|
|
mod_sofia_globals.threads++;
|
|
|
|
} else {
|
|
|
|
done = 1;
|
|
|
|
}
|
2008-03-14 03:46:54 +00:00
|
|
|
switch_mutex_unlock(mod_sofia_globals.mutex);
|
|
|
|
|
2008-03-14 13:05:28 +00:00
|
|
|
if (done) {
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
switch_log_printf(SWITCH_CHANNEL_LOG, SWITCH_LOG_CONSOLE, "Event Thread Started\n");
|
2008-05-27 04:54:52 +00:00
|
|
|
|
2008-03-14 03:46:54 +00:00
|
|
|
while (mod_sofia_globals.running == 1) {
|
|
|
|
int count = 0;
|
2008-12-10 20:54:24 +00:00
|
|
|
|
2008-03-14 03:46:54 +00:00
|
|
|
if (switch_queue_trypop(mod_sofia_globals.presence_queue, &pop) == SWITCH_STATUS_SUCCESS) {
|
|
|
|
switch_event_t *event = (switch_event_t *) pop;
|
|
|
|
|
|
|
|
if (!pop) {
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
actual_sofia_presence_event_handler(event);
|
|
|
|
switch_event_destroy(&event);
|
|
|
|
count++;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (switch_queue_trypop(mod_sofia_globals.mwi_queue, &pop) == SWITCH_STATUS_SUCCESS) {
|
|
|
|
switch_event_t *event = (switch_event_t *) pop;
|
|
|
|
|
|
|
|
if (!pop) {
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
actual_sofia_presence_mwi_event_handler(event);
|
|
|
|
switch_event_destroy(&event);
|
|
|
|
count++;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!count) {
|
|
|
|
switch_yield(100000);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
while (switch_queue_trypop(mod_sofia_globals.presence_queue, &pop) == SWITCH_STATUS_SUCCESS && pop) {
|
|
|
|
switch_event_t *event = (switch_event_t *) pop;
|
|
|
|
switch_event_destroy(&event);
|
|
|
|
}
|
2008-05-27 04:54:52 +00:00
|
|
|
|
2008-03-14 03:46:54 +00:00
|
|
|
while (switch_queue_trypop(mod_sofia_globals.mwi_queue, &pop) == SWITCH_STATUS_SUCCESS && pop) {
|
|
|
|
switch_event_t *event = (switch_event_t *) pop;
|
|
|
|
switch_event_destroy(&event);
|
|
|
|
}
|
|
|
|
|
|
|
|
switch_log_printf(SWITCH_CHANNEL_LOG, SWITCH_LOG_CONSOLE, "Event Thread Ended\n");
|
|
|
|
|
|
|
|
switch_mutex_lock(mod_sofia_globals.mutex);
|
|
|
|
mod_sofia_globals.threads--;
|
2008-03-14 13:05:28 +00:00
|
|
|
EVENT_THREAD_RUNNING = EVENT_THREAD_STARTED = 0;
|
2008-03-14 03:46:54 +00:00
|
|
|
switch_mutex_unlock(mod_sofia_globals.mutex);
|
2008-05-27 04:54:52 +00:00
|
|
|
|
2008-03-14 03:46:54 +00:00
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
void sofia_presence_event_thread_start(void)
|
|
|
|
{
|
|
|
|
switch_thread_t *thread;
|
|
|
|
switch_threadattr_t *thd_attr = NULL;
|
2008-03-14 13:05:28 +00:00
|
|
|
int done = 0;
|
|
|
|
|
|
|
|
switch_mutex_lock(mod_sofia_globals.mutex);
|
|
|
|
if (!EVENT_THREAD_STARTED) {
|
|
|
|
EVENT_THREAD_STARTED++;
|
|
|
|
} else {
|
|
|
|
done = 1;
|
|
|
|
}
|
|
|
|
switch_mutex_unlock(mod_sofia_globals.mutex);
|
|
|
|
|
|
|
|
if (done) {
|
|
|
|
return;
|
|
|
|
}
|
2008-03-14 03:46:54 +00:00
|
|
|
|
|
|
|
switch_threadattr_create(&thd_attr, mod_sofia_globals.pool);
|
|
|
|
switch_threadattr_detach_set(thd_attr, 1);
|
|
|
|
switch_threadattr_stacksize_set(thd_attr, SWITCH_THREAD_STACKSIZE);
|
|
|
|
switch_threadattr_priority_increase(thd_attr);
|
|
|
|
switch_thread_create(&thread, thd_attr, sofia_presence_event_thread_run, NULL, mod_sofia_globals.pool);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
void sofia_presence_event_handler(switch_event_t *event)
|
|
|
|
{
|
|
|
|
switch_event_t *cloned_event;
|
|
|
|
|
|
|
|
switch_event_dup(&cloned_event, event);
|
|
|
|
switch_assert(cloned_event);
|
|
|
|
switch_queue_push(mod_sofia_globals.presence_queue, cloned_event);
|
2008-03-14 13:05:28 +00:00
|
|
|
|
|
|
|
if (!EVENT_THREAD_STARTED) {
|
|
|
|
sofia_presence_event_thread_start();
|
|
|
|
}
|
2008-03-14 03:46:54 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void sofia_presence_mwi_event_handler(switch_event_t *event)
|
|
|
|
{
|
|
|
|
switch_event_t *cloned_event;
|
|
|
|
|
|
|
|
switch_event_dup(&cloned_event, event);
|
|
|
|
switch_assert(cloned_event);
|
|
|
|
switch_queue_push(mod_sofia_globals.mwi_queue, cloned_event);
|
2008-03-14 13:05:28 +00:00
|
|
|
|
|
|
|
if (!EVENT_THREAD_STARTED) {
|
|
|
|
sofia_presence_event_thread_start();
|
|
|
|
}
|
2008-03-14 03:46:54 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2007-04-02 19:54:25 +00:00
|
|
|
static int sofia_presence_sub_reg_callback(void *pArg, int argc, char **argv, char **columnNames)
|
2007-03-31 19:01:33 +00:00
|
|
|
{
|
|
|
|
sofia_profile_t *profile = (sofia_profile_t *) pArg;
|
|
|
|
char *user = argv[1];
|
|
|
|
char *host = argv[2];
|
|
|
|
switch_event_t *event;
|
2007-04-02 19:54:25 +00:00
|
|
|
char *event_name = argv[5];
|
|
|
|
|
|
|
|
if (!strcasecmp(event_name, "message-summary")) {
|
2007-04-02 20:20:46 +00:00
|
|
|
if (switch_event_create(&event, SWITCH_EVENT_MESSAGE_QUERY) == SWITCH_STATUS_SUCCESS) {
|
|
|
|
switch_event_add_header(event, SWITCH_STACK_BOTTOM, "Message-Account", "sip:%s@%s", user, host);
|
2008-08-16 02:19:43 +00:00
|
|
|
switch_event_add_header_string(event, SWITCH_STACK_BOTTOM, "VM-Sofia-Profile", profile->name);
|
2007-04-02 19:54:25 +00:00
|
|
|
switch_event_fire(&event);
|
|
|
|
}
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2007-12-15 00:39:53 +00:00
|
|
|
if (switch_event_create(&event, SWITCH_EVENT_PRESENCE_PROBE) == SWITCH_STATUS_SUCCESS) {
|
2008-08-16 02:19:43 +00:00
|
|
|
switch_event_add_header_string(event, SWITCH_STACK_BOTTOM, "proto", SOFIA_CHAT_PROTO);
|
|
|
|
switch_event_add_header_string(event, SWITCH_STACK_BOTTOM, "login", profile->url);
|
2007-03-31 19:01:33 +00:00
|
|
|
switch_event_add_header(event, SWITCH_STACK_BOTTOM, "from", "%s@%s", user, host);
|
2008-08-16 02:19:43 +00:00
|
|
|
switch_event_add_header_string(event, SWITCH_STACK_BOTTOM, "event_type", "presence");
|
|
|
|
switch_event_add_header_string(event, SWITCH_STACK_BOTTOM, "event_subtype", "probe");
|
|
|
|
switch_event_add_header_string(event, SWITCH_STACK_BOTTOM, "proto-specific-event-name", event_name);
|
2007-03-31 19:01:33 +00:00
|
|
|
switch_event_fire(&event);
|
|
|
|
}
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2007-04-02 19:54:25 +00:00
|
|
|
static int sofia_presence_resub_callback(void *pArg, int argc, char **argv, char **columnNames)
|
2007-03-31 19:01:33 +00:00
|
|
|
{
|
|
|
|
sofia_profile_t *profile = (sofia_profile_t *) pArg;
|
|
|
|
char *user = argv[0];
|
|
|
|
char *host = argv[1];
|
|
|
|
char *status = argv[2];
|
|
|
|
char *rpid = argv[3];
|
|
|
|
char *proto = argv[4];
|
2008-11-22 01:34:19 +00:00
|
|
|
|
2008-06-02 16:40:44 +00:00
|
|
|
char *to_user = NULL;
|
2007-12-18 01:12:50 +00:00
|
|
|
char *uuid = NULL;
|
|
|
|
char *state = NULL;
|
|
|
|
char *direction = NULL;
|
2007-03-31 19:01:33 +00:00
|
|
|
switch_event_t *event;
|
2008-06-02 16:40:44 +00:00
|
|
|
char to_buf[128] = "";
|
2007-03-31 19:01:33 +00:00
|
|
|
|
2007-12-18 01:12:50 +00:00
|
|
|
if (argc > 5) {
|
|
|
|
uuid = switch_str_nil(argv[5]);
|
|
|
|
state = switch_str_nil(argv[6]);
|
|
|
|
direction = switch_str_nil(argv[7]);
|
2008-06-02 16:40:44 +00:00
|
|
|
if (argc > 8) {
|
|
|
|
switch_set_string(to_buf, argv[8]);
|
|
|
|
switch_url_decode(to_buf);
|
|
|
|
to_user = to_buf;
|
|
|
|
}
|
2008-11-22 01:34:19 +00:00
|
|
|
if (argc > 10 && !switch_strlen_zero(argv[9]) && !switch_strlen_zero(argv[10])) {
|
|
|
|
status = argv[9];
|
|
|
|
rpid = argv[10];
|
|
|
|
}
|
2007-12-18 01:12:50 +00:00
|
|
|
}
|
|
|
|
|
2007-03-31 19:01:33 +00:00
|
|
|
if (switch_strlen_zero(proto)) {
|
|
|
|
proto = NULL;
|
|
|
|
}
|
|
|
|
|
2008-12-10 20:54:24 +00:00
|
|
|
if (mod_sofia_globals.debug_presence > 0) {
|
|
|
|
switch_log_printf(SWITCH_CHANNEL_LOG, SWITCH_LOG_NOTICE, "%s PRESENCE_PROBE %s@%s\n", profile->name, user, host);
|
|
|
|
}
|
|
|
|
|
2007-03-31 19:01:33 +00:00
|
|
|
if (switch_event_create(&event, SWITCH_EVENT_PRESENCE_IN) == SWITCH_STATUS_SUCCESS) {
|
2008-08-16 02:19:43 +00:00
|
|
|
switch_event_add_header_string(event, SWITCH_STACK_BOTTOM, "proto", proto ? proto : SOFIA_CHAT_PROTO);
|
|
|
|
switch_event_add_header_string(event, SWITCH_STACK_BOTTOM, "login", profile->url);
|
2007-03-31 19:01:33 +00:00
|
|
|
switch_event_add_header(event, SWITCH_STACK_BOTTOM, "from", "%s@%s", user, host);
|
2008-08-16 02:19:43 +00:00
|
|
|
switch_event_add_header_string(event, SWITCH_STACK_BOTTOM, "status", status);
|
|
|
|
switch_event_add_header_string(event, SWITCH_STACK_BOTTOM, "rpid", rpid);
|
|
|
|
switch_event_add_header_string(event, SWITCH_STACK_BOTTOM, "event_type", "presence");
|
|
|
|
switch_event_add_header_string(event, SWITCH_STACK_BOTTOM, "alt_event_type", "dialog");
|
2007-12-18 01:12:50 +00:00
|
|
|
switch_event_add_header(event, SWITCH_STACK_BOTTOM, "event_count", "%d", 0);
|
|
|
|
|
2008-06-02 16:40:44 +00:00
|
|
|
if (!switch_strlen_zero(to_user)) {
|
2008-08-16 02:19:43 +00:00
|
|
|
switch_event_add_header_string(event, SWITCH_STACK_BOTTOM, "to-user", to_user);
|
2008-06-02 16:40:44 +00:00
|
|
|
}
|
2008-12-11 20:20:20 +00:00
|
|
|
|
2007-12-18 01:12:50 +00:00
|
|
|
if (switch_strlen_zero(state)) {
|
2008-08-16 02:19:43 +00:00
|
|
|
switch_event_add_header_string(event, SWITCH_STACK_BOTTOM, "unique-id", SOFIA_CHAT_PROTO);
|
|
|
|
switch_event_add_header_string(event, SWITCH_STACK_BOTTOM, "channel-state", "CS_HANGUP");
|
|
|
|
switch_event_add_header_string(event, SWITCH_STACK_BOTTOM, "answer-state", "resubscribe");
|
2007-12-18 01:12:50 +00:00
|
|
|
} else {
|
2008-08-16 02:19:43 +00:00
|
|
|
switch_event_add_header_string(event, SWITCH_STACK_BOTTOM, "channel-state", "CS_ROUTING");
|
|
|
|
switch_event_add_header_string(event, SWITCH_STACK_BOTTOM, "unique-id", uuid);
|
|
|
|
switch_event_add_header_string(event, SWITCH_STACK_BOTTOM, "answer-state", state);
|
|
|
|
switch_event_add_header_string(event, SWITCH_STACK_BOTTOM, "astate", state);
|
2009-02-23 16:31:59 +00:00
|
|
|
switch_event_add_header_string(event, SWITCH_STACK_BOTTOM, "presence-call-direction", direction);
|
2007-12-18 01:12:50 +00:00
|
|
|
}
|
2008-05-27 04:54:52 +00:00
|
|
|
|
2007-03-31 19:01:33 +00:00
|
|
|
switch_event_fire(&event);
|
|
|
|
}
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2007-10-24 23:20:47 +00:00
|
|
|
static char *translate_rpid(char *in)
|
|
|
|
{
|
|
|
|
char *r = in;
|
2008-05-27 04:54:52 +00:00
|
|
|
|
2007-10-24 23:20:47 +00:00
|
|
|
if (in && (strstr(in, "null") || strstr(in, "NULL"))) {
|
|
|
|
in = NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!in || !strcasecmp(in, "unknown")) {
|
|
|
|
r = "online";
|
|
|
|
goto end;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!strcasecmp(in, "busy")) {
|
|
|
|
r = in;
|
|
|
|
goto end;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!strcasecmp(in, "unavailable")) {
|
|
|
|
r = "away";
|
|
|
|
goto end;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!strcasecmp(in, "idle")) {
|
|
|
|
r = "busy";
|
|
|
|
}
|
|
|
|
|
2008-05-27 04:54:52 +00:00
|
|
|
end:
|
2007-10-24 23:20:47 +00:00
|
|
|
return r;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2008-06-02 15:42:59 +00:00
|
|
|
static char *gen_pidf(char *user_agent, char *id, char *url, char *open, char *rpid, char *prpid, char *status, const char **ct)
|
2008-05-28 20:58:57 +00:00
|
|
|
{
|
|
|
|
if (switch_stristr("polycom", user_agent)) {
|
|
|
|
*ct = "application/xpidf+xml";
|
2008-10-21 20:01:52 +00:00
|
|
|
|
|
|
|
/* of course!, lets make a big deal over dashes. Now the stupidity is complete. */
|
|
|
|
|
|
|
|
if (!strcmp(prpid, "on-the-phone")) {
|
|
|
|
prpid = "onthephone";
|
|
|
|
}
|
|
|
|
|
2008-05-28 20:58:57 +00:00
|
|
|
return switch_mprintf(
|
|
|
|
"<?xml version=\"1.0\"?>\n"
|
|
|
|
"<!DOCTYPE presence PUBLIC \"-//IETF//DTD RFCxxxx XPIDF 1.0//EN\" \"xpidf.dtd\">\n"
|
|
|
|
"<presence>\n"
|
2008-06-02 15:42:59 +00:00
|
|
|
" <status>\n"
|
|
|
|
" <note>%s</note>\n"
|
|
|
|
" </status>\n"
|
2008-05-28 20:58:57 +00:00
|
|
|
" <presentity uri=\"%s;method=SUBSCRIBE\" />\n"
|
|
|
|
" <atom id=\"%s\">\n"
|
|
|
|
" <address uri=\"%s;user=ip\" priority=\"0.800000\">\n"
|
|
|
|
" <status status=\"%s\" />\n"
|
|
|
|
" <msnsubstatus substatus=\"%s\" />\n"
|
|
|
|
" </address>\n"
|
|
|
|
" </atom>\n"
|
2008-06-02 15:42:59 +00:00
|
|
|
"</presence>\n", status, id, id, url, open, prpid
|
2008-05-28 20:58:57 +00:00
|
|
|
);
|
|
|
|
} else {
|
|
|
|
*ct = "application/pidf+xml";
|
|
|
|
return switch_mprintf(
|
2008-06-02 15:42:59 +00:00
|
|
|
"<?xml version=\"1.0\" encoding=\"ISO-8859-1\"?> \n"
|
|
|
|
"<presence xmlns='urn:ietf:params:xml:ns:pidf' \n"
|
|
|
|
"xmlns:dm='urn:ietf:params:xml:ns:pidf:data-model' \n"
|
|
|
|
"xmlns:rpid='urn:ietf:params:xml:ns:pidf:rpid' \n"
|
|
|
|
"xmlns:c='urn:ietf:params:xml:ns:pidf:cipid' entity='%s'>\n"
|
|
|
|
|
2008-05-28 20:58:57 +00:00
|
|
|
" <tuple id='t6a5ed77e'>\n"
|
|
|
|
" <status>\r\n"
|
|
|
|
" <basic>%s</basic>\n"
|
|
|
|
" </status>\n"
|
|
|
|
" </tuple>\n"
|
|
|
|
" <dm:person id='p06360c4a'>\n"
|
|
|
|
" <rpid:activities>\r\n"
|
|
|
|
" <rpid:%s/>\n"
|
2008-06-02 15:42:59 +00:00
|
|
|
" </rpid:activities>\n"
|
2008-12-07 21:19:37 +00:00
|
|
|
" <dm:note>%s</dm:note>\n"
|
2008-05-28 20:58:57 +00:00
|
|
|
" </dm:person>\n"
|
2008-11-22 01:34:19 +00:00
|
|
|
"</presence>", id, open, prpid, status);
|
2008-05-28 20:58:57 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2007-04-02 19:54:25 +00:00
|
|
|
static int sofia_presence_sub_callback(void *pArg, int argc, char **argv, char **columnNames)
|
2007-03-31 19:01:33 +00:00
|
|
|
{
|
2007-12-15 00:39:53 +00:00
|
|
|
struct presence_helper *helper = (struct presence_helper *) pArg;
|
2007-12-18 01:12:50 +00:00
|
|
|
char *pl = NULL;
|
2008-06-02 15:42:59 +00:00
|
|
|
char *clean_id = NULL, *id = NULL;
|
2007-10-24 23:20:47 +00:00
|
|
|
char *proto = argv[0];
|
|
|
|
char *user = argv[1];
|
|
|
|
char *host = argv[2];
|
|
|
|
char *sub_to_user = argv[3];
|
|
|
|
char *event = argv[5];
|
|
|
|
char *call_id = argv[7];
|
2007-12-29 16:07:03 +00:00
|
|
|
char *expires = argv[10];
|
2008-05-28 20:58:57 +00:00
|
|
|
char *user_agent = argv[11];
|
2008-09-18 00:01:03 +00:00
|
|
|
char *profile_name = argv[13];
|
2008-12-11 20:20:20 +00:00
|
|
|
uint32_t in = 0;
|
|
|
|
char *status = argv[14];
|
|
|
|
char *rpid = argv[15];
|
|
|
|
char *sub_to_host = argv[16];
|
2008-11-22 01:34:19 +00:00
|
|
|
|
2007-03-31 19:01:33 +00:00
|
|
|
nua_handle_t *nh;
|
2007-12-18 01:12:50 +00:00
|
|
|
char *to = NULL;
|
2007-03-31 19:01:33 +00:00
|
|
|
char *open;
|
2007-10-24 23:20:47 +00:00
|
|
|
char *prpid;
|
2008-02-14 17:51:16 +00:00
|
|
|
const char *ct = "no/idea";
|
2009-01-25 21:23:07 +00:00
|
|
|
time_t exptime = switch_epoch_time_now(NULL) + 3600;
|
2008-02-14 17:28:58 +00:00
|
|
|
int is_dialog = 0;
|
2008-09-18 00:01:03 +00:00
|
|
|
sofia_profile_t *ext_profile = NULL, *profile = helper->profile;
|
2008-12-11 20:20:20 +00:00
|
|
|
char sstr[128] = "";
|
|
|
|
int kill_handle = 0;
|
|
|
|
char expires_str[10] = "";
|
2008-09-18 00:01:03 +00:00
|
|
|
|
2008-12-11 20:20:20 +00:00
|
|
|
if (argc > 18 && !switch_strlen_zero(argv[17]) && !switch_strlen_zero(argv[18])) {
|
|
|
|
status = argv[17];
|
|
|
|
rpid = argv[18];
|
2008-11-22 02:51:00 +00:00
|
|
|
}
|
|
|
|
|
2008-12-11 20:20:20 +00:00
|
|
|
in = helper->event && helper->event->event_id == SWITCH_EVENT_PRESENCE_IN;
|
|
|
|
|
|
|
|
|
2008-11-25 20:54:58 +00:00
|
|
|
if (switch_strlen_zero(rpid)) {
|
|
|
|
rpid = "unknown";
|
2008-11-22 02:51:00 +00:00
|
|
|
}
|
|
|
|
|
2008-11-25 20:54:58 +00:00
|
|
|
if (switch_strlen_zero(status)) {
|
|
|
|
if (!strcasecmp(rpid, "busy")) {
|
|
|
|
status = "Busy";
|
|
|
|
} else if (!strcasecmp(rpid, "unavailable")) {
|
|
|
|
status = "Idle";
|
|
|
|
} else if (!strcasecmp(rpid, "away")) {
|
|
|
|
status = "Idle";
|
|
|
|
} else {
|
|
|
|
status = "Available";
|
|
|
|
}
|
|
|
|
}
|
2008-09-18 00:01:03 +00:00
|
|
|
|
|
|
|
if (profile_name && strcasecmp(profile_name, helper->profile->name)) {
|
|
|
|
if ((ext_profile = sofia_glue_find_profile(profile_name))) {
|
|
|
|
profile = ext_profile;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2008-12-11 20:20:20 +00:00
|
|
|
if (!(nh = nua_handle_by_call_id(profile->nua, call_id))) {
|
|
|
|
goto end;
|
|
|
|
}
|
|
|
|
|
2008-05-27 04:54:52 +00:00
|
|
|
if (expires) {
|
|
|
|
long tmp = atol(expires);
|
|
|
|
if (tmp > 0) {
|
2009-01-25 21:23:07 +00:00
|
|
|
exptime = tmp - switch_epoch_time_now(NULL) - SUB_OVERLAP;
|
2008-12-11 20:20:20 +00:00
|
|
|
} else {
|
|
|
|
exptime = tmp;
|
2007-12-29 16:07:03 +00:00
|
|
|
}
|
|
|
|
}
|
2008-12-11 20:20:20 +00:00
|
|
|
|
2007-03-31 19:01:33 +00:00
|
|
|
if (!rpid) {
|
|
|
|
rpid = "unknown";
|
|
|
|
}
|
|
|
|
|
2007-10-24 23:20:47 +00:00
|
|
|
prpid = translate_rpid(rpid);
|
|
|
|
|
2007-12-18 01:12:50 +00:00
|
|
|
if (!strcasecmp(proto, SOFIA_CHAT_PROTO)) {
|
|
|
|
clean_id = switch_mprintf("sip:%s@%s", sub_to_user, sub_to_host);
|
|
|
|
} else {
|
|
|
|
clean_id = switch_mprintf("sip:%s+%s@%s", proto, sub_to_user, sub_to_host);
|
|
|
|
}
|
2007-12-15 18:26:10 +00:00
|
|
|
|
2008-12-10 20:54:24 +00:00
|
|
|
if (mod_sofia_globals.debug_presence > 0) {
|
|
|
|
switch_log_printf(SWITCH_CHANNEL_LOG, SWITCH_LOG_NOTICE,
|
|
|
|
"SEND PRESENCE\nTo: \t%s@%s\nFrom: \t%s@%s\nCall-ID: \t%s\nProfile:\t%s [%s]\n\n",
|
2008-12-11 20:20:20 +00:00
|
|
|
user, host, sub_to_user, sub_to_host, call_id, profile_name, helper->profile->name);
|
2008-12-10 20:54:24 +00:00
|
|
|
}
|
|
|
|
|
2007-03-31 19:01:33 +00:00
|
|
|
if (!strcasecmp(sub_to_host, host)) {
|
|
|
|
/* same host */
|
|
|
|
id = switch_mprintf("sip:%s+%s@%s", proto, sub_to_user, sub_to_host);
|
|
|
|
} else if (strcasecmp(proto, SOFIA_CHAT_PROTO)) {
|
|
|
|
/*encapsulate */
|
|
|
|
id = switch_mprintf("sip:%s+%s+%s@%s", proto, sub_to_user, sub_to_host, host);
|
|
|
|
} else {
|
|
|
|
id = switch_mprintf("sip:%s@%s", sub_to_user, sub_to_host);
|
|
|
|
}
|
|
|
|
|
|
|
|
to = switch_mprintf("sip:%s@%s", user, host);
|
2008-02-14 17:28:58 +00:00
|
|
|
|
|
|
|
is_dialog = !strcmp(event, "dialog");
|
2008-05-27 04:54:52 +00:00
|
|
|
|
2008-02-14 17:28:58 +00:00
|
|
|
if (helper->event) {
|
2007-12-15 00:39:53 +00:00
|
|
|
switch_stream_handle_t stream = { 0 };
|
2009-02-23 16:31:59 +00:00
|
|
|
const char *direction = switch_str_nil(switch_event_get_header(helper->event, "presence-call-direction"));
|
2007-12-15 00:39:53 +00:00
|
|
|
const char *uuid = switch_str_nil(switch_event_get_header(helper->event, "unique-id"));
|
|
|
|
const char *state = switch_str_nil(switch_event_get_header(helper->event, "channel-state"));
|
2007-12-15 19:33:36 +00:00
|
|
|
const char *event_status = switch_str_nil(switch_event_get_header(helper->event, "status"));
|
2007-12-18 03:12:51 +00:00
|
|
|
const char *astate = switch_str_nil(switch_event_get_header(helper->event, "astate"));
|
2007-12-29 18:30:48 +00:00
|
|
|
const char *answer_state = switch_str_nil(switch_event_get_header(helper->event, "answer-state"));
|
2007-12-18 01:12:50 +00:00
|
|
|
const char *dft_state;
|
2008-02-14 17:28:58 +00:00
|
|
|
const char *from_id = switch_str_nil(switch_event_get_header(helper->event, "Other-Leg-Caller-ID-Number"));
|
|
|
|
const char *to_user = switch_str_nil(switch_event_get_header(helper->event, "variable_sip_to_user"));
|
|
|
|
const char *from_user = switch_str_nil(switch_event_get_header(helper->event, "variable_sip_from_user"));
|
|
|
|
const char *clean_to_user = NULL;
|
|
|
|
const char *clean_from_user = NULL;
|
2008-06-02 16:40:44 +00:00
|
|
|
const char *p_to_user = switch_str_nil(switch_event_get_header(helper->event, "to-user"));
|
2008-06-02 20:06:07 +00:00
|
|
|
#if 0
|
|
|
|
char *buf;
|
|
|
|
switch_event_serialize(helper->event, &buf, SWITCH_FALSE);
|
|
|
|
switch_assert(buf);
|
|
|
|
switch_log_printf(SWITCH_CHANNEL_LOG, SWITCH_LOG_INFO, "CHANNEL_DATA:\n%s\n", buf);
|
|
|
|
free(buf);
|
|
|
|
#endif
|
|
|
|
|
2008-02-14 17:28:58 +00:00
|
|
|
if (is_dialog) {
|
|
|
|
SWITCH_STANDARD_STREAM(stream);
|
|
|
|
}
|
2007-12-18 03:12:51 +00:00
|
|
|
|
2007-12-18 01:12:50 +00:00
|
|
|
if (!strcasecmp(direction, "outbound")) {
|
|
|
|
direction = "recipient";
|
|
|
|
dft_state = "early";
|
|
|
|
} else {
|
|
|
|
direction = "initiator";
|
|
|
|
dft_state = "confirmed";
|
|
|
|
}
|
2008-05-27 04:54:52 +00:00
|
|
|
|
2007-12-20 02:55:36 +00:00
|
|
|
if (!strcasecmp(state, "cs_execute") && !strstr(event_status, "hold")) {
|
2007-12-18 01:12:50 +00:00
|
|
|
goto end;
|
2008-05-27 04:54:52 +00:00
|
|
|
}
|
|
|
|
|
2007-12-29 17:50:38 +00:00
|
|
|
if (!strcasecmp(event_status, "Registered")) {
|
|
|
|
answer_state = "resubscribe";
|
|
|
|
}
|
|
|
|
|
2008-02-14 17:28:58 +00:00
|
|
|
if (is_dialog) {
|
2008-05-27 04:54:52 +00:00
|
|
|
stream.write_function(&stream,
|
2008-02-14 17:28:58 +00:00
|
|
|
"<?xml version=\"1.0\"?>\n"
|
|
|
|
"<dialog-info xmlns=\"urn:ietf:params:xml:ns:dialog-info\" "
|
2008-05-27 04:54:52 +00:00
|
|
|
"version=\"%s\" state=\"%s\" entity=\"%s\">\n",
|
2008-02-14 17:28:58 +00:00
|
|
|
switch_str_nil(switch_event_get_header(helper->event, "event_count")),
|
|
|
|
!strcasecmp(answer_state, "resubscribe") ? "partial" : "full", clean_id);
|
|
|
|
}
|
2008-05-27 04:54:52 +00:00
|
|
|
|
2007-12-29 17:50:38 +00:00
|
|
|
if (strcasecmp(answer_state, "resubscribe")) {
|
2007-12-20 02:55:36 +00:00
|
|
|
|
2008-01-01 01:03:33 +00:00
|
|
|
if (!strcasecmp(state, "cs_hangup")) {
|
|
|
|
astate = "terminated";
|
2008-05-27 04:54:52 +00:00
|
|
|
} else if (switch_strlen_zero(astate)) {
|
2008-01-01 01:03:33 +00:00
|
|
|
astate = switch_str_nil(switch_event_get_header(helper->event, "answer-state"));
|
2008-05-27 04:54:52 +00:00
|
|
|
if (switch_strlen_zero(astate)) {
|
2008-06-02 15:42:59 +00:00
|
|
|
if (is_dialog) {
|
|
|
|
astate = dft_state;
|
|
|
|
} else {
|
|
|
|
astate = "terminated";
|
|
|
|
}
|
2008-01-01 01:03:33 +00:00
|
|
|
}
|
2008-05-27 04:54:52 +00:00
|
|
|
}
|
|
|
|
|
2008-01-01 01:03:33 +00:00
|
|
|
if (!strcasecmp(event_status, "hold")) {
|
2007-12-18 03:12:51 +00:00
|
|
|
astate = "early";
|
2008-01-01 01:03:33 +00:00
|
|
|
}
|
2008-05-27 04:54:52 +00:00
|
|
|
|
2008-01-01 01:03:33 +00:00
|
|
|
if (!strcasecmp(astate, "answered")) {
|
2007-12-18 03:12:51 +00:00
|
|
|
astate = "confirmed";
|
|
|
|
}
|
2008-05-27 04:54:52 +00:00
|
|
|
|
2008-01-01 01:03:33 +00:00
|
|
|
if (!strcasecmp(astate, "ringing")) {
|
|
|
|
if (!strcasecmp(direction, "recipient")) {
|
|
|
|
astate = "early";
|
|
|
|
} else {
|
|
|
|
astate = "confirmed";
|
|
|
|
}
|
|
|
|
}
|
2008-02-14 17:28:58 +00:00
|
|
|
if (is_dialog) {
|
|
|
|
stream.write_function(&stream, "<dialog id=\"%s\" direction=\"%s\">\n", uuid, direction);
|
|
|
|
stream.write_function(&stream, "<state>%s</state>\n", astate);
|
|
|
|
}
|
|
|
|
if (!strcasecmp(astate, "early") || !strcasecmp(astate, "confirmed")) {
|
2008-05-27 04:54:52 +00:00
|
|
|
|
2008-02-14 17:28:58 +00:00
|
|
|
clean_to_user = switch_mprintf("%s", sub_to_user ? sub_to_user : to_user);
|
|
|
|
clean_from_user = switch_mprintf("%s", from_id ? from_id : from_user);
|
|
|
|
|
|
|
|
if (is_dialog) {
|
2008-02-15 23:18:18 +00:00
|
|
|
if (!switch_strlen_zero(clean_to_user) && !switch_strlen_zero(clean_from_user)) {
|
2008-02-14 17:28:58 +00:00
|
|
|
stream.write_function(&stream, "<local>\n<identity display=\"%s\">sip:%s@%s</identity>\n", clean_to_user, clean_to_user, host);
|
|
|
|
stream.write_function(&stream, "<target uri=\"sip:%s@%s\">\n", clean_to_user, host);
|
2008-05-27 04:54:52 +00:00
|
|
|
stream.write_function(&stream, "<param pname=\"+sip.rendering\" pvalue=\"%s\"/>\n",
|
|
|
|
!strcasecmp(event_status, "hold") ? "no" : "yes");
|
|
|
|
stream.write_function(&stream, "</target>\n</local>\n");
|
|
|
|
stream.write_function(&stream, "<remote>\n<identity display=\"%s\">sip:%s@%s</identity>\n", clean_from_user, clean_from_user,
|
|
|
|
host);
|
2008-02-14 17:28:58 +00:00
|
|
|
stream.write_function(&stream, "<target uri=\"sip:**%s@%s\"/>\n", clean_to_user, host);
|
|
|
|
stream.write_function(&stream, "</remote>\n");
|
|
|
|
} else if (!strcasecmp(proto, "park")) {
|
2008-05-27 04:54:52 +00:00
|
|
|
stream.write_function(&stream, "<local>\n<identity display=\"parking\">sip:parking@%s;fifo=%s</identity>\n",
|
2008-02-14 17:28:58 +00:00
|
|
|
host, !switch_strlen_zero(clean_to_user) ? clean_to_user : "unknown");
|
|
|
|
stream.write_function(&stream, "<target uri=\"sip:parking@%s\">\n", host);
|
2008-05-27 04:54:52 +00:00
|
|
|
stream.write_function(&stream, "<param pname=\"+sip.rendering\" pvalue=\"no\"/>\n</target>\n</local>\n");
|
2008-02-14 17:28:58 +00:00
|
|
|
stream.write_function(&stream, "<remote>\n<identity display=\"parking\">sip:%s</identity>\n", uuid);
|
|
|
|
stream.write_function(&stream, "<target uri=\"sip:park+%s\"/>\n", uuid);
|
|
|
|
stream.write_function(&stream, "</remote>\n");
|
|
|
|
} else if (!strcasecmp(proto, "conf")) {
|
2008-05-27 04:54:52 +00:00
|
|
|
stream.write_function(&stream, "<local>\n<identity display=\"conference\">sip:conference@%s;conference=%s</identity>\n",
|
2008-02-14 17:28:58 +00:00
|
|
|
host, !switch_strlen_zero(clean_to_user) ? clean_to_user : "unknown");
|
|
|
|
stream.write_function(&stream, "<target uri=\"sip:conference@%s\">\n", host);
|
2008-05-27 04:54:52 +00:00
|
|
|
stream.write_function(&stream, "<param pname=\"+sip.rendering\" pvalue=\"yes\"/>\n</target>\n</local>\n");
|
2008-02-14 17:28:58 +00:00
|
|
|
stream.write_function(&stream, "<remote>\n<identity display=\"conference\">sip:%s@%s</identity>\n", uuid, host);
|
|
|
|
stream.write_function(&stream, "<target uri=\"sip:conf+%s@%s\"/>\n", uuid, host);
|
|
|
|
stream.write_function(&stream, "</remote>\n");
|
|
|
|
}
|
2007-12-29 17:50:38 +00:00
|
|
|
}
|
|
|
|
}
|
2008-02-14 17:28:58 +00:00
|
|
|
if (is_dialog) {
|
2008-05-27 04:54:52 +00:00
|
|
|
stream.write_function(&stream, "</dialog>\n");
|
2008-02-14 17:28:58 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (is_dialog) {
|
|
|
|
stream.write_function(&stream, "</dialog-info>\n");
|
|
|
|
pl = stream.data;
|
|
|
|
ct = "application/dialog-info+xml";
|
|
|
|
}
|
2008-05-27 04:54:52 +00:00
|
|
|
|
2008-04-11 19:41:24 +00:00
|
|
|
if (!switch_strlen_zero(astate) && !switch_strlen_zero(uuid) && helper && helper->stream.data && strcmp(helper->last_uuid, uuid)) {
|
|
|
|
helper->stream.write_function(&helper->stream, "update sip_dialogs set state='%s' where uuid='%s';", astate, uuid);
|
2008-05-27 04:54:52 +00:00
|
|
|
|
2008-04-11 19:41:24 +00:00
|
|
|
switch_copy_string(helper->last_uuid, uuid, sizeof(helper->last_uuid));
|
2007-12-29 17:50:38 +00:00
|
|
|
}
|
2007-12-29 17:52:22 +00:00
|
|
|
|
2008-02-14 17:28:58 +00:00
|
|
|
if (!is_dialog) {
|
2008-06-02 15:42:59 +00:00
|
|
|
char status_line[256] = "";
|
2008-11-22 01:34:19 +00:00
|
|
|
|
|
|
|
switch_set_string(status_line, status);
|
|
|
|
|
2008-02-14 17:28:58 +00:00
|
|
|
if (in) {
|
|
|
|
if (!strcmp(astate, "early")) {
|
2008-06-02 15:42:59 +00:00
|
|
|
switch_snprintf(status_line, sizeof(status_line), "Ring %s", switch_str_nil(from_id));
|
2008-02-14 19:45:15 +00:00
|
|
|
rpid = "on-the-phone";
|
2008-02-14 17:28:58 +00:00
|
|
|
} else if (!strcmp(astate, "confirmed")) {
|
|
|
|
char *dest = switch_event_get_header(helper->event, "Caller-Destination-Number");
|
|
|
|
if (switch_strlen_zero(from_id) && !switch_strlen_zero(dest)) {
|
|
|
|
from_id = dest;
|
2008-05-27 04:54:52 +00:00
|
|
|
}
|
|
|
|
|
2008-06-02 16:40:44 +00:00
|
|
|
if (switch_strlen_zero(from_id)) {
|
|
|
|
from_id = p_to_user;
|
|
|
|
}
|
|
|
|
|
2008-02-14 19:45:15 +00:00
|
|
|
if (switch_strlen_zero(from_id)) {
|
2008-11-22 01:34:19 +00:00
|
|
|
switch_snprintf(status_line, sizeof(status_line), "On The Phone %s", status);
|
2008-02-14 19:45:15 +00:00
|
|
|
} else {
|
2008-06-02 15:42:59 +00:00
|
|
|
switch_snprintf(status_line, sizeof(status_line), "Talk %s", switch_str_nil(from_id));
|
2008-02-14 19:45:15 +00:00
|
|
|
}
|
2008-06-02 15:42:59 +00:00
|
|
|
rpid = "on-the-phone";
|
2008-02-14 17:28:58 +00:00
|
|
|
}
|
2007-12-29 18:30:48 +00:00
|
|
|
|
2008-02-14 17:28:58 +00:00
|
|
|
open = "open";
|
|
|
|
} else {
|
|
|
|
open = "closed";
|
|
|
|
}
|
2008-06-02 15:42:59 +00:00
|
|
|
|
2008-02-14 17:28:58 +00:00
|
|
|
prpid = translate_rpid(rpid);
|
2008-06-02 15:42:59 +00:00
|
|
|
pl = gen_pidf(user_agent, clean_id, profile->url, open, rpid, prpid, status_line, &ct);
|
2007-12-18 01:12:50 +00:00
|
|
|
}
|
2008-12-11 20:20:20 +00:00
|
|
|
|
2007-12-15 00:39:53 +00:00
|
|
|
} else {
|
2008-02-14 17:28:58 +00:00
|
|
|
if (in) {
|
|
|
|
open = "open";
|
|
|
|
} else {
|
|
|
|
open = "closed";
|
|
|
|
}
|
2008-05-28 20:58:57 +00:00
|
|
|
prpid = translate_rpid(rpid);
|
2008-06-02 15:42:59 +00:00
|
|
|
pl = gen_pidf(user_agent, clean_id, profile->url, open, rpid, prpid, status, &ct);
|
2007-12-15 00:39:53 +00:00
|
|
|
}
|
2008-06-02 20:06:07 +00:00
|
|
|
|
2008-05-07 20:06:46 +00:00
|
|
|
nua_handle_bind(nh, &mod_sofia_globals.keep_private);
|
2008-12-11 20:20:20 +00:00
|
|
|
|
|
|
|
if (helper->event && helper->event->event_id == SWITCH_EVENT_PRESENCE_OUT) {
|
|
|
|
switch_set_string(sstr, "terminated;reason=noresource");
|
|
|
|
switch_set_string(expires_str, "0");
|
|
|
|
kill_handle = 1;
|
|
|
|
} else if (exptime > 0) {
|
2008-12-11 20:52:04 +00:00
|
|
|
switch_snprintf(sstr, sizeof(sstr), "active;expires=%u", (unsigned)exptime);
|
2008-12-11 20:20:20 +00:00
|
|
|
} else {
|
2008-12-11 20:52:04 +00:00
|
|
|
unsigned delta = (unsigned) (exptime * -1);
|
|
|
|
switch_snprintf(sstr, sizeof(sstr), "active;expires=%u", delta);
|
|
|
|
switch_snprintf(expires_str, sizeof(expires_str), "%u", delta);
|
2008-12-18 20:33:21 +00:00
|
|
|
if (nh && nh->nh_ds && nh->nh_ds->ds_usage) {
|
|
|
|
nua_dialog_usage_set_refresh_range(nh->nh_ds->ds_usage, delta, delta);
|
|
|
|
}
|
2008-12-11 20:20:20 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
nua_notify(nh,
|
|
|
|
TAG_IF(*expires_str, SIPTAG_EXPIRES_STR(expires_str)),
|
|
|
|
SIPTAG_SUBSCRIPTION_STATE_STR(sstr),
|
2008-12-10 16:52:48 +00:00
|
|
|
SIPTAG_EVENT_STR(event), SIPTAG_CONTENT_TYPE_STR(ct), SIPTAG_PAYLOAD_STR(pl), TAG_END());
|
2008-06-02 20:06:07 +00:00
|
|
|
|
2008-05-27 04:54:52 +00:00
|
|
|
end:
|
2008-09-18 00:01:03 +00:00
|
|
|
|
|
|
|
if (ext_profile) {
|
|
|
|
sofia_glue_release_profile(ext_profile);
|
|
|
|
}
|
2007-12-18 01:12:50 +00:00
|
|
|
|
2007-03-31 19:01:33 +00:00
|
|
|
switch_safe_free(id);
|
2007-12-15 18:26:10 +00:00
|
|
|
switch_safe_free(clean_id);
|
2007-03-31 19:01:33 +00:00
|
|
|
switch_safe_free(pl);
|
|
|
|
switch_safe_free(to);
|
|
|
|
|
2008-12-11 20:20:20 +00:00
|
|
|
if (nh && kill_handle) {
|
|
|
|
nua_handle_destroy(nh);
|
|
|
|
}
|
|
|
|
|
2007-03-31 19:01:33 +00:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2007-04-02 19:54:25 +00:00
|
|
|
static int sofia_presence_mwi_callback(void *pArg, int argc, char **argv, char **columnNames)
|
|
|
|
{
|
|
|
|
char *sub_to_user = argv[3];
|
2008-09-18 00:01:03 +00:00
|
|
|
char *sub_to_host = argv[15];
|
2007-04-02 19:54:25 +00:00
|
|
|
char *event = argv[5];
|
|
|
|
char *call_id = argv[7];
|
|
|
|
char *expires = argv[10];
|
2008-09-18 00:01:03 +00:00
|
|
|
char *profile_name = argv[13];
|
|
|
|
char *body = argv[14];
|
2007-10-28 16:07:23 +00:00
|
|
|
char *id = NULL;
|
2007-04-02 19:54:25 +00:00
|
|
|
nua_handle_t *nh;
|
2007-04-02 20:33:06 +00:00
|
|
|
int expire_sec = atoi(expires);
|
2008-05-08 16:09:49 +00:00
|
|
|
struct mwi_helper *h = (struct mwi_helper *) pArg;
|
2008-09-18 00:01:03 +00:00
|
|
|
sofia_profile_t *ext_profile = NULL, *profile = h->profile;
|
2008-05-27 04:54:52 +00:00
|
|
|
|
2008-09-18 00:01:03 +00:00
|
|
|
if (profile_name && strcasecmp(profile_name, h->profile->name)) {
|
|
|
|
if ((ext_profile = sofia_glue_find_profile(profile_name))) {
|
|
|
|
profile = ext_profile;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2008-05-08 16:09:49 +00:00
|
|
|
if (!(nh = nua_handle_by_call_id(h->profile->nua, call_id))) {
|
2008-11-05 15:55:53 +00:00
|
|
|
if (profile->debug) {
|
|
|
|
switch_log_printf(SWITCH_CHANNEL_LOG, SWITCH_LOG_DEBUG, "Cannot find handle for %s\n", call_id);
|
|
|
|
}
|
2008-09-18 00:01:03 +00:00
|
|
|
goto end;
|
2007-10-24 23:20:47 +00:00
|
|
|
}
|
2007-04-02 19:54:25 +00:00
|
|
|
|
|
|
|
id = switch_mprintf("sip:%s@%s", sub_to_user, sub_to_host);
|
2009-01-25 21:23:07 +00:00
|
|
|
expire_sec = (int) (expire_sec - switch_epoch_time_now(NULL));
|
2007-04-03 01:24:24 +00:00
|
|
|
if (expire_sec < 0) {
|
|
|
|
expire_sec = 3600;
|
|
|
|
}
|
2008-12-10 16:52:48 +00:00
|
|
|
|
2008-05-07 20:06:46 +00:00
|
|
|
nua_handle_bind(nh, &mod_sofia_globals.keep_private);
|
2008-12-10 16:52:48 +00:00
|
|
|
nua_notify(nh, SIPTAG_SUBSCRIPTION_STATE_STR("active"),
|
2007-04-02 19:54:25 +00:00
|
|
|
SIPTAG_EVENT_STR(event), SIPTAG_CONTENT_TYPE_STR("application/simple-message-summary"), SIPTAG_PAYLOAD_STR(body), TAG_END());
|
2008-05-27 04:54:52 +00:00
|
|
|
|
2007-04-02 19:54:25 +00:00
|
|
|
switch_safe_free(id);
|
2007-04-29 01:16:49 +00:00
|
|
|
|
2008-05-08 16:09:49 +00:00
|
|
|
h->total++;
|
2008-09-18 00:01:03 +00:00
|
|
|
|
|
|
|
end:
|
|
|
|
|
|
|
|
if (ext_profile) {
|
|
|
|
sofia_glue_release_profile(ext_profile);
|
|
|
|
}
|
|
|
|
|
2007-12-13 23:30:16 +00:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
static int sofia_presence_mwi_callback2(void *pArg, int argc, char **argv, char **columnNames)
|
|
|
|
{
|
2007-12-14 17:39:45 +00:00
|
|
|
char *sub_to_user = argv[0];
|
|
|
|
char *sub_to_host = argv[1];
|
2007-12-13 23:30:16 +00:00
|
|
|
char *event = "message-summary";
|
2008-05-21 21:49:27 +00:00
|
|
|
char *contact, *o_contact = argv[2];
|
2008-09-18 00:01:03 +00:00
|
|
|
char *profile_name = argv[3];
|
|
|
|
char *body = argv[4];
|
2007-12-13 23:30:16 +00:00
|
|
|
char *id = NULL;
|
|
|
|
nua_handle_t *nh;
|
2008-05-08 16:09:49 +00:00
|
|
|
struct mwi_helper *h = (struct mwi_helper *) pArg;
|
2008-09-18 00:01:03 +00:00
|
|
|
sofia_profile_t *ext_profile = NULL, *profile = h->profile;
|
2008-10-30 03:12:52 +00:00
|
|
|
char *route = NULL, *route_uri = NULL;
|
|
|
|
char *p;
|
2008-05-27 04:54:52 +00:00
|
|
|
|
2008-09-18 00:01:03 +00:00
|
|
|
if (profile_name && strcasecmp(profile_name, h->profile->name)) {
|
|
|
|
if ((ext_profile = sofia_glue_find_profile(profile_name))) {
|
|
|
|
profile = ext_profile;
|
|
|
|
}
|
|
|
|
}
|
2008-05-27 04:54:52 +00:00
|
|
|
|
2007-12-13 23:30:16 +00:00
|
|
|
id = switch_mprintf("sip:%s@%s", sub_to_user, sub_to_host);
|
2007-12-14 00:00:57 +00:00
|
|
|
|
2008-05-21 21:49:27 +00:00
|
|
|
contact = sofia_glue_get_url_from_contact(o_contact, 1);
|
2008-11-03 20:07:45 +00:00
|
|
|
if ((route = strstr(contact, ";fs_path=")) && (route = strdup(route + 9))) {
|
2008-10-30 03:12:52 +00:00
|
|
|
|
|
|
|
for (p = route; p && *p ; p++) {
|
|
|
|
if (*p == '>' || *p == ';') {
|
|
|
|
*p = '\0';
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
switch_url_decode(route);
|
2008-10-30 16:40:54 +00:00
|
|
|
route_uri = route;
|
2008-10-30 03:12:52 +00:00
|
|
|
if ((p = strchr(route_uri, ','))) {
|
|
|
|
while (*(p-1) == ' ') {
|
|
|
|
p--;
|
|
|
|
}
|
|
|
|
if (*p) {
|
|
|
|
*p = '\0';
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2008-11-03 20:07:45 +00:00
|
|
|
if (!route_uri && strstr(contact, ";fs_nat")) {
|
2008-10-30 03:12:52 +00:00
|
|
|
route_uri = contact;
|
|
|
|
}
|
2008-05-27 04:54:52 +00:00
|
|
|
|
2008-11-03 20:07:45 +00:00
|
|
|
if ((p = strstr(contact, ";fs_"))) {
|
|
|
|
*p = '\0';
|
|
|
|
}
|
|
|
|
|
|
|
|
if (route_uri) {
|
|
|
|
while (route_uri && *route_uri && (*route_uri == '<' || *route_uri == ' ')) {
|
|
|
|
route_uri++;
|
|
|
|
}
|
|
|
|
if ((p = strchr(route_uri, '>'))) {
|
|
|
|
*p++ = '\0';
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2008-09-18 00:01:03 +00:00
|
|
|
nh = nua_handle(profile->nua, NULL, NUTAG_URL(contact), SIPTAG_FROM_STR(id), SIPTAG_TO_STR(id), SIPTAG_CONTACT_STR(h->profile->url), TAG_END());
|
2008-05-27 04:54:52 +00:00
|
|
|
|
2007-12-13 23:30:16 +00:00
|
|
|
nua_notify(nh,
|
|
|
|
NUTAG_NEWSUB(1),
|
2008-10-30 03:12:52 +00:00
|
|
|
TAG_IF(route_uri, NUTAG_PROXY(route_uri)),
|
2007-12-13 23:30:16 +00:00
|
|
|
SIPTAG_EVENT_STR(event), SIPTAG_CONTENT_TYPE_STR("application/simple-message-summary"), SIPTAG_PAYLOAD_STR(body), TAG_END());
|
2008-05-27 04:54:52 +00:00
|
|
|
|
2008-05-21 21:49:27 +00:00
|
|
|
switch_safe_free(contact);
|
2007-12-13 23:30:16 +00:00
|
|
|
switch_safe_free(id);
|
2008-10-30 03:12:52 +00:00
|
|
|
switch_safe_free(route);
|
2008-09-18 00:01:03 +00:00
|
|
|
if (ext_profile) {
|
|
|
|
sofia_glue_release_profile(ext_profile);
|
|
|
|
}
|
|
|
|
|
2007-04-02 19:54:25 +00:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2007-03-31 19:01:33 +00:00
|
|
|
void sofia_presence_handle_sip_i_subscribe(int status,
|
2008-05-27 04:54:52 +00:00
|
|
|
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[])
|
2007-03-31 19:01:33 +00:00
|
|
|
{
|
|
|
|
if (sip) {
|
2008-12-11 20:20:20 +00:00
|
|
|
long exp_abs, exp_delta;
|
|
|
|
char exp_delta_str[30] = "";
|
2007-03-31 19:01:33 +00:00
|
|
|
sip_to_t const *to = sip->sip_to;
|
|
|
|
sip_from_t const *from = sip->sip_from;
|
|
|
|
sip_contact_t const *contact = sip->sip_contact;
|
2007-11-20 03:26:40 +00:00
|
|
|
const char *from_user = NULL, *from_host = NULL;
|
|
|
|
const char *to_user = NULL, *to_host = NULL;
|
|
|
|
char *my_to_user = NULL;
|
2007-03-31 19:01:33 +00:00
|
|
|
char *sql, *event = NULL;
|
|
|
|
char *proto = "sip";
|
|
|
|
char *d_user = NULL;
|
|
|
|
char *contact_str = "";
|
2007-10-24 23:20:47 +00:00
|
|
|
const char *call_id = NULL;
|
2007-03-31 19:01:33 +00:00
|
|
|
char *to_str = NULL;
|
|
|
|
char *full_from = NULL;
|
|
|
|
char *full_via = NULL;
|
2007-12-14 02:09:22 +00:00
|
|
|
char *full_agent = NULL;
|
2007-03-31 19:01:33 +00:00
|
|
|
char *sstr;
|
|
|
|
const char *display = "\"user\"";
|
|
|
|
switch_event_t *sevent;
|
2007-10-24 23:20:47 +00:00
|
|
|
int sub_state;
|
2007-12-15 00:39:53 +00:00
|
|
|
int sent_reply = 0;
|
2008-05-21 21:49:27 +00:00
|
|
|
su_addrinfo_t *my_addrinfo = msg_addrinfo(nua_current_request(nua));
|
|
|
|
int network_port = 0;
|
|
|
|
char network_ip[80];
|
|
|
|
const char *contact_host, *contact_user;
|
|
|
|
char *port;
|
|
|
|
char new_port[25] = "";
|
|
|
|
char *is_nat = NULL;
|
2008-07-03 18:50:15 +00:00
|
|
|
const char *ipv6;
|
2008-05-21 21:49:27 +00:00
|
|
|
|
|
|
|
if (!(contact && sip->sip_contact->m_url)) {
|
|
|
|
nua_respond(nh, 481, "INVALID SUBSCRIPTION", TAG_END());
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2008-07-03 18:50:15 +00:00
|
|
|
get_addr(network_ip, sizeof(network_ip), my_addrinfo->ai_addr, my_addrinfo->ai_addrlen);
|
2008-05-21 21:49:27 +00:00
|
|
|
network_port = ntohs(((struct sockaddr_in *) msg_addrinfo(nua_current_request(nua))->ai_addr)->sin_port);
|
2007-10-24 23:20:47 +00:00
|
|
|
|
2008-05-27 04:54:52 +00:00
|
|
|
tl_gets(tags, NUTAG_SUBSTATE_REF(sub_state), TAG_END());
|
2007-10-24 23:20:47 +00:00
|
|
|
|
2007-12-15 00:39:53 +00:00
|
|
|
event = sip_header_as_string(profile->home, (void *) sip->sip_event);
|
|
|
|
|
2008-05-21 21:49:27 +00:00
|
|
|
port = (char *) contact->m_url->url_port;
|
|
|
|
contact_host = sip->sip_contact->m_url->url_host;
|
|
|
|
contact_user = sip->sip_contact->m_url->url_user;
|
2008-05-27 04:54:52 +00:00
|
|
|
|
2008-05-21 21:49:27 +00:00
|
|
|
display = contact->m_display;
|
2007-03-31 19:01:33 +00:00
|
|
|
|
2008-05-21 21:49:27 +00:00
|
|
|
if (switch_strlen_zero(display)) {
|
|
|
|
if (from) {
|
|
|
|
display = from->a_display;
|
|
|
|
if (switch_strlen_zero(display)) {
|
|
|
|
display = "\"user\"";
|
2007-03-31 19:01:33 +00:00
|
|
|
}
|
|
|
|
}
|
2008-05-21 21:49:27 +00:00
|
|
|
} else {
|
|
|
|
display = "\"user\"";
|
|
|
|
}
|
2008-05-27 04:54:52 +00:00
|
|
|
|
2009-02-09 17:56:38 +00:00
|
|
|
if (sofia_test_pflag(profile, PFLAG_AGGRESSIVE_NAT_DETECTION)) {
|
2008-05-21 21:49:27 +00:00
|
|
|
if (sip && sip->sip_via) {
|
2008-05-22 01:24:13 +00:00
|
|
|
const char *v_port = sip->sip_via->v_port;
|
|
|
|
const char *v_host = sip->sip_via->v_host;
|
2008-05-27 04:54:52 +00:00
|
|
|
|
2008-05-22 01:24:13 +00:00
|
|
|
if (v_host && sip->sip_via->v_received) {
|
2008-05-21 21:49:27 +00:00
|
|
|
is_nat = "via received";
|
2008-05-22 01:24:13 +00:00
|
|
|
} else if (v_host && strcmp(network_ip, v_host)) {
|
2008-05-21 21:49:27 +00:00
|
|
|
is_nat = "via host";
|
2008-05-22 01:24:13 +00:00
|
|
|
} else if (v_port && atoi(v_port) != network_port) {
|
2008-05-21 21:49:27 +00:00
|
|
|
is_nat = "via port";
|
|
|
|
}
|
2007-03-31 19:01:33 +00:00
|
|
|
}
|
2008-05-21 21:49:27 +00:00
|
|
|
}
|
2008-05-27 04:54:52 +00:00
|
|
|
|
2008-05-21 21:49:27 +00:00
|
|
|
if (!is_nat && profile->nat_acl_count) {
|
|
|
|
uint32_t x = 0;
|
|
|
|
int ok = 1;
|
|
|
|
char *last_acl = NULL;
|
2008-05-27 04:54:52 +00:00
|
|
|
|
2008-05-21 21:49:27 +00:00
|
|
|
if (!switch_strlen_zero(contact_host)) {
|
2008-05-27 04:54:52 +00:00
|
|
|
for (x = 0; x < profile->nat_acl_count; x++) {
|
2008-05-21 21:49:27 +00:00
|
|
|
last_acl = profile->nat_acl[x];
|
|
|
|
if (!(ok = switch_check_network_list_ip(contact_host, last_acl))) {
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
2008-05-27 04:54:52 +00:00
|
|
|
|
2008-05-21 21:49:27 +00:00
|
|
|
if (ok) {
|
|
|
|
is_nat = last_acl;
|
|
|
|
}
|
2007-03-31 19:01:33 +00:00
|
|
|
}
|
|
|
|
}
|
2008-05-27 04:54:52 +00:00
|
|
|
|
2008-05-21 21:49:27 +00:00
|
|
|
if (is_nat) {
|
|
|
|
contact_host = network_ip;
|
|
|
|
switch_snprintf(new_port, sizeof(new_port), ":%d", network_port);
|
|
|
|
port = NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
if (port) {
|
|
|
|
switch_snprintf(new_port, sizeof(new_port), ":%s", port);
|
|
|
|
}
|
2008-05-27 04:54:52 +00:00
|
|
|
|
2008-07-03 18:50:15 +00:00
|
|
|
ipv6 = strchr(contact_host, ':');
|
2008-05-21 21:49:27 +00:00
|
|
|
if (contact->m_url->url_params) {
|
2008-07-03 18:50:15 +00:00
|
|
|
contact_str = switch_mprintf("%s <sip:%s@%s%s%s%s;%s>%s",
|
|
|
|
display, contact->m_url->url_user,
|
|
|
|
ipv6 ? "[" : "",
|
|
|
|
contact_host,
|
|
|
|
ipv6 ? "]" : "",
|
|
|
|
new_port,
|
|
|
|
contact->m_url->url_params,
|
2009-02-01 00:29:14 +00:00
|
|
|
is_nat ? ";fs_nat" : "");
|
2008-05-21 21:49:27 +00:00
|
|
|
} else {
|
2008-07-03 18:50:15 +00:00
|
|
|
contact_str = switch_mprintf("%s <sip:%s@%s%s%s%s>%s",
|
|
|
|
display,
|
|
|
|
contact->m_url->url_user,
|
|
|
|
ipv6 ? "[" : "",
|
|
|
|
contact_host,
|
|
|
|
ipv6 ? "]" : "",
|
|
|
|
new_port,
|
2009-02-01 00:29:14 +00:00
|
|
|
is_nat ? ";fs_nat" : "");
|
2008-05-21 21:49:27 +00:00
|
|
|
}
|
2008-05-27 04:54:52 +00:00
|
|
|
|
2009-02-01 00:06:46 +00:00
|
|
|
|
|
|
|
|
|
|
|
/* the following could be refactored back to the calling event handler in sofia.c XXX MTK */
|
2009-02-12 14:46:14 +00:00
|
|
|
if (sofia_test_pflag(profile, PFLAG_MANAGE_SHARED_APPEARANCE)) {
|
2009-02-02 07:27:04 +00:00
|
|
|
if (sip->sip_request->rq_url->url_user && !strncmp(sip->sip_request->rq_url->url_user, "sla-agent", sizeof("sla-agent"))) {
|
2009-02-01 00:06:46 +00:00
|
|
|
/* only fire this on <200 to try to avoid resubscribes. probably better ways to do this? */
|
|
|
|
if (status < 200) {
|
|
|
|
sofia_sla_handle_sip_i_subscribe(nua, contact_str, profile, nh, sip, tags);
|
|
|
|
}
|
|
|
|
switch_safe_free(contact_str);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
2007-03-31 19:01:33 +00:00
|
|
|
if (to) {
|
2008-10-11 19:20:11 +00:00
|
|
|
to_str = switch_mprintf("sip:%s@%s", to->a_url->url_user, to->a_url->url_host);
|
2007-03-31 19:01:33 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
if (to) {
|
2007-11-20 03:26:40 +00:00
|
|
|
to_user = to->a_url->url_user;
|
|
|
|
to_host = to->a_url->url_host;
|
2007-03-31 19:01:33 +00:00
|
|
|
}
|
|
|
|
|
2007-10-24 23:20:47 +00:00
|
|
|
if (sip && sip->sip_from) {
|
2007-11-20 03:26:40 +00:00
|
|
|
from_user = sip->sip_from->a_url->url_user;
|
|
|
|
from_host = sip->sip_from->a_url->url_host;
|
2007-10-24 23:20:47 +00:00
|
|
|
} else {
|
|
|
|
from_user = "n/a";
|
|
|
|
from_host = "n/a";
|
|
|
|
}
|
2007-03-31 19:01:33 +00:00
|
|
|
|
2007-12-13 04:01:29 +00:00
|
|
|
if (to_user && (strstr(to_user, "ext+") || strstr(to_user, "user+"))) {
|
|
|
|
char protocol[80];
|
2007-03-31 19:01:33 +00:00
|
|
|
char *p;
|
|
|
|
|
2007-12-13 04:01:29 +00:00
|
|
|
switch_copy_string(protocol, to_user, sizeof(protocol));
|
|
|
|
if ((p = strchr(protocol, '+'))) {
|
2007-03-31 19:01:33 +00:00
|
|
|
*p = '\0';
|
|
|
|
}
|
2008-05-27 04:54:52 +00:00
|
|
|
|
2007-03-31 19:01:33 +00:00
|
|
|
if (switch_event_create(&sevent, SWITCH_EVENT_PRESENCE_IN) == SWITCH_STATUS_SUCCESS) {
|
2008-08-16 02:19:43 +00:00
|
|
|
switch_event_add_header_string(sevent, SWITCH_STACK_BOTTOM, "proto", protocol);
|
|
|
|
switch_event_add_header_string(sevent, SWITCH_STACK_BOTTOM, "login", profile->name);
|
2007-03-31 19:01:33 +00:00
|
|
|
switch_event_add_header(sevent, SWITCH_STACK_BOTTOM, "from", "%s@%s", to_user, to_host);
|
2008-08-16 02:19:43 +00:00
|
|
|
switch_event_add_header_string(sevent, SWITCH_STACK_BOTTOM, "rpid", "active");
|
|
|
|
switch_event_add_header_string(sevent, SWITCH_STACK_BOTTOM, "status", "Click To Call");
|
2007-03-31 19:01:33 +00:00
|
|
|
switch_event_fire(&sevent);
|
|
|
|
}
|
2008-05-27 04:54:52 +00:00
|
|
|
|
2007-10-24 23:20:47 +00:00
|
|
|
} else {
|
|
|
|
if (switch_event_create(&sevent, SWITCH_EVENT_PRESENCE_PROBE) == SWITCH_STATUS_SUCCESS) {
|
2008-08-16 02:19:43 +00:00
|
|
|
switch_event_add_header_string(sevent, SWITCH_STACK_BOTTOM, "proto", SOFIA_CHAT_PROTO);
|
|
|
|
switch_event_add_header_string(sevent, SWITCH_STACK_BOTTOM, "login", profile->name);
|
2007-10-24 23:20:47 +00:00
|
|
|
switch_event_add_header(sevent, SWITCH_STACK_BOTTOM, "from", "%s@%s", from_user, from_host);
|
|
|
|
switch_event_add_header(sevent, SWITCH_STACK_BOTTOM, "to", "%s@%s", to_user, to_host);
|
2008-08-16 02:19:43 +00:00
|
|
|
switch_event_add_header_string(sevent, SWITCH_STACK_BOTTOM, "proto-specific-event-name", event);
|
2007-10-24 23:20:47 +00:00
|
|
|
switch_event_fire(&sevent);
|
|
|
|
}
|
2007-03-31 19:01:33 +00:00
|
|
|
}
|
|
|
|
|
2007-12-13 04:01:29 +00:00
|
|
|
if (to_user && strchr(to_user, '+')) {
|
2007-03-31 19:01:33 +00:00
|
|
|
char *h;
|
|
|
|
if ((proto = (d_user = strdup(to_user)))) {
|
2007-11-20 03:26:40 +00:00
|
|
|
if ((my_to_user = strchr(d_user, '+'))) {
|
|
|
|
*my_to_user++ = '\0';
|
|
|
|
to_user = my_to_user;
|
2007-03-31 19:01:33 +00:00
|
|
|
if ((h = strchr(to_user, '+')) || (h = strchr(to_user, '@'))) {
|
|
|
|
*h++ = '\0';
|
|
|
|
to_host = h;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!(proto && to_user && to_host)) {
|
|
|
|
nua_respond(nh, SIP_404_NOT_FOUND, NUTAG_WITH_THIS(nua), TAG_END());
|
|
|
|
goto end;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2007-11-20 03:26:40 +00:00
|
|
|
call_id = sip->sip_call_id->i_id;
|
2007-03-31 19:01:33 +00:00
|
|
|
full_from = sip_header_as_string(profile->home, (void *) sip->sip_from);
|
|
|
|
full_via = sip_header_as_string(profile->home, (void *) sip->sip_via);
|
2008-12-11 20:20:20 +00:00
|
|
|
|
|
|
|
exp_delta = profile->force_subscription_expires ? profile->force_subscription_expires : (sip->sip_expires ? sip->sip_expires->ex_delta : 3600);
|
|
|
|
|
|
|
|
if (exp_delta) {
|
2009-01-25 21:23:07 +00:00
|
|
|
exp_abs = (long) switch_epoch_time_now(NULL) + exp_delta;
|
2008-12-10 16:52:48 +00:00
|
|
|
} else {
|
2008-12-11 20:20:20 +00:00
|
|
|
exp_abs = 0;
|
2008-12-10 16:52:48 +00:00
|
|
|
sub_state = nua_substate_terminated;
|
|
|
|
}
|
2007-03-31 19:01:33 +00:00
|
|
|
|
2008-12-11 20:20:20 +00:00
|
|
|
switch_snprintf(exp_delta_str, sizeof(exp_delta_str), "%ld", exp_delta);
|
|
|
|
|
2008-03-05 20:31:18 +00:00
|
|
|
if (sofia_test_pflag(profile, PFLAG_MULTIREG)) {
|
|
|
|
sql = switch_mprintf("delete from sip_subscriptions where call_id='%q'", call_id);
|
|
|
|
} else {
|
|
|
|
sql = switch_mprintf("delete from sip_subscriptions where "
|
2008-09-18 00:01:03 +00:00
|
|
|
"proto='%q' and sip_user='%q' and sip_host='%q' and sub_to_user='%q' and sub_to_host='%q' and event='%q' and hostname='%q'",
|
|
|
|
proto, from_user, from_host, to_user, to_host, event, mod_sofia_globals.hostname);
|
2008-03-05 20:31:18 +00:00
|
|
|
}
|
2007-04-04 03:08:17 +00:00
|
|
|
|
2008-03-05 20:31:18 +00:00
|
|
|
switch_mutex_lock(profile->ireg_mutex);
|
2007-12-12 23:21:45 +00:00
|
|
|
switch_assert(sql != NULL);
|
2008-03-05 20:31:18 +00:00
|
|
|
sofia_glue_execute_sql(profile, &sql, SWITCH_TRUE);
|
2007-03-31 19:01:33 +00:00
|
|
|
|
2007-10-24 23:20:47 +00:00
|
|
|
if (sub_state == nua_substate_terminated) {
|
|
|
|
sstr = switch_mprintf("terminated");
|
|
|
|
} else {
|
2008-02-14 17:28:58 +00:00
|
|
|
sip_accept_t *ap = sip->sip_accept;
|
|
|
|
char accept[256] = "";
|
2007-12-14 02:09:22 +00:00
|
|
|
full_agent = sip_header_as_string(profile->home, (void *) sip->sip_user_agent);
|
2008-05-27 04:54:52 +00:00
|
|
|
while (ap) {
|
2008-02-14 17:28:58 +00:00
|
|
|
switch_snprintf(accept + strlen(accept), sizeof(accept) - strlen(accept), "%s%s ", ap->ac_type, ap->ac_next ? "," : "");
|
|
|
|
ap = ap->ac_next;
|
|
|
|
}
|
2008-12-11 20:20:20 +00:00
|
|
|
|
|
|
|
/* negative in exptime means keep bumping up sub time to avoid a snafu where every device has it's own rules about subscriptions
|
|
|
|
that somehow barely resemble the RFC not that I blame them because the RFC MAY be amibiguous and SHOULD be deleted.
|
|
|
|
So to avoid the problem we keep resetting the expiration date of the subscription so it never expires.
|
|
|
|
|
|
|
|
Eybeam completely ignores this option and most other subscription-state: directives from rfc3265 and still expires.
|
|
|
|
Polycom is happy to keep upping the subscription expiry back to the original time on each new notify.
|
|
|
|
The rest ... who knows...?
|
|
|
|
|
|
|
|
*/
|
|
|
|
|
2008-09-18 00:01:03 +00:00
|
|
|
sql = switch_mprintf("insert into sip_subscriptions "
|
|
|
|
"(proto,sip_user,sip_host,sub_to_user,sub_to_host,presence_hosts,event,contact,call_id,full_from,"
|
|
|
|
"full_via,expires,user_agent,accept,profile_name,hostname) "
|
|
|
|
"values ('%q','%q','%q','%q','%q','%q','%q','%q','%q','%q','%q',%ld,'%q','%q','%q','%q')",
|
|
|
|
proto, from_user, from_host, to_user, to_host, profile->presence_hosts ? profile->presence_hosts : to_host,
|
2008-12-11 20:20:20 +00:00
|
|
|
event, contact_str, call_id, full_from, full_via,
|
|
|
|
|
|
|
|
exp_delta * -1,
|
|
|
|
//exp_abs + SUB_OVERLAP,
|
|
|
|
|
|
|
|
full_agent, accept, profile->name,mod_sofia_globals.hostname);
|
2008-09-18 00:01:03 +00:00
|
|
|
|
|
|
|
|
2008-12-11 20:20:20 +00:00
|
|
|
if (mod_sofia_globals.debug_presence > 0) {
|
|
|
|
switch_log_printf(SWITCH_CHANNEL_LOG, SWITCH_LOG_NOTICE, "%s SUBSCRIBE %s@%s %s@%s\n", profile->name, from_user, from_host, to_user, to_host);
|
|
|
|
}
|
|
|
|
|
2007-12-12 23:21:45 +00:00
|
|
|
switch_assert(sql != NULL);
|
2008-03-05 20:31:18 +00:00
|
|
|
sofia_glue_execute_sql(profile, &sql, SWITCH_TRUE);
|
2007-10-24 23:20:47 +00:00
|
|
|
|
2008-12-11 20:20:20 +00:00
|
|
|
sstr = switch_mprintf("active;expires=%ld", exp_delta);
|
2007-10-24 23:20:47 +00:00
|
|
|
}
|
2008-05-27 04:54:52 +00:00
|
|
|
|
2007-12-30 23:25:45 +00:00
|
|
|
switch_mutex_unlock(profile->ireg_mutex);
|
|
|
|
|
2007-12-15 00:39:53 +00:00
|
|
|
if (status < 200) {
|
2008-05-21 21:49:27 +00:00
|
|
|
char *sticky = NULL;
|
2008-11-26 19:52:38 +00:00
|
|
|
char *contactstr = profile->url;
|
2008-11-26 19:30:40 +00:00
|
|
|
|
2008-05-21 21:49:27 +00:00
|
|
|
if (is_nat) {
|
2008-11-26 17:59:14 +00:00
|
|
|
char params[128] = "";
|
|
|
|
if (contact->m_url->url_params) {
|
|
|
|
switch_snprintf(params, sizeof(params), ";%s", contact->m_url->url_params);
|
|
|
|
}
|
2008-07-03 23:54:35 +00:00
|
|
|
ipv6 = strchr(network_ip, ':');
|
2008-11-26 17:59:14 +00:00
|
|
|
sticky = switch_mprintf("sip:%s@%s%s%s:%d%s",
|
2008-07-03 18:50:15 +00:00
|
|
|
contact_user,
|
|
|
|
ipv6 ? "[" : "",
|
|
|
|
network_ip,
|
|
|
|
ipv6 ? "]" : "",
|
2008-11-26 17:59:14 +00:00
|
|
|
network_port,
|
|
|
|
params);
|
2008-05-21 21:49:27 +00:00
|
|
|
}
|
2008-11-26 19:30:40 +00:00
|
|
|
|
|
|
|
if (switch_stristr("port=tcp", contact->m_url->url_params)) {
|
2008-11-26 19:52:38 +00:00
|
|
|
contactstr = profile->tcp_contact;
|
2008-11-26 19:30:40 +00:00
|
|
|
} else if (switch_stristr("port=tls", contact->m_url->url_params)) {
|
2008-11-26 19:52:38 +00:00
|
|
|
contactstr = profile->tls_contact;
|
2008-11-26 19:30:40 +00:00
|
|
|
}
|
|
|
|
|
2008-12-11 20:20:20 +00:00
|
|
|
|
2008-12-18 20:33:21 +00:00
|
|
|
if (nh && nh->nh_ds && nh->nh_ds->ds_usage) {
|
|
|
|
nua_dialog_usage_set_refresh_range(nh->nh_ds->ds_usage, exp_delta + SUB_OVERLAP, exp_delta + SUB_OVERLAP);
|
|
|
|
}
|
2008-05-21 21:49:27 +00:00
|
|
|
|
2008-12-08 17:37:26 +00:00
|
|
|
nua_respond(nh, SIP_202_ACCEPTED, SIPTAG_CONTACT_STR(contact_str), NUTAG_WITH_THIS(nua),
|
|
|
|
SIPTAG_SUBSCRIPTION_STATE_STR(sstr),
|
2008-12-11 20:20:20 +00:00
|
|
|
SIPTAG_EXPIRES_STR(exp_delta_str),
|
2008-11-26 19:30:40 +00:00
|
|
|
TAG_IF(sticky, NUTAG_PROXY(sticky)), TAG_END());
|
2008-05-21 21:49:27 +00:00
|
|
|
|
|
|
|
switch_safe_free(sticky);
|
|
|
|
|
2007-12-15 00:39:53 +00:00
|
|
|
}
|
2008-05-27 04:54:52 +00:00
|
|
|
|
2007-12-15 00:39:53 +00:00
|
|
|
sent_reply++;
|
|
|
|
|
|
|
|
#if 0
|
2008-05-27 04:54:52 +00:00
|
|
|
nua_notify(nh,
|
|
|
|
SIPTAG_SUBSCRIPTION_STATE_STR(sstr), SIPTAG_EVENT_STR(event),
|
2008-12-11 20:20:20 +00:00
|
|
|
SIPTAG_EXPIRES_STR(exp_delta_str),
|
|
|
|
SIPTAG_CONTENT_TYPE_STR("application/notice"),
|
2008-05-27 04:54:52 +00:00
|
|
|
SIPTAG_PAYLOAD_STR("Note: Come to ClueCon http://www.cluecon.com\n\n"), TAG_END());
|
2007-12-15 00:39:53 +00:00
|
|
|
#endif
|
2007-07-02 20:17:10 +00:00
|
|
|
|
2007-03-31 19:01:33 +00:00
|
|
|
switch_safe_free(sstr);
|
|
|
|
|
2008-09-18 00:01:03 +00:00
|
|
|
if ((sql = switch_mprintf(
|
2008-09-18 01:31:56 +00:00
|
|
|
"select proto,sip_user,'%q',sub_to_user,sub_to_host,event,contact,call_id,full_from,"
|
2008-09-18 00:01:03 +00:00
|
|
|
"full_via,expires,user_agent,accept,profile_name"
|
2008-09-18 01:31:56 +00:00
|
|
|
" from sip_subscriptions where sip_user='%q' and (sip_host='%q' or presence_hosts like '%%%q%%')",
|
|
|
|
to_host, to_user, to_host, to_host))) {
|
2008-05-27 04:54:52 +00:00
|
|
|
sofia_glue_execute_sql_callback(profile, SWITCH_FALSE, profile->ireg_mutex, sql, sofia_presence_sub_reg_callback, profile);
|
2008-09-18 01:31:56 +00:00
|
|
|
|
2007-03-31 19:01:33 +00:00
|
|
|
switch_safe_free(sql);
|
|
|
|
}
|
2008-05-27 04:54:52 +00:00
|
|
|
end:
|
2007-03-31 19:01:33 +00:00
|
|
|
|
|
|
|
if (event) {
|
|
|
|
su_free(profile->home, event);
|
|
|
|
}
|
2007-10-24 23:20:47 +00:00
|
|
|
|
2007-03-31 19:01:33 +00:00
|
|
|
if (full_from) {
|
|
|
|
su_free(profile->home, full_from);
|
|
|
|
}
|
|
|
|
if (full_via) {
|
|
|
|
su_free(profile->home, full_via);
|
|
|
|
}
|
2007-12-14 02:09:22 +00:00
|
|
|
if (full_agent) {
|
|
|
|
su_free(profile->home, full_agent);
|
|
|
|
}
|
2007-03-31 19:01:33 +00:00
|
|
|
|
|
|
|
switch_safe_free(d_user);
|
|
|
|
switch_safe_free(to_str);
|
|
|
|
switch_safe_free(contact_str);
|
2007-12-15 00:39:53 +00:00
|
|
|
|
|
|
|
if (!sent_reply) {
|
|
|
|
nua_respond(nh, 481, "INVALID SUBSCRIPTION", TAG_END());
|
|
|
|
}
|
2007-03-31 19:01:33 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2008-11-24 15:52:55 +00:00
|
|
|
sofia_gateway_subscription_t *sofia_find_gateway_subscription(sofia_gateway_t *gateway_ptr, const char *event) {
|
|
|
|
sofia_gateway_subscription_t *gw_sub_ptr;
|
|
|
|
for (gw_sub_ptr = gateway_ptr->subscriptions; gw_sub_ptr; gw_sub_ptr = gw_sub_ptr->next) {
|
|
|
|
if (!strcasecmp(gw_sub_ptr->event, event)) {
|
|
|
|
/* this is the gateway subscription we are interested in */
|
|
|
|
return gw_sub_ptr;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
2007-03-31 19:01:33 +00:00
|
|
|
void sofia_presence_handle_sip_r_subscribe(int status,
|
2008-05-27 04:54:52 +00:00
|
|
|
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[])
|
2007-03-31 19:01:33 +00:00
|
|
|
{
|
2008-11-24 15:52:55 +00:00
|
|
|
sip_event_t const *o = NULL;
|
|
|
|
sofia_gateway_subscription_t *gw_sub_ptr;
|
|
|
|
|
|
|
|
if (!sip) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
tl_gets(tags, SIPTAG_EVENT_REF(o), TAG_END());
|
|
|
|
/* o->o_type: message-summary (for example) */
|
|
|
|
if (!o) {
|
|
|
|
switch_log_printf(SWITCH_CHANNEL_LOG, SWITCH_LOG_ERROR, "Event information not given\n");
|
|
|
|
return;
|
|
|
|
}
|
2007-03-31 19:01:33 +00:00
|
|
|
|
2009-01-30 16:46:37 +00:00
|
|
|
/* the following could possibly be refactored back towards the calling event handler in sofia.c XXX MTK */
|
2009-02-12 14:46:14 +00:00
|
|
|
if (sofia_test_pflag(profile, PFLAG_MANAGE_SHARED_APPEARANCE)) {
|
2009-01-30 16:46:37 +00:00
|
|
|
if (!strcasecmp(o->o_type, "dialog") && msg_params_find(o->o_params, "sla")) {
|
|
|
|
sofia_sla_handle_sip_r_subscribe(nua, profile, nh, sip, tags);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2008-11-24 15:52:55 +00:00
|
|
|
if (!sofia_private || !sofia_private->gateway) {
|
|
|
|
switch_log_printf(SWITCH_CHANNEL_LOG, SWITCH_LOG_ERROR, "Gateway information missing\n");
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Find the subscription if one exists */
|
|
|
|
if (!(gw_sub_ptr = sofia_find_gateway_subscription(sofia_private->gateway, o->o_type))) {
|
|
|
|
switch_log_printf(SWITCH_CHANNEL_LOG, SWITCH_LOG_WARNING, "Could not find gateway subscription. Gateway: %s. Subscription Event: %s\n",
|
|
|
|
sofia_private->gateway->name, o->o_type);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Update the subscription status for the subscription */
|
|
|
|
switch (status) {
|
|
|
|
case 200:
|
|
|
|
/* TODO: in the spec it is possible for the other side to change the original expiry time,
|
|
|
|
* this needs to be researched (eg, what sip header this information will be in) and implemented.
|
|
|
|
* Although, since it seems the sofia stack is pretty much handling the subscription expiration
|
|
|
|
* anyway, then maybe its not even worth bothering.
|
|
|
|
*/
|
|
|
|
switch_log_printf(SWITCH_CHANNEL_LOG, SWITCH_LOG_DEBUG, "got 200 OK response, updated state to SUB_STATE_SUBSCRIBE.\n");
|
|
|
|
gw_sub_ptr->state = SUB_STATE_SUBSCRIBE;
|
|
|
|
break;
|
|
|
|
case 100:
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
switch_log_printf(SWITCH_CHANNEL_LOG, SWITCH_LOG_WARNING, "status (%d) != 200, updated state to SUB_STATE_FAILED.\n", status);
|
|
|
|
gw_sub_ptr->state = SUB_STATE_FAILED;
|
|
|
|
break;
|
|
|
|
}
|
2007-03-31 19:01:33 +00:00
|
|
|
}
|
|
|
|
|
2008-05-27 04:54:52 +00:00
|
|
|
void sofia_presence_handle_sip_i_publish(nua_t *nua, sofia_profile_t *profile, nua_handle_t *nh, sofia_private_t *sofia_private, sip_t const *sip,
|
|
|
|
tagi_t tags[])
|
2007-03-31 19:01:33 +00:00
|
|
|
{
|
|
|
|
if (sip) {
|
|
|
|
sip_from_t const *from = sip->sip_from;
|
|
|
|
char *from_user = NULL;
|
|
|
|
char *from_host = NULL;
|
|
|
|
char *rpid = "unknown";
|
|
|
|
sip_payload_t *payload = sip->sip_payload;
|
|
|
|
char *event_type;
|
|
|
|
|
2009-01-30 16:46:37 +00:00
|
|
|
/* the following could instead be refactored back to the calling event handler in sofia.c XXX MTK */
|
2009-02-12 14:46:14 +00:00
|
|
|
if (sofia_test_pflag(profile, PFLAG_MANAGE_SHARED_APPEARANCE)) {
|
2009-01-30 16:46:37 +00:00
|
|
|
/* also it probably is unsafe to dereference so many things in a row without testing XXX MTK */
|
2009-02-02 07:27:04 +00:00
|
|
|
if (sip->sip_request->rq_url->url_user && !strncmp(sip->sip_request->rq_url->url_user, "sla-agent", sizeof("sla-agent"))) {
|
2009-01-30 16:46:37 +00:00
|
|
|
sofia_sla_handle_sip_i_publish(nua, profile, nh, sip, tags);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2007-03-31 19:01:33 +00:00
|
|
|
if (from) {
|
|
|
|
from_user = (char *) from->a_url->url_user;
|
|
|
|
from_host = (char *) from->a_url->url_host;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (payload) {
|
|
|
|
switch_xml_t xml, note, person, tuple, status, basic, act;
|
|
|
|
switch_event_t *event;
|
|
|
|
uint8_t in = 0;
|
|
|
|
char *sql;
|
2008-11-22 01:34:19 +00:00
|
|
|
char *full_agent = NULL;
|
2008-12-11 20:20:20 +00:00
|
|
|
long exp, exp_delta;
|
2007-03-31 19:01:33 +00:00
|
|
|
|
|
|
|
if ((xml = switch_xml_parse_str(payload->pl_data, strlen(payload->pl_data)))) {
|
|
|
|
char *status_txt = "", *note_txt = "";
|
|
|
|
|
2008-11-22 01:34:19 +00:00
|
|
|
if (sip->sip_user_agent) {
|
|
|
|
full_agent = sip_header_as_string(profile->home, (void *) sip->sip_user_agent);
|
|
|
|
}
|
|
|
|
|
2007-03-31 19:01:33 +00:00
|
|
|
if ((tuple = switch_xml_child(xml, "tuple")) && (status = switch_xml_child(tuple, "status"))
|
|
|
|
&& (basic = switch_xml_child(status, "basic"))) {
|
|
|
|
status_txt = basic->txt;
|
|
|
|
}
|
|
|
|
|
|
|
|
if ((person = switch_xml_child(xml, "dm:person")) && (note = switch_xml_child(person, "dm:note"))) {
|
|
|
|
note_txt = note->txt;
|
|
|
|
}
|
|
|
|
|
2008-11-20 22:47:11 +00:00
|
|
|
if (person && (act = switch_xml_child(person, "rpid:activities")) && act->child && act->child->name) {
|
2007-03-31 19:01:33 +00:00
|
|
|
if ((rpid = strchr(act->child->name, ':'))) {
|
|
|
|
rpid++;
|
|
|
|
} else {
|
|
|
|
rpid = act->child->name;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!strcasecmp(status_txt, "open")) {
|
|
|
|
if (switch_strlen_zero(note_txt)) {
|
|
|
|
note_txt = "Available";
|
|
|
|
}
|
|
|
|
in = 1;
|
|
|
|
} else if (!strcasecmp(status_txt, "closed")) {
|
|
|
|
if (switch_strlen_zero(note_txt)) {
|
|
|
|
note_txt = "Unavailable";
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2008-12-11 20:20:20 +00:00
|
|
|
exp_delta = (sip->sip_expires ? sip->sip_expires->ex_delta : 3600);
|
2009-01-25 21:23:07 +00:00
|
|
|
exp = (long) switch_epoch_time_now(NULL) + exp_delta;
|
2008-11-22 01:34:19 +00:00
|
|
|
|
2007-03-31 19:01:33 +00:00
|
|
|
if ((sql =
|
2008-11-22 01:34:19 +00:00
|
|
|
switch_mprintf("delete from sip_presence where sip_user='%q' and sip_host='%q' "
|
|
|
|
" and profile_name='%q' and hostname='%q'",
|
|
|
|
from_user, from_host, profile->name, mod_sofia_globals.hostname
|
|
|
|
))) {
|
2008-03-05 20:31:18 +00:00
|
|
|
sofia_glue_execute_sql(profile, &sql, SWITCH_TRUE);
|
2007-03-31 19:01:33 +00:00
|
|
|
}
|
2008-11-22 01:34:19 +00:00
|
|
|
|
|
|
|
if ((sql =
|
|
|
|
switch_mprintf("insert into sip_presence (sip_user, sip_host, status, rpid, expires, user_agent, profile_name, hostname) "
|
|
|
|
"values ('%q','%q','%q','%q',%ld,'%q','%q','%q')",
|
|
|
|
from_user, from_host, note_txt, rpid, exp, full_agent, profile->name, mod_sofia_globals.hostname))) {
|
|
|
|
sofia_glue_execute_sql(profile, &sql, SWITCH_TRUE);
|
|
|
|
}
|
|
|
|
|
2007-03-31 19:01:33 +00:00
|
|
|
event_type = sip_header_as_string(profile->home, (void *) sip->sip_event);
|
|
|
|
|
|
|
|
if (in) {
|
|
|
|
if (switch_event_create(&event, SWITCH_EVENT_PRESENCE_IN) == SWITCH_STATUS_SUCCESS) {
|
2008-08-16 02:19:43 +00:00
|
|
|
switch_event_add_header_string(event, SWITCH_STACK_BOTTOM, "proto", SOFIA_CHAT_PROTO);
|
|
|
|
switch_event_add_header_string(event, SWITCH_STACK_BOTTOM, "rpid", rpid);
|
|
|
|
switch_event_add_header_string(event, SWITCH_STACK_BOTTOM, "login", profile->url);
|
2008-11-22 01:34:19 +00:00
|
|
|
switch_event_add_header_string(event, SWITCH_STACK_BOTTOM, "user-agent", full_agent);
|
2007-03-31 19:01:33 +00:00
|
|
|
switch_event_add_header(event, SWITCH_STACK_BOTTOM, "from", "%s@%s", from_user, from_host);
|
2008-08-16 02:19:43 +00:00
|
|
|
switch_event_add_header_string(event, SWITCH_STACK_BOTTOM, "status", note_txt);
|
|
|
|
switch_event_add_header_string(event, SWITCH_STACK_BOTTOM, "event_type", event_type);
|
2007-03-31 19:01:33 +00:00
|
|
|
switch_event_fire(&event);
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
if (switch_event_create(&event, SWITCH_EVENT_PRESENCE_OUT) == SWITCH_STATUS_SUCCESS) {
|
2008-08-16 02:19:43 +00:00
|
|
|
switch_event_add_header_string(event, SWITCH_STACK_BOTTOM, "proto", SOFIA_CHAT_PROTO);
|
|
|
|
switch_event_add_header_string(event, SWITCH_STACK_BOTTOM, "rpid", rpid);
|
|
|
|
switch_event_add_header_string(event, SWITCH_STACK_BOTTOM, "login", profile->url);
|
2008-11-22 01:34:19 +00:00
|
|
|
switch_event_add_header_string(event, SWITCH_STACK_BOTTOM, "user-agent", full_agent);
|
2007-03-31 19:01:33 +00:00
|
|
|
switch_event_add_header(event, SWITCH_STACK_BOTTOM, "from", "%s@%s", from_user, from_host);
|
|
|
|
|
2008-08-16 02:19:43 +00:00
|
|
|
switch_event_add_header_string(event, SWITCH_STACK_BOTTOM, "event_type", event_type);
|
2007-03-31 19:01:33 +00:00
|
|
|
switch_event_fire(&event);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (event_type) {
|
|
|
|
su_free(profile->home, event_type);
|
|
|
|
}
|
2008-11-22 01:34:19 +00:00
|
|
|
|
|
|
|
if (full_agent) {
|
|
|
|
su_free(profile->home, full_agent);
|
|
|
|
}
|
2007-03-31 19:01:33 +00:00
|
|
|
switch_xml_free(xml);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
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)
|
|
|
|
{
|
2007-11-20 03:26:40 +00:00
|
|
|
url_t *to = sip->sip_to->a_url;
|
|
|
|
url_t *from = sip->sip_from->a_url;
|
2007-12-12 21:53:32 +00:00
|
|
|
switch_snprintf(hash_key, len, "%s%s%s", from->url_user, from->url_host, to->url_user);
|
2007-03-31 19:01:33 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void sofia_presence_handle_sip_i_message(int status,
|
2008-05-27 04:54:52 +00:00
|
|
|
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[])
|
2007-03-31 19:01:33 +00:00
|
|
|
{
|
|
|
|
if (sip) {
|
|
|
|
sip_from_t const *from = sip->sip_from;
|
2007-11-20 03:26:40 +00:00
|
|
|
const char *from_user = NULL;
|
|
|
|
const char *from_host = NULL;
|
2007-03-31 19:01:33 +00:00
|
|
|
sip_to_t const *to = sip->sip_to;
|
2007-11-20 03:26:40 +00:00
|
|
|
const char *to_user = NULL;
|
|
|
|
const char *to_host = NULL;
|
2007-03-31 19:01:33 +00:00
|
|
|
sip_subject_t const *sip_subject = sip->sip_subject;
|
|
|
|
sip_payload_t *payload = sip->sip_payload;
|
|
|
|
const char *subject = "n/a";
|
|
|
|
char *msg = NULL;
|
|
|
|
|
2007-12-30 01:43:57 +00:00
|
|
|
if (sip->sip_content_type && sip->sip_content_type->c_subtype) {
|
2007-11-20 03:26:40 +00:00
|
|
|
if (strstr(sip->sip_content_type->c_subtype, "composing")) {
|
2007-03-31 19:01:33 +00:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (from) {
|
2007-11-20 03:26:40 +00:00
|
|
|
from_user = from->a_url->url_user;
|
|
|
|
from_host = from->a_url->url_host;
|
2007-03-31 19:01:33 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
if (to) {
|
2007-11-20 03:26:40 +00:00
|
|
|
to_user = to->a_url->url_user;
|
|
|
|
to_host = to->a_url->url_host;
|
2007-03-31 19:01:33 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
if (!to_user) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (payload) {
|
|
|
|
msg = payload->pl_data;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (sip_subject) {
|
|
|
|
subject = sip_subject->g_value;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (nh) {
|
|
|
|
char hash_key[512];
|
|
|
|
private_object_t *tech_pvt;
|
|
|
|
switch_channel_t *channel;
|
|
|
|
switch_event_t *event;
|
|
|
|
char *to_addr;
|
|
|
|
char *from_addr;
|
|
|
|
char *p;
|
|
|
|
char *full_from;
|
|
|
|
char proto[512] = SOFIA_CHAT_PROTO;
|
|
|
|
|
|
|
|
full_from = sip_header_as_string(profile->home, (void *) sip->sip_from);
|
|
|
|
|
|
|
|
if ((p = strchr(to_user, '+'))) {
|
|
|
|
switch_copy_string(proto, to_user, sizeof(proto));
|
|
|
|
p = strchr(proto, '+');
|
|
|
|
*p++ = '\0';
|
2008-08-04 22:51:03 +00:00
|
|
|
|
2007-03-31 19:01:33 +00:00
|
|
|
if ((to_addr = strdup(p))) {
|
|
|
|
if ((p = strchr(to_addr, '+'))) {
|
|
|
|
*p = '@';
|
|
|
|
}
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
to_addr = switch_mprintf("%s@%s", to_user, to_host);
|
|
|
|
}
|
|
|
|
|
|
|
|
from_addr = switch_mprintf("%s@%s", from_user, from_host);
|
|
|
|
|
|
|
|
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))) {
|
|
|
|
channel = switch_core_session_get_channel(tech_pvt->session);
|
|
|
|
if (switch_event_create(&event, SWITCH_EVENT_MESSAGE) == SWITCH_STATUS_SUCCESS) {
|
2008-08-16 02:19:43 +00:00
|
|
|
switch_event_add_header_string(event, SWITCH_STACK_BOTTOM, "proto", SOFIA_CHAT_PROTO);
|
|
|
|
switch_event_add_header_string(event, SWITCH_STACK_BOTTOM, "login", profile->url);
|
|
|
|
switch_event_add_header_string(event, SWITCH_STACK_BOTTOM, "from", from_addr);
|
|
|
|
switch_event_add_header_string(event, SWITCH_STACK_BOTTOM, "hint", full_from);
|
|
|
|
switch_event_add_header_string(event, SWITCH_STACK_BOTTOM, "to", to_addr);
|
|
|
|
switch_event_add_header_string(event, SWITCH_STACK_BOTTOM, "subject", "SIMPLE MESSAGE");
|
2007-03-31 19:01:33 +00:00
|
|
|
if (msg) {
|
|
|
|
switch_event_add_body(event, "%s", msg);
|
|
|
|
}
|
2008-08-04 22:51:03 +00:00
|
|
|
|
2007-03-31 19:01:33 +00:00
|
|
|
if (switch_core_session_queue_event(tech_pvt->session, &event) != SWITCH_STATUS_SUCCESS) {
|
2008-08-16 02:19:43 +00:00
|
|
|
switch_event_add_header_string(event, SWITCH_STACK_BOTTOM, "delivery-failure", "true");
|
2007-03-31 19:01:33 +00:00
|
|
|
switch_event_fire(&event);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
} else {
|
2009-01-20 20:49:47 +00:00
|
|
|
switch_core_chat_send(proto, SOFIA_CHAT_PROTO, from_addr, to_addr, "", msg, NULL, full_from);
|
2007-03-31 19:01:33 +00:00
|
|
|
}
|
|
|
|
switch_safe_free(to_addr);
|
|
|
|
switch_safe_free(from_addr);
|
|
|
|
if (full_from) {
|
|
|
|
su_free(profile->home, full_from);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2007-04-13 22:15:58 +00:00
|
|
|
void sofia_presence_set_chat_hash(private_object_t *tech_pvt, sip_t const *sip)
|
2007-03-31 19:01:33 +00:00
|
|
|
{
|
|
|
|
char hash_key[256] = "";
|
|
|
|
char buf[512];
|
2007-09-29 01:06:08 +00:00
|
|
|
su_home_t *home = NULL;
|
2007-03-31 19:01:33 +00:00
|
|
|
|
2008-06-27 18:38:21 +00:00
|
|
|
if (!tech_pvt || tech_pvt->hash_key || !sip || !sip->sip_from || !sip->sip_from->a_url ||
|
|
|
|
!sip->sip_from->a_url->url_user || !sip->sip_from->a_url->url_host) {
|
2007-03-31 19:01:33 +00:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (sofia_reg_find_reg_url(tech_pvt->profile, sip->sip_from->a_url->url_user, sip->sip_from->a_url->url_host, buf, sizeof(buf))) {
|
2007-09-29 01:06:08 +00:00
|
|
|
home = su_home_new(sizeof(*home));
|
2008-05-27 04:54:52 +00:00
|
|
|
switch_assert(home != NULL);
|
2007-09-29 01:06:08 +00:00
|
|
|
tech_pvt->chat_from = sip_header_as_string(home, (const sip_header_t *) sip->sip_to);
|
2007-03-31 19:01:33 +00:00
|
|
|
tech_pvt->chat_to = switch_core_session_strdup(tech_pvt->session, buf);
|
|
|
|
sofia_presence_set_hash_key(hash_key, sizeof(hash_key), sip);
|
2007-09-29 01:06:08 +00:00
|
|
|
su_home_unref(home);
|
|
|
|
home = NULL;
|
2007-03-31 19:01:33 +00:00
|
|
|
} else {
|
|
|
|
return;
|
|
|
|
}
|
2008-09-16 20:04:33 +00:00
|
|
|
|
|
|
|
switch_mutex_lock(tech_pvt->profile->flag_mutex);
|
2007-03-31 19:01:33 +00:00
|
|
|
tech_pvt->hash_key = switch_core_session_strdup(tech_pvt->session, hash_key);
|
|
|
|
switch_core_hash_insert(tech_pvt->profile->chat_hash, tech_pvt->hash_key, tech_pvt);
|
2008-09-16 20:04:33 +00:00
|
|
|
switch_mutex_unlock(tech_pvt->profile->flag_mutex);
|
2007-03-31 19:01:33 +00:00
|
|
|
}
|
2008-05-27 04:54:52 +00:00
|
|
|
|
2007-11-17 04:10:35 +00:00
|
|
|
/* For Emacs:
|
|
|
|
* Local Variables:
|
|
|
|
* mode:c
|
2008-02-03 22:14:57 +00:00
|
|
|
* indent-tabs-mode:t
|
2007-11-17 04:10:35 +00:00
|
|
|
* tab-width:4
|
|
|
|
* c-basic-offset:4
|
|
|
|
* End:
|
|
|
|
* For VIM:
|
2008-07-03 19:12:26 +00:00
|
|
|
* vim:set softtabstop=4 shiftwidth=4 tabstop=4:
|
2007-11-17 04:10:35 +00:00
|
|
|
*/
|