]> jfr.im git - irc/evilnet/x3.git/blame - src/proto-p10.c
nickserv: support auto_admin and auto_oper for login-on-connect
[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
10be9be0 1199void irc_sno(unsigned int mask, char const* format, ...) {
1200 va_list arg_list;
1201 char buffer[MAXLEN];
1202 va_start(arg_list, format);
1203 vsnprintf(buffer, MAXLEN-2, format, arg_list);
1204 buffer[MAXLEN-1] = 0;
1205 putsock("%s " CMD_SNO " %d :%s", self->numeric, mask, buffer);
1206}
1207
d76ed9a9 1208static void send_burst(void);
1209
1210static void
1211change_nicklen(int new_nicklen)
1212{
1213 unsigned int nn;
1214 char new_nick[NICKLEN+1];
1215 struct userNode *user;
1216
1217 nicklen = new_nicklen;
1218 /* fix up any users we have here */
1219 for (nn=0; nn<=self->num_mask; nn++) {
1220 if (!(user = self->users[nn]))
1221 continue;
1222 safestrncpy(new_nick, user->nick, sizeof(new_nick));
1223 new_nick[nicklen] = 0;
1224 NickChange(user, new_nick, 1);
1225 }
1226}
1227
1228static CMD_FUNC(cmd_whois)
1229{
1230 struct userNode *from;
1231 struct userNode *who;
1232
1233 if (argc < 3)
1234 return 0;
1235 if (!(from = GetUserH(origin))) {
1236 log_module(MAIN_LOG, LOG_ERROR, "Could not find WHOIS origin user %s", origin);
1237 return 0;
1238 }
1239 if(!(who = GetUserH(argv[2]))) {
1240 irc_numeric(from, ERR_NOSUCHNICK, "%s@%s :No such nick", argv[2], self->name);
1241 return 1;
1242 }
0f6fe38c 1243 if (IsHiddenHost(who) && !IsOper(from)) {
1244 /* Just stay quiet. */
1245 return 1;
d76ed9a9 1246 }
0f6fe38c 1247 irc_numeric(from, RPL_WHOISUSER, "%s %s %s * :%s", who->nick, who->ident, who->hostname, who->info);
1248 if (his_servername && his_servercomment)
a32da4c7 1249 irc_numeric(from, RPL_WHOISSERVER, "%s %s :%s", who->nick, his_servername, his_servercomment);
d76ed9a9 1250 else
a32da4c7 1251 irc_numeric(from, RPL_WHOISSERVER, "%s %s :%s", who->nick, who->uplink->name, who->uplink->description);
1252 if (IsOper(who))
0f6fe38c 1253 irc_numeric(from, RPL_WHOISOPERATOR, "%s :is a megalomaniacal power hungry tyrant", who->nick);
d76ed9a9 1254 irc_numeric(from, RPL_ENDOFWHOIS, "%s :End of /WHOIS list", who->nick);
1255 return 1;
1256}
1257
1258static CMD_FUNC(cmd_server)
1259{
1260 struct server *srv;
1261 const char *str;
1262
1263 if (argc < 8)
1264 return 0;
a32da4c7 1265 if (self->uplink) {
d76ed9a9 1266 /* another server introduced us */
1267 srv = AddServer(GetServerH(origin), argv[1], atoi(argv[2]), atoi(argv[3]), atoi(argv[4]), argv[6], argv[argc-1]);
1268 if (!srv)
1269 return 0;
1270 srv->self_burst = argv[5][0] == 'J';
1271 srv->burst = 1;
1272 } else {
1273 /* this must be our uplink */
1274 srv = self->uplink = AddServer(self, argv[1], atoi(argv[2]), atoi(argv[3]), atoi(argv[4]), argv[6], argv[argc-1]);
1275 if (!srv)
1276 return 0;
1277 srv->self_burst = argv[5][0] == 'J';
1278 srv->burst = 1;
1279 if ((argv[7][0] == '+') && !force_n2k) {
1280 log_module(MAIN_LOG, LOG_WARNING, "Got Undernet-style SERVER message but \"force_n2k\" not on.");
1281 }
1282 send_burst();
1283 }
1284
1285 /* Fix up our timestamps if necessary. */
1286 if (srv->boot <= PREHISTORY) {
1287 /* Server from the mists of time.. */
1288 if (srv->hops == 1) {
1289 log_module(MAIN_LOG, LOG_ERROR, "Server %s claims to have booted at time "FMT_TIME_T". This is absurd.", srv->name, srv->boot);
1290 }
1291 } else if ((str = conf_get_data("server/reliable_clock", RECDB_QSTRING))
1292 && enabled_string(str)) {
1293 /* If we have a reliable clock, we just keep our current time. */
1294 } else {
1295 if (srv->boot <= self->boot) {
1296 /* The other server is older than us. Accept their timestamp.
1297 * Alternately, we are same age, but we accept their time
1298 * since we are linking to them. */
1299 self->boot = srv->boot;
1300 ioset_set_time(srv->link);
1301 }
1302 }
1303 if (srv == self->uplink) {
1304 extern time_t burst_begin;
1305 burst_begin = now;
1306 }
1307 return 1;
1308}
1309
47956fc5 1310
d76ed9a9 1311static CMD_FUNC(cmd_eob)
1312{
1313 struct server *sender;
1314 dict_iterator_t it;
1315 unsigned int ii;
1316
1317 if (!(sender = GetServerH(origin)))
1318 return 0;
1319 if (sender == self->uplink) {
1320 cManager.uplink->state = CONNECTED;
1321 for (it = dict_first(unbursted_channels); it; it = iter_next(it))
1322 irc_burst(iter_data(it));
1323 dict_delete(unbursted_channels);
1324 unbursted_channels = NULL;
1325 irc_eob();
1326 irc_eob_ack();
47956fc5 1327
1328 /* now that we know who our uplink is,
1329 * we can center the routing map and activate auto-routing.
1330 */
9079d26c 1331 //activate_routing(NULL, NULL, NULL);
1332 routing_init();
d76ed9a9 1333 }
1334 sender->self_burst = 0;
1335 recalc_bursts(sender);
1336 for (ii=0; ii<slf_used; ii++)
1337 slf_list[ii](sender);
47956fc5 1338 /* let auto-routing figure out if we were
1339 * wating on this server to link a child to it */
1340 /* DONT call this if uplink is _US_ */
1341 if(sender->uplink != self)
1342 routing_handle_connect(sender->name, sender->uplink->name);
d76ed9a9 1343 return 1;
1344}
1345
1346static CMD_FUNC(cmd_eob_ack)
1347{
1348 extern time_t burst_begin;
1349
1350 if (GetServerH(origin) == self->uplink) {
1351 burst_length = now - burst_begin;
1352 self->self_burst = self->burst = 0;
1353 }
1354 cManager.uplink->state = CONNECTED;
1355 return 1;
1356}
1357
956ee72d 1358static CMD_FUNC(cmd_rping)
1359{
1360 struct server *dest;
1361
1362 if (!(dest = GetServerN(argv[1])))
74ac2349 1363 return 1; /* if its a jupe or something, return 1 so its silently ignored */
956ee72d 1364
1365 if (dest == self)
1366 irc_rpong(argv[2], argv[3], argv[4], argv[5]);
1367
1368 return 1;
1369}
1370
d76ed9a9 1371static CMD_FUNC(cmd_ping)
1372{
1373 struct server *srv;
1374 struct userNode *un;
1375
2f61d1d7 1376 if (argc > 3)
1377 irc_pong_asll(argv[2], argv[3]);
d76ed9a9 1378 else if ((srv = GetServerH(origin)))
1379 irc_pong(self->name, srv->numeric);
1380 else if ((un = GetUserH(origin)))
1381 irc_pong(self->name, un->numeric);
1382 else
1383 irc_pong(self->name, origin);
1384
1385 timeq_del(0, timed_send_ping, 0, TIMEQ_IGNORE_WHEN|TIMEQ_IGNORE_DATA);
1386 timeq_del(0, timed_ping_timeout, 0, TIMEQ_IGNORE_WHEN|TIMEQ_IGNORE_DATA);
1387 timeq_add(now + ping_freq, timed_send_ping, 0);
1388 received_ping();
1389 return 1;
1390}
1391
1392static CMD_FUNC(cmd_error_nick)
1393{
1394 /* Go back to original IRC length .. and try to reconnect :/ */
1395 change_nicklen(9);
1396 irc_squit(self, "Got erroneous nickname, truncating nicks.", NULL);
1397 return 1;
1398}
1399
1400struct create_desc {
1401 struct userNode *user;
1402 time_t when;
1403};
1404
1405static void
1406join_helper(struct chanNode *chan, void *data)
1407{
1408 struct create_desc *cd = data;
1409 AddChannelUser(cd->user, chan);
1410}
1411
1412static void
1413create_helper(char *name, void *data)
1414{
1415 struct create_desc *cd = data;
1416
1417 if (!strcmp(name, "0")) {
1418 while (cd->user->channels.used > 0)
1419 DelChannelUser(cd->user, cd->user->channels.list[0]->channel, 0, 0);
1420 return;
1421 }
1422
2aef5f4b 1423 AddChannelUser(cd->user, AddChannel(name, cd->when, NULL, NULL, NULL));
d76ed9a9 1424}
1425
1426static CMD_FUNC(cmd_create)
1427{
1428 struct create_desc cd;
1429 struct userNode *user;
1430
1431 if ((argc < 3) || !(user = GetUserH(origin)))
1432 return 0;
1433 cd.user = user;
1434 cd.when = atoi(argv[2]);
1435 parse_foreach(argv[1], join_helper, create_helper, NULL, NULL, &cd);
1436 return 1;
1437}
1438
1439static CMD_FUNC(cmd_join)
1440{
1441 struct create_desc cd;
1442
1443 if (!(cd.user = GetUserH(origin)))
1444 return 0;
1445 if (argc < 2)
1446 return 0;
1447 else if (argc < 3)
1448 cd.when = now;
1449 else
1450 cd.when = atoi(argv[2]);
1451 parse_foreach(argv[1], join_helper, create_helper, NULL, NULL, &cd);
1452 return 1;
1453}
1454
cd25f2e9 1455static CMD_FUNC(cmd_svsjoin)
1456{
1457 struct create_desc cd;
1458
1459 if (!(cd.user = GetUserH(argv[1])))
1460 return 0;
1461 if (argc < 3)
1462 return 0;
1463 else if (argc < 4)
1464 cd.when = now;
1465 else
1466 cd.when = atoi(argv[3]);
1467 parse_foreach(argv[2], join_helper, create_helper, NULL, NULL, &cd);
1468 return 1;
1469}
1470
d76ed9a9 1471static CMD_FUNC(cmd_pong)
1472{
1473 if (argc < 3)
1474 return 0;
1475 if (!strcmp(argv[2], self->name)) {
1476 timeq_del(0, timed_send_ping, 0, TIMEQ_IGNORE_WHEN|TIMEQ_IGNORE_DATA);
1477 timeq_del(0, timed_ping_timeout, 0, TIMEQ_IGNORE_WHEN|TIMEQ_IGNORE_DATA);
1478 timeq_add(now + ping_freq, timed_send_ping, 0);
1479 received_ping();
1480 }
1481 return 1;
1482}
1483
1484static CMD_FUNC(cmd_nick)
1485{
1486 struct userNode *user;
1487 if ((user = GetUserH(origin))) {
1488 /* nick change (since the source is a user numeric) */
1489 if (argc < 2)
1490 return 0;
1491 NickChange(user, argv[1], 1);
1492 } else {
1493 struct server *serv;
08895577 1494 struct userNode *nuser;
d76ed9a9 1495 char modes[MAXLEN];
1496 /* new nick */
1497 if (argc < 9)
1498 return 0;
1499 serv = GetServerH(origin);
1500 if (argc > 9)
1501 unsplit_string(argv+6, argc-9, modes);
1502 else
1503 strcpy(modes, "+");
08895577 1504 nuser = AddUser(serv, argv[1], argv[4], argv[5], modes, argv[argc-2], argv[argc-1], atoi(argv[3]), argv[argc-3]);
d76ed9a9 1505 }
1506 return 1;
1507}
1508
1509static CMD_FUNC(cmd_account)
1510{
1511 struct userNode *user;
d9cd0e9d 1512 struct server *server;
b21e2cfe 1513 struct handle_info *hi;
d76ed9a9 1514
d9cd0e9d 1515 if ((argc < 3) || !origin || !(server = GetServerH(origin)))
d76ed9a9 1516 return 0; /* Origin must be server. */
d9cd0e9d 1517
1518 /* This next line appears to tremple origin.. why? */
d76ed9a9 1519 user = GetUserN(argv[1]);
1520 if (!user)
1521 return 1; /* A QUIT probably passed the ACCOUNT. */
805e7c7a 1522
1523 if(!extended_accounts) /* any need for this function without? */
1524 return 1;
d9cd0e9d 1525
1526 if(!strcmp(argv[2],"C"))
1527 {
7dd05763 1528 if((hi = loc_auth(argv[4], argv[5], NULL)))
1529 {
1530 /* Return a AC A */
a837dcf5 1531 putsock("%s " P10_ACCOUNT " %s A %s "FMT_TIME_T, self->numeric, server->numeric , argv[3], hi->registered);
7dd05763 1532
50220e57 1533 log_module(MAIN_LOG, LOG_DEBUG, "loc_auth: %s\n", user->nick);
50220e57 1534 }
7dd05763 1535 else
1536 {
1537 /* Return a AC D */
1538 putsock("%s " P10_ACCOUNT " %s D %s", self->numeric, server->numeric , argv[3]);
1539 }
1540 return 1;
1541 }
1542 else if(!strcmp(argv[2],"H")) /* New enhanced (host) version of C */
1543 {
1544 if((hi = loc_auth(argv[5], argv[6], argv[4] )))
d9cd0e9d 1545 {
1546 /* Return a AC A */
a837dcf5 1547 putsock("%s " P10_ACCOUNT " %s A %s "FMT_TIME_T, self->numeric, server->numeric , argv[3], hi->registered);
d9cd0e9d 1548 }
1549 else
1550 {
1551 /* Return a AC D */
1552 putsock("%s " P10_ACCOUNT " %s D %s", self->numeric, server->numeric , argv[3]);
1553 }
1554 return 1;
1555 }
1556 else if(!strcmp(argv[2],"R"))
1557 call_account_func(user, argv[3]);
1558 else
1559 call_account_func(user, argv[2]); /* For backward compatability */
d76ed9a9 1560 return 1;
1561}
1562
1563static CMD_FUNC(cmd_fakehost)
1564{
1565 struct userNode *user;
1566
1567 if ((argc < 3) || !origin || !GetServerH(origin))
1568 return 0;
1569 if (!(user = GetUserN(argv[1])))
1570 return 1;
1571 assign_fakehost(user, argv[2], 0);
1572 return 1;
1573}
1574
89d871d8 1575static struct {
1576 char *name;
1577 unsigned int priv;
1578} privtab[] = {
1579#define P(priv) { #priv, PRIV_ ## priv }
1580 P(CHAN_LIMIT), P(MODE_LCHAN), P(WALK_LCHAN), P(DEOP_LCHAN),
1581 P(SHOW_INVIS), P(SHOW_ALL_INVIS), P(UNLIMIT_QUERY), P(KILL),
1582 P(LOCAL_KILL), P(REHASH), P(RESTART), P(DIE),
1583 P(GLINE), P(LOCAL_GLINE), P(JUPE), P(LOCAL_JUPE),
1584 P(OPMODE), P(LOCAL_OPMODE), P(SET), P(WHOX),
1585 P(BADCHAN), P(LOCAL_BADCHAN), P(SEE_CHAN), P(PROPAGATE),
1586 P(DISPLAY), P(SEE_OPERS), P(WIDE_GLINE), P(FORCE_OPMODE),
1587 P(FORCE_LOCAL_OPMODE), P(REMOTEREHASH), P(CHECK), P(SEE_SECRET_CHAN),
625ccd4e 1588 P(SHUN), P(LOCAL_SHUN), P(WIDE_SHUN), P(ZLINE),
055f3f30 1589 P(LOCAL_ZLINE), P(WIDE_ZLINE), P(LIST_CHAN), P(WHOIS_NOTICE),
1590 P(HIDE_IDLE), P(XTRAOP), P(HIDE_CHANNELS),
89d871d8 1591#undef P
1592 { 0, 0 }
1593};
1594
1595char *client_report_privs(struct userNode *client)
1596{
1597 int i;
1598
1599 privbuf[0] = '\0';
1600 for (i = 0; privtab[i].name; i++) {
1601 if (HasPriv(client, privtab[i].priv)) {
1602 strcat(privbuf, privtab[i].name);
1603 strcat(privbuf, " ");
1604 }
1605 }
1606
1607 privbuf[strlen(privbuf)] = 0;
1608
1609 return privbuf;
1610}
1611
9c88eebe 1612int clear_privs(struct userNode *who) {
1613 int i = 0;
1614 assert(0 != who);
1615
1616 for (i = 0; privtab[i].name; i++)
1617 RevokePriv(who, privtab[i].priv);
1618
1619 return 0;
1620}
1621
89d871d8 1622int client_modify_priv_by_name(struct userNode *who, char *priv, int what) {
1623 int i = 0;
1624 assert(0 != priv);
1625 assert(0 != who);
1626
1627 for (i = 0; privtab[i].name; i++) {
1628 if (0 == strcmp(privtab[i].name, priv)) {
1629 if (what == PRIV_ADD)
1630 GrantPriv(who, privtab[i].priv);
1631 else if (what == PRIV_DEL) {
1632 RevokePriv(who, privtab[i].priv);
1633 }
1634 }
1635 }
1636 return 0;
1637}
1638
6c34bb5a 1639int check_priv(char *priv)
1640{
1641 int i;
1642
1643 for (i = 0; privtab[i].name; i++) {
1644 if (0 == strcmp(privtab[i].name, priv)) {
1645 return 1;
1646 }
1647 }
1648 return 0;
1649}
1650
1651void
1652irc_privs(struct userNode *target, char *flag, int add)
1653{
1654 client_modify_priv_by_name(target, flag, add);
1655 putsock("%s " P10_PRIVS " %s %s%s", self->numeric, target->numeric, (add == PRIV_ADD) ? "+" : "-", flag);
1656}
1657
89d871d8 1658static CMD_FUNC(cmd_privs)
1659{
9c88eebe 1660 char *tstr = NULL;
1661 char buf[512] = "";
1662 char *p = 0;
1663 char *tmp;
89d871d8 1664
9c88eebe 1665 int what = PRIV_ADD;
1666 int type = 0;
89d871d8 1667
9c88eebe 1668 unsigned int i;
89d871d8 1669
9c88eebe 1670 struct server *sender;
1671 struct userNode *user;
89d871d8 1672
89d871d8 1673
9c88eebe 1674 tstr = conf_get_data("server/type", RECDB_QSTRING);
1675 if(tstr)
1676 type = atoi(tstr);
89d871d8 1677
89d871d8 1678
9c88eebe 1679 if (!(sender = GetServerH(origin))) { /* from oper */
1680 return 1; /* ignore as no services have privs set */
1681 } else { /* from server */
1682 if (type < 5)
1683 return 1; /* silently ignore */
1684
1685 user = argc > 1 ? GetUserN(argv[1]) : NULL;
1686
1687 if (!user)
1688 return 0;
1689
1690 for (i=1; i<argc; i++) {
1691 strcat(buf, argv[i]);
1692 strcat(buf, " ");
1693 }
1694
1695 for (i = 2; i < argc; i++) {
1696 if (*argv[i] == '+') { what = PRIV_ADD; argv[i]++; }
1697 if (*argv[i] == '-') { what = PRIV_DEL; argv[i]++; }
1698 for (tmp = x3_strtok(&p, argv[i], ","); tmp;
1699 tmp = x3_strtok(&p, NULL, ",")) {
1700 if (!strcmp(tmp, "PRIV_NONE")) {
1701 clear_privs(user);
1702 break;
1703 } else
1704 client_modify_priv_by_name(user, tmp, what);
1705 }
1706 }
89d871d8 1707 }
9c88eebe 1708 return 1;
89d871d8 1709}
1710
d76ed9a9 1711static CMD_FUNC(cmd_burst)
1712{
1713 extern int rel_age;
2aef5f4b 1714 char modes[MAXLEN], *members = "";
1715 static char exemptlist[MAXLEN], banlist[MAXLEN];
a32da4c7 1716 unsigned int next = 3, res = 1;
2aef5f4b 1717 int ctype = 0, echeck = 0, bcheck = 0;
fa86a9e7 1718 struct chanData *cData;
d76ed9a9 1719 struct chanNode *cNode;
1720 struct userNode *un;
1721 struct modeNode *mNode;
1722 long mode;
2f61d1d7 1723 int oplevel = -1;
d76ed9a9 1724 char *user, *end, sep;
1725 time_t in_timestamp;
2aef5f4b 1726 char* parm = NULL;
d76ed9a9 1727
1728 if (argc < 3)
1729 return 0;
1730 modes[0] = 0;
2aef5f4b 1731
1732 exemptlist[0] = 0;
1733 banlist[0] = 0;
1734
d76ed9a9 1735 while (next < argc) {
1736 switch (argv[next][0]) {
1737 case '+': {
1738 const char *pos;
1739 int n_modes;
a32da4c7 1740 for (pos=argv[next], n_modes = 1; *pos; pos++)
2f61d1d7 1741 if ((*pos == 'k') || (*pos == 'l') || (*pos == 'A')
1742 || (*pos == 'U'))
d76ed9a9 1743 n_modes++;
1744 unsplit_string(argv+next, n_modes, modes);
1745 next += n_modes;
1746 break;
1747 }
2aef5f4b 1748 case '%': {
1749 for(parm = mysep(&argv[next], " "); /* parm = first param */
1750 parm; /* While param is not null */
1751 parm = mysep(&argv[next], " ") /* parm = next param */
1752 )
1753 {
1754 switch (parm[0]) {
1755 case '%': {
1756 ctype = 1;
1757 break;
1758 }
1759 case '~': {
1760 ctype = 2;
1761 break;
1762 }
1763 default: {
1764 break;
1765 }
1766 }
1767 if (ctype == 1) {
1768 if (bcheck == 0) {
1769 /* strip % char off start of very first ban */
23475fc6 1770 if (strlen(parm) > 1) {
1771 strncat(banlist, strtok(parm, "%"), sizeof(banlist) - 1 - strlen(banlist));
1772 strncat(banlist, " ", sizeof(banlist) - 1 - strlen(banlist));
1773 }
2aef5f4b 1774 bcheck = 1;
1775 } else {
1776 strncat(banlist, parm, sizeof(banlist) - 1 - strlen(banlist));
1777 strncat(banlist, " ", sizeof(banlist) - 1 - strlen(banlist));
1778 }
1779 } else if (ctype == 2) {
1780 if (echeck == 0) {
1781 echeck = 1;
1782 } else {
1783 strncat(exemptlist, parm, sizeof(exemptlist) - 1 - strlen(exemptlist));
1784 strncat(exemptlist, " ", sizeof(exemptlist) - 1 - strlen(exemptlist));
1785 }
1786 }
1787 }
1788 next++;
1789 break;
1790 }
a32da4c7 1791 default: members = argv[next++]; break;
2aef5f4b 1792 }
d76ed9a9 1793 }
1794
1795 in_timestamp = atoi(argv[2]);
1796 if ((cNode = dict_find(unbursted_channels, argv[1], NULL))) {
1797 cNode->timestamp = in_timestamp;
1798 dict_remove(unbursted_channels, cNode->name);
1799 irc_burst(cNode);
1800 }
2aef5f4b 1801 cNode = AddChannel(argv[1], in_timestamp, modes, banlist, exemptlist);
fa86a9e7 1802 cData = cNode->channel_info;
1803
1804 if (!cData) {
1805 if (cNode->modes & MODE_REGISTERED) {
1806 irc_join(opserv, cNode);
1807 irc_mode(opserv, cNode, "-z");
1808 irc_part(opserv, cNode, "");
1809 }
1810 }
2aef5f4b 1811
a32da4c7 1812 /* Burst channel members in now. */
d76ed9a9 1813 for (user = members, sep = *members, mode = 0; sep; user = end) {
1814 for (end = user + 3; isalnum(*end) || *end == '[' || *end == ']'; end++) ;
1815 sep = *end++; end[-1] = 0;
1816 if (sep == ':') {
1817 mode = 0;
1818 while ((sep = *end++)) {
2f61d1d7 1819 if (sep == 'o') {
d76ed9a9 1820 mode |= MODE_CHANOP;
2f61d1d7 1821 oplevel = -1;
1822 } else if (sep == 'h') {
55342ce8 1823 mode |= MODE_HALFOP;
2f61d1d7 1824 oplevel = -1;
1825 } else if (sep == 'v') {
d76ed9a9 1826 mode |= MODE_VOICE;
2f61d1d7 1827 oplevel = -1;
1828 } else if (isdigit(sep)) {
1117fc5a 1829 mode |= MODE_CHANOP;
2f61d1d7 1830 if (oplevel >= 0)
1831 oplevel += parse_oplevel(end);
1832 else
1833 oplevel = parse_oplevel(end);
1117fc5a 1834 while (isdigit(*end)) end++;
1835 } else
d76ed9a9 1836 break;
1837 }
1838 if (rel_age < 0)
1839 mode = 0;
1840 }
1841 if (!(un = GetUserN(user))) {
1842 res = 0;
1843 continue;
1844 }
2f61d1d7 1845 if ((mNode = AddChannelUser(un, cNode))) {
d76ed9a9 1846 mNode->modes = mode;
2f61d1d7 1847 mNode->oplevel = oplevel;
1848 }
d76ed9a9 1849 }
1850
1851 return res;
1852}
1853
d82cf2f0 1854/* TODO:
1855 * This is a stub that doesn't actually do anything. It should be completed
1856 * so that bans on *!*@markname.* match users as it does in nefarious
1857 */
1858static CMD_FUNC(cmd_mark)
1859{
1860 struct userNode *target;
1861 /*
1862 * log_module(MAIN_LOG, LOG_ERROR, "DEBUG: mark, user %s, type %s, arg %s", argv[1], argv[2], argv[3]);
1863 */
1864
1865 if(argc < 4)
1866 return 0;
d82cf2f0 1867 if(!strcasecmp(argv[2], "DNSBL")) {
1868 /* DNSBL <modes> */
1869 return 1;
1870 }
1871 else if(!strcasecmp(argv[2], "DNSBL_DATA")) {
1872 /* DNSBL_DATA name */
bc06f607 1873 target = GetUserH(argv[1]);
1874 if(!target) {
6d58d26f 1875 log_module(MAIN_LOG, LOG_ERROR, "Unable to find user %s whose dnsbl mark is changing.", argv[1]);
bc06f607 1876 return 0;
1877 }
2c00fbc2 1878 target->mark = strdup(argv[3]);
d82cf2f0 1879 return 1;
1880
1881 }
6d58d26f 1882 else if(!strcasecmp(argv[2], "CVERSION")) {
1883 /* DNSBL_DATA name */
1884 target = GetUserH(argv[1]);
1885 if(!target) {
1886 log_module(MAIN_LOG, LOG_ERROR, "Unable to find user %s whose version mark is changing.", argv[1]);
1887 return 0;
1888 }
1889
fa3c07bb 1890 char *version = unsplit_string(argv + 3, argc - 3, NULL);
1891 if(!version)
1892 version = "";
1893
1894 target->version_reply = strdup(version);
1895
1896 if(match_ircglob(version, "WebTV;*"))
1897 target->no_notice = true; /* webbies cant see notices */
1898
6d58d26f 1899 return 1;
1900 }
d82cf2f0 1901 /* unknown type of mark */
1902 return 1;
1903}
1904
d76ed9a9 1905static CMD_FUNC(cmd_mode)
1906{
1907 struct chanNode *cn;
1908 struct userNode *un;
182dd032 1909 char *sethost; // sethost - reed/apples
1910 int i; // sethost - reed/apples
d76ed9a9 1911
1912 if (argc < 3)
1913 return 0;
1914 if (!IsChannelName(argv[1])) {
1915 un = GetUserH(argv[1]);
1916 if (!un) {
1917 log_module(MAIN_LOG, LOG_ERROR, "Unable to find user %s whose mode is changing.", argv[1]);
1918 return 0;
1919 }
182dd032 1920 // sethost - reed/apples
1921 if (argc == 3)
d76ed9a9 1922 mod_usermode(un, argv[2]);
182dd032 1923 else {
1924 sethost = malloc(strlen(argv[2]) + 1 + strlen(argv[3]) + 1);
1925 i = 0;
1926 while((sethost[i++] = *argv[2]++));
1927 i--;
1928 sethost[i++] = ' ';
1929 while((sethost[i++] = *argv[3]++));
1930 mod_usermode(un, sethost); // sethost - reed/apples
1931 }
1932
d76ed9a9 1933 return 1;
1934 }
1935
1936 if (!(cn = GetChannel(argv[1]))) {
1937 log_module(MAIN_LOG, LOG_ERROR, "Unable to find channel %s whose mode is changing.", argv[1]);
1938 return 0;
1939 }
1940 if ((un = GetUserH(origin))) {
1941 struct modeNode *mn;
1942 /* Update idle time for person setting the mode */
1943 if ((mn = GetUserMode(cn, un)))
1944 mn->idle_since = now;
1945 } else {
1946 /* If it came from a server, reset timestamp to re-sync. */
1947 cn->timestamp = atoi(argv[argc-1]);
1948 }
1949
08895577 1950 if (checkDefCon(DEFCON_NO_MODE_CHANGE) && !IsOper(un)) {
1951 const char *str;
1952 str = conf_get_data("services/opserv/nick", RECDB_QSTRING);
1953 if (str) {
1954 char modes[MODELEN];
1955 struct userNode *opserv = GetUserH(str);
1956
1957 send_message_type(4, un, opserv, "Channel modes cannot be changed due to DefCon level %d in effect, please try again soon", DefConLevel);
1958 irc_make_chanmode(cn, modes);
1959 irc_mode(opserv, cn, modes);
1960 irc_mode(opserv, cn, DefConChanModes);
1961 }
1962 return 1;
1963 }
1964
1965
d76ed9a9 1966 return mod_chanmode(un, cn, argv+2, argc-2, MCP_ALLOW_OVB|MCP_FROM_SERVER|(un ? MC_NOTIFY : 0));
1967}
1968
1969static CMD_FUNC(cmd_opmode)
1970{
1971 struct chanNode *cn;
1972 struct userNode *un;
1973
1974 if (argc < 3)
1975 return 0;
1976
1977 if (!(cn = GetChannel(argv[1]))) {
1978 log_module(MAIN_LOG, LOG_ERROR, "Unable to find channel %s whose mode is changing.", argv[1]);
1979 return 0;
1980 }
1981 if (!(un = GetUserH(origin))) {
1982 log_module(MAIN_LOG, LOG_ERROR, "Unable to find user %s requesting OPMODE.", origin);
1983 return 0;
1984 }
1985 if (!IsOper(un)) {
1986 log_module(MAIN_LOG, LOG_ERROR, "Non-privileged user %s using OPMODE.", un->nick);
1987 return 0;
1988 }
1989
1990 return mod_chanmode(un, cn, argv+2, argc-2, MCP_ALLOW_OVB|MCP_FROM_SERVER); /* do NOT announce opmode locally */
1991}
1992
1993static int clear_chanmode(struct chanNode *channel, const char *modes);
1994
1995static CMD_FUNC(cmd_clearmode)
1996{
1997 struct chanNode *cn;
1998 struct userNode *un;
1999
2000 if (argc < 3)
2001 return 0;
2002
2003 if (!(cn = GetChannel(argv[1]))) {
2004 log_module(MAIN_LOG, LOG_ERROR, "Unable to find channel %s whose mode is changing.", argv[1]);
2005 return 0;
2006 }
2007 if (!(un = GetUserH(origin))) {
2008 log_module(MAIN_LOG, LOG_ERROR, "Unable to find user %s requesting CLEARMODE.", origin);
2009 return 0;
2010 }
2011 if (!IsOper(un)) {
2012 log_module(MAIN_LOG, LOG_ERROR, "Non-privileged user %s using CLEARMODE.", un->nick);
2013 return 0;
2014 }
2015
2016 return clear_chanmode(cn, argv[2]);
2017}
2018
2019static CMD_FUNC(cmd_topic)
2020{
2021 struct chanNode *cn;
2022 time_t chan_ts, topic_ts;
7fda2b52 2023 struct userNode *user;
d76ed9a9 2024
2025 if (argc < 3)
2026 return 0;
2027 if (!(cn = GetChannel(argv[1]))) {
2028 log_module(MAIN_LOG, LOG_ERROR, "Unable to find channel %s whose topic is being set", argv[1]);
2029 return 0;
2030 }
7fda2b52 2031
2032
2033 if (argc == 5) { /* Asuka / Topic Bursting IRCu's */
2034 user = GetUserH(origin);
d76ed9a9 2035 chan_ts = atoi(argv[2]);
2036 topic_ts = atoi(argv[3]);
7fda2b52 2037 } else if (argc >= 6) { /* Nefarious 0.5.0 */
2038 user = GetUserH(strtok(argv[2], "!"));
2039 chan_ts = atoi(argv[3]);
2040 topic_ts = atoi(argv[4]);
2041 } else { /* Regular IRCu (No Topic Bursting)*/
2042 user = GetUserH(origin);
d76ed9a9 2043 chan_ts = cn->timestamp;
2044 topic_ts = now;
2045 }
7fda2b52 2046
2047 SetChannelTopic(cn, user, user, argv[argc-1], 0);
d76ed9a9 2048 cn->topic_time = topic_ts;
2049 return 1;
2050}
2051
2052static CMD_FUNC(cmd_num_topic)
2053{
2054 struct chanNode *cn;
2055
2056 if (!argv[0])
2057 return 0; /* huh? */
2058 if (argv[2]) {
2059 cn = GetChannel(argv[2]);
2060 if (!cn) {
2061 log_module(MAIN_LOG, LOG_ERROR, "Unable to find channel %s in topic reply", argv[2]);
2062 return 0;
2063 }
2064 } else
2065 return 0;
2066
2067 switch (atoi(argv[0])) {
2068 case 331:
2069 cn->topic_time = 0;
2070 break; /* no topic */
2071 case 332:
2072 if (argc < 4)
2073 return 0;
2074 safestrncpy(cn->topic, unsplit_string(argv+3, argc-3, NULL), sizeof(cn->topic));
2075 break;
2076 case 333:
2077 if (argc < 5)
2078 return 0;
2079 safestrncpy(cn->topic_nick, argv[3], sizeof(cn->topic_nick));
2080 cn->topic_time = atoi(argv[4]);
2081 break;
2082 default:
2083 return 0; /* should never happen */
2084 }
2085 return 1;
2086}
2087
2088static CMD_FUNC(cmd_num_gline)
2089{
cad8f3a7 2090 if (argc < 7) {
2091 if (argc < 6)
2092 return 0;
2093 else
2094 gline_add(origin, argv[3], atoi(argv[4])-now, argv[5], now, 0, 0);
2095 } else {
4cb36ef0 2096 if (!strcmp(argv[5], "+"))
cad8f3a7 2097 gline_add(origin, argv[3], atoi(argv[4])-now, argv[6], now, 0, 0);
2098 }
d76ed9a9 2099 return 1;
2100}
2101
d914d1cb 2102static CMD_FUNC(cmd_num_shun)
2103{
cad8f3a7 2104 if (argc < 7) {
2105 if (argc < 6)
2106 return 0;
2107 else
2108 shun_add(origin, argv[3], atoi(argv[4])-now, argv[5], now, 0);
2109 } else {
4cb36ef0 2110 if (!strcmp(argv[5], "+"))
cad8f3a7 2111 shun_add(origin, argv[3], atoi(argv[4])-now, argv[6], now, 0);
2112 }
d914d1cb 2113 return 1;
2114}
2115
d76ed9a9 2116static CMD_FUNC(cmd_quit)
2117{
2118 struct userNode *user;
2119 if (argc < 2)
2120 return 0;
2121 /* Sometimes we get a KILL then a QUIT or the like, so we don't want to
2122 * call DelUser unless we have the user in our grasp. */
2123 if ((user = GetUserH(origin)))
2124 DelUser(user, NULL, false, argv[1]);
2125 return 1;
2126}
2127
2128static CMD_FUNC(cmd_kill)
2129{
2130 struct userNode *user;
2131 if (argc < 2)
2132 return 0;
2133 user = GetUserN(argv[1]);
2134 if (!user) {
2135 /* If we get a KILL for a non-existent user, it could be a
2136 * Ghost response to a KILL we sent out earlier. So we only
2137 * whine if the target is local.
2138 */
2139 if (!strncmp(argv[1], self->numeric, strlen(self->numeric)))
2140 log_module(MAIN_LOG, LOG_ERROR, "Unable to find kill victim %s", argv[1]);
2141 return 0;
2142 }
2143
2144 if (IsLocal(user) && IsService(user)) {
2145 /* TODO: rate limit this so silly things don't happen. */
2146 ReintroduceUser(user);
2147 return 1;
2148 }
2149
2150 DelUser(user, NULL, false, argv[2]);
2151 return 1;
2152}
2153
0f6fe38c 2154static CMD_FUNC(cmd_part)
2155{
2156 struct userNode *user;
2157
2158 if (argc < 2)
2159 return 0;
2160 user = GetUserH(origin);
2161 if (!user)
2162 return 0;
2163 parse_foreach(argv[1], part_helper, NULL, NULL, NULL, user);
2164 return 1;
2165}
2166
cd25f2e9 2167static CMD_FUNC(cmd_svspart)
2168{
2169 struct userNode *user;
2170
2171 if (argc < 3)
2172 return 0;
c9ec8867 2173 user = GetUserN(argv[1]);
cd25f2e9 2174 if (!user)
2175 return 0;
2176 parse_foreach(argv[2], part_helper, NULL, NULL, NULL, user);
2177 return 1;
2178}
2179
a6a4707e 2180static CMD_FUNC(cmd_silence)
2181{
2182 struct userNode *user;
2183 char *mask;
f0b5157b 2184 char *new_mask;
a6a4707e 2185 unsigned int i;
2186
2187 if (argc < 2)
2188 return 0;
2189
2190 user = GetUserN(argv[1]);
2191
2192 /* Sanity, go nuts if this happens */
2193 if (!user)
2194 return 0;
2195
2196 /* We can safely ignore this if a user adding a silence is not
2197 * ignored. However this brings up a TODO. If a user logs in and
2198 * they have silences on the IRCd then we need to set them here
2199 * somehow
2200 */
2201 if (!user->handle_info)
2202 return 1;
2203
b6599cc2 2204 mask = argv[2];
a6a4707e 2205
2206 if (*mask == '-') {
2207 for (i=0; i<user->handle_info->ignores->used; i++) {
2208 if (!irccasecmp(mask+1, user->handle_info->ignores->list[i]))
2209 user->handle_info->ignores->list[i] = user->handle_info->ignores->list[--user->handle_info->ignores->used];
2210 }
2211 } else {
2212 for (i=0; i<user->handle_info->ignores->used; i++) {
2213 if (!strcmp(mask+1, user->handle_info->ignores->list[i]))
2214 return 1; /* Already on the users NickServ ignore list, safely ignore */
2215 }
2216
f0b5157b 2217 new_mask = strdup(mask+1);
b6599cc2 2218 string_list_append(user->handle_info->ignores, new_mask);
a6a4707e 2219 }
a6a4707e 2220 return 1;
2221}
2222
d76ed9a9 2223static CMD_FUNC(cmd_kick)
2224{
2225 if (argc < 3)
2226 return 0;
2227 ChannelUserKicked(GetUserH(origin), GetUserN(argv[2]), GetChannel(argv[1]));
2228 return 1;
2229}
2230
2231static CMD_FUNC(cmd_squit)
2232{
2233 struct server *server;
47956fc5 2234 char *uplink;
d76ed9a9 2235
2236 if (argc < 4)
2237 return 0;
2238 if (!(server = GetServerH(argv[1])))
2239 return 0;
2240
2241 if (server == self->uplink) {
2242 /* Force a reconnect to the currently selected server. */
2243 cManager.uplink->tries = 0;
2244 log_module(MAIN_LOG, LOG_INFO, "Squitting from uplink: %s", argv[3]);
2245 close_socket();
2246 return 1;
2247 }
2248
47956fc5 2249 uplink = strdup(server->uplink->name);
d76ed9a9 2250 DelServer(server, 0, argv[3]);
47956fc5 2251 /* if its a pingout and pingout connecting is enabled
2252 or its a read error and readerror connecting is enabled
2253 or were doing a "N" and i need to connect that server somewhere */
2254 routing_handle_squit(argv[1], uplink, argv[3]);
2255 free(uplink);
d76ed9a9 2256 return 1;
2257}
2258
2259static CMD_FUNC(cmd_privmsg)
2260{
2261 struct privmsg_desc pd;
0f6fe38c 2262 if (argc != 3)
d76ed9a9 2263 return 0;
2264 pd.user = GetUserH(origin);
2265 if (!pd.user || (IsGagged(pd.user) && !IsOper(pd.user)))
2266 return 1;
08895577 2267
2268 if (checkDefCon(DEFCON_OPER_ONLY) && !IsOper(pd.user)) {
2269 const char *str;
2270 str = conf_get_data("services/opserv/nick", RECDB_QSTRING);
2271 if (str)
2272 send_message_type(4, pd.user, GetUserH(str), "Services are currently not available, please try again soon");
2273 return 1;
2274 }
2275
2276 if (checkDefCon(DEFCON_SILENT_OPER_ONLY) && !IsOper(pd.user))
2277 return 1; /* Silently Ignore */
2278
d76ed9a9 2279 pd.is_notice = 0;
0f6fe38c 2280 pd.text = argv[2];
d76ed9a9 2281 parse_foreach(argv[1], privmsg_chan_helper, NULL, privmsg_user_helper, privmsg_invalid, &pd);
c99dcaf6 2282
d76ed9a9 2283 return 1;
2284}
2285
2286static CMD_FUNC(cmd_notice)
2287{
2288 struct privmsg_desc pd;
47956fc5 2289 struct server *srv;
2290 int nuser = 0;
2291
0f6fe38c 2292 if (argc != 3)
d76ed9a9 2293 return 0;
47956fc5 2294
d76ed9a9 2295 pd.user = GetUserH(origin);
47956fc5 2296 if(!pd.user)
2297 nuser = 1;
2298 if (!pd.user || (IsGagged(pd.user) && !IsOper(pd.user))) {
2299 }
2300 else {
2301 pd.is_notice = 1;
0f6fe38c 2302 pd.text = argv[2];
47956fc5 2303 parse_foreach(argv[1], privmsg_chan_helper, NULL, privmsg_user_helper, privmsg_invalid, &pd);
2304 }
2305
2306 srv = GetServerH(origin);
2307 if(srv) {
2308 char *sargv[MAXNUMPARAMS];
2309 int sargc;
2310
2311 sargc = split_line(argv[2], true, MAXNUMPARAMS, sargv);
2312
2313 if(!strcasecmp(sargv[0], "Connect:")) {
2314 /* :Connect: Host shoe.loxxin.net not listed in ircd.conf */
2315 if(!strcasecmp(sargv[3], "not") && !strcasecmp(sargv[4], "listed")) {
2316 routing_handle_connect_failure(srv, sargv[2], unsplit_string(sargv+3, sargc-3, NULL));
2317 }
2318 }
2319 else if(!strcasecmp(sargv[0], "Link")) {
2320 /* :Link with mephisto.etheria.cx cancelled: Server mephisto.etheria.cx[216.46.33.71]
2321 *
2322 * :Link with laptop.afternet.org cancelled: Connection refused
2323 */
2324 if(!strcasecmp(sargv[3], "cancelled:")) {
2325 routing_handle_connect_failure(srv, sargv[2], unsplit_string(sargv+4, sargc-4, NULL));
2326 }
2327 }
2328 }
d76ed9a9 2329 return 1;
2330}
2331
2332static CMD_FUNC(cmd_away)
2333{
2334 struct userNode *uNode;
2335
2336 uNode = GetUserH(origin);
2337 if (!uNode)
2338 return 1;
2339 if (argc < 2)
2340 uNode->modes &= ~FLAGS_AWAY;
2341 else
2342 uNode->modes |= FLAGS_AWAY;
2343 return 1;
2344}
2345
2346static CMD_FUNC(cmd_gline)
2347{
2348 if (argc < 3)
2349 return 0;
2350 if (argv[2][0] == '+') {
2351 if (argc < 5)
2352 return 0;
0f6fe38c 2353 gline_add(origin, argv[2]+1, strtoul(argv[3], NULL, 0), argv[argc-1], now, 0, 0);
d76ed9a9 2354 return 1;
2355 } else if (argv[2][0] == '-') {
2356 gline_remove(argv[2]+1, 0);
2357 return 1;
2358 } else
2359 return 0;
2360}
2361
59b38cc0 2362static CMD_FUNC(cmd_sgline)
2363{
2364 struct server *sender;
2365
2366 if (argc < 4)
2367 return 0;
2368
2369 if (!(sender = GetServerH(origin)))
2370 return 0;
2371
56b83e80 2372 gline_add(origin, argv[1], strtoul(argv[2], NULL, 0), argv[argc-1], now, 1, 0);
59b38cc0 2373 return 1;
2374}
2375
2376static CMD_FUNC(cmd_sshun)
2377{
2378 struct server *sender;
2379
2380 if (argc < 4)
2381 return 0;
2382
2383 if (!(sender = GetServerH(origin)))
2384 return 0;
2385
56b83e80 2386 shun_add(origin, argv[1], strtoul(argv[2], NULL, 0), argv[argc-1], now, 1);
59b38cc0 2387 return 1;
2388}
2389
d914d1cb 2390static CMD_FUNC(cmd_shun)
2391{
2392 if (argc < 3)
2393 return 0;
2394 if (argv[2][0] == '+') {
2395 if (argc < 5)
2396 return 0;
0f6fe38c 2397 shun_add(origin, argv[2]+1, strtoul(argv[3], NULL, 0), argv[argc-1], now, 0);
d914d1cb 2398 return 1;
2399 } else if (argv[2][0] == '-') {
2400 shun_remove(argv[2]+1, 0);
2401 return 1;
2402 } else
2403 return 0;
2404}
2405
d76ed9a9 2406static CMD_FUNC(cmd_svsnick)
2407{
2408 struct userNode *target, *dest;
2409 if ((argc < 4)
2410 || !(target = GetUserN(argv[1]))
2411 || !IsLocal(target)
2412 || (dest = GetUserH(argv[2])))
2413 return 0;
2414 NickChange(target, argv[2], 0);
2415 return 1;
2416}
2417
2418static oper_func_t *of_list;
2419static unsigned int of_size = 0, of_used = 0;
2420
2421void
2422free_user(struct userNode *user)
2423{
2424 free(user->nick);
2425 free(user);
2426}
2427
2428static void
2429parse_cleanup(void)
2430{
2431 unsigned int nn;
2432 free(of_list);
2433 free(privmsg_funcs);
2434 free(notice_funcs);
2435 free(mcf_list);
2436 dict_delete(irc_func_dict);
2437 for (nn=0; nn<dead_users.used; nn++)
2438 free_user(dead_users.list[nn]);
2439 userList_clean(&dead_users);
2440}
2441
2442static void
2443p10_conf_reload(void) {
2444 hidden_host_suffix = conf_get_data("server/hidden_host", RECDB_QSTRING);
2445}
2446
2447static void
2448remove_unbursted_channel(struct chanNode *cNode) {
2449 if (unbursted_channels)
2450 dict_remove(unbursted_channels, cNode->name);
2451}
2452
2453void
2454init_parse(void)
2455{
2456 const char *str, *desc;
2457 int numnick, usermask, max_users;
2458 char numer[COMBO_NUMERIC_LEN+1];
2459
2460 /* read config items */
805e7c7a 2461 str = conf_get_data("server/extended_accounts", RECDB_QSTRING);
2462 extended_accounts = str ? enabled_string(str) : 1;
d76ed9a9 2463 str = conf_get_data("server/ping_freq", RECDB_QSTRING);
2464 ping_freq = str ? ParseInterval(str) : 120;
2465 str = conf_get_data("server/ping_timeout", RECDB_QSTRING);
2466 ping_timeout = str ? ParseInterval(str) : 30;
2467 str = conf_get_data("server/force_n2k", RECDB_QSTRING);
2468 force_n2k = str ? enabled_string(str) : 1;
2469 str = conf_get_data("server/numeric", RECDB_QSTRING);
2470 if (!str) {
2471 log_module(MAIN_LOG, LOG_ERROR, "No server/numeric entry in config file.");
2472 exit(1);
2473 }
2474 numnick = atoi(str);
2475 str = conf_get_data("server/max_users", RECDB_QSTRING);
2476 max_users = str ? atoi(str) : 4096;
2477 for (usermask = 4; usermask < max_users; usermask <<= 1) ;
2478 usermask--;
2479 if ((numnick < 64) && (usermask < 4096) && !force_n2k)
2480 inttobase64(numer, (numnick << 12) + (usermask & 0x00fff), 3);
2481 else
2482 inttobase64(numer, (numnick << 18) + (usermask & 0x3ffff), 5);
2483
2484 str = conf_get_data("server/his_servername", RECDB_QSTRING);
2485 his_servername = str ? strdup(str) : NULL;
2486 str = conf_get_data("server/his_servercomment", RECDB_QSTRING);
2487 his_servercomment = str ? strdup(str) : NULL;
2488
2489 str = conf_get_data("server/hostname", RECDB_QSTRING);
2490 desc = conf_get_data("server/description", RECDB_QSTRING);
2491 if (!str || !desc) {
2492 log_module(MAIN_LOG, LOG_ERROR, "No server/hostname entry in config file.");
2493 exit(1);
2494 }
2495 self = AddServer(NULL, str, 0, boot_time, now, numer, desc);
2496 conf_register_reload(p10_conf_reload);
2497
2498 irc_func_dict = dict_new();
2499 dict_insert(irc_func_dict, CMD_BURST, cmd_burst);
2500 dict_insert(irc_func_dict, TOK_BURST, cmd_burst);
2501 dict_insert(irc_func_dict, CMD_CREATE, cmd_create);
2502 dict_insert(irc_func_dict, TOK_CREATE, cmd_create);
2503 dict_insert(irc_func_dict, CMD_EOB, cmd_eob);
2504 dict_insert(irc_func_dict, TOK_EOB, cmd_eob);
2505 dict_insert(irc_func_dict, CMD_EOB_ACK, cmd_eob_ack);
2506 dict_insert(irc_func_dict, TOK_EOB_ACK, cmd_eob_ack);
2507 dict_insert(irc_func_dict, CMD_MODE, cmd_mode);
2508 dict_insert(irc_func_dict, TOK_MODE, cmd_mode);
d82cf2f0 2509 dict_insert(irc_func_dict, CMD_MARK, cmd_mark);
2510 dict_insert(irc_func_dict, TOK_MARK, cmd_mark);
d76ed9a9 2511 dict_insert(irc_func_dict, CMD_NICK, cmd_nick);
2512 dict_insert(irc_func_dict, TOK_NICK, cmd_nick);
2513 dict_insert(irc_func_dict, CMD_ACCOUNT, cmd_account);
2514 dict_insert(irc_func_dict, TOK_ACCOUNT, cmd_account);
2515 dict_insert(irc_func_dict, CMD_FAKEHOST, cmd_fakehost);
2516 dict_insert(irc_func_dict, TOK_FAKEHOST, cmd_fakehost);
2517 dict_insert(irc_func_dict, CMD_PASS, cmd_pass);
2518 dict_insert(irc_func_dict, TOK_PASS, cmd_pass);
2519 dict_insert(irc_func_dict, CMD_PING, cmd_ping);
2520 dict_insert(irc_func_dict, TOK_PING, cmd_ping);
2521 dict_insert(irc_func_dict, CMD_PRIVMSG, cmd_privmsg);
2522 dict_insert(irc_func_dict, TOK_PRIVMSG, cmd_privmsg);
2523 dict_insert(irc_func_dict, CMD_PONG, cmd_pong);
2524 dict_insert(irc_func_dict, TOK_PONG, cmd_pong);
2525 dict_insert(irc_func_dict, CMD_QUIT, cmd_quit);
2526 dict_insert(irc_func_dict, TOK_QUIT, cmd_quit);
2527 dict_insert(irc_func_dict, CMD_SERVER, cmd_server);
2528 dict_insert(irc_func_dict, TOK_SERVER, cmd_server);
2529 dict_insert(irc_func_dict, CMD_JOIN, cmd_join);
2530 dict_insert(irc_func_dict, TOK_JOIN, cmd_join);
2531 dict_insert(irc_func_dict, CMD_PART, cmd_part);
2532 dict_insert(irc_func_dict, TOK_PART, cmd_part);
2533 dict_insert(irc_func_dict, CMD_ERROR, cmd_error);
2534 dict_insert(irc_func_dict, TOK_ERROR, cmd_error);
2535 dict_insert(irc_func_dict, CMD_TOPIC, cmd_topic);
2536 dict_insert(irc_func_dict, TOK_TOPIC, cmd_topic);
2537 dict_insert(irc_func_dict, CMD_AWAY, cmd_away);
2538 dict_insert(irc_func_dict, TOK_AWAY, cmd_away);
a6a4707e 2539 dict_insert(irc_func_dict, CMD_SILENCE, cmd_silence);
2540 dict_insert(irc_func_dict, TOK_SILENCE, cmd_silence);
d76ed9a9 2541 dict_insert(irc_func_dict, CMD_KICK, cmd_kick);
2542 dict_insert(irc_func_dict, TOK_KICK, cmd_kick);
2543 dict_insert(irc_func_dict, CMD_SQUIT, cmd_squit);
2544 dict_insert(irc_func_dict, TOK_SQUIT, cmd_squit);
2545 dict_insert(irc_func_dict, CMD_KILL, cmd_kill);
2546 dict_insert(irc_func_dict, TOK_KILL, cmd_kill);
2547 dict_insert(irc_func_dict, CMD_NOTICE, cmd_notice);
2548 dict_insert(irc_func_dict, TOK_NOTICE, cmd_notice);
2549 dict_insert(irc_func_dict, CMD_STATS, cmd_stats);
2550 dict_insert(irc_func_dict, TOK_STATS, cmd_stats);
cd25f2e9 2551 dict_insert(irc_func_dict, CMD_SVSJOIN, cmd_svsjoin);
2552 dict_insert(irc_func_dict, TOK_SVSJOIN, cmd_svsjoin);
d76ed9a9 2553 dict_insert(irc_func_dict, CMD_SVSNICK, cmd_svsnick);
2554 dict_insert(irc_func_dict, TOK_SVSNICK, cmd_svsnick);
cd25f2e9 2555 dict_insert(irc_func_dict, CMD_SVSPART, cmd_svspart);
2556 dict_insert(irc_func_dict, TOK_SVSPART, cmd_svspart);
56958740 2557 dict_insert(irc_func_dict, CMD_SWHOIS, cmd_dummy);
2558 dict_insert(irc_func_dict, TOK_SWHOIS, cmd_dummy);
d76ed9a9 2559 dict_insert(irc_func_dict, CMD_WHOIS, cmd_whois);
2560 dict_insert(irc_func_dict, TOK_WHOIS, cmd_whois);
2561 dict_insert(irc_func_dict, CMD_GLINE, cmd_gline);
2562 dict_insert(irc_func_dict, TOK_GLINE, cmd_gline);
59b38cc0 2563 dict_insert(irc_func_dict, CMD_SGLINE, cmd_sgline);
2564 dict_insert(irc_func_dict, TOK_SGLINE, cmd_sgline);
d914d1cb 2565 dict_insert(irc_func_dict, CMD_SHUN, cmd_shun);
2566 dict_insert(irc_func_dict, TOK_SHUN, cmd_shun);
59b38cc0 2567 dict_insert(irc_func_dict, CMD_SSHUN, cmd_sshun);
2568 dict_insert(irc_func_dict, TOK_SSHUN, cmd_sshun);
d76ed9a9 2569 dict_insert(irc_func_dict, CMD_OPMODE, cmd_opmode);
2570 dict_insert(irc_func_dict, TOK_OPMODE, cmd_opmode);
2571 dict_insert(irc_func_dict, CMD_CLEARMODE, cmd_clearmode);
2572 dict_insert(irc_func_dict, TOK_CLEARMODE, cmd_clearmode);
2573 dict_insert(irc_func_dict, CMD_VERSION, cmd_version);
2574 dict_insert(irc_func_dict, TOK_VERSION, cmd_version);
2575 dict_insert(irc_func_dict, CMD_ADMIN, cmd_admin);
2576 dict_insert(irc_func_dict, TOK_ADMIN, cmd_admin);
56b83e80 2577 dict_insert(irc_func_dict, CMD_SMO, cmd_dummy);
2578 dict_insert(irc_func_dict, TOK_SMO, cmd_dummy);
1c76f1b4 2579 dict_insert(irc_func_dict, CMD_SNO, cmd_dummy);
2580 dict_insert(irc_func_dict, TOK_SNO, cmd_dummy);
d76ed9a9 2581
956ee72d 2582 dict_insert(irc_func_dict, CMD_RPING, cmd_rping);
2583 dict_insert(irc_func_dict, TOK_RPING, cmd_rping);
8f9e3388 2584 dict_insert(irc_func_dict, CMD_RPONG, cmd_dummy);
2585 dict_insert(irc_func_dict, TOK_RPONG, cmd_dummy);
2586
d76ed9a9 2587 /* In P10, DESTRUCT doesn't do anything except be broadcast to servers.
2588 * Apparently to obliterate channels from any servers that think they
2589 * exist?
2590 */
2591 dict_insert(irc_func_dict, CMD_DESTRUCT, cmd_dummy);
2592 dict_insert(irc_func_dict, TOK_DESTRUCT, cmd_dummy);
2593 /* Ignore invites */
2594 dict_insert(irc_func_dict, CMD_INVITE, cmd_dummy);
2595 dict_insert(irc_func_dict, TOK_INVITE, cmd_dummy);
2596 /* DESYNCH is just informational, so ignore it */
2597 dict_insert(irc_func_dict, CMD_DESYNCH, cmd_dummy);
2598 dict_insert(irc_func_dict, TOK_DESYNCH, cmd_dummy);
2599 /* Ignore channel operator notices. */
2600 dict_insert(irc_func_dict, CMD_WALLCHOPS, cmd_dummy);
2601 dict_insert(irc_func_dict, TOK_WALLCHOPS, cmd_dummy);
2602 dict_insert(irc_func_dict, CMD_WALLVOICES, cmd_dummy);
2603 dict_insert(irc_func_dict, TOK_WALLVOICES, cmd_dummy);
55342ce8 2604 dict_insert(irc_func_dict, CMD_WALLHOPS, cmd_dummy);
2605 dict_insert(irc_func_dict, TOK_WALLHOPS, cmd_dummy);
d76ed9a9 2606 /* Ignore opers being silly. */
2607 dict_insert(irc_func_dict, CMD_WALLOPS, cmd_dummy);
2608 dict_insert(irc_func_dict, TOK_WALLOPS, cmd_dummy);
55342ce8 2609 dict_insert(irc_func_dict, CMD_WALLHOPS, cmd_dummy);
2610 dict_insert(irc_func_dict, TOK_WALLHOPS, cmd_dummy);
41fadebe 2611 dict_insert(irc_func_dict, TOK_WALLUSERS, cmd_dummy);
850bf0cf 2612 /* Ignore dnsbl exemptions */
2613 dict_insert(irc_func_dict, TOK_EXEMPT, cmd_dummy);
89d871d8 2614 dict_insert(irc_func_dict, CMD_PRIVS, cmd_privs);
2615 dict_insert(irc_func_dict, TOK_PRIVS, cmd_privs);
763de701 2616 /* ignore ALIST for now */
2617 dict_insert(irc_func_dict, TOK_ALIST, cmd_dummy);
2618 dict_insert(irc_func_dict, CMD_ALIST, cmd_dummy);
a74c9eac 2619 /* Ignore remote luser */
0c835e0f 2620 dict_insert(irc_func_dict, TOK_LUSERS, cmd_dummy);
d76ed9a9 2621 /* We have reliable clock! Always! Wraaa! */
2622 dict_insert(irc_func_dict, CMD_SETTIME, cmd_dummy);
2623 dict_insert(irc_func_dict, TOK_SETTIME, cmd_dummy);
b34a7505 2624
2625 /* ignore /trace and /motd commands targetted at us */
2626 dict_insert(irc_func_dict, TOK_TRACE, cmd_dummy);
2627 dict_insert(irc_func_dict, TOK_MOTD, cmd_dummy);
74ac2349 2628 dict_insert(irc_func_dict, TOK_UPING, cmd_dummy);
b34a7505 2629
d76ed9a9 2630 /* handle topics */
2631 dict_insert(irc_func_dict, "331", cmd_num_topic);
2632 dict_insert(irc_func_dict, "332", cmd_num_topic);
2633 dict_insert(irc_func_dict, "333", cmd_num_topic);
2634 dict_insert(irc_func_dict, "345", cmd_dummy); /* blah has been invited to blah */
2635 dict_insert(irc_func_dict, "432", cmd_error_nick); /* Erroneus [sic] nickname */
2636 /* ban list resetting */
2637 /* "stats g" responses */
2638 dict_insert(irc_func_dict, "247", cmd_num_gline);
d914d1cb 2639 dict_insert(irc_func_dict, "542", cmd_num_shun);
d76ed9a9 2640 dict_insert(irc_func_dict, "219", cmd_dummy); /* "End of /STATS report" */
2641 /* other numeric responses we might get */
2642 dict_insert(irc_func_dict, "401", cmd_dummy); /* target left network */
2643 dict_insert(irc_func_dict, "403", cmd_dummy); /* no such channel */
2644 dict_insert(irc_func_dict, "404", cmd_dummy); /* cannot send to channel */
0d16e639 2645 dict_insert(irc_func_dict, "439", cmd_dummy); /* target change too fast */
d76ed9a9 2646 dict_insert(irc_func_dict, "441", cmd_dummy); /* target isn't on that channel */
2647 dict_insert(irc_func_dict, "442", cmd_dummy); /* you aren't on that channel */
2648 dict_insert(irc_func_dict, "443", cmd_dummy); /* is already on channel (after invite?) */
2649 dict_insert(irc_func_dict, "461", cmd_dummy); /* Not enough parameters (after TOPIC w/ 0 args) */
2650 dict_insert(irc_func_dict, "467", cmd_dummy); /* Channel key already set */
2651
2652 num_privmsg_funcs = 16;
2653 privmsg_funcs = malloc(sizeof(privmsg_func_t)*num_privmsg_funcs);
2654 memset(privmsg_funcs, 0, sizeof(privmsg_func_t)*num_privmsg_funcs);
2655
2656 num_notice_funcs = 16;
2657 notice_funcs = malloc(sizeof(privmsg_func_t)*num_notice_funcs);
2658 memset(notice_funcs, 0, sizeof(privmsg_func_t)*num_notice_funcs);
2659
2660 userList_init(&dead_users);
2661 reg_del_channel_func(remove_unbursted_channel);
2662 reg_exit_func(parse_cleanup);
da5b7dfc 2663 // reg_notice_func(opserv, check_ctcp);
d76ed9a9 2664}
2665
2666int
2667parse_line(char *line, int recursive)
2668{
2669 char *argv[MAXNUMPARAMS], *origin;
2670 int argc, cmd, res=0;
2671 cmd_func_t *func;
2672
2673 argc = split_line(line, true, MAXNUMPARAMS, argv);
2674 cmd = self->uplink || !argv[0][1] || !argv[0][2];
2675 if (argc > cmd) {
2676 if (cmd) {
2677 if (argv[0][0] == ':') {
2678 origin = argv[0]+1;
2679 } else if (!argv[0][1] || !argv[0][2]) {
2680 struct server *sNode = GetServerN(argv[0]);
2681 origin = sNode ? sNode->name : 0;
2682 } else {
2683 struct userNode *uNode = GetUserN(argv[0]);
2684 origin = uNode ? uNode->nick : 0;
2685 }
2686 } else
2687 origin = 0;
2688 if ((func = dict_find(irc_func_dict, argv[cmd], NULL)))
2689 res = func(origin, argc-cmd, argv+cmd);
2690 }
2691 if (!res) {
2692 log_module(MAIN_LOG, LOG_ERROR, "PARSE ERROR on line: %s", unsplit_string(argv, argc, NULL));
2693 } else if (!recursive) {
2694 unsigned int i;
2695 for (i=0; i<dead_users.used; i++)
2696 free_user(dead_users.list[i]);
2697 dead_users.used = 0;
2698 }
2699 return res;
2700}
2701
2702static void
2703parse_foreach(char *target_list, foreach_chanfunc cf, foreach_nonchan nc, foreach_userfunc uf, foreach_nonuser nu, void *data)
2704{
2705 char *j, old;
2f61d1d7 2706
d76ed9a9 2707 do {
2708 j = target_list;
2709 while (*j != 0 && *j != ',')
2710 j++;
2711 old = *j;
2712 *j = 0;
2f61d1d7 2713
d76ed9a9 2714 if (IsChannelName(target_list)
2715 || (target_list[0] == '0' && target_list[1] == '\0')) {
2716 struct chanNode *chan = GetChannel(target_list);
2f61d1d7 2717
d76ed9a9 2718 if (chan) {
2719 if (cf)
2720 cf(chan, data);
2721 } else {
2722 if (nc)
2723 nc(target_list, data);
2724 }
2725 } else {
2726 struct userNode *user;
2727 struct privmsg_desc *pd = data;
2728
2729 pd->is_qualified = 0;
2730 if (*target_list == '@') {
2731 user = NULL;
2732 } else if (strchr(target_list, '@')) {
2733 struct server *server;
2734
2735 pd->is_qualified = 1;
2736 user = GetUserH(strtok(target_list, "@"));
2737 server = GetServerH(strtok(NULL, "@"));
2738
2739 if (user && (user->uplink != server)) {
2740 /* Don't attempt to index into any arrays
2741 using a user's numeric on another server. */
2742 user = NULL;
2743 }
2744 } else {
2745 user = GetUserN(target_list);
2746 }
2747
2748 if (user) {
2749 if (uf)
2750 uf(user, data);
2751 } else {
2752 if (nu)
2753 nu(target_list, data);
2754 }
2755 }
2756 target_list = j+1;
2757 } while (old == ',');
2758}
2759
2760static int
2761get_local_numeric(void)
2762{
2763 static unsigned int next_numeric = 0;
2764 if (self->clients > self->num_mask)
2765 return -1;
2766 while (self->users[next_numeric])
2767 if (++next_numeric > self->num_mask)
2768 next_numeric = 0;
2769 return next_numeric;
2770}
2771
2772static void
2773make_numeric(struct server *svr, int local_num, char *outbuf)
2774{
2775 int slen, llen;
2776
2777 if (force_n2k || svr->numeric[1]) {
2778 slen = 2;
2779 llen = 3;
2780 } else {
2781 slen = 1;
2782 llen = (local_num < 64*64) ? 2 : 3;
2783 }
2784 strncpy(outbuf, svr->numeric, slen);
2785 inttobase64(outbuf+slen, local_num, llen);
2786 outbuf[slen+llen] = 0;
2787}
2788
2789struct server *
2790AddServer(struct server *uplink, const char *name, int hops, time_t boot, time_t link, const char *numeric, const char *description)
2791{
2792 struct server* sNode;
2793 int slen, mlen;
2794
2795 if ((sNode = GetServerN(numeric))) {
2796 /* This means we're trying to re-add an existant server.
2797 * To be safe, we should forget the previous incarnation.
2798 * (And all its linked servers.)
2799 *
2800 * It usually only happens in replays when the original
2801 * had a ping timeout and the replay didn't (because
2802 * replaying a ping timeout invariably gets things wrong).
2803 */
2804 DelServer(sNode, 0, NULL);
2805 }
2806
2807 switch (strlen(numeric)) {
2808 case 5: slen = 2; mlen = 3; break;
2809 case 4: slen = 1; mlen = 3; break;
2810 case 3: slen = 1; mlen = 2; break;
2811 default:
2812 log_module(MAIN_LOG, LOG_ERROR, "AddServer(\"%s\", \"%s\", ...): Numeric %s has invalid length.", uplink->name, name, numeric);
2813 return NULL;
2814 }
2815
2816 sNode = calloc(1, sizeof(*sNode));
2817 sNode->uplink = uplink;
2818 safestrncpy(sNode->name, name, sizeof(sNode->name));
2819 sNode->num_mask = base64toint(numeric+slen, mlen);
2820 sNode->hops = hops;
2821 sNode->boot = boot;
2822 sNode->link = link;
2823 strncpy(sNode->numeric, numeric, slen);
2824 safestrncpy(sNode->description, description, sizeof(sNode->description));
2825 sNode->users = calloc(sNode->num_mask+1, sizeof(*sNode->users));
2826 serverList_init(&sNode->children);
2827 if (sNode->uplink) {
2828 /* uplink may be NULL if we're just building ourself */
2829 serverList_append(&sNode->uplink->children, sNode);
2830 }
2831 servers_num[base64toint(numeric, slen)] = sNode;
2832 dict_insert(servers, sNode->name, sNode);
2833 return sNode;
2834}
2835
2836void DelServer(struct server* serv, int announce, const char *message)
2837{
2838 unsigned int i;
2839
2840 /* If we receive an ERROR command before the SERVER
2841 * command a NULL server can be passed */
2842 if (!serv)
2843 return;
2844
2845 /* Hrm, what's the right way to SQUIT some other server?
2846 * (This code is only to handle killing juped servers.) */
2847 if (announce && (serv->uplink == self) && (serv != self->uplink))
2848 irc_squit(serv, message, NULL);
2849
2850 /* must recursively remove servers linked to this one first */
2851 for (i=serv->children.used;i>0;)
2852 if (serv->children.list[--i] != self)
2853 DelServer(serv->children.list[i], false, NULL);
2854
2855 /* clean up server's user hash tables */
2856 for (i=0;i<=serv->num_mask;i++)
2857 if (serv->users[i])
2858 DelUser(serv->users[i], NULL, false, "server delinked");
2859
2860 /* delete server */
2861 if (serv->uplink)
2862 serverList_remove(&serv->uplink->children, serv);
2863 if (serv == self->uplink)
2864 self->uplink = NULL;
2865 servers_num[base64toint(serv->numeric, strlen(serv->numeric))] = NULL;
2866 dict_remove(servers, serv->name);
2867 serverList_clean(&serv->children);
2868 free(serv->users);
2869 free(serv);
2870}
2871
2872struct userNode *
0f6fe38c 2873AddService(const char *nick, const char *modes, const char *desc, const char *hostname)
d76ed9a9 2874{
2875 char numeric[COMBO_NUMERIC_LEN+1];
2876 int local_num = get_local_numeric();
2877 time_t timestamp = now;
2878 struct userNode *old_user = GetUserH(nick);
2879
2880 if (old_user) {
2881 if (IsLocal(old_user))
2882 return old_user;
2883 timestamp = old_user->timestamp - 1;
2884 }
2885 if (local_num == -1) {
2886 log_module(MAIN_LOG, LOG_ERROR, "Unable to allocate numnick for service %s", nick);
2887 return 0;
2888 }
2889 if (!hostname)
2890 hostname = self->name;
2891 make_numeric(self, local_num, numeric);
0f6fe38c 2892 /* TODO: Make these modes part of the conf file */
2893 return AddUser(self, nick, nick, hostname, modes ? modes : "+oik", numeric, desc, now, "AAAAAA");
d76ed9a9 2894}
2895
2896struct userNode *
2897AddClone(const char *nick, const char *ident, const char *hostname, const char *desc)
2898{
2899 char numeric[COMBO_NUMERIC_LEN+1];
2900 int local_num = get_local_numeric();
2901 time_t timestamp = now;
2902 struct userNode *old_user = GetUserH(nick);
2903
2904 if (old_user) {
2905 if (IsLocal(old_user))
2906 return old_user;
2907 timestamp = old_user->timestamp - 1;
2908 }
2909 if (local_num == -1) {
2910 log_module(MAIN_LOG, LOG_ERROR, "Unable to allocate numnick for clone %s", nick);
2911 return 0;
2912 }
2913 make_numeric(self, local_num, numeric);
2914 return AddUser(self, nick, ident, hostname, "+i", numeric, desc, timestamp, "AAAAAA");
2915}
2916
2917int
2918is_valid_nick(const char *nick) {
ec1a68c8 2919 unsigned int ii;
d76ed9a9 2920 /* IRC has some of The Most Fucked-Up ideas about character sets
2921 * in the world.. */
2922 if (!isalpha(*nick) && !strchr("{|}~[\\]^_`", *nick))
2923 return 0;
ec1a68c8 2924 for (ii = 0; nick[ii]; ++ii)
2925 if (!isalnum(nick[ii]) && !strchr("{|}~[\\]^-_`", nick[ii]))
d76ed9a9 2926 return 0;
2927 if (strlen(nick) > nicklen)
2928 return 0;
2929 return 1;
2930}
2931
2932static struct userNode*
2933AddUser(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)
2934{
2935 struct userNode *oldUser, *uNode;
0f6fe38c 2936 unsigned int n, ignore_user;
412b1e16 2937 char *tstr;
2938 int type;
d76ed9a9 2939
2940 if ((strlen(numeric) < 3) || (strlen(numeric) > 5)) {
2941 log_module(MAIN_LOG, LOG_WARNING, "AddUser(%p, %s, ...): numeric %s wrong length!", uplink, nick, numeric);
2942 return NULL;
2943 }
2944
2945 if (!uplink) {
2946 log_module(MAIN_LOG, LOG_WARNING, "AddUser(%p, %s, ...): server for numeric %s doesn't exist!", uplink, nick, numeric);
2947 return NULL;
2948 }
2949
2950 if (uplink != GetServerN(numeric)) {
2951 log_module(MAIN_LOG, LOG_WARNING, "AddUser(%p, %s, ...): server for numeric %s differs from nominal uplink %s.", uplink, nick, numeric, uplink->name);
2952 return NULL;
2953 }
2954
0f6fe38c 2955 if (!is_valid_nick(nick)) {
d76ed9a9 2956 log_module(MAIN_LOG, LOG_WARNING, "AddUser(%p, %s, ...): invalid nickname detected.", uplink, nick);
2957 return NULL;
2958 }
2959
2960 ignore_user = 0;
2961 if ((oldUser = GetUserH(nick))) {
2962 if (IsLocal(oldUser) && (IsService(oldUser) || IsPersistent(oldUser))) {
2963 /* The service should collide the new user off. */
2964 oldUser->timestamp = timestamp - 1;
2965 irc_user(oldUser);
2966 }
2967 if (oldUser->timestamp > timestamp) {
2968 /* "Old" user is really newer; remove them */
2969 DelUser(oldUser, 0, 1, "Overruled by older nick");
2970 } else {
2971 /* User being added is too new; do not add them to
2972 * clients, but do add them to the server's list, since it
2973 * will send a KILL and QUIT soon. */
2974 ignore_user = 1;
2975 }
2976 }
2977
2978 /* create new usernode and set all values */
2979 uNode = calloc(1, sizeof(*uNode));
2980 uNode->nick = strdup(nick);
2981 safestrncpy(uNode->ident, ident, sizeof(uNode->ident));
2982 safestrncpy(uNode->info, userinfo, sizeof(uNode->info));
2983 safestrncpy(uNode->hostname, hostname, sizeof(uNode->hostname));
2984 safestrncpy(uNode->numeric, numeric, sizeof(uNode->numeric));
2f61d1d7 2985 irc_p10_pton(&uNode->ip, realip);
37ef8ee3 2986
412b1e16 2987 tstr = conf_get_data("server/type", RECDB_QSTRING);
2988 type = atoi(tstr);
2989 if (type > 6) {
2990 if (irc_in_addr_is_ipv4(uNode->ip)) {
2991 make_virtip((char*)irc_ntoa(&uNode->ip), (char*)irc_ntoa(&uNode->ip), uNode->cryptip);
2992 make_virthost((char*)irc_ntoa(&uNode->ip), uNode->hostname, uNode->crypthost);
2993 } else if (irc_in_addr_is_ipv6(uNode->ip)) {
2994 make_ipv6virthost((char*)irc_ntoa(&uNode->ip), uNode->hostname, uNode->crypthost);
2995 }
68b75482 2996 }
37ef8ee3 2997
f16ad9e7 2998 if (!uNode->crypthost && uNode->cryptip)
2999 snprintf(uNode->crypthost, sizeof(uNode->crypthost), "%s", strdup(uNode->cryptip));
3000
d76ed9a9 3001 uNode->timestamp = timestamp;
3002 modeList_init(&uNode->channels);
3003 uNode->uplink = uplink;
3004 if (++uNode->uplink->clients > uNode->uplink->max_clients) {
3005 uNode->uplink->max_clients = uNode->uplink->clients;
3006 }
3007 uNode->num_local = base64toint(numeric+strlen(uNode->uplink->numeric), 3) & uNode->uplink->num_mask;
3008 uNode->uplink->users[uNode->num_local] = uNode;
3009 mod_usermode(uNode, modes);
21f6caee 3010
3011 set_geoip_info(uNode);
3012
d76ed9a9 3013 if (ignore_user)
3014 return uNode;
3015
3016 dict_insert(clients, uNode->nick, uNode);
3017 if (dict_size(clients) > max_clients) {
3018 max_clients = dict_size(clients);
3019 max_clients_time = now;
3020 }
3021 if (IsLocal(uNode))
3022 irc_user(uNode);
3023 for (n=0; n<nuf_used; n++)
3024 if (nuf_list[n](uNode))
3025 break;
3fdd6a74 3026
3027 if ((uNode->loc == 1) && (uNode->handle_info))
3028 send_func_list(uNode);
3029
d76ed9a9 3030 return uNode;
3031}
3032
3033/* removes user from it's server's hash table and nick hash table */
3034void
3035DelUser(struct userNode* user, struct userNode *killer, int announce, const char *why)
3036{
3037 unsigned int n;
3038
ec1a68c8 3039 verify(user);
3040
d76ed9a9 3041 /* mark them as dead, in case anybody cares */
3042 user->dead = 1;
3043
ac3bdc8d 3044 /* remove pending adduser commands */
3045 wipe_adduser_pending(NULL, user);
3046
d76ed9a9 3047 /* remove user from all channels */
3048 while (user->channels.used > 0)
0f6fe38c 3049 DelChannelUser(user, user->channels.list[user->channels.used-1]->channel, false, 0);
d76ed9a9 3050
3051 /* Call these in reverse order so ChanServ can update presence
3052 information before NickServ nukes the handle_info. */
3053 for (n = duf_used; n > 0; )
3054 duf_list[--n](user, killer, why);
3055
3056 user->uplink->clients--;
3057 user->uplink->users[user->num_local] = NULL;
3058 if (IsOper(user))
3059 userList_remove(&curr_opers, user);
3060 /* remove from global dictionary, but not if after a collide */
3061 if (user == dict_find(clients, user->nick, NULL))
3062 dict_remove(clients, user->nick);
3063
3064 if (IsInvisible(user))
3065 invis_clients--;
3066
3067 if (announce) {
3068 if (IsLocal(user))
3069 irc_quit(user, why);
3070 else
3071 irc_kill(killer, user, why);
3072 }
3073
3074 modeList_clean(&user->channels);
0e08a8e0 3075
3076 /* Clean up version data */
3077 if(user->version_reply) {
3078 free(user->version_reply);
3079 user->version_reply = NULL;
3080 }
3081
2c00fbc2 3082 /* clean up mark */
3083 if(user->mark) {
3084 free(user->mark);
3085 user->mark = NULL;
3086 }
3087
0e08a8e0 3088 /* clean up geoip data if any */
3089 if(user->country_code) free(user->country_code);
3090 if(user->city) free(user->city);
3091 if(user->region) free(user->region);
3092 if(user->postal_code) free(user->postal_code);
3093
d76ed9a9 3094 /* We don't free them, in case we try to privmsg them or something
3095 * (like when a stupid oper kills themself). We just put them onto
3096 * a list of clients that get freed after processing each line.
3097 */
3098 if (dead_users.size)
3099 userList_append(&dead_users, user);
3100 else
3101 free_user(user);
3102}
3103
697f4c9a 3104static void call_oper_funcs(struct userNode *user);
3105
d76ed9a9 3106void mod_usermode(struct userNode *user, const char *mode_change) {
d76ed9a9 3107 int add = 1;
3108 const char *word = mode_change;
3109
3110 if (!user || !mode_change)
3111 return;
ec311f39 3112
3113 call_user_mode_funcs(user, mode_change);
3114
a32da4c7 3115 while (*word != ' ' && *word) word++;
3116 while (*word == ' ') word++;
d76ed9a9 3117 while (1) {
3118#define do_user_mode(FLAG) do { if (add) user->modes |= FLAG; else user->modes &= ~FLAG; } while (0)
3119 switch (*mode_change++) {
3120 case 0: case ' ': return;
3121 case '+': add = 1; break;
3122 case '-': add = 0; break;
3123 case 'o':
d76ed9a9 3124 if (add) {
ec0120d1 3125 if(!IsOper(user)) { /* Dont re-oper an oper */
3126 userList_append(&curr_opers, user);
3127 call_oper_funcs(user);
3128 }
d76ed9a9 3129 } else {
3130 userList_remove(&curr_opers, user);
3131 }
3d0b24ce 3132 do_user_mode(FLAGS_OPER);
d76ed9a9 3133 break;
3134 case 'O': do_user_mode(FLAGS_LOCOP); break;
3135 case 'i': do_user_mode(FLAGS_INVISIBLE);
3136 if (add)
3137 invis_clients++;
3138 else
3139 invis_clients--;
3140 break;
3141 case 'w': do_user_mode(FLAGS_WALLOP); break;
3142 case 's': do_user_mode(FLAGS_SERVNOTICE); break;
3143 case 'd': do_user_mode(FLAGS_DEAF); break;
3144 case 'k': do_user_mode(FLAGS_SERVICE); break;
3145 case 'g': do_user_mode(FLAGS_GLOBAL); break;
cf8bedff 3146 case 'B': do_user_mode(FLAGS_BOT); break;
3147 case 'n': do_user_mode(FLAGS_HIDECHANS); break;
3148 case 'I': do_user_mode(FLAGS_HIDEIDLE); break;
3149 case 'X': do_user_mode(FLAGS_XTRAOP); break;
412b1e16 3150 case 'C': do_user_mode(FLAGS_CLOAKHOST);
3151 if (*word) {
3152 char cloakhost[MAXLEN];
3153 unsigned int ii;
3154 for (ii=0; (*word != ' ') && (*word != '\0'); )
3155 cloakhost[ii++] = *word++;
3156 cloakhost[ii] = 0;
3157 while (*word == ' ')
3158 word++;
3159 safestrncpy(user->crypthost, cloakhost, sizeof(user->crypthost));
3160 }
3161 break;
3162 case 'c': do_user_mode(FLAGS_CLOAKIP);
3163 if (*word) {
3164 char cloakip[MAXLEN];
3165 unsigned int ii;
3166 for (ii=0; (*word != ' ') && (*word != '\0'); )
3167 cloakip[ii++] = *word++;
3168 cloakip[ii] = 0;
3169 while (*word == ' ')
3170 word++;
3171 safestrncpy(user->cryptip, cloakip, sizeof(user->cryptip));
3172 }
3173 break;
cf8bedff 3174 // sethost - reed/apples
3175 // case 'h': do_user_mode(FLAGS_HELPER); break;
3176 // I check if there's an 'h' in the first part, and if there,
3177 // then everything after the space becomes their new host.
182dd032 3178 case 'h': do_user_mode(FLAGS_SETHOST);
3179 if (*word) {
3180 char sethost[MAXLEN];
3181 unsigned int ii;
3182 for (ii=0; (*word != ' ') && (*word != '\0'); )
3183 sethost[ii++] = *word++;
3184 sethost[ii] = 0;
3185 while (*word == ' ')
3186 word++;
3187 safestrncpy(user->sethost, sethost, sizeof(user->sethost));
3188 }
3189 break;
d76ed9a9 3190 case 'x': do_user_mode(FLAGS_HIDDEN_HOST); break;
3191 case 'r':
3192 if (*word) {
3193 char tag[MAXLEN];
3194 unsigned int ii;
3195 for (ii=0; (*word != ' ') && (*word != '\0'); )
3196 tag[ii++] = *word++;
3197 tag[ii] = 0;
3198 while (*word == ' ')
3199 word++;
3200 call_account_func(user, tag);
3201 }
3202 break;
3203 case 'f':
3204 if (*word) {
3205 char host[MAXLEN];
3206 unsigned int ii;
3207 for (ii=0; (*word != ' ') && (*word != '\0'); )
3208 host[ii++] = *word++;
3209 host[ii] = 0;
3210 while (*word == ' ')
3211 word++;
3212 assign_fakehost(user, host, 0);
3213 }
3214 break;
3215 }
3216#undef do_user_mode
3217 }
3218}
3219
3220struct mod_chanmode *
2f61d1d7 3221mod_chanmode_parse(struct chanNode *channel, char **modes, unsigned int argc, unsigned int flags, short base_oplevel)
d76ed9a9 3222{
3223 struct mod_chanmode *change;
3224 unsigned int ii, in_arg, ch_arg, add;
3225
3226 if (argc == 0)
3227 return NULL;
3228 if (!(change = mod_chanmode_alloc(argc - 1)))
3229 return NULL;
3230
3231 for (ii = ch_arg = 0, in_arg = add = 1;
3232 (modes[0][ii] != '\0') && (modes[0][ii] != ' ');
3233 ++ii) {
3234 switch (modes[0][ii]) {
3235 case '+':
3236 add = 1;
3237 break;
3238 case '-':
3239 add = 0;
3240 break;
3241#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)
3242 case 'C': do_chan_mode(MODE_NOCTCPS); break;
3243 case 'D': do_chan_mode(MODE_DELAYJOINS); break;
3244 case 'c': do_chan_mode(MODE_NOCOLORS); break;
3245 case 'i': do_chan_mode(MODE_INVITEONLY); break;
3246 case 'm': do_chan_mode(MODE_MODERATED); break;
3247 case 'n': do_chan_mode(MODE_NOPRIVMSGS); break;
3248 case 'p': do_chan_mode(MODE_PRIVATE); break;
3249 case 'r': do_chan_mode(MODE_REGONLY); break;
3250 case 's': do_chan_mode(MODE_SECRET); break;
3251 case 't': do_chan_mode(MODE_TOPICLIMIT); break;
d0cb2fb6 3252 case 'S': do_chan_mode(MODE_STRIPCOLOR); break;
3253 case 'M': do_chan_mode(MODE_MODUNREG); break;
3254 case 'N': do_chan_mode(MODE_NONOTICE); break;
3255 case 'Q': do_chan_mode(MODE_NOQUITMSGS); break;
3256 case 'T': do_chan_mode(MODE_NOAMSG); break;
3257 case 'O': do_chan_mode(MODE_OPERSONLY); break;
4648ded2 3258 case 'a': do_chan_mode(MODE_ADMINSONLY); break;
c8ca69a0 3259 case 'Z': do_chan_mode(MODE_SSLONLY); break;
88c7cb10 3260 case 'L': do_chan_mode(MODE_HIDEMODE); break;
ec1a68c8 3261 case 'z':
3262 if (!(flags & MCP_REGISTERED)) {
3263 do_chan_mode(MODE_REGISTERED);
3264 } else {
3265 mod_chanmode_free(change);
3266 return NULL;
3267 }
3268 break;
d76ed9a9 3269#undef do_chan_mode
3270 case 'l':
3271 if (add) {
3272 if (in_arg >= argc)
3273 goto error;
3274 change->modes_set |= MODE_LIMIT;
3275 change->new_limit = atoi(modes[in_arg++]);
3276 } else {
3277 change->modes_set &= ~MODE_LIMIT;
3278 change->modes_clear |= MODE_LIMIT;
3279 }
3280 break;
3281 case 'k':
3282 if (add) {
3283 if (in_arg >= argc)
3284 goto error;
3285 change->modes_set |= MODE_KEY;
3286 safestrncpy(change->new_key, modes[in_arg++], sizeof(change->new_key));
3287 } else {
3288 change->modes_clear |= MODE_KEY;
3289 if (!(flags & MCP_KEY_FREE)) {
3290 if (in_arg >= argc)
3291 goto error;
3292 in_arg++;
3293 }
3294 }
3295 break;
2f61d1d7 3296 case 'U':
3297 if (add)
3298 {
3299 if (in_arg >= argc)
3300 goto error;
3301 change->modes_set |= MODE_UPASS;
3302 safestrncpy(change->new_upass, modes[in_arg++], sizeof(change->new_upass));
3303 } else {
3304 change->modes_clear |= MODE_UPASS;
3305 if (!(flags & MCP_UPASS_FREE)) {
3306 if (in_arg >= argc)
3307 goto error;
3308 in_arg++;
3309 }
3310 }
3311 break;
3312 case 'A':
3313 if (add) {
3314 if (in_arg >= argc)
3315 goto error;
3316 change->modes_set |= MODE_APASS;
3317 safestrncpy(change->new_apass, modes[in_arg++], sizeof(change->new_apass));
3318 } else {
3319 change->modes_clear |= MODE_APASS;
3320 if (!(flags & MCP_APASS_FREE)) {
3321 if (in_arg >= argc)
3322 goto error;
3323 in_arg++;
3324 }
3325 }
3326 break;
d76ed9a9 3327 case 'b':
3328 if (!(flags & MCP_ALLOW_OVB))
3329 goto error;
3330 if (in_arg >= argc)
3331 goto error;
3332 change->args[ch_arg].mode = MODE_BAN;
3333 if (!add)
3334 change->args[ch_arg].mode |= MODE_REMOVE;
a32da4c7 3335 change->args[ch_arg++].u.hostmask = modes[in_arg++];
d76ed9a9 3336 break;
2aef5f4b 3337 case 'e':
3338 if (!(flags & MCP_ALLOW_OVB))
3339 goto error;
3340 if (in_arg >= argc)
3341 goto error;
3342 change->args[ch_arg].mode = MODE_EXEMPT;
3343 if (!add)
3344 change->args[ch_arg].mode |= MODE_REMOVE;
a32da4c7 3345 change->args[ch_arg++].u.hostmask = modes[in_arg++];
2aef5f4b 3346 break;
55342ce8 3347 case 'o': case 'h': case 'v':
d76ed9a9 3348 {
3349 struct userNode *victim;
2f61d1d7 3350 char *oplevel_str;
3351 int oplevel;
3352
3353 if (in_arg >= argc)
3354 goto error;
3355 oplevel_str = strchr(modes[in_arg], ':');
3356 if (oplevel_str)
3357 {
3358 /* XXYYY M #channel +o XXYYY:<oplevel> */
3359 *oplevel_str++ = '\0';
3360 oplevel = parse_oplevel(oplevel_str);
3361 if (oplevel <= base_oplevel && !(flags & MCP_FROM_SERVER))
3362 oplevel = base_oplevel + 1;
3363 }
3364 else if (channel->modes & MODE_UPASS)
3365 oplevel = base_oplevel + 1;
3366 else
3367 oplevel = -1;
3368
3369 /* Check that oplevel is within bounds. */
3370 if (oplevel > MAXOPLEVEL)
3371 oplevel = MAXOPLEVEL;
3372
d76ed9a9 3373 if (!(flags & MCP_ALLOW_OVB))
3374 goto error;
3375 if (in_arg >= argc)
3376 goto error;
55342ce8 3377
3378 if (modes[0][ii] == 'o')
3379 change->args[ch_arg].mode = MODE_CHANOP;
3380 else if (modes[0][ii] == 'h')
3381 change->args[ch_arg].mode = MODE_HALFOP;
3382 else if (modes[0][ii] == 'v')
3383 change->args[ch_arg].mode = MODE_VOICE;
3384
d76ed9a9 3385 if (!add)
3386 change->args[ch_arg].mode |= MODE_REMOVE;
3387 if (flags & MCP_FROM_SERVER)
3388 victim = GetUserN(modes[in_arg++]);
3389 else
3390 victim = GetUserH(modes[in_arg++]);
3391 if (!victim)
3392 continue;
a32da4c7 3393 if ((change->args[ch_arg].u.member = GetUserMode(channel, victim)))
2f61d1d7 3394 {
3395 /* Apply the oplevel change */
3396 change->args[ch_arg].u.member->oplevel = oplevel;
d76ed9a9 3397 ch_arg++;
2f61d1d7 3398 }
d76ed9a9 3399 break;
3400 }
3401 default:
3402 if (!(flags & MCP_FROM_SERVER))
3403 goto error;
3404 break;
3405 }
3406 }
3407 change->argc = ch_arg; /* in case any turned out to be ignored */
3408 if (change->modes_set & MODE_SECRET) {
3409 change->modes_set &= ~(MODE_PRIVATE);
3410 change->modes_clear |= MODE_PRIVATE;
3411 } else if (change->modes_set & MODE_PRIVATE) {
3412 change->modes_set &= ~(MODE_SECRET);
3413 change->modes_clear |= MODE_SECRET;
3414 }
3415 return change;
3416 error:
3417 mod_chanmode_free(change);
3418 return NULL;
3419}
3420
3421struct chanmode_buffer {
3422 char modes[MAXLEN];
3423 char args[MAXLEN];
3424 struct chanNode *channel;
3425 struct userNode *actor;
3426 unsigned int modes_used;
3427 unsigned int args_used;
3428 size_t chname_len;
3429 unsigned int is_add : 1;
3430 unsigned int is_chanop : 1;
3431};
3432
3433static void
3434mod_chanmode_append(struct chanmode_buffer *buf, char ch, const char *arg)
3435{
3436 size_t arg_len = strlen(arg);
3437 if (buf->modes_used > (MAXMODEPARAMS) ||
3438 buf->modes_used + buf->args_used + buf->chname_len + arg_len > 450) {
3439 memcpy(buf->modes + buf->modes_used, buf->args, buf->args_used);
3440 buf->modes[buf->modes_used + buf->args_used] = '\0';
3441 irc_mode((buf->is_chanop ? buf->actor : NULL), buf->channel, buf->modes);
3442 buf->modes[0] = buf->is_add ? '+' : '-';
3443 buf->modes_used = 1;
3444 buf->args_used = 0;
3445 }
3446 buf->modes[buf->modes_used++] = ch;
3447 buf->args[buf->args_used++] = ' ';
3448 memcpy(buf->args + buf->args_used, arg, arg_len);
3449 buf->args_used += arg_len;
3450}
3451
3452void
3453mod_chanmode_announce(struct userNode *who, struct chanNode *channel, struct mod_chanmode *change)
3454{
3455 struct chanmode_buffer chbuf;
3456 unsigned int arg;
3457 struct modeNode *mn;
3458 char int_buff[32], mode = '\0';
3459
3460 assert(change->argc <= change->alloc_argc);
3461 memset(&chbuf, 0, sizeof(chbuf));
3462 chbuf.channel = channel;
3463 chbuf.actor = who;
3464 chbuf.chname_len = strlen(channel->name);
3465
3466 mn = GetUserMode(channel, who);
3467 if ((mn && (mn->modes & MODE_CHANOP)) || off_channel)
3468 chbuf.is_chanop = 1;
3469
3470 /* First remove modes */
3471 chbuf.is_add = 0;
3472 if (change->modes_clear) {
3473 if (mode != '-')
3474 chbuf.modes[chbuf.modes_used++] = mode = '-';
3475#define DO_MODE_CHAR(BIT, CHAR) if (change->modes_clear & MODE_##BIT) chbuf.modes[chbuf.modes_used++] = CHAR
3476 DO_MODE_CHAR(PRIVATE, 'p');
3477 DO_MODE_CHAR(SECRET, 's');
3478 DO_MODE_CHAR(MODERATED, 'm');
3479 DO_MODE_CHAR(TOPICLIMIT, 't');
3480 DO_MODE_CHAR(INVITEONLY, 'i');
3481 DO_MODE_CHAR(NOPRIVMSGS, 'n');
3482 DO_MODE_CHAR(LIMIT, 'l');
3483 DO_MODE_CHAR(DELAYJOINS, 'D');
3484 DO_MODE_CHAR(REGONLY, 'r');
3485 DO_MODE_CHAR(NOCOLORS, 'c');
3486 DO_MODE_CHAR(NOCTCPS, 'C');
d0cb2fb6 3487 DO_MODE_CHAR(STRIPCOLOR, 'S');
3488 DO_MODE_CHAR(MODUNREG, 'M');
3489 DO_MODE_CHAR(NONOTICE, 'N');
3490 DO_MODE_CHAR(NOQUITMSGS, 'Q');
3491 DO_MODE_CHAR(NOAMSG, 'T');
3492 DO_MODE_CHAR(OPERSONLY, 'O');
ec1a68c8 3493 DO_MODE_CHAR(REGISTERED, 'z');
c8ca69a0 3494 DO_MODE_CHAR(SSLONLY, 'Z');
88c7cb10 3495 DO_MODE_CHAR(HIDEMODE, 'L');
d76ed9a9 3496#undef DO_MODE_CHAR
3497 if (change->modes_clear & channel->modes & MODE_KEY)
3498 mod_chanmode_append(&chbuf, 'k', channel->key);
2f61d1d7 3499 if (change->modes_clear & channel->modes & MODE_UPASS)
3500 mod_chanmode_append(&chbuf, 'U', channel->upass);
0f6fe38c 3501 if (change->modes_clear * channel->modes & MODE_APASS)
2f61d1d7 3502 mod_chanmode_append(&chbuf, 'A', channel->apass);
d76ed9a9 3503 }
3504 for (arg = 0; arg < change->argc; ++arg) {
3505 if (!(change->args[arg].mode & MODE_REMOVE))
3506 continue;
3507 if (mode != '-')
3508 chbuf.modes[chbuf.modes_used++] = mode = '-';
3509 switch (change->args[arg].mode & ~MODE_REMOVE) {
3510 case MODE_BAN:
a32da4c7 3511 mod_chanmode_append(&chbuf, 'b', change->args[arg].u.hostmask);
d76ed9a9 3512 break;
2aef5f4b 3513 case MODE_EXEMPT:
a32da4c7 3514 mod_chanmode_append(&chbuf, 'e', change->args[arg].u.hostmask);
2aef5f4b 3515 break;
d76ed9a9 3516 default:
3517 if (change->args[arg].mode & MODE_CHANOP)
a32da4c7 3518 mod_chanmode_append(&chbuf, 'o', change->args[arg].u.member->user->numeric);
55342ce8 3519 if (change->args[arg].mode & MODE_HALFOP)
a32da4c7 3520 mod_chanmode_append(&chbuf, 'h', change->args[arg].u.member->user->numeric);
d76ed9a9 3521 if (change->args[arg].mode & MODE_VOICE)
a32da4c7 3522 mod_chanmode_append(&chbuf, 'v', change->args[arg].u.member->user->numeric);
d76ed9a9 3523 break;
3524 }
3525 }
3526
3527 /* Then set them */
3528 chbuf.is_add = 1;
3529 if (change->modes_set) {
3530 if (mode != '+')
3531 chbuf.modes[chbuf.modes_used++] = mode = '+';
3532#define DO_MODE_CHAR(BIT, CHAR) if (change->modes_set & MODE_##BIT) chbuf.modes[chbuf.modes_used++] = CHAR
3533 DO_MODE_CHAR(PRIVATE, 'p');
3534 DO_MODE_CHAR(SECRET, 's');
3535 DO_MODE_CHAR(MODERATED, 'm');
3536 DO_MODE_CHAR(TOPICLIMIT, 't');
3537 DO_MODE_CHAR(INVITEONLY, 'i');
3538 DO_MODE_CHAR(NOPRIVMSGS, 'n');
3539 DO_MODE_CHAR(DELAYJOINS, 'D');
3540 DO_MODE_CHAR(REGONLY, 'r');
3541 DO_MODE_CHAR(NOCOLORS, 'c');
3542 DO_MODE_CHAR(NOCTCPS, 'C');
d0cb2fb6 3543 DO_MODE_CHAR(STRIPCOLOR, 'S');
3544 DO_MODE_CHAR(MODUNREG, 'M');
3545 DO_MODE_CHAR(NONOTICE, 'N');
3546 DO_MODE_CHAR(NOQUITMSGS, 'Q');
3547 DO_MODE_CHAR(NOAMSG, 'T');
3548 DO_MODE_CHAR(OPERSONLY, 'O');
ec1a68c8 3549 DO_MODE_CHAR(REGISTERED, 'z');
c8ca69a0 3550 DO_MODE_CHAR(SSLONLY, 'Z');
88c7cb10 3551 DO_MODE_CHAR(HIDEMODE, 'L');
d76ed9a9 3552#undef DO_MODE_CHAR
3553 if(change->modes_set & MODE_KEY)
3554 mod_chanmode_append(&chbuf, 'k', change->new_key);
2f61d1d7 3555 if (change->modes_set & MODE_UPASS)
3556 mod_chanmode_append(&chbuf, 'U', change->new_upass);
3557 if (change->modes_set & MODE_APASS)
3558 mod_chanmode_append(&chbuf, 'A', change->new_apass);
d76ed9a9 3559 if(change->modes_set & MODE_LIMIT) {
3560 sprintf(int_buff, "%d", change->new_limit);
3561 mod_chanmode_append(&chbuf, 'l', int_buff);
3562 }
3563 }
3564 for (arg = 0; arg < change->argc; ++arg) {
3565 if (change->args[arg].mode & MODE_REMOVE)
3566 continue;
3567 if (mode != '+')
3568 chbuf.modes[chbuf.modes_used++] = mode = '+';
3569 switch (change->args[arg].mode) {
3570 case MODE_BAN:
a32da4c7 3571 mod_chanmode_append(&chbuf, 'b', change->args[arg].u.hostmask);
d76ed9a9 3572 break;
2aef5f4b 3573 case MODE_EXEMPT:
a32da4c7 3574 mod_chanmode_append(&chbuf, 'e', change->args[arg].u.hostmask);
2aef5f4b 3575 break;
d76ed9a9 3576 default:
3577 if (change->args[arg].mode & MODE_CHANOP)
a32da4c7 3578 mod_chanmode_append(&chbuf, 'o', change->args[arg].u.member->user->numeric);
55342ce8 3579 if (change->args[arg].mode & MODE_HALFOP)
a32da4c7 3580 mod_chanmode_append(&chbuf, 'h', change->args[arg].u.member->user->numeric);
d76ed9a9 3581 if (change->args[arg].mode & MODE_VOICE)
a32da4c7 3582 mod_chanmode_append(&chbuf, 'v', change->args[arg].u.member->user->numeric);
d76ed9a9 3583 break;
3584 }
3585 }
3586
3587 /* Flush the buffer and apply changes locally */
3588 if (chbuf.modes_used > 0) {
3589 memcpy(chbuf.modes + chbuf.modes_used, chbuf.args, chbuf.args_used);
3590 chbuf.modes[chbuf.modes_used + chbuf.args_used] = '\0';
3591 irc_mode((chbuf.is_chanop ? chbuf.actor : NULL), chbuf.channel, chbuf.modes);
3592 }
3593 mod_chanmode_apply(who, channel, change);
3594}
3595
3596char *
3597mod_chanmode_format(struct mod_chanmode *change, char *outbuff)
3598{
3599 unsigned int used = 0;
3600 assert(change->argc <= change->alloc_argc);
3601 if (change->modes_clear) {
3602 outbuff[used++] = '-';
3603#define DO_MODE_CHAR(BIT, CHAR) if (change->modes_clear & MODE_##BIT) outbuff[used++] = CHAR
3604 DO_MODE_CHAR(PRIVATE, 'p');
3605 DO_MODE_CHAR(SECRET, 's');
3606 DO_MODE_CHAR(MODERATED, 'm');
3607 DO_MODE_CHAR(TOPICLIMIT, 't');
3608 DO_MODE_CHAR(INVITEONLY, 'i');
3609 DO_MODE_CHAR(NOPRIVMSGS, 'n');
3610 DO_MODE_CHAR(LIMIT, 'l');
3611 DO_MODE_CHAR(KEY, 'k');
2f61d1d7 3612 DO_MODE_CHAR(UPASS, 'U');
3613 DO_MODE_CHAR(APASS, 'A');
d76ed9a9 3614 DO_MODE_CHAR(DELAYJOINS, 'D');
3615 DO_MODE_CHAR(REGONLY, 'r');
3616 DO_MODE_CHAR(NOCOLORS, 'c');
3617 DO_MODE_CHAR(NOCTCPS, 'C');
d0cb2fb6 3618 DO_MODE_CHAR(STRIPCOLOR, 'S');
3619 DO_MODE_CHAR(MODUNREG, 'M');
3620 DO_MODE_CHAR(NONOTICE, 'N');
3621 DO_MODE_CHAR(NOQUITMSGS, 'Q');
3622 DO_MODE_CHAR(NOAMSG, 'T');
3623 DO_MODE_CHAR(OPERSONLY, 'O');
ec1a68c8 3624 DO_MODE_CHAR(REGISTERED, 'z');
c8ca69a0 3625 DO_MODE_CHAR(SSLONLY, 'Z');
88c7cb10 3626 DO_MODE_CHAR(HIDEMODE, 'L');
d76ed9a9 3627#undef DO_MODE_CHAR
3628 }
3629 if (change->modes_set) {
3630 outbuff[used++] = '+';
3631#define DO_MODE_CHAR(BIT, CHAR) if (change->modes_set & MODE_##BIT) outbuff[used++] = CHAR
3632 DO_MODE_CHAR(PRIVATE, 'p');
3633 DO_MODE_CHAR(SECRET, 's');
3634 DO_MODE_CHAR(MODERATED, 'm');
3635 DO_MODE_CHAR(TOPICLIMIT, 't');
3636 DO_MODE_CHAR(INVITEONLY, 'i');
3637 DO_MODE_CHAR(NOPRIVMSGS, 'n');
3638 DO_MODE_CHAR(DELAYJOINS, 'D');
3639 DO_MODE_CHAR(REGONLY, 'r');
3640 DO_MODE_CHAR(NOCOLORS, 'c');
3641 DO_MODE_CHAR(NOCTCPS, 'C');
d0cb2fb6 3642 DO_MODE_CHAR(STRIPCOLOR, 'S');
3643 DO_MODE_CHAR(MODUNREG, 'M');
3644 DO_MODE_CHAR(NONOTICE, 'N');
3645 DO_MODE_CHAR(NOQUITMSGS, 'Q');
3646 DO_MODE_CHAR(NOAMSG, 'T');
3647 DO_MODE_CHAR(OPERSONLY, 'O');
ec1a68c8 3648 DO_MODE_CHAR(REGISTERED, 'z');
c8ca69a0 3649 DO_MODE_CHAR(SSLONLY, 'Z');
88c7cb10 3650 DO_MODE_CHAR(HIDEMODE, 'L');
d76ed9a9 3651#undef DO_MODE_CHAR
2f61d1d7 3652 switch (change->modes_set & (MODE_KEY|MODE_LIMIT|MODE_APASS|MODE_UPASS)) {
3653 /* Doing this implementation has been a pain in the arse, I hope I didn't forget a possible combination */
3654 case MODE_KEY|MODE_LIMIT|MODE_APASS|MODE_UPASS:
3655 used += sprintf(outbuff+used, "lkAU %d %s %s %s", change->new_limit, change->new_key,change->new_apass, change->new_upass);
3656 break;
3657
3658 case MODE_KEY|MODE_LIMIT|MODE_APASS:
3659 used += sprintf(outbuff+used, "lkA %d %s %s", change->new_limit, change->new_key, change->new_apass);
3660 break;
3661 case MODE_KEY|MODE_LIMIT|MODE_UPASS:
3662 used += sprintf(outbuff+used, "lkU %d %s %s", change->new_limit, change->new_key, change->new_upass);
3663 break;
3664 case MODE_KEY|MODE_APASS|MODE_UPASS:
3665 used += sprintf(outbuff+used, "kAU %s %s %s", change->new_key, change->new_apass, change->new_upass);
3666 break;
3667
3668 case MODE_KEY|MODE_APASS:
3669 used += sprintf(outbuff+used, "kA %s %s", change->new_key, change->new_apass);
3670 break;
3671 case MODE_KEY|MODE_UPASS:
3672 used += sprintf(outbuff+used, "kU %s %s", change->new_key, change->new_upass);
3673 break;
d76ed9a9 3674 case MODE_KEY|MODE_LIMIT:
3675 used += sprintf(outbuff+used, "lk %d %s", change->new_limit, change->new_key);
3676 break;
2f61d1d7 3677
3678 case MODE_LIMIT|MODE_UPASS:
3679 used += sprintf(outbuff+used, "lU %d %s", change->new_limit, change->new_upass);
3680 break;
3681 case MODE_LIMIT|MODE_APASS:
3682 used += sprintf(outbuff+used, "lA %d %s", change->new_limit, change->new_apass);
3683 break;
3684 case MODE_APASS|MODE_UPASS:
3685 used += sprintf(outbuff+used, "AU %s %s", change->new_apass, change->new_upass);
3686
3687 case MODE_LIMIT|MODE_APASS|MODE_UPASS:
3688 used += sprintf(outbuff+used, "lAU %d %s %s", change->new_limit, change->new_apass, change->new_upass);
3689 break;
3690
3691 case MODE_APASS:
3692 used += sprintf(outbuff+used, "A %s", change->new_apass);
3693 break;
3694 case MODE_UPASS:
3695 used += sprintf(outbuff+used, "U %s", change->new_upass);
3696 break;
d76ed9a9 3697 case MODE_KEY:
3698 used += sprintf(outbuff+used, "k %s", change->new_key);
3699 break;
3700 case MODE_LIMIT:
3701 used += sprintf(outbuff+used, "l %d", change->new_limit);
3702 break;
3703 }
3704 }
3705 outbuff[used] = 0;
3706 return outbuff;
3707}
3708
3709static int
3710clear_chanmode(struct chanNode *channel, const char *modes)
3711{
3712 unsigned int remove;
3713
3714 for (remove = 0; *modes; modes++) {
3715 switch (*modes) {
3716 case 'o': remove |= MODE_CHANOP; break;
55342ce8 3717 case 'h': remove |= MODE_HALFOP; break;
d76ed9a9 3718 case 'v': remove |= MODE_VOICE; break;
3719 case 'p': remove |= MODE_PRIVATE; break;
3720 case 's': remove |= MODE_SECRET; break;
3721 case 'm': remove |= MODE_MODERATED; break;
3722 case 't': remove |= MODE_TOPICLIMIT; break;
3723 case 'i': remove |= MODE_INVITEONLY; break;
3724 case 'n': remove |= MODE_NOPRIVMSGS; break;
3725 case 'k':
3726 remove |= MODE_KEY;
3727 channel->key[0] = '\0';
3728 break;
2f61d1d7 3729 case 'A':
3730 remove |= MODE_APASS;
3731 channel->apass[0] = '\0';
3732 break;
3733 case 'U':
3734 remove |= MODE_UPASS;
3735 channel->upass[0] = '\0';
3736 break;
d76ed9a9 3737 case 'l':
3738 remove |= MODE_LIMIT;
3739 channel->limit = 0;
3740 break;
3741 case 'b': remove |= MODE_BAN; break;
2aef5f4b 3742 case 'e': remove |= MODE_EXEMPT; break;
d76ed9a9 3743 case 'D': remove |= MODE_DELAYJOINS; break;
3744 case 'r': remove |= MODE_REGONLY; break;
850bf0cf 3745 case 'c': remove |= MODE_NOCOLORS; break;
d76ed9a9 3746 case 'C': remove |= MODE_NOCTCPS; break;
d0cb2fb6 3747 case 'S': remove |= MODE_STRIPCOLOR; break;
3748 case 'M': remove |= MODE_MODUNREG; break;
3749 case 'N': remove |= MODE_NONOTICE; break;
3750 case 'Q': remove |= MODE_NOQUITMSGS; break;
3751 case 'T': remove |= MODE_NOAMSG; break;
3752 case 'O': remove |= MODE_OPERSONLY; break;
4648ded2 3753 case 'a': remove |= MODE_ADMINSONLY; break;
ec1a68c8 3754 case 'z': remove |= MODE_REGISTERED; break;
c8ca69a0 3755 case 'Z': remove |= MODE_SSLONLY; break;
88c7cb10 3756 case 'L': remove |= MODE_HIDEMODE; break;
d76ed9a9 3757 }
3758 }
3759
3760 if (!remove)
3761 return 1;
3762
3763 /* Remove simple modes. */
3764 channel->modes &= ~remove;
3765
3766 /* If removing bans, kill 'em all. */
3767 if ((remove & MODE_BAN) && channel->banlist.used) {
3768 unsigned int i;
3769 for (i=0; i<channel->banlist.used; i++)
3770 free(channel->banlist.list[i]);
3771 channel->banlist.used = 0;
3772 }
3773
2aef5f4b 3774 /* If removing exempts, kill 'em all. */
3775 if ((remove & MODE_EXEMPT) && channel->exemptlist.used) {
3776 unsigned int i;
3777 for (i=0; i<channel->exemptlist.used; i++)
3778 free(channel->exemptlist.list[i]);
3779 channel->exemptlist.used = 0;
3780 }
3781
d76ed9a9 3782 /* Remove member modes. */
55342ce8 3783 if ((remove & (MODE_CHANOP | MODE_HALFOP | MODE_VOICE)) && channel->members.used) {
3784 int mask = ~(remove & (MODE_CHANOP | MODE_HALFOP | MODE_VOICE));
d76ed9a9 3785 unsigned int i;
3786
3787 for (i = 0; i < channel->members.used; i++)
3788 channel->members.list[i]->modes &= mask;
3789 }
3790
3791 return 1;
3792}
3793
3794void
3795reg_privmsg_func(struct userNode *user, privmsg_func_t handler)
3796{
3797 unsigned int numeric = user->num_local;
3798 if (numeric >= num_privmsg_funcs) {
0f6fe38c 3799 int newnum = numeric + 8;
d76ed9a9 3800 privmsg_funcs = realloc(privmsg_funcs, newnum*sizeof(privmsg_func_t));
0f6fe38c 3801 memset(privmsg_funcs+num_privmsg_funcs, 0, (newnum-num_privmsg_funcs)*sizeof(privmsg_func_t));
d76ed9a9 3802 num_privmsg_funcs = newnum;
3803 }
3804 if (privmsg_funcs[numeric])
3805 log_module(MAIN_LOG, LOG_WARNING, "re-registering new privmsg handler for numeric %d", numeric);
3806 privmsg_funcs[numeric] = handler;
3807}
3808
3809void
0f6fe38c 3810unreg_privmsg_func(struct userNode *user, privmsg_func_t handler)
d76ed9a9 3811{
0f6fe38c 3812 unsigned int x;
3813
3814 if (!user || handler)
d76ed9a9 3815 return; /* this really only works with users */
3816
0f6fe38c 3817 memset(privmsg_funcs+user->num_local, 0, sizeof(privmsg_func_t));
3818
3819 for (x = user->num_local+1; x < num_privmsg_funcs; x++)
3820 memmove(privmsg_funcs+x-1, privmsg_funcs+x, sizeof(privmsg_func_t));
3821
3822 privmsg_funcs = realloc(privmsg_funcs, num_privmsg_funcs*sizeof(privmsg_func_t));
3823 num_privmsg_funcs--;
d76ed9a9 3824}
3825
3826
3827void
3828reg_notice_func(struct userNode *user, privmsg_func_t handler)
3829{
3830 unsigned int numeric = user->num_local;
3831 if (numeric >= num_notice_funcs) {
0f6fe38c 3832 int newnum = numeric + 8;
d76ed9a9 3833 notice_funcs = realloc(notice_funcs, newnum*sizeof(privmsg_func_t));
0f6fe38c 3834 memset(notice_funcs+num_notice_funcs, 0, (newnum-num_notice_funcs)*sizeof(privmsg_func_t));
d76ed9a9 3835 num_notice_funcs = newnum;
3836 }
3837 if (notice_funcs[numeric])
3838 log_module(MAIN_LOG, LOG_WARNING, "re-registering new notice handler for numeric %d", numeric);
3839 notice_funcs[numeric] = handler;
3840}
3841
3842void
0f6fe38c 3843unreg_notice_func(struct userNode *user, privmsg_func_t handler)
d76ed9a9 3844{
0f6fe38c 3845 unsigned int x;
3846
3847 if (!user || handler)
d76ed9a9 3848 return; /* this really only works with users */
3849
0f6fe38c 3850 memset(notice_funcs+user->num_local, 0, sizeof(privmsg_func_t));
3851
3852 for (x = user->num_local+1; x < num_notice_funcs; x++)
3853 memmove(notice_funcs+x-1, notice_funcs+x, sizeof(privmsg_func_t));
3854
3855 memset(notice_funcs+user->num_local, 0, sizeof(privmsg_func_t));
3856 notice_funcs = realloc(notice_funcs, num_notice_funcs*sizeof(privmsg_func_t));
3857 num_notice_funcs--;
d76ed9a9 3858}
3859
3860void
3861reg_oper_func(oper_func_t handler)
3862{
3863 if (of_used == of_size) {
3864 if (of_size) {
3865 of_size <<= 1;
3866 of_list = realloc(of_list, of_size*sizeof(oper_func_t));
3867 } else {
3868 of_size = 8;
3869 of_list = malloc(of_size*sizeof(oper_func_t));
3870 }
3871 }
3872 of_list[of_used++] = handler;
3873}
3874
3875static void
3876call_oper_funcs(struct userNode *user)
3877{
3878 unsigned int n;
3879 if (IsLocal(user))
3880 return;
3881 for (n=0; n<of_used; n++)
3882 of_list[n](user);
3883}
3884
3885static void
3886send_burst(void)
3887{
3888 unsigned int i, hop, max_hop=1;
3889 dict_iterator_t it;
3890
3891 /* burst (juped) servers, closest first (except self, which is sent already) */
3892 for (i=0; i<ArrayLength(servers_num); i++)
3893 if (servers_num[i] && servers_num[i]->hops > max_hop)
3894 max_hop = servers_num[i]->hops;
3895 for (hop=1; hop<=max_hop; hop++) {
3896 for (i=0;i<ArrayLength(servers_num);i++) {
3897 if (servers_num[i]
3898 && (servers_num[i]->hops == hop)
3899 && (servers_num[i] != self->uplink))
3900 irc_server(servers_num[i]);
3901 }
3902 }
3903
3904 /* burst local nicks */
3905 for (i=0; i<=self->num_mask; i++)
3906 if (self->users[i])
3907 irc_user(self->users[i]);
3908
3909 /* build dict of unbursted channel names (just copy existing channels) */
3910 unbursted_channels = dict_new();
3911 for (it = dict_first(channels); it; it = iter_next(it))
3912 dict_insert(unbursted_channels, iter_key(it), iter_data(it));
3913}
2f61d1d7 3914
3915/*
3916 * Oplevel parsing
3917 */
3918static int
3919parse_oplevel(char *str)
3920{
3921 int oplevel = 0;
3922 while (isdigit(*str))
3923 oplevel = oplevel * 10 + *str++ - '0';
3924 return oplevel;
3925}
3926