mirror of
https://github.com/weechat/weechat.git
synced 2026-06-30 06:46:38 +02:00
relay: add option relay.look.raw_messages_max_length (issue #2122)
This commit is contained in:
@@ -59,6 +59,7 @@ struct t_config_section *relay_config_section_remote = NULL;
|
||||
struct t_config_option *relay_config_look_auto_open_buffer = NULL;
|
||||
struct t_config_option *relay_config_look_display_clients = NULL;
|
||||
struct t_config_option *relay_config_look_raw_messages = NULL;
|
||||
struct t_config_option *relay_config_look_raw_messages_max_length = NULL;
|
||||
|
||||
/* relay config, color section */
|
||||
|
||||
@@ -1455,6 +1456,17 @@ relay_config_init ()
|
||||
"buffer)"),
|
||||
NULL, 0, 65535, "256", NULL, 0,
|
||||
NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL);
|
||||
relay_config_look_raw_messages_max_length = weechat_config_new_option (
|
||||
relay_config_file, relay_config_section_look,
|
||||
"raw_messages_max_length", "integer",
|
||||
N_("maximum length (in number of chars) of a raw message displayed "
|
||||
"(0 = display whole message); the beginning and end of message "
|
||||
"is always displayed with at the middle: \" (...) \"; "
|
||||
"for example if the value is 8 and the raw message is "
|
||||
"\"abcdefghijklmnopqrstuvwxyz\", then the raw message displayed is: "
|
||||
"\"abcd (...) wxyz\""),
|
||||
NULL, 0, INT_MAX, "4096", NULL, 0,
|
||||
NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL);
|
||||
}
|
||||
|
||||
/* section color */
|
||||
|
||||
@@ -34,6 +34,7 @@ extern struct t_config_section *relay_config_section_path;
|
||||
|
||||
extern struct t_config_option *relay_config_look_auto_open_buffer;
|
||||
extern struct t_config_option *relay_config_look_raw_messages;
|
||||
extern struct t_config_option *relay_config_look_raw_messages_max_length;
|
||||
|
||||
extern struct t_config_option *relay_config_color_client;
|
||||
extern struct t_config_option *relay_config_color_status[];
|
||||
|
||||
@@ -293,6 +293,72 @@ relay_raw_convert_text_message (const char *data)
|
||||
return buf2;
|
||||
}
|
||||
|
||||
/*
|
||||
* Cuts a raw message in the middle using max_length: half of max length is
|
||||
* displayed at beginning, then " (...) " then half of max length at the end.
|
||||
*
|
||||
* Note: result must be freed after use.
|
||||
*/
|
||||
|
||||
char *
|
||||
relay_raw_message_cut (const char *data, int max_length)
|
||||
{
|
||||
int length, chars_start, chars_end, count;
|
||||
const char *ptr_end;
|
||||
char **result;
|
||||
|
||||
if (!data || (max_length < 0))
|
||||
return NULL;
|
||||
|
||||
if (max_length == 0)
|
||||
return strdup (data);
|
||||
|
||||
length = weechat_utf8_strlen (data);
|
||||
if (length <= max_length)
|
||||
return strdup (data);
|
||||
|
||||
chars_end = max_length / 2;
|
||||
chars_start = max_length - chars_end;
|
||||
|
||||
result = weechat_string_dyn_alloc (max_length);
|
||||
if (!result)
|
||||
return NULL;
|
||||
|
||||
if (chars_start > 0)
|
||||
{
|
||||
ptr_end = weechat_utf8_add_offset (data, chars_start);
|
||||
if (!ptr_end)
|
||||
{
|
||||
weechat_string_dyn_free (result, 1);
|
||||
return NULL;
|
||||
}
|
||||
weechat_string_dyn_concat (result, data, ptr_end - data);
|
||||
}
|
||||
if (*result[0])
|
||||
weechat_string_dyn_concat (result, " ", -1);
|
||||
weechat_string_dyn_concat (result, "(...)", -1);
|
||||
if (chars_end > 0)
|
||||
{
|
||||
ptr_end = data + strlen (data);
|
||||
count = 0;
|
||||
while (count < chars_end)
|
||||
{
|
||||
ptr_end = weechat_utf8_prev_char (data, ptr_end);
|
||||
if (!ptr_end)
|
||||
{
|
||||
weechat_string_dyn_free (result, 1);
|
||||
return NULL;
|
||||
}
|
||||
count++;
|
||||
}
|
||||
if (*result[0])
|
||||
weechat_string_dyn_concat (result, " ", -1);
|
||||
weechat_string_dyn_concat (result, ptr_end, -1);
|
||||
}
|
||||
|
||||
return weechat_string_dyn_free (result, 0);
|
||||
}
|
||||
|
||||
/*
|
||||
* Adds a new raw message to list.
|
||||
*/
|
||||
@@ -303,8 +369,9 @@ relay_raw_message_add (enum t_relay_msg_type msg_type,
|
||||
const char *peer_id,
|
||||
const char *data, int data_size)
|
||||
{
|
||||
char *raw_data, *buf, prefix[1024], prefix_arrow[16];
|
||||
int length;
|
||||
char *raw_data, *raw_data_cut, *buf, prefix[1024], prefix_arrow[16];
|
||||
const char *ptr_raw_data;
|
||||
int max_length;
|
||||
struct t_relay_raw_message *new_raw_message;
|
||||
struct timeval tv_now;
|
||||
|
||||
@@ -350,15 +417,18 @@ relay_raw_message_add (enum t_relay_msg_type msg_type,
|
||||
(peer_id && peer_id[0]) ? peer_id : "");
|
||||
}
|
||||
|
||||
length = strlen (relay_msg_type_string[msg_type]) + strlen (raw_data) + 1;
|
||||
buf = malloc (length);
|
||||
if (buf)
|
||||
raw_data_cut = NULL;
|
||||
ptr_raw_data = raw_data;
|
||||
max_length = weechat_config_integer (relay_config_look_raw_messages_max_length);
|
||||
if (max_length > 0)
|
||||
{
|
||||
snprintf (buf, length, "%s%s",
|
||||
relay_msg_type_string[msg_type],
|
||||
raw_data);
|
||||
raw_data_cut = relay_raw_message_cut (raw_data, max_length);
|
||||
ptr_raw_data = raw_data_cut;
|
||||
}
|
||||
|
||||
weechat_asprintf (&buf, "%s%s",
|
||||
relay_msg_type_string[msg_type],
|
||||
(ptr_raw_data) ? ptr_raw_data : "");
|
||||
free (raw_data_cut);
|
||||
gettimeofday (&tv_now, NULL);
|
||||
new_raw_message = relay_raw_message_add_to_list (
|
||||
tv_now.tv_sec,
|
||||
|
||||
Reference in New Issue
Block a user