]> git.ipfire.org Git - thirdparty/freeswitch.git/commitdiff
mod_sofia: Override the MWI User@Domain used per user/group/domain (MODENDP-241)
authorMarc Olivier Chouinard <mochouinard@moctel.com>
Fri, 25 Sep 2009 20:07:40 +0000 (20:07 +0000)
committerMarc Olivier Chouinard <mochouinard@moctel.com>
Fri, 25 Sep 2009 20:07:40 +0000 (20:07 +0000)
To use this, set a PARAM named MWI-Account with the user@domain you want to monitor the MWI in the user directory.

git-svn-id: http://svn.freeswitch.org/svn/freeswitch/trunk@14987 d0543943-73ff-0310-b7d9-9358b9ac24b2

src/mod/endpoints/mod_sofia/mod_sofia.c
src/mod/endpoints/mod_sofia/sofia.c
src/mod/endpoints/mod_sofia/sofia_glue.c
src/mod/endpoints/mod_sofia/sofia_presence.c
src/mod/endpoints/mod_sofia/sofia_reg.c

index 1d1e35cfd304158c28e0ef4578378c88dcafe0a7..be7df6850fe85a892cec302762f13f3862ceb371 100644 (file)
@@ -1634,10 +1634,12 @@ static int show_reg_callback(void *pArg, int argc, char **argv, char **columnNam
                                                           "IP:         \t%s\n"
                                                           "Port:       \t%s\n"
                                                           "Auth-User:  \t%s\n"
-                                                          "Auth-Realm: \t%s\n\n",
+                                                          "Auth-Realm: \t%s\n"
+                                                          "MWI-Account:\t%s@%s\n\n",
                                                           switch_str_nil(argv[0]), switch_str_nil(argv[1]), switch_str_nil(argv[2]), switch_str_nil(argv[3]),
                                                           switch_str_nil(argv[7]), switch_str_nil(argv[4]), switch_str_nil(argv[5]), exp_buf, switch_str_nil(argv[11]),
-                                                          switch_str_nil(argv[12]), switch_str_nil(argv[13]), switch_str_nil(argv[14]), switch_str_nil(argv[15]));
+                                                          switch_str_nil(argv[12]), switch_str_nil(argv[13]), switch_str_nil(argv[14]), switch_str_nil(argv[15]),
+                                                          switch_str_nil(argv[16]), switch_str_nil(argv[17]));
        return 0;
 }
 
@@ -1669,11 +1671,13 @@ static int show_reg_callback_xml(void *pArg, int argc, char **argv, char **colum
                                                           "        <network-port>%s</network-port>\n"
                                                           "        <sip-auth-user>%s</sip-auth-user>\n"
                                                           "        <sip-auth-realm>%s</sip-auth-realm>\n"
+                                                          "        <mwi-account>%s@%s</mwi-account>\n"
                                                           "    </registration>\n", 
                                                           switch_str_nil(argv[0]), switch_str_nil(argv[1]), switch_str_nil(argv[2]), 
                                                           switch_amp_encode(switch_str_nil(argv[3]),xmlbuf,buflen),
                                                           switch_str_nil(argv[7]), switch_str_nil(argv[4]), switch_str_nil(argv[5]), exp_buf, switch_str_nil(argv[11]),
-                                                          switch_str_nil(argv[12]), switch_str_nil(argv[13]), switch_str_nil(argv[14]), switch_str_nil(argv[15]));
+                                                          switch_str_nil(argv[12]), switch_str_nil(argv[13]), switch_str_nil(argv[14]), switch_str_nil(argv[15]),
+                                                          switch_str_nil(argv[16]), switch_str_nil(argv[17]));
        return 0;
 }
 
