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