From 3ca3ea32fd6d0d0f54ae033304bfcb2299bf79b5 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?S=C3=A9bastien=20Helleu?= Date: Sat, 20 May 2023 16:07:01 +0200 Subject: irc: create server/channel/raw buffers with function buffer_new_props (issue #1942) With this function, buffer properties are set when the buffer is created and can be read immediately by other plugins/scripts, for example in a callback of signal "buffer_opened". --- src/plugins/irc/irc-channel.c | 90 +++++++++++++++++++++++++++++-------------- src/plugins/irc/irc-raw.c | 32 +++++++++++---- src/plugins/irc/irc-server.c | 46 +++++++++++++++------- 3 files changed, 119 insertions(+), 49 deletions(-) (limited to 'src/plugins/irc') diff --git a/src/plugins/irc/irc-channel.c b/src/plugins/irc/irc-channel.c index ff3d2681a..e95c442ea 100644 --- a/src/plugins/irc/irc-channel.c +++ b/src/plugins/irc/irc-channel.c @@ -235,6 +235,24 @@ irc_channel_search_buffer (struct t_irc_server *server, int channel_type, return NULL; } +/* + * Applies properties to a buffer. + */ + +void +irc_channel_apply_props (void *data, + struct t_hashtable *hashtable, + const void *key, + const void *value) +{ + /* make C compiler happy */ + (void) hashtable; + + weechat_buffer_set ((struct t_gui_buffer *)data, + (const char *)key, + (const char *)value); +} + /* * Creates a buffer for a channel. */ @@ -247,13 +265,14 @@ irc_channel_create_buffer (struct t_irc_server *server, int auto_switch) { struct t_gui_buffer *ptr_buffer, *ptr_buffer_for_merge; + struct t_hashtable *buffer_props; int buffer_created, current_buffer_number, buffer_position; int autojoin_join, manual_join, noswitch; char str_number[32], *channel_name_lower, *buffer_name; const char *short_name, *localvar_channel; buffer_created = 0; - + buffer_props = NULL; buffer_name = irc_buffer_build_name (server->name, channel_name); ptr_buffer = irc_channel_search_buffer (server, channel_type, @@ -270,9 +289,44 @@ irc_channel_create_buffer (struct t_irc_server *server, if (ptr_buffer) weechat_bar_item_update ("buffer_name"); } + + buffer_props = weechat_hashtable_new ( + 32, + WEECHAT_HASHTABLE_STRING, + WEECHAT_HASHTABLE_STRING, + NULL, NULL); + if (buffer_props) + { + weechat_hashtable_set ( + buffer_props, + "input_multiline", + (weechat_hashtable_has_key (server->cap_list, "batch") + && weechat_hashtable_has_key (server->cap_list, "draft/multiline")) ? + "1" : "0"); + weechat_hashtable_set (buffer_props, "name", buffer_name); + weechat_hashtable_set ( + buffer_props, + "localvar_set_type", + (channel_type == IRC_CHANNEL_TYPE_CHANNEL) ? "channel" : "private"); + weechat_hashtable_set (buffer_props, "localvar_set_nick", server->nick); + weechat_hashtable_set (buffer_props, "localvar_set_host", server->host); + weechat_hashtable_set (buffer_props, "localvar_set_server", server->name); + weechat_hashtable_set (buffer_props, "localvar_set_channel", channel_name); + if (server->is_away && server->away_message) + { + weechat_hashtable_set (buffer_props, + "localvar_set_away", server->away_message); + } + else + { + weechat_hashtable_set (buffer_props, "localvar_del_away", ""); + } + } + if (ptr_buffer) { weechat_nicklist_remove_all (ptr_buffer); + weechat_hashtable_map (buffer_props, &irc_channel_apply_props, ptr_buffer); } else { @@ -294,9 +348,11 @@ irc_channel_create_buffer (struct t_irc_server *server, current_buffer_number = weechat_buffer_get_integer ( weechat_current_buffer (), "number"); - ptr_buffer = weechat_buffer_new (buffer_name, - &irc_input_data_cb, NULL, NULL, - &irc_buffer_close_cb, NULL, NULL); + ptr_buffer = weechat_buffer_new_props ( + buffer_name, + buffer_props, + &irc_input_data_cb, NULL, NULL, + &irc_buffer_close_cb, NULL, NULL); if (!ptr_buffer) goto end; @@ -347,30 +403,6 @@ irc_channel_create_buffer (struct t_irc_server *server, } } - weechat_buffer_set ( - ptr_buffer, - "input_multiline", - (weechat_hashtable_has_key (server->cap_list, "batch") - && weechat_hashtable_has_key (server->cap_list, "draft/multiline")) ? - "1" : "0"); - - weechat_buffer_set (ptr_buffer, "name", buffer_name); - weechat_buffer_set (ptr_buffer, "localvar_set_type", - (channel_type == IRC_CHANNEL_TYPE_CHANNEL) ? "channel" : "private"); - weechat_buffer_set (ptr_buffer, "localvar_set_nick", server->nick); - weechat_buffer_set (ptr_buffer, "localvar_set_host", server->host); - weechat_buffer_set (ptr_buffer, "localvar_set_server", server->name); - weechat_buffer_set (ptr_buffer, "localvar_set_channel", channel_name); - if (server->is_away && server->away_message) - { - weechat_buffer_set (ptr_buffer, "localvar_set_away", - server->away_message); - } - else - { - weechat_buffer_set (ptr_buffer, "localvar_del_away", ""); - } - if (buffer_created) { (void) weechat_hook_signal_send ("logger_backlog", @@ -450,6 +482,8 @@ irc_channel_create_buffer (struct t_irc_server *server, } end: + if (buffer_props) + weechat_hashtable_free (buffer_props); if (buffer_name) free (buffer_name); return ptr_buffer; diff --git a/src/plugins/irc/irc-raw.c b/src/plugins/irc/irc-raw.c index 1a20ee4f5..83513a3a8 100644 --- a/src/plugins/irc/irc-raw.c +++ b/src/plugins/irc/irc-raw.c @@ -379,16 +379,39 @@ irc_raw_refresh (int clear) void irc_raw_open (int switch_to_buffer) { + struct t_hashtable *buffer_props; + if (!irc_raw_buffer) { irc_raw_buffer = weechat_buffer_search (IRC_PLUGIN_NAME, IRC_RAW_BUFFER_NAME); if (!irc_raw_buffer) { - irc_raw_buffer = weechat_buffer_new ( + buffer_props = weechat_hashtable_new ( + 32, + WEECHAT_HASHTABLE_STRING, + WEECHAT_HASHTABLE_STRING, + NULL, NULL); + if (buffer_props) + { + weechat_hashtable_set (buffer_props, + "localvar_set_type", "debug"); + weechat_hashtable_set (buffer_props, + "localvar_set_server", IRC_RAW_BUFFER_NAME); + weechat_hashtable_set (buffer_props, + "localvar_set_channel", IRC_RAW_BUFFER_NAME); + weechat_hashtable_set (buffer_props, + "localvar_set_no_log", "1"); + /* disable all highlights on this buffer */ + weechat_hashtable_set (buffer_props, "highlight_words", "-"); + } + irc_raw_buffer = weechat_buffer_new_props ( IRC_RAW_BUFFER_NAME, + buffer_props, &irc_input_data_cb, NULL, NULL, &irc_buffer_close_cb, NULL, NULL); + if (buffer_props) + weechat_hashtable_free (buffer_props); /* failed to create buffer ? then return */ if (!irc_raw_buffer) @@ -399,13 +422,6 @@ irc_raw_open (int switch_to_buffer) weechat_buffer_set (irc_raw_buffer, "short_name", IRC_RAW_BUFFER_NAME); } - weechat_buffer_set (irc_raw_buffer, "localvar_set_type", "debug"); - weechat_buffer_set (irc_raw_buffer, "localvar_set_server", IRC_RAW_BUFFER_NAME); - weechat_buffer_set (irc_raw_buffer, "localvar_set_channel", IRC_RAW_BUFFER_NAME); - weechat_buffer_set (irc_raw_buffer, "localvar_set_no_log", "1"); - - /* disable all highlights on this buffer */ - weechat_buffer_set (irc_raw_buffer, "highlight_words", "-"); irc_raw_set_localvar_filter (); diff --git a/src/plugins/irc/irc-server.c b/src/plugins/irc/irc-server.c index 41caaeed8..b8ad806c6 100644 --- a/src/plugins/irc/irc-server.c +++ b/src/plugins/irc/irc-server.c @@ -4668,6 +4668,7 @@ irc_server_create_buffer (struct t_irc_server *server) { char buffer_name[1024], charset_modifier[1024]; struct t_gui_buffer *ptr_buffer_for_merge; + struct t_hashtable *buffer_props; ptr_buffer_for_merge = NULL; switch (weechat_config_integer (irc_config_look_server_buffer)) @@ -4682,31 +4683,50 @@ irc_server_create_buffer (struct t_irc_server *server) break; } + buffer_props = weechat_hashtable_new ( + 32, + WEECHAT_HASHTABLE_STRING, + WEECHAT_HASHTABLE_STRING, + NULL, NULL); + if (buffer_props) + { + weechat_hashtable_set (buffer_props, "localvar_set_type", "server"); + weechat_hashtable_set (buffer_props, + "localvar_set_server", server->name); + weechat_hashtable_set (buffer_props, + "localvar_set_channel", server->name); + snprintf (charset_modifier, sizeof (charset_modifier), + "irc.%s", server->name); + weechat_hashtable_set (buffer_props, + "localvar_set_charset_modifier", + charset_modifier); + if (weechat_config_boolean (irc_config_network_send_unknown_commands)) + { + weechat_hashtable_set (buffer_props, + "input_get_unknown_commands", "1"); + } + } + snprintf (buffer_name, sizeof (buffer_name), "server.%s", server->name); - server->buffer = weechat_buffer_new (buffer_name, - &irc_input_data_cb, NULL, NULL, - &irc_buffer_close_cb, NULL, NULL); + server->buffer = weechat_buffer_new_props ( + buffer_name, + buffer_props, + &irc_input_data_cb, NULL, NULL, + &irc_buffer_close_cb, NULL, NULL); + if (buffer_props) + weechat_hashtable_free (buffer_props); + if (!server->buffer) return NULL; if (!weechat_buffer_get_integer (server->buffer, "short_name_is_set")) weechat_buffer_set (server->buffer, "short_name", server->name); - weechat_buffer_set (server->buffer, "localvar_set_type", "server"); - weechat_buffer_set (server->buffer, "localvar_set_server", server->name); - weechat_buffer_set (server->buffer, "localvar_set_channel", server->name); - snprintf (charset_modifier, sizeof (charset_modifier), - "irc.%s", server->name); - weechat_buffer_set (server->buffer, "localvar_set_charset_modifier", - charset_modifier); (void) weechat_hook_signal_send ("logger_backlog", WEECHAT_HOOK_SIGNAL_POINTER, server->buffer); - if (weechat_config_boolean (irc_config_network_send_unknown_commands)) - weechat_buffer_set (server->buffer, "input_get_unknown_commands", "1"); - /* set highlights settings on server buffer */ weechat_buffer_set (server->buffer, "highlight_words_add", weechat_config_string (irc_config_look_highlight_server)); -- cgit v1.2.3