Skip to content

Commit

Permalink
irc: add support for CAP account-notify
Browse files Browse the repository at this point in the history
  • Loading branch information
maxteufel committed Dec 7, 2014
1 parent 72e6b3d commit ff2f840
Show file tree
Hide file tree
Showing 8 changed files with 185 additions and 23 deletions.
17 changes: 8 additions & 9 deletions src/plugins/irc/irc-channel.c
Original file line number Diff line number Diff line change
Expand Up @@ -292,7 +292,7 @@ irc_channel_new (struct t_irc_server *server, int channel_type,
WEECHAT_HASHTABLE_STRING,
NULL,
NULL);
new_channel->checking_away = 0;
new_channel->checking_whox = 0;
new_channel->away_message = NULL;
new_channel->has_quit_server = 0;
new_channel->cycle = 0;
Expand Down Expand Up @@ -549,22 +549,21 @@ irc_channel_remove_away (struct t_irc_server *server,
*/

void
irc_channel_check_away (struct t_irc_server *server,
irc_channel_check_whox (struct t_irc_server *server,
struct t_irc_channel *channel)
{
if ((channel->type == IRC_CHANNEL_TYPE_CHANNEL) && channel->nicks)
{
if (server->cap_away_notify
|| server->cap_account_notify
|| ((IRC_SERVER_OPTION_INTEGER(server, IRC_SERVER_OPTION_AWAY_CHECK) > 0)
&& ((IRC_SERVER_OPTION_INTEGER(server, IRC_SERVER_OPTION_AWAY_CHECK_MAX_NICKS) == 0)
|| (channel->nicks_count <= IRC_SERVER_OPTION_INTEGER(server, IRC_SERVER_OPTION_AWAY_CHECK_MAX_NICKS)))))
{
channel->checking_away++;
channel->checking_whox++;
irc_server_sendf (server, IRC_SERVER_SEND_OUTQ_PRIO_LOW, NULL,
"WHO %s", channel->name);
"WHO %s %%cuhsnfdar", channel->name);
}
else
irc_channel_remove_away (server, channel);
}
}

Expand Down Expand Up @@ -1303,7 +1302,7 @@ irc_channel_hdata_channel_cb (void *data, const char *hdata_name)
WEECHAT_HDATA_VAR(struct t_irc_channel, limit, INTEGER, 0, NULL, NULL);
WEECHAT_HDATA_VAR(struct t_irc_channel, key, STRING, 0, NULL, NULL);
WEECHAT_HDATA_VAR(struct t_irc_channel, join_msg_received, HASHTABLE, 0, NULL, NULL);
WEECHAT_HDATA_VAR(struct t_irc_channel, checking_away, INTEGER, 0, NULL, NULL);
WEECHAT_HDATA_VAR(struct t_irc_channel, checking_whox, INTEGER, 0, NULL, NULL);
WEECHAT_HDATA_VAR(struct t_irc_channel, away_message, STRING, 0, NULL, NULL);
WEECHAT_HDATA_VAR(struct t_irc_channel, has_quit_server, INTEGER, 0, NULL, NULL);
WEECHAT_HDATA_VAR(struct t_irc_channel, cycle, INTEGER, 0, NULL, NULL);
Expand Down Expand Up @@ -1400,7 +1399,7 @@ irc_channel_add_to_infolist (struct t_infolist *infolist,
if (!weechat_infolist_new_var_string (ptr_item, "join_msg_received",
weechat_hashtable_get_string (channel->join_msg_received, "keys")))
return 0;
if (!weechat_infolist_new_var_integer (ptr_item, "checking_away", channel->checking_away))
if (!weechat_infolist_new_var_integer (ptr_item, "checking_whox", channel->checking_whox))
return 0;
if (!weechat_infolist_new_var_string (ptr_item, "away_message", channel->away_message))
return 0;
Expand Down Expand Up @@ -1484,7 +1483,7 @@ irc_channel_print_log (struct t_irc_channel *channel)
channel->join_msg_received,
weechat_hashtable_get_string (channel->join_msg_received,
"keys_values"));
weechat_log_printf (" checking_away. . . . . . : %d", channel->checking_away);
weechat_log_printf (" checking_whox. . . . . . : %d", channel->checking_whox);
weechat_log_printf (" away_message . . . . . . : '%s'", channel->away_message);
weechat_log_printf (" has_quit_server. . . . . : %d", channel->has_quit_server);
weechat_log_printf (" cycle. . . . . . . . . . : %d", channel->cycle);
Expand Down
4 changes: 2 additions & 2 deletions src/plugins/irc/irc-channel.h
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@ struct t_irc_channel
struct t_hashtable *join_msg_received; /* messages received after join: */
/* 353=names, 366=names count, */
/* 332/333=topic, 329=creation date */
int checking_away; /* = 1 if checking away with WHO cmd */
int checking_whox; /* = 1 if checking WHOX */
char *away_message; /* to display away only once in pv */
int has_quit_server; /* =1 if nick has quit (pv only), to */
/* display message when he's back */
Expand Down Expand Up @@ -101,7 +101,7 @@ extern const char *irc_channel_get_auto_chantype (struct t_irc_server *server,
const char *channel_name);
extern void irc_channel_remove_away (struct t_irc_server *server,
struct t_irc_channel *channel);
extern void irc_channel_check_away (struct t_irc_server *server,
extern void irc_channel_check_whox (struct t_irc_server *server,
struct t_irc_channel *channel);
extern void irc_channel_set_away (struct t_irc_server *server,
struct t_irc_channel *channel,
Expand Down
13 changes: 12 additions & 1 deletion src/plugins/irc/irc-nick.c
Original file line number Diff line number Diff line change
Expand Up @@ -624,7 +624,7 @@ irc_nick_nicklist_set_color_all ()
struct t_irc_nick *
irc_nick_new (struct t_irc_server *server, struct t_irc_channel *channel,
const char *nickname, const char *host, const char *prefixes,
int away)
int away, char *account)
{
struct t_irc_nick *new_nick, *ptr_nick;
int length;
Expand All @@ -642,12 +642,16 @@ irc_nick_new (struct t_irc_server *server, struct t_irc_channel *channel,
/* save away status from existing nick (before removing it) */
away = ptr_nick->away;

/* save account from existing nick (before removing it) */
account = ptr_nick->account;

/* remove old nick from nicklist */
irc_nick_nicklist_remove (server, channel, ptr_nick);

/* update nick */
irc_nick_set_prefixes (server, ptr_nick, prefixes);
ptr_nick->away = away;
ptr_nick->account = account;

/* add new nick in nicklist */
irc_nick_nicklist_add (server, channel, ptr_nick);
Expand All @@ -662,6 +666,7 @@ irc_nick_new (struct t_irc_server *server, struct t_irc_channel *channel,
/* initialize new nick */
new_nick->name = strdup (nickname);
new_nick->host = (host) ? strdup (host) : NULL;
new_nick->account = (account) ? strdup (account) : NULL;
length = strlen (irc_server_get_prefix_chars (server));
new_nick->prefixes = malloc (length + 1);
if (!new_nick->name || !new_nick->prefixes)
Expand All @@ -670,6 +675,8 @@ irc_nick_new (struct t_irc_server *server, struct t_irc_channel *channel,
free (new_nick->name);
if (new_nick->host)
free (new_nick->host);
if (new_nick->account)
free (new_nick->account);
if (new_nick->prefixes)
free (new_nick->prefixes);
free (new_nick);
Expand Down Expand Up @@ -810,6 +817,8 @@ irc_nick_free (struct t_irc_server *server, struct t_irc_channel *channel,
free (nick->host);
if (nick->prefixes)
free (nick->prefixes);
if (nick->account)
free (nick->account);
if (nick->color)
free (nick->color);

Expand Down Expand Up @@ -1167,6 +1176,8 @@ irc_nick_add_to_infolist (struct t_infolist *infolist,
return 0;
if (!weechat_infolist_new_var_integer (ptr_item, "away", nick->away))
return 0;
if (!weechat_infolist_new_var_string (ptr_item, "account", nick->account))
return 0;
if (!weechat_infolist_new_var_string (ptr_item, "color", nick->color))
return 0;

Expand Down
4 changes: 3 additions & 1 deletion src/plugins/irc/irc-nick.h
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,7 @@ struct t_irc_nick
char prefix[2]; /* current prefix (higher prefix set in */
/* prefixes) */
int away; /* 1 if nick is away */
char *account; /* account name of the user */
char *color; /* color for nickname */
struct t_irc_nick *prev_nick; /* link to previous nick on channel */
struct t_irc_nick *next_nick; /* link to next nick on channel */
Expand All @@ -64,7 +65,8 @@ extern struct t_irc_nick *irc_nick_new (struct t_irc_server *server,
const char *nickname,
const char *host,
const char *prefixes,
int away);
int away,
char *account);
extern void irc_nick_change (struct t_irc_server *server,
struct t_irc_channel *channel,
struct t_irc_nick *nick, const char *new_nick);
Expand Down
155 changes: 148 additions & 7 deletions src/plugins/irc/irc-protocol.c
Original file line number Diff line number Diff line change
Expand Up @@ -291,6 +291,36 @@ IRC_PROTOCOL_CALLBACK(away)
return WEECHAT_RC_OK;
}

/*
* Callback for the IRC message "ACCOUNT": account info about a nick
* (with capability "account-notify").
*
* Message looks like:
* :nick!user@host ACCOUNT *
* :nick!user@host ACCOUNT accountname
*/

IRC_PROTOCOL_CALLBACK(account)
{
struct t_irc_channel *ptr_channel;
struct t_irc_nick *ptr_nick;

IRC_PROTOCOL_MIN_ARGS(3);

for (ptr_channel = server->channels; ptr_channel;
ptr_channel = ptr_channel->next_channel)
{
ptr_nick = irc_nick_search (server, ptr_channel, nick);
if (ptr_nick)
{
free (ptr_nick->account);
ptr_nick->account = strdup (argv[2]);
}
}

return WEECHAT_RC_OK;
}

/*
* Callback for the IRC message "CAP": client capability.
*
Expand Down Expand Up @@ -432,6 +462,10 @@ IRC_PROTOCOL_CALLBACK(cap)
{
server->cap_away_notify = 1;
}
else if (strcmp (caps_supported[i], "account-notify") == 0)
{
server->cap_account_notify = 1;
}
}
weechat_string_free_split (caps_supported);
}
Expand Down Expand Up @@ -706,11 +740,14 @@ IRC_PROTOCOL_CALLBACK(join)
}
ptr_channel->limit = 0;
weechat_hashtable_remove_all (ptr_channel->join_msg_received);
ptr_channel->checking_away = 0;
ptr_channel->checking_whox = 0;
}

/* add nick in channel */
ptr_nick = irc_nick_new (server, ptr_channel, nick, address, NULL, 0);
if (pos_account)
ptr_nick = irc_nick_new (server, ptr_channel, nick, address, NULL, 0, pos_account);
else
ptr_nick = irc_nick_new (server, ptr_channel, nick, address, NULL, 0, "*");

/* rename the nick if it was in list with a different case */
irc_channel_nick_speaking_rename_if_present (server, ptr_channel, nick);
Expand Down Expand Up @@ -2862,9 +2899,9 @@ IRC_PROTOCOL_CALLBACK(315)
IRC_PROTOCOL_MIN_ARGS(5);

ptr_channel = irc_channel_search (server, argv[3]);
if (ptr_channel && (ptr_channel->checking_away > 0))
if (ptr_channel && (ptr_channel->checking_whox > 0))
{
ptr_channel->checking_away--;
ptr_channel->checking_whox--;
}
else
{
Expand Down Expand Up @@ -4025,7 +4062,7 @@ IRC_PROTOCOL_CALLBACK(352)
}

/* display output of who (manual who from user) */
if (!ptr_channel || (ptr_channel->checking_away <= 0))
if (!ptr_channel || (ptr_channel->checking_whox <= 0))
{
weechat_printf_date_tags (
irc_msgbuffer_get_target_buffer (
Expand Down Expand Up @@ -4134,7 +4171,7 @@ IRC_PROTOCOL_CALLBACK(353)
if (ptr_channel && ptr_channel->nicks)
{
if (!irc_nick_new (server, ptr_channel, nickname, pos_host,
prefixes, 0))
prefixes, 0, "*"))
{
weechat_printf (
server->buffer,
Expand Down Expand Up @@ -4199,6 +4236,108 @@ IRC_PROTOCOL_CALLBACK(353)
return WEECHAT_RC_OK;
}

/*
* Callback for the IRC message "354": WHOX output
*
* Message looks like:
* :server 354 mynick #channel user host server nick status hopcount account :GECOS Information
*/

IRC_PROTOCOL_CALLBACK(354)
{
char *pos_attr, *pos_hopcount, *pos_account, *pos_realname;
int length;
struct t_irc_channel *ptr_channel;
struct t_irc_nick *ptr_nick;

IRC_PROTOCOL_MIN_ARGS(5);

/* silently ignore malformed 354 message (missing infos) */
if (argc < 11)
return WEECHAT_RC_OK;

pos_attr = argv[8];
pos_hopcount = argv[9];
pos_account = NULL;
pos_realname = (argc > 11) ?
((argv_eol[11][0] == ':') ? argv_eol[11] + 1 : argv_eol[11]) : NULL;

if (argv[10][0] != '0')
pos_account = argv[10];

ptr_channel = irc_channel_search (server, argv[3]);
ptr_nick = (ptr_channel) ?
irc_nick_search (server, ptr_channel, argv[7]) : NULL;

/* update host for nick */
if (ptr_nick)
{
if (ptr_nick->host)
free (ptr_nick->host);
length = strlen (argv[4]) + 1 + strlen (argv[5]) + 1;
ptr_nick->host = malloc (length);
if (ptr_nick->host)
snprintf (ptr_nick->host, length, "%s@%s", argv[4], argv[5]);
}

/* update away flag for nick */
if (ptr_channel && ptr_nick && pos_attr
&& (server->cap_away_notify
|| ((IRC_SERVER_OPTION_INTEGER(server, IRC_SERVER_OPTION_AWAY_CHECK) > 0)
&& ((IRC_SERVER_OPTION_INTEGER(server, IRC_SERVER_OPTION_AWAY_CHECK_MAX_NICKS) == 0)
|| (ptr_channel->nicks_count <= IRC_SERVER_OPTION_INTEGER(server, IRC_SERVER_OPTION_AWAY_CHECK_MAX_NICKS))))))
{
irc_nick_set_away (server, ptr_channel, ptr_nick,
(pos_attr[0] == 'G') ? 1 : 0);
}
else
{
irc_nick_set_away (server, ptr_channel, ptr_nick, 0);
}

/* update account flag for nick */
if (ptr_channel && ptr_nick && pos_account && server->cap_account_notify)
ptr_nick->account = (pos_account) ? strdup (pos_account) : strdup ("*");
else
ptr_nick->account = strdup ("*");

/* display output of who (manual who from user) */
if (!ptr_channel || (ptr_channel->checking_whox <= 0))
{
weechat_printf_date_tags (
irc_msgbuffer_get_target_buffer (
server, NULL, command, "who", NULL),
date,
irc_protocol_tags (command, "irc_numeric", NULL, NULL),
"%s%s[%s%s%s] %s%s %s%s%s%s%s%s(%s%s@%s%s)%s %s%s%s%s(%s)",
weechat_prefix("network"),
IRC_COLOR_CHAT_DELIMITERS,
IRC_COLOR_CHAT_CHANNEL,
argv[3],
IRC_COLOR_CHAT_DELIMITERS,
irc_nick_color_for_msg (server, 1, NULL, argv[7]),
argv[7],
IRC_COLOR_CHAT_DELIMITERS,
(pos_account) ? "[" : "",
(pos_account) ? IRC_COLOR_CHAT_HOST : "",
(pos_account) ? pos_account : "",
(pos_account) ? IRC_COLOR_CHAT_DELIMITERS : "",
(pos_account) ? "] " : "",
IRC_COLOR_CHAT_HOST,
argv[4],
argv[5],
IRC_COLOR_CHAT_DELIMITERS,
IRC_COLOR_RESET,
(pos_attr) ? pos_attr : "",
(pos_attr) ? " " : "",
(pos_hopcount) ? pos_hopcount : "",
(pos_hopcount) ? " " : "",
(pos_realname) ? pos_realname : "");
}

return WEECHAT_RC_OK;
}

/*
* Callback for the IRC message "366": end of /names list.
*
Expand Down Expand Up @@ -4390,7 +4529,7 @@ IRC_PROTOCOL_CALLBACK(366)
{
irc_command_mode_server (server, "MODE", ptr_channel, NULL,
IRC_SERVER_SEND_OUTQ_PRIO_LOW);
irc_channel_check_away (server, ptr_channel);
irc_channel_check_whox (server, ptr_channel);
}
}
else
Expand Down Expand Up @@ -5260,6 +5399,7 @@ irc_protocol_recv_command (struct t_irc_server *server,
struct t_irc_protocol_msg irc_protocol_messages[] =
{ { "authenticate", /* authenticate */ 1, 0, &irc_protocol_cb_authenticate },
{ "away", /* away (cap away-notify) */ 1, 0, &irc_protocol_cb_away },
{ "account", /* account (cap account-notify) */ 1, 0, &irc_protocol_cb_account },
{ "cap", /* client capability */ 1, 0, &irc_protocol_cb_cap },
{ "error", /* error received from IRC server */ 1, 0, &irc_protocol_cb_error },
{ "invite", /* invite a nick on a channel */ 1, 0, &irc_protocol_cb_invite },
Expand Down Expand Up @@ -5325,6 +5465,7 @@ irc_protocol_recv_command (struct t_irc_server *server,
{ "351", /* server version */ 1, 0, &irc_protocol_cb_351 },
{ "352", /* who */ 1, 0, &irc_protocol_cb_352 },
{ "353", /* list of nicks on channel */ 1, 0, &irc_protocol_cb_353 },
{ "354", /* whox */ 1, 0, &irc_protocol_cb_354 },
{ "366", /* end of /names list */ 1, 0, &irc_protocol_cb_366 },
{ "367", /* banlist */ 1, 0, &irc_protocol_cb_367 },
{ "368", /* end of banlist */ 1, 0, &irc_protocol_cb_368 },
Expand Down
Loading

0 comments on commit ff2f840

Please sign in to comment.