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