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