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