From 6ade3ddea39c9f3d4f2c138aca2b633f055ba893 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Fran=C3=A7ois=20Grisez?= Date: Tue, 6 Oct 2015 22:28:25 +0200 Subject: [PATCH] Use a better regex to detect URIs in chat messages --- gtk/chat.c | 26 +++++++++++++++++++++++++- gtk/linphone.h | 8 +------- gtk/main.c | 21 --------------------- 3 files changed, 26 insertions(+), 29 deletions(-) diff --git a/gtk/chat.c b/gtk/chat.c index 4da3b1948..87acccf22 100644 --- a/gtk/chat.c +++ b/gtk/chat.c @@ -31,6 +31,30 @@ Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. #define CONFIG_FILE ".linphone-history.db" +#include "regex.h" + +GRegex *uri_regex = NULL; + +static void free_uri_regex(void) { + if(uri_regex) g_regex_unref(uri_regex); +} + +static const GRegex *get_uri_regex(void) { + const gchar *pattern = BC_REGEX_URI; + GError *error = NULL; + if(uri_regex == NULL) { + uri_regex = g_regex_new(pattern, G_REGEX_OPTIMIZE, 0, &error); + if(error) { + g_warning("Could not parse regex pattern for URIs: %s", error->message); + g_error_free(error); + uri_regex = NULL; + return NULL; + } + atexit(free_uri_regex); + } + return uri_regex; +} + char *linphone_gtk_message_storage_get_db_file(const char *filename){ const int path_max=1024; char *db_file=NULL; @@ -157,7 +181,7 @@ void linphone_gtk_push_text(GtkWidget *w, const LinphoneAddress *from, char *from_str=linphone_address_as_string_uri_only(from); gchar *from_message=(gchar *)g_object_get_data(G_OBJECT(w),"from_message"); GHashTable *table=(GHashTable*)g_object_get_data(G_OBJECT(w),"table"); - const GRegex *uri_regex = linphone_gtk_get_uri_regex(); + const GRegex *uri_regex = get_uri_regex(); GMatchInfo *match_info = NULL; const char *message = linphone_chat_message_get_text(msg); time_t t; diff --git a/gtk/linphone.h b/gtk/linphone.h index f7cce8407..156f9d389 100644 --- a/gtk/linphone.h +++ b/gtk/linphone.h @@ -340,10 +340,4 @@ LINPHONE_PUBLIC void linphone_gtk_proxy_transport_changed(GtkWidget *combo); LINPHONE_PUBLIC void linphone_gtk_tunnel_ok(GtkButton *button); LINPHONE_PUBLIC void linphone_gtk_notebook_current_page_changed(GtkNotebook *notebook, GtkWidget *page, guint page_num, gpointer user_data); LINPHONE_PUBLIC void linphone_gtk_reload_sound_devices(void); -LINPHONE_PUBLIC void linphone_gtk_reload_video_devices(void); - -/** - * Provide a regex to match URIs - * @return A singleton regex object - */ -const GRegex *linphone_gtk_get_uri_regex(void); +LINPHONE_PUBLIC void linphone_gtk_reload_video_devices(void); \ No newline at end of file diff --git a/gtk/main.c b/gtk/main.c index f5fcfed1b..1930f8b89 100644 --- a/gtk/main.c +++ b/gtk/main.c @@ -63,7 +63,6 @@ const char *this_program_ident_string="linphone_ident_string=" LINPHONE_VERSION; static LinphoneCore *the_core=NULL; static GtkWidget *the_ui=NULL; static LinphoneLDAPContactProvider* ldap_provider = NULL; -static GRegex *uri_regex = NULL; static void linphone_gtk_global_state_changed(LinphoneCore *lc, LinphoneGlobalState state, const char*str); static void linphone_gtk_registration_state_changed(LinphoneCore *lc, LinphoneProxyConfig *cfg, LinphoneRegistrationState rs, const char *msg); @@ -2026,26 +2025,6 @@ static void populate_xdg_data_dirs_envvar(void) { #endif } -static void free_uri_regex(void) { - if(uri_regex) g_regex_unref(uri_regex); -} - -const GRegex *linphone_gtk_get_uri_regex(void) { - const gchar *pattern = "\\b[a-z0-9]+://[\\S]+\\b"; - GError *error = NULL; - if(uri_regex == NULL) { - uri_regex = g_regex_new(pattern, G_REGEX_OPTIMIZE, 0, &error); - if(error) { - g_warning("Could not parse regex pattern for URIs: %s", error->message); - g_error_free(error); - uri_regex = NULL; - return NULL; - } - atexit(free_uri_regex); - } - return uri_regex; -} - int main(int argc, char *argv[]){ char *config_file; const char *factory_config_file;