Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
51 changes: 51 additions & 0 deletions include/caps.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
/*
* IRC - Internet Relay Chat, include/caps.h
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 1, or (at your option)
* any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
*/

#ifndef CAPS_H
#define CAPS_H

/*
* IRCv3 client capabilities (CAP).
*
* To add a capability: define a new CAP_ bit here, add an entry to
* clientcaps[] in caps.c, and check it with HasCap() where the
* capability changes behavior.
*/

#define CAP_CAP_NOTIFY (1ULL << 0) /* cap-notify */

#define CAPF_STICKY_302 0x0001 /* cannot be disabled after CAP LS 302 */

typedef struct ClientCap aClientCap;

struct ClientCap
{
char *name;
uint64_t bit;
char *value; /* advertised as name=value with CAP LS 302 */
int flags;
};

#define HasCap(x, c) (MyConnect(x) && ((x)->caps & (c)))
#define IsCapNegotiating(x) ((x)->flags & FLAGS_CAPNEGO)
#define SetCapNegotiating(x) ((x)->flags |= FLAGS_CAPNEGO)
#define ClearCapNegotiating(x) ((x)->flags &= ~FLAGS_CAPNEGO)

extern int m_cap(aClient *, aClient *, int, char **);

#endif
1 change: 1 addition & 0 deletions include/h.h
Original file line number Diff line number Diff line change
Expand Up @@ -267,6 +267,7 @@ extern void verbose_to_opers(aClient *sptr, aChannel *chptr, char *cmd, char *re

extern int m_umode(aClient *, aClient *, int, char **);
extern int m_names(aClient *, aClient *, int, char **);
extern int register_user(aClient *, aClient *, char *, char *, char *);
extern void send_umode(aClient *, aClient *, long, long, char *, int);
extern int del_silence(aClient *, char *);

Expand Down
3 changes: 3 additions & 0 deletions include/msg.h
Original file line number Diff line number Diff line change
Expand Up @@ -131,6 +131,7 @@
#define MSG_POST "POST" /* POST */
#define MSG_CHECK "CHECK" /* CHECK */

#define MSG_CAP "CAP" /* IRCv3 capability negotiation */
#define MSG_WEBIRC "WEBIRC" /* WEBIRC */

#define MAXPARA 15
Expand Down Expand Up @@ -233,6 +234,7 @@ extern int m_put(aClient *, aClient *, int, char **);
extern int m_post(aClient *, aClient *, int, char **);
extern int m_check(aClient *, aClient *, int, char **);
extern int m_webirc(aClient *, aClient *, int, char **);
extern int m_cap(aClient *, aClient *, int, char **);
extern int m_spamops(aClient *, aClient *, int, char **);
extern int m_sf(aClient *, aClient *, int, char **);
extern int m_aj(aClient *, aClient *, int, char **);
Expand Down Expand Up @@ -383,6 +385,7 @@ struct Message msgtab[] =
{"AJ", m_aj, MAXPARA, 0, 0},
{"SJR", m_sjr, MAXPARA, MF_ALIAS, AII_NS},
{MSG_WEBIRC, m_webirc, MAXPARA, MF_UNREG, 0},
{MSG_CAP, m_cap, MAXPARA, MF_UNREG, 0},
{ 0 }
};
#ifdef __clang__
Expand Down
1 change: 1 addition & 0 deletions include/numeric.h
Original file line number Diff line number Diff line change
Expand Up @@ -176,6 +176,7 @@
#define ERR_TOOMANYTARGETS 407
#define ERR_NOCTRLSONCHAN 408
#define ERR_NOORIGIN 409
#define ERR_INVALIDCAPCMD 410

#define ERR_NORECIPIENT 411
#define ERR_NOTEXTTOSEND 412
Expand Down
4 changes: 4 additions & 0 deletions include/struct.h
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@
#include <netdb.h>
#if defined( HAVE_STDDEF_H )
#include <stddef.h>
#include <stdint.h>
#endif
#ifdef ORATIMING
#include <sys/time.h>
Expand Down Expand Up @@ -229,6 +230,7 @@ typedef struct SServicesTag ServicesTag;
#define FLAGS_NONL 0x002000 /* No \n in buffer */
#define FLAGS_NORMALEX 0x004000 /* Client exited normally */
#define FLAGS_SENDQEX 0x008000 /* Sendq exceeded */
#define FLAGS_CAPNEGO 0x010000 /* registration held until CAP END */
#define FLAGS_ULINE 0x020000 /* client is U-lined */
#define FLAGS_USERBURST 0x040000 /* server in nick/channel netburst */
#define FLAGS_TOPICBURST 0x080000 /* server in topic netburst */
Expand Down Expand Up @@ -1057,6 +1059,8 @@ struct Client
int sockerr; /* what was the last error returned for
* this socket? */
int capabilities; /* what this server/client supports */
uint64_t caps; /* IRCv3 capabilities enabled (CAP_*) */
int cap_version; /* CAP LS version, e.g. 302 */
aClass *class; /* our current effective class */

