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