summaryrefslogtreecommitdiff
path: root/src
diff options
context:
space:
mode:
authorSébastien Helleu <flashcode@flashtux.org>2023-04-27 22:58:02 +0200
committerSébastien Helleu <flashcode@flashtux.org>2023-04-27 22:59:24 +0200
commitf90d8667a4d6a008d3523e29617759231399ceff (patch)
treecd4fe89eadb14c0043792706be12d8c520f4c33e /src
parent882284325481759b7d7a3ea25ace791d4c5a1b21 (diff)
downloadweechat-f90d8667a4d6a008d3523e29617759231399ceff.zip
irc: add `${username}` in server options "nicks" and "username", change their default values to use it
Diffstat (limited to 'src')
-rw-r--r--src/plugins/irc/irc-config.c73
-rw-r--r--src/plugins/irc/irc-server.c15
2 files changed, 24 insertions, 64 deletions
diff --git a/src/plugins/irc/irc-config.c b/src/plugins/irc/irc-config.c
index 6e4be1606..a0bef9006 100644
--- a/src/plugins/irc/irc-config.c
+++ b/src/plugins/irc/irc-config.c
@@ -25,7 +25,6 @@
#include <string.h>
#include <time.h>
#include <limits.h>
-#include <pwd.h>
#include "../weechat-plugin.h"
#include "irc.h"
@@ -2128,9 +2127,11 @@ irc_config_server_new_option (struct t_config_file *config_file,
config_file, section,
option_name, "string",
N_("nicknames to use on server (separated by comma) "
- "(note: content is evaluated, see /help eval; server "
- "options are evaluated with ${irc_server.xxx} and "
- "${server} is replaced by the server name)"),
+ "(note: content is evaluated, see /help eval; ${username} "
+ "is replaced by system username (fallback to \"weechat\" "
+ "if not found), server options are evaluated with "
+ "${irc_server.xxx} and ${server} is replaced by the "
+ "server name)"),
NULL, 0, 0,
default_value, value,
null_value_allowed,
@@ -2167,9 +2168,11 @@ irc_config_server_new_option (struct t_config_file *config_file,
config_file, section,
option_name, "string",
N_("user name to use on server "
- "(note: content is evaluated, see /help eval; server "
- "options are evaluated with ${irc_server.xxx} and "
- "${server} is replaced by the server name)"),
+ "(note: content is evaluated, see /help eval; ${username} "
+ "is replaced by system username (fallback to \"weechat\" "
+ "if not found), server options are evaluated with "
+ "${irc_server.xxx} and ${server} is replaced by the "
+ "server name)"),
NULL, 0, 0,
default_value, value,
null_value_allowed,
@@ -2731,62 +2734,17 @@ irc_config_server_write_cb (const void *pointer, void *data,
void
irc_config_server_create_default_options (struct t_config_section *section)
{
- int i, length;
- char *nicks, *username, *realname, *default_value;
- struct passwd *my_passwd;
-
- nicks = NULL;
- username = NULL;
- realname = strdup ("");
-
- /* Get the user's name from /etc/passwd */
- if ((my_passwd = getpwuid (geteuid ())) != NULL)
- {
- length = (strlen (my_passwd->pw_name) + 4) * 5;
- nicks = malloc (length);
- if (nicks)
- {
- snprintf (nicks, length, "%s,%s1,%s2,%s3,%s4",
- my_passwd->pw_name,
- my_passwd->pw_name,
- my_passwd->pw_name,
- my_passwd->pw_name,
- my_passwd->pw_name);
- }
- username = strdup (my_passwd->pw_name);
- }
- else
- {
- /* default values if /etc/passwd can't be read */
- nicks = strdup (IRC_SERVER_DEFAULT_NICKS);
- username = strdup ("weechat");
- }
+ int i;
for (i = 0; i < IRC_SERVER_NUM_OPTIONS; i++)
{
- default_value = NULL;
- switch (i)
- {
- case IRC_SERVER_OPTION_NICKS:
- default_value = nicks;
- break;
- case IRC_SERVER_OPTION_USERNAME:
- default_value = username;
- break;
- case IRC_SERVER_OPTION_REALNAME:
- default_value = realname;
- break;
- }
- if (!default_value)
- default_value = irc_server_options[i][1];
-
irc_config_server_default[i] = irc_config_server_new_option (
irc_config_file,
section,
i,
irc_server_options[i][0],
irc_server_options[i][1],
- default_value,
+ irc_server_options[i][1],
0,
&irc_config_server_check_value_cb,
irc_server_options[i][0],
@@ -2795,13 +2753,6 @@ irc_config_server_create_default_options (struct t_config_section *section)
irc_server_options[i][0],
NULL);
}
-
- if (nicks)
- free (nicks);
- if (username)
- free (username);
- if (realname)
- free (realname);
}
/*
diff --git a/src/plugins/irc/irc-server.c b/src/plugins/irc/irc-server.c
index 35c39fd25..d61e8f938 100644
--- a/src/plugins/irc/irc-server.c
+++ b/src/plugins/irc/irc-server.c
@@ -43,6 +43,7 @@
#include <netinet/in.h>
#include <arpa/nameser.h>
#include <resolv.h>
+#include <pwd.h>
#include <gnutls/gnutls.h>
#include <gnutls/x509.h>
@@ -98,9 +99,11 @@ char *irc_server_options[IRC_SERVER_NUM_OPTIONS][2] =
{ "autoconnect", "off" },
{ "autoreconnect", "on" },
{ "autoreconnect_delay", "10" },
- { "nicks", "" },
+ { "nicks",
+ "${username},${username}2,${username}3,"
+ "${username}4,${username}5" },
{ "nicks_alternate", "on" },
- { "username", "" },
+ { "username", "${username}" },
{ "realname", "" },
{ "local_hostname", "" },
{ "usermode", "" },
@@ -361,6 +364,7 @@ irc_server_eval_expression (struct t_irc_server *server, const char *string)
{
struct t_hashtable *pointers, *extra_vars;
char *value;
+ struct passwd *my_passwd;
pointers = weechat_hashtable_new (
32,
@@ -381,6 +385,11 @@ irc_server_eval_expression (struct t_irc_server *server, const char *string)
weechat_hashtable_set (extra_vars, "server", server->name);
}
+ if ((my_passwd = getpwuid (geteuid ())) != NULL)
+ weechat_hashtable_set (extra_vars, "username", my_passwd->pw_name);
+ else
+ weechat_hashtable_set (extra_vars, "username", "weechat");
+
value = weechat_string_eval_expression (string,
pointers, extra_vars, NULL);
@@ -1954,7 +1963,7 @@ irc_server_alloc_with_url (const char *irc_url)
if (server_nicks)
{
snprintf (server_nicks, length,
- "%s,%s1,%s2,%s3,%s4",
+ "%s,%s2,%s3,%s4,%s5",
pos_nick, pos_nick, pos_nick, pos_nick, pos_nick);
weechat_config_option_set (
ptr_server->options[IRC_SERVER_OPTION_NICKS],