#ifdef MSG_TARGET_LIMIT
Expand Down
2 changes: 1 addition & 1 deletion src/Makefile.in
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ SOURCES = blalloc.c bsd.c channel.c clientlist.c clones.c confparse.c \
probability.c res.c s_auth.c s_bsd.c s_conf.c s_debug.c s_err.c \
s_misc.c s_numeric.c s_serv.c s_user.c sbuf.c scache.c send.c \
struct.c support.c throttle.c userban.c whowas.c zlink.c ssl.c \
bitncmp.c inet_parse_cidr.c m_webirc.c spamfilter.c \
bitncmp.c inet_parse_cidr.c m_webirc.c spamfilter.c caps.c \
$(ENGINE) $(CRYPTO) $(RES_SRC)

OBJECTS = $(SOURCES:.c=.o) version.o
Expand Down
240 changes: 240 additions & 0 deletions src/caps.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,240 @@
/*
* IRC - Internet Relay Chat, src/caps.c
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 1, or (at your option)
* any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
*/

/*
* IRCv3 client capability negotiation.
* See https://ircv3.net/specs/extensions/capability-negotiation
*/

#include "struct.h"
#include "common.h"
#include "sys.h"
#include "numeric.h"
#include "h.h"
#include "caps.h"

/* leave room for ":server CAP nick LS * :" within the 512 byte limit */
#define CAP_LINELEN 400

static aClientCap clientcaps[] =
{
{ "cap-notify", CAP_CAP_NOTIFY, NULL, CAPF_STICKY_302 },
{ NULL, 0, NULL, 0 }
};

static aClientCap *
find_cap(char *name)
{
aClientCap *cap;

for (cap = clientcaps; cap->name; cap++)
if (!strcmp(cap->name, name))
return cap;
return NULL;
}

static char *
cap_nick(aClient *sptr)
{
return *sptr->name ? sptr->name : "*";
}

/*
* send_caps - send the capabilities in mask as CAP <subcmd>, split over
* several lines if needed. CAP 302 clients get a "*" continuation
* marker on all but the last line, and name=value pairs if values is set
* (only allowed for LS and NEW).
*/
static void
send_caps(aClient *sptr, char *subcmd, uint64_t mask, int values)
{
char buf[BUFSIZE], tok[BUFSIZE];
aClientCap *cap;
int v302 = (sptr->cap_version >= 302);
int len = 0, n;

*buf = '\0';
for (cap = clientcaps; cap->name; cap++)
{
if (!(mask & cap->bit))
continue;

if (v302 && values && cap->value)
ircsnprintf(tok, sizeof(tok), "%s=%s", cap->name, cap->value);
else
strncpyzt(tok, cap->name, sizeof(tok));
n = strlen(tok);

if (len && len + 1 + n > CAP_LINELEN)
{
sendto_one(sptr, ":%s CAP %s %s %s:%s", me.name, cap_nick(sptr),
subcmd, v302 ? "* " : "", buf);
len = 0;
*buf = '\0';
}

if (len)
buf[len++] = ' ';
strcpy(buf + len, tok);
len += n;
}

sendto_one(sptr, ":%s CAP %s %s :%s", me.name, cap_nick(sptr), subcmd,
buf);
}

static int
cap_ls(aClient *sptr, int parc, char *parv[])
{
if (!IsRegistered(sptr))
SetCapNegotiating(sptr);

if (parc > 2 && atoi(parv[2]) >= 302 && sptr->cap_version < 302)
{
sptr->cap_version = 302;
sptr->caps |= CAP_CAP_NOTIFY;
}

send_caps(sptr, "LS", ~(uint64_t)0, 1);
return 0;
}

static int
cap_list(aClient *sptr)
{
send_caps(sptr, "LIST", sptr->caps, 0);
return 0;
}

