diff options
author | Sébastien Helleu <flashcode@flashtux.org> | 2022-03-11 19:31:56 +0100 |
---|---|---|
committer | Sébastien Helleu <flashcode@flashtux.org> | 2022-03-11 19:31:56 +0100 |
commit | 3f9c46468c3fa664c7216d9bff731d2355f5fa20 (patch) | |
tree | adcdcedafd8756d1c0e280bbd7416efd6120b283 /src/plugins/irc | |
parent | 9a286c65c62e95964c96bf06d4d241649d37820a (diff) | |
download | weechat-3f9c46468c3fa664c7216d9bff731d2355f5fa20.zip |
irc: add option "sort" in command /autojoin
Diffstat (limited to 'src/plugins/irc')
-rw-r--r-- | src/plugins/irc/irc-command.c | 17 | ||||
-rw-r--r-- | src/plugins/irc/irc-completion.c | 3 | ||||
-rw-r--r-- | src/plugins/irc/irc-join.c | 110 | ||||
-rw-r--r-- | src/plugins/irc/irc-join.h | 6 |
4 files changed, 123 insertions, 13 deletions
diff --git a/src/plugins/irc/irc-command.c b/src/plugins/irc/irc-command.c index c02b19234..c2241f34a 100644 --- a/src/plugins/irc/irc-command.c +++ b/src/plugins/irc/irc-command.c @@ -995,6 +995,13 @@ IRC_COMMAND_CALLBACK(autojoin) goto end; } + /* sort channels */ + if (weechat_strcasecmp (argv[1], "sort") == 0) + { + irc_join_sort_autojoin (ptr_server); + goto end; + } + end: ptr_autojoin = IRC_SERVER_OPTION_STRING(ptr_server, IRC_SERVER_OPTION_AUTOJOIN); @@ -6784,7 +6791,8 @@ irc_command_init () N_("add [<channel1> [<channel2>...]]" " || addraw <channel1>[,<channel2>...] [<key1>[,<key2>...]]" " || del [<channel1> [<channel2>...]]" - " || save"), + " || save" + " || sort"), N_(" add: add current channel or a list of channels (with optional " "keys) to the autojoin option; if you are on the channel and the " "key is not provided, the key is read in the channel\n" @@ -6795,6 +6803,7 @@ irc_command_init () "channel: channel name\n" " key: key for the channel\n" " save: save currently joined channels in the autojoin option\n" + " sort: sort alphabetically channels in the autojoin option\n" "\n" "Examples:\n" " /autojoin add\n" @@ -6803,11 +6812,13 @@ irc_command_init () " /autojoin addraw #chan1,#chan2,#chan3 key1,key2\n" " /autojoin del\n" " /autojoin del #chan1\n" - " /autojoin save"), + " /autojoin save\n" + " /autojoin sort"), "add %(irc_channels)|%*" " || addraw %(irc_channels) %-" " || del %(irc_channels_autojoin)|%*" - " || save", + " || save" + " || sort", &irc_command_autojoin, NULL, NULL); weechat_hook_command_run ("/away", &irc_command_run_away, NULL, NULL); weechat_hook_command ( diff --git a/src/plugins/irc/irc-completion.c b/src/plugins/irc/irc-completion.c index 289d3779b..8a485dc55 100644 --- a/src/plugins/irc/irc-completion.c +++ b/src/plugins/irc/irc-completion.c @@ -662,7 +662,8 @@ irc_completion_channels_autojoin_cb (const void *pointer, void *data, arraylist = irc_join_split ( ptr_server, - IRC_SERVER_OPTION_STRING(ptr_server, IRC_SERVER_OPTION_AUTOJOIN)); + IRC_SERVER_OPTION_STRING(ptr_server, IRC_SERVER_OPTION_AUTOJOIN), + 0); if (!arraylist) return WEECHAT_RC_OK; diff --git a/src/plugins/irc/irc-join.c b/src/plugins/irc/irc-join.c index 7e350b530..4a4a024ea 100644 --- a/src/plugins/irc/irc-join.c +++ b/src/plugins/irc/irc-join.c @@ -33,7 +33,7 @@ /* - * Compares two join channels. + * Compares two join channels (no sort, keyed channels first). */ int @@ -71,6 +71,44 @@ irc_join_compare_cb (void *data, struct t_arraylist *arraylist, } /* + * Compares two join channels (alphabetic sort, keyed channels first). + */ + +int +irc_join_compare_sort_cb (void *data, struct t_arraylist *arraylist, + void *pointer1, void *pointer2) +{ + struct t_irc_server *server; + struct t_irc_join_channel *ptr_join_chan1, *ptr_join_chan2; + int rc; + + /* make C compiler happy */ + (void) arraylist; + + server = (struct t_irc_server *)data; + + ptr_join_chan1 = (struct t_irc_join_channel *)pointer1; + ptr_join_chan2 = (struct t_irc_join_channel *)pointer2; + + /* + * if channel is the same, always consider it's the same, even if the key + * is different + */ + rc = irc_server_strcasecmp (server, ptr_join_chan1->name, + ptr_join_chan2->name); + if (rc == 0) + return 0; + + /* channels with a key are first in list */ + if (ptr_join_chan1->key && !ptr_join_chan2->key) + return -1; + if (!ptr_join_chan1->key && ptr_join_chan2->key) + return 1; + + return rc; +} + +/* * Frees a join channel. */ @@ -122,10 +160,14 @@ irc_join_arraylist_add (struct t_arraylist *arraylist, * command (channels with key first in list), for example: * * #channel1,#channel2,#channel3 key1,key2 + * + * If sort == 1, channels are sorted alphabetically, otherwise in the order + * they are received. + * In all cases, keyed channels are first in list. */ struct t_arraylist * -irc_join_split (struct t_irc_server *server, const char *join) +irc_join_split (struct t_irc_server *server, const char *join, int sort) { struct t_arraylist *arraylist; char **items, **channels, **keys; @@ -171,8 +213,10 @@ irc_join_split (struct t_irc_server *server, const char *join) arraylist = weechat_arraylist_new ( 16, 1, 0, - &irc_join_compare_cb, server, - &irc_join_free_cb, NULL); + (sort) ? &irc_join_compare_sort_cb : &irc_join_compare_cb, + server, + &irc_join_free_cb, + NULL); if (!arraylist) goto end; @@ -282,7 +326,7 @@ irc_join_add_channel (struct t_irc_server *server, if (!channel_name) return NULL; - arraylist = irc_join_split (server, join); + arraylist = irc_join_split (server, join, 0); if (!arraylist) return NULL; @@ -320,11 +364,11 @@ irc_join_add_channels (struct t_irc_server *server, char *new_join; int i, size; - arraylist = irc_join_split (server, join); + arraylist = irc_join_split (server, join, 0); if (!arraylist) return NULL; - arraylist2 = irc_join_split (server, join2); + arraylist2 = irc_join_split (server, join2, 0); if (!arraylist2) { weechat_arraylist_free (arraylist); @@ -424,7 +468,7 @@ irc_join_remove_channel (struct t_irc_server *server, if (!channel_name) return NULL; - arraylist = irc_join_split (server, join); + arraylist = irc_join_split (server, join, 0); if (!arraylist) return NULL; @@ -517,3 +561,53 @@ irc_join_save_channels_to_autojoin (struct t_irc_server *server) weechat_arraylist_free (arraylist); } + +/* + * Sorts channels. + */ + +char * +irc_join_sort_channels (struct t_irc_server *server, const char *join) +{ + struct t_arraylist *arraylist; + char *new_join; + + arraylist = irc_join_split (server, join, 1); + if (!arraylist) + return NULL; + + new_join = irc_join_build_string (arraylist); + + weechat_arraylist_free (arraylist); + + return new_join; +} + +/* + * Sorts channels in autojoin option of a server. + */ + +void +irc_join_sort_autojoin (struct t_irc_server *server) +{ + const char *ptr_autojoin; + char *new_autojoin; + + if (!server) + return; + + ptr_autojoin = IRC_SERVER_OPTION_STRING(server, IRC_SERVER_OPTION_AUTOJOIN); + + if (!ptr_autojoin || !ptr_autojoin[0]) + return; + + new_autojoin = irc_join_sort_channels ( + server, + IRC_SERVER_OPTION_STRING(server, IRC_SERVER_OPTION_AUTOJOIN)); + if (new_autojoin) + { + weechat_config_option_set (server->options[IRC_SERVER_OPTION_AUTOJOIN], + new_autojoin, 1); + free (new_autojoin); + } +} diff --git a/src/plugins/irc/irc-join.h b/src/plugins/irc/irc-join.h index eb1d396fd..bec30ee60 100644 --- a/src/plugins/irc/irc-join.h +++ b/src/plugins/irc/irc-join.h @@ -29,7 +29,8 @@ struct t_irc_join_channel }; extern struct t_arraylist *irc_join_split (struct t_irc_server *server, - const char *join); + const char *join, + int sort); extern char *irc_join_build_string (struct t_arraylist *arraylist); extern char *irc_join_add_channel (struct t_irc_server *server, const char *join, const char *channel_name, @@ -47,5 +48,8 @@ extern char *irc_join_remove_channel (struct t_irc_server *server, extern void irc_join_remove_channel_from_autojoin (struct t_irc_server *server, const char *channel_name); extern void irc_join_save_channels_to_autojoin (struct t_irc_server *server); +extern char *irc_join_sort_channels (struct t_irc_server *server, + const char *join); +extern void irc_join_sort_autojoin (struct t_irc_server *server); #endif /* WEECHAT_PLUGIN_IRC_JOIN_H */ |