00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00025
00026
00027
00028
00029
00030 #include "stdinc.h"
00031 #include "handlers.h"
00032 #include "client.h"
00033 #include "hash.h"
00034 #include "ircd.h"
00035 #include "numeric.h"
00036 #include "s_conf.h"
00037 #include "s_user.h"
00038 #include "s_serv.h"
00039 #include "send.h"
00040 #include "msg.h"
00041 #include "parse.h"
00042 #include "modules.h"
00043 #include "common.h"
00044 #include "packet.h"
00045 #include "irc_string.h"
00046
00047 static void m_cap(struct Client *, struct Client *, int, char *[]);
00048
00049 struct Message cap_msgtab = {
00050 "CAP", 0, 0, 2, 0, MFLG_SLOW, 0,
00051 { m_cap, m_cap, m_ignore, m_ignore, m_cap, m_ignore }
00052 };
00053
00054 #ifndef STATIC_MODULES
00055 void
00056 _modinit(void)
00057 {
00058 mod_add_cmd(&cap_msgtab);
00059 }
00060
00061 void
00062 _moddeinit(void)
00063 {
00064 mod_del_cmd(&cap_msgtab);
00065 }
00066
00067 const char *_version = "$Revision: 33 $";
00068 #endif
00069
00070 #define CAPFL_HIDDEN 0x0001
00071 #define CAPFL_PROHIBIT 0x0002
00072 #define CAPFL_PROTO 0x0004
00073 #define CAPFL_STICKY 0x0008
00074
00075 typedef int (*bqcmp)(const void *, const void *);
00076
00077 static struct capabilities
00078 {
00079 unsigned int cap;
00080 unsigned int flags;
00081 const char *name;
00082 size_t namelen;
00083 } capab_list[] = {
00084 #define _CAP(cap, flags, name) \
00085 { (cap), (flags), (name), sizeof(name) - 1 }
00086 _CAP(CAP_MULTI_PREFIX, 0, "multi-prefix")
00087 #undef _CAP
00088 };
00089
00090 #define CAPAB_LIST_LEN (sizeof(capab_list) / sizeof(struct capabilities))
00091
00092 static int
00093 capab_sort(const struct capabilities *cap1, const struct capabilities *cap2)
00094 {
00095 return strcasecmp(cap1->name, cap2->name);
00096 }
00097
00098 static int
00099 capab_search(const char *key, const struct capabilities *cap)
00100 {
00101 const char *rb = cap->name;
00102
00103 while (ToLower(*key) == ToLower(*rb))
00104 if (*key++ == '\0')
00105 return 0;
00106 else
00107 rb++;
00108
00109
00110
00111
00112
00113 return (IsSpace(*key) && *rb == '\0') ? 0 : (ToLower(*key) - ToLower(*rb));
00114 }
00115
00116 static struct capabilities *
00117 find_cap(const char **caplist_p, int *neg_p)
00118 {
00119 static int inited = 0;
00120 const char *caplist = *caplist_p;
00121 struct capabilities *cap = 0;
00122
00123 *neg_p = 0;
00124
00125 if (!inited)
00126 {
00127
00128 qsort(capab_list, CAPAB_LIST_LEN, sizeof(struct capabilities), (bqcmp)capab_sort);
00129 ++inited;
00130 }
00131
00132
00133 while (*caplist && IsSpace(*caplist))
00134 ++caplist;
00135
00136
00137 if (*caplist == '-') {
00138 ++caplist;
00139 *neg_p = 1;
00140 }
00141
00142
00143 if (*caplist) {
00144 if (!(cap = bsearch(caplist, capab_list, CAPAB_LIST_LEN,
00145 sizeof(struct capabilities),
00146 (bqcmp)capab_search)))
00147 {
00148
00149 while (*caplist && !IsSpace(*caplist))
00150 ++caplist;
00151 }
00152 else
00153 caplist += cap->namelen;
00154 }
00155
00156 assert(caplist != *caplist_p || !*caplist);
00157
00158
00159 *caplist_p = *caplist ? caplist : 0;
00160
00161 return cap;
00162 }
00163
00164
00165
00166
00167
00168
00169
00170
00171
00172 static int
00173 send_caplist(struct Client *sptr, unsigned int set,
00174 unsigned int rem, const char *subcmd)
00175 {
00176 char capbuf[IRCD_BUFSIZE] = "", pfx[16];
00177 char cmdbuf[IRCD_BUFSIZE] = "";
00178 unsigned int i, loc, len, flags, pfx_len, clen;
00179
00180
00181 clen = snprintf(cmdbuf, sizeof(capbuf), ":%s CAP %s %s ", me.name,
00182 sptr->name[0] ? sptr->name : "*", subcmd);
00183
00184 for (i = 0, loc = 0; i < CAPAB_LIST_LEN; ++i)
00185 {
00186 flags = capab_list[i].flags;
00187
00188
00189
00190
00191
00192
00193
00194 if (!(rem && (rem & capab_list[i].cap)) &&
00195 !(set && (set & capab_list[i].cap)) &&
00196 (rem || set || (flags & CAPFL_HIDDEN)))
00197 continue;
00198
00199
00200 pfx_len = 0;
00201
00202 if (loc)
00203 pfx[pfx_len++] = ' ';
00204 if (rem && (rem & capab_list[i].cap))
00205 pfx[pfx_len++] = '-';
00206 else
00207 {
00208 if (flags & CAPFL_PROTO)
00209 pfx[pfx_len++] = '~';
00210 if (flags & CAPFL_STICKY)
00211 pfx[pfx_len++] = '=';
00212 }
00213
00214 pfx[pfx_len] = '\0';
00215
00216 len = capab_list[i].namelen + pfx_len;
00217
00218 if (sizeof(capbuf) < (clen + loc + len + 15))
00219 {
00220
00221 sendto_one(sptr, "%s* :%s", cmdbuf, capbuf);
00222 capbuf[(loc = 0)] = '\0';
00223 }
00224
00225 loc += snprintf(capbuf + loc, sizeof(capbuf) - loc,
00226 "%s%s", pfx, capab_list[i].name);
00227 }
00228
00229 sendto_one(sptr, "%s:%s", cmdbuf, capbuf);
00230
00231 return 0;
00232 }
00233
00234 static int
00235 cap_ls(struct Client *sptr, const char *caplist)
00236 {
00237 if (IsUnknown(sptr))
00238 sptr->localClient->registration |= REG_NEED_CAP;
00239
00240 return send_caplist(sptr, 0, 0, "LS");
00241 }
00242
00243 static int
00244 cap_req(struct Client *sptr, const char *caplist)
00245 {
00246 const char *cl = caplist;
00247 struct capabilities *cap;
00248 unsigned int set = 0, rem = 0;
00249 unsigned int cs = sptr->localClient->cap_client;
00250 unsigned int as = sptr->localClient->cap_active;
00251 int neg;
00252
00253 if (IsUnknown(sptr))
00254 sptr->localClient->registration |= REG_NEED_CAP;
00255
00256 while (cl) {
00257 if (!(cap = find_cap(&cl, &neg))
00258 || (!neg && (cap->flags & CAPFL_PROHIBIT))
00259 || (neg && (cap->flags & CAPFL_STICKY))) {
00260 sendto_one(sptr, ":%s CAP %s NAK :%s", me.name,
00261 sptr->name[0] ? sptr->name : "*", caplist);
00262 return 0;
00263 }
00264
00265 if (neg)
00266 {
00267
00268 rem |= cap->cap;
00269 set &= ~cap->cap;
00270 cs &= ~cap->cap;
00271
00272 if (!(cap->flags & CAPFL_PROTO))
00273 as &= ~cap->cap;
00274 }
00275 else
00276 {
00277 rem &= ~cap->cap;
00278 set |= cap->cap;
00279 cs |= cap->cap;
00280
00281 if (!(cap->flags & CAPFL_PROTO))
00282 as |= cap->cap;
00283 }
00284 }
00285
00286
00287 send_caplist(sptr, set, rem, "ACK");
00288
00289 sptr->localClient->cap_client = cs;
00290 sptr->localClient->cap_active = as;
00291
00292 return 0;
00293 }
00294
00295 static int
00296 cap_ack(struct Client *sptr, const char *caplist)
00297 {
00298 const char *cl = caplist;
00299 struct capabilities *cap = NULL;
00300 int neg;
00301
00302
00303
00304
00305
00306
00307 while (cl)
00308 {
00309
00310 if (!(cap = find_cap(&cl, &neg)) ||
00311 (neg ? (sptr->localClient->cap_active & cap->cap) :
00312 !(sptr->localClient->cap_active & cap->cap)))
00313 continue;
00314
00315 if (neg)
00316 sptr->localClient->cap_active &= ~cap->cap;
00317 else
00318 sptr->localClient->cap_active |= cap->cap;
00319 }
00320
00321 return 0;
00322 }
00323
00324 static int
00325 cap_clear(struct Client *sptr, const char *caplist)
00326 {
00327 struct capabilities *cap;
00328 unsigned int ii;
00329 unsigned int cleared = 0;
00330
00331 for (ii = 0; ii < CAPAB_LIST_LEN; ++ii)
00332 {
00333 cap = &capab_list[ii];
00334
00335
00336 if (!(sptr->localClient->cap_active & cap->cap) || (cap->flags & CAPFL_STICKY))
00337 continue;
00338
00339 cleared |= cap->cap;
00340 sptr->localClient->cap_client &= ~cap->cap;
00341
00342 if (!(cap->flags & CAPFL_PROTO))
00343 sptr->localClient->cap_active &= ~cap->cap;
00344 }
00345
00346 return send_caplist(sptr, 0, cleared, "ACK");
00347 }
00348
00349 static int
00350 cap_end(struct Client *sptr, const char *caplist)
00351 {
00352 if (!IsUnknown(sptr))
00353 return 0;
00354
00355
00356 sptr->localClient->registration &= ~REG_NEED_CAP;
00357
00358
00359 if (!sptr->localClient->registration)
00360 {
00361 char buf[USERLEN + 1];
00362
00363 strlcpy(buf, sptr->username, sizeof(buf));
00364 register_local_user(sptr, sptr, sptr->name, buf);
00365 return 0;
00366 }
00367
00368 return 0;
00369 }
00370
00371 static int
00372 cap_list(struct Client *sptr, const char *caplist)
00373 {
00374
00375 return send_caplist(sptr, sptr->localClient->cap_client, 0, "LIST");
00376 }
00377
00378 static struct subcmd
00379 {
00380 const char *cmd;
00381 int (*proc)(struct Client *sptr, const char *caplist);
00382 } cmdlist[] = {
00383 { "ACK", cap_ack },
00384 { "CLEAR", cap_clear },
00385 { "END", cap_end },
00386 { "LIST", cap_list },
00387 { "LS", cap_ls },
00388 { "NAK", NULL },
00389 { "REQ", cap_req }
00390 };
00391
00392 static int
00393 subcmd_search(const char *cmd, const struct subcmd *elem)
00394 {
00395 return strcasecmp(cmd, elem->cmd);
00396 }
00397
00398
00399
00400
00401
00402
00403
00404 static void
00405 m_cap(struct Client *cptr, struct Client *sptr, int parc, char *parv[])
00406 {
00407 char *subcmd, *caplist = 0;
00408 struct subcmd *cmd = NULL;
00409
00410 if (parc < 2)
00411 return;
00412
00413 subcmd = parv[1];
00414
00415 if (parc > 2)
00416 caplist = parv[2];
00417
00418
00419 if (!(cmd = bsearch(subcmd, cmdlist,
00420 sizeof(cmdlist) / sizeof(struct subcmd),
00421 sizeof(struct subcmd), (bqcmp)subcmd_search)))
00422 {
00423 sendto_one(sptr, form_str(ERR_INVALIDCAPCMD),
00424 me.name, sptr->name, subcmd);
00425 return;
00426 }
00427
00428
00429 if (cmd->proc)
00430 (cmd->proc)(sptr, caplist);
00431 }