From ec3f6f4ec629023c2e032d601e53ebd1ced0e83a Mon Sep 17 00:00:00 2001 From: Anthony Minessale Date: Tue, 20 Jan 2009 20:49:47 +0000 Subject: [PATCH] add chat interface EXTRAGUY-00 git-svn-id: http://svn.freeswitch.org/svn/freeswitch/trunk@11319 d0543943-73ff-0310-b7d9-9358b9ac24b2 --- src/include/switch_core.h | 2 ++ src/include/switch_module_interfaces.h | 3 ++- .../mod_conference/mod_conference.c | 20 ++++++-------- .../applications/mod_dptools/mod_dptools.c | 26 ++++++++----------- .../endpoints/mod_dingaling/mod_dingaling.c | 17 +++++------- src/mod/endpoints/mod_sofia/mod_sofia.h | 3 ++- src/mod/endpoints/mod_sofia/sofia_presence.c | 10 +++---- src/switch_core.c | 20 ++++++++++++++ src/switch_loadable_module.c | 6 +---- 9 files changed, 56 insertions(+), 51 deletions(-) diff --git a/src/include/switch_core.h b/src/include/switch_core.h index f36c067a85..bf6d40d6aa 100644 --- a/src/include/switch_core.h +++ b/src/include/switch_core.h @@ -1707,6 +1707,8 @@ SWITCH_DECLARE(switch_status_t) switch_console_set_alias(const char *string); SWITCH_DECLARE(int) switch_system(const char *cmd, switch_bool_t wait); SWITCH_DECLARE(void) switch_cond_yield(switch_interval_time_t t); SWITCH_DECLARE(void) switch_cond_next(void); +SWITCH_DECLARE(switch_status_t) switch_core_chat_send(const char *name, const char *proto, const char *from, const char *to, + const char *subject, const char *body, const char *type, const char *hint); ///\} /*! diff --git a/src/include/switch_module_interfaces.h b/src/include/switch_module_interfaces.h index cee2cfbf4d..c85a50e681 100644 --- a/src/include/switch_module_interfaces.h +++ b/src/include/switch_module_interfaces.h @@ -442,7 +442,8 @@ struct switch_chat_interface { /*! the name of the interface */ const char *interface_name; /*! function to open the directory interface */ - switch_status_t (*chat_send) (char *proto, char *from, char *to, char *subject, char *body, char *hint); + switch_status_t (*chat_send) (const char *proto, const char *from, const char *to, + const char *subject, const char *body, const char *type, const char *hint); switch_thread_rwlock_t *rwlock; int refs; switch_mutex_t *reflock; diff --git a/src/mod/applications/mod_conference/mod_conference.c b/src/mod/applications/mod_conference/mod_conference.c index 6b557fc779..cdfd0456d9 100644 --- a/src/mod/applications/mod_conference/mod_conference.c +++ b/src/mod/applications/mod_conference/mod_conference.c @@ -372,7 +372,9 @@ static switch_status_t conference_member_play_file(conference_member_t *member, static switch_status_t conference_member_say(conference_member_t *member, char *text, uint32_t leadin); static uint32_t conference_member_stop_file(conference_member_t *member, file_stop_t stop); static conference_obj_t *conference_new(char *name, conf_xml_cfg_t cfg, switch_memory_pool_t *pool); -static switch_status_t chat_send(char *proto, char *from, char *to, char *subject, char *body, char *hint); +static switch_status_t chat_send(const char *proto, const char *from, const char *to, const char *subject, + const char *body, const char *type, const char *hint); + static void launch_conference_record_thread(conference_obj_t *conference, char *path); typedef switch_status_t (*conf_api_args_cmd_t) (conference_obj_t *, switch_stream_handle_t *, int, char **); @@ -1907,7 +1909,7 @@ static void conference_loop_output(conference_member_t *member) freeme = switch_mprintf("%s+%s@%s", CONF_CHAT_PROTO, member->conference->name, member->conference->domain); to = freeme; } - chat_send(proto, from, to, subject, body, hint); + chat_send(proto, from, to, subject, body, NULL, hint); switch_safe_free(freeme); } } @@ -4957,10 +4959,10 @@ static void launch_conference_record_thread(conference_obj_t *conference, char * switch_thread_create(&thread, thd_attr, conference_record_thread_run, rec, rec->pool); } -static switch_status_t chat_send(char *proto, char *from, char *to, char *subject, char *body, char *hint) +static switch_status_t chat_send(const char *proto, const char *from, const char *to, const char *subject, + const char *body, const char *type, const char *hint) { char name[512] = "", *p, *lbuf = NULL; - switch_chat_interface_t *ci; conference_obj_t *conference = NULL; switch_stream_handle_t stream = { 0 }; @@ -4972,12 +4974,6 @@ static switch_status_t chat_send(char *proto, char *from, char *to, char *subjec return SWITCH_STATUS_SUCCESS; } - if (!(ci = switch_loadable_module_get_chat_interface(proto))) { - switch_log_printf(SWITCH_CHANNEL_LOG, SWITCH_LOG_ERROR, "Invalid Chat Interface [%s]!\n", proto); - return SWITCH_STATUS_FALSE; - } - - if ((p = strchr(to, '@'))) { switch_copy_string(name, to, ++p - to); } else { @@ -4986,7 +4982,7 @@ static switch_status_t chat_send(char *proto, char *from, char *to, char *subjec if (!(conference = (conference_obj_t *) switch_core_hash_find(globals.conference_hash, name))) { - ci->chat_send(CONF_CHAT_PROTO, to, hint && strchr(hint, '/') ? hint : from, "", "Conference not active.", NULL); + switch_core_chat_send(proto, CONF_CHAT_PROTO, to, hint && strchr(hint, '/') ? hint : from, "", "Conference not active.", NULL, NULL); return SWITCH_STATUS_FALSE; } @@ -5014,7 +5010,7 @@ static switch_status_t chat_send(char *proto, char *from, char *to, char *subjec switch_safe_free(lbuf); - ci->chat_send(CONF_CHAT_PROTO, to, hint && strchr(hint, '/') ? hint : from, "", stream.data, NULL); + switch_core_chat_send(proto, CONF_CHAT_PROTO, to, hint && strchr(hint, '/') ? hint : from, "", stream.data, NULL, NULL); switch_safe_free(stream.data); return SWITCH_STATUS_SUCCESS; diff --git a/src/mod/applications/mod_dptools/mod_dptools.c b/src/mod/applications/mod_dptools/mod_dptools.c index a195b72792..f3b15a2146 100644 --- a/src/mod/applications/mod_dptools/mod_dptools.c +++ b/src/mod/applications/mod_dptools/mod_dptools.c @@ -1069,16 +1069,11 @@ SWITCH_STANDARD_API(chat_api_function) if (!switch_strlen_zero(cmd) && (lbuf = strdup(cmd)) && (argc = switch_separate_string(lbuf, '|', argv, (sizeof(argv) / sizeof(argv[0])))) == 4) { - switch_chat_interface_t *ci; - - if ((ci = switch_loadable_module_get_chat_interface(argv[0]))) { - if (ci->chat_send("dp", argv[1], argv[2], "", argv[3], "") == SWITCH_STATUS_SUCCESS) { - stream->write_function(stream, "Sent"); - } else { - stream->write_function(stream, "Error! Message Not Sent"); - } + + if (switch_core_chat_send(argv[0], "dp", argv[1], argv[2], "", argv[3], NULL, "") == SWITCH_STATUS_SUCCESS) { + stream->write_function(stream, "Sent"); } else { - switch_log_printf(SWITCH_CHANNEL_LOG, SWITCH_LOG_ERROR, "Invalid Chat Interface [%s]!\n", argv[0]); + stream->write_function(stream, "Error! Message Not Sent"); } } else { stream->write_function(stream, "Invalid"); @@ -2438,7 +2433,8 @@ SWITCH_STANDARD_APP(wait_for_silence_function) switch_log_printf(SWITCH_CHANNEL_LOG, SWITCH_LOG_ERROR, "Usage: %s\n", WAIT_FOR_SILENCE_SYNTAX); } -static switch_status_t event_chat_send(char *proto, char *from, char *to, char *subject, char *body, char *hint) +static switch_status_t event_chat_send(const char *proto, const char *from, const char *to, const char *subject, + const char *body, const char *type, const char *hint) { switch_event_t *event; @@ -2466,14 +2462,14 @@ static switch_status_t event_chat_send(char *proto, char *from, char *to, char * return SWITCH_STATUS_MEMERR; } -static switch_status_t api_chat_send(char *proto, char *from, char *to, char *subject, char *body, char *hint) +static switch_status_t api_chat_send(const char *proto, const char *from, const char *to, const char *subject, + const char *body, const char *type, const char *hint) { if (to) { const char *v; switch_stream_handle_t stream = { 0 }; char *cmd, *arg; - switch_chat_interface_t *ci; - + if (!(v = switch_core_get_variable(to))) { v = to; } @@ -2490,8 +2486,8 @@ static switch_status_t api_chat_send(char *proto, char *from, char *to, char *su SWITCH_STANDARD_STREAM(stream); switch_api_execute(cmd, arg, NULL, &stream); - if (proto && (ci = switch_loadable_module_get_chat_interface(proto))) { - ci->chat_send("api", to, hint && strchr(hint, '/') ? hint : from, "text/plain", (char *) stream.data, NULL); + if (proto) { + switch_core_chat_send(proto, "api", to, hint && strchr(hint, '/') ? hint : from, "text/plain", (char *) stream.data, NULL, NULL); } switch_safe_free(stream.data); diff --git a/src/mod/endpoints/mod_dingaling/mod_dingaling.c b/src/mod/endpoints/mod_dingaling/mod_dingaling.c index 7f1f614f4b..450d6833af 100644 --- a/src/mod/endpoints/mod_dingaling/mod_dingaling.c +++ b/src/mod/endpoints/mod_dingaling/mod_dingaling.c @@ -506,7 +506,8 @@ static void pres_event_handler(switch_event_t *event) switch_safe_free(sql); } -static switch_status_t chat_send(char *proto, char *from, char *to, char *subject, char *body, char *hint) +static switch_status_t chat_send(const char *proto, const char *from, const char *to, const char *subject, + const char *body, const char *type, const char *hint); { char *user, *host, *f_user = NULL, *ffrom = NULL, *f_host = NULL, *f_resource = NULL; mdl_profile_t *profile = NULL; @@ -2459,10 +2460,9 @@ static ldl_status handle_signalling(ldl_handle_t *handle, ldl_session_t *dlsessi switch (dl_signal) { case LDL_SIGNAL_MSG:{ - switch_chat_interface_t *ci; - char *proto = MDL_CHAT_PROTO; - char *pproto = NULL, *ffrom = NULL; - char *hint; + char *proto = MDL_CHAT_PROTO; + char *pproto = NULL, *ffrom = NULL; + char *hint; #ifdef AUTO_REPLY if (profile->auto_reply) { ldl_handle_send_msg(handle, @@ -2489,11 +2489,8 @@ static ldl_status handle_signalling(ldl_handle_t *handle, ldl_session_t *dlsessi from = ffrom; } - if ((ci = switch_loadable_module_get_chat_interface(proto))) { - ci->chat_send(MDL_CHAT_PROTO, from, to, subject, switch_str_nil(msg), hint); - } else { - switch_log_printf(SWITCH_CHANNEL_LOG, SWITCH_LOG_ERROR, "Invalid Chat Interface [%s]!\n", proto); - } + + switch_core_chat_send(proto, MDL_CHAT_PROTO, from, to, subject, switch_str_nil(msg), NULL, hint); switch_safe_free(pproto); switch_safe_free(ffrom); diff --git a/src/mod/endpoints/mod_sofia/mod_sofia.h b/src/mod/endpoints/mod_sofia/mod_sofia.h index 1ee2d1f86a..e896e2640c 100644 --- a/src/mod/endpoints/mod_sofia/mod_sofia.h +++ b/src/mod/endpoints/mod_sofia/mod_sofia.h @@ -624,7 +624,8 @@ void *SWITCH_THREAD_FUNC sofia_profile_thread_run(switch_thread_t *thread, void void launch_sofia_profile_thread(sofia_profile_t *profile); -switch_status_t sofia_presence_chat_send(char *proto, char *from, char *to, char *subject, char *body, char *hint); +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); void sofia_glue_tech_absorb_sdp(private_object_t *tech_pvt); switch_status_t sofia_glue_tech_media(private_object_t *tech_pvt, const char *r_sdp); char *sofia_reg_find_reg_url(sofia_profile_t *profile, const char *user, const char *host, char *val, switch_size_t len); diff --git a/src/mod/endpoints/mod_sofia/sofia_presence.c b/src/mod/endpoints/mod_sofia/sofia_presence.c index 8253b8c66f..be2267b694 100644 --- a/src/mod/endpoints/mod_sofia/sofia_presence.c +++ b/src/mod/endpoints/mod_sofia/sofia_presence.c @@ -49,7 +49,8 @@ struct presence_helper { char last_uuid[512]; }; -switch_status_t sofia_presence_chat_send(char *proto, char *from, char *to, char *subject, char *body, char *hint) +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) { char buf[256]; char *prof = NULL, *user = NULL, *host = NULL; @@ -2003,12 +2004,7 @@ void sofia_presence_handle_sip_i_message(int status, } } } else { - switch_chat_interface_t *ci; - if ((ci = switch_loadable_module_get_chat_interface(proto))) { - ci->chat_send(SOFIA_CHAT_PROTO, from_addr, to_addr, "", msg, full_from); - } else { - switch_log_printf(SWITCH_CHANNEL_LOG, SWITCH_LOG_ERROR, "Invalid Chat Interface [%s]!\n", proto); - } + switch_core_chat_send(proto, SOFIA_CHAT_PROTO, from_addr, to_addr, "", msg, NULL, full_from); } switch_safe_free(to_addr); switch_safe_free(from_addr); diff --git a/src/switch_core.c b/src/switch_core.c index 2926a42a88..ba86212a43 100644 --- a/src/switch_core.c +++ b/src/switch_core.c @@ -1503,6 +1503,24 @@ SWITCH_DECLARE(switch_status_t) switch_core_destroy(void) return switch_test_flag((&runtime), SCF_RESTART) ? SWITCH_STATUS_RESTART : SWITCH_STATUS_SUCCESS; } +SWITCH_DECLARE(switch_status_t) switch_core_chat_send(const char *name, const char *proto, const char *from, const char *to, + const char *subject, const char *body, const char *type, const char *hint) +{ + switch_chat_interface_t *ci; + switch_status_t status; + + if (!name || !(ci = switch_loadable_module_get_chat_interface(name)) || !ci->chat_send) { + switch_log_printf(SWITCH_CHANNEL_LOG, SWITCH_LOG_ERROR, "Invalid Chat Interface [%s]!\n", name); + return SWITCH_STATUS_FALSE; + } + + status = ci->chat_send(proto, from, to, subject, body, type, hint); + + UNPROTECT_INTERFACE(ci); + + return status; +} + SWITCH_DECLARE(switch_status_t) switch_core_management_exec(char *relative_oid, switch_management_action_t action, char *data, switch_size_t datalen) { const switch_management_interface_t *ptr; @@ -1594,6 +1612,8 @@ SWITCH_DECLARE(int) switch_system(const char *cmd, switch_bool_t wait) return ret; } + + /* For Emacs: * Local Variables: * mode:c diff --git a/src/switch_loadable_module.c b/src/switch_loadable_module.c index 2e141625af..fdbba42b5a 100644 --- a/src/switch_loadable_module.c +++ b/src/switch_loadable_module.c @@ -1353,13 +1353,9 @@ HASH_FUNC(file) HASH_FUNC(speech) HASH_FUNC(asr) HASH_FUNC(directory) +HASH_FUNC(chat) -SWITCH_DECLARE(switch_chat_interface_t *) switch_loadable_module_get_chat_interface(const char *name) -{ - return switch_core_hash_find_locked(loadable_modules.chat_hash, name, loadable_modules.mutex); -} - SWITCH_DECLARE(switch_say_interface_t *) switch_loadable_module_get_say_interface(const char *name) { return switch_core_hash_find_locked(loadable_modules.say_hash, name, loadable_modules.mutex);