@@ -1791,14 +1795,14 @@ static switch_status_t cmd_status(char **argv, int argc, switch_stream_handle_t
                                if (!sql && argv[2] && !strcasecmp(argv[2], "pres") && argv[3]) {
                                        sql = switch_mprintf("select call_id,sip_user,sip_host,contact,status,"
                                                        "rpid,expires,user_agent,server_user,server_host,profile_name,hostname,"
-                                                       "network_ip,network_port,sip_username,sip_realm"
+                                                       "network_ip,network_port,sip_username,sip_realm,mwi_user,mwi_host"
                                                        " from sip_registrations where profile_name='%q' and presence_hosts like '%%%q%%'", 
                                                        profile->name, argv[3]);
                                }
                                if (!sql && argv[2] && !strcasecmp(argv[2], "reg") && argv[3]) {
                                        sql = switch_mprintf("select call_id,sip_user,sip_host,contact,status,"
                                                        "rpid,expires,user_agent,server_user,server_host,profile_name,hostname,"
-                                                       "network_ip,network_port,sip_username,sip_realm"
+                                                       "network_ip,network_port,sip_username,sip_realm,mwi_user,mwi_host"
                                                        " from sip_registrations where profile_name='%q' and contact like '%%%q%%'", 
                                                        profile->name, argv[3]);
                                }
@@ -1826,7 +1830,7 @@ static switch_status_t cmd_status(char **argv, int argc, switch_stream_handle_t
 
                                        sql = switch_mprintf("select call_id,sip_user,sip_host,contact,status,"
                                                        "rpid,expires,user_agent,server_user,server_host,profile_name,hostname,"
-                                                       "network_ip,network_port,sip_username,sip_realm"
+                                                       "network_ip,network_port,sip_username,sip_realm,mwi_user,mwi_host"
                                                        " from sip_registrations where profile_name='%q' and %s",
                                                        profile->name, sqlextra);
                                        switch_safe_free(dup);
@@ -1836,7 +1840,7 @@ static switch_status_t cmd_status(char **argv, int argc, switch_stream_handle_t
                                if (!sql) {
                                        sql = switch_mprintf("select call_id,sip_user,sip_host,contact,status,"
                                                                                 "rpid,expires,user_agent,server_user,server_host,profile_name,hostname,"
-                                                                                "network_ip,network_port,sip_username,sip_realm"
+                                                                                "network_ip,network_port,sip_username,sip_realm,mwi_user,mwi_host"
                                                                                 " from sip_registrations where profile_name='%q'", 
                                                                                 profile->name);
                                }
@@ -2011,7 +2015,7 @@ static switch_status_t cmd_xml_status(char **argv, int argc, switch_stream_handl
 
                                        sql = switch_mprintf("select call_id,sip_user,sip_host,contact,status,"
                                                        "rpid,expires,user_agent,server_user,server_host,profile_name,hostname,"
-                                                       "network_ip,network_port,sip_username,sip_realm"
+                                                       "network_ip,network_port,sip_username,sip_realm,mwi_user,mwi_host"
                                                        " from sip_registrations where profile_name='%q' and presence_hosts like '%%%q%%'", 
                                                        profile->name, argv[3]);
                                }
@@ -2019,7 +2023,7 @@ static switch_status_t cmd_xml_status(char **argv, int argc, switch_stream_handl
 
                                        sql = switch_mprintf("select call_id,sip_user,sip_host,contact,status,"
                                                        "rpid,expires,user_agent,server_user,server_host,profile_name,hostname,"
-                                                       "network_ip,network_port,sip_username,sip_realm"
+                                                       "network_ip,network_port,sip_username,sip_realm,mwi_user,mwi_host"
                                                        " from sip_registrations where profile_name='%q' and contact like '%%%q%%'", 
                                                        profile->name, argv[3]);
                                }
@@ -2047,7 +2051,7 @@ static switch_status_t cmd_xml_status(char **argv, int argc, switch_stream_handl
 
                                        sql = switch_mprintf("select call_id,sip_user,sip_host,contact,status,"
                                                        "rpid,expires,user_agent,server_user,server_host,profile_name,hostname,"
-                                                       "network_ip,network_port,sip_username,sip_realm"
+                                                       "network_ip,network_port,sip_username,sip_realm,mwi_user,mwi_host"
                                                        " from sip_registrations where profile_name='%q' and %s",
                                                        profile->name, sqlextra);
                                        switch_safe_free(dup);
@@ -2057,7 +2061,7 @@ static switch_status_t cmd_xml_status(char **argv, int argc, switch_stream_handl
                                if (!sql) {
                                        sql = switch_mprintf("select call_id,sip_user,sip_host,contact,status,"
                                                                                 "rpid,expires,user_agent,server_user,server_host,profile_name,hostname,"
-                                                                                "network_ip,network_port,sip_username,sip_realm"
+                                                                                "network_ip,network_port,sip_username,sip_realm,mwi_user,mwi_host"
                                                                                 " from sip_registrations where profile_name='%q'", 
                                                                                 profile->name);
                                }
@@ -3108,10 +3112,18 @@ static void general_event_handler(switch_event_t *event)
                                                                                 "from sip_registrations where call_id='%q'", ct, es, switch_str_nil(body), call_id
                                                                                 );
                                } else {
-                                       sql = switch_mprintf("select sip_user,sip_host,contact,profile_name,'%q','%q','%q' "
-                                                                                "from sip_registrations where sip_user='%s' and sip_host='%q'",
-                                                                                ct, es, switch_str_nil(body), switch_str_nil(user), switch_str_nil(host)
-                                                                                );
+                                       if (!strcasecmp(es, "message-summary")) {
+                                               sql = switch_mprintf("select sip_user,sip_host,contact,profile_name,'%q','%q','%q' "
+                                                               "from sip_registrations where mwi_user='%s' and mwi_host='%q'",
+                                                               ct, es, switch_str_nil(body), switch_str_nil(user), switch_str_nil(host)
+                                                               );
+                                       } else {
+                                               sql = switch_mprintf("select sip_user,sip_host,contact,profile_name,'%q','%q','%q' "
+                                                               "from sip_registrations where sip_user='%s' and sip_host='%q'",
+                                                               ct, es, switch_str_nil(body), switch_str_nil(user), switch_str_nil(host)
+                                                               );
+
+                                       }
                                }
                                
 
index 0734d19d317ff0c4bfa61321ab0b6e002d5d97c4..62a49dc7e5d8480850a69385678813c97e7359f2 100644 (file)
@@ -651,6 +651,24 @@ void event_handler(switch_event_t *event)
 
                sofia_profile_t *profile = NULL;
 
+               char *mwi_account = NULL;
+               char *dup_mwi_account = NULL;
+               char *mwi_user = NULL;
+               char *mwi_host = NULL;
+
+               if ((mwi_account = switch_event_get_header(event, "orig-mwi-account"))) {
+                       dup_mwi_account = strdup(mwi_account);
+                       switch_assert(dup_mwi_account != NULL);
+                       sofia_glue_get_user_host(dup_mwi_account, &mwi_user, &mwi_host);
+               }
+
+               if (!mwi_user) {
+                       mwi_user = (char *) from_user;
+               }
+               if (!mwi_host) {
+                       mwi_host = (char *) from_host;
+               }
+
                char guess_ip4[256];
 
                if (exp_str) {
@@ -663,7 +681,7 @@ void event_handler(switch_event_t *event)
 
                if (!profile_name || !(profile = sofia_glue_find_profile(profile_name))) {
                        switch_log_printf(SWITCH_CHANNEL_LOG, SWITCH_LOG_ERROR, "Invalid Profile\n");
-                       return;
+                       goto end;
                }
                if (sofia_test_pflag(profile, PFLAG_MULTIREG)) {
                        sql = switch_mprintf("delete from sip_registrations where call_id='%q'", call_id);
@@ -699,10 +717,10 @@ void event_handler(switch_event_t *event)
                switch_find_local_ip(guess_ip4, sizeof(guess_ip4), NULL, AF_INET);
                sql = switch_mprintf("insert into sip_registrations "
                                                         "(call_id, sip_user, sip_host, presence_hosts, contact, status, rpid, expires,"
-                                                        "user_agent, server_user, server_host, profile_name, hostname, network_ip, network_port, sip_username, sip_realm) "
-                                                        "values ('%q','%q','%q','%q','%q','Registered','%q',%ld, '%q','%q','%q','%q','%q','%q','%q','%q','%q')",
+                                                        "user_agent, server_user, server_host, profile_name, hostname, network_ip, network_port, sip_username, sip_realm, mwi_user, mwi_host) "
+                                                        "values ('%q','%q','%q','%q','%q','Registered','%q',%ld, '%q','%q','%q','%q','%q','%q','%q','%q','%q','%q','%q')",
                                                         call_id, from_user, from_host, presence_hosts, contact_str, rpid, expires, user_agent, to_user, guess_ip4, 
-                                                        profile_name, mod_sofia_globals.hostname, network_ip, network_port, username, realm);
+                                                        profile_name, mod_sofia_globals.hostname, network_ip, network_port, username, realm, mwi_user, mwi_host);
 
                if (sql) {
                        sofia_glue_execute_sql(profile, &sql, SWITCH_TRUE);
@@ -713,8 +731,9 @@ void event_handler(switch_event_t *event)
                if (profile) {
                        sofia_glue_release_profile(profile);
                }
-
+end:
                switch_safe_free(fixed_contact_str);
+               switch_safe_free(dup_mwi_account);
        }
 }
 
index 728df586b5035ff27ad7e243848ddee539cb0b3b..8a40caab1f4e1cee1a35becd548d2a5768ec5a29 100644 (file)
@@ -3597,7 +3597,9 @@ int sofia_glue_init_sql(sofia_profile_t *profile)
                "   network_ip      VARCHAR(255),\n" 
                "   network_port    VARCHAR(6),\n" 
                "   sip_username    VARCHAR(255),\n" 
-               "   sip_realm       VARCHAR(255)\n" 
+               "   sip_realm       VARCHAR(255),\n"
+               "   mwi_user        VARCHAR(255),\n"
+               "   mwi_host        VARCHAR(255)\n"
                ");\n";
 
 
@@ -3740,7 +3742,7 @@ int sofia_glue_init_sql(sofia_profile_t *profile)
                
                test_sql = switch_mprintf("delete from sip_registrations where (contact like '%%TCP%%' "
                                                                  "or status like '%%TCP%%' or status like '%%TLS%%') and hostname='%q' "
-                                                                 "and network_ip!='-1' and network_port!='-1' and sip_username != '-1'", 
+                                                                 "and network_ip!='-1' and network_port!='-1' and sip_username != '-1' and mwi_user != '-1' and mwi_host != '-1'", 
                                                                  mod_sofia_globals.hostname);
 
                if (switch_odbc_handle_exec(profile->master_odbc, test_sql, NULL) != SWITCH_ODBC_SUCCESS) {
@@ -3813,7 +3815,7 @@ int sofia_glue_init_sql(sofia_profile_t *profile)
 
                test_sql = switch_mprintf("delete from sip_registrations where (contact like '%%TCP%%' "
                                                                  "or status like '%%TCP%%' or status like '%%TLS%%') and hostname='%q' "
-                                                                 "and network_ip!='-1' and network_port!='-1' and sip_username != '-1'",
+                                                                 "and network_ip!='-1' and network_port!='-1' and sip_username != '-1' and mwi_user != '-1' and mwi_host != '-1'",
                                                                  mod_sofia_globals.hostname);
                
                switch_core_db_test_reactive(profile->master_db, test_sql, "DROP TABLE sip_registrations", reg_sql);
@@ -4120,45 +4122,43 @@ char *sofia_glue_execute_sql2str(sofia_profile_t *profile, switch_mutex_t *mutex
 
 int sofia_glue_get_user_host(char *in, char **user, char **host)
 {
-       char *p, *h, *u = in;
+       char *p = NULL, *h = NULL, *u = in;
 
-       if (user) {
-               *user = NULL;
+       if (!in) {
+               return 0;
        }
 
-       *host = NULL;
-
        /* First isolate the host part from the user part */
        if ((h = strchr(u, '@'))) {
                *h++ = '\0';
-       } else {
-               return 0;
        }
 
        /* Clean out the user part of its protocol prefix (if any) */
-       if (user && (p = strchr(u, ':'))) {
+       if ((p = strchr(u, ':'))) {
                *p++ = '\0';
                u = p;
        }
 
        /* Clean out the host part of any suffix */
-       if ((p = strchr(h, ':'))) {
-               *p = '\0';
-       }
-       
-       if ((p = strchr(h, ';'))) {
-               *p = '\0';
-       }
-       
-       if ((p = strchr(h, ' '))) {
-               *p = '\0';
-       }
+       if (h) {
+               if ((p = strchr(h, ':'))) {
+                       *p = '\0';
+               }
+
+               if ((p = strchr(h, ';'))) {
+                       *p = '\0';
+               }
 
+               if ((p = strchr(h, ' '))) {
+                       *p = '\0';
+               }
+       }
        if (user) {
-       *user = u;
+               *user = u;
+       }
+       if (host) {
+               *host = h;
        }
-
-       *host = h;
 
        return 1;
 }
index ca8f9e4627d9cb4e11b78248c64968871a7a565d..4ceb9d6554cbc77b9236a1aa6fde92c7cdf3c047 100644 (file)
@@ -345,11 +345,11 @@ static void actual_sofia_presence_mwi_event_handler(switch_event_t *event)
 
        if (for_everyone) {
                sql = switch_mprintf("select sip_user,sip_host,contact,profile_name,network_ip,'%q' "
-                                                        "from sip_registrations where sip_user='%q' and sip_host='%q'", 
+                                                        "from sip_registrations where mwi_user='%q' and mwi_host='%q'", 
                                                         stream.data, user, host);
        } else if (call_id) {
                sql = switch_mprintf("select sip_user,sip_host,contact,profile_name,network_ip,'%q' "
-                                                        "from sip_registrations where sip_user='%q' and sip_host='%q' and call_id='%q'", 
+                                                        "from sip_registrations where mwi_user='%q' and mwi_host='%q' and call_id='%q'", 
                                                         stream.data, user, host, call_id);
        }
 
index d3301879e524164a0e72c8e70e69e3d603dfd04b..5ea3f572729e96f82eace4a91e00fe57f522379d 100644 (file)
@@ -710,6 +710,10 @@ uint8_t sofia_reg_handle_register(nua_t *nua, sofia_profile_t *profile, nua_hand
        switch_event_t *s_event;
        const char *to_user = NULL;
        const char *to_host = NULL;
+       char *mwi_account = NULL;
+       char *dup_mwi_account = NULL;
+       char *mwi_user = NULL;
+       char *mwi_host = NULL;
        const char *from_user = NULL;
        const char *from_host = NULL;
        const char *reg_host = profile->reg_db_domain;
@@ -1004,6 +1008,19 @@ uint8_t sofia_reg_handle_register(nua_t *nua, sofia_profile_t *profile, nua_hand
        }
   reg:
 
+       if ((mwi_account = switch_event_get_header(*v_event, "mwi-account"))) {
+               dup_mwi_account = strdup(mwi_account);
+               switch_assert(dup_mwi_account != NULL);
+               sofia_glue_get_user_host(dup_mwi_account, &mwi_user, &mwi_host);
+       }
+
+       if (!mwi_user) {
+               mwi_user = (char *) to_user;
+       }
+       if (!mwi_host) {
+               mwi_host = (char *) reg_host;
+       }
+
        if (regtype != REG_REGISTER) {
                switch_goto_int(r, 0, end);
        }
@@ -1049,21 +1066,20 @@ uint8_t sofia_reg_handle_register(nua_t *nua, sofia_profile_t *profile, nua_hand
                }
                switch_mutex_lock(profile->ireg_mutex);
                sofia_glue_execute_sql(profile, &sql, SWITCH_TRUE);
-               
+
                switch_find_local_ip(guess_ip4, sizeof(guess_ip4), NULL, AF_INET);
                sql = switch_mprintf("insert into sip_registrations "
                                                         "(call_id,sip_user,sip_host,presence_hosts,contact,status,rpid,expires,"
-                                                        "user_agent,server_user,server_host,profile_name,hostname,network_ip,network_port,sip_username,sip_realm) "
-                                                        "values ('%q','%q', '%q','%q','%q','%q', '%q', %ld, '%q', '%q', '%q', '%q', '%q', '%q', '%q','%q','%q')", 
+                                                        "user_agent,server_user,server_host,profile_name,hostname,network_ip,network_port,sip_username,sip_realm,mwi_user,mwi_host) "
+                                                        "values ('%q','%q', '%q','%q','%q','%q', '%q', %ld, '%q', '%q', '%q', '%q', '%q', '%q', '%q','%q','%q','%q','%q')", 
                                                         call_id, to_user, reg_host, profile->presence_hosts ? profile->presence_hosts : reg_host, 
                                                         contact_str, reg_desc, rpid, (long) switch_epoch_time_now(NULL) + (long) exptime * 2, 
-                                                        agent, from_user, guess_ip4, profile->name, mod_sofia_globals.hostname, network_ip, network_port_c, username, realm);
+                                                        agent, from_user, guess_ip4, profile->name, mod_sofia_globals.hostname, network_ip, network_port_c, username, realm, mwi_user, mwi_host);
                                                         
                if (sql) {
                        sofia_glue_execute_sql(profile, &sql, SWITCH_TRUE);
                }
 
-
                switch_mutex_unlock(profile->ireg_mutex);
 
                if (switch_event_create_subclass(&s_event, SWITCH_EVENT_CUSTOM, MY_EVENT_REGISTER) == SWITCH_STATUS_SUCCESS) {
@@ -1198,7 +1214,7 @@ uint8_t sofia_reg_handle_register(nua_t *nua, sofia_profile_t *profile, nua_hand
 
                        if (sofia_test_pflag(profile, PFLAG_MESSAGE_QUERY_ON_REGISTER)) {
                                if (switch_event_create(&s_event, SWITCH_EVENT_MESSAGE_QUERY) == SWITCH_STATUS_SUCCESS) {
-                                       switch_event_add_header(s_event, SWITCH_STACK_BOTTOM, "Message-Account", "sip:%s@%s", to_user, reg_host);
+                                       switch_event_add_header(s_event, SWITCH_STACK_BOTTOM, "Message-Account", "sip:%s@%s", mwi_user, mwi_host);
                                        switch_event_add_header_string(s_event, SWITCH_STACK_BOTTOM, "VM-Sofia-Profile", profile->name);
                                        switch_event_add_header_string(s_event, SWITCH_STACK_BOTTOM, "VM-Call-ID", call_id);
                                }
@@ -1239,6 +1255,7 @@ uint8_t sofia_reg_handle_register(nua_t *nua, sofia_profile_t *profile, nua_hand
 
 
  end:
+       switch_safe_free(dup_mwi_account);
 
        if (auth_params) {
                switch_event_destroy(&auth_params);
@@ -1568,6 +1585,7 @@ auth_res_t sofia_reg_parse_auth(sofia_profile_t *profile,
        int first = 0;
        const char *passwd = NULL;
        const char *a1_hash = NULL;
+       const char *mwi_account = NULL;
        char *sql;
        char *number_alias = NULL;
        switch_xml_t domain, xml = NULL, user, param, uparams, dparams, group = NULL, gparams = NULL;
@@ -1762,6 +1780,9 @@ auth_res_t sofia_reg_parse_auth(sofia_profile_t *profile,
                        if (!strcasecmp(var, "a1-hash")) {
                                a1_hash = val;
                        }
+                       if (!strcasecmp(var, "mwi-account")) {
+                               mwi_account = val;
+                       }
                }
        }
 
@@ -1786,6 +1807,9 @@ auth_res_t sofia_reg_parse_auth(sofia_profile_t *profile,
                        if (!strcasecmp(var, "a1-hash")) {
                                a1_hash = val;
                        }
+                       if (!strcasecmp(var, "mwi-account")) {
+                               mwi_account = val;
+                       }
                }
        }
 
@@ -1810,6 +1834,9 @@ auth_res_t sofia_reg_parse_auth(sofia_profile_t *profile,
                        if (!strcasecmp(var, "a1-hash")) {
                                a1_hash = val;
                        }
+                       if (!strcasecmp(var, "mwi-account")) {
+                               mwi_account = val;
+                       }
                }
        }
 
@@ -1897,6 +1924,10 @@ auth_res_t sofia_reg_parse_auth(sofia_profile_t *profile,
                        switch_event_add_header_string(*v_event, SWITCH_STACK_BOTTOM, "user_name", username);
                        switch_event_add_header_string(*v_event, SWITCH_STACK_BOTTOM, "domain_name", domain_name);
 
+                       if (mwi_account) {
+                               switch_event_add_header_string(*v_event, SWITCH_STACK_BOTTOM, "mwi-account", mwi_account);
+                       }
+
                        if ((uparams = switch_xml_child(user, "params"))) {
                                xparams_type[i] = 0;
                                xparams[i++] = uparams;