]> jfr.im git - irc/evilnet/x3.git/blame - src/proto-p10.c
Fix for SASL vulnerability
[irc/evilnet/x3.git] / src / proto-p10.c
CommitLineData
d76ed9a9 1/* proto-p10.c - IRC protocol output
2 * Copyright 2000-2004 srvx Development Team
3 *
83ff05c3 4 * This file is part of x3.
d76ed9a9 5 *
d0f04f71 6 * x3 is free software; you can redistribute it and/or modify
d76ed9a9 7 * it under the terms of the GNU General Public License as published by
348683aa 8 * the Free Software Foundation; either version 3 of the License, or
d76ed9a9 9 * (at your option) any later version.
10 *
11 * This program is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU General Public License for more details.
15 *
16 * You should have received a copy of the GNU General Public License
17 * along with srvx; if not, write to the Free Software Foundation,
18 * Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA.
19 */
20
3fdd6a74 21#include "nickserv.h"
23475fc6 22#include "chanserv.h"
89d871d8 23#include "hash.h"
08895577 24#include "helpfile.h"
37ef8ee3 25#include "hosthiding.h"
d76ed9a9 26#include "proto-common.c"
47956fc5 27#include "opserv.h"
d76ed9a9 28
29/* Full commands. */
30#define CMD_ACCOUNT "ACCOUNT"
31#define CMD_ADMIN "ADMIN"
763de701 32#define CMD_ALIST "ALIST"
d76ed9a9 33#define CMD_ASLL "ASLL"
34#define CMD_AWAY "AWAY"
35#define CMD_BURST "BURST"
36#define CMD_CLEARMODE "CLEARMODE"
37#define CMD_CLOSE "CLOSE"
38#define CMD_CNOTICE "CNOTICE"
39#define CMD_CONNECT "CONNECT"
40#define CMD_CPRIVMSG "CPRIVMSG"
41#define CMD_CREATE "CREATE"
42#define CMD_DESTRUCT "DESTRUCT"
43#define CMD_DESYNCH "DESYNCH"
44#define CMD_DIE "DIE"
45#define CMD_DNS "DNS"
46#define CMD_EOB "END_OF_BURST"
47#define CMD_EOB_ACK "EOB_ACK"
48#define CMD_ERROR "ERROR"
49#define CMD_FAKEHOST "FAKE"
50#define CMD_GET "GET"
51#define CMD_GLINE "GLINE"
52#define CMD_HASH "HASH"
53#define CMD_HELP "HELP"
54#define CMD_INFO "INFO"
55#define CMD_INVITE "INVITE"
56#define CMD_ISON "ISON"
57#define CMD_JOIN "JOIN"
58#define CMD_JUPE "JUPE"
59#define CMD_KICK "KICK"
60#define CMD_KILL "KILL"
61#define CMD_LINKS "LINKS"
62#define CMD_LIST "LIST"
63#define CMD_LUSERS "LUSERS"
64#define CMD_MAP "MAP"
23475fc6 65#define CMD_MARK "MARK"
d76ed9a9 66#define CMD_MODE "MODE"
67#define CMD_MOTD "MOTD"
68#define CMD_NAMES "NAMES"
69#define CMD_NICK "NICK"
70#define CMD_NOTICE "NOTICE"
71#define CMD_OPER "OPER"
72#define CMD_OPMODE "OPMODE"
73#define CMD_PART "PART"
74#define CMD_PASS "PASS"
75#define CMD_PING "PING"
76#define CMD_PONG "PONG"
77#define CMD_POST "POST"
78#define CMD_PRIVMSG "PRIVMSG"
79#define CMD_PRIVS "PRIVS"
80#define CMD_PROTO "PROTO"
81#define CMD_QUIT "QUIT"
82#define CMD_REHASH "REHASH"
e10feb7a 83#define CMD_REMOVE "REMOVE"
d76ed9a9 84#define CMD_RESET "RESET"
85#define CMD_RESTART "RESTART"
86#define CMD_RPING "RPING"
87#define CMD_RPONG "RPONG"
c8b4b87b 88#define CMD_SASL "SASL"
d76ed9a9 89#define CMD_SERVER "SERVER"
90#define CMD_SERVLIST "SERVLIST"
91#define CMD_SERVSET "SERVSET"
92#define CMD_SET "SET"
93#define CMD_SETTIME "SETTIME"
59b38cc0 94#define CMD_SGLINE "SGLINE"
d914d1cb 95#define CMD_SHUN "SHUN"
d76ed9a9 96#define CMD_SILENCE "SILENCE"
56b83e80 97#define CMD_SMO "SMO"
1c76f1b4 98#define CMD_SNO "SNO"
59b38cc0 99#define CMD_SSHUN "SSHUN"
4b2dcc0d 100#define CMD_SPAMFILTER "SPAMFILTER"
d76ed9a9 101#define CMD_SQUERY "SQUERY"
102#define CMD_SQUIT "SQUIT"
103#define CMD_STATS "STATS"
cd25f2e9 104#define CMD_SVSJOIN "SVSJOIN"
d76ed9a9 105#define CMD_SVSNICK "SVSNICK"
cd25f2e9 106#define CMD_SVSPART "SVSPART"
5e6460e4 107#define CMD_SVSQUIT "SVSQUIT"
56958740 108#define CMD_SWHOIS "SWHOIS"
d76ed9a9 109#define CMD_TIME "TIME"
110#define CMD_TOPIC "TOPIC"
111#define CMD_TRACE "TRACE"
112#define CMD_UPING "UPING"
113#define CMD_USER "USER"
114#define CMD_USERHOST "USERHOST"
115#define CMD_USERIP "USERIP"
116#define CMD_VERSION "VERSION"
117#define CMD_WALLCHOPS "WALLCHOPS"
118#define CMD_WALLOPS "WALLOPS"
55342ce8 119#define CMD_WALLHOPS "WALLHOPS"
d76ed9a9 120#define CMD_WALLUSERS "WALLUSERS"
121#define CMD_WALLVOICES "WALLVOICES"
55342ce8 122#define CMD_WALLHOPS "WALLHOPS"
d76ed9a9 123#define CMD_WHO "WHO"
124#define CMD_WHOIS "WHOIS"
125#define CMD_WHOWAS "WHOWAS"
e10feb7a 126#define CMD_ZLINE "ZLINE"
d76ed9a9 127
128/* Tokenized commands. */
129#define TOK_ACCOUNT "AC"
130#define TOK_ADMIN "AD"
763de701 131#define TOK_ALIST "AL"
d76ed9a9 132#define TOK_ASLL "LL"
133#define TOK_AWAY "A"
134#define TOK_BURST "B"
135#define TOK_CLEARMODE "CM"
136#define TOK_CLOSE "CLOSE"
137#define TOK_CNOTICE "CN"
138#define TOK_CONNECT "CO"
139#define TOK_CPRIVMSG "CP"
140#define TOK_CREATE "C"
141#define TOK_DESTRUCT "DE"
142#define TOK_DESYNCH "DS"
143#define TOK_DIE "DIE"
144#define TOK_DNS "DNS"
145#define TOK_EOB "EB"
146#define TOK_EOB_ACK "EA"
147#define TOK_ERROR "Y"
fede8b64 148#define TOK_EXEMPT "EX"
d76ed9a9 149#define TOK_FAKEHOST "FA"
150#define TOK_GET "GET"
151#define TOK_GLINE "GL"
152#define TOK_HASH "HASH"
153#define TOK_HELP "HELP"
154#define TOK_INFO "F"
155#define TOK_INVITE "I"
156#define TOK_ISON "ISON"
157#define TOK_JOIN "J"
158#define TOK_JUPE "JU"
159#define TOK_KICK "K"
160#define TOK_KILL "D"
161#define TOK_LINKS "LI"
162#define TOK_LIST "LIST"
163#define TOK_LUSERS "LU"
164#define TOK_MAP "MAP"
23475fc6 165#define TOK_MARK "MK"
d76ed9a9 166#define TOK_MODE "M"
167#define TOK_MOTD "MO"
168#define TOK_NAMES "E"
169#define TOK_NICK "N"
170#define TOK_NOTICE "O"
171#define TOK_OPER "OPER"
172#define TOK_OPMODE "OM"
173#define TOK_PART "L"
174#define TOK_PASS "PA"
175#define TOK_PING "G"
176#define TOK_PONG "Z"
177#define TOK_POST "POST"
178#define TOK_PRIVMSG "P"
a25d8a57 179#define TOK_PRIVS "PRIVS"
d76ed9a9 180#define TOK_PROTO "PROTO"
181#define TOK_QUIT "Q"
182#define TOK_REHASH "REHASH"
e10feb7a 183#define TOK_REMOVE "RM"
d76ed9a9 184#define TOK_RESET "RESET"
185#define TOK_RESTART "RESTART"
186#define TOK_RPING "RI"
187#define TOK_RPONG "RO"
c8b4b87b 188#define TOK_SASL "SASL"
d76ed9a9 189#define TOK_SERVER "S"
190#define TOK_SERVLIST "SERVSET"
191#define TOK_SERVSET "SERVSET"
192#define TOK_SET "SET"
193#define TOK_SETTIME "SE"
59b38cc0 194#define TOK_SGLINE "SGL"
d914d1cb 195#define TOK_SHUN "SU"
d76ed9a9 196#define TOK_SILENCE "U"
56b83e80 197#define TOK_SMO "SMO"
1c76f1b4 198#define TOK_SNO "SNO"
59b38cc0 199#define TOK_SSHUN "SSU"
4b2dcc0d 200#define TOK_SPAMFILTER "SF"
d76ed9a9 201#define TOK_SQUERY "SQUERY"
202#define TOK_SQUIT "SQ"
203#define TOK_STATS "R"
cd25f2e9 204#define TOK_SVSJOIN "SJ"
d76ed9a9 205#define TOK_SVSNICK "SN"
cd25f2e9 206#define TOK_SVSPART "SP"
5e6460e4 207#define TOK_SVSQUIT "SX"
56958740 208#define TOK_SWHOIS "SW"
d76ed9a9 209#define TOK_TIME "TI"
210#define TOK_TOPIC "T"
211#define TOK_TRACE "TR"
212#define TOK_UPING "UP"
213#define TOK_USER "USER"
214#define TOK_USERHOST "USERHOST"
215#define TOK_USERIP "USERIP"
216#define TOK_VERSION "V"
217#define TOK_WALLCHOPS "WC"
218#define TOK_WALLOPS "WA"
55342ce8 219#define TOK_WALLHOPS "WH"
d76ed9a9 220#define TOK_WALLUSERS "WU"
221#define TOK_WALLVOICES "WV"
55342ce8 222#define TOK_WALLHOPS "WH"
d76ed9a9 223#define TOK_WHO "H"
224#define TOK_WHOIS "W"
225#define TOK_WHOWAS "X"
e10feb7a 226#define TOK_ZLINE "ZL"
d76ed9a9 227
228/* Protocol messages; aliased to full commands or tokens depending
229 on compile-time configuration. ircu prefers tokens WITH THE
230 EXCEPTION OF THE SERVER AND PASS COMMANDS, which cannot be
231 tokenized, because clients' (ie. a linking server) commands are
232 only checked against the full command list.
233*/
234#if defined(ENABLE_TOKENS)
235#define TYPE(NAME) TOK_ ## NAME
236#else /* !ENABLE_TOKENS */
237#define TYPE(NAME) CMD_ ## NAME
238#endif /* ENABLE_TOKENS */
239
240#define P10_ACCOUNT TYPE(ACCOUNT)
241#define P10_ADMIN TYPE(ADMIN)
242#define P10_ASLL TYPE(ASLL)
243#define P10_AWAY TYPE(AWAY)
244#define P10_BURST TYPE(BURST)
245#define P10_CLEARMODE TYPE(CLEARMODE)
246#define P10_CLOSE TYPE(CLOSE)
247#define P10_CNOTICE TYPE(CNOTICE)
248#define P10_CONNECT TYPE(CONNECT)
249#define P10_CPRIVMSG TYPE(CPRIVMSG)
250#define P10_CREATE TYPE(CREATE)
251#define P10_DESTRUCT TYPE(DESTRUCT)
252#define P10_DESYNCH TYPE(DESYNCH)
253#define P10_DIE TYPE(DIE)
254#define P10_DNS TYPE(DNS)
255#define P10_EOB TYPE(EOB)
256#define P10_EOB_ACK TYPE(EOB_ACK)
257#define P10_ERROR TYPE(ERROR)
258#define P10_FAKEHOST TYPE(FAKEHOST)
259#define P10_GET TYPE(GET)
260#define P10_GLINE TYPE(GLINE)
261#define P10_HASH TYPE(HASH)
262#define P10_HELP TYPE(HELP)
263#define P10_INFO TYPE(INFO)
264#define P10_INVITE TYPE(INVITE)
265#define P10_ISON TYPE(ISON)
266#define P10_JOIN TYPE(JOIN)
267#define P10_JUPE TYPE(JUPE)
268#define P10_KICK TYPE(KICK)
269#define P10_KILL TYPE(KILL)
270#define P10_LINKS TYPE(LINKS)
271#define P10_LIST TYPE(LIST)
272#define P10_LUSERS TYPE(LUSERS)
273#define P10_MAP TYPE(MAP)
23475fc6 274#define P10_MARK TYPE(MARK)
d76ed9a9 275#define P10_MODE TYPE(MODE)
276#define P10_MOTD TYPE(MOTD)
277#define P10_NAMES TYPE(NAMES)
278#define P10_NICK TYPE(NICK)
279#define P10_NOTICE TYPE(NOTICE)
280#define P10_OPER TYPE(OPER)
281#define P10_OPMODE TYPE(OPMODE)
282#define P10_PART TYPE(PART)
283#define P10_PASS CMD_PASS
284#define P10_PING TYPE(PING)
285#define P10_PONG TYPE(PONG)
286#define P10_POST TYPE(POST)
287#define P10_PRIVMSG TYPE(PRIVMSG)
288#define P10_PRIVS TYPE(PRIVS)
289#define P10_PROTO TYPE(PROTO)
290#define P10_QUIT TYPE(QUIT)
291#define P10_REHASH TYPE(REHASH)
e10feb7a 292#define P10_REMOVE TYPE(REMOVE)
d76ed9a9 293#define P10_RESET TYPE(RESET)
294#define P10_RESTART TYPE(RESTART)
295#define P10_RPING TYPE(RPING)
296#define P10_RPONG TYPE(RPONG)
c8b4b87b 297#define P10_SASL TYPE(SASL)
d76ed9a9 298#define P10_SERVER CMD_SERVER
299#define P10_SERVLIST TYPE(SERVLIST)
300#define P10_SERVSET TYPE(SERVSET)
301#define P10_SET TYPE(SET)
302#define P10_SETTIME TYPE(SETTIME)
59b38cc0 303#define P10_SGLINE TYPE(SGLINE)
d914d1cb 304#define P10_SHUN TYPE(SHUN)
d76ed9a9 305#define P10_SILENCE TYPE(SILENCE)
56b83e80 306#define P10_SMO TYPE(SMO)
1c76f1b4 307#define P10_SNO TYPE(SNO)
59b38cc0 308#define P10_SSHUN TYPE(SSHUN)
4b2dcc0d 309#define P10_SPAMFILTER TYPE(SPAMFILTER)
d76ed9a9 310#define P10_SQUERY TYPE(SQUERY)
311#define P10_SQUIT TYPE(SQUIT)
312#define P10_STATS TYPE(STATS)
cd25f2e9 313#define P10_SVSJOIN TYPE(SVSJOIN)
d76ed9a9 314#define P10_SVSNICK TYPE(SVSNICK)
cd25f2e9 315#define P10_SVSPART TYPE(SVSPART)
5e6460e4 316#define P10_SVSQUIT TYPE(SVSQUIT)
56958740 317#define P10_SWHOIS TYPE(SWHOIS)
d76ed9a9 318#define P10_TIME TYPE(TIME)
319#define P10_TOPIC TYPE(TOPIC)
320#define P10_TRACE TYPE(TRACE)
321#define P10_UPING TYPE(UPING)
322#define P10_USER TYPE(USER)
323#define P10_USERHOST TYPE(USERHOST)
324#define P10_USERIP TYPE(USERIP)
325#define P10_VERSION TYPE(VERSION)
326#define P10_WALLCHOPS TYPE(WALLCHOPS)
327#define P10_WALLOPS TYPE(WALLOPS)
55342ce8 328#define P10_WALLHOPS TYPE(WALLHOPS)
d76ed9a9 329#define P10_WALLUSERS TYPE(WALLUSERS)
330#define P10_WALLVOICES TYPE(WALLVOICES)
331#define P10_WHO TYPE(WHO)
332#define P10_WHOIS TYPE(WHOIS)
333#define P10_WHOWAS TYPE(WHOWAS)
e10feb7a 334#define P10_ZLINE TYPE(ZLINE)
850bf0cf 335#define P10_EXEMPT TYPE(EXEMPT)
d76ed9a9 336
337/* Servers claiming to have a boot or link time before PREHISTORY
338 * trigger errors to the log. We hope no server has been running
339 * constantly since September 1994. :)
340 */
341#define PREHISTORY 780000000
342
08895577 343#define MODELEN 40 + KEYLEN
344
d76ed9a9 345static struct server *servers_num[64*64];
346static privmsg_func_t *privmsg_funcs;
347static unsigned int num_privmsg_funcs;
348static privmsg_func_t *notice_funcs;
349static unsigned int num_notice_funcs;
350static struct dict *unbursted_channels;
1136f709 351static const char *his_servername;
352static const char *his_servercomment;
805e7c7a 353static int extended_accounts;
d76ed9a9 354
8d882965
MB
355/* These correspond to 1 << X: 01234567890123456789012345 */
356const char irc_user_mode_chars[] = "oOiw dkgh x BnIX azDRWHLq";
57fdf922 357
d76ed9a9 358static struct userNode *AddUser(struct server* uplink, const char *nick, const char *ident, const char *hostname, const char *modes, const char *numeric, const char *userinfo, time_t timestamp, const char *realip);
359
360extern int off_channel;
08895577 361extern int DefConLevel;
08895577 362extern int DefConTimeOut;
363extern char *DefConChanModes;
d76ed9a9 364
2f61d1d7 365static int parse_oplevel(char *str);
366
89d871d8 367char privbuf[512] = "";
368
d76ed9a9 369/* Numerics can be XYY, XYYY, or XXYYY; with X's identifying the
370 * server and Y's indentifying the client on that server. */
371struct server*
372GetServerN(const char *numeric)
373{
374 switch (strlen(numeric)) {
375 default:
376 return servers_num[base64toint(numeric, 2)];
377 case 4:
378 case 3:
379 case 1:
380 return servers_num[base64toint(numeric, 1)];
381 case 0:
382 return NULL;
383 }
384}
385
386struct userNode*
387GetUserN(const char *numeric) /* using numeric */
388{
389 struct userNode *un;
390 struct server *s;
391 int n, slen, ulen;
392
393 switch (strlen(numeric)) {
394 default:
395 log_module(MAIN_LOG, LOG_WARNING, "GetUserN(%s): numeric too long!", numeric);
396 return NULL;
397 case 5: slen = 2; ulen = 3; break;
398 case 4: slen = 1; ulen = 3; break;
399 case 3: slen = 1; ulen = 2; break;
400 case 2: case 1: case 0:
401 log_module(MAIN_LOG, LOG_WARNING, "GetUserN(%s): numeric too short!", numeric);
402 return NULL;
403 }
404 if (!(s = servers_num[base64toint(numeric, slen)])) {
405 log_module(MAIN_LOG, LOG_WARNING, "GetUserN(%s): couldn't find server (len=%d)!", numeric, slen);
406 return NULL;
407 }
408 n = base64toint(numeric+slen, ulen) & s->num_mask;
409 if (!(un = s->users[n])) {
410 log_module(MAIN_LOG, LOG_WARNING, "GetUserN(%s) couldn't find user!", numeric);
411 }
412 return un;
413}
414
da5b7dfc 415extern struct userNode *opserv;
416static void
417check_ctcp(struct userNode *user, struct userNode *bot, char *text, UNUSED_ARG(int server_qualified))
418{
419 char *cmd;
420 /* if its a version reply, do an alert check (only alerts with version=something) */
421 if(bot == opserv) {
422 if(text[0] == '\001') {
423 text++;
424 cmd = mysep(&text, " ");
425 if(!irccasecmp(cmd, "VERSION")) {
426 char *version = mysep(&text, "\n");
427 if(!version)
428 version = "";
429 /* opserv_debug("Opserv got CTCP VERSION Notice from %s: %s", user->nick, version); */
430 /* TODO: setup a ctcp_funcs thing to handle this and other CTCPS properly */
431 user->version_reply = strdup(version);
432 /* TODO: put this in the db */
433 if(match_ircglob(version, "WebTV;*"))
434 user->no_notice = true; /* webbies cant see notices */
435 }
436 }
437 }
438}
439
440
d76ed9a9 441static void
442privmsg_user_helper(struct userNode *un, void *data)
443{
444 struct privmsg_desc *pd = data;
445 unsigned int num = un->num_local;
446 if (!pd->is_notice) {
447 if ((num < num_privmsg_funcs) && privmsg_funcs[num]) {
448 privmsg_funcs[num](pd->user, un, pd->text, pd->is_qualified);
449 }
450 } else {
451 if ((num < num_notice_funcs) && notice_funcs[num]) {
da5b7dfc 452 check_ctcp(pd->user, un, pd->text, pd->is_qualified);
d76ed9a9 453 notice_funcs[num](pd->user, un, pd->text, pd->is_qualified);
454 }
455 }
456}
457
47956fc5 458/* equiv to user doing /connect server port target */
459void irc_connect(struct userNode *user, char *server, unsigned int port, struct server *target)
460{
461 putsock("%s " P10_CONNECT " %s %d %s", user->numeric, server, port, target->numeric);
462}
463
464void
465irc_squit_route(struct server *srv, const char *message, ...)
466{
467 va_list arg_list;
468 char buffer[MAXLEN];
469 va_start(arg_list, message);
470 vsnprintf(buffer, MAXLEN-2, message, arg_list);
471 buffer[MAXLEN-1] = 0;
472
473 /* When would we squit ourselves exactly?? -Rubin */
474 if(srv == self && cManager.uplink->state == CONNECTED ) {
475 unsigned int i;
476
477 /* Quit all clients linked to me. */
478 for(i = 0; i <= self->num_mask; i++) {
479 if(!self->users[i])
480 continue;
481 irc_quit(self->users[i], buffer);
482 }
483 }
484
485 putsock("%s " P10_SQUIT " %s %d :%s", self->numeric, srv->name, 0, buffer);
486
487 if(srv == self) {
488 /* Force a reconnect to the currently selected server. */
489 cManager.uplink->tries = 0;
490 log_module(MAIN_LOG, LOG_INFO, "Squitting from uplink: %s", buffer);
491 close_socket();
492 }
493}
494
d76ed9a9 495void
496irc_server(struct server *srv)
497{
498 char extranum[COMBO_NUMERIC_LEN+1];
499
500 inttobase64(extranum, srv->num_mask, (srv->numeric[1] || (srv->num_mask >= 64*64)) ? 3 : 2);
501 if (srv == self) {
2b9de5dd 502 putsock(P10_SERVER " %s %d " FMT_TIME_T " " FMT_TIME_T " J10 %s%s +s6o :%s",
1136f709 503 srv->name, srv->hops+1, srv->boot, srv->link_time, srv->numeric, extranum, srv->description);
d76ed9a9 504 } else {
2b9de5dd 505 putsock("%s " P10_SERVER " %s %d " FMT_TIME_T " " FMT_TIME_T " %c10 %s%s +s6o :%s",
1136f709 506 self->numeric, srv->name, srv->hops+1, srv->boot, srv->link_time, (srv->self_burst ? 'J' : 'P'), srv->numeric, extranum, srv->description);
d76ed9a9 507 }
508}
509
cf8bedff 510void
2f61d1d7 511irc_p10_pton(irc_in_addr_t *ip, const char *input)
512{
513 if (strlen(input) == 6) {
514 unsigned int value;
515 memset(ip, 0, 6 * sizeof(ip->in6[0]));
516 value = base64toint(input, 6);
517 if (value)
518 ip->in6[5] = htons(65535);
519 ip->in6[6] = htons(value >> 16);
520 ip->in6[7] = htons(value & 65535);
521 } else {
522 unsigned int pos = 0;
523 do {
524 if (*input == '_') {
525 unsigned int left;
7da677be 526 for (left = (25 - strlen(input)) / 3 - pos; left; left--)
2f61d1d7 527 ip->in6[pos++] = 0;
528 input++;
529 } else {
530 ip->in6[pos++] = ntohs(base64toint(input, 3));
531 input += 3;
532 }
533 } while (pos < 8);
534 }
535}
536
cf8bedff 537void
2f61d1d7 538irc_p10_ntop(char *output, const irc_in_addr_t *ip)
539{
540 if (!irc_in_addr_is_valid(*ip)) {
541 strcpy(output, "AAAAAA");
542 } else if (irc_in_addr_is_ipv4(*ip)) {
543 unsigned int in4;
544 in4 = (ntohs(ip->in6[6]) << 16) | ntohs(ip->in6[7]);
545 inttobase64(output, in4, 6);
546 output[6] = '\0';
547 } else if (irc_in_addr_is_ipv6(*ip)) {
548 unsigned int max_start, max_zeros, curr_zeros, zero, ii;
549 /* Can start by printing out the leading non-zero parts. */
550 for (ii = 0; (ip->in6[ii]) && (ii < 8); ++ii) {
551 inttobase64(output, ntohs(ip->in6[ii]), 3);
552 output += 3;
553 }
554 /* Find the longest run of zeros. */
555 for (max_start = zero = ii, max_zeros = curr_zeros = 0; ii < 8; ++ii) {
556 if (!ip->in6[ii])
557 curr_zeros++;
558 else if (curr_zeros > max_zeros) {
559 max_start = ii - curr_zeros;
560 max_zeros = curr_zeros;
561 curr_zeros = 0;
562 }
563 }
564 if (curr_zeros > max_zeros) {
565 max_start = ii - curr_zeros;
566 max_zeros = curr_zeros;
567 curr_zeros = 0;
568 }
569 /* Print the rest of the address */
570 for (ii = zero; ii < 8; ) {
571 if ((ii == max_start) && max_zeros) {
572 *output++ = '_';
573 ii += max_zeros;
574 } else {
575 inttobase64(output, ntohs(ip->in6[ii]), 3);
576 output += 3;
577 }
578 }
579 *output = '\0';
580 } else {
581 strcpy(output, "???");
582 }
583}
584
d76ed9a9 585void
586irc_user(struct userNode *user)
587{
2f61d1d7 588 char b64ip[25];
1136f709 589 if (!user || IsDummy(user))
d76ed9a9 590 return;
2f61d1d7 591 irc_p10_ntop(b64ip, &user->ip);
d76ed9a9 592 if (user->modes) {
d76ed9a9 593 char modes[32];
594
57fdf922 595 irc_user_modes(user, modes, sizeof(modes));
d76ed9a9 596 /* we don't need to put the + in modes because it's in the format string. */
a837dcf5 597 putsock("%s " P10_NICK " %s %d " FMT_TIME_T " %s %s +%s %s %s :%s",
d76ed9a9 598 user->uplink->numeric, user->nick, user->uplink->hops+1, user->timestamp, user->ident, user->hostname, modes, b64ip, user->numeric, user->info);
599 } else {
a837dcf5 600 putsock("%s " P10_NICK " %s %d " FMT_TIME_T " %s %s %s %s :%s",
d76ed9a9 601 user->uplink->numeric, user->nick, user->uplink->hops+1, user->timestamp, user->ident, user->hostname, b64ip, user->numeric, user->info);
602 }
603}
604
a45e6ec7 605void
606irc_rename(struct userNode *user, const char *new_handle)
607{
805e7c7a 608 if(extended_accounts)
609 putsock("%s " P10_ACCOUNT " %s M %s", self->numeric, user->numeric, new_handle);
a45e6ec7 610}
611
612void
613irc_delete(struct userNode *user)
614{
805e7c7a 615 if(extended_accounts)
616 putsock("%s " P10_ACCOUNT " %s U", self->numeric, user->numeric);
a45e6ec7 617}
618
d76ed9a9 619void
b21e2cfe 620irc_account(struct userNode *user, const char *stamp, time_t timestamp)
d76ed9a9 621{
805e7c7a 622 if(extended_accounts)
a837dcf5 623 putsock("%s " P10_ACCOUNT " %s R %s "FMT_TIME_T, self->numeric, user->numeric, stamp, timestamp);
805e7c7a 624 else
a837dcf5 625 putsock("%s " P10_ACCOUNT " %s %s "FMT_TIME_T, self->numeric, user->numeric, stamp, timestamp);
d76ed9a9 626}
627
628void
629irc_fakehost(struct userNode *user, const char *host)
630{
631 putsock("%s " P10_FAKEHOST " %s %s", self->numeric, user->numeric, host);
632}
633
634void
635irc_regnick(UNUSED_ARG(struct userNode *user))
636{
637 /* no operation here */
638}
639
640void
641irc_nick(struct userNode *user, UNUSED_ARG(const char *old_nick))
642{
643 putsock("%s " P10_NICK " %s "FMT_TIME_T, user->numeric, user->nick, now);
644}
645
646void
647irc_fetchtopic(struct userNode *from, const char *to)
648{
649 if (!from || !to)
650 return;
651 putsock("%s " P10_TOPIC " %s", from->numeric, to);
652}
653
654void
655irc_squit(struct server *srv, const char *message, const char *service_message)
656{
657 if (!service_message)
658 service_message = message;
659
660 /* Are we leaving the network? */
661 if (srv == self && cManager.uplink->state == CONNECTED) {
662 unsigned int i;
663
664 /* Quit all clients linked to me. */
665 for (i = 0; i <= self->num_mask; i++) {
666 if (!self->users[i])
667 continue;
668 irc_quit(self->users[i], service_message);
669 }
670 }
671
672 putsock("%s " P10_SQUIT " %s %d :%s", self->numeric, srv->name, 0, message);
673
674 if (srv == self) {
675 /* Force a reconnect to the currently selected server. */
676 cManager.uplink->tries = 0;
677 log_module(MAIN_LOG, LOG_INFO, "Squitting from uplink: %s", message);
678 close_socket();
679 }
680}
681
682void
683irc_wallchops(struct userNode *from, const char *to, const char *message)
684{
685 putsock("%s " P10_WALLCHOPS " %s :%s", from->numeric, to, message);
686}
687
47956fc5 688void
689irc_wallops(const char *format, ...)
690{
691 va_list arg_list;
692 char buffer[MAXLEN];
693 va_start(arg_list, format);
694 vsnprintf(buffer, MAXLEN-2, format, arg_list);
695 buffer[MAXLEN-1] = 0;
696 putsock("%s " P10_WALLOPS " :%s", self->numeric, buffer);
697}
698
1136f709 699static int
700deliver_to_dummy(struct userNode *source, struct userNode *dest, const char *message, int type)
701{
702 unsigned int num;
703
704 if (!dest || !IsDummy(dest) || !IsLocal(dest))
705 return 0;
706 num = dest->num_local;
707 switch (type) {
708 default:
709 if ((num < num_notice_funcs) && notice_funcs[num])
710 notice_funcs[num](source, dest, message, 0);
711 break;
712 case 1:
713 if ((num < num_privmsg_funcs) && privmsg_funcs[num])
714 privmsg_funcs[num](source, dest, message, 0);
715 break;
716 }
717 return 1;
718}
0e08a8e0 719
d76ed9a9 720void
721irc_notice(struct userNode *from, const char *to, const char *message)
722{
1136f709 723 if (to[0] == '#' || to[0] == '$'
724 || !deliver_to_dummy(from, GetUserN(to), message, 0))
725 putsock("%s " P10_NOTICE " %s :%s", from->numeric, to, message);
d76ed9a9 726}
727
728void
729irc_notice_user(struct userNode *from, struct userNode *to, const char *message)
730{
1136f709 731 if (!deliver_to_dummy(from, to, message, 0))
732 putsock("%s " P10_NOTICE " %s :%s", from->numeric, to->numeric, message);
d76ed9a9 733}
734
735void
736irc_privmsg(struct userNode *from, const char *to, const char *message)
737{
1136f709 738 if (to[0] == '#' || to[0] == '$'
739 || !deliver_to_dummy(from, GetUserN(to), message, 1))
740 putsock("%s " P10_PRIVMSG " %s :%s", from->numeric, to, message);
d76ed9a9 741}
742
39c1a4ef 743void
0e08a8e0 744irc_privmsg_user(struct userNode *from, struct userNode *to, const char *message)
745{
746 putsock("%s " P10_PRIVMSG " %s :%s", from->numeric, to->numeric, message);
747}
748
749void
750irc_version_user(struct userNode *from, struct userNode *to)
751{
752 irc_privmsg_user(from, to, "\001VERSION\001");
753}
754
d76ed9a9 755void
756irc_eob(void)
757{
758 putsock("%s " P10_EOB, self->numeric);
759}
760
761void
762irc_eob_ack(void)
763{
764 putsock("%s " P10_EOB_ACK, self->numeric);
08895577 765
766 char *nick;
767 const char *str;
768 str = conf_get_data("services/opserv/nick", RECDB_QSTRING);
769 nick = strdup(str);
770
771 if (nick && (DefConLevel < 5)) {
772 DefConProcess(GetUserH(nick));
773
774 if (DefConTimeOut > 0)
775 timeq_add(now + DefConTimeOut, defcon_timeout, NULL);
776 }
d76ed9a9 777}
778
956ee72d 779void
780irc_rpong(const char *from1, const char *from2, const char *pingtime, const char *clientinfo)
781{
782 putsock("%s " P10_RPONG " %s %s %s :%s", self->numeric, from1, from2, pingtime, clientinfo);
783}
784
d76ed9a9 785void
786irc_ping(const char *payload)
787{
788 putsock("%s " P10_PING " :%s", self->numeric, payload);
789}
790
791void
792irc_pong(const char *who, const char *data)
793{
794 putsock("%s " P10_PONG " %s :%s", self->numeric, who, data);
795}
796
2f61d1d7 797void
798irc_pong_asll(const char *who, const char *orig_ts)
799{
800 char *delim;
801 struct timeval orig;
1136f709 802 struct timeval sys_now;
2f61d1d7 803 int diff;
804
805 orig.tv_sec = strtoul(orig_ts, &delim, 10);
806 orig.tv_usec = (*delim == '.') ? strtoul(delim + 1, NULL, 10) : 0;
1136f709 807 gettimeofday(&sys_now, NULL);
808 diff = (sys_now.tv_sec - orig.tv_sec) * 1000 + (sys_now.tv_usec - orig.tv_usec) / 1000;
809 putsock("%s " P10_PONG " %s %s %d %lu.%06lu", self->numeric, who, orig_ts, diff, (unsigned long)sys_now.tv_sec, (unsigned long)sys_now.tv_usec);
2f61d1d7 810}
811
d76ed9a9 812void
813irc_pass(const char *passwd)
814{
815 putsock(P10_PASS " :%s", passwd);
816}
817
818void
819irc_introduce(const char *passwd)
820{
821 void timed_send_ping(void *data);
822
823 self->self_burst = self->burst = 1;
824 irc_pass(passwd);
825 irc_server(self);
826 burst_length = 0;
827 timeq_add(now + ping_freq, timed_send_ping, 0);
828}
829
830void
9a75756e 831irc_gline(struct server *srv, struct gline *gline, int silent)
d76ed9a9 832{
995043b4 833 putsock("%s " P10_GLINE " %s +%s " FMT_TIME_T " " FMT_TIME_T " :%s<%s> %s",
834 self->numeric, (srv ? srv->numeric : "*"), gline->target, gline->expires-now, now, silent ? "AUTO " : "", gline->issuer, gline->reason);
d76ed9a9 835}
836
d914d1cb 837void
838irc_shun(struct server *srv, struct shun *shun)
839{
18ec491d
MB
840 putsock("%s " P10_SHUN " %s +%s " FMT_TIME_T " " FMT_TIME_T " :<%s> %s",
841 self->numeric, (srv ? srv->numeric : "*"), shun->target, shun->expires-now, now, shun->issuer, shun->reason);
d914d1cb 842}
843
d76ed9a9 844void
845irc_settime(const char *srv_name_mask, time_t new_time)
846{
847 ioset_set_time(new_time);
848 if (!strcmp(srv_name_mask, "*"))
849 srv_name_mask = "";
850 putsock("%s " P10_SETTIME " " FMT_TIME_T " %s", self->numeric, new_time, srv_name_mask);
851}
852
853void
854irc_ungline(const char *mask)
855{
f2e03a28 856 putsock("%s " P10_GLINE " * -%s " FMT_TIME_T, self->numeric, mask, now);
d76ed9a9 857}
858
d914d1cb 859void
860irc_unshun(const char *mask)
861{
18ec491d 862 putsock("%s " P10_SHUN " * -%s " FMT_TIME_T, self->numeric, mask, now);
d914d1cb 863}
864
e206ed60 865void
d76ed9a9 866irc_burst(struct chanNode *chan)
867{
868 char burst_line[512];
869 int pos, base_len, len;
870 struct modeNode *mn;
871 struct banNode *bn;
2aef5f4b 872 struct exemptNode *en;
d76ed9a9 873 long last_mode=-1;
1136f709 874 unsigned int first_ban;
875 unsigned int first_exempt;
d76ed9a9 876 unsigned int n;
877
878 base_len = sprintf(burst_line, "%s " P10_BURST " %s " FMT_TIME_T " ",
879 self->numeric, chan->name, chan->timestamp);
880 len = irc_make_chanmode(chan, burst_line+base_len);
881 pos = base_len + len;
1136f709 882 if (len > 0 && chan->members.used > 0)
d76ed9a9 883 burst_line[pos++] = ' ';
884
d9d33535 885 if(chan->members.used < 1)
886 return; /* dont burst empty channels (created by discrims) */
d76ed9a9 887 /* dump the users */
888 for (n=0; n<chan->members.used; n++) {
889 mn = chan->members.list[n];
890 if (pos > 500) {
891 burst_line[pos-1] = 0; /* -1 to back up over the space or comma */
892 putsock("%s", burst_line);
893 pos = base_len;
894 last_mode = -1;
895 }
c99dcaf6 896
d76ed9a9 897 memcpy(burst_line+pos, mn->user->numeric, strlen(mn->user->numeric));
898 pos += strlen(mn->user->numeric);
899 if (mn->modes && (mn->modes != last_mode)) {
900 last_mode = mn->modes;
901 burst_line[pos++] = ':';
902 if (last_mode & MODE_CHANOP)
903 burst_line[pos++] = 'o';
55342ce8 904 if (last_mode & MODE_HALFOP)
905 burst_line[pos++] = 'h';
d76ed9a9 906 if (last_mode & MODE_VOICE)
907 burst_line[pos++] = 'v';
908 }
909 if ((n+1)<chan->members.used)
910 burst_line[pos++] = ',';
911 }
d1a70383
MB
912
913 if (len > 0 && (chan->banlist.used > 0 || chan->exemptlist.used > 0))
914 burst_line[pos++] = ' ';
915
d76ed9a9 916 if (chan->banlist.used) {
917 /* dump the bans */
1136f709 918 first_ban = 1;
d76ed9a9 919
1136f709 920 for (n=0; n<chan->banlist.used; ) {
921 if (first_ban && (pos < 500)) {
922 burst_line[pos++] = ':';
923 burst_line[pos++] = '%';
924 }
d76ed9a9 925 bn = chan->banlist.list[n];
926 len = strlen(bn->ban);
1136f709 927 if (pos + 2 + len < 505) {
928 memcpy(burst_line + pos, bn->ban, len);
929 pos += len;
930 burst_line[pos++] = ' ';
931 first_ban = 0;
932 n++;
933 } else {
934 burst_line[pos-1] = 0;
d76ed9a9 935 putsock("%s", burst_line);
936 pos = base_len;
1136f709 937 first_ban = 1;
d76ed9a9 938 }
d76ed9a9 939 }
940 }
d1a70383 941
2aef5f4b 942 if (chan->exemptlist.used) {
1136f709 943 /* dump the exempts */
944 first_exempt = 1;
945
946 for (n=0; n<chan->exemptlist.used; ) {
947 if (first_exempt && (pos < 500)) {
d1a70383
MB
948 if (chan->banlist.used < 1) {
949 burst_line[pos++] = ':';
950 burst_line[pos++] = '%';
951 burst_line[pos++] = ' ';
952 }
1136f709 953 burst_line[pos++] = '~';
954 burst_line[pos++] = ' ';
955 }
2aef5f4b 956 en = chan->exemptlist.list[n];
957 len = strlen(en->exempt);
1136f709 958 if (pos + 2 + len < 505) {
959 memcpy(burst_line + pos, en->exempt, len);
960 pos += len;
961 burst_line[pos++] = ' ';
962 first_exempt = 0;
963 n++;
964 } else {
965 burst_line[pos-1] = 0;
2aef5f4b 966 putsock("%s", burst_line);
967 pos = base_len;
1136f709 968 first_exempt = 1;
2aef5f4b 969 }
2aef5f4b 970 }
971 }
d76ed9a9 972 /* print the last line */
973 burst_line[pos] = 0;
974 putsock("%s", burst_line);
975}
976
977void
978irc_quit(struct userNode *user, const char *message)
979{
980 putsock("%s " P10_QUIT " :%s", user->numeric, message);
981}
982
983void
984irc_error(const char *to, const char *message)
985{
986 if (to) {
987 putsock("%s " P10_ERROR " :%s", to, message);
988 } else {
989 putsock(":%s " P10_ERROR " :%s", self->name, message);
990 }
991}
992
993void
994irc_kill(struct userNode *from, struct userNode *target, const char *message)
995{
996 if (from) {
997 putsock("%s " P10_KILL " %s :%s!%s (%s)",
998 from->numeric, target->numeric, self->name, from->nick, message);
999 } else {
1000 putsock("%s " P10_KILL " %s :%s (%s)",
1001 self->numeric, target->numeric, self->name, message);
1002 }
1003}
1004
1005void
1006irc_mode(struct userNode *from, struct chanNode *target, const char *modes)
1007{
ec311f39 1008 call_channel_mode_funcs(from, target, (char **)&modes, 0);
d76ed9a9 1009 putsock("%s " P10_MODE " %s %s "FMT_TIME_T,
1010 (from ? from->numeric : self->numeric),
1011 target->name, modes, target->timestamp);
1012}
1013
5a1daaab 1014/* Added to allow services to mode users
1015 2005 - 8 - 10 by Life4Christ
1016*/
1017void
1018irc_umode(struct userNode *target, const char *modes)
1019{
1020 putsock("%s " P10_MODE " %s %s ",self->numeric,target->nick, modes);
1021}
1022
1023
d76ed9a9 1024void
1025irc_invite(struct userNode *from, struct userNode *who, struct chanNode *to)
1026{
1027 putsock("%s " P10_INVITE " %s %s", from->numeric, who->nick, to->name);
1028}
1029
5177fd21 1030void
1031irc_silence(struct userNode *who, const char *mask, int add)
1032{
668dc38e 1033 putsock("%s " P10_SILENCE " %s %s%s", self->numeric, who->numeric, add ? "+" : "-", mask);
5177fd21 1034}
1035
d76ed9a9 1036void
1037irc_join(struct userNode *who, struct chanNode *what)
1038{
1039 if (what->members.used == 1) {
a837dcf5 1040 putsock("%s " P10_CREATE " %s "FMT_TIME_T,
d76ed9a9 1041 who->numeric, what->name, what->timestamp);
1042 } else {
a837dcf5 1043 putsock("%s " P10_JOIN " %s "FMT_TIME_T, who->numeric, what->name, what->timestamp);
d76ed9a9 1044 }
1045}
1046
cd25f2e9 1047void
1048irc_svsjoin(struct userNode *from, struct userNode *who, struct chanNode *to)
1049{
1050 putsock("%s " P10_SVSJOIN " %s %s "FMT_TIME_T, from->uplink->numeric, who->numeric, to->name, now);
1051}
1052
39c1a4ef 1053void
1054irc_svspart(struct userNode *from, struct userNode *who, struct chanNode *to)
1055{
1056 putsock("%s " P10_SVSPART " %s %s", from->uplink->numeric, who->numeric, to->name);
1057}
1058
5e6460e4 1059void
f20f0ab5 1060irc_svsquit(struct userNode *from, struct userNode *who, char const *reason)
5e6460e4 1061{
1062 putsock("%s " P10_SVSQUIT " %s :%s", from->uplink->numeric, who->numeric, reason);
1063}
1064
d76ed9a9 1065void
1066irc_kick(struct userNode *who, struct userNode *target, struct chanNode *channel, const char *msg)
1067{
1068 const char *numeric;
1069 struct modeNode *mn = GetUserMode(channel, who);
1070 numeric = ((mn && (mn->modes & MODE_CHANOP)) || off_channel) ? who->numeric : self->numeric;
1071 putsock("%s " P10_KICK " %s %s :%s",
1072 numeric, channel->name, target->numeric, msg);
1073}
1074
1075void
1076irc_stats(struct userNode *from, struct server *target, char type)
1077{
1078 putsock("%s " P10_STATS " %c :%s", from->numeric, type, target->numeric);
1079}
1080
1081void
1082irc_svsnick(struct userNode *from, struct userNode *target, const char *newnick)
1083{
1084 putsock("%s " P10_SVSNICK " %s %s "FMT_TIME_T, from->uplink->numeric, target->numeric, newnick, now);
1085}
1086
56958740 1087void
1088irc_swhois(struct userNode *from, struct userNode *target, const char *message)
1089{
1090 putsock("%s " P10_SWHOIS " %s %s%s", from->uplink->numeric, target->numeric, message ? ":" : "",
1091 message ? message : "");
1092
1093}
1094
d76ed9a9 1095void
1096irc_part(struct userNode *who, struct chanNode *what, const char *reason)
1097{
1098 if (reason) {
1099 putsock("%s " P10_PART " %s :%s", who->numeric, what->name, reason);
1100 } else {
1101 putsock("%s " P10_PART " %s", who->numeric, what->name);
1102 }
1103}
1104
1105void
7fda2b52 1106irc_topic(struct userNode *service, struct userNode *who, struct chanNode *what, const char *topic)
d76ed9a9 1107{
7fda2b52 1108
5ee4dc2b
MB
1109 int type = 4, host_in_topic = 0, hasident = 0, hhtype = 0;
1110 const char *hstr, *tstr, *hhstr, *htstr;
eb43ca8c 1111 char *host, *hostmask;
1112 char shost[MAXLEN];
1113 char sident[MAXLEN];
5006460e 1114
1115 tstr = conf_get_data("server/type", RECDB_QSTRING);
1116 hstr = conf_get_data("server/host_in_topic", RECDB_QSTRING);
5ee4dc2b
MB
1117 hhstr = conf_get_data("server/hidden_host", RECDB_QSTRING);
1118 htstr = conf_get_data("server/hidden_host_type", RECDB_QSTRING);
5006460e 1119 if(tstr)
1120 type = atoi(tstr);
b5c80c61 1121 else
1122 type = 4;/* default to 040 style topics */
5ee4dc2b
MB
1123 if (htstr)
1124 hhtype = atoi(htstr);
a20238a2 1125
eb43ca8c 1126 if (hstr) {
5ee4dc2b 1127 if (IsHiddenHost(who) && IsFakeHost(who))
eb43ca8c 1128 safestrncpy(shost, who->fakehost, sizeof(shost));
5ee4dc2b 1129 else if (IsHiddenHost(who) && IsSetHost(who)) {
eb43ca8c 1130 hostmask = strdup(who->sethost);
da4f9236 1131 if ((host = (strrchr(hostmask, '@')))) {
1132 hasident = 1;
eb43ca8c 1133 *host++ = '\0';
da4f9236 1134 } else
eb43ca8c 1135 host = hostmask;
1136
da4f9236 1137 if (hasident)
1138 safestrncpy(sident, hostmask, sizeof(shost));
1139 else
1140 safestrncpy(sident, who->ident, sizeof(shost));
1141
eb43ca8c 1142 safestrncpy(shost, host, sizeof(shost));
5b360094 1143 } else if (IsHiddenHost(who) && ((hhtype == 1) || (hhtype == 3)) && who->handle_info && hhstr) {
5ee4dc2b 1144 snprintf(shost, sizeof(shost), "%s.%s", who->handle_info->handle, hhstr);
5b360094 1145 } else if (IsHiddenHost(who) && ((hhtype == 2) || (hhtype == 3)) && who->crypthost[0]) {
5ee4dc2b 1146 safestrncpy(shost, who->crypthost, sizeof(shost));
eb43ca8c 1147 } else
1148 safestrncpy(shost, who->hostname, sizeof(shost));
1149
1150 host_in_topic = atoi(hstr);
1151 }
5006460e 1152
dd019452 1153 if (type >= 5) {
5006460e 1154 putsock("%s " P10_TOPIC " %s %s%s%s%s%s " FMT_TIME_T " " FMT_TIME_T " :%s", service->numeric, what->name,
eb43ca8c 1155 who->nick, host_in_topic ? "!" : "", host_in_topic ? (IsSetHost(who) ? sident : who->ident) : "",
1156 host_in_topic ? "@" : "", host_in_topic ? shost : "", what->timestamp, now, topic);
a20238a2 1157 } else {
b5c80c61 1158 who = service;
a20238a2 1159 putsock("%s " P10_TOPIC " %s :%s", who->numeric, what->name, topic);
1160 }
d76ed9a9 1161}
1162
1163void
1164irc_raw(const char *what)
1165{
1166 putsock("%s", what);
1167}
1168
1169void
1170irc_numeric(struct userNode *user, unsigned int num, const char *format, ...)
1171{
1172 va_list arg_list;
1173 char buffer[MAXLEN];
1174 va_start(arg_list, format);
1175 vsnprintf(buffer, MAXLEN-2, format, arg_list);
1176 buffer[MAXLEN-1] = 0;
1177 putsock(":%s %03d %s %s", self->name, num, user->nick, buffer);
1178}
1179
d82cf2f0 1180void
1181irc_mark(struct userNode *user, char *mark)
1182{
1183 char *host = user->hostname;
b66855e2
MB
1184 int type = 4;
1185 const char *tstr = NULL;
df7819d9
MB
1186 unsigned int ii = 0;
1187 int markfound = 0;
2c00fbc2 1188
b66855e2
MB
1189 tstr = conf_get_data("server/type", RECDB_QSTRING);
1190 if(tstr)
1191 type = atoi(tstr);
1192 else
1193 type = 4;
1194
df7819d9
MB
1195 if (user->marks)
1196 for (ii=0; ii<user->marks->used; ii++)
1197 if (!irccasecmp(user->marks->list[ii], mark))
1198 markfound = 1;
1199
1200 if (!markfound)
1201 {
1202 if (!user->marks)
1203 user->marks = alloc_string_list(1);
1204 string_list_append(user->marks, strdup(mark));
1205 }
1206
b66855e2
MB
1207 if (type >= 9)
1208 {
1209 putsock("%s " CMD_MARK " %s MARK %s", self->numeric, user->nick, mark);
1210 return;
1211 }
1212
df7819d9
MB
1213 /* TODO: Allow mark overwrite. If they are marked, and their fakehost is oldmark.hostname, update it to newmark.hostname so mark can be called multiple times. Probably requires ircd modification also */
1214 if(user->mark)
1215 return;
1216
d82cf2f0 1217 /* if the mark will put us over the host length, clip some off the left hand side
1218 * to make room...
1219 */
1220 if(strlen(host) + 1 + strlen(mark) > HOSTLEN)
1221 host += 1 + ( (strlen(host) + 1 + strlen(mark)) - HOSTLEN );
1222 putsock("%s " CMD_MARK " %s DNSBL +m %s.%s", self->numeric, user->nick, mark, host);
1223 putsock("%s " CMD_MARK " %s DNSBL_DATA %s", self->numeric, user->nick, mark);
2c00fbc2 1224
1225 /* Save it in the user */
1226 user->mark = strdup(mark);
1227
d82cf2f0 1228 /* If they are not otherwise marked, mark their host with fakehost */
537128ea 1229 if(!IsFakeHost(user) && !IsSetHost(user) && !(IsHiddenHost(user) && user->handle_info) )
d82cf2f0 1230 {
c99dcaf6 1231 struct modeNode *mn = NULL;
1232 char fakehost[HOSTLEN];
1233 unsigned int count = 0;
1234 unsigned int n = 0;
1235
d82cf2f0 1236 putsock("%s " CMD_FAKEHOST " %s %s.%s", self->numeric, user->numeric, mark, host);
1b6b5e2f 1237 putsock("%s " CMD_MODE " %s +x", self->numeric, user->nick);
c99dcaf6 1238
1239 snprintf(fakehost, sizeof(fakehost), "%s.%s", mark, host);
1240 safestrncpy(user->fakehost, fakehost, sizeof(user->fakehost));
1241
1242 for (n=count=0; n<user->channels.used; n++) {
1243 mn = user->channels.list[n];
1b6b5e2f 1244 if (strlen(mn->channel->name) >= 1) /* Sanity */
1245 check_bans(user, mn->channel->name);
c99dcaf6 1246 }
d82cf2f0 1247 }
1248}
1249
10be9be0 1250void irc_sno(unsigned int mask, char const* format, ...) {
1251 va_list arg_list;
1252 char buffer[MAXLEN];
1253 va_start(arg_list, format);
1254 vsnprintf(buffer, MAXLEN-2, format, arg_list);
1255 buffer[MAXLEN-1] = 0;
1256 putsock("%s " CMD_SNO " %d :%s", self->numeric, mask, buffer);
1257}
1258
c8b4b87b
MB
1259void
1260irc_sasl(struct server* dest, const char *identifier, const char *subcmd, const char *data)
1261{
2c4ce77b 1262 putsock("%s " P10_SASL " %s %s %s %s", self->numeric, dest->numeric, identifier, subcmd, data);
c8b4b87b
MB
1263}
1264
d76ed9a9 1265static void send_burst(void);
1266
1267static void
1268change_nicklen(int new_nicklen)
1269{
1270 unsigned int nn;
1271 char new_nick[NICKLEN+1];
1272 struct userNode *user;
1273
1274 nicklen = new_nicklen;
1275 /* fix up any users we have here */
1276 for (nn=0; nn<=self->num_mask; nn++) {
1277 if (!(user = self->users[nn]))
1278 continue;
1279 safestrncpy(new_nick, user->nick, sizeof(new_nick));
1280 new_nick[nicklen] = 0;
1281 NickChange(user, new_nick, 1);
1282 }
1283}
1284
1285static CMD_FUNC(cmd_whois)
1286{
1287 struct userNode *from;
1288 struct userNode *who;
1136f709 1289 char buf[MAXLEN];
1290 unsigned int i, mlen, len;
d76ed9a9 1291
1292 if (argc < 3)
1293 return 0;
1294 if (!(from = GetUserH(origin))) {
1295 log_module(MAIN_LOG, LOG_ERROR, "Could not find WHOIS origin user %s", origin);
1296 return 0;
1297 }
1136f709 1298 if (!(who = GetUserH(argv[2]))) {
d76ed9a9 1299 irc_numeric(from, ERR_NOSUCHNICK, "%s@%s :No such nick", argv[2], self->name);
1300 return 1;
1301 }
1136f709 1302
1303 if (IsFakeHost(who) && IsHiddenHost(who))
1304 irc_numeric(from, RPL_WHOISUSER, "%s %s %s * :%s", who->nick, who->ident, who->fakehost, who->info);
1305 else if (IsHiddenHost(who) && who->handle_info && hidden_host_suffix)
1306 irc_numeric(from, RPL_WHOISUSER, "%s %s %s.%s * :%s", who->nick, who->ident, who->handle_info->handle, hidden_host_suffix, who->info);
1307 else
1308 irc_numeric(from, RPL_WHOISUSER, "%s %s %s * :%s", who->nick, who->ident, who->hostname, who->info);
1309
b6f5999f 1310 if (!IsService(who) || (from == who)) {
1136f709 1311 struct modeNode *mn;
1312 mlen = strlen(self->name) + strlen(from->nick) + 12 + strlen(who->nick);
1313 len = 0;
1314 *buf = '\0';
1315 for (i = who->channels.used; i > 0; )
1316 {
1317 mn = who->channels.list[--i];
1318
1319 if (!IsOper(from) && (mn->channel->modes & (MODE_PRIVATE | MODE_SECRET)) && !GetUserMode(mn->channel, from))
1320 continue;
1321
1322 if (len + strlen(mn->channel->name) + mlen > MAXLEN - 5)
1323 {
1324 irc_numeric(from, RPL_WHOISCHANNELS, "%s :%s", who->nick, buf);
1325 *buf = '\0';
1326 len = 0;
1327 }
1328
1329 if (IsDeaf(who))
1330 *(buf + len++) = '-';
1331 if ((mn->channel->modes & (MODE_PRIVATE | MODE_SECRET)) && !GetUserMode(mn->channel, from))
1332 *(buf + len++) = '*';
1333 if (mn->modes & MODE_CHANOP)
1334 *(buf + len++) = '@';
1335 else if (mn->modes & MODE_HALFOP)
1336 *(buf + len++) = '%';
1337 else if (mn->modes & MODE_VOICE)
1338 *(buf + len++) = '+';
1339
1340 if (len)
1341 *(buf + len) = '\0';
1342 strcpy(buf + len, mn->channel->name);
1343 len += strlen(mn->channel->name);
1344 strcat(buf + len, " ");
1345 len++;
1346 }
1347 if (buf[0] != '\0')
1348 irc_numeric(from, RPL_WHOISCHANNELS, "%s :%s", who->nick, buf);
d76ed9a9 1349 }
1136f709 1350
1351 if (his_servername && his_servercomment && !IsOper(from) && from != who)
a32da4c7 1352 irc_numeric(from, RPL_WHOISSERVER, "%s %s :%s", who->nick, his_servername, his_servercomment);
d76ed9a9 1353 else
a32da4c7 1354 irc_numeric(from, RPL_WHOISSERVER, "%s %s :%s", who->nick, who->uplink->name, who->uplink->description);
1136f709 1355
1356 if (IsAway(who))
1357 irc_numeric(from, RPL_AWAY, "%s :Away", who->nick);
a32da4c7 1358 if (IsOper(who))
1136f709 1359 irc_numeric(from, RPL_WHOISOPERATOR, "%s :%s", who->nick, IsLocal(who) ? "is a megalomaniacal power hungry tyrant" : "is an IRC Operator");
1360 if (who->handle_info)
1361 irc_numeric(from, RPL_WHOISACCOUNT, "%s %s :is logged in as", who->nick, who->handle_info->handle);
1362 if (IsHiddenHost(who) && who->handle_info && (IsOper(from) || from == who))
1363 irc_numeric(from, RPL_WHOISACTUALLY, "%s %s@%s %s :Actual user@host, Actual IP", who->nick, who->ident, who->hostname, irc_ntoa(&who->ip));
1364 if (IsLocal(who) && !IsService(who) && (IsOper(from) || from == who))
1365 irc_numeric(from, RPL_WHOISIDLE, "%s %ld %ld :seconds idle, signon time", who->nick, now - who->idle_since, who->timestamp);
1366
d76ed9a9 1367 irc_numeric(from, RPL_ENDOFWHOIS, "%s :End of /WHOIS list", who->nick);
1368 return 1;
1369}
1370
1371static CMD_FUNC(cmd_server)
1372{
1373 struct server *srv;
1374 const char *str;
1375
1376 if (argc < 8)
1377 return 0;
a32da4c7 1378 if (self->uplink) {
d76ed9a9 1379 /* another server introduced us */
1380 srv = AddServer(GetServerH(origin), argv[1], atoi(argv[2]), atoi(argv[3]), atoi(argv[4]), argv[6], argv[argc-1]);
1381 if (!srv)
1382 return 0;
1383 srv->self_burst = argv[5][0] == 'J';
1384 srv->burst = 1;
1385 } else {
1386 /* this must be our uplink */
1387 srv = self->uplink = AddServer(self, argv[1], atoi(argv[2]), atoi(argv[3]), atoi(argv[4]), argv[6], argv[argc-1]);
1388 if (!srv)
1389 return 0;
1390 srv->self_burst = argv[5][0] == 'J';
1391 srv->burst = 1;
1392 if ((argv[7][0] == '+') && !force_n2k) {
1393 log_module(MAIN_LOG, LOG_WARNING, "Got Undernet-style SERVER message but \"force_n2k\" not on.");
1394 }
1395 send_burst();
1396 }
1397
1398 /* Fix up our timestamps if necessary. */
1399 if (srv->boot <= PREHISTORY) {
1400 /* Server from the mists of time.. */
1401 if (srv->hops == 1) {
1402 log_module(MAIN_LOG, LOG_ERROR, "Server %s claims to have booted at time "FMT_TIME_T". This is absurd.", srv->name, srv->boot);
1403 }
1404 } else if ((str = conf_get_data("server/reliable_clock", RECDB_QSTRING))
1405 && enabled_string(str)) {
1406 /* If we have a reliable clock, we just keep our current time. */
1407 } else {
1408 if (srv->boot <= self->boot) {
1409 /* The other server is older than us. Accept their timestamp.
1410 * Alternately, we are same age, but we accept their time
1411 * since we are linking to them. */
1412 self->boot = srv->boot;
1136f709 1413 ioset_set_time(srv->link_time
1414);
d76ed9a9 1415 }
1416 }
1417 if (srv == self->uplink) {
1418 extern time_t burst_begin;
1419 burst_begin = now;
1420 }
1421 return 1;
1422}
1423
47956fc5 1424
d76ed9a9 1425static CMD_FUNC(cmd_eob)
1426{
1427 struct server *sender;
1428 dict_iterator_t it;
d76ed9a9 1429
1430 if (!(sender = GetServerH(origin)))
1431 return 0;
1432 if (sender == self->uplink) {
1433 cManager.uplink->state = CONNECTED;
1434 for (it = dict_first(unbursted_channels); it; it = iter_next(it))
1435 irc_burst(iter_data(it));
1436 dict_delete(unbursted_channels);
1437 unbursted_channels = NULL;
1438 irc_eob();
1439 irc_eob_ack();
47956fc5 1440
1441 /* now that we know who our uplink is,
1442 * we can center the routing map and activate auto-routing.
1443 */
9079d26c 1444 //activate_routing(NULL, NULL, NULL);
1445 routing_init();
d76ed9a9 1446 }
1447 sender->self_burst = 0;
1448 recalc_bursts(sender);
9d59f196 1449 call_server_link_funcs(sender);
47956fc5 1450 /* let auto-routing figure out if we were
1451 * wating on this server to link a child to it */
1452 /* DONT call this if uplink is _US_ */
1453 if(sender->uplink != self)
1454 routing_handle_connect(sender->name, sender->uplink->name);
d76ed9a9 1455 return 1;
1456}
1457
1458static CMD_FUNC(cmd_eob_ack)
1459{
1460 extern time_t burst_begin;
1461
1462 if (GetServerH(origin) == self->uplink) {
1463 burst_length = now - burst_begin;
1464 self->self_burst = self->burst = 0;
1465 }
1466 cManager.uplink->state = CONNECTED;
1467 return 1;
1468}
1469
956ee72d 1470static CMD_FUNC(cmd_rping)
1471{
1472 struct server *dest;
1473
1474 if (!(dest = GetServerN(argv[1])))
74ac2349 1475 return 1; /* if its a jupe or something, return 1 so its silently ignored */
956ee72d 1476
1477 if (dest == self)
1478 irc_rpong(argv[2], argv[3], argv[4], argv[5]);
1479
1480 return 1;
1481}
1482
c8b4b87b
MB
1483static CMD_FUNC(cmd_sasl)
1484{
1485 struct server *serv;
1486
1487 if (argc < 5)
1488 return 0;
1489
1490 serv = GetServerH(origin);
1491 if (!serv)
1492 return 0;
1493
b8036263 1494 call_sasl_input_func(serv, argv[2], argv[3], argv[4], (argc>5 ? argv[argc-1] : NULL));
c8b4b87b
MB
1495
1496 return 1;
1497}
1498
d76ed9a9 1499static CMD_FUNC(cmd_ping)
1500{
1501 struct server *srv;
1502 struct userNode *un;
1503
2f61d1d7 1504 if (argc > 3)
1505 irc_pong_asll(argv[2], argv[3]);
d76ed9a9 1506 else if ((srv = GetServerH(origin)))
1507 irc_pong(self->name, srv->numeric);
1508 else if ((un = GetUserH(origin)))
1509 irc_pong(self->name, un->numeric);
1510 else
1511 irc_pong(self->name, origin);
1512
1513 timeq_del(0, timed_send_ping, 0, TIMEQ_IGNORE_WHEN|TIMEQ_IGNORE_DATA);
1514 timeq_del(0, timed_ping_timeout, 0, TIMEQ_IGNORE_WHEN|TIMEQ_IGNORE_DATA);
1515 timeq_add(now + ping_freq, timed_send_ping, 0);
1516 received_ping();
1517 return 1;
1518}
1519
1520static CMD_FUNC(cmd_error_nick)
1521{
1522 /* Go back to original IRC length .. and try to reconnect :/ */
1523 change_nicklen(9);
1524 irc_squit(self, "Got erroneous nickname, truncating nicks.", NULL);
1525 return 1;
1526}
1527
1528struct create_desc {
1529 struct userNode *user;
1530 time_t when;
1531};
1532
1533static void
1534join_helper(struct chanNode *chan, void *data)
1535{
1536 struct create_desc *cd = data;
1537 AddChannelUser(cd->user, chan);
1538}
1539
1540static void
1541create_helper(char *name, void *data)
1542{
1543 struct create_desc *cd = data;
1544
1545 if (!strcmp(name, "0")) {
1546 while (cd->user->channels.used > 0)
1547 DelChannelUser(cd->user, cd->user->channels.list[0]->channel, 0, 0);
1548 return;
1549 }
1550
2aef5f4b 1551 AddChannelUser(cd->user, AddChannel(name, cd->when, NULL, NULL, NULL));
d76ed9a9 1552}
1553
1554static CMD_FUNC(cmd_create)
1555{
1556 struct create_desc cd;
1557 struct userNode *user;
1558
1559 if ((argc < 3) || !(user = GetUserH(origin)))
1560 return 0;
1561 cd.user = user;
1562 cd.when = atoi(argv[2]);
1563 parse_foreach(argv[1], join_helper, create_helper, NULL, NULL, &cd);
1564 return 1;
1565}
1566
1567static CMD_FUNC(cmd_join)
1568{
1569 struct create_desc cd;
1570
1571 if (!(cd.user = GetUserH(origin)))
1572 return 0;
1573 if (argc < 2)
1574 return 0;
1575 else if (argc < 3)
1576 cd.when = now;
1577 else
1578 cd.when = atoi(argv[2]);
1579 parse_foreach(argv[1], join_helper, create_helper, NULL, NULL, &cd);
1580 return 1;
1581}
1582
cd25f2e9 1583static CMD_FUNC(cmd_svsjoin)
1584{
1585 struct create_desc cd;
1586
1587 if (!(cd.user = GetUserH(argv[1])))
1588 return 0;
1589 if (argc < 3)
1590 return 0;
1591 else if (argc < 4)
1592 cd.when = now;
1593 else
1594 cd.when = atoi(argv[3]);
1595 parse_foreach(argv[2], join_helper, create_helper, NULL, NULL, &cd);
1596 return 1;
1597}
1598
d76ed9a9 1599static CMD_FUNC(cmd_pong)
1600{
1601 if (argc < 3)
1602 return 0;
1603 if (!strcmp(argv[2], self->name)) {
1604 timeq_del(0, timed_send_ping, 0, TIMEQ_IGNORE_WHEN|TIMEQ_IGNORE_DATA);
1605 timeq_del(0, timed_ping_timeout, 0, TIMEQ_IGNORE_WHEN|TIMEQ_IGNORE_DATA);
1606 timeq_add(now + ping_freq, timed_send_ping, 0);
1607 received_ping();
1608 }
1609 return 1;
1610}
1611
1612static CMD_FUNC(cmd_nick)
1613{
1614 struct userNode *user;
1615 if ((user = GetUserH(origin))) {
1616 /* nick change (since the source is a user numeric) */
1617 if (argc < 2)
1618 return 0;
1619 NickChange(user, argv[1], 1);
1620 } else {
1621 struct server *serv;
08895577 1622 struct userNode *nuser;
d76ed9a9 1623 char modes[MAXLEN];
1624 /* new nick */
1625 if (argc < 9)
1626 return 0;
1627 serv = GetServerH(origin);
1628 if (argc > 9)
1629 unsplit_string(argv+6, argc-9, modes);
1630 else
1631 strcpy(modes, "+");
08895577 1632 nuser = AddUser(serv, argv[1], argv[4], argv[5], modes, argv[argc-2], argv[argc-1], atoi(argv[3]), argv[argc-3]);
d76ed9a9 1633 }
1634 return 1;
1635}
1636
1637static CMD_FUNC(cmd_account)
1638{
1639 struct userNode *user;
d9cd0e9d 1640 struct server *server;
b21e2cfe 1641 struct handle_info *hi;
d76ed9a9 1642
d9cd0e9d 1643 if ((argc < 3) || !origin || !(server = GetServerH(origin)))
d76ed9a9 1644 return 0; /* Origin must be server. */
d9cd0e9d 1645
1646 /* This next line appears to tremple origin.. why? */
d76ed9a9 1647 user = GetUserN(argv[1]);
1648 if (!user)
1649 return 1; /* A QUIT probably passed the ACCOUNT. */
805e7c7a 1650
1651 if(!extended_accounts) /* any need for this function without? */
1652 return 1;
d9cd0e9d 1653
1654 if(!strcmp(argv[2],"C"))
1655 {
1679a283 1656 if((hi = loc_auth(NULL, argv[4], argv[5], NULL)))
7dd05763 1657 {
1658 /* Return a AC A */
a837dcf5 1659 putsock("%s " P10_ACCOUNT " %s A %s "FMT_TIME_T, self->numeric, server->numeric , argv[3], hi->registered);
7dd05763 1660
50220e57 1661 log_module(MAIN_LOG, LOG_DEBUG, "loc_auth: %s\n", user->nick);
50220e57 1662 }
7dd05763 1663 else
1664 {
1665 /* Return a AC D */
1666 putsock("%s " P10_ACCOUNT " %s D %s", self->numeric, server->numeric , argv[3]);
1667 }
1668 return 1;
1669 }
1670 else if(!strcmp(argv[2],"H")) /* New enhanced (host) version of C */
1671 {
1679a283 1672 if((hi = loc_auth(NULL, argv[5], argv[6], argv[4] )))
1673 {
1674 /* Return a AC A */
1675 putsock("%s " P10_ACCOUNT " %s A %s "FMT_TIME_T, self->numeric, server->numeric , argv[3], hi->registered);
1676 }
1677 else
1678 {
1679 /* Return a AC D */
1680 putsock("%s " P10_ACCOUNT " %s D %s", self->numeric, server->numeric , argv[3]);
1681 }
1682 return 1;
1683 }
1684 else if(!strcmp(argv[2],"S"))
1685 {
1686 if((hi = loc_auth(argv[5], argv[6], argv[7], argv[4])))
d9cd0e9d 1687 {
1688 /* Return a AC A */
a837dcf5 1689 putsock("%s " P10_ACCOUNT " %s A %s "FMT_TIME_T, self->numeric, server->numeric , argv[3], hi->registered);
d9cd0e9d 1690 }
1691 else
1692 {
1693 /* Return a AC D */
1694 putsock("%s " P10_ACCOUNT " %s D %s", self->numeric, server->numeric , argv[3]);
1695 }
1696 return 1;
1697 }
1698 else if(!strcmp(argv[2],"R"))
1699 call_account_func(user, argv[3]);
1700 else
1701 call_account_func(user, argv[2]); /* For backward compatability */
d76ed9a9 1702 return 1;
1703}
1704
1705static CMD_FUNC(cmd_fakehost)
1706{
1707 struct userNode *user;
1708
1709 if ((argc < 3) || !origin || !GetServerH(origin))
1710 return 0;
1711 if (!(user = GetUserN(argv[1])))
1712 return 1;
1713 assign_fakehost(user, argv[2], 0);
1714 return 1;
1715}
1716
89d871d8 1717static struct {
1718 char *name;
1719 unsigned int priv;
1720} privtab[] = {
1721#define P(priv) { #priv, PRIV_ ## priv }
1722 P(CHAN_LIMIT), P(MODE_LCHAN), P(WALK_LCHAN), P(DEOP_LCHAN),
1723 P(SHOW_INVIS), P(SHOW_ALL_INVIS), P(UNLIMIT_QUERY), P(KILL),
1724 P(LOCAL_KILL), P(REHASH), P(RESTART), P(DIE),
1725 P(GLINE), P(LOCAL_GLINE), P(JUPE), P(LOCAL_JUPE),
1726 P(OPMODE), P(LOCAL_OPMODE), P(SET), P(WHOX),
1727 P(BADCHAN), P(LOCAL_BADCHAN), P(SEE_CHAN), P(PROPAGATE),
1728 P(DISPLAY), P(SEE_OPERS), P(WIDE_GLINE), P(FORCE_OPMODE),
1729 P(FORCE_LOCAL_OPMODE), P(REMOTEREHASH), P(CHECK), P(SEE_SECRET_CHAN),
625ccd4e 1730 P(SHUN), P(LOCAL_SHUN), P(WIDE_SHUN), P(ZLINE),
055f3f30 1731 P(LOCAL_ZLINE), P(WIDE_ZLINE), P(LIST_CHAN), P(WHOIS_NOTICE),
c9aeceb5 1732 P(HIDE_IDLE), P(XTRAOP), P(HIDE_CHANNELS), P(DISPLAY_MODE),
57cd328b
MB
1733 P(FREEFORM), P(REMOVE), P(SPAMFILTER), P(ADMIN),
1734 P(APASS_OPMODE), P(HIDE_OPER), P(REMOTE), P(SERVICE),
89d871d8 1735#undef P
1736 { 0, 0 }
1737};
1738
1739char *client_report_privs(struct userNode *client)
1740{
1741 int i;
1742
1743 privbuf[0] = '\0';
1744 for (i = 0; privtab[i].name; i++) {
1745 if (HasPriv(client, privtab[i].priv)) {
1746 strcat(privbuf, privtab[i].name);
1747 strcat(privbuf, " ");
1748 }
1749 }
1750
1751 privbuf[strlen(privbuf)] = 0;
1752
1753 return privbuf;
1754}
1755
9c88eebe 1756int clear_privs(struct userNode *who) {
1757 int i = 0;
1758 assert(0 != who);
1759
1760 for (i = 0; privtab[i].name; i++)
1761 RevokePriv(who, privtab[i].priv);
1762
1763 return 0;
1764}
1765
89d871d8 1766int client_modify_priv_by_name(struct userNode *who, char *priv, int what) {
1767 int i = 0;
1768 assert(0 != priv);
1769 assert(0 != who);
1770
1771 for (i = 0; privtab[i].name; i++) {
1772 if (0 == strcmp(privtab[i].name, priv)) {
1773 if (what == PRIV_ADD)
1774 GrantPriv(who, privtab[i].priv);
1775 else if (what == PRIV_DEL) {
1776 RevokePriv(who, privtab[i].priv);
1777 }
1778 }
1779 }
1780 return 0;
1781}
1782
6c34bb5a 1783int check_priv(char *priv)
1784{
1785 int i;
1786
1787 for (i = 0; privtab[i].name; i++) {
1788 if (0 == strcmp(privtab[i].name, priv)) {
1789 return 1;
1790 }
1791 }
1792 return 0;
1793}
1794
1795void
1796irc_privs(struct userNode *target, char *flag, int add)
1797{
1798 client_modify_priv_by_name(target, flag, add);
1799 putsock("%s " P10_PRIVS " %s %s%s", self->numeric, target->numeric, (add == PRIV_ADD) ? "+" : "-", flag);
1800}
1801
69517d70 1802void
6ae87c77 1803irc_raw_privs(struct userNode *target, const char *privs)
69517d70 1804{
1805 putsock("%s " P10_PRIVS " %s %s", self->numeric, target->numeric, privs);
1806}
1807
89d871d8 1808static CMD_FUNC(cmd_privs)
1809{
9c88eebe 1810 char *tstr = NULL;
1811 char buf[512] = "";
1812 char *p = 0;
1813 char *tmp;
89d871d8 1814
9c88eebe 1815 int what = PRIV_ADD;
1816 int type = 0;
89d871d8 1817
9c88eebe 1818 unsigned int i;
89d871d8 1819
9c88eebe 1820 struct server *sender;
1821 struct userNode *user;
89d871d8 1822
89d871d8 1823
9c88eebe 1824 tstr = conf_get_data("server/type", RECDB_QSTRING);
1825 if(tstr)
1826 type = atoi(tstr);
89d871d8 1827
89d871d8 1828
9c88eebe 1829 if (!(sender = GetServerH(origin))) { /* from oper */
1830 return 1; /* ignore as no services have privs set */
1831 } else { /* from server */
1832 if (type < 5)
1833 return 1; /* silently ignore */
1834
1835 user = argc > 1 ? GetUserN(argv[1]) : NULL;
1836
1837 if (!user)
1838 return 0;
1839
1840 for (i=1; i<argc; i++) {
1841 strcat(buf, argv[i]);
1842 strcat(buf, " ");
1843 }
1844
1845 for (i = 2; i < argc; i++) {
1846 if (*argv[i] == '+') { what = PRIV_ADD; argv[i]++; }
1847 if (*argv[i] == '-') { what = PRIV_DEL; argv[i]++; }
1848 for (tmp = x3_strtok(&p, argv[i], ","); tmp;
1849 tmp = x3_strtok(&p, NULL, ",")) {
1850 if (!strcmp(tmp, "PRIV_NONE")) {
c3915bdc 1851 if (now > user->timestamp+5)
1852 clear_privs(user);
9c88eebe 1853 break;
1854 } else
1855 client_modify_priv_by_name(user, tmp, what);
1856 }
1857 }
89d871d8 1858 }
9c88eebe 1859 return 1;
89d871d8 1860}
1861
d76ed9a9 1862static CMD_FUNC(cmd_burst)
1863{
1864 extern int rel_age;
2aef5f4b 1865 char modes[MAXLEN], *members = "";
1866 static char exemptlist[MAXLEN], banlist[MAXLEN];
a32da4c7 1867 unsigned int next = 3, res = 1;
2aef5f4b 1868 int ctype = 0, echeck = 0, bcheck = 0;
fa86a9e7 1869 struct chanData *cData;
d76ed9a9 1870 struct chanNode *cNode;
1871 struct userNode *un;
1872 struct modeNode *mNode;
1873 long mode;
2f61d1d7 1874 int oplevel = -1;
3e06ac80 1875 int type = 0;
d76ed9a9 1876 char *user, *end, sep;
3e06ac80 1877 char *tstr;
d76ed9a9 1878 time_t in_timestamp;
2aef5f4b 1879 char* parm = NULL;
d76ed9a9 1880
1881 if (argc < 3)
1882 return 0;
1883 modes[0] = 0;
2aef5f4b 1884
3e06ac80
MB
1885 tstr = conf_get_data("server/type", RECDB_QSTRING);
1886 if(tstr) {
1887 type = atoi(tstr);
1888 }
1889
2aef5f4b 1890 exemptlist[0] = 0;
1891 banlist[0] = 0;
1892
d76ed9a9 1893 while (next < argc) {
1894 switch (argv[next][0]) {
1895 case '+': {
1896 const char *pos;
1897 int n_modes;
a32da4c7 1898 for (pos=argv[next], n_modes = 1; *pos; pos++)
2f61d1d7 1899 if ((*pos == 'k') || (*pos == 'l') || (*pos == 'A')
3e06ac80 1900 || (*pos == 'U') || ((type > 7) && (*pos == 'L')))
d76ed9a9 1901 n_modes++;
1136f709 1902 if (next + n_modes > argc)
1903 n_modes = argc - next;
d76ed9a9 1904 unsplit_string(argv+next, n_modes, modes);
1905 next += n_modes;
1906 break;
1907 }
2aef5f4b 1908 case '%': {
3408efdc 1909 ctype = 1;
2aef5f4b 1910 for(parm = mysep(&argv[next], " "); /* parm = first param */
1911 parm; /* While param is not null */
1912 parm = mysep(&argv[next], " ") /* parm = next param */
1913 )
1914 {
3408efdc
MB
1915 if (0 == strcmp("~", parm))
1916 ctype = 2;
2aef5f4b 1917 if (ctype == 1) {
1918 if (bcheck == 0) {
1919 /* strip % char off start of very first ban */
23475fc6 1920 if (strlen(parm) > 1) {
1921 strncat(banlist, strtok(parm, "%"), sizeof(banlist) - 1 - strlen(banlist));
1922 strncat(banlist, " ", sizeof(banlist) - 1 - strlen(banlist));
1923 }
2aef5f4b 1924 bcheck = 1;
1925 } else {
1926 strncat(banlist, parm, sizeof(banlist) - 1 - strlen(banlist));
1927 strncat(banlist, " ", sizeof(banlist) - 1 - strlen(banlist));
1928 }
1929 } else if (ctype == 2) {
1930 if (echeck == 0) {
1931 echeck = 1;
1932 } else {
1933 strncat(exemptlist, parm, sizeof(exemptlist) - 1 - strlen(exemptlist));
1934 strncat(exemptlist, " ", sizeof(exemptlist) - 1 - strlen(exemptlist));
1935 }
1936 }
1937 }
1938 next++;
1939 break;
1940 }
a32da4c7 1941 default: members = argv[next++]; break;
2aef5f4b 1942 }
d76ed9a9 1943 }
1944
1945 in_timestamp = atoi(argv[2]);
1946 if ((cNode = dict_find(unbursted_channels, argv[1], NULL))) {
1947 cNode->timestamp = in_timestamp;
1948 dict_remove(unbursted_channels, cNode->name);
1949 irc_burst(cNode);
1950 }
2aef5f4b 1951 cNode = AddChannel(argv[1], in_timestamp, modes, banlist, exemptlist);
fa86a9e7 1952 cData = cNode->channel_info;
1953
1954 if (!cData) {
1955 if (cNode->modes & MODE_REGISTERED) {
1956 irc_join(opserv, cNode);
1957 irc_mode(opserv, cNode, "-z");
1958 irc_part(opserv, cNode, "");
1959 }
1960 }
2aef5f4b 1961
a32da4c7 1962 /* Burst channel members in now. */
d76ed9a9 1963 for (user = members, sep = *members, mode = 0; sep; user = end) {
1964 for (end = user + 3; isalnum(*end) || *end == '[' || *end == ']'; end++) ;
1965 sep = *end++; end[-1] = 0;
1966 if (sep == ':') {
1967 mode = 0;
1968 while ((sep = *end++)) {
2f61d1d7 1969 if (sep == 'o') {
d76ed9a9 1970 mode |= MODE_CHANOP;
2f61d1d7 1971 oplevel = -1;
1972 } else if (sep == 'h') {
55342ce8 1973 mode |= MODE_HALFOP;
2f61d1d7 1974 oplevel = -1;
1975 } else if (sep == 'v') {
d76ed9a9 1976 mode |= MODE_VOICE;
2f61d1d7 1977 oplevel = -1;
1978 } else if (isdigit(sep)) {
1117fc5a 1979 mode |= MODE_CHANOP;
2f61d1d7 1980 if (oplevel >= 0)
1981 oplevel += parse_oplevel(end);
1982 else
1983 oplevel = parse_oplevel(end);
1117fc5a 1984 while (isdigit(*end)) end++;
1985 } else
d76ed9a9 1986 break;
1987 }
1988 if (rel_age < 0)
1989 mode = 0;
1990 }
1991 if (!(un = GetUserN(user))) {
1992 res = 0;
1993 continue;
1994 }
2f61d1d7 1995 if ((mNode = AddChannelUser(un, cNode))) {
d76ed9a9 1996 mNode->modes = mode;
2f61d1d7 1997 mNode->oplevel = oplevel;
1998 }
d76ed9a9 1999 }
2000
2001 return res;
2002}
2003
d82cf2f0 2004/* TODO:
2005 * This is a stub that doesn't actually do anything. It should be completed
2006 * so that bans on *!*@markname.* match users as it does in nefarious
2007 */
2008static CMD_FUNC(cmd_mark)
2009{
df7819d9
MB
2010 const char *tstr;
2011 int type = 4;
d82cf2f0 2012 struct userNode *target;
2013 /*
2014 * log_module(MAIN_LOG, LOG_ERROR, "DEBUG: mark, user %s, type %s, arg %s", argv[1], argv[2], argv[3]);
2015 */
2016
df7819d9
MB
2017 tstr = conf_get_data("server/type", RECDB_QSTRING);
2018 if(tstr)
2019 type = atoi(tstr);
2020 else
2021 type = 4;/* default to 040 style topics */
2022
d82cf2f0 2023 if(argc < 4)
2024 return 0;
d82cf2f0 2025 if(!strcasecmp(argv[2], "DNSBL")) {
2026 /* DNSBL <modes> */
2027 return 1;
2028 }
df7819d9
MB
2029 else if(!strcasecmp(argv[2], "DNSBL_DATA") || !strcasecmp(argv[2], "MARK")) {
2030 int markfound = 0;
2031 unsigned int ii = 0;
d82cf2f0 2032 /* DNSBL_DATA name */
bc06f607 2033 target = GetUserH(argv[1]);
2034 if(!target) {
6d58d26f 2035 log_module(MAIN_LOG, LOG_ERROR, "Unable to find user %s whose dnsbl mark is changing.", argv[1]);
bc06f607 2036 return 0;
2037 }
df7819d9
MB
2038 if (type >= 9) {
2039 if (target->marks)
2040 for (ii=0; ii<target->marks->used; ii++)
2041 if (!irccasecmp(target->marks->list[ii], argv[3]))
2042 markfound = 1;
2043 if (!markfound)
2044 {
2045 if (!target->marks)
2046 target->marks = alloc_string_list(1);
2047 string_list_append(target->marks, strdup(argv[3]));
2048 }
2049 } else
2050 target->mark = strdup(argv[3]);
d82cf2f0 2051 return 1;
2052
2053 }
6d58d26f 2054 else if(!strcasecmp(argv[2], "CVERSION")) {
ba4b4d51 2055 /* CTCP VERSION mark */
6d58d26f 2056 target = GetUserH(argv[1]);
2057 if(!target) {
2058 log_module(MAIN_LOG, LOG_ERROR, "Unable to find user %s whose version mark is changing.", argv[1]);
2059 return 0;
2060 }
2061
fa3c07bb 2062 char *version = unsplit_string(argv + 3, argc - 3, NULL);
2063 if(!version)
2064 version = "";
2065
2066 target->version_reply = strdup(version);
2067
2068 if(match_ircglob(version, "WebTV;*"))
2069 target->no_notice = true; /* webbies cant see notices */
2070
6d58d26f 2071 return 1;
2072 }
ba4b4d51 2073 else if(!strcasecmp(argv[2], "SSLCLIFP")) {
2074 /* SSL fingerprint mark */
2075 target = GetUserH(argv[1]);
2076 if(!target) {
2077 log_module(MAIN_LOG, LOG_ERROR, "Unable to find user %s whose SSL fingerprint mark is changing.", argv[1]);
2078 return 0;
2079 }
2080
2081 char *sslfp = unsplit_string(argv + 3, argc - 3, NULL);
2082 if(!sslfp)
2083 sslfp = "";
2084
2085 target->sslfp = strdup(sslfp);
2086
2087 return 1;
2088 }
d82cf2f0 2089 /* unknown type of mark */
2090 return 1;
2091}
2092
d76ed9a9 2093static CMD_FUNC(cmd_mode)
2094{
2095 struct chanNode *cn;
2096 struct userNode *un;
182dd032 2097 char *sethost; // sethost - reed/apples
2098 int i; // sethost - reed/apples
d76ed9a9 2099
2100 if (argc < 3)
2101 return 0;
2102 if (!IsChannelName(argv[1])) {
2103 un = GetUserH(argv[1]);
2104 if (!un) {
2105 log_module(MAIN_LOG, LOG_ERROR, "Unable to find user %s whose mode is changing.", argv[1]);
2106 return 0;
2107 }
182dd032 2108 // sethost - reed/apples
2109 if (argc == 3)
d76ed9a9 2110 mod_usermode(un, argv[2]);
182dd032 2111 else {
2112 sethost = malloc(strlen(argv[2]) + 1 + strlen(argv[3]) + 1);
2113 i = 0;
2114 while((sethost[i++] = *argv[2]++));
2115 i--;
2116 sethost[i++] = ' ';
2117 while((sethost[i++] = *argv[3]++));
2118 mod_usermode(un, sethost); // sethost - reed/apples
2119 }
2120
d76ed9a9 2121 return 1;
2122 }
2123
2124 if (!(cn = GetChannel(argv[1]))) {
2125 log_module(MAIN_LOG, LOG_ERROR, "Unable to find channel %s whose mode is changing.", argv[1]);
2126 return 0;
2127 }
2128 if ((un = GetUserH(origin))) {
2129 struct modeNode *mn;
2130 /* Update idle time for person setting the mode */
2131 if ((mn = GetUserMode(cn, un)))
2132 mn->idle_since = now;
2133 } else {
2134 /* If it came from a server, reset timestamp to re-sync. */
2135 cn->timestamp = atoi(argv[argc-1]);
2136 }
2137
08895577 2138 if (checkDefCon(DEFCON_NO_MODE_CHANGE) && !IsOper(un)) {
2139 const char *str;
2140 str = conf_get_data("services/opserv/nick", RECDB_QSTRING);
2141 if (str) {
2142 char modes[MODELEN];
2143 struct userNode *opserv = GetUserH(str);
2144
2145 send_message_type(4, un, opserv, "Channel modes cannot be changed due to DefCon level %d in effect, please try again soon", DefConLevel);
2146 irc_make_chanmode(cn, modes);
2147 irc_mode(opserv, cn, modes);
2148 irc_mode(opserv, cn, DefConChanModes);
2149 }
2150 return 1;
2151 }
2152
2153
d76ed9a9 2154 return mod_chanmode(un, cn, argv+2, argc-2, MCP_ALLOW_OVB|MCP_FROM_SERVER|(un ? MC_NOTIFY : 0));
2155}
2156
2157static CMD_FUNC(cmd_opmode)
2158{
2159 struct chanNode *cn;
2160 struct userNode *un;
2161
2162 if (argc < 3)
2163 return 0;
2164
2165 if (!(cn = GetChannel(argv[1]))) {
2166 log_module(MAIN_LOG, LOG_ERROR, "Unable to find channel %s whose mode is changing.", argv[1]);
2167 return 0;
2168 }
2169 if (!(un = GetUserH(origin))) {
2170 log_module(MAIN_LOG, LOG_ERROR, "Unable to find user %s requesting OPMODE.", origin);
2171 return 0;
2172 }
2173 if (!IsOper(un)) {
2174 log_module(MAIN_LOG, LOG_ERROR, "Non-privileged user %s using OPMODE.", un->nick);
2175 return 0;
2176 }
2177
2178 return mod_chanmode(un, cn, argv+2, argc-2, MCP_ALLOW_OVB|MCP_FROM_SERVER); /* do NOT announce opmode locally */
2179}
2180
2181static int clear_chanmode(struct chanNode *channel, const char *modes);
2182
2183static CMD_FUNC(cmd_clearmode)
2184{
2185 struct chanNode *cn;
2186 struct userNode *un;
2187
2188 if (argc < 3)
2189 return 0;
2190
2191 if (!(cn = GetChannel(argv[1]))) {
2192 log_module(MAIN_LOG, LOG_ERROR, "Unable to find channel %s whose mode is changing.", argv[1]);
2193 return 0;
2194 }
2195 if (!(un = GetUserH(origin))) {
2196 log_module(MAIN_LOG, LOG_ERROR, "Unable to find user %s requesting CLEARMODE.", origin);
2197 return 0;
2198 }
2199 if (!IsOper(un)) {
2200 log_module(MAIN_LOG, LOG_ERROR, "Non-privileged user %s using CLEARMODE.", un->nick);
2201 return 0;
2202 }
2203
2204 return clear_chanmode(cn, argv[2]);
2205}
2206
2207static CMD_FUNC(cmd_topic)
2208{
2209 struct chanNode *cn;
2210 time_t chan_ts, topic_ts;
7fda2b52 2211 struct userNode *user;
d76ed9a9 2212
2213 if (argc < 3)
2214 return 0;
2215 if (!(cn = GetChannel(argv[1]))) {
2216 log_module(MAIN_LOG, LOG_ERROR, "Unable to find channel %s whose topic is being set", argv[1]);
2217 return 0;
2218 }
7fda2b52 2219
2220
2221 if (argc == 5) { /* Asuka / Topic Bursting IRCu's */
2222 user = GetUserH(origin);
d76ed9a9 2223 chan_ts = atoi(argv[2]);
2224 topic_ts = atoi(argv[3]);
7fda2b52 2225 } else if (argc >= 6) { /* Nefarious 0.5.0 */
2226 user = GetUserH(strtok(argv[2], "!"));
2227 chan_ts = atoi(argv[3]);
2228 topic_ts = atoi(argv[4]);
2229 } else { /* Regular IRCu (No Topic Bursting)*/
2230 user = GetUserH(origin);
d76ed9a9 2231 chan_ts = cn->timestamp;
2232 topic_ts = now;
2233 }
7fda2b52 2234
2235 SetChannelTopic(cn, user, user, argv[argc-1], 0);
d76ed9a9 2236 cn->topic_time = topic_ts;
2237 return 1;
2238}
2239
2240static CMD_FUNC(cmd_num_topic)
2241{
2242 struct chanNode *cn;
2243
2244 if (!argv[0])
2245 return 0; /* huh? */
2246 if (argv[2]) {
2247 cn = GetChannel(argv[2]);
2248 if (!cn) {
2249 log_module(MAIN_LOG, LOG_ERROR, "Unable to find channel %s in topic reply", argv[2]);
2250 return 0;
2251 }
2252 } else
2253 return 0;
2254
2255 switch (atoi(argv[0])) {
2256 case 331:
2257 cn->topic_time = 0;
2258 break; /* no topic */
2259 case 332:
2260 if (argc < 4)
2261 return 0;
2262 safestrncpy(cn->topic, unsplit_string(argv+3, argc-3, NULL), sizeof(cn->topic));
2263 break;
2264 case 333:
2265 if (argc < 5)
2266 return 0;
2267 safestrncpy(cn->topic_nick, argv[3], sizeof(cn->topic_nick));
2268 cn->topic_time = atoi(argv[4]);
2269 break;
2270 default:
2271 return 0; /* should never happen */
2272 }
2273 return 1;
2274}
2275
2276static CMD_FUNC(cmd_num_gline)
2277{
cad8f3a7 2278 if (argc < 7) {
2279 if (argc < 6)
2280 return 0;
995043b4 2281 else
2282 gline_add(origin, argv[3], atoi(argv[4])-now, argv[5], now, 0, 0);
cad8f3a7 2283 } else {
995043b4 2284 if (!strcmp(argv[5], "+"))
2285 gline_add(origin, argv[3], atoi(argv[4])-now, argv[6], now, 0, 0);
cad8f3a7 2286 }
d76ed9a9 2287 return 1;
2288}
2289
d914d1cb 2290static CMD_FUNC(cmd_num_shun)
2291{
cad8f3a7 2292 if (argc < 7) {
2293 if (argc < 6)
2294 return 0;
2295 else
995043b4 2296 shun_add(origin, argv[3], atoi(argv[4])-now, argv[5], now, 0);
cad8f3a7 2297 } else {
4cb36ef0 2298 if (!strcmp(argv[5], "+"))
995043b4 2299 shun_add(origin, argv[3], atoi(argv[4])-now, argv[6], now, 0);
cad8f3a7 2300 }
d914d1cb 2301 return 1;
2302}
2303
d76ed9a9 2304static CMD_FUNC(cmd_quit)
2305{
2306 struct userNode *user;
2307 if (argc < 2)
2308 return 0;
2309 /* Sometimes we get a KILL then a QUIT or the like, so we don't want to
2310 * call DelUser unless we have the user in our grasp. */
2311 if ((user = GetUserH(origin)))
2312 DelUser(user, NULL, false, argv[1]);
2313 return 1;
2314}
2315
2316static CMD_FUNC(cmd_kill)
2317{
2318 struct userNode *user;
2319 if (argc < 2)
2320 return 0;
2321 user = GetUserN(argv[1]);
2322 if (!user) {
2323 /* If we get a KILL for a non-existent user, it could be a
2324 * Ghost response to a KILL we sent out earlier. So we only
2325 * whine if the target is local.
2326 */
429f04a4 2327 if (!strncmp(argv[1], self->numeric, strlen(self->numeric))) {
d76ed9a9 2328 log_module(MAIN_LOG, LOG_ERROR, "Unable to find kill victim %s", argv[1]);
429f04a4
MB
2329 return 0;
2330 }
2331 return 1;
d76ed9a9 2332 }
2333
2334 if (IsLocal(user) && IsService(user)) {
2335 /* TODO: rate limit this so silly things don't happen. */
2336 ReintroduceUser(user);
2337 return 1;
2338 }
2339
2340 DelUser(user, NULL, false, argv[2]);
2341 return 1;
2342}
2343
cd25f2e9 2344static CMD_FUNC(cmd_svspart)
2345{
2346 struct userNode *user;
2347
2348 if (argc < 3)
2349 return 0;
c9ec8867 2350 user = GetUserN(argv[1]);
cd25f2e9 2351 if (!user)
2352 return 0;
2353 parse_foreach(argv[2], part_helper, NULL, NULL, NULL, user);
2354 return 1;
2355}
2356
a6a4707e 2357static CMD_FUNC(cmd_silence)
2358{
2359 struct userNode *user;
2360 char *mask;
f0b5157b 2361 char *new_mask;
a6a4707e 2362 unsigned int i;
2363
2364 if (argc < 2)
2365 return 0;
2366
8da65b0a
MB
2367 if (strncmp(argv[1], "*", 2) == 0)
2368 {
2369 if (!(user = GetUserH(origin))) {
2370 log_module(MAIN_LOG, LOG_ERROR, "Could not find SILENCE origin user %s", origin);
2371 return 0;
2372 }
2373 }
2374 else
2375 user = GetUserN(argv[1]);
a6a4707e 2376
2377 /* Sanity, go nuts if this happens */
2378 if (!user)
2379 return 0;
2380
2381 /* We can safely ignore this if a user adding a silence is not
2382 * ignored. However this brings up a TODO. If a user logs in and
2383 * they have silences on the IRCd then we need to set them here
2384 * somehow
2385 */
2386 if (!user->handle_info)
2387 return 1;
2388
b6599cc2 2389 mask = argv[2];
a6a4707e 2390
2391 if (*mask == '-') {
2392 for (i=0; i<user->handle_info->ignores->used; i++) {
2393 if (!irccasecmp(mask+1, user->handle_info->ignores->list[i]))
2394 user->handle_info->ignores->list[i] = user->handle_info->ignores->list[--user->handle_info->ignores->used];
2395 }
2396 } else {
2397 for (i=0; i<user->handle_info->ignores->used; i++) {
2398 if (!strcmp(mask+1, user->handle_info->ignores->list[i]))
2399 return 1; /* Already on the users NickServ ignore list, safely ignore */
2400 }
2401
f0b5157b 2402 new_mask = strdup(mask+1);
b6599cc2 2403 string_list_append(user->handle_info->ignores, new_mask);
a6a4707e 2404 }
a6a4707e 2405 return 1;
2406}
2407
d76ed9a9 2408static CMD_FUNC(cmd_kick)
2409{
2410 if (argc < 3)
2411 return 0;
2412 ChannelUserKicked(GetUserH(origin), GetUserN(argv[2]), GetChannel(argv[1]));
2413 return 1;
2414}
2415
2416static CMD_FUNC(cmd_squit)
2417{
2418 struct server *server;
47956fc5 2419 char *uplink;
d76ed9a9 2420
2421 if (argc < 4)
2422 return 0;
2423 if (!(server = GetServerH(argv[1])))
2424 return 0;
2425
2426 if (server == self->uplink) {
2427 /* Force a reconnect to the currently selected server. */
2428 cManager.uplink->tries = 0;
2429 log_module(MAIN_LOG, LOG_INFO, "Squitting from uplink: %s", argv[3]);
2430 close_socket();
2431 return 1;
2432 }
2433
47956fc5 2434 uplink = strdup(server->uplink->name);
d76ed9a9 2435 DelServer(server, 0, argv[3]);
47956fc5 2436 /* if its a pingout and pingout connecting is enabled
2437 or its a read error and readerror connecting is enabled
2438 or were doing a "N" and i need to connect that server somewhere */
2439 routing_handle_squit(argv[1], uplink, argv[3]);
2440 free(uplink);
d76ed9a9 2441 return 1;
2442}
2443
2444static CMD_FUNC(cmd_privmsg)
2445{
2446 struct privmsg_desc pd;
1136f709 2447 if (argc < 3)
d76ed9a9 2448 return 0;
2449 pd.user = GetUserH(origin);
2450 if (!pd.user || (IsGagged(pd.user) && !IsOper(pd.user)))
2451 return 1;
08895577 2452
2453 if (checkDefCon(DEFCON_OPER_ONLY) && !IsOper(pd.user)) {
2454 const char *str;
2455 str = conf_get_data("services/opserv/nick", RECDB_QSTRING);
2456 if (str)
2457 send_message_type(4, pd.user, GetUserH(str), "Services are currently not available, please try again soon");
2458 return 1;
2459 }
2460
2461 if (checkDefCon(DEFCON_SILENT_OPER_ONLY) && !IsOper(pd.user))
2462 return 1; /* Silently Ignore */
2463
d76ed9a9 2464 pd.is_notice = 0;
1136f709 2465 pd.text = argv[argc - 1];
d76ed9a9 2466 parse_foreach(argv[1], privmsg_chan_helper, NULL, privmsg_user_helper, privmsg_invalid, &pd);
c99dcaf6 2467
d76ed9a9 2468 return 1;
2469}
2470
2471static CMD_FUNC(cmd_notice)
2472{
2473 struct privmsg_desc pd;
47956fc5 2474 struct server *srv;
2475 int nuser = 0;
2476
0f6fe38c 2477 if (argc != 3)
d76ed9a9 2478 return 0;
47956fc5 2479
d76ed9a9 2480 pd.user = GetUserH(origin);
47956fc5 2481 if(!pd.user)
2482 nuser = 1;
2483 if (!pd.user || (IsGagged(pd.user) && !IsOper(pd.user))) {
2484 }
2485 else {
2486 pd.is_notice = 1;
0f6fe38c 2487 pd.text = argv[2];
47956fc5 2488 parse_foreach(argv[1], privmsg_chan_helper, NULL, privmsg_user_helper, privmsg_invalid, &pd);
2489 }
2490
2491 srv = GetServerH(origin);
2492 if(srv) {
2493 char *sargv[MAXNUMPARAMS];
2494 int sargc;
2495
2496 sargc = split_line(argv[2], true, MAXNUMPARAMS, sargv);
2497
2498 if(!strcasecmp(sargv[0], "Connect:")) {
2499 /* :Connect: Host shoe.loxxin.net not listed in ircd.conf */
2500 if(!strcasecmp(sargv[3], "not") && !strcasecmp(sargv[4], "listed")) {
2501 routing_handle_connect_failure(srv, sargv[2], unsplit_string(sargv+3, sargc-3, NULL));
2502 }
2503 }
2504 else if(!strcasecmp(sargv[0], "Link")) {
2505 /* :Link with mephisto.etheria.cx cancelled: Server mephisto.etheria.cx[216.46.33.71]
2506 *
2507 * :Link with laptop.afternet.org cancelled: Connection refused
2508 */
2509 if(!strcasecmp(sargv[3], "cancelled:")) {
2510 routing_handle_connect_failure(srv, sargv[2], unsplit_string(sargv+4, sargc-4, NULL));
2511 }
2512 }
2513 }
d76ed9a9 2514 return 1;
2515}
2516
2517static CMD_FUNC(cmd_away)
2518{
2519 struct userNode *uNode;
2520
2521 uNode = GetUserH(origin);
2522 if (!uNode)
2523 return 1;
2524 if (argc < 2)
2525 uNode->modes &= ~FLAGS_AWAY;
2526 else
2527 uNode->modes |= FLAGS_AWAY;
2528 return 1;
2529}
2530
2531static CMD_FUNC(cmd_gline)
2532{
2533 if (argc < 3)
2534 return 0;
2535 if (argv[2][0] == '+') {
2536 if (argc < 5)
2537 return 0;
995043b4 2538 gline_add(origin, argv[2]+1, strtoul(argv[3], NULL, 0), argv[argc-1], now, 0, 0);
d76ed9a9 2539 return 1;
2540 } else if (argv[2][0] == '-') {
2541 gline_remove(argv[2]+1, 0);
2542 return 1;
2543 } else
2544 return 0;
2545}
2546
59b38cc0 2547static CMD_FUNC(cmd_sgline)
2548{
2549 struct server *sender;
2550
2551 if (argc < 4)
2552 return 0;
2553
2554 if (!(sender = GetServerH(origin)))
2555 return 0;
2556
995043b4 2557 gline_add(origin, argv[1], strtoul(argv[2], NULL, 0), argv[argc-1], now, 1, 0);
59b38cc0 2558 return 1;
2559}
2560
2561static CMD_FUNC(cmd_sshun)
2562{
2563 struct server *sender;
2564
2565 if (argc < 4)
2566 return 0;
2567
2568 if (!(sender = GetServerH(origin)))
2569 return 0;
2570
995043b4 2571 shun_add(origin, argv[1], strtoul(argv[2], NULL, 0), argv[argc-1], now, 1);
59b38cc0 2572 return 1;
2573}
2574
d914d1cb 2575static CMD_FUNC(cmd_shun)
2576{
2577 if (argc < 3)
2578 return 0;
2579 if (argv[2][0] == '+') {
2580 if (argc < 5)
2581 return 0;
995043b4 2582 shun_add(origin, argv[2]+1, strtoul(argv[3], NULL, 0), argv[argc-1], now, 0);
d914d1cb 2583 return 1;
2584 } else if (argv[2][0] == '-') {
2585 shun_remove(argv[2]+1, 0);
2586 return 1;
2587 } else
2588 return 0;
2589}
2590
d76ed9a9 2591static CMD_FUNC(cmd_svsnick)
2592{
2593 struct userNode *target, *dest;
2594 if ((argc < 4)
2595 || !(target = GetUserN(argv[1]))
2596 || !IsLocal(target)
2597 || (dest = GetUserH(argv[2])))
2598 return 0;
2599 NickChange(target, argv[2], 0);
2600 return 1;
2601}
2602
d76ed9a9 2603void
2604free_user(struct userNode *user)
2605{
2606 free(user->nick);
2607 free(user);
2608}
2609
2610static void
30874d66 2611parse_cleanup(UNUSED_ARG(void *extra))
d76ed9a9 2612{
2613 unsigned int nn;
2614 free(of_list);
e9b00805 2615 free(of_list_extra);
d76ed9a9 2616 free(privmsg_funcs);
1136f709 2617 num_privmsg_funcs = 0;
d76ed9a9 2618 free(notice_funcs);
1136f709 2619 num_notice_funcs = 0;
d76ed9a9 2620 free(mcf_list);
2621 dict_delete(irc_func_dict);
2622 for (nn=0; nn<dead_users.used; nn++)
2623 free_user(dead_users.list[nn]);
2624 userList_clean(&dead_users);
2625}
2626
2627static void
2628p10_conf_reload(void) {
2629 hidden_host_suffix = conf_get_data("server/hidden_host", RECDB_QSTRING);
1136f709 2630 his_servername = conf_get_data("server/his_servername", RECDB_QSTRING);
2631 his_servercomment = conf_get_data("server/his_servercomment", RECDB_QSTRING);
d76ed9a9 2632}
2633
2634static void
1898d8ea 2635remove_unbursted_channel(struct chanNode *cNode, UNUSED_ARG(void *extra)) {
d76ed9a9 2636 if (unbursted_channels)
2637 dict_remove(unbursted_channels, cNode->name);
2638}
2639
2640void
2641init_parse(void)
2642{
2643 const char *str, *desc;
2644 int numnick, usermask, max_users;
2645 char numer[COMBO_NUMERIC_LEN+1];
2646
2647 /* read config items */
805e7c7a 2648 str = conf_get_data("server/extended_accounts", RECDB_QSTRING);
2649 extended_accounts = str ? enabled_string(str) : 1;
d76ed9a9 2650 str = conf_get_data("server/ping_freq", RECDB_QSTRING);
2651 ping_freq = str ? ParseInterval(str) : 120;
2652 str = conf_get_data("server/ping_timeout", RECDB_QSTRING);
2653 ping_timeout = str ? ParseInterval(str) : 30;
2654 str = conf_get_data("server/force_n2k", RECDB_QSTRING);
2655 force_n2k = str ? enabled_string(str) : 1;
2656 str = conf_get_data("server/numeric", RECDB_QSTRING);
2657 if (!str) {
2658 log_module(MAIN_LOG, LOG_ERROR, "No server/numeric entry in config file.");
2659 exit(1);
2660 }
2661 numnick = atoi(str);
2662 str = conf_get_data("server/max_users", RECDB_QSTRING);
2663 max_users = str ? atoi(str) : 4096;
2664 for (usermask = 4; usermask < max_users; usermask <<= 1) ;
2665 usermask--;
2666 if ((numnick < 64) && (usermask < 4096) && !force_n2k)
2667 inttobase64(numer, (numnick << 12) + (usermask & 0x00fff), 3);
2668 else
2669 inttobase64(numer, (numnick << 18) + (usermask & 0x3ffff), 5);
2670
d76ed9a9 2671 str = conf_get_data("server/hostname", RECDB_QSTRING);
2672 desc = conf_get_data("server/description", RECDB_QSTRING);
2673 if (!str || !desc) {
2674 log_module(MAIN_LOG, LOG_ERROR, "No server/hostname entry in config file.");
2675 exit(1);
2676 }
2677 self = AddServer(NULL, str, 0, boot_time, now, numer, desc);
2678 conf_register_reload(p10_conf_reload);
2679
2680 irc_func_dict = dict_new();
2681 dict_insert(irc_func_dict, CMD_BURST, cmd_burst);
2682 dict_insert(irc_func_dict, TOK_BURST, cmd_burst);
2683 dict_insert(irc_func_dict, CMD_CREATE, cmd_create);
2684 dict_insert(irc_func_dict, TOK_CREATE, cmd_create);
2685 dict_insert(irc_func_dict, CMD_EOB, cmd_eob);
2686 dict_insert(irc_func_dict, TOK_EOB, cmd_eob);
2687 dict_insert(irc_func_dict, CMD_EOB_ACK, cmd_eob_ack);
2688 dict_insert(irc_func_dict, TOK_EOB_ACK, cmd_eob_ack);
2689 dict_insert(irc_func_dict, CMD_MODE, cmd_mode);
2690 dict_insert(irc_func_dict, TOK_MODE, cmd_mode);
d82cf2f0 2691 dict_insert(irc_func_dict, CMD_MARK, cmd_mark);
2692 dict_insert(irc_func_dict, TOK_MARK, cmd_mark);
d76ed9a9 2693 dict_insert(irc_func_dict, CMD_NICK, cmd_nick);
2694 dict_insert(irc_func_dict, TOK_NICK, cmd_nick);
2695 dict_insert(irc_func_dict, CMD_ACCOUNT, cmd_account);
2696 dict_insert(irc_func_dict, TOK_ACCOUNT, cmd_account);
2697 dict_insert(irc_func_dict, CMD_FAKEHOST, cmd_fakehost);
2698 dict_insert(irc_func_dict, TOK_FAKEHOST, cmd_fakehost);
2699 dict_insert(irc_func_dict, CMD_PASS, cmd_pass);
2700 dict_insert(irc_func_dict, TOK_PASS, cmd_pass);
2701 dict_insert(irc_func_dict, CMD_PING, cmd_ping);
2702 dict_insert(irc_func_dict, TOK_PING, cmd_ping);
2703 dict_insert(irc_func_dict, CMD_PRIVMSG, cmd_privmsg);
2704 dict_insert(irc_func_dict, TOK_PRIVMSG, cmd_privmsg);
2705 dict_insert(irc_func_dict, CMD_PONG, cmd_pong);
2706 dict_insert(irc_func_dict, TOK_PONG, cmd_pong);
2707 dict_insert(irc_func_dict, CMD_QUIT, cmd_quit);
2708 dict_insert(irc_func_dict, TOK_QUIT, cmd_quit);
2709 dict_insert(irc_func_dict, CMD_SERVER, cmd_server);
2710 dict_insert(irc_func_dict, TOK_SERVER, cmd_server);
2711 dict_insert(irc_func_dict, CMD_JOIN, cmd_join);
2712 dict_insert(irc_func_dict, TOK_JOIN, cmd_join);
2713 dict_insert(irc_func_dict, CMD_PART, cmd_part);
2714 dict_insert(irc_func_dict, TOK_PART, cmd_part);
2715 dict_insert(irc_func_dict, CMD_ERROR, cmd_error);
2716 dict_insert(irc_func_dict, TOK_ERROR, cmd_error);
2717 dict_insert(irc_func_dict, CMD_TOPIC, cmd_topic);
2718 dict_insert(irc_func_dict, TOK_TOPIC, cmd_topic);
2719 dict_insert(irc_func_dict, CMD_AWAY, cmd_away);
2720 dict_insert(irc_func_dict, TOK_AWAY, cmd_away);
a6a4707e 2721 dict_insert(irc_func_dict, CMD_SILENCE, cmd_silence);
2722 dict_insert(irc_func_dict, TOK_SILENCE, cmd_silence);
d76ed9a9 2723 dict_insert(irc_func_dict, CMD_KICK, cmd_kick);
2724 dict_insert(irc_func_dict, TOK_KICK, cmd_kick);
2725 dict_insert(irc_func_dict, CMD_SQUIT, cmd_squit);
2726 dict_insert(irc_func_dict, TOK_SQUIT, cmd_squit);
2727 dict_insert(irc_func_dict, CMD_KILL, cmd_kill);
2728 dict_insert(irc_func_dict, TOK_KILL, cmd_kill);
2729 dict_insert(irc_func_dict, CMD_NOTICE, cmd_notice);
2730 dict_insert(irc_func_dict, TOK_NOTICE, cmd_notice);
2731 dict_insert(irc_func_dict, CMD_STATS, cmd_stats);
2732 dict_insert(irc_func_dict, TOK_STATS, cmd_stats);
cd25f2e9 2733 dict_insert(irc_func_dict, CMD_SVSJOIN, cmd_svsjoin);
2734 dict_insert(irc_func_dict, TOK_SVSJOIN, cmd_svsjoin);
d76ed9a9 2735 dict_insert(irc_func_dict, CMD_SVSNICK, cmd_svsnick);
2736 dict_insert(irc_func_dict, TOK_SVSNICK, cmd_svsnick);
cd25f2e9 2737 dict_insert(irc_func_dict, CMD_SVSPART, cmd_svspart);
2738 dict_insert(irc_func_dict, TOK_SVSPART, cmd_svspart);
56958740 2739 dict_insert(irc_func_dict, CMD_SWHOIS, cmd_dummy);
2740 dict_insert(irc_func_dict, TOK_SWHOIS, cmd_dummy);
d76ed9a9 2741 dict_insert(irc_func_dict, CMD_WHOIS, cmd_whois);
2742 dict_insert(irc_func_dict, TOK_WHOIS, cmd_whois);
2743 dict_insert(irc_func_dict, CMD_GLINE, cmd_gline);
2744 dict_insert(irc_func_dict, TOK_GLINE, cmd_gline);
59b38cc0 2745 dict_insert(irc_func_dict, CMD_SGLINE, cmd_sgline);
2746 dict_insert(irc_func_dict, TOK_SGLINE, cmd_sgline);
d914d1cb 2747 dict_insert(irc_func_dict, CMD_SHUN, cmd_shun);
2748 dict_insert(irc_func_dict, TOK_SHUN, cmd_shun);
59b38cc0 2749 dict_insert(irc_func_dict, CMD_SSHUN, cmd_sshun);
2750 dict_insert(irc_func_dict, TOK_SSHUN, cmd_sshun);
d76ed9a9 2751 dict_insert(irc_func_dict, CMD_OPMODE, cmd_opmode);
2752 dict_insert(irc_func_dict, TOK_OPMODE, cmd_opmode);
2753 dict_insert(irc_func_dict, CMD_CLEARMODE, cmd_clearmode);
2754 dict_insert(irc_func_dict, TOK_CLEARMODE, cmd_clearmode);
2755 dict_insert(irc_func_dict, CMD_VERSION, cmd_version);
2756 dict_insert(irc_func_dict, TOK_VERSION, cmd_version);
2757 dict_insert(irc_func_dict, CMD_ADMIN, cmd_admin);
2758 dict_insert(irc_func_dict, TOK_ADMIN, cmd_admin);
56b83e80 2759 dict_insert(irc_func_dict, CMD_SMO, cmd_dummy);
2760 dict_insert(irc_func_dict, TOK_SMO, cmd_dummy);
1c76f1b4 2761 dict_insert(irc_func_dict, CMD_SNO, cmd_dummy);
2762 dict_insert(irc_func_dict, TOK_SNO, cmd_dummy);
d76ed9a9 2763
956ee72d 2764 dict_insert(irc_func_dict, CMD_RPING, cmd_rping);
2765 dict_insert(irc_func_dict, TOK_RPING, cmd_rping);
8f9e3388 2766 dict_insert(irc_func_dict, CMD_RPONG, cmd_dummy);
2767 dict_insert(irc_func_dict, TOK_RPONG, cmd_dummy);
2768
c8b4b87b
MB
2769 dict_insert(irc_func_dict, CMD_SASL, cmd_sasl);
2770 dict_insert(irc_func_dict, TOK_SASL, cmd_sasl);
2771
d76ed9a9 2772 /* In P10, DESTRUCT doesn't do anything except be broadcast to servers.
2773 * Apparently to obliterate channels from any servers that think they
2774 * exist?
2775 */
2776 dict_insert(irc_func_dict, CMD_DESTRUCT, cmd_dummy);
2777 dict_insert(irc_func_dict, TOK_DESTRUCT, cmd_dummy);
2778 /* Ignore invites */
2779 dict_insert(irc_func_dict, CMD_INVITE, cmd_dummy);
2780 dict_insert(irc_func_dict, TOK_INVITE, cmd_dummy);
2781 /* DESYNCH is just informational, so ignore it */
2782 dict_insert(irc_func_dict, CMD_DESYNCH, cmd_dummy);
2783 dict_insert(irc_func_dict, TOK_DESYNCH, cmd_dummy);
2784 /* Ignore channel operator notices. */
2785 dict_insert(irc_func_dict, CMD_WALLCHOPS, cmd_dummy);
2786 dict_insert(irc_func_dict, TOK_WALLCHOPS, cmd_dummy);
2787 dict_insert(irc_func_dict, CMD_WALLVOICES, cmd_dummy);
2788 dict_insert(irc_func_dict, TOK_WALLVOICES, cmd_dummy);
55342ce8 2789 dict_insert(irc_func_dict, CMD_WALLHOPS, cmd_dummy);
2790 dict_insert(irc_func_dict, TOK_WALLHOPS, cmd_dummy);
d76ed9a9 2791 /* Ignore opers being silly. */
2792 dict_insert(irc_func_dict, CMD_WALLOPS, cmd_dummy);
2793 dict_insert(irc_func_dict, TOK_WALLOPS, cmd_dummy);
55342ce8 2794 dict_insert(irc_func_dict, CMD_WALLHOPS, cmd_dummy);
2795 dict_insert(irc_func_dict, TOK_WALLHOPS, cmd_dummy);
41fadebe 2796 dict_insert(irc_func_dict, TOK_WALLUSERS, cmd_dummy);
850bf0cf 2797 /* Ignore dnsbl exemptions */
2798 dict_insert(irc_func_dict, TOK_EXEMPT, cmd_dummy);
89d871d8 2799 dict_insert(irc_func_dict, CMD_PRIVS, cmd_privs);
2800 dict_insert(irc_func_dict, TOK_PRIVS, cmd_privs);
763de701 2801 /* ignore ALIST for now */
2802 dict_insert(irc_func_dict, TOK_ALIST, cmd_dummy);
2803 dict_insert(irc_func_dict, CMD_ALIST, cmd_dummy);
4b2dcc0d 2804 /* ignore SPAMFILTER */
2805 dict_insert(irc_func_dict, TOK_SPAMFILTER, cmd_dummy);
2806 dict_insert(irc_func_dict, CMD_SPAMFILTER, cmd_dummy);
a74c9eac 2807 /* Ignore remote luser */
0c835e0f 2808 dict_insert(irc_func_dict, TOK_LUSERS, cmd_dummy);
d76ed9a9 2809 /* We have reliable clock! Always! Wraaa! */
2810 dict_insert(irc_func_dict, CMD_SETTIME, cmd_dummy);
2811 dict_insert(irc_func_dict, TOK_SETTIME, cmd_dummy);
e10feb7a 2812 /* Ignore ZLINE/ZL/REMOVE/RM */
2813 dict_insert(irc_func_dict, CMD_ZLINE, cmd_dummy);
2814 dict_insert(irc_func_dict, TOK_ZLINE, cmd_dummy);
2815 dict_insert(irc_func_dict, CMD_REMOVE, cmd_dummy);
2816 dict_insert(irc_func_dict, TOK_REMOVE, cmd_dummy);
b34a7505 2817
2818 /* ignore /trace and /motd commands targetted at us */
2819 dict_insert(irc_func_dict, TOK_TRACE, cmd_dummy);
2820 dict_insert(irc_func_dict, TOK_MOTD, cmd_dummy);
74ac2349 2821 dict_insert(irc_func_dict, TOK_UPING, cmd_dummy);
b34a7505 2822
d76ed9a9 2823 /* handle topics */
2824 dict_insert(irc_func_dict, "331", cmd_num_topic);
2825 dict_insert(irc_func_dict, "332", cmd_num_topic);
2826 dict_insert(irc_func_dict, "333", cmd_num_topic);
2827 dict_insert(irc_func_dict, "345", cmd_dummy); /* blah has been invited to blah */
2828 dict_insert(irc_func_dict, "432", cmd_error_nick); /* Erroneus [sic] nickname */
2829 /* ban list resetting */
2830 /* "stats g" responses */
39b3b541 2831 dict_insert(irc_func_dict, "230", cmd_dummy); /* ignore stats headers */
d76ed9a9 2832 dict_insert(irc_func_dict, "247", cmd_num_gline);
d914d1cb 2833 dict_insert(irc_func_dict, "542", cmd_num_shun);
d76ed9a9 2834 dict_insert(irc_func_dict, "219", cmd_dummy); /* "End of /STATS report" */
2835 /* other numeric responses we might get */
2836 dict_insert(irc_func_dict, "401", cmd_dummy); /* target left network */
2837 dict_insert(irc_func_dict, "403", cmd_dummy); /* no such channel */
2838 dict_insert(irc_func_dict, "404", cmd_dummy); /* cannot send to channel */
0d16e639 2839 dict_insert(irc_func_dict, "439", cmd_dummy); /* target change too fast */
d76ed9a9 2840 dict_insert(irc_func_dict, "441", cmd_dummy); /* target isn't on that channel */
2841 dict_insert(irc_func_dict, "442", cmd_dummy); /* you aren't on that channel */
2842 dict_insert(irc_func_dict, "443", cmd_dummy); /* is already on channel (after invite?) */
2843 dict_insert(irc_func_dict, "461", cmd_dummy); /* Not enough parameters (after TOPIC w/ 0 args) */
2844 dict_insert(irc_func_dict, "467", cmd_dummy); /* Channel key already set */
2845
2846 num_privmsg_funcs = 16;
2847 privmsg_funcs = malloc(sizeof(privmsg_func_t)*num_privmsg_funcs);
2848 memset(privmsg_funcs, 0, sizeof(privmsg_func_t)*num_privmsg_funcs);
2849
2850 num_notice_funcs = 16;
2851 notice_funcs = malloc(sizeof(privmsg_func_t)*num_notice_funcs);
2852 memset(notice_funcs, 0, sizeof(privmsg_func_t)*num_notice_funcs);
2853
2854 userList_init(&dead_users);
1898d8ea 2855 reg_del_channel_func(remove_unbursted_channel, NULL);
30874d66 2856 reg_exit_func(parse_cleanup, NULL);
da5b7dfc 2857 // reg_notice_func(opserv, check_ctcp);
d76ed9a9 2858}
2859
2860int
2861parse_line(char *line, int recursive)
2862{
2863 char *argv[MAXNUMPARAMS], *origin;
2864 int argc, cmd, res=0;
2865 cmd_func_t *func;
2866
2867 argc = split_line(line, true, MAXNUMPARAMS, argv);
2868 cmd = self->uplink || !argv[0][1] || !argv[0][2];
2869 if (argc > cmd) {
2870 if (cmd) {
2871 if (argv[0][0] == ':') {
2872 origin = argv[0]+1;
2873 } else if (!argv[0][1] || !argv[0][2]) {
2874 struct server *sNode = GetServerN(argv[0]);
2875 origin = sNode ? sNode->name : 0;
2876 } else {
2877 struct userNode *uNode = GetUserN(argv[0]);
2878 origin = uNode ? uNode->nick : 0;
2879 }
2880 } else
2881 origin = 0;
2882 if ((func = dict_find(irc_func_dict, argv[cmd], NULL)))
2883 res = func(origin, argc-cmd, argv+cmd);
2884 }
2885 if (!res) {
2886 log_module(MAIN_LOG, LOG_ERROR, "PARSE ERROR on line: %s", unsplit_string(argv, argc, NULL));
2887 } else if (!recursive) {
2888 unsigned int i;
2889 for (i=0; i<dead_users.used; i++)
2890 free_user(dead_users.list[i]);
2891 dead_users.used = 0;
2892 }
2893 return res;
2894}
2895
2896static void
2897parse_foreach(char *target_list, foreach_chanfunc cf, foreach_nonchan nc, foreach_userfunc uf, foreach_nonuser nu, void *data)
2898{
2899 char *j, old;
2f61d1d7 2900
d76ed9a9 2901 do {
2902 j = target_list;
2903 while (*j != 0 && *j != ',')
2904 j++;
2905 old = *j;
2906 *j = 0;
2f61d1d7 2907
d76ed9a9 2908 if (IsChannelName(target_list)
2909 || (target_list[0] == '0' && target_list[1] == '\0')) {
2910 struct chanNode *chan = GetChannel(target_list);
2f61d1d7 2911
d76ed9a9 2912 if (chan) {
2913 if (cf)
2914 cf(chan, data);
2915 } else {
2916 if (nc)
2917 nc(target_list, data);
2918 }
2919 } else {
2920 struct userNode *user;
2921 struct privmsg_desc *pd = data;
2922
2923 pd->is_qualified = 0;
2924 if (*target_list == '@') {
2925 user = NULL;
2926 } else if (strchr(target_list, '@')) {
2927 struct server *server;
2928
2929 pd->is_qualified = 1;
2930 user = GetUserH(strtok(target_list, "@"));
2931 server = GetServerH(strtok(NULL, "@"));
2932
2933 if (user && (user->uplink != server)) {
2934 /* Don't attempt to index into any arrays
2935 using a user's numeric on another server. */
2936 user = NULL;
2937 }
2938 } else {
2939 user = GetUserN(target_list);
2940 }
2941
2942 if (user) {
2943 if (uf)
2944 uf(user, data);
2945 } else {
2946 if (nu)
2947 nu(target_list, data);
2948 }
2949 }
2950 target_list = j+1;
2951 } while (old == ',');
2952}
2953
2954static int
2955get_local_numeric(void)
2956{
2957 static unsigned int next_numeric = 0;
2958 if (self->clients > self->num_mask)
2959 return -1;
2960 while (self->users[next_numeric])
2961 if (++next_numeric > self->num_mask)
2962 next_numeric = 0;
2963 return next_numeric;
2964}
2965
2966static void
2967make_numeric(struct server *svr, int local_num, char *outbuf)
2968{
2969 int slen, llen;
2970
2971 if (force_n2k || svr->numeric[1]) {
2972 slen = 2;
2973 llen = 3;
2974 } else {
2975 slen = 1;
2976 llen = (local_num < 64*64) ? 2 : 3;
2977 }
2978 strncpy(outbuf, svr->numeric, slen);
2979 inttobase64(outbuf+slen, local_num, llen);
2980 outbuf[slen+llen] = 0;
2981}
2982
2983struct server *
1136f709 2984AddServer(struct server *uplink, const char *name, int hops, time_t boot, time_t link_time, const char *numeric, const char *description)
d76ed9a9 2985{
2986 struct server* sNode;
2987 int slen, mlen;
2988
2989 if ((sNode = GetServerN(numeric))) {
2990 /* This means we're trying to re-add an existant server.
2991 * To be safe, we should forget the previous incarnation.
2992 * (And all its linked servers.)
2993 *
2994 * It usually only happens in replays when the original
2995 * had a ping timeout and the replay didn't (because
2996 * replaying a ping timeout invariably gets things wrong).
2997 */
2998 DelServer(sNode, 0, NULL);
2999 }
3000
3001 switch (strlen(numeric)) {
3002 case 5: slen = 2; mlen = 3; break;
3003 case 4: slen = 1; mlen = 3; break;
3004 case 3: slen = 1; mlen = 2; break;
3005 default:
3006 log_module(MAIN_LOG, LOG_ERROR, "AddServer(\"%s\", \"%s\", ...): Numeric %s has invalid length.", uplink->name, name, numeric);
3007 return NULL;
3008 }
3009
3010 sNode = calloc(1, sizeof(*sNode));
3011 sNode->uplink = uplink;
3012 safestrncpy(sNode->name, name, sizeof(sNode->name));
3013 sNode->num_mask = base64toint(numeric+slen, mlen);
3014 sNode->hops = hops;
3015 sNode->boot = boot;
1136f709 3016 sNode->link_time = link_time;
d76ed9a9 3017 strncpy(sNode->numeric, numeric, slen);
3018 safestrncpy(sNode->description, description, sizeof(sNode->description));
3019 sNode->users = calloc(sNode->num_mask+1, sizeof(*sNode->users));
3020 serverList_init(&sNode->children);
3021 if (sNode->uplink) {
3022 /* uplink may be NULL if we're just building ourself */
3023 serverList_append(&sNode->uplink->children, sNode);
3024 }
3025 servers_num[base64toint(numeric, slen)] = sNode;
3026 dict_insert(servers, sNode->name, sNode);
3027 return sNode;
3028}
3029
3030void DelServer(struct server* serv, int announce, const char *message)
3031{
3032 unsigned int i;
3033
3034 /* If we receive an ERROR command before the SERVER
3035 * command a NULL server can be passed */
3036 if (!serv)
3037 return;
3038
3039 /* Hrm, what's the right way to SQUIT some other server?
3040 * (This code is only to handle killing juped servers.) */
3041 if (announce && (serv->uplink == self) && (serv != self->uplink))
3042 irc_squit(serv, message, NULL);
3043
3044 /* must recursively remove servers linked to this one first */
3045 for (i=serv->children.used;i>0;)
3046 if (serv->children.list[--i] != self)
3047 DelServer(serv->children.list[i], false, NULL);
3048
3049 /* clean up server's user hash tables */
3050 for (i=0;i<=serv->num_mask;i++)
3051 if (serv->users[i])
3052 DelUser(serv->users[i], NULL, false, "server delinked");
3053
3054 /* delete server */
3055 if (serv->uplink)
3056 serverList_remove(&serv->uplink->children, serv);
3057 if (serv == self->uplink)
3058 self->uplink = NULL;
3059 servers_num[base64toint(serv->numeric, strlen(serv->numeric))] = NULL;
3060 dict_remove(servers, serv->name);
3061 serverList_clean(&serv->children);
3062 free(serv->users);
3063 free(serv);
3064}
3065
3066struct userNode *
1136f709 3067AddLocalUser(const char *nick, const char *ident, const char *hostname, const char *desc, const char *modes)
d76ed9a9 3068{
3069 char numeric[COMBO_NUMERIC_LEN+1];
3070 int local_num = get_local_numeric();
3071 time_t timestamp = now;
3072 struct userNode *old_user = GetUserH(nick);
3073
1136f709 3074 if (!modes)
3075 modes = "+oik";
3076
d76ed9a9 3077 if (old_user) {
3078 if (IsLocal(old_user))
3079 return old_user;
3080 timestamp = old_user->timestamp - 1;
3081 }
3082 if (local_num == -1) {
3083 log_module(MAIN_LOG, LOG_ERROR, "Unable to allocate numnick for service %s", nick);
3084 return 0;
3085 }
3086 if (!hostname)
3087 hostname = self->name;
3088 make_numeric(self, local_num, numeric);
ab2acf0a 3089 return AddUser(self, nick, ident, hostname, modes, numeric, desc, timestamp, "AAAAAA");
d76ed9a9 3090}
3091
3092struct userNode *
3093AddClone(const char *nick, const char *ident, const char *hostname, const char *desc)
3094{
3095 char numeric[COMBO_NUMERIC_LEN+1];
3096 int local_num = get_local_numeric();
3097 time_t timestamp = now;
3098 struct userNode *old_user = GetUserH(nick);
3099
3100 if (old_user) {
3101 if (IsLocal(old_user))
3102 return old_user;
3103 timestamp = old_user->timestamp - 1;
3104 }
3105 if (local_num == -1) {
3106 log_module(MAIN_LOG, LOG_ERROR, "Unable to allocate numnick for clone %s", nick);
3107 return 0;
3108 }
3109 make_numeric(self, local_num, numeric);
3110 return AddUser(self, nick, ident, hostname, "+i", numeric, desc, timestamp, "AAAAAA");
3111}
3112
3113int
3114is_valid_nick(const char *nick) {
ec1a68c8 3115 unsigned int ii;
d76ed9a9 3116 /* IRC has some of The Most Fucked-Up ideas about character sets
3117 * in the world.. */
3118 if (!isalpha(*nick) && !strchr("{|}~[\\]^_`", *nick))
3119 return 0;
ec1a68c8 3120 for (ii = 0; nick[ii]; ++ii)
3121 if (!isalnum(nick[ii]) && !strchr("{|}~[\\]^-_`", nick[ii]))
d76ed9a9 3122 return 0;
3123 if (strlen(nick) > nicklen)
3124 return 0;
3125 return 1;
3126}
3127
3128static struct userNode*
3129AddUser(struct server* uplink, const char *nick, const char *ident, const char *hostname, const char *modes, const char *numeric, const char *userinfo, time_t timestamp, const char *realip)
3130{
3131 struct userNode *oldUser, *uNode;
f0fb2e2d 3132 unsigned int ignore_user, dummy;
412b1e16 3133 char *tstr;
3134 int type;
d76ed9a9 3135
3136 if ((strlen(numeric) < 3) || (strlen(numeric) > 5)) {
1136f709 3137 log_module(MAIN_LOG, LOG_WARNING, "AddUser(%p, %s, ...): numeric %s wrong length!", (void*)uplink, nick, numeric);
d76ed9a9 3138 return NULL;
3139 }
3140
3141 if (!uplink) {
1136f709 3142 log_module(MAIN_LOG, LOG_WARNING, "AddUser(%p, %s, ...): server for numeric %s doesn't exist!", (void*)uplink, nick, numeric);
d76ed9a9 3143 return NULL;
3144 }
3145
3146 if (uplink != GetServerN(numeric)) {
1136f709 3147 log_module(MAIN_LOG, LOG_WARNING, "AddUser(%p, %s, ...): server for numeric %s differs from nominal uplink %s.", (void*)uplink, nick, numeric, uplink->name);
d76ed9a9 3148 return NULL;
3149 }
3150
1136f709 3151 dummy = modes && modes[0] == '*';
3152 if (dummy) {
3153 ++modes;
3154 } else if (!is_valid_nick(nick)) {
3155 log_module(MAIN_LOG, LOG_WARNING, "AddUser(%p, %s, ...): invalid nickname detected.", (void*)uplink, nick);
d76ed9a9 3156 return NULL;
3157 }
3158
3159 ignore_user = 0;
3160 if ((oldUser = GetUserH(nick))) {
1136f709 3161 if (IsLocal(oldUser)
3162 && (IsService(oldUser) || IsPersistent(oldUser))) {
3163 /* The service should collide the new user off - but not
3164 * if the new user is coming in during a burst. (During a
3165 * burst, the bursting server will kill either our user --
3166 * triggering a ReintroduceUser() -- or its own.)
3167 */
d76ed9a9 3168 oldUser->timestamp = timestamp - 1;
1136f709 3169 ignore_user = 1;
3170 if (!uplink->burst)
3171 irc_user(oldUser);
3172 } else if (oldUser->timestamp > timestamp) {
d76ed9a9 3173 /* "Old" user is really newer; remove them */
3174 DelUser(oldUser, 0, 1, "Overruled by older nick");
3175 } else {
3176 /* User being added is too new; do not add them to
3177 * clients, but do add them to the server's list, since it
3178 * will send a KILL and QUIT soon. */
3179 ignore_user = 1;
3180 }
3181 }
3182
3183 /* create new usernode and set all values */
3184 uNode = calloc(1, sizeof(*uNode));
3185 uNode->nick = strdup(nick);
3186 safestrncpy(uNode->ident, ident, sizeof(uNode->ident));
3187 safestrncpy(uNode->info, userinfo, sizeof(uNode->info));
3188 safestrncpy(uNode->hostname, hostname, sizeof(uNode->hostname));
3189 safestrncpy(uNode->numeric, numeric, sizeof(uNode->numeric));
2f61d1d7 3190 irc_p10_pton(&uNode->ip, realip);
37ef8ee3 3191
412b1e16 3192 tstr = conf_get_data("server/type", RECDB_QSTRING);
3193 type = atoi(tstr);
1dfca92d 3194 if (type == 7) {
412b1e16 3195 if (irc_in_addr_is_ipv4(uNode->ip)) {
3196 make_virtip((char*)irc_ntoa(&uNode->ip), (char*)irc_ntoa(&uNode->ip), uNode->cryptip);
3197 make_virthost((char*)irc_ntoa(&uNode->ip), uNode->hostname, uNode->crypthost);
3198 } else if (irc_in_addr_is_ipv6(uNode->ip)) {
3199 make_ipv6virthost((char*)irc_ntoa(&uNode->ip), uNode->hostname, uNode->crypthost);
3200 }
68b75482 3201 }
37ef8ee3 3202
f16ad9e7 3203 if (!uNode->crypthost && uNode->cryptip)
3204 snprintf(uNode->crypthost, sizeof(uNode->crypthost), "%s", strdup(uNode->cryptip));
1136f709 3205 uNode->idle_since = timestamp;
d76ed9a9 3206 uNode->timestamp = timestamp;
3207 modeList_init(&uNode->channels);
3208 uNode->uplink = uplink;
3209 if (++uNode->uplink->clients > uNode->uplink->max_clients) {
3210 uNode->uplink->max_clients = uNode->uplink->clients;
3211 }
3212 uNode->num_local = base64toint(numeric+strlen(uNode->uplink->numeric), 3) & uNode->uplink->num_mask;
3213 uNode->uplink->users[uNode->num_local] = uNode;
3214 mod_usermode(uNode, modes);
1136f709 3215 if (dummy)
3216 uNode->modes |= FLAGS_DUMMY;
21f6caee 3217
3218 set_geoip_info(uNode);
3219
d76ed9a9 3220 if (ignore_user)
3221 return uNode;
3222
3223 dict_insert(clients, uNode->nick, uNode);
3224 if (dict_size(clients) > max_clients) {
3225 max_clients = dict_size(clients);
3226 max_clients_time = now;
3227 }
3228 if (IsLocal(uNode))
3229 irc_user(uNode);
63189c10 3230 call_new_user_funcs(uNode);
3fdd6a74 3231
3232 if ((uNode->loc == 1) && (uNode->handle_info))
3233 send_func_list(uNode);
3234
d76ed9a9 3235 return uNode;
3236}
3237
3238/* removes user from it's server's hash table and nick hash table */
3239void
3240DelUser(struct userNode* user, struct userNode *killer, int announce, const char *why)
3241{
d76ed9a9 3242
ec1a68c8 3243 verify(user);
3244
d76ed9a9 3245 /* mark them as dead, in case anybody cares */
3246 user->dead = 1;
3247
ac3bdc8d 3248 /* remove pending adduser commands */
3249 wipe_adduser_pending(NULL, user);
3250
d76ed9a9 3251 /* remove user from all channels */
3252 while (user->channels.used > 0)
1136f709 3253 DelChannelUser(user, user->channels.list[user->channels.used-1]->channel, NULL, 0);
d76ed9a9 3254
3255 /* Call these in reverse order so ChanServ can update presence
3256 information before NickServ nukes the handle_info. */
a6bcc929 3257 call_del_user_funcs(user, killer, why);
d76ed9a9 3258
3259 user->uplink->clients--;
3260 user->uplink->users[user->num_local] = NULL;
dc92ef7c 3261 if (IsOper(user)) {
d76ed9a9 3262 userList_remove(&curr_opers, user);
dc92ef7c
MB
3263 if (count_opers > 0 && !IsBotM(user) && !IsService(user) && !IsHideOper(user))
3264 count_opers--;
3265 }
d76ed9a9 3266 /* remove from global dictionary, but not if after a collide */
3267 if (user == dict_find(clients, user->nick, NULL))
3268 dict_remove(clients, user->nick);
3269
3270 if (IsInvisible(user))
3271 invis_clients--;
3272
3273 if (announce) {
3274 if (IsLocal(user))
3275 irc_quit(user, why);
3276 else
3277 irc_kill(killer, user, why);
3278 }
3279
1136f709 3280 if (IsLocal(user)) {
3281 unsigned int num = user->num_local;
3282 if (num < num_privmsg_funcs)
3283 privmsg_funcs[num] = NULL;
3284 if (num < num_notice_funcs)
3285 notice_funcs[num] = NULL;
3286 }
3287
d76ed9a9 3288 modeList_clean(&user->channels);
0e08a8e0 3289
3290 /* Clean up version data */
3291 if(user->version_reply) {
3292 free(user->version_reply);
3293 user->version_reply = NULL;
3294 }
3295
ba4b4d51 3296 /* Clean up SSL fingerprint data */
3297 if(user->sslfp) {
3298 free(user->sslfp);
3299 user->sslfp = NULL;
3300 }
3301
2c00fbc2 3302 /* clean up mark */
3303 if(user->mark) {
3304 free(user->mark);
3305 user->mark = NULL;
3306 }
df7819d9
MB
3307 free_string_list(user->marks);
3308 user->marks = NULL;
2c00fbc2 3309
0e08a8e0 3310 /* clean up geoip data if any */
3311 if(user->country_code) free(user->country_code);
3312 if(user->city) free(user->city);
3313 if(user->region) free(user->region);
3314 if(user->postal_code) free(user->postal_code);
3315
d76ed9a9 3316 /* We don't free them, in case we try to privmsg them or something
3317 * (like when a stupid oper kills themself). We just put them onto
3318 * a list of clients that get freed after processing each line.
3319 */
3320 if (dead_users.size)
3321 userList_append(&dead_users, user);
3322 else
3323 free_user(user);
3324}
3325
697f4c9a 3326static void call_oper_funcs(struct userNode *user);
3327
d76ed9a9 3328void mod_usermode(struct userNode *user, const char *mode_change) {
d76ed9a9 3329 int add = 1;
dc92ef7c
MB
3330 long setmodes = 0;
3331 int donemodes = 0;
d76ed9a9 3332 const char *word = mode_change;
3333
3334 if (!user || !mode_change)
3335 return;
ec311f39 3336
3337 call_user_mode_funcs(user, mode_change);
3338
dc92ef7c
MB
3339 setmodes = user->modes;
3340
a32da4c7 3341 while (*word != ' ' && *word) word++;
3342 while (*word == ' ') word++;
dc92ef7c 3343 while (!donemodes) {
d76ed9a9 3344#define do_user_mode(FLAG) do { if (add) user->modes |= FLAG; else user->modes &= ~FLAG; } while (0)
3345 switch (*mode_change++) {
dc92ef7c
MB
3346 case 0: case ' ':
3347 donemodes = 1;
3348 break;
d76ed9a9 3349 case '+': add = 1; break;
3350 case '-': add = 0; break;
3351 case 'o':
3d0b24ce 3352 do_user_mode(FLAGS_OPER);
1136f709 3353 if (!add) {
3354 userList_remove(&curr_opers, user);
3355 } else if (!userList_contains(&curr_opers, user)) {
3356 userList_append(&curr_opers, user);
3357 call_oper_funcs(user);
3358 }
d76ed9a9 3359 break;
3360 case 'O': do_user_mode(FLAGS_LOCOP); break;
3361 case 'i': do_user_mode(FLAGS_INVISIBLE);
3362 if (add)
3363 invis_clients++;
3364 else
3365 invis_clients--;
3366 break;
3367 case 'w': do_user_mode(FLAGS_WALLOP); break;
d76ed9a9 3368 case 'd': do_user_mode(FLAGS_DEAF); break;
3369 case 'k': do_user_mode(FLAGS_SERVICE); break;
3370 case 'g': do_user_mode(FLAGS_GLOBAL); break;
cf8bedff 3371 case 'B': do_user_mode(FLAGS_BOT); break;
3372 case 'n': do_user_mode(FLAGS_HIDECHANS); break;
3373 case 'I': do_user_mode(FLAGS_HIDEIDLE); break;
3374 case 'X': do_user_mode(FLAGS_XTRAOP); break;
412b1e16 3375 case 'C': do_user_mode(FLAGS_CLOAKHOST);
3376 if (*word) {
3377 char cloakhost[MAXLEN];
3378 unsigned int ii;
3379 for (ii=0; (*word != ' ') && (*word != '\0'); )
3380 cloakhost[ii++] = *word++;
3381 cloakhost[ii] = 0;
3382 while (*word == ' ')
3383 word++;
3384 safestrncpy(user->crypthost, cloakhost, sizeof(user->crypthost));
3385 }
3386 break;
3387 case 'c': do_user_mode(FLAGS_CLOAKIP);
3388 if (*word) {
3389 char cloakip[MAXLEN];
3390 unsigned int ii;
3391 for (ii=0; (*word != ' ') && (*word != '\0'); )
3392 cloakip[ii++] = *word++;
3393 cloakip[ii] = 0;
3394 while (*word == ' ')
3395 word++;
3396 safestrncpy(user->cryptip, cloakip, sizeof(user->cryptip));
3397 }
3398 break;
cf8bedff 3399 // sethost - reed/apples
3400 // case 'h': do_user_mode(FLAGS_HELPER); break;
3401 // I check if there's an 'h' in the first part, and if there,
3402 // then everything after the space becomes their new host.
182dd032 3403 case 'h': do_user_mode(FLAGS_SETHOST);
3404 if (*word) {
3405 char sethost[MAXLEN];
3406 unsigned int ii;
3407 for (ii=0; (*word != ' ') && (*word != '\0'); )
3408 sethost[ii++] = *word++;
3409 sethost[ii] = 0;
3410 while (*word == ' ')
3411 word++;
3412 safestrncpy(user->sethost, sethost, sizeof(user->sethost));
3413 }
3414 break;
d76ed9a9 3415 case 'x': do_user_mode(FLAGS_HIDDEN_HOST); break;
3416 case 'r':
3417 if (*word) {
3418 char tag[MAXLEN];
3419 unsigned int ii;
3420 for (ii=0; (*word != ' ') && (*word != '\0'); )
3421 tag[ii++] = *word++;
3422 tag[ii] = 0;
3423 while (*word == ' ')
3424 word++;
3425 call_account_func(user, tag);
3426 }
3427 break;
3428 case 'f':
3429 if (*word) {
3430 char host[MAXLEN];
3431 unsigned int ii;
3432 for (ii=0; (*word != ' ') && (*word != '\0'); )
3433 host[ii++] = *word++;
3434 host[ii] = 0;
3435 while (*word == ' ')
3436 word++;
3437 assign_fakehost(user, host, 0);
3438 }
3439 break;
fc55e757
MB
3440 case 'a': do_user_mode(FLAGS_ADMIN); break;
3441 case 'z': do_user_mode(FLAGS_SSL); break;
8d882965
MB
3442 case 'D': do_user_mode(FLAGS_PRIVDEAF); break;
3443 case 'R': do_user_mode(FLAGS_ACCOUNTONLY); break;
3444 case 'W': do_user_mode(FLAGS_WHOIS); break;
3445 case 'H': do_user_mode(FLAGS_HIDEOPER); break;
3446 case 'L': do_user_mode(FLAGS_NOLINK); break;
3447 case 'q': do_user_mode(FLAGS_COMMONCHANSONLY); break;
d76ed9a9 3448 }
3449#undef do_user_mode
3450 }
dc92ef7c
MB
3451
3452 // Set user mode +o
3453 if (!(setmodes & FLAGS_OPER) && IsOper(user)) {
3454 if (!IsBotM(user) && !IsService(user) && !IsHideOper(user))
3455 count_opers++;
3456 }
3457
3458 // Set user mode -o
3459 if ((setmodes & FLAGS_OPER) && !IsOper(user)) {
3460 if (count_opers > 1 && !(setmodes & FLAGS_BOT) &&
3461 !(setmodes & FLAGS_SERVICE) && !(setmodes & FLAGS_HIDEOPER))
3462 count_opers--;
3463 }
3464
3465 // Set +H, +k or +B
3466 if (!(setmodes & FLAGS_HIDEOPER) &&
3467 !(setmodes & FLAGS_SERVICE) &&
3468 !(setmodes & FLAGS_BOT) &&
3469 (IsHideOper(user) || IsService(user) || IsBotM(user))) {
3470 if ((setmodes & FLAGS_OPER) && IsOper(user) && count_opers > 0)
3471 count_opers--;
3472 }
3473
3474 // Set -H, -k or -B
3475 if (((setmodes & FLAGS_HIDEOPER) ||
3476 (setmodes & FLAGS_SERVICE) ||
3477 (setmodes & FLAGS_BOT)) &&
3478 !IsHideOper(user) && !IsService(user) && !IsBotM(user)) {
3479 if ((setmodes & FLAGS_OPER) && IsOper(user))
3480 count_opers++;
3481 }
d76ed9a9 3482}
3483
1136f709 3484static int
3485keyncpy(char output[], char input[], size_t output_size)
3486{
3487 size_t ii;
3488
3489 if (input[0] == ':')
3490 {
3491 output[0] = '\0';
3492 return 1;
3493 }
3494
3495 for (ii = 0; (ii + 1 < output_size) && (input[ii] != '\0'); ++ii)
3496 {
3497 output[ii] = input[ii];
3498 }
3499
3500 output[ii] = '\0';
3501 return 0;
3502}
3503
d76ed9a9 3504struct mod_chanmode *
2f61d1d7 3505mod_chanmode_parse(struct chanNode *channel, char **modes, unsigned int argc, unsigned int flags, short base_oplevel)
d76ed9a9 3506{
3507 struct mod_chanmode *change;
3508 unsigned int ii, in_arg, ch_arg, add;
3509
3510 if (argc == 0)
3511 return NULL;
3512 if (!(change = mod_chanmode_alloc(argc - 1)))
3513 return NULL;
3514
3515 for (ii = ch_arg = 0, in_arg = add = 1;
3516 (modes[0][ii] != '\0') && (modes[0][ii] != ' ');
3517 ++ii) {
3518 switch (modes[0][ii]) {
3519 case '+':
3520 add = 1;
3521 break;
3522 case '-':
3523 add = 0;
3524 break;
3525#define do_chan_mode(FLAG) do { if (add) change->modes_set |= (FLAG), change->modes_clear &= ~(FLAG); else change->modes_clear |= (FLAG), change->modes_set &= ~(FLAG); } while(0)
3526 case 'C': do_chan_mode(MODE_NOCTCPS); break;
3527 case 'D': do_chan_mode(MODE_DELAYJOINS); break;
3528 case 'c': do_chan_mode(MODE_NOCOLORS); break;
3529 case 'i': do_chan_mode(MODE_INVITEONLY); break;
3530 case 'm': do_chan_mode(MODE_MODERATED); break;
3531 case 'n': do_chan_mode(MODE_NOPRIVMSGS); break;
3532 case 'p': do_chan_mode(MODE_PRIVATE); break;
3533 case 'r': do_chan_mode(MODE_REGONLY); break;
3534 case 's': do_chan_mode(MODE_SECRET); break;
3535 case 't': do_chan_mode(MODE_TOPICLIMIT); break;
d0cb2fb6 3536 case 'S': do_chan_mode(MODE_STRIPCOLOR); break;
3537 case 'M': do_chan_mode(MODE_MODUNREG); break;
3538 case 'N': do_chan_mode(MODE_NONOTICE); break;
3539 case 'Q': do_chan_mode(MODE_NOQUITMSGS); break;
3540 case 'T': do_chan_mode(MODE_NOAMSG); break;
3541 case 'O': do_chan_mode(MODE_OPERSONLY); break;
4648ded2 3542 case 'a': do_chan_mode(MODE_ADMINSONLY); break;
c8ca69a0 3543 case 'Z': do_chan_mode(MODE_SSLONLY); break;
88c7cb10 3544 case 'L': do_chan_mode(MODE_HIDEMODE); break;
ec1a68c8 3545 case 'z':
3546 if (!(flags & MCP_REGISTERED)) {
1136f709 3547 do_chan_mode(MODE_REGISTERED);
ec1a68c8 3548 } else {
1136f709 3549 mod_chanmode_free(change);
3550 return NULL;
ec1a68c8 3551 }
3552 break;
d76ed9a9 3553#undef do_chan_mode
3554 case 'l':
3555 if (add) {
3556 if (in_arg >= argc)
3557 goto error;
3558 change->modes_set |= MODE_LIMIT;
3559 change->new_limit = atoi(modes[in_arg++]);
3560 } else {
3561 change->modes_set &= ~MODE_LIMIT;
3562 change->modes_clear |= MODE_LIMIT;
3563 }
3564 break;
3565 case 'k':
3566 if (add) {
1136f709 3567 if ((in_arg >= argc)
3568 || keyncpy(change->new_key, modes[in_arg++], sizeof(change->new_key)))
d76ed9a9 3569 goto error;
3570 change->modes_set |= MODE_KEY;
d76ed9a9 3571 } else {
3572 change->modes_clear |= MODE_KEY;
3573 if (!(flags & MCP_KEY_FREE)) {
3574 if (in_arg >= argc)
3575 goto error;
3576 in_arg++;
3577 }
3578 }
3579 break;
2f61d1d7 3580 case 'U':
3581 if (add)
3582 {
1136f709 3583 if ((in_arg >= argc)
3584 || keyncpy(change->new_upass, modes[in_arg++], sizeof(change->new_upass)))
3585 goto error;
3586 change->modes_set |= MODE_UPASS;
2f61d1d7 3587 } else {
3588 change->modes_clear |= MODE_UPASS;
3589 if (!(flags & MCP_UPASS_FREE)) {
3590 if (in_arg >= argc)
3591 goto error;
3592 in_arg++;
3593 }
3594 }
3595 break;
3596 case 'A':
3597 if (add) {
1136f709 3598 if ((in_arg >= argc)
2b9de5dd
MB
3599 || keyncpy(change->new_apass, modes[in_arg++], sizeof(change->new_apass)))
3600 goto error;
2f61d1d7 3601 change->modes_set |= MODE_APASS;
2f61d1d7 3602 } else {
3603 change->modes_clear |= MODE_APASS;
3604 if (!(flags & MCP_APASS_FREE)) {
3605 if (in_arg >= argc)
3606 goto error;
3607 in_arg++;
3608 }
3609 }
3610 break;
d76ed9a9 3611 case 'b':
3612 if (!(flags & MCP_ALLOW_OVB))
3613 goto error;
3614 if (in_arg >= argc)
3615 goto error;
3616 change->args[ch_arg].mode = MODE_BAN;
3617 if (!add)
3618 change->args[ch_arg].mode |= MODE_REMOVE;
a32da4c7 3619 change->args[ch_arg++].u.hostmask = modes[in_arg++];
d76ed9a9 3620 break;
2aef5f4b 3621 case 'e':
3622 if (!(flags & MCP_ALLOW_OVB))
3623 goto error;
3624 if (in_arg >= argc)
3625 goto error;
3626 change->args[ch_arg].mode = MODE_EXEMPT;
3627 if (!add)
3628 change->args[ch_arg].mode |= MODE_REMOVE;
a32da4c7 3629 change->args[ch_arg++].u.hostmask = modes[in_arg++];
2aef5f4b 3630 break;
55342ce8 3631 case 'o': case 'h': case 'v':
d76ed9a9 3632 {
3633 struct userNode *victim;
2f61d1d7 3634 char *oplevel_str;
3635 int oplevel;
3636
3637 if (in_arg >= argc)
3638 goto error;
3639 oplevel_str = strchr(modes[in_arg], ':');
3640 if (oplevel_str)
3641 {
3642 /* XXYYY M #channel +o XXYYY:<oplevel> */
3643 *oplevel_str++ = '\0';
3644 oplevel = parse_oplevel(oplevel_str);
3645 if (oplevel <= base_oplevel && !(flags & MCP_FROM_SERVER))
3646 oplevel = base_oplevel + 1;
3647 }
3648 else if (channel->modes & MODE_UPASS)
3649 oplevel = base_oplevel + 1;
3650 else
3651 oplevel = -1;
3652
3653 /* Check that oplevel is within bounds. */
3654 if (oplevel > MAXOPLEVEL)
3655 oplevel = MAXOPLEVEL;
3656
d76ed9a9 3657 if (!(flags & MCP_ALLOW_OVB))
3658 goto error;
3659 if (in_arg >= argc)
3660 goto error;
55342ce8 3661
3662 if (modes[0][ii] == 'o')
3663 change->args[ch_arg].mode = MODE_CHANOP;
3664 else if (modes[0][ii] == 'h')
3665 change->args[ch_arg].mode = MODE_HALFOP;
3666 else if (modes[0][ii] == 'v')
3667 change->args[ch_arg].mode = MODE_VOICE;
3668
d76ed9a9 3669 if (!add)
3670 change->args[ch_arg].mode |= MODE_REMOVE;
3671 if (flags & MCP_FROM_SERVER)
3672 victim = GetUserN(modes[in_arg++]);
3673 else
3674 victim = GetUserH(modes[in_arg++]);
3675 if (!victim)
3676 continue;
a32da4c7 3677 if ((change->args[ch_arg].u.member = GetUserMode(channel, victim)))
2f61d1d7 3678 {
3679 /* Apply the oplevel change */
3680 change->args[ch_arg].u.member->oplevel = oplevel;
d76ed9a9 3681 ch_arg++;
2f61d1d7 3682 }
d76ed9a9 3683 break;
3684 }
3685 default:
3686 if (!(flags & MCP_FROM_SERVER))
3687 goto error;
3688 break;
3689 }
3690 }
3691 change->argc = ch_arg; /* in case any turned out to be ignored */
3692 if (change->modes_set & MODE_SECRET) {
3693 change->modes_set &= ~(MODE_PRIVATE);
3694 change->modes_clear |= MODE_PRIVATE;
3695 } else if (change->modes_set & MODE_PRIVATE) {
3696 change->modes_set &= ~(MODE_SECRET);
3697 change->modes_clear |= MODE_SECRET;
3698 }
3699 return change;
3700 error:
3701 mod_chanmode_free(change);
3702 return NULL;
3703}
3704
3705struct chanmode_buffer {
3706 char modes[MAXLEN];
3707 char args[MAXLEN];
3708 struct chanNode *channel;
3709 struct userNode *actor;
3710 unsigned int modes_used;
3711 unsigned int args_used;
3712 size_t chname_len;
3713 unsigned int is_add : 1;
3714 unsigned int is_chanop : 1;
3715};
3716
3717static void
3718mod_chanmode_append(struct chanmode_buffer *buf, char ch, const char *arg)
3719{
3720 size_t arg_len = strlen(arg);
3721 if (buf->modes_used > (MAXMODEPARAMS) ||
3722 buf->modes_used + buf->args_used + buf->chname_len + arg_len > 450) {
3723 memcpy(buf->modes + buf->modes_used, buf->args, buf->args_used);
3724 buf->modes[buf->modes_used + buf->args_used] = '\0';
3725 irc_mode((buf->is_chanop ? buf->actor : NULL), buf->channel, buf->modes);
3726 buf->modes[0] = buf->is_add ? '+' : '-';
3727 buf->modes_used = 1;
3728 buf->args_used = 0;
3729 }
3730 buf->modes[buf->modes_used++] = ch;
3731 buf->args[buf->args_used++] = ' ';
3732 memcpy(buf->args + buf->args_used, arg, arg_len);
3733 buf->args_used += arg_len;
3734}
3735
3736void
3737mod_chanmode_announce(struct userNode *who, struct chanNode *channel, struct mod_chanmode *change)
3738{
3739 struct chanmode_buffer chbuf;
3740 unsigned int arg;
3741 struct modeNode *mn;
3742 char int_buff[32], mode = '\0';
3743
3744 assert(change->argc <= change->alloc_argc);
3745 memset(&chbuf, 0, sizeof(chbuf));
3746 chbuf.channel = channel;
3747 chbuf.actor = who;
3748 chbuf.chname_len = strlen(channel->name);
3749
3750 mn = GetUserMode(channel, who);
3751 if ((mn && (mn->modes & MODE_CHANOP)) || off_channel)
3752 chbuf.is_chanop = 1;
3753
3754 /* First remove modes */
3755 chbuf.is_add = 0;
3756 if (change->modes_clear) {
3757 if (mode != '-')
3758 chbuf.modes[chbuf.modes_used++] = mode = '-';
3759#define DO_MODE_CHAR(BIT, CHAR) if (change->modes_clear & MODE_##BIT) chbuf.modes[chbuf.modes_used++] = CHAR
3760 DO_MODE_CHAR(PRIVATE, 'p');
3761 DO_MODE_CHAR(SECRET, 's');
3762 DO_MODE_CHAR(MODERATED, 'm');
3763 DO_MODE_CHAR(TOPICLIMIT, 't');
3764 DO_MODE_CHAR(INVITEONLY, 'i');
3765 DO_MODE_CHAR(NOPRIVMSGS, 'n');
3766 DO_MODE_CHAR(LIMIT, 'l');
3767 DO_MODE_CHAR(DELAYJOINS, 'D');
3768 DO_MODE_CHAR(REGONLY, 'r');
3769 DO_MODE_CHAR(NOCOLORS, 'c');
3770 DO_MODE_CHAR(NOCTCPS, 'C');
d0cb2fb6 3771 DO_MODE_CHAR(STRIPCOLOR, 'S');
3772 DO_MODE_CHAR(MODUNREG, 'M');
3773 DO_MODE_CHAR(NONOTICE, 'N');
3774 DO_MODE_CHAR(NOQUITMSGS, 'Q');
3775 DO_MODE_CHAR(NOAMSG, 'T');
3776 DO_MODE_CHAR(OPERSONLY, 'O');
238102e1 3777 DO_MODE_CHAR(ADMINSONLY, 'a');
ec1a68c8 3778 DO_MODE_CHAR(REGISTERED, 'z');
c8ca69a0 3779 DO_MODE_CHAR(SSLONLY, 'Z');
88c7cb10 3780 DO_MODE_CHAR(HIDEMODE, 'L');
d76ed9a9 3781#undef DO_MODE_CHAR
3782 if (change->modes_clear & channel->modes & MODE_KEY)
3783 mod_chanmode_append(&chbuf, 'k', channel->key);
2f61d1d7 3784 if (change->modes_clear & channel->modes & MODE_UPASS)
3785 mod_chanmode_append(&chbuf, 'U', channel->upass);
1136f709 3786 if (change->modes_clear & channel->modes & MODE_APASS)
2f61d1d7 3787 mod_chanmode_append(&chbuf, 'A', channel->apass);
d76ed9a9 3788 }
3789 for (arg = 0; arg < change->argc; ++arg) {
3790 if (!(change->args[arg].mode & MODE_REMOVE))
3791 continue;
3792 if (mode != '-')
3793 chbuf.modes[chbuf.modes_used++] = mode = '-';
3794 switch (change->args[arg].mode & ~MODE_REMOVE) {
3795 case MODE_BAN:
a32da4c7 3796 mod_chanmode_append(&chbuf, 'b', change->args[arg].u.hostmask);
d76ed9a9 3797 break;
2aef5f4b 3798 case MODE_EXEMPT:
a32da4c7 3799 mod_chanmode_append(&chbuf, 'e', change->args[arg].u.hostmask);
2aef5f4b 3800 break;
d76ed9a9 3801 default:
3802 if (change->args[arg].mode & MODE_CHANOP)
a32da4c7 3803 mod_chanmode_append(&chbuf, 'o', change->args[arg].u.member->user->numeric);
55342ce8 3804 if (change->args[arg].mode & MODE_HALFOP)
a32da4c7 3805 mod_chanmode_append(&chbuf, 'h', change->args[arg].u.member->user->numeric);
d76ed9a9 3806 if (change->args[arg].mode & MODE_VOICE)
a32da4c7 3807 mod_chanmode_append(&chbuf, 'v', change->args[arg].u.member->user->numeric);
d76ed9a9 3808 break;
3809 }
3810 }
3811
3812 /* Then set them */
3813 chbuf.is_add = 1;
3814 if (change->modes_set) {
3815 if (mode != '+')
3816 chbuf.modes[chbuf.modes_used++] = mode = '+';
3817#define DO_MODE_CHAR(BIT, CHAR) if (change->modes_set & MODE_##BIT) chbuf.modes[chbuf.modes_used++] = CHAR
3818 DO_MODE_CHAR(PRIVATE, 'p');
3819 DO_MODE_CHAR(SECRET, 's');
3820 DO_MODE_CHAR(MODERATED, 'm');
3821 DO_MODE_CHAR(TOPICLIMIT, 't');
3822 DO_MODE_CHAR(INVITEONLY, 'i');
3823 DO_MODE_CHAR(NOPRIVMSGS, 'n');
3824 DO_MODE_CHAR(DELAYJOINS, 'D');
3825 DO_MODE_CHAR(REGONLY, 'r');
3826 DO_MODE_CHAR(NOCOLORS, 'c');
3827 DO_MODE_CHAR(NOCTCPS, 'C');
d0cb2fb6 3828 DO_MODE_CHAR(STRIPCOLOR, 'S');
3829 DO_MODE_CHAR(MODUNREG, 'M');
3830 DO_MODE_CHAR(NONOTICE, 'N');
3831 DO_MODE_CHAR(NOQUITMSGS, 'Q');
3832 DO_MODE_CHAR(NOAMSG, 'T');
3833 DO_MODE_CHAR(OPERSONLY, 'O');
238102e1 3834 DO_MODE_CHAR(ADMINSONLY, 'a');
ec1a68c8 3835 DO_MODE_CHAR(REGISTERED, 'z');
c8ca69a0 3836 DO_MODE_CHAR(SSLONLY, 'Z');
88c7cb10 3837 DO_MODE_CHAR(HIDEMODE, 'L');
d76ed9a9 3838#undef DO_MODE_CHAR
3839 if(change->modes_set & MODE_KEY)
3840 mod_chanmode_append(&chbuf, 'k', change->new_key);
2f61d1d7 3841 if (change->modes_set & MODE_UPASS)
3842 mod_chanmode_append(&chbuf, 'U', change->new_upass);
3843 if (change->modes_set & MODE_APASS)
3844 mod_chanmode_append(&chbuf, 'A', change->new_apass);
d76ed9a9 3845 if(change->modes_set & MODE_LIMIT) {
3846 sprintf(int_buff, "%d", change->new_limit);
3847 mod_chanmode_append(&chbuf, 'l', int_buff);
3848 }
3849 }
3850 for (arg = 0; arg < change->argc; ++arg) {
3851 if (change->args[arg].mode & MODE_REMOVE)
3852 continue;
3853 if (mode != '+')
3854 chbuf.modes[chbuf.modes_used++] = mode = '+';
3855 switch (change->args[arg].mode) {
3856 case MODE_BAN:
a32da4c7 3857 mod_chanmode_append(&chbuf, 'b', change->args[arg].u.hostmask);
d76ed9a9 3858 break;
2aef5f4b 3859 case MODE_EXEMPT:
a32da4c7 3860 mod_chanmode_append(&chbuf, 'e', change->args[arg].u.hostmask);
2aef5f4b 3861 break;
d76ed9a9 3862 default:
3863 if (change->args[arg].mode & MODE_CHANOP)
a32da4c7 3864 mod_chanmode_append(&chbuf, 'o', change->args[arg].u.member->user->numeric);
55342ce8 3865 if (change->args[arg].mode & MODE_HALFOP)
a32da4c7 3866 mod_chanmode_append(&chbuf, 'h', change->args[arg].u.member->user->numeric);
d76ed9a9 3867 if (change->args[arg].mode & MODE_VOICE)
a32da4c7 3868 mod_chanmode_append(&chbuf, 'v', change->args[arg].u.member->user->numeric);
d76ed9a9 3869 break;
3870 }
3871 }
3872
3873 /* Flush the buffer and apply changes locally */
3874 if (chbuf.modes_used > 0) {
3875 memcpy(chbuf.modes + chbuf.modes_used, chbuf.args, chbuf.args_used);
3876 chbuf.modes[chbuf.modes_used + chbuf.args_used] = '\0';
3877 irc_mode((chbuf.is_chanop ? chbuf.actor : NULL), chbuf.channel, chbuf.modes);
3878 }
3879 mod_chanmode_apply(who, channel, change);
3880}
3881
3882char *
3883mod_chanmode_format(struct mod_chanmode *change, char *outbuff)
3884{
3885 unsigned int used = 0;
3886 assert(change->argc <= change->alloc_argc);
3887 if (change->modes_clear) {
3888 outbuff[used++] = '-';
3889#define DO_MODE_CHAR(BIT, CHAR) if (change->modes_clear & MODE_##BIT) outbuff[used++] = CHAR
3890 DO_MODE_CHAR(PRIVATE, 'p');
3891 DO_MODE_CHAR(SECRET, 's');
3892 DO_MODE_CHAR(MODERATED, 'm');
3893 DO_MODE_CHAR(TOPICLIMIT, 't');
3894 DO_MODE_CHAR(INVITEONLY, 'i');
3895 DO_MODE_CHAR(NOPRIVMSGS, 'n');
3896 DO_MODE_CHAR(LIMIT, 'l');
3897 DO_MODE_CHAR(KEY, 'k');
2f61d1d7 3898 DO_MODE_CHAR(UPASS, 'U');
3899 DO_MODE_CHAR(APASS, 'A');
d76ed9a9 3900 DO_MODE_CHAR(DELAYJOINS, 'D');
3901 DO_MODE_CHAR(REGONLY, 'r');
3902 DO_MODE_CHAR(NOCOLORS, 'c');
3903 DO_MODE_CHAR(NOCTCPS, 'C');
d0cb2fb6 3904 DO_MODE_CHAR(STRIPCOLOR, 'S');
3905 DO_MODE_CHAR(MODUNREG, 'M');
3906 DO_MODE_CHAR(NONOTICE, 'N');
3907 DO_MODE_CHAR(NOQUITMSGS, 'Q');
3908 DO_MODE_CHAR(NOAMSG, 'T');
3909 DO_MODE_CHAR(OPERSONLY, 'O');
238102e1 3910 DO_MODE_CHAR(ADMINSONLY, 'a');
ec1a68c8 3911 DO_MODE_CHAR(REGISTERED, 'z');
c8ca69a0 3912 DO_MODE_CHAR(SSLONLY, 'Z');
88c7cb10 3913 DO_MODE_CHAR(HIDEMODE, 'L');
d76ed9a9 3914#undef DO_MODE_CHAR
3915 }
3916 if (change->modes_set) {
3917 outbuff[used++] = '+';
3918#define DO_MODE_CHAR(BIT, CHAR) if (change->modes_set & MODE_##BIT) outbuff[used++] = CHAR
3919 DO_MODE_CHAR(PRIVATE, 'p');
3920 DO_MODE_CHAR(SECRET, 's');
3921 DO_MODE_CHAR(MODERATED, 'm');
3922 DO_MODE_CHAR(TOPICLIMIT, 't');
3923 DO_MODE_CHAR(INVITEONLY, 'i');
3924 DO_MODE_CHAR(NOPRIVMSGS, 'n');
3925 DO_MODE_CHAR(DELAYJOINS, 'D');
3926 DO_MODE_CHAR(REGONLY, 'r');
3927 DO_MODE_CHAR(NOCOLORS, 'c');
3928 DO_MODE_CHAR(NOCTCPS, 'C');
d0cb2fb6 3929 DO_MODE_CHAR(STRIPCOLOR, 'S');
3930 DO_MODE_CHAR(MODUNREG, 'M');
3931 DO_MODE_CHAR(NONOTICE, 'N');
3932 DO_MODE_CHAR(NOQUITMSGS, 'Q');
3933 DO_MODE_CHAR(NOAMSG, 'T');
3934 DO_MODE_CHAR(OPERSONLY, 'O');
238102e1 3935 DO_MODE_CHAR(ADMINSONLY, 'a');
ec1a68c8 3936 DO_MODE_CHAR(REGISTERED, 'z');
c8ca69a0 3937 DO_MODE_CHAR(SSLONLY, 'Z');
88c7cb10 3938 DO_MODE_CHAR(HIDEMODE, 'L');
2f61d1d7 3939
2b9de5dd
MB
3940 DO_MODE_CHAR(KEY, 'k');
3941 DO_MODE_CHAR(LIMIT, 'l');
3942 DO_MODE_CHAR(APASS, 'A');
3943 DO_MODE_CHAR(UPASS, 'U');
3944#undef DO_MODE_CHAR
2f61d1d7 3945
2b9de5dd
MB
3946#define DO_MODE_PARM(BIT, PARM) if (change->modes_set & MODE_##BIT) used += sprintf(outbuff+used, " %s", PARM);
3947 DO_MODE_PARM(KEY, change->new_key);
0fb450a2 3948 if (change->modes_set & MODE_LIMIT) used += sprintf(outbuff+used, " %d", change->new_limit);
2b9de5dd
MB
3949 DO_MODE_PARM(APASS, change->new_apass);
3950 DO_MODE_PARM(UPASS, change->new_upass);
3951#undef DO_MODE_PARM
d76ed9a9 3952 }
3953 outbuff[used] = 0;
3954 return outbuff;
3955}
3956
3957static int
3958clear_chanmode(struct chanNode *channel, const char *modes)
3959{
1136f709 3960 unsigned int cleared;
d76ed9a9 3961
1136f709 3962 for (cleared = 0; *modes; modes++) {
d76ed9a9 3963 switch (*modes) {
1136f709 3964 case 'o': cleared |= MODE_CHANOP; break;
3965 case 'h': cleared |= MODE_HALFOP; break;
3966 case 'v': cleared |= MODE_VOICE; break;
3967 case 'p': cleared |= MODE_PRIVATE; break;
3968 case 's': cleared |= MODE_SECRET; break;
3969 case 'm': cleared |= MODE_MODERATED; break;
3970 case 't': cleared |= MODE_TOPICLIMIT; break;
3971 case 'i': cleared |= MODE_INVITEONLY; break;
3972 case 'n': cleared |= MODE_NOPRIVMSGS; break;
d76ed9a9 3973 case 'k':
1136f709 3974 cleared |= MODE_KEY;
d76ed9a9 3975 channel->key[0] = '\0';
3976 break;
2f61d1d7 3977 case 'A':
1136f709 3978 cleared |= MODE_APASS;
2f61d1d7 3979 channel->apass[0] = '\0';
3980 break;
3981 case 'U':
1136f709 3982 cleared |= MODE_UPASS;
2f61d1d7 3983 channel->upass[0] = '\0';
3984 break;
d76ed9a9 3985 case 'l':
1136f709 3986 cleared |= MODE_LIMIT;
d76ed9a9 3987 channel->limit = 0;
3988 break;
1136f709 3989 case 'b': cleared |= MODE_BAN; break;
3990 case 'e': cleared |= MODE_EXEMPT; break;
3991 case 'D': cleared |= MODE_DELAYJOINS; break;
3992 case 'r': cleared |= MODE_REGONLY; break;
3993 case 'c': cleared |= MODE_NOCOLORS; break;
3994 case 'C': cleared |= MODE_NOCTCPS; break;
3995 case 'S': cleared |= MODE_STRIPCOLOR; break;
3996 case 'M': cleared |= MODE_MODUNREG; break;
3997 case 'N': cleared |= MODE_NONOTICE; break;
3998 case 'Q': cleared |= MODE_NOQUITMSGS; break;
3999 case 'T': cleared |= MODE_NOAMSG; break;
4000 case 'O': cleared |= MODE_OPERSONLY; break;
4001 case 'a': cleared |= MODE_ADMINSONLY; break;
4002 case 'z': cleared |= MODE_REGISTERED; break;
4003 case 'Z': cleared |= MODE_SSLONLY; break;
4004 case 'L': cleared |= MODE_HIDEMODE; break;
d76ed9a9 4005 }
4006 }
4007
1136f709 4008 if (!cleared)
d76ed9a9 4009 return 1;
4010
4011 /* Remove simple modes. */
1136f709 4012 channel->modes &= ~cleared;
d76ed9a9 4013
4014 /* If removing bans, kill 'em all. */
1136f709 4015 if ((cleared & MODE_BAN) && channel->banlist.used) {
d76ed9a9 4016 unsigned int i;
4017 for (i=0; i<channel->banlist.used; i++)
4018 free(channel->banlist.list[i]);
4019 channel->banlist.used = 0;
4020 }
4021
2aef5f4b 4022 /* If removing exempts, kill 'em all. */
1136f709 4023 if ((cleared & MODE_EXEMPT) && channel->exemptlist.used) {
2aef5f4b 4024 unsigned int i;
4025 for (i=0; i<channel->exemptlist.used; i++)
4026 free(channel->exemptlist.list[i]);
4027 channel->exemptlist.used = 0;
4028 }
4029
1136f709 4030 /* Removed member modes. */
4031 if ((cleared & (MODE_CHANOP | MODE_HALFOP | MODE_VOICE)) && channel->members.used) {
4032 int mask = ~(cleared & (MODE_CHANOP | MODE_HALFOP | MODE_VOICE));
d76ed9a9 4033 unsigned int i;
4034
4035 for (i = 0; i < channel->members.used; i++)
4036 channel->members.list[i]->modes &= mask;
4037 }
4038
4039 return 1;
4040}
4041
4042void
4043reg_privmsg_func(struct userNode *user, privmsg_func_t handler)
4044{
4045 unsigned int numeric = user->num_local;
4046 if (numeric >= num_privmsg_funcs) {
1136f709 4047 int newnum = numeric + 8, ii;
d76ed9a9 4048 privmsg_funcs = realloc(privmsg_funcs, newnum*sizeof(privmsg_func_t));
1136f709 4049 for (ii = num_privmsg_funcs; ii < newnum; ++ii)
4050 privmsg_funcs[ii] = NULL;
d76ed9a9 4051 num_privmsg_funcs = newnum;
4052 }
4053 if (privmsg_funcs[numeric])
4054 log_module(MAIN_LOG, LOG_WARNING, "re-registering new privmsg handler for numeric %d", numeric);
4055 privmsg_funcs[numeric] = handler;
4056}
4057
4058void
1136f709 4059unreg_privmsg_func(struct userNode *user)
d76ed9a9 4060{
1136f709 4061 if (!IsLocal(user) || user->num_local >= num_privmsg_funcs)
4062 return; /* this really only works with users */
d76ed9a9 4063
1136f709 4064 privmsg_funcs[user->num_local] = NULL;
d76ed9a9 4065}
4066
4067
4068void
4069reg_notice_func(struct userNode *user, privmsg_func_t handler)
4070{
4071 unsigned int numeric = user->num_local;
4072 if (numeric >= num_notice_funcs) {
1136f709 4073 int newnum = numeric + 8, ii;
d76ed9a9 4074 notice_funcs = realloc(notice_funcs, newnum*sizeof(privmsg_func_t));
1136f709 4075 for (ii = num_privmsg_funcs; ii < newnum; ++ii)
4076 privmsg_funcs[ii] = NULL;
d76ed9a9 4077 num_notice_funcs = newnum;
4078 }
4079 if (notice_funcs[numeric])
4080 log_module(MAIN_LOG, LOG_WARNING, "re-registering new notice handler for numeric %d", numeric);
4081 notice_funcs[numeric] = handler;
4082}
4083
4084void
1136f709 4085unreg_notice_func(struct userNode *user)
d76ed9a9 4086{
1136f709 4087 if (!IsLocal(user) || user->num_local >= num_privmsg_funcs)
4088 return; /* this really only works with users */
d76ed9a9 4089
1136f709 4090 notice_funcs[user->num_local] = NULL;
d76ed9a9 4091}
4092
4093static void
4094send_burst(void)
4095{
4096 unsigned int i, hop, max_hop=1;
4097 dict_iterator_t it;
4098
4099 /* burst (juped) servers, closest first (except self, which is sent already) */
4100 for (i=0; i<ArrayLength(servers_num); i++)
4101 if (servers_num[i] && servers_num[i]->hops > max_hop)
4102 max_hop = servers_num[i]->hops;
4103 for (hop=1; hop<=max_hop; hop++) {
4104 for (i=0;i<ArrayLength(servers_num);i++) {
4105 if (servers_num[i]
4106 && (servers_num[i]->hops == hop)
4107 && (servers_num[i] != self->uplink))
4108 irc_server(servers_num[i]);
4109 }
4110 }
4111
4112 /* burst local nicks */
4113 for (i=0; i<=self->num_mask; i++)
4114 if (self->users[i])
4115 irc_user(self->users[i]);
4116
4117 /* build dict of unbursted channel names (just copy existing channels) */
4118 unbursted_channels = dict_new();
4119 for (it = dict_first(channels); it; it = iter_next(it))
4120 dict_insert(unbursted_channels, iter_key(it), iter_data(it));
4121}
2f61d1d7 4122
4123/*
4124 * Oplevel parsing
4125 */
4126static int
4127parse_oplevel(char *str)
4128{
4129 int oplevel = 0;
4130 while (isdigit(*str))
4131 oplevel = oplevel * 10 + *str++ - '0';
4132 return oplevel;
4133}
4134