/*
* cap_req - the request is applied atomically: either every capability
* in it is changed and the whole request is ACKed, or none are and it
* is NAKed.
*/
static int
cap_req(aClient *sptr, int parc, char *parv[])
{
char reqbuf[BUFSIZE], *tok, *p = NULL;
uint64_t newcaps = sptr->caps;
aClientCap *cap;
int neg;

if (parc < 3 || BadPtr(parv[2]))
{
sendto_one(sptr, err_str(ERR_NEEDMOREPARAMS), me.name,
cap_nick(sptr), "CAP");
return 0;
}

for (tok = parv[2]; *tok == ' '; tok++)
;
if (!*tok)
{
sendto_one(sptr, err_str(ERR_NEEDMOREPARAMS), me.name,
cap_nick(sptr), "CAP");
return 0;
}

if (!IsRegistered(sptr))
SetCapNegotiating(sptr);

/* too long to echo back in a single ACK, reject it unchanged */
if (strlen(parv[2]) > CAP_LINELEN)
{
strncpyzt(reqbuf, parv[2], CAP_LINELEN + 1);
if ((tok = strrchr(reqbuf, ' ')))
*tok = '\0';
sendto_one(sptr, ":%s CAP %s NAK :%s", me.name, cap_nick(sptr),
reqbuf);
return 0;
}

strncpyzt(reqbuf, parv[2], sizeof(reqbuf));
for (tok = strtoken(&p, reqbuf, " "); tok; tok = strtoken(&p, NULL, " "))
{
neg = (*tok == '-');
if (neg)
tok++;

cap = find_cap(tok);
if (!cap || (neg && (cap->flags & CAPF_STICKY_302) &&
sptr->cap_version >= 302))
{
sendto_one(sptr, ":%s CAP %s NAK :%s", me.name, cap_nick(sptr),
parv[2]);
return 0;
}

if (neg)
newcaps &= ~cap->bit;
else
newcaps |= cap->bit;
}

sptr->caps = newcaps;
sendto_one(sptr, ":%s CAP %s ACK :%s", me.name, cap_nick(sptr), parv[2]);
return 0;
}

static int
cap_end(aClient *cptr, aClient *sptr)
{
if (IsRegistered(sptr) || !IsCapNegotiating(sptr))
return 0;

ClearCapNegotiating(sptr);

/* NICK and USER already received, finish registration now */
if (*sptr->name && sptr->user)
return register_user(cptr, sptr, sptr->name, sptr->user->username,
NULL);
return 0;
}

/*
* m_cap
* parv[0] = sender prefix
* parv[1] = subcommand (LS, LIST, REQ, END)
* parv[2] = LS version or REQ capability list
*/
int
m_cap(aClient *cptr, aClient *sptr, int parc, char *parv[])
{
if (!MyConnect(sptr) || IsServer(sptr))
return 0;

if (parc < 2 || BadPtr(parv[1]))
{
sendto_one(sptr, err_str(ERR_NEEDMOREPARAMS), me.name,
cap_nick(sptr), "CAP");
return 0;
}

if (!mycmp(parv[1], "LS"))
return cap_ls(sptr, parc, parv);
if (!mycmp(parv[1], "LIST"))
return cap_list(sptr);
if (!mycmp(parv[1], "REQ"))
return cap_req(sptr, parc, parv);
if (!mycmp(parv[1], "END"))
return cap_end(cptr, sptr);

sendto_one(sptr, err_str(ERR_INVALIDCAPCMD), me.name, cap_nick(sptr),
parv[1]);
return 0;
}
4 changes: 2 additions & 2 deletions src/m_nick.c
Original file line number Diff line number Diff line change
Expand Up @@ -31,11 +31,11 @@
#include "h.h"
#include "userban.h"
#include "hooks.h"
#include "caps.h"

extern int do_user(char *, aClient *, aClient *, char *, char *, char *,
unsigned long, char *, char *);

extern int register_user(aClient *, aClient *, char *, char *, char *);
extern int del_dccallow(aClient *, aClient *, int);

extern int is_xflags_exempted(aClient *sptr, aChannel *chptr);
Expand Down Expand Up @@ -639,7 +639,7 @@ int m_nick(aClient *cptr, aClient *sptr, int parc, char *parv[])

strcpy(sptr->name, nick);
sptr->tsinfo = timeofday;
if (sptr->user)
if (sptr->user && !IsCapNegotiating(sptr))
{
/* USER already received, now we have NICK */

Expand Down
2 changes: 1 addition & 1 deletion src/s_err.c
Original file line number Diff line number Diff line change
Expand Up @@ -454,7 +454,7 @@ static char *replies[] =
/* 408 ERR_NOCTRLSONCHAN */ ":%s 408 %s %s :You cannot use control codes "
"on this channel. Not sent: %s",
/* 409 ERR_NOORIGIN */ ":%s 409 %s :No origin specified",
/* 410 */ NULL,
/* 410 ERR_INVALIDCAPCMD */ ":%s 410 %s %s :Invalid CAP subcommand",
/* 411 ERR_NORECIPIENT */ ":%s 411 %s :No recipient given (%s)",
/* 412 ERR_NOTEXTOSEND */ ":%s 412 %s :No text to send",
/* 413 ERR_NOTOPLEVEL */ ":%s 413 %s %s :No toplevel domain specified",
Expand Down
Loading
